﻿
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, isSearchPage)
{
    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 + '?query=' + Url.encode(tb.value) + '&isSearchPage=' + isSearchPage;
            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 + '?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 SetImgBrowserHeader(elmtID, text, href, changeImageInterval_ms) {
    var headerTag, linkStart = '', linkEnd = '';
    if (href != '') {
        var target = '_new';
        if (href.substring(0,1) == '/') {
            target = '_self';
        }
        linkStart = '<a href="'+ href +'" target="'+ target +'">';
        linkEnd = '</a>';
    }
    headerTag = linkStart + '<H3>' + text + '</H3>' + linkEnd;
    SetInnerHTML(elmtID, headerTag, changeImageInterval_ms)
}

function SetInnerHTML(elmtID, txt, changeImageInterval_ms) {
	if (document.getElementById(elmtID) ) {
	   
	    if (j$("#" + elmtID).html() != txt) {
	        //var oldbgColor = document.getElementById(elmtID).style.backgroundColor;
//	        j$("#" + elmtID).fadeOut(changeImageInterval_ms);
//	        setTimeout(function() {
	            document.getElementById(elmtID).innerHTML = txt;
//	            j$("#" + elmtID).fadeIn(changeImageInterval_ms, function() {
//	                this.style.removeAttribute('filter');
//	            });
//	        }, changeImageInterval_ms);
	        //document.getElementById(elmtID).style.backgroundColor = oldbgColor
	    }
	}
}

function SetVisibility(elmtID, visible) {
	if (document.getElementById(elmtID) ) {
		var visibility = 'hidden';
		if (visible)
			visibility = 'visible';
		
		document.getElementById(elmtID).style.visibility=visibility;
	}
}	

function ChangeElementDisplayMode(elmtID) {
    if ( document.getElementById(elmtID).style.display != 'none'  ) {
        document.getElementById(elmtID).style.display='none';
    }
    else {
        document.getElementById(elmtID).style.display='block';
    }
    
    return void(0);
}


function SetImageSrc(elmtID, src, altText, changeImageInterval_ms) {
    
	if (document.getElementById(elmtID) ) {
	    if (j$("#" + elmtID).attr("src") != src) {

	        
	        
	        var visibility = 'hidden';
	        if (!src || src == '') {
	            SetVisibility(elmtID, false);
	        }
	        else {


//	            j$("#" + elmtID).fadeTo(changeImageInterval_ms, 0);
//	            setTimeout(function() {
	                   j$("#" + elmtID).attr("src", src);
//	                        .fadeTo(changeImageInterval_ms, 1.0);
//	               }, changeImageInterval_ms);
	                
	                
	            if (!altText)
	                altText = '';
	            altText = new String(altText).replace(new RegExp('&amp;', 'gi'), '&');
	            document.getElementById(elmtID).alt = altText;
	            document.getElementById(elmtID).title = altText;
	        }
	    }				
	}
}






/* 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(userControlClientId) {
    var imgs, titles, texts, links;
    imgs = GetArray(userControlClientId + '_ImgBrowserImgsHidden', '|');
    titles = GetArray(userControlClientId + '_ImgBrowserTitlesHidden', '|');
    texts = GetArray(userControlClientId + '_ImgBrowserDescHidden', '|');
    links = GetArray(userControlClientId + '_ImgBrowserLinksHidden', '|');
    if (imgs.length > 0) {
        var imgPointer = 0;
        var jQueryElement = j$("#" + userControlClientId + "_ImgBrowserImgsHidden");
        if (j$(jQueryElement).data('imgPointer')) {
            imgPointer = j$(jQueryElement).data('imgPointer');
        }
        imgPointer--;

        if (imgPointer < 0) imgPointer = imgs.length - 1;
        SetImgBrowserValues(userControlClientId, imgs[imgPointer], titles[imgPointer], texts[imgPointer], links[imgPointer]);
        j$(jQueryElement).data('imgPointer', imgPointer);
    }
}

function next_image(userControlClientId) {


    

    var imgs, titles, texts, links;
    imgs = GetArray(userControlClientId + '_ImgBrowserImgsHidden', '|');
    titles = GetArray(userControlClientId + '_ImgBrowserTitlesHidden', '|');
    texts = GetArray(userControlClientId + '_ImgBrowserDescHidden', '|');
    links = GetArray(userControlClientId + '_ImgBrowserLinksHidden', '|');
    if (imgs.length > 0) {
        var imgPointer = 0;
        var jQueryElement = j$("#" + userControlClientId + "_ImgBrowserImgsHidden");
        if (j$(jQueryElement).data('imgPointer')) {
            imgPointer = j$(jQueryElement).data('imgPointer');
        }
        imgPointer++;
        
        
        
        //imgBrowserIx++;
        if (imgPointer >= imgs.length) imgPointer = 0;
        SetImgBrowserValues(userControlClientId, imgs[imgPointer], titles[imgPointer], texts[imgPointer], links[imgPointer]);
        j$(jQueryElement).data('imgPointer', imgPointer);
    }
    

    
    
}


function InitImageBrowser(userControlClientId) {
    var imgs, titles, texts;
    imgs = GetArray(userControlClientId + '_ImgBrowserImgsHidden', '|');
    titles = GetArray(userControlClientId + '_ImgBrowserTitlesHidden', '|');
    texts = GetArray(userControlClientId + '_ImgBrowserDescHidden', '|');
    links = GetArray(userControlClientId + '_ImgBrowserLinksHidden', '|');

    if (imgs.length > 0) {
        SetImgBrowserValues(userControlClientId, imgs[0], titles[0], texts[0], links[0]);
    }
    else {
        SetImgBrowserValues(userControlClientId, '', '', '', '');
    }

}

function SetImgBrowserValues(userControlClientId, imgSrc, title, text, link) {
    var changeImageInterval_ms = 200;
    SetImageSrc(userControlClientId + '_ImgBrowserImg', imgSrc, title, changeImageInterval_ms);
    SetImgBrowserHeader(userControlClientId + '_ImgBrowserTitleDiv', title, link, changeImageInterval_ms);
    SetInnerHTML(userControlClientId + '_ImgBrowserTextDiv', text, changeImageInterval_ms);
}



/**
*
*  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;
	}

}

/**
*
*  Ajax loader script - "borrowed" from PIB
*
**/

function ajaxLoader(id) {
    var el = document.getElementById(id);
    el.style.display = 'block';
    var COMMENT = '<!--imgIsLoaded-->';
    el.innerHTML += COMMENT;

}


