<!--//

// This file contains general JavaScript functions.

// ----------------------------------------------------------------------------------------

// This is the JavaScript equivalent of the VBScript InStr function. It checks for the
// existance of one string within another.

function inStr(strString1, strString2) {
	re = new RegExp(strString2, "ig");  // ig = Ignore case, search globally
	indicator = strString1.search(re)
	if (indicator != -1) {
		return true;
	}
	return false;
}

// ----------------------------------------------------------------------------------------

// This is the Javascript equivalent of the VBScript Left function.
// Author: Jason Karlin, 6/21/2002

function fnLeft(strString, iNumCharacters) {
	
	// If strString is a number, convert it to the string representation of the number
	strString = strString.toString();
	
	return strString.substr(0, iNumCharacters);

}

// ----------------------------------------------------------------------------------------

// This is the Javascript equivalent of the VBScript Right function.
// Author: Jason Karlin, 6/21/2002

function fnRight(strString, iNumCharacters) {
	
	// If strString is a number, convert it to the string representation of the number
	strString = strString.toString();
	
	var startPos = strString.length - iNumCharacters;
	return strString.substr(startPos, iNumCharacters);

}

// ----------------------------------------------------------------------------------------

// This function is used to move one or more options from one select list to another. This function is
// commonly called by forms that manage user roles.
// Author: Troy Bugos or Jill Fears?
// Modified By: Jason Karlin, 06/26/02 (Added code to prevent prompt from being moved)

function fnOptionSwap(oDestSel, oSourceSel) {
	with(oSourceSel) {
		for (i=0;i<options.length;i++) {
			if (options[i].selected && options[i].value != "-1"){
				oDestSel.options[oDestSel.options.length] = new Option(options[i].text, options[i].value);
				options[i--] = null;
			}
		}
	}
}

// ----------------------------------------------------------------------------------------

// This function is used to add an option to a select list.
// Author: Jason Karlin, 6/26/2002

function fnAddOption(oSel, strOptText, intOptValue) {
	var oOption = new Option(strOptText, intOptValue);
	oSel.options.add(oOption);
}

// ----------------------------------------------------------------------------------------

// This function is used to select all options multiple-select select list.
// Author: Jason Karlin, 6/26/2002

function fnSelAllOptions(oSel) {
	if(oSel) {
		for(i=0; i<oSel.options.length; i++) {
			if(oSel.options[i].value != -1) {
				oSel.options[i].selected = true;
			}
		}
	}
}

// ----------------------------------------------------------------------------------------

// This function is used to set all elements in a form (except buttons) to disabled or read-only
// (depending on element type) and set the background color of the elements to Lemon
// Chiffon (light yellow).
// Author: Jason Karlin, 7/25/2002

function fnDisableElements(strFormName) {
		
	var objForm = eval(strFormName);
	var iFormFieldCount = objForm.elements.length;
	for (i = 0; i < iFormFieldCount; i++) {
   		with(objForm.elements[i]) {
   			if(type=="submit" || type=="button") {
				continue;
			}
   			if(type=="radio") {
				disabled = true;
				continue;
			}
   			if(tagName == "SELECT") {
				disabled = true;
				style.backgroundColor = "Lemonchiffon";
				style.color = "black";
				continue;
			}
			if(tagName == "TEXTAREA") {
				readOnly = true;
				className = "dataNoColor";
				style.backgroundColor = "Lemonchiffon";
				continue;
			}
  			readOnly = true;
   			className = "ReadOnlyYellow";
   		}
   	}
}

// ----------------------------------------------------------------------------------------

// This function is used to set a single element in a form to disabled or read-only
// (depending on element type), set the background color of the element to Lemon
// Chiffon (light yellow), and clear the validation type. If the element is a select list,
// the function selects the first item in the list.
// Author: Jason Karlin, 7/25/2002

function fnSetReadOnly(oElem, oElemObject) {
	with(oElem) {
		if(tagName == "SELECT") {
			if(oElem.options) {
				// Empty the select list by clearing its innerHTML.
				innerHTML = "";
				// Add the prompt.
				oElem.options[0] = new Option('Select One');
				oElem.options[0].value = -1;
			}
			if(oElemObject) {
				oElemObject.selectedID = (!PageObject.pageReload) ? "" : oElemObject.selectedID;
			}
		}
		if(tagName == "SELECT" || type == "radio") {
			oElem.disabled = true;
		}
		if(tagName != "SELECT" && type != "radio") {
			oElem.readOnly = true;
		}
		if(type != "radio") {
			oElem.style.backgroundColor = "Lemonchiffon";
			oElem.style.color = "black";
			oElem.validationType = "";
		}
	}
}

// ----------------------------------------------------------------------------------------

// This function is used to clear a single element's disabled or read-only properties,
// clearing the background color of the element and setting the validation type.
// Author: Jason Karlin, 7/25/2002

function fnClearReadOnly(oElem, strValType) {
	with(oElem) {
		if(tagName == "SELECT" || type == "radio") {
			oElem.disabled = false;
		}
		else {
			oElem.readOnly = false;
		}
		oElem.className = "";
		oElem.validationType = strValType;
	}
}

// ----------------------------------------------------------------------------------------

// Removes horizontal tabs, carriage returns, and line feeds from a string
// Author: Jason Karlin, 9/13/2002

function fnRemoveCRLF(strString) {
	
	var newString = "";
	for(i=0; i<strString.length; i++) {
		var charString = strString.charAt(i);
		var code = strString.charCodeAt(i);	// Get the Unicode value of the character at index i.
		switch(code) {
			case 9:
				// Horizontal Tab
				newString += "";
				break;
			case 10:
				// Line Feed
				newString += "";
				break;
			case 13:
				// Carriage Return
				newString += "";
				break;
			default:
				newString += charString
		}
	}
	strString = newString
	
	// Trim off leading and trailing spaces and return the string
	return(fnTrim(strString));
	
}

// ----------------------------------------------------------------------------------------

// Used to convert a string to an array. Delimiter is hard coded to ||.

function fnDataStringToArray(strData) {
	
	aryData = strData.split("||"); // Data string is delimited by "||";
	return(aryData);
		
}

// ----------------------------------------------------------------------------------------

// Used to convert a string to an array. Delimiter is passed into the function, defaults to ||.

function fnStringToArray(s, delim){
	
	var d = (delim == null) ? '||' : delim;
	return s.split(d);
}


// ----------------------------------------------------------------------------------------

// Used to trim leading and trailing spaces off of a string. Similar to the VB/VBScript Trim function.

function fnTrim(strString) {

	while(''+strString.charAt(strString.length-1)==' ') {
		strString = strString.substring(0,strString.length-1);
	}
	while(''+strString.charAt(0)==' ') {
		strString = strString.substring(1,strString.length);
	}
	return(strString);
}

// ----------------------------------------------------------------------------------------

// Used to determine what objects are in the prototype chain for an object.

function instanceOf(object, constructor) {
	
	while (object != null) {
		if (object == constructor.prototype)
			return true;
		object = object.__proto__;
	}
	return false;

}

//=========================================================

function fnFormCleanup(strString) {
	
	// Replace quotation marks with HTML equivalent
	var re = /(\")/g;
	var strTemp = strString.replace(re, '&#34;')
	
	// Replace single ticks with double single ticks
	re = /(\')/g;
	strTemp = strTemp.replace(re, '\'\'')
	//strTemp = strTemp.replace(re, '&#39;')
	
	// Convert whitespaces to HTML equivalents
	var charString;
	var code;
	var newString = "";
	for(i=0; i<strTemp.length; i++) {
		charString = strTemp.charAt(i);
		code = strTemp.charCodeAt(i);	// Get the Unicode value of the character at index i.
		switch(code) {
			case 9:
				// Horizontal Tab
				newString += "&#09;";
				break;
			case 10:
				// Line Feed
				newString += "&#10;";
				break;
			case 13:
				// Carriage Return
				newString += "&#13;";
				break;
			default:
				newString += charString
		}
	}
	strTemp = newString
	
	re = /\\/g;	// Look for backslashes
	var indicator = strTemp.search(re)
    if (indicator >= 0) {	// Found a backslash
		// Replace all backslashes with the HTML equivalent
		strTemp = strTemp.replace(re, '&#92;')
	}
	
	// Trim off leading and trailing spaces and return the string
	return(fnTrim(strTemp));
	
}

// ----------------------------------------------------------------------------------------

// This function is used to select an option on a select list, given the selected ID value.
// Author: Jason Karlin, 10/03/2002

function fnMakeSelection(oSelectList, intSelectedID) {
	oSelectList.disabled = false;
	oSelectList.style.backgroundColor = "#ffffff";
	oSelectList.style.color = "black";
	if(intSelectedID.length == 0) {
		oSelectList.options[0].selected = true;
		return;
	}
	for(i=0; i < oSelectList.options.length; i++) {
		if(oSelectList.options[i].value == intSelectedID) {
			oSelectList.options[i].selected = true;
			break;
		}
	}
}

// ----------------------------------------------------------------------------------------

// This function is used to reset the text color and backgroud color of an element.
// Author: Jason Karlin, 10/03/2002

function fnInitElemColor(oElem) {
	oElem.style.backgroundColor = "#ffffff";
	oElem.style.color = "black";
}

// ----------------------------------------------------------------------------------------

// This function is used to open a new browser window. All function input parameters are
// optional. The function contains default values for each parameter. These default values
// can be edited for each application, as desired.
// Author: Jason Karlin, 10/15/2002

function fnOpenWindow(url, width, height, top, left, resizable, scrollbars, toolbar, 
titlebar, menubar, location, status, fullscreen, directories, channelmode, windowname, replace) {
	
	// Default the url to the application online help file:
	url = (url) ? url : "TempHelp.jsp";
	
	windowname = (windowname) ? windowname : "_blank";
	replace = (replace) ? replace : false;
	
	aryFeatures = new Array();

	aryFeatures[0]		=	(width) ? "width="+width : "width=850";
	aryFeatures[1]		=	(height) ? "height="+height : "height=600";
	aryFeatures[2]		=	(top) ? "top="+top : "top=50";
	aryFeatures[3]		=	(left) ? "left="+left : "left=50";
	aryFeatures[4]		=	(resizable) ? "resizable="+resizable : "resizable=yes";
	aryFeatures[5]		=	(scrollbars) ? "scrollbars="+scrollbars : "scrollbars=yes";
	aryFeatures[6]		=	(toolbar) ? "toolbar="+toolbar : "toolbar=no";
	aryFeatures[7]		=	(titlebar) ? "titlebar="+titlebar : "titlebar=no";
	aryFeatures[8]		=	(menubar) ? "menubar="+menubar : "menubar=no";
	aryFeatures[9]		=	(location) ? "location="+location : "location=no";
	aryFeatures[10]	=	(status) ? "status="+status : "status=yes";
	aryFeatures[11]	=	(fullscreen) ? "fullscreen="+fullscreen : "fullscreen=no";
	aryFeatures[12]	=	(directories) ? "directories="+directories : "directories=no";
	aryFeatures[13]	=	(channelmode) ? "channelmode="+channelmode : "channelmode=no";
	
	auxWindow = window.open(url, windowname, aryFeatures.join(","), replace);
    
    if (auxWindow.opener == null) {
		auxWindow.opener = window;
	}
    else {
		auxWindow.focus();
	} 
	
	return auxWindow;
}

// ----------------------------------------------------------------------------------------

// This function is a "Back" button event handler. It will either send the user back one URL in
// the browser's history list (if the history list has anything in it), or to a specified default URL
// (sent in as a parameter). If the history list is empty and no URL was sent in, an error message
// is displayed.
// Author: Jason Karlin, 11/11/2002

function fnBack(defaultUrl) {
	if(history.length > 0) {
		// Send user to the previous page in the browser's history list.
		history.go(-1);
	}
	else {
		// The history list is empty. Send user to the specified default URL.
		if(defaultUrl) {
			window.location.href(defaultUrl);
		}
		else {
			alert("No default URL was specified for the 'Back' button. Please contact the Help Desk");
		}
	}
}

// ----------------------------------------------------------------------------------------

// This function is used to set the tab indices on all elements in a form (except hidden fields).
// Author: Jason Karlin, 10/24/2002

function fnSetTabIndices() {
		
	var cnt = 1;
	var formColl = document.all.tags("FORM");
	for (i = 0; i < formColl.length; i++) {
		for (j = 0; j < formColl[i].elements.length; j++) {
   			with(formColl[i].elements[j]) {
				if(tagName == "FIELDSET") { continue; }
   				if(type=="hidden") { 	continue; }
				if(formColl[i].elements[j].style.display == "none") { continue }
   				tabIndex = cnt;
   				cnt++;
   			}
   		}
   	}
}

// ----------------------------------------------------------------------------------------

// This function is used to replace single backslashes in a file path w/ double backslashes. This prevents
// Javascript from treating the backslashes like escape characters and stripping them out.
// Author: Jason Karlin, 12/16/2002

function fnModifyFilePath(strPath) {
	var re = /(\\)/g;
	return strPath.replace(re, '\\\\');
}

// ----------------------------------------------------------------------------------------

// This function is used to determine the boolean value of a checkbox element's current state.
// Author: Jason Karlin, 12/31/2002

function fnGetBooleanValue(oElem) {
	if(oElem.checked == true) {
		return true;
	}
	return false;
}

// ----------------------------------------------------------------------------------------

// This function is used to set the validation type of an optional field. If the field has been filled
// out, the appropriate validation type is set. If it hasn't been filled out, the validation type is
// cleared.
// Author: Jason Karlin, 1/2/2003

function fnCheckOptField(oElem, strValue, strValType) {
	if(strValue.length > 0) {
		oElem.setAttribute("validationType", strValType);
		return;
	}
	oElem.setAttribute("validationType", "");
}

// ----------------------------------------------------------------------------------------

// This function is used to get the current date in the format mm/dd/yyyy.
// Author: Jason Karlin, 1/21/2003

function getTodaysDate() {
   var d										//Declare variables.
   var s
   s = ""				
   d = new Date();						//Create Date object.
   s += (d.getMonth() + 1) + "/";		//Get month
   s += d.getDate() + "/";				//Get day
   s += d.getYear();						//Get year.
   return(s);								//Return date.
}

// ----------------------------------------------------------------------------------------

// This function returns a number as a string in the format xxxxxx.xx
// Author: Copied and hacked from the internet.

function formatNumber(anynum) {
	anynum = "" + eval(anynum);  // Evaluate (in case an expression sent)
	intnum = parseInt(anynum);  // Isolate integer portion
	intstr = "" + intnum;

	decnum = Math.abs(parseFloat(anynum) - parseInt(anynum)); // Isolate decimal portion
	decnum = decnum * 100; // Multiply decimal portion by 100.
	decstr = "" + Math.abs(Math.round(decnum));
	if (decstr.length > 2) {
		decstr = decstr.substring(0, 2);
	}
	while (decstr.length < 2) {
		decstr = "0" + decstr;
	}
	retval = intstr + "." + decstr;
	return retval;
}


function showhide(rowName) {
	element = document.getElementById(rowName);
	if (element != null) {
		if (element.style.display == "none") {
			element.style.display = "";
		}
		else {
			element.style.display = "none";
		}
	}
}

function show(id) {
	element = document.getElementById(id);
	if (element != null) {
		element.style.display = "";
	}
}
	
function hide(id) {
	element = document.getElementById(id);
	if (element != null) {
		element.style.display = "none";
	}
}

// ----------------------------------------------------------------------------------------

//-->

<!--
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_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_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];}
}

function replaceImage(img, src) {
	if (img != null) {
		img.src = src;
	}
}

function validateNumeric(event) {
    var unicode = event.charCode;
    if (unicode == null) { unicode = event.keyCode; }
    if ( (unicode < 48 || unicode > 57) && unicode != 0 && unicode != 8 & unicode != 9) {
    	if (navigator.appName=="Microsoft Internet Explorer") {
    		window.event.returnValue = false;
    	}
    	else {
	    	event.preventDefault();
		}
		    	
	    return false;
	}
	else { return true; }
}

function limitTextArea(fieldId, maxLength, fieldHelpId) {
	if ($(fieldId) != null) {
		if ($(fieldId).value.length > maxLength) {
			$(fieldId).value = $(fieldId).value.substring(0, maxLength);
		}
		else {
			$(fieldHelpId).innerHTML = maxLength - $(fieldId).value.length;
		}
	}
}

//-->
