function YahooclickButton(e, buttonid)
{ 
      var bt = document.getElementById(buttonid); 
      if (typeof bt == 'object'){ 
            if(navigator.appName.indexOf("Netscape")>(-1)){ 
                  if (e.keyCode == 13){ 
                        bt.click(); 
                        return false; 
                  } 
            } 
            if (navigator.appName.indexOf("Microsoft Internet Explorer")>(-1)){ 
                  if (event.keyCode == 13){ 
                        bt.click(); 
                        return false; 
                  } 
            } 
      } 
} 


var isIE = false;	

if (navigator.userAgent.indexOf('MSIE') > 0)
	isIE = true;

function confirmDelete()
{
	confirm("Are you sure you wish to remove this module?");
}

var Timer = null;

function ShowDropDowns() {
 if (ComponentArt_Snap_Page_Loaded == true) {
   var DropDowns = document.getElementsByTagName("select");
   for (i = 0; i < DropDowns.length; i++) {
     if (DropDowns[i].style.visibility == "hidden") DropDowns[i].style.visibility = "visible";
   }
   window.clearTimeout(Timer);
 }
}

//-- this function is used to correct a problem with the SNAP modules rendering drop down lists... 
//-- remove the call to this function in the onLoad event of a template to see the effects
function StartTimer() { Timer = window.setTimeout("ShowDropDowns();",50); }


function changeImage(x)
{
	var img = document.images[x];
	var temp = x.replace("img_", "");
		
	var lnk = document.getElementsByName(temp);
	var cell = document.getElementById("td_" + temp);
	
	if (img.src.indexOf("off") > 0)
	{
		img.src = "./images/toolboxside_on.gif";
		lnk[0].className = "TabActive";
		cell.className = "TabActiveCell";
	}
	else
	{
		img.src = "./images/toolboxside_off.gif";
		lnk[0].className = "TabInactive";
		cell.className = "TabInactiveCell";
	}
		
}

function resizeLeftNav()
{
	/*var mainContainer = document.getElementById("MainContainer");
	var sideNavBar = document.getElementById("SideNavBar");

	if (isIE)
	{
		if ((mainContainer.offsetHeight - 55) > 0)
			sideNavBar.style["height"] = mainContainer.offsetHeight - 60; //the 57 pixels are for the web search box
	}
	else
	{
		if ((mainContainer.offsetHeight - 60) > 0)
			sideNavBar.style["height"] = mainContainer.offsetHeight - 60; //the 60 pixels are for the web search box
	}*/
}

function resizeLoginLeftNav()
{
	/*var mainContainer = document.getElementById("MainContainer");
	var sideNavBar = document.getElementById("SideNavBar");
	var rightContentBox = document.getElementById("ContentBox");

	if (mainContainer.offsetHeight > sideNavBar.offsetHeight)
	{
		if (isIE)
		{
			sideNavBar.style["height"] = mainContainer.offsetHeight-5; //the 66 pixels are for the web search box
			rightContentBox.style["height"] = mainContainer.offsetHeight-7;
		}
		else
		{
			sideNavBar.style["height"] = mainContainer.offsetHeight+7; //the 66 pixels are for the web search box
			rightContentBox.style["height"] = mainContainer.offsetHeight+7;
		}
	}
	else
	{
		if (isIE)
		{
			mainContainer.style["height"] = sideNavBar.offsetHeight;
			rightContentBox.style["height"] = sideNavBar.offsetHeight;
		}
		else
		{
			mainContainer.style["height"] = sideNavBar.offsetHeight;
			rightContentBox.style["height"] = sideNavBar.offsetHeight;
		}
	}*/
}

function CaptureClick(e)
{			
	if (window.event)
	{
		if(window.event.keyCode==13)								
		{
			window.event.returnValue = false;
			window.event.cancel = true;             // these two lines stops the event activated by the enter
			if (document.forms[0].elements['_ctl0__ctl0__ctl0_txtUsername'].value!="" && document.forms[0].elements['_ctl0__ctl0__ctl0_txtPassword'].value!="")
				document.getElementById('_ctl0__ctl0__ctl0_btnLogin').click();
		}
	}
	else
	{
		if (e.keyCode == 13)
		{
			if (document.getElementById('_ctl0__ctl0__ctl0_txtUsername').value!="" && document.getElementById('_ctl0__ctl0__ctl0_txtPassword').value!="")
			{
				var element = document.getElementById('_ctl0__ctl0__ctl0_btnLogin');
				element.click();
				setTimeout("refocus()",0);
				return false;
			}
		}
	}
}

function showLayer(layerName, showAsBlock)
{
	var layer = document.getElementById(layerName);
	if (layer.style["display"] == "none")
	{
		if (showAsBlock)
			layer.style["display"] = "block";
		else
			layer.style["display"] = "inline";
	}
	else
		layer.style["display"] = "none";
}

function showLayerChangeText(layerName, linkName, textOn, textOff)
{
	var layer = document.getElementById(layerName);
	var link = document.getElementById(linkName);
	
	if (layer.style["display"] == "none")
	{
		layer.style["display"] = "inline";
		link.innerText = textOff;
	}
	else
	{
		layer.style["display"] = "none";
		link.innerText = textOn;
	}
}

function TrimString(sInString) {
  if ( sInString ) {
    sInString = sInString.replace( /^\s+/g, "" );// strip leading
    return sInString.replace( /\s+$/g, "" );// strip trailing
  }
}

// Populates the country selected with the counties from the country list
function populateCountry(defaultCountry, uniqueId) {
	
	var countryLineArray = eval("arrCountry"+uniqueId).split('|');  // Split into lines
	var selObj = document.getElementById('countrySelect'+uniqueId);

	selObj.options[0] = new Option('Select Country','');
	selObj.selectedIndex = 0;
	
	for (var loop = 0; loop < countryLineArray.length-1; loop++) {
		lineArray = countryLineArray[loop].split(':');
		countryCode  = TrimString(lineArray[0]);
		countryName  = TrimString(lineArray[1]);
		if ( countryCode != '' ) {
		selObj.options[loop + 1] = new Option(countryName, countryCode);
		}
		if ( defaultCountry == countryCode ) {
		selObj.selectedIndex = loop + 1;
		}
	}
	
	
	//set country cookie if cookie is null
	if (getCookie("BookingCountrySelection"+uniqueId) == null)
		setCookie("BookingCountrySelection"+uniqueId, selObj.selectedIndex, null);
	else
	{
		var currentCountry = getCookie("BookingCountrySelection"+uniqueId);
		selObj.selectedIndex = currentCountry;
	}
	
	//set hidden field
	//var hidObj = document.getElementById('hidCountry'+uniqueId);
	//hidObj.value = selObj[selObj.selectedIndex].value;
}

function populateCity(uniqueId) 
{

	//set country cookie
	var countryObj = document.getElementById("countrySelect"+uniqueId);

	//check if country has changed - if it has reset city cookie
	if (countryObj.selectedIndex != getCookie("BookingCountrySelection" + uniqueId))
		setCookie("BookingCitySelection" + uniqueId, null, null);	

	setCookie("BookingCountrySelection" + uniqueId, countryObj.selectedIndex, null);
	
	var selObj = document.getElementById('citySelect' + uniqueId);
	var selLocationObj = document.getElementById('locationSelect' + uniqueId);

	// Empty options just in case new drop down is shorter
	if ( selObj.type == 'select-one' ) {
		for (var i = 0; i < selObj.options.length; i++) {
			selObj.options[i] = null;
	}
	selObj.options.length=null;
	selObj.options[0] = new Option('Select City','');
	selObj.selectedIndex = 0;
	}

	if ( selLocationObj.type == 'select-one' ) {
		for (var i = 0; i < selLocationObj.options.length; i++) {
			selLocationObj.options[i] = null;
	}
	selLocationObj.options.length=null;
	selLocationObj.options[0] = new Option('Select Location','');
	selLocationObj.selectedIndex = 0;
	}

	// Populate the drop down with cities from the selected country
	var cityLineArray = eval("arrCity" + uniqueId).split("|");  // Split into lines
	var optionCntr = 1;
	for (var loop = 0; loop < cityLineArray.length; loop++) {
		lineArray = cityLineArray[loop].split(":");
		countryCode  = TrimString(lineArray[0]);
		cityCode    = TrimString(lineArray[1]);
		cityName    = TrimString(lineArray[2]);

		if (document.getElementById('countrySelect'+uniqueId).value == countryCode && countryCode != '' ) 
		{
			if ( cityCode != '' ) {
			selObj.options[optionCntr] = new Option(cityName, cityCode);
			}
			// See if it's selected from a previous post
			/*if ( cityCode == postCity && countryCode == postCountry ) {
			selObj.selectedIndex = optionCntr;
			}*/
			foundCity = true;
			optionCntr++
		}
	}
	
	//set city cookie if cookie is null
	if (getCookie("BookingCitySelection" + uniqueId) == null)
		setCookie("BookingCitySelection" + uniqueId, selObj.selectedIndex, null);
	else
	{
		var currentCity = getCookie("BookingCitySelection" + uniqueId);
		selObj.selectedIndex = currentCity;
	}
	
	//set city hidden object
	//var hidCityObj = document.getElementById('hidCity'+uniqueId);
	//hidCityObj.value = selObj[selObj.selectedIndex].value;
}

function populateLocation(uniqueId) 
{
	//set city cookie
	var cityObj = document.getElementById('citySelect' + uniqueId);

	//check if city has changed - if it has reset location cookie
	if (cityObj.selectedIndex != getCookie("BookingCitySelection" + uniqueId))
		setCookie("BookingLocationSelection" + uniqueId, null, null);	
		
	setCookie("BookingCitySelection" + uniqueId, cityObj.selectedIndex, null);
	
	
	var selObj = document.getElementById('locationSelect' + uniqueId);

	// Empty options just in case new drop down is shorter
	if ( selObj.type == 'select-one' ) {
		for (var i = 0; i < selObj.options.length; i++) {
			selObj.options[i] = null;
	}
	selObj.options.length=null;
	selObj.options[0] = new Option('Select Location','');
	selObj.selectedIndex = 0;
	}

	// Populate the drop down with cities from the selected country
	var locationLineArray = eval("arrLocation" + uniqueId).split("|");  // Split into lines
	var optionCntr = 1;
	for (var loop = 0; loop < locationLineArray.length; loop++) {
		lineArray = locationLineArray[loop].split(":");
		cityCode  = TrimString(lineArray[0]);
		locationCode    = TrimString(lineArray[1]);
		locationName    = TrimString(lineArray[2]);

		if (document.getElementById('citySelect'+uniqueId).value == cityCode && cityCode != '' ) 
		{
			if ( locationCode != '' ) {
			selObj.options[optionCntr] = new Option(locationName, locationCode);
			}
			// See if it's selected from a previous post
			/*if ( cityCode == postCity && countryCode == postCountry ) {
			selObj.selectedIndex = optionCntr;
			}*/
			foundLocation = true;
			optionCntr++
		}
	}
	
	//set location cookie if cookie is null
	if (getCookie("BookingLocationSelection" + uniqueId) == null)
		setCookie("BookingLocationSelection" + uniqueId, selObj.selectedIndex, null);
	else
	{
		var currentLocation = getCookie("BookingLocationSelection" + uniqueId);
		selObj.selectedIndex = currentLocation;
	}
	
	//set location hidden object
	//var hidLocationObj = document.getElementById('hidLocation'+uniqueId);
	//hidLocationObj.value = selObj[selObj.selectedIndex].value;
}

function setLocation(uniqueId)
{
	
	//set location cookie
	var locationObj = document.getElementById('locationSelect' + uniqueId);
	setCookie("BookingLocationSelection" + uniqueId, locationObj.selectedIndex, null);
}

function initCountry(country, uniqueId) {
  populateCountry(country, uniqueId);
  populateCity(uniqueId);
  populateLocation(uniqueId);
}

function URLEncode(x)
{
	// The Javascript escape and unescape functions do not correspond
	// with what browsers actually do...
	var SAFECHARS = "0123456789" +					// Numeric
					"ABCDEFGHIJKLMNOPQRSTUVWXYZ" +	// Alphabetic
					"abcdefghijklmnopqrstuvwxyz" +
					"-_.!~*'()";					// RFC2396 Mark characters
	var HEX = "0123456789ABCDEF";

	var plaintext = x;
	var encoded = "";
	for (var i = 0; i < plaintext.length; i++ ) {
		var ch = plaintext.charAt(i);
	    if (ch == " ") {
		    encoded += "+";				// x-www-urlencoded, rather than %20
		} else if (SAFECHARS.indexOf(ch) != -1) {
		    encoded += ch;
		} else {
		    var charCode = ch.charCodeAt(0);
			if (charCode > 255) {
			    alert( "Unicode Character '" 
                        + ch 
                        + "' cannot be encoded using standard URL encoding.\n" +
				          "(URL encoding only supports 8-bit characters.)\n" +
						  "A space (+) will be substituted." );
				encoded += "+";
			} else {
				encoded += "%";
				encoded += HEX.charAt((charCode >> 4) & 0xF);
				encoded += HEX.charAt(charCode & 0xF);
			}
		}
	} // for

	return encoded;
}

function setCookie(name, value, expires, path, domain, secure) {
	document.cookie= name + "=" + escape(value) +
    ((expires) ? "; expires=" + expires.toGMTString() : "") +
    ((path) ? "; path=" + path : "") +
    ((domain) ? "; domain=" + domain : "") +
    ((secure) ? "; secure" : "");
}

function getCookie(name) {
	var dc = document.cookie;
	var prefix = name + "=";
	var begin = dc.indexOf("; " + prefix);
	if (begin == -1) {
		begin = dc.indexOf(prefix);
		if (begin != 0) return null;
	} else {
		begin += 2;
	}
	var end = document.cookie.indexOf(";", begin);
	if (end == -1) {
		end = dc.length;
	}
	return unescape(dc.substring(begin + prefix.length, end));
}

function openCloseFav(x, title)
{
	var hideDiv = document.getElementById(x);
	
	if (hideDiv.style["display"] == "block")
	{
		hideDiv.style["display"] = "none"
		title.className = "FavoriteFolderClosed";
	}
	else
	{
		hideDiv.style["display"] = "block"
		title.className = "FavoriteFolderOpen";
	}
	
	resizeLeftNav();
}

function webSearch(x)
{
	window.open("http://search.yahoo.com/search?p="+x);
}

function shutPopUpWindow(x)
{
	var layer = document.getElementById(x);
	layer.style["display"] = "none";
}

function checkClick(e)
{
	var eventElement = "";
	var eventHref = null;
	
	if (window.event)
		eventElement = window.event.srcElement.type;
	else
		eventElement = e.target.type;

	if (eventElement != "text")
	{
		var arrDivs = document.getElementsByTagName("div");

		for (i=0;i<arrDivs.length;i++)
		{
			if (arrDivs[i].id.indexOf("PopUp") > -1 || arrDivs[i].id.indexOf("Popup") > -1)
				shutPopUpWindow(arrDivs[i].id);
		}

		var arrIframes = document.getElementsByTagName("iframe");
		
		for (i=0;i<arrIframes.length;i++)
		{
			if (arrIframes[i].id.indexOf("Hack") > -1)
				shutPopUpWindow(arrIframes[i].id);
		}
	}
}

	
	function launchHelp()
	{
		//maximise main window first
		//window.moveTo(0,0); 
		if (isIE)
			window.resizeTo(screen.availWidth,screen.availHeight); 
		else 
		{
			if (top.window.outerHeight<screen.availHeight||top.window.outerWidth<screen.availWidth)
			{ 
				top.window.outerHeight = screen.availHeight; 
				top.window.outerWidth = screen.availWidth; 
			}
		}
	
		var winWidth = 230; 
		var winLeft = screen.availWidth - winWidth;
		
		
		//var winFeatures = "scrollbars=yes,resizable=yes,top=0,width="+(winWidth-5)+",height="+(screen.availHeight - 28)+",left="+winLeft;
		var winFeatures = "scrollbars=yes,resizable=yes,top=10,width=600,height=800,left="+winLeft;
		
		/*if (isIE)
		//window.resizeTo((screen.availWidth - (winWidth-5)), screen.availHeight+5);
		//else
		//window.outerWidth = screen.availWidth - (winWidth+3);
		*/
		
	//var HELPTEXT=document.layers['HELPDIV'].document;
	var HELPTEXT="";
	if (document.getElementById != "" && document.getElementById != 0 && document.getElementById != false && document.getElementById != undefined && document.getElementById != null)
	   HELPTEXT=document.getElementById('HELPDIV').innerHTML;		    
	
	if (HELPTEXT !='')
	window.open(HELPTEXT, "HelpSystem", winFeatures);		
	
	//alert (HELPTEXT);	
	//writing to the popup
	//var generator=window.open('','name',winFeatures); 	  
	//generator.document.write(HELPTEXT);
  	//generator.document.close();
	
	//var HelpSystem = window.open("modules/HelpFile.aspx", "HelpSystem", winFeatures);		
	//HelpSystem.document.getElementById('HELPCONTENT').innerHTML=HELPTEXT;
	//HelpSystem.focus();	
	}

function ResizeOpener()
{
	if (isIE)
		opener.resizeTo(screen.availWidth, screen.availHeight);
	else
		opener.outerWidth = screen.availWidth;
}

function correctPNG() // correctly handle PNG transparency in Win IE 5.5 & 6.
{
   var arVersion = navigator.appVersion.split("MSIE")
   var version = parseFloat(arVersion[1])
   if ((version >= 5.5) && (document.body.filters)) 
   {
      for(var i=0; i<document.images.length; i++)
      {
         var img = document.images[i]
         var imgName = img.src.toUpperCase()
         if (imgName.substring(imgName.length-3, imgName.length) == "PNG")
         {
            var imgID = (img.id) ? "id='" + img.id + "' " : ""
            var imgClass = (img.className) ? "class='" + img.className + "' " : ""
            var imgTitle = (img.title) ? "title='" + img.title + "' " : "title='" + img.alt + "' "
            var imgName = (img.name) ? "name='" + img.name + "' " : ""
            var imgSrc = (img.src) ? "src='" + img.src + "' " : ""
            var imgStyle = "display:inline-block;" + img.style.cssText 
            if (img.align == "left") imgStyle = "float:left;" + imgStyle
            if (img.align == "right") imgStyle = "float:right;" + imgStyle
            if (img.parentElement.href) imgStyle = "cursor:hand;" + imgStyle
            var strNewHTML = "<span " + imgID + imgClass + imgTitle + imgName
            + " style=\"" + "width:" + img.width + "px; height:" + img.height + "px;" + imgStyle + ";"
            + "filter:progid:DXImageTransform.Microsoft.AlphaImageLoader"
            + "(src=\'" + img.src + "\', sizingMethod='scale');\"></span>" 
            img.outerHTML = strNewHTML
            i = i-1
	     }
      }
   }    
}

function changePrimaryNav(x)
{
	var y = document.getElementById("PrimaryNavTable");
	var temp = y.getElementsByTagName("td");
	
	for (i=0;i<temp.length;i++)
	{
		if (temp[i]["name"] == x)
		{
			if (temp[i].className == "NavOff")
				temp[i].className = "NavOn";
			else
				temp[i].className = "NavOff";
		}
	}
}


function querystring(key) 
{ 
	var value = null; 
	for (var i=0;i<querystring.keys.length;i++) 
	{ 
		if (querystring.keys[i]==key) 
		{ 
			value = querystring.values[i]; 
			break; 
		} 
	} 
	return value; 
} 


querystring.keys = new Array(); 
querystring.values = new Array(); 

function querystring_Parse() 
{ 
	var query = window.location.search.substring(1); 
	var pairs = query.split("&"); 

	for (var i=0;i<pairs.length;i++) 
	{ 
		var pos = pairs[i].indexOf('='); 
		if (pos >= 0) 
		{ 
			var argname = pairs[i].substring(0,pos); 
			var value = pairs[i].substring(pos+1); 
			querystring.keys[querystring.keys.length] = argname; 
			querystring.values[querystring.values.length] = value; 
		} 
	} 
} 

querystring_Parse(); 

function populateDefaults(x)
{
	//setup country
	var countryObj = document.getElementById("countrySelect"+x);
	
	for(i=0;i<countryObj.length;i++)
	{
		if (countryObj[i].value == defaultCountry)
		{
			setCookie("BookingCountrySelection"+x, countryObj.selectedIndex, null);
			countryObj[i].selected = true;
		}
	}
	
	populateCity(x);
	
	//setup city
	var cityObj = document.getElementById("citySelect"+x);
	
	for(i=0;i<cityObj.length;i++)
	{
		if (cityObj[i].value == defaultCity)
		{
			setCookie("BookingCitySelection"+x, cityObj.selectedIndex, null);
			cityObj[i].selected = true;
		}
	}

	populateLocation(x);
	
	//setup location
	var locationObj = document.getElementById("locationSelect"+x);
	for(i=0;i<locationObj.length;i++)
	{
		if (locationObj[i].value == defaultLocation)
		{
			setCookie("BookingLocationSelection"+x, locationObj.selectedIndex, null);
			locationObj[i].selected = true;
		}
	}

}

function setupLogin()
{
	var username = document.getElementById('_ctl0__ctl0__ctl0_txtUsername');
	username.focus();
}

function LoadALL()
{
	correctPNG();
	resizeLeftNav(); 
}

function showCenteredLayer(layerName)
{
	var layer = document.getElementById(layerName);
	if (layer.style["display"] == "none")
	{
		layer.style["display"] = "inline";
		//layer.style.left=document.body.scrollLeft+document.body.clientWidth/2-layer.offsetWidth/2;
		//layer.style.top=document.body.scrollTop+document.body.clientHeight/2-layer.offsetHeight/2;
	}
	else
	{
		// NOTE: currently never hit this because checkClick reset the layer just before this
		layer.style["display"] = "none";
	}
}

function shutThirdNav()
	{
		var sidebar = document.getElementById("SideNavBar");
		var divs = sidebar.getElementsByTagName("div");
		var hrefs = sidebar.getElementsByTagName("a");
		
		for (i=0;i<divs.length;i++)
		{
			divs[i].style["display"] = "none";
		}
		
		for (i=0;i<hrefs.length;i++)
		{
			hrefs[i].className = "";
		}

	}
	
	function goThru()
	{
		window.open("http://servcorp.thruinc.net/");
}
