//Returns true if the string is not empty
function isEmpty(ctrl){
    if(ctrl == null || typeof ctrl == undefined)
        return false;
    var strSelect = "--Select--"
    var _isEmpty = false;
    switch(ctrl.type)
    {
        case "div":
        case "span":
            _isEmpty = (ctrl == null) || (ctrl.innerHTML == "0") || (ctrl.innerHTML.length == 0);
            break;
        case "text":
            _isEmpty = (ctrl == null) || (ctrl.value.length == 0); 
            break;
        case "password":
            _isEmpty = (ctrl == null) || (ctrl.value.length == 0); 
            break;
        case "select-one":
            if(ctrl.selectedIndex >= 0)
            {
                _isEmpty = ctrl.options[ctrl.selectedIndex].text == '' || ctrl.options[ctrl.selectedIndex].text.toLowerCase() == strSelect.toLowerCase(); 
            }
            else
            {
                _isEmpty = true;
            }
            break;
        case "select-multiple":
            var count = 0;
            for (var i = 0; i < ctrl.options.length; i++) 
                count++;
            if (count == 0 )
                _isEmpty = true;
            break;
    }
    
    return _isEmpty;
}
// returns true if the string is a valid email or string is empty
function isEmail(ctrl){
	if(isEmpty(ctrl))
	    return true;
	var re = /^[^\s()<>@,;:\/]+@\w[\w\.-]+\.[a-z]{2,}$/i
	return re.test(ctrl.value);
}

function confirmDialog(url)
{
	confirmInternal(function () {
			window.open(url);
		});
}

function confirmInternal(callback) {
	$('#confirm').modal({
		close:false, 
		overlayId:'confirmModalOverlay',
		containerId:'confirmModalContainer', 
		onShow: function (dialog) {
			//dialog.data.find('.message').append(message);

			// if the user clicks "yes"
			dialog.data.find('.yes').click(function () {
				// call the callback
				if ($.isFunction(callback)) {
					callback.apply();
				}
				// close the dialog
				$.modal.close();
			});
		}
	});
}

//macromedia rollovers

	function MM_swapImgRestore() { //v3.0
	  var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
	}
	
	function MM_preloadImages() { //v3.0
	  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
	    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
	    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
	}
	
	function MM_findObj(n, d) { //v4.01
	  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
	    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
	  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
	  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
	  if(!x && d.getElementById) x=d.getElementById(n); return x;
	}
	
	function MM_swapImage() { //v3.0
	  var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
	   if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
	}
	
//browser detection

var BrowserDetect = {
	init: function () {
		this.browser = this.searchString(this.dataBrowser) || "An unknown browser";
		this.version = this.searchVersion(navigator.userAgent)
			|| this.searchVersion(navigator.appVersion)
			|| "an unknown version";
		this.OS = this.searchString(this.dataOS) || "an unknown OS";
	},
	searchString: function (data) {
		for (var i=0;i<data.length;i++)	{
			var dataString = data[i].string;
			var dataProp = data[i].prop;
			this.versionSearchString = data[i].versionSearch || data[i].identity;
			if (dataString) {
				if (dataString.indexOf(data[i].subString) != -1)
					return data[i].identity;
			}
			else if (dataProp)
				return data[i].identity;
		}
	},
	searchVersion: function (dataString) {
		var index = dataString.indexOf(this.versionSearchString);
		if (index == -1) return;
		return parseFloat(dataString.substring(index+this.versionSearchString.length+1));
	},
	dataBrowser: [
		{ 	string: navigator.userAgent,
			subString: "OmniWeb",
			versionSearch: "OmniWeb/",
			identity: "OmniWeb"
		},
		{
			string: navigator.vendor,
			subString: "Apple",
			identity: "Safari"
		},
		{
			prop: window.opera,
			identity: "Opera"
		},
		{
			string: navigator.vendor,
			subString: "iCab",
			identity: "iCab"
		},
		{
			string: navigator.vendor,
			subString: "KDE",
			identity: "Konqueror"
		},
		{
			string: navigator.userAgent,
			subString: "Firefox",
			identity: "Firefox"
		},
		{
			string: navigator.vendor,
			subString: "Camino",
			identity: "Camino"
		},
		{		// for newer Netscapes (6+)
			string: navigator.userAgent,
			subString: "Netscape",
			identity: "Netscape"
		},
		{
			string: navigator.userAgent,
			subString: "MSIE",
			identity: "Explorer",
			versionSearch: "MSIE"
		},
		{
			string: navigator.userAgent,
			subString: "Gecko",
			identity: "Mozilla",
			versionSearch: "rv"
		},
		{ 		// for older Netscapes (4-)
			string: navigator.userAgent,
			subString: "Mozilla",
			identity: "Netscape",
			versionSearch: "Mozilla"
		}
	],
	dataOS : [
		{
			string: navigator.platform,
			subString: "Win",
			identity: "Windows"
		},
		{
			string: navigator.platform,
			subString: "Mac",
			identity: "Mac"
		},
		{
			string: navigator.platform,
			subString: "Linux",
			identity: "Linux"
		}
	]

};
BrowserDetect.init();

//popup function

var popUpWin=0;
function popUpWindow(URLStr, left, top, width, height)
{
  if(popUpWin)
  {
    if(!popUpWin.closed) popUpWin.close();
  }
  popUpWin = window.open(URLStr, 'popUpWin', 'toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=no,resizable=no,copyhistory=yes,width='+width+',height='+height+',left='+left+', top='+top+',screenX='+left+',screenY='+top+'');
}

//end of popup function

//jump function

function jump(where) { 
	var thePage = "contribute.aspx?" + where; 
	openNewWindow(thePage,"temp",530,171,0,0,"none"); 
}

function jump2(where) {
	openNewWindow(where,"outside",650,550,0,0,"full");
}

function jump3(where) {
	openNewWindow(where,"outside",800,768,0,0,"full");
}

function jump4(where) {
	openNewWindow(where,"outside",786,740,0,0,"full");
}

//open window function
function openNewWindow(page,name,width,height,top,left,propSet) {

	var windowProps = new Array (8);

	windowProps[0] = "resizable=yes";
	windowProps[1] = "scrollbars=yes";
	windowProps[2] = "titlebar=yes";
	windowProps[3] = "toolbar=yes";
	windowProps[4] = "menubar=yes";
	windowProps[5] = "location=yes";
	windowProps[6] = "status=yes";
	windowProps[7] = "directories=yes";
	
	var myProps = "";
	var mySize = "";
	
	if (propSet == 'one') {
		 myProps = ',' + windowProps[0] + ',' + windowProps[1];
	} else if (propSet == "full") {
		myProps = ',' + windowProps.join(",");
	} else if (propSet == "print") {
		myProps = ',' + "scrollbars=yes";
	} else {
		myProps = "";
	}	
	
	if ((width > 50)||(height > 50)) {
		var mySize = 'width=' + width + ',' + 'height=' + height + ',' + 'top=' + top + ',' + 'left=' + left;
	}
	
	var myString = mySize + myProps;
	window.open(page,name,myString);
	
}

function doSearch(e, id)
{
	var code;
	if (!e) var e = window.event;
	if (e.keyCode) code = e.keyCode;
	else code = e.which;
	if (code == 13) {
		e.returnValue = false;
		var query = document.getElementById(id).value;
		window.location = '1.7.0_search_results.aspx?q=' + query;
		document.getElementById('Search_txtSearch').focus();
		return false;
	}
	return true;
}

function disableEnterKey(e) {
	var code;
	if (!e) var e = window.event;
	if (e.keyCode) code = e.keyCode;
	else code = e.which;
	if (code == 13) {
		return false;
	}
	return true;
}


function GP_popupConfirmMsg(msg) { //v1.0
  document.MM_returnValue = confirm(msg);
}


/*
 * Start functions associated with ExternalLinkPopup.ascx
 */

function showMessageBox(messageBoxID) {
    document.getElementById(messageBoxID).style.visibility = "visible";
}

function hideMessageBox(messageBoxID)
{
    document.getElementById(messageBoxID).style.visibility = "hidden";
}

function showStatus(sMsg) 
{
    window.status = sMsg;
    return true ;
}

function gotoLink(messageBoxID, linkURL)
{
    hideMessageBox(messageBoxID);
    window.open(linkURL);
}

/*
 * End functions associated with ExternalLinkPopup.ascx
 */


//Check email address in valid format.
function validateEmail(src) {
    var emailReg = "^[\\w-_\.]*[\\w-_\.]\@[\\w]\.+[\\w]+[\\w]$";
    var regex = new RegExp(emailReg);
    return regex.test(src);
}

//Printing the contents of Page
function printPartOfPage(elementId, hostName)
{
	var printContent = document.getElementById(elementId);
	var windowUrl = 'about:blank';
	var uniqueName = new Date();
	var windowName = 'Print' + uniqueName.getTime();
	var printWindow = window.open("", "", 'left=5000,top=5000,width=0,height=0');

	printWindow.document.write('<html><head><link href="' + hostName + '/Styles/styles.css" type="text/css" rel="stylesheet"></head><body>'); 
	printWindow.document.write('<img src="' + hostName + '/images/HSI_print_logo.jpg" AlternateText="">');
	printWindow.document.write(printContent.innerHTML);
	printWindow.document.write('</body></html>'); 

	printWindow.document.close();
	printWindow.focus();
	printWindow.print();
	printWindow.close();
}


//Alternate StyleSheet switching - Start
function setCookie(name, value, days) {
	var expireDate = new Date()
	//set "expstring" to either future or past date, to set or delete cookie, respectively
	var expstring=(typeof days!="undefined")? expireDate.setDate(expireDate.getDate()+parseInt(days)) : expireDate.setDate(expireDate.getDate()-5)
	document.cookie = name+"="+value+"; expires="+expireDate.toGMTString()+"; path=/";
}

function setStylesheet(title, randomize){ //Main stylesheet switcher function. Second parameter if defined causes a random alternate stylesheet (including none) to be enabled
	var i, cacheobj, altsheets=[""]
	for(i=0; (cacheobj=document.getElementsByTagName("link")[i]); i++) 
	{
		if(cacheobj.getAttribute("rel").toLowerCase()=="alternate stylesheet" && cacheobj.getAttribute("title")) 
		{ //if this is an alternate stylesheet with title
			cacheobj.disabled = true
			altsheets.push(cacheobj) //store reference to alt stylesheets inside array
			if(cacheobj.getAttribute("title") == title) //enable alternate stylesheet with title that matches parameter
			{
				cacheobj.disabled = false //enable chosen style sheet
			}
		}
	}
	if (typeof randomize!="undefined")
	{ //if second paramter is defined, randomly enable an alt style sheet (includes non)
		var randomnumber=Math.floor(Math.random()*altsheets.length)
		altsheets[randomnumber].disabled=false
	}
	return (typeof randomize!="undefined" && altsheets[randomnumber]!="")? altsheets[randomnumber].getAttribute("title") : "" //if in "random" mode, return "title" of randomly enabled alt stylesheet
}

function chooseStyle(styletitle, days, elementid){ //Interface function to switch style sheets plus save "title" attr of selected stylesheet to cookie
	if (document.getElementById)
	{
		setStylesheet(styletitle)
		setCookie("mysheet", styletitle, days)
		var obj = document.getElementById(elementid);
		var objSmall = document.getElementById("smallTextSize");
		var objMed = document.getElementById("medTextSize");
		var objLarge = document.getElementById("largeTextSize");

		if(obj.className == "smallA")
		{
			obj.className = "smallAOn";
			objMed.className = "medA";
			objLarge.className = "largeA";
		}
		else if(obj.className == "medA")
		{
			obj.className = "medAOn";
			objSmall.className = "smallA";
			objLarge.className = "largeA";
		}
		else if(obj.className == "largeA")
		{
			obj.className = "largeAOn";
			objMed.className = "medA";
			objSmall.className = "smallA";
		}
	}
}
//Alternate StyleSheet switching - End