Number.prototype.NaN0=function(){return isNaN(this)?0:this;}

// CREDITS: // Automatic Page Refresher by Peter Gehrig and Urs Dudli www.24fun.com
// Permission given to use the script provided that this notice remains as is.
// Additional scripts can be found at http://www.hypergfurl.com.
// Configure refresh interval (in seconds)
var refreshinterval = 60
// Shall the coundown be displayed inside your status bar? Say "yes" or "no" below:
var displaycountdown = "yes"
// Do not edit the code below
var starttime
var nowtime
var reloadseconds = 0
var secondssinceloaded = 0 
function starttime() {
	starttime=new Date();
	starttime=starttime.getTime();
	countdown();
}

function countdown() {
	nowtime = new Date();
	nowtime = nowtime.getTime();
	secondssinceloaded = (nowtime - starttime) / 1000;
	reloadseconds = Math.round(refreshinterval - secondssinceloaded);
	if (refreshinterval >= secondssinceloaded) {
		var timer = setTimeout("countdown()",1000);
		if (displaycountdown == "yes")  {
			window.status = "Page refreshing in " + reloadseconds + " seconds";
		}
	} else {
		clearTimeout(timer);
		window.location.reload(true);
	}
}

// create global xmlHttp object for Ajax coolness
var xmlHttp = null;
if (window.ActiveXObject) {
	xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
} else if (window.XMLHttpRequest) {
	xmlHttp = new XMLHttpRequest();
}

/* IN - num:            the number to be formatted
	   decimalNum:     the number of decimals after the digit
	   bolLeadingZero: true / false to use leading zero
	   bolParens:      true / false to use parenthesis for - num

  RETVAL - formatted number
*/
function FormatNumber(num, decimalNum, bolLeadingZero, bolParens) {
  	var tmpNum = num;

  	// Return the right number of decimal places
  	tmpNum *= Math.pow(10, decimalNum);
  	tmpNum = Math.floor(tmpNum);
  	tmpNum /= Math.pow(10, decimalNum);

  	var tmpStr = new String(tmpNum);
	
  	// See if we need to hack off a leading zero or not
  	if (!bolLeadingZero && num < 1 && num > -1 && num !=0)
		if (num > 0)
			tmpStr = tmpStr.substring(1,tmpStr.length);
		else
			// Take out the minus sign out (start at 2)
			tmpStr = "-" + tmpStr.substring(2,tmpStr.length);                        

	// See if we need to put parenthesis around the number
	if (bolParens && num < 0)
		tmpStr = "(" + tmpStr.substring(1,tmpStr.length) + ")";
	
	return tmpStr;
}

function FormatCent(num) {
	num -= 0;
	return (num == Math.floor(num) ? num + '.00' : (num * 10 == Math.floor(num * 10) ? num + '0' : num));
}

function NVL(value, valueIfNull) {
	if (value == null)
		return valueIfNull;
	else
		return value;
}

// remove leading spaces 
function ltrim(strText) {
	var str = new String(strText);
	
	if (str > "") {
		while (str.substring(0,1) == ' ' || str.substring(0, 1) == "\n" || str.substring(0, 1) == "\r") 
			str = str.substring(1, str.length);
	}
	
	return str.toString();
}

// remove trailing spaces 
function rtrim(strText) {
	var str = new String(strText);
	
	if (str > "") {
		while (str.substring(str.length-1, str.length) == ' ' || str.substring(str.length-1, str.length) == "\n" || str.substring(str.length-1, str.length) == "\r")
			str = strText.substring(0, str.length-1);
	}
	
	return str.toString();
}

// remove leading and trailing spaces
function trim(str) {
	return(rtrim(ltrim(str)));
} 

function left(str, n){
	if (n <= 0)
		return "";
	else if (n > String(str).length)
		return str;
	else
		return String(str).substring(0,n);
}

function right(str, n){
	if (n <= 0)
		return "";
	else if (n > String(str).length)
		return str;
	else {
		var iLen = String(str).length;
		return String(str).substring(iLen, iLen - n);
	}
}

function filterKeys(e) {	
	if (e && e.which) {
		what = this;
		code = e.which;
			
	} else {
		what = event.srcElement;
		code = event.keyCode;
	}
	
	if (!(code >=32 && code <= 165) || code == 127) return;
		
	var filterType = what._type;
	var allowLeadingSpace = what._allowLeadingSpace;
		
	if (allowLeadingSpace != null && code == 32 &&
		(allowLeadingSpace == "0" || allowLeadingSpace == "false") &&
		trim(what.value) == "") {
		
		if (!e) event.keyCode = 0;
		return false;
	}

	if (filterType == "alphanumeric") {
		if (!((code >= 48 && code <= 57) ||
			  (code >= 65 && code <= 90) ||
			  (code >= 97 && code <= 122) ||
			  (code == 32)))
		{ 
			if (!e) event.keyCode = 0;
			return false;
		}
	}
	else if (filterType == "ALPHANUMERIC") {
		if (!((code >= 48 && code <= 57) ||
			  (code >= 65 && code <= 90) ||
			  (code >= 97 && code <= 122) ||
			  (code == 32)))
		{ 
			if (!e) event.keyCode = 0;
			return false;
		}
		else if (code >= 97 && code <= 122) {
			// auto upper case letters
			if (e)
				e.which -= 32;
			else
				event.keyCode -=32;
		}
	}
	else if (filterType == "alpha") {
		if (!((code >= 65 && code <= 90) ||
			  (code >= 97 && code <= 122)||
			  (code == 32)))
		{ 
			if (!e) event.keyCode = 0;
			return false;
		}
	}
	else if (filterType == "integer") {
		if (!((code >= 48 && code <= 57) || code == 45)) {
			if (!e) event.keyCode = 0;
			return false;
		}
	}
	else if (filterType == "float") {
		if (!((code >= 48 && code <= 57) || code == 46 || code == 45)) {
			if (!e) event.keyCode = 0;
			return false;
		}
	}
}

function validationPassed(formName) {
	var msg = "";
	var msgRequired = "";
	var msgRange = "";
	var msgNaN = "";
	var elementCount;
	var formElements;
	var curElement;
	var setElementFocus = null;
	var element;
	var addMode = false;
	
	element = eval("document." + formName + "(\"hid.cmd\")");
	if (element != null && element.value == "add")
		addMode = true;
		
	elementCount = eval("document." + formName + ".elements.length");
	formElements = "document." + formName + ".elements[i]";
	
	for (var i = 0; i < elementCount; i++) {
		curElement = eval(formElements);
		if (curElement == null) continue;
		if (curElement.type == "hidden" || curElement.type == "button") continue;
		
		if (curElement.type == "text" || curElement.type == "password") {
			
			if (!addMode) {
				// if the value has not changed then do no further checks even if the current value is wrong.  This will prevent the
				// web app from forcing users to change values currently on the database when they may not have enough info to do so
				if (curElement._type == "float" || curElement._type == "integer") {
					if (curElement._curValue == curElement.value)
						// for when they are literal matched.  (allows for NULL checks)
						continue;
					else if ((curElement._curValue - 0) == (curElement.value - 0))
						// for when they are value matched
						continue;
						
				} else if (curElement._curValue == curElement.value) {
					// for when alpha types did not change
					continue;
				}
			}
			
			if (curElement._required == "1" && curElement.value == "") {
				msgRequired = msgRequired + "\n\t" + curElement._label;
				setFieldColor(curElement, "error");
				if (setElementFocus == null) setElementFocus = curElement;
				
			}  else if (curElement._type == "float" || curElement._type == "integer") {
			
				if (isNaN(curElement.value)) {
					msgNaN = msgNaN + "\n\t" + curElement._label;
					setFieldColor(curElement, "error");
					if (setElementFocus == null) setElementFocus = curElement;
					
				} else if (curElement._min > "" && curElement.value > "" &&
						 (curElement._min - 0) > (curElement.value - 0)) {
					msgRange = msgRange + "\n\t" + curElement._label;
					setFieldColor(curElement, "error");
					if (setElementFocus == null) setElementFocus = curElement;
				
				} else if (curElement._max > "" && curElement.value > "" &&
						 (curElement._max - 0) < (curElement.value - 0)) {
					msgRange = msgRange + "\n\t" + curElement._label;
					curElement.style.border = "thin #FF0000 solid";
					if (setElementFocus == null) setElementFocus = curElement;
				
				} else {
					// return border to normal
					setFieldColor(curElement, "");
				}
			} else {
				// return border to normal
				setFieldColor(curElement, "");
			}
		} else if (curElement.type == "dropdown") {
			if (!addMode) {
				// if the value has not changed then do no further checks even if the current value is wrong.  This will prevent the
				// web app from forcing users to change values currently on the database when they may not have enough info to do so
				if (curElement._curValue == curElement.value)
					continue;
			}
			
			if (curElement._required == "1" && curElement.value == "") {
				msgRequired = msgRequired + "\n\t" + curElement._label;
				setFieldColor(curElement, "error");
				if (setElementFocus == null) setElementFocus = curElement;
			} else {
				setFieldColor(curElement, "");
			}
		}
	}

	if (msgRequired > "") {
		msg = "The following required fields has no data:\n" + msgRequired;
	}

	if (msgNaN > "") {
		msgNaN = "The following fields contains invalid value:\n" + msgNaN;
		
		if (msg > "")
			msg = msg + "\n\n" + msgNaN;
		else
			msg = msgNaN;
	}
		
	if (msgRange > "") {
		msgRange = "The following fields contains out of range data:\n" + msgRange;
		
		if (msg > "")
			msg = msg + "\n\n" + msgRange;
		else
			msg = msgRange;
	}

	if (msg > "") alert(msg + "\n\nPlease correct and try again.");

	if (setElementFocus != null) setElementFocus.focus();
	
	// if has a message then return false to indicate validation failed
	return (msg == "");
}

function setFieldTooltips(formName) {
	var elementCount;
	var formElements;
	var curElement;
	
	elementCount = eval("document." + formName + ".elements.length");
	formElements = "document." + formName + ".elements[i]";
	
	for (var i = 0; i < elementCount; i++) {
		curElement = eval(formElements);
		if (curElement == null) continue;
		if (curElement.type == "hidden" || curElement.type == "button") continue;
		
		if (curElement.type == "text" || curElement.type == "password") {
			//set standard text field look and feel
			setFieldColor(curElement, "");
			setToolTip(curElement);
		}
	}
}

function setToolTip(fieldElement) {
	var tooltip ="";
	var minValue;
	var maxValue;
	
	if (!(fieldElement.type == "text" || fieldElement.type == "password"))
		return;
	
	if (fieldElement._required == "1") tooltip = "Required";
	
	if (fieldElement._min > "" || fieldElement._max > "") {
		if (tooltip > "") tooltip = tooltip + ": ";
		
		minValue = (fieldElement._min > "" ? fieldElement._min : "MIN");
		maxValue = (fieldElement._max > "" ? fieldElement._max : "MAX");
		
		tooltip = tooltip + minValue + " - " + maxValue;
	}
	
	fieldElement.title = tooltip;
}

function setFieldColor(curElement, state) {
	if (curElement.type == "text" || curElement.type == "password") {
		if (state == "error") {
			curElement.style.border = "1px #FF0000 solid";
			curElement.style.background = "white";
		}
		else if (state == "warning") {
			curElement.style.border = "1px solid #7F9DB9";
			curElement.style.background = "#FFFF00";
		}
		else {
			curElement.style.border = "1px solid #7F9DB9";
			curElement.style.background = "white";
		}
			
	} else if (curElement.type == "dropdown") {
		if (state == "error")
			curElement.style.background = "#FF7777";
		else if (state == "warning")
			curElement.style.background = "#FFFF00";
		else
			curElement.style.background = "white"
	}
}

function IsNumeric(sText)
{
	var ValidChars = "-0123456789.";
	var IsNumber = true;
	var Char;
 
	for (i = 0; i < sText.length && IsNumber == true; i++) 
	{ 
		Char = sText.charAt(i); 
		if (ValidChars.indexOf(Char) == -1) IsNumber = false;
	}
	
	return IsNumber;
}

// checks a numeric input field for errors
// returns 0 = OK, 1 = Warning, 2 = Error
function checkNumericField(element) {
	var errorCode = 0;
	
	var required = NVL(element._required, "");
	var minValue = NVL(element._min, "");
	var maxValue = NVL(element._max, "");
	var lowValue = NVL(element._low, "");
	var highValue = NVL(element._high, "");

	element._msg = "";
	
	if (element.value == "") {
		if (required - 0 != 0) {
			setFieldColor(element, "error");
			element._msg = "Error: " + NVL(element._label, element.name) + " is required";
			errorCode = 2;
			
		} else
			setFieldColor(element, "");
		
	} else if (!IsNumeric(element.value)) {
		element._msg = "Error: Value is not a valid number";
		setFieldColor(element, "error");
		errorCode = 2;
			
	} else if (minValue > "" && (minValue - 0 > element.value - 0)) {
		setFieldColor(element, "error");
		element._msg = "Error: Value cannot be less than min " + minValue;
		errorCode = 2;
		
	} else if (maxValue > "" && (maxValue - 0 < element.value - 0)) {
		setFieldColor(element, "error");
		element._msg = "Error: Value cannot be greater than max " + maxValue;
		errorCode = 2;
		
	} else if (lowValue > "" && (lowValue - 0 > element.value - 0)) {
		setFieldColor(element, "warning");
		element._msg = "Warning: Value is less than " + minValue + " low";
		errorCode = 1;
		
	} else if (highValue > "" && (highValue - 0 < element.value - 0)) {
		setFieldColor(element, "warning");
		element._msg = "Warning: Value is greater than " + highValue + " high";
		errorCode = 1;
		
	} else
		setFieldColor(element, "");
	
	return errorCode;
	
}

// Sets the proper appearance of toggle buttons using custom attribute "_state"
function setToggleStyle(toggle) {
	if (toggle == null) return false;
	
	if (toggle._state == null || toggle._state == "" || toggle._state == "0") {
		toggle.style.backgroundColor = "#A7A898";
		toggle.style.borderTop = "1px solid #FFFFF8";
		toggle.style.borderRight = "1px solid #82837D";
		toggle.style.borderBottom = "1px solid #82837D";
		toggle.style.borderLeft = "1px solid #FFFFF8";
		toggle.style.cursor = "hand";
		
	} else {
		toggle.style.backgroundColor = "#FEFFBF";
		toggle.style.borderTop = "1px solid #82837D";
		toggle.style.borderRight = "1px solid #FFFFF8";
		toggle.style.borderBottom = "1px solid #FFFFF8";
		toggle.style.borderLeft = "1px solid #82837D";
		toggle.style.cursor = "hand";
	}
}

//return IE4+or W3C DOM reference for an ID
function getObject(obj){
	var theObj
	if (document.all){
		if (typeof obj =="string"){
			return document.all(obj)
		}else {
			return obj.style
		}
	}
	if (document.getElementById){
		if (typeof obj =="string"){
			return document.getElementById(obj)
		}else {
			return obj.style
		}
	}
	return null
}

function btnMouseDown(button) {
	button.style.borderTopColor = "#6B6E72";
	button.style.borderRightColor = "#D7DBDF";
	button.style.borderBottomColor = "#D7DBDF";
	button.style.borderLeftColor = "#6B6E72";
}

function btnMouseUp(button) {
	button.style.borderTopColor = "#D7DBDF";
	button.style.borderRightColor = "#6B6E72";
	button.style.borderBottomColor = "#6B6E72";
	button.style.borderLeftColor = "#D7DBDF";
}

// disable the enter key on all broswers except IE, Firefox or Opera (i.e. Safari)
// this is a workaround to the Safari penchant for submitting the form no matter what
function formKeys(e) {
	var userAgent = navigator.userAgent.toLowerCase();
	if (userAgent.indexOf('safari') + 1 == 0) return true;
	
	if (e && e.which)
		code = e.which;	
	else
		code = event.keyCode;
			
	if (code == 13) {
		if (!e) event.keyCode = 0;
		return false;
	}
}

function setStyleClass(element, className) {
	element.setAttribute("class", className);
	element.setAttribute("className", className);
}

function initSelect(obj) {
	obj.options.length = 0;
}

function addSelect(obj, selItem) {
}

function URLencode(sStr) { 
	return escape(sStr). 
		replace(/\+/g, '%2B'). 
			replace(/\"/g,'%22'). 
				replace(/\'/g, '%27'). 
					replace(/\//g,'%2F'); 
}

function openDialog(dir, title, url, height) {
	var defaultData = "";
	
	if (window.showModalDialog) {
		changePending = "true"
		var returnValue = window.showModalDialog(
			dir + "/framesetDialog.jsp?title=" + escape(title) + "&url=" + escape(url), defaultData,
			"status:no;help:no;resizable:no;dialogHeight:" + height + ";dialogWidth:330px");
		
		changePending = "false"
	
		return (returnValue);
		
	} else {
		window.open(dir + "/framesetDialog.jsp?title=" + escape(title) + "&url=" + escape(url), title,
			"height=" + height + ",width=330,toolbar=no,directories=no,status=no, menubar=no, scrollbars=no, resizable=yes ,modal=yes");
	}
}
