
// Header.js - Various javascript routines.

//===== Global variables. =================================================
var ieFix = false;
var ieMac = false;
var ie6 = false;
var ie7 = false;
var fixSrchSummary = false;
var srchSum;
var timerSecs;
var timerID = null;
var timerRunning = false;
var timerDelay = 500;


//===== Functions. ========================================================


// DDMenuStart - Called on document load.
//
// Drop Down Menus - This fixes some issues with the drop down menus in
//                   various browsers (Win Internet Explorer in separate file).
//                   Only executed when the document is loaded.
//
// Note: document.getElementsByName does not work in IE 5 for the Mac.

DDMenuStart = function()  {

	var e, i1, place;

	if (parseInt(navigator.appVersion) >= 4) {
		if (navigator.appName == "Microsoft Internet Explorer") {
			if (navigator.userAgent.indexOf("Opera")==-1) {
				ieFix = true;
				var agt=navigator.userAgent.toLowerCase();
				if (agt.indexOf("mac")!=-1) {
					// IE Mac too slow to draw these menus. Just let it display the top level.
					ieMac = true;
					ieFix = false;
				}
			}
		}
	}

	if ( navigator.userAgent.indexOf("Firefox/1.0")!=-1 && document.getElementById) {
		fixSrchSummary = true;
		srchSum = document.getElementById('tdSearchSummary');
	}

	if (! ieFix && ieMac == false && document.getElementById) {

		var loginBar = document.getElementById("LoginBar");
		if (loginBar) {
			loginBar.style.backgroundColor="#1B60A1";
		}

	}

	// The search summary scroll box causes problems with Firefox version 1.0 browers.
	// Drop down menu going over scroll box unreacheable though displays ok.
	// Not a problem in Firefox version 1.5 browsers.
	if (fixSrchSummary) {
		e = document.getElementsByTagName("ul");
		for (i1=0; i1<e.length;i1++) {
			if (e[i1].className == "MainMenu" || e[i1].className == "MainMenul") {
				e[i1].onmouseover=function() { SSOff(); }
				e[i1].onmouseout=function() { SSOn(); }
			}
		}
	}
}


// Change Search Summary overflow to auto for Firefox 1.0 browsers (bug fix - menus).
function SSOn() {
	// Firefox fires the SSOn and SSOff event for every menu item.
	// Need to put in delay before turning the scroll bar back on.
	TimerInit();
}

// Change Search Summary overflow to hidden for Firefox.
function SSOff() {
	TimerStop();
	if (srchSum.style.overflow.charAt(0)!='h') {
		srchSum.style.overflow='hidden';
	}
}


function TimerInit()
{
    // Set the length of the timer, in seconds
    timerSecs = 0.5;
    TimerStop()
    TimerStart()
}

function TimerStop()
{
    if(timerRunning)
        clearTimeout(timerID)
    timerRunning = false;
}

function TimerStart()
{
    if (timerSecs==0)
    {
        TimerStop()
		if (srchSum.style.overflow.charAt(0)!='a') {
			srchSum.style.overflow='auto';
		}
    }
    else
    {
        timerSecs -= 0.5
        timerRunning = true
        timerID = self.setTimeout("TimerStart()", timerDelay)
    }
}


//----- GENERAL FUNCTIONS. ------------------------------------------------
function OpenUniqueWindow(arAddr, arWName, arWid, arHei) {
	var leftPos = winLeft(arWid);
	var topPos = winTop(arHei);

	window.open(arAddr,stripInvalid(arWName),'status=0,width=' + arWid + ',height=' + arHei + ',resizable=1,scrollbars=1,screenX=' + leftPos + ',screenY=' + topPos + ',top=' + topPos + ',left=' + leftPos)
}

function OpenUMenuWindow(arAddr, arTitle, arWid, arHei) {
	var leftPos = winLeft(arWid);
	var topPos = winTop(arHei+110);

	window.open(arAddr,stripInvalid(arTitle),'status,width=' + arWid + ',height=' + arHei + ',resizable=1,scrollbars=1,menubar=1,screenX=' + leftPos + ',screenY=' + topPos + ',top=' + topPos + ',left=' + leftPos)
}


//----- Remove invalid characters from a window name. -----
function stripInvalid(arStr) {
	return arStr = arStr.replace('-','');
}

//----- Calculate the left position of a popup window. -----
function winLeft(arWid) {
	var w = 480;

	if (screen.availWidth) {
	   w = screen.availWidth;
	}
	return (w-arWid)/2;
}

//----- Calculate the top position of a popup window. -----
function winTop(arHei) {
	var h = 340;

	if (screen.availHeight) {
	   h = screen.availHeight;
	}
	var topPos = (h-arHei)/2;

	if (navigator.appName == 'Opera' && topPos>120) {
		topPos-=120;
	}

	if (topPos<1) {
		topPos=1;
	}

	return topPos;
}

//----- Open a window. -----
function OpenWindow(arAddr, arWName, arWid, arHei, arStyle) {

	var leftPos = winLeft(arWid);
	var topPos = winTop(arHei);
	var wName = stripInvalid(arWName);
	var winOpts;

	switch (arStyle) {
		case 'min' :
			winOpts = 'width=' + arWid + ',height=' + arHei + ',dependent=1,titlebar=0,personalbar=0,location=0,status=0,directories=0,toolbar=0,menubar=0,resizable=0,scrollbars=0,screenX=' + leftPos + ',screenY=' + topPos + ',top=' + topPos + ',left=' + leftPos;
			break;
		case 'nostatus' :
			winOpts = 'status=0,width=' + arWid + ',height=' + arHei + ',resizable=1,scrollbars=1,screenX=' + leftPos + ',screenY=' + topPos + ',top=' + topPos + ',left=' + leftPos;
			break;
		case 'menu' :
			winOpts = 'status,width=' + arWid + ',height=' + arHei + ',resizable=1,scrollbars=1,menubar=1,toolbar=1,screenX=' + leftPos + ',screenY=' + topPos + ',top=' + topPos + ',left=' + leftPos;
			break;
		case 'toolbar' :
			winOpts = 'status=0,toolbar=1,width=' + arWid + ',height=' + arHei + ',resizable=1,scrollbars=1,screenX=' + leftPos + ',screenY=' + topPos + ',top=' + topPos + ',left=' + leftPos;
			break;
		default :
			winOpts = 'status=1,width=' + arWid + ',height=' + arHei + ',resizable=1,scrollbars=1,screenX=' + leftPos + ',screenY=' + topPos + ',top=' + topPos + ',left=' + leftPos;
			break;
	}

	window.open(arAddr,wName,winOpts)
}

//----- Open or re-open a popup wait (animated clock) window. -----
function OpenWaitWindow() {
	var wid=84, hei =84;
	var leftPos = winLeft(84);
	var topPos = winTop(84);

	return window.open('/popups/PleaseWait.aspx','waitwin','width=' + wid + ',height=' + hei + ',dependent=1,titlebar=0,personalbar=0,location=0,status=0,directories=0,toolbar=0,menubar=0,resizable=0,scrollbars=0,screenX=' + leftPos + ',screenY=' + topPos + ',top=' + topPos + ',left=' + leftPos)
}

//----- Display a message and ask for confirmation. -----
function ConfirmPrompt(arMsg, arURL) {
	if (confirm(arMsg))
		location.href = arURL;
}

//---- Check an email address. -----
function IsEmail(arStr) {
  //----- Are regular expressions supported?
  var supported = 0;
  if (window.RegExp) {
	  var tempStr = "a";
	  var tempReg = new RegExp(tempStr);
	  if (tempReg.test(tempStr)) {
		  supported = 1;
	  }
  }
  if (!supported) {
	  return (arStr.indexOf(".") > 2) && (arStr.indexOf("@") > 0);
  }
  var r1 = new RegExp("(@.*@)|(\\.\\.)|(@\\.)|(^\\.)");
  var r2 = new RegExp("^.+\\@(\\[?)[a-zA-Z0-9\\-\\.]+\\.([a-zA-Z]{2,6}|[0-9]{1,6})(\\]?)$");
  return (!r1.test(arStr) && r2.test(arStr));
}

//----- Remove spaces, tabs and carriage returns from a string. -----
function stripWhiteSpace(arStr) {
	var cnt;
	var thisChar;
	var returnString = "";
	var whiteSpace = " \t\n\r";

	for ( cnt = 0; cnt < arStr.length; cnt++) {
		 thisChar = arStr.charAt(cnt);
		 if ( whiteSpace.indexOf(thisChar) == -1) returnString += thisChar;
	}

	return returnString;
}

// Use JS to embed Flash into an HTML page to skip new IE changes
// Go to http://www.adobe.com/devnet/activecontent/articles/devletter.html for more details...
function DisplayFlash(FlashRef)
{
    document.write('<object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" codebase="http://fpdownload.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=7,0,0,0" width="760" height="229" id="' + FlashRef + '" align="middle">\n');
    document.write('<param name="allowScriptAccess" value="sameDomain" />\n');
    document.write('<param name="movie" value="' + FlashRef + '" />\n');
    document.write('<param name="menu" value="false" />\n');
    document.write('<param name="quality" value="best" />\n');
    document.write('<param name="bgcolor" value="#ffffff" />\n');
    document.write('<embed src="' + FlashRef + '" menu="false" quality="best" bgcolor="#ffffff" width="760" height="229" name="' + FlashRef + '" align="middle" allowScriptAccess="sameDomain" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" />\n');
    document.write('</object>\n');
}

//----- Underline buttons on hover. Call usage:  onmouseover="buttonUnderline(this, 'on');"
function buttonUnderline(arObj, arOnOff) {

	var curText;

	// Windows IE 7 does not need this; handled with CSS. IE 6 needs it done differently.
	if (navigator.appName == "Microsoft Internet Explorer" && navigator.userAgent.indexOf("Opera")==-1 && navigator.userAgent.indexOf("Mac")==-1) {
		if (navigator.appVersion.indexOf("MSIE 6") != -1) {
			if (arOnOff == 'on') {
				arObj.style.textDecoration = 'underline';
			} else {
				arObj.style.textDecoration = 'none';
			}
		}
	} else {
		// Apple Safari does not need this either.
		if (navigator.userAgent.indexOf("Safari") == -1) {
			// For Firefox, Opera and other browsers.
			curText = arObj.innerHTML;

			if (arOnOff == 'on') {
				arObj.innerHTML= '<u>' + curText + '</u>';
			} else {
				curText = curText.replace('<u>','');
				curText = curText.replace('<U>','');
				curText = curText.replace('</u>','');
				curText = curText.replace('</U>','');
				arObj.innerHTML= curText;
			}
		}
	}
}

//----- Set the document onload function. ---------------------------------
window.onload=DDMenuStart;
