// commonroutines.js

// Javascript Includes
document.write("<script language='javascript' src='java/jquery.min.js'></script>");
document.write("<script language='javascript' src='java/FormValidator.js'></script>");
document.write("<script language='javascript' src='java/SlidingDiv.js'></script>");
document.write("<script language='javascript' src='java/StyleExtensions.js'></script>");

// parse string date to Date object
function parseDate(dstr,fmt) {
	if (fmt == null) fmt = 'mm/dd/yy';
	return top.$.datepicker.parseDate(fmt,dstr);
}


// Browser-safe getElementById
function getElementById(doc,id) {
	if (doc == null) {
		doc = document;
	}
	if (doc.getElementById != null) {
		return doc.getElementById(id);
	}
	if (doc.all != null) {
		return doc.all[id];
	}
	if (doc.layers != null) {
		return doc.layers[id];
	}
	return null;
}

function getElementsByName(doc,name) {
	if (doc.getElementsName != null) {
		return doc.getElementsByName(name);
	}
	if (doc.all != null) {
		return doc.all[name];
	}
	if (doc.layers != null) {
		return doc.layers[name];
	}
	// try forms
	var elems = new Array;
	for (i=0; i<doc.forms.length; i++) {
		for (j=0; j<doc.forms[i].elements.length; j++) {
			if (doc.forms[i].elements[j].name == name) {
				elems.push(doc.forms[i].elements[j]);
			}
		}
	}
	return elems.length > 0 ? elems : null;
}

function getParameterValue(parmName) {
	var qstr = window.top.location.search.substring(1);
	var pname = parmName+"=";
	beg = qstr.indexOf(pname);
	if (beg == -1) { return "null"; }
	beg += pname.length;
	end = qstr.indexOf("&",beg);
	if (end == -1) { end = qstr.length; }
	return unescape(qstr.substring(beg,end));
}

//FRAME CHECKING ROUTINES
function CheckFrames(strPage) {
	if (top.location.href != location.href){
		if(parent.frames.length!=0){
			parent.location.href=strPage;
		}
	}
}
// Break out of Frames
function BreakFrames(strPage) {
	if(parent.frames.length!=0){
		parent.location.href=strPage;
	}
}
//Force Frames within the application
function ForceFrames(strPage) {
	if (parent.location.href == self.location.href) {
    	window.location.href = 'MainPage.jsp?page='+strPage;		
	}
}
	
//Function for jumping to other URL
function jumptoURL(hreflink) {
	window.location = hreflink;
}

//Function for jumping to other URL
function setMainURL(hreflink) {
	parent.frames.main.location = hreflink;
}

function ShowMsgs(thisObj, thisEvent) {
	//alert('ChkBox Checked: '+ document.getElementById('chkMsgs').checked);
	strVal = "";
	if(getElementById(document,'chkMsgs').checked==true) {
		//Show Message Frame
		getElementById(top.document,'mainSet').rows = '70,*,70'; 
		strVal = "checked";
	} else {
		//Hide Message Frame
		getElementById(top.document,'mainSet').rows = '70,*,0'; 
	}
	//Set chkMsgVal Cookie info
	setCookie("ELANYchkMsgVal",strVal,null); 
}

// Sets cookie values. Expiration date is optional 
// 
function setCookie(name, value, expire) { 
   document.cookie = name + "=" + escape(value) + ((expire == null) ? "" : ("; expires=" + expire.toGMTString()));
} 
//Gets a specified Cookie
function getCookie(Name) { 
   var search = Name + "=" 
   if (document.cookie.length > 0) { // if there are any cookies 
      offset = document.cookie.indexOf(search) 
      if (offset != -1) { // if cookie exists 
         offset += search.length 
         // set index of beginning of value 
         end = document.cookie.indexOf(";", offset) 
         // set index of end of cookie value 
         if (end == -1) 
            end = document.cookie.length 
         return unescape(document.cookie.substring(offset, end)) 
      } 
   }
}

var swImg; swImg=new Array;
//Roll-Over Image functions
//
//  Licensed Materials - Property of IBM
//  (C) Copyright IBM Corp. 1998, 2001 All Rights Reserved.
//  US Government Users Restricted Rights -
//  Use, duplication or disclosure restricted
//  by GSA ADP Schedule Contract with IBM Corp.
//

// HpbImgPreload:
//
function HpbImgPreload()
{
    if (document.images)
    {
      var imgName = HpbImgPreload.arguments[0];
      var cnt;
      swImg[imgName] = new Array;
      for (cnt = 1; cnt < HpbImgPreload.arguments.length; cnt++)
      {
        swImg[imgName][HpbImgPreload.arguments[cnt]] = new Image();
        swImg[imgName][HpbImgPreload.arguments[cnt]].src = HpbImgPreload.arguments[cnt];
      }
    }
}
// HpbImgFind:
//
function HpbImgFind(doc, imgName)
{
  for (var i=0; i < doc.layers.length; i++){
    var img = doc.layers[i].document.images[imgName];
    if (!img) img = HpbImgFind(doc.layers[i], imgName);
    if (img) return img;
  }
  return null;
}
// HpbImgSwap:
//
function HpbImgSwap(img, imgSrc){
	if (document.images){
	  //var img = document.images[imgName];
	  //if (!img) img = HpbImgFind(document, imgName);
	  if (img) img.src = imgSrc;
	}
}

// LocalBeep
//-----------------------------------------------------------------
// Cause the system beep to be played.
//	* Non-InternetExplorer: will get the AWT Toolkit to do the beep
//	* InternetExplorer: Must have the following element in the <HEAD>
//		portion of the document:
//    		<BGSOUND id="IEBeeper" />
//		Will play whatever wav file is specified (not necessarily just a beep)
//
function localBeep() {
	if (document.all) {
		var relativePathToWaveFile = "theme/Ding.wav";
		getElementById(document,"IEBeeper").src = relativePathToWaveFile;
	} else {
		try {
			java.awt.Toolkit.getDefaultToolkit().beep();
		} catch (err) {
			// no awt installed, so this user does not get a beep
		}
	}
	return false;
}

// absoluteX
//-----------------------------------------------------------------
// Calculates the absolute page Y coordinate of any element relative
// to its top-most container (frame/body)
function absoluteX(element)
{
	var curleft = 0;
	if (element.offsetParent)
	{
		curleft += element.offsetLeft
		while (element = element.offsetParent)
			curleft += element.offsetLeft
	}
	else if (element.x)
		curleft += element.x;
		
	return curleft;
}

// absoluteX
//-----------------------------------------------------------------
// Calculates the absolute page Y coordinate of any element relative
// to its top-most container (frame/body)
function absoluteY(element)
{
	var curtop = 0;
	if (element.offsetParent)
	{
		curtop += element.offsetTop
		while (element = element.offsetParent)
			curtop += element.offsetTop
	}
	else if (element.y)
		curtop += element.y;
	return curtop;
}

// Array.intersection(Array2)
//-----------------------------------------------------------------
//	[This function is added to the Array class and can be called
//  	on any array, including those created by the system]
// Creates an Array wich represents the intersection of the Array
// on which the function is called and the Array passed as a 
// parameter
Array.prototype.intersection = function(arr2)  {
	var returnArray = new Array(); 
	var y = 0;
	for(var x=0;x<this.length;x++) {
		if(arr2.contains(this[x])) {
			returnArray[y++] = this[x];
		}
	}
	return returnArray;
} 

// Array.contains(object)
//-----------------------------------------------------------------
//	[This function is added to the Array class and can be called
//  	on any array, including those created by the system]
// Returns true of the Array contains at least one reference to object
Array.prototype.contains = function(object) {
	for(var x=0;x<this.length;x++) {
		if(this[x]==object) {
			return true;
		}
	}
	return false;
} 							

// ----------------------------------------------------------------
// ----------------------------------------------------------------
// JavaScript Stack Trace Utilities
// ----------------------------------------------------------------
function stackGetFunctionName(theFunction) 
{ 
	if(theFunction.name) { return theFunction.name; } 
	// try to parse the function name from the defintion 
	var definition = theFunction.toString(); 
	var fi = definition.indexOf('function') + 8;
	definition = definition.substring(fi);
	var ei = definition.indexOf('(');
	var name = definition.substring(0,ei); 
	if(name) return name; 
	// sometimes there won't be a function name 
	// like for dynamic functions 
	return "anonymous"; 
}
function stackGetSignature(theFunction) 
{ 
	var signature = stackGetFunctionName(theFunction); 
	signature += "("; 
	for(var x=0; x<theFunction.arguments.length; x++) { 
		// trim long arguments 
		var nextArgument = theFunction.arguments[x]; 
		if(nextArgument.length > 30) nextArgument = nextArgument.substring(0, 30) + "..."; 
		// apend the next argument to the signature 
		signature += "'" + nextArgument + "'"; 
		// comma separator 
		if(x < theFunction.arguments.length - 1) 
			signature += ", "; 
	} 
	signature += ")"; 
	return signature; 
}
function stackTraceText(startingPoint) 
{ 
	var stackTraceMessage = "Stack trace: <br>\n"; 
	stackTraceMessage += stackTrace(startingPoint).join("<br>\n");
	stackTraceMessage += "<br>\n\n";
	return stackTraceMessage;
}
function stackTrace(startingPoint) 
{ 
	var a = new Array();
	var nextCaller = startingPoint; 
	while(nextCaller) { 
		a.push(stackGetSignature(nextCaller)); 
		nextCaller = nextCaller.caller; 
	} 
	return a;
}

// ----------------------------------------------------------------
// ----------------------------------------------------------------
// Tab panel manipulation
// ----------------------------------------------------------------
// Will cause the tab at index tabNum to be hidden
//   (tabs indexes are 1 based!)
//
// *** For this to work in IE, the method needs to be called
// AFTER onPageload has completed.   This means you need to set
// a timeout for 10ms in onPageload to call this method.
function removeTabAtIndex(tabNum) {
			var tabs = document.getElementsByTagName("table");
			for (i=tabs.length-1; i>0; --i) {
			if (!tabs[i].id)
				continue;
			if (tabs[i].id.indexOf(":pnl") != -1 &&
				tabs[i].id.indexOf("hyperlink_"+tabNum) != -1) {
				tabs[i].style.display = "none";
				break;
			}
	}
	return true;
}


function dateToString(indate) {
	dt = "";
	if (indate.getMonth()+1 < 10)
		dt += "0";
	dt += (indate.getMonth()+1);
	dt += "/";
	if (indate.getDate() < 10)
		dt += "0";
	dt += (indate.getDate());
	dt += "/";
	dt += indate.getFullYear();
	return dt;
}

function getInputsByType(doc,type) {
	var ins = doc.getElementsByTagName("input");
	var a = new Array;
	for (var i = 0; i<ins.length; i++) {
		if (ins[i].type == type) {
			a.push(ins[i]);
		}
	}
	return a;
}
