//************************************************************************************************************* CORE FUNCTIONS

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_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_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];}
}


//*********************************************************************************************************** COOKIE FUNCTIONS

function getCookieData(labelName)
// returns cookie data from corresponding label, or null if label does not exist.
{
	var cookieData 		= document.cookie;		// grab the data once
	var cookieEnd;

	for (i=0; i < cookieData.length; i++)
	{
		var j = i + labelName.length;
		if (cookieData.substring(i,j) == labelName)
		{
			cookieEnd = cookieData.indexOf(";",j)
			if (cookieEnd == -1) { cookieEnd = cookieData.length; }
			
			return unescape(cookieData.substring(j+1, cookieEnd))
		}
	}
	return null;
}

function delCookieData(labelName)
// deletes specific cookie by label name
{
	document.cookie = labelName + "=" + ";expires=Thursday, 01 Jan 1970 00:00:01 GMT;";
	return null;
}

function setCookieData(name,value,days)
{
	if (days)
	{
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	}
	else var expires = "";
	document.cookie = name+"="+value+expires+"; path=/";
}

//***************************************************************************************************************** ISM FUNCTIONS

function jumpToCtrlOnCRLogin(evt, ctrl_id)
// General-purpose keypress handler--sets focus to a control when Enter (CR) is pressed.
// Note that this function does NOT consume the CR char--it'll stil act on the control given
// focus.  Add "return false;" to function to consume the CR...
{
  evt = evt || event;
  if ((evt.keyCode | evt.which) == 13)
  {
    document.LoginInfo.elements[ctrl_id].focus();
  }
}


function jumpToCtrlOnCR(evt, ctrl_id)
// General-purpose keypress handler--sets focus to a control when Enter (CR) is pressed.
// Note that this function does NOT consume the CR char--it'll stil act on the control given
// focus.  Add "return false;" to function to consume the CR...
{
  evt = evt || event;
  if ((evt.keyCode | evt.which) == 13)
  {
    document.frmSearch.elements[ctrl_id].focus();
  }
}


/******************************** Info Popup ********************************************/

function findPos(obj)
{
	var curleft = curtop = 0;
	if (obj.offsetParent)
	{
		curleft = obj.offsetLeft
		curtop = obj.offsetTop
		while (obj = obj.offsetParent)
		{
			curleft += obj.offsetLeft
			curtop += obj.offsetTop
		}
	}
	return [curleft,curtop];
}

function showInfoPopup(oSender, strContent, xOffset, yOffset)
{	
	// Insert the content into the div
	document.getElementById("divInfoPopup").innerHTML = strContent;
	
	// Position the div based on the caller's position
	document.getElementById("divInfoPopup").style.left = (findPos(oSender)[0] + xOffset) + "px";
	document.getElementById("divInfoPopup").style.top = (findPos(oSender)[1] + yOffset) + "px";
	
	// Display the div
	document.getElementById("divInfoPopup").style.display ="block"
	
	startObjectFadeIn(document.getElementById("divInfoPopup"));
}

function hideInfoPopup()
{
	startObjectFadeOut(document.getElementById("divInfoPopup"));
	(hideInfoPopup.arguments[0]) ? document.getElementById("divInfoPopup").style.display = "none" : doNothing();
}

function doNothing() {}

var global_iFadeStep = 5		// By what percent should I change the opacity per cycle?
var global_iFadeRate = 1		// How long should a fade cycle be (msec)?

var global_iCurrentOpacity = 0
var global_oTimerOpacity
var global_oTarget


function startObjectFadeIn(oTarget)
{
	global_oTarget = oTarget									// has to be accessible to the timer after this function has lost scope
	clearInterval(global_oTimerOpacity)
	global_oTimerOpacity = setInterval("doObjectFade('in')",global_iFadeRate)
}

function startObjectFadeOut(oTarget)
{
	//alert(oTarget.id);
	global_oTarget = oTarget									// has to be accessible to the timer after this function has lost scope
	clearInterval(global_oTimerOpacity)
	global_oTimerOpacity = setInterval("doObjectFade('out')",global_iFadeRate)
}

function setObjectOpacity(iOpacity)
// Sets the opacity of oTarget based on a 0-100 index where 0 is transparent and 100 is opaque.
{
	// Are we in IE?
	if (window.global_oTarget.filters) { global_oTarget.filters.alpha.opacity = iOpacity }
	else { global_oTarget.style.MozOpacity = iOpacity / 100 }
	
}

function doObjectFade(strFadeDirection)
// Handles actually changing the opacity of the object depending on if we're 
// fading in or out.  Clears timer if we're done fading.
{
	// Are we fading in and we're not maxed out yet?
	if (strFadeDirection == "in" && global_iCurrentOpacity < 100)
	{
		setObjectOpacity(global_iCurrentOpacity += global_iFadeStep)
	}
	// Are we fading out and we haven't bottomed out?
	else if (strFadeDirection == "out" && global_iCurrentOpacity > 0)
	{
		setObjectOpacity(global_iCurrentOpacity -= global_iFadeStep)
	}
	else if (window.global_oTimerOpacity)
	{
		clearInterval(global_oTimerOpacity)
		global_iCurrentOpacity = (strFadeDirection == "in") ? 100 : 0
		if (strFadeDirection == "out") { global_oTarget.style.display = "none" }
	}
}
