﻿
function IsEmpty(textBox)
{
    // RegEx to match anyhting but whitespace
    var blankRE=/^\s*$/;

    if(blankRE.test(textBox.value))
    {
        return true;
    }
    else
    {
        return false;
    }
}


function searchOnEnter(e, tb)
{
    var code;

    if (!e) var e = window.event;

    if (e.keyCode) code = e.keyCode;
    else if (e.which) code = e.which;

    // Enter key
    if (code==13) 
    {
        // the event will be handled here
        e.cancelBubble = true;
        if (e.stopPropagation) e.stopPropagation();

        if (tb) 
        {
            if (tb.value == '')
            {
                return false;
            }
            window.location = searchPrefix + 'Default.aspx?query=' + Url.encode(tb.value);
            return false;
        }
    }
    return true;
}


function searchFaqOnEnter(e, tb)
{
    var code;

    if (!e) var e = window.event;

    if (e.keyCode) code = e.keyCode;
    else if (e.which) code = e.which;

    // Enter key
    if (code==13) 
    {
        // the event will be handled here
        e.cancelBubble = true;
        if (e.stopPropagation) e.stopPropagation();

        if (tb) 
        {
            if (tb.value == '')
            {
                return false;
            }
            window.location = searchFaqPrefix + 'Default.aspx?query=' + Url.encode(tb.value);
            return false;
        }
    }
    return true;
}


function clickOnEnter(ctrlID){
	if (event.keyCode == 13){
		var ctrl=document.getElementById(ctrlID);
		if (ctrl!=null){
			event.returnValue=false;
			event.cancel = true;
			ctrl.click();
		}
	}
}


function setFocus(elemId){
	elem = document.getElementById(elemId);
	
	if(elem != null && elem.type != "hidden"){
		elem.focus();
	}
}

/** dynamic content support **/
function SetLinkHref(elmtID, href) {
	if (document.getElementById(elmtID) ) {
		document.getElementById(elmtID).href = href;
	}
}

function SetInnerHTML(elmtID, txt) {
	if (document.getElementById(elmtID) ) {
		document.getElementById(elmtID).innerHTML = txt;
	}
}

function SetVisibility(elmtID, visible) {
	if (document.getElementById(elmtID) ) {
		var visibility = 'hidden';
		if (visible)
			visibility = 'visible';
		
		document.getElementById(elmtID).style.visibility=visibility;
	}
}	

function SetImageSrc(elmtID, src) {
	if (document.getElementById(elmtID) ) {
		var visibility = 'hidden';
		if (!src || src == '') {
			SetVisibility(elmtID, false) ;
		}
		else {
			SetVisibility(elmtID, true);
			document.getElementById(elmtID).src = src;
		}
						
	}
}
/* get/create array from doc element */
function GetArray(elmID, sep ) {
  var retArr;
  var elm = document.getElementById(elmID);
  if (elm && elm.innerHTML != null && elm.innerHTML.indexOf(sep) >= 0  ) {
	retArr = elm.innerHTML.split(sep)
  }
  else
	retArr = new Array();

  return retArr;
}

/**
*
* Image browser scripts ("@ a glance")
**/
var imgBrowserIx = 0;

function prev_image() {
	var imgs, titles, texts ;
	imgs = GetArray('ImgBrowserImgsHidden', '|');
	titles = GetArray('ImgBrowserTitlesHidden', '|');
	texts = GetArray('ImgBrowserDescHidden', '|');
	if (imgs.length > 0) {
		imgBrowserIx--;
		if (imgBrowserIx < 0) imgBrowserIx = imgs.length-1;
		SetImageSrc('current_img', imgs[imgBrowserIx]);
		SetInnerHTML('div_current_title', titles[imgBrowserIx]);
		SetInnerHTML('div_current_text', texts[imgBrowserIx]);
	}
}

function next_image() {
	var imgs, titles, texts ;
	imgs = GetArray('ImgBrowserImgsHidden', '|');
	titles = GetArray('ImgBrowserTitlesHidden', '|');
	texts = GetArray('ImgBrowserDescHidden', '|');
	if (imgs.length > 0) {
		imgBrowserIx++;
		if (imgBrowserIx >= imgs.length) imgBrowserIx = 0;
		SetImageSrc('current_img', imgs[imgBrowserIx]);
		SetInnerHTML('div_current_title', titles[imgBrowserIx]);
		SetInnerHTML('div_current_text', texts[imgBrowserIx]);
	}
}


function InitImageBrowser() {
	var imgs, titles, texts ;
	imgs = GetArray('ImgBrowserImgsHidden', '|');
	titles = GetArray('ImgBrowserTitlesHidden', '|');
	texts = GetArray('ImgBrowserDescHidden', '|');

	if (imgs.length > 0)
		SetImageSrc('current_img', imgs[0]);
	else
		SetImageSrc('div_current_img', "");

	if (texts.length > 0)
		SetInnerHTML('div_current_text', texts[0]);
	else
		SetInnerHTML('div_current_text', "");
	if (titles.length > 0)
		SetInnerHTML('div_current_title', titles[0]);
	else
		SetInnerHTML('div_current_title', "");
}



/**
*
*  URL encode / decode
*  http://www.webtoolkit.info/
*
**/

var Url = {

	// public method for url encoding
	encode : function (string) {
		return escape(this._utf8_encode(string));
	},

	// public method for url decoding
	decode : function (string) {
		return this._utf8_decode(unescape(string));
	},

	// private method for UTF-8 encoding
	_utf8_encode : function (string) {
		string = string.replace(/\r\n/g,"\n");
		var utftext = "";

		for (var n = 0; n < string.length; n++) {

			var c = string.charCodeAt(n);

			if (c < 128) {
				utftext += String.fromCharCode(c);
			}
			else if((c > 127) && (c < 2048)) {
				utftext += String.fromCharCode((c >> 6) | 192);
				utftext += String.fromCharCode((c & 63) | 128);
			}
			else {
				utftext += String.fromCharCode((c >> 12) | 224);
				utftext += String.fromCharCode(((c >> 6) & 63) | 128);
				utftext += String.fromCharCode((c & 63) | 128);
			}

		}

		return utftext;
	},

	// private method for UTF-8 decoding
	_utf8_decode : function (utftext) {
		var string = "";
		var i = 0;
		var c = c1 = c2 = 0;

		while ( i < utftext.length ) {

			c = utftext.charCodeAt(i);

			if (c < 128) {
				string += String.fromCharCode(c);
				i++;
			}
			else if((c > 191) && (c < 224)) {
				c2 = utftext.charCodeAt(i+1);
				string += String.fromCharCode(((c & 31) << 6) | (c2 & 63));
				i += 2;
			}
			else {
				c2 = utftext.charCodeAt(i+1);
				c3 = utftext.charCodeAt(i+2);
				string += String.fromCharCode(((c & 15) << 12) | ((c2 & 63) << 6) | (c3 & 63));
				i += 3;
			}

		}

		return string;
	}

}
