//Gets and Object by ID
function getElement(idName)
{
	if ( document.all )
		return document.all[idName];
	else if ( document.getElementById )
		return document.getElementById(idName);
	else if ( document[idName] )
		return document[idName];
	else if ( document.layers )
		return document.layers[idName];
	else
		return document.idName;
}
//returns the code for the keypressed for all browsers
function getKeyCode(event)
{
	//show_props(event,"event");
	if(event.keyCode != 0)
		return event.keyCode;
	else if(event.charCode != 0)
		return event.charCode;
	else
		return event.which;
}

// returns the absolute position of an element's top pixel
function getAbsoluteTop(elm) 
{
	var mOffsetTop = elm.offsetTop;
	if(elm.offsetParent)
	{
		mOffsetTop += getAbsoluteTop(elm.offsetParent);
	}
	return mOffsetTop;
}

// returns the absolute position of an element's top pixel
function getAbsoluteLeft(elm) 
{
	var mOffsetLeft = elm.offsetLeft;
	if(elm.offsetParent)
	{
		mOffsetLeft += getAbsoluteLeft(elm.offsetParent);
	}
	return mOffsetLeft;
}

function clickButton(event, targetId)
{
	if(getKeyCode(event) == 13) // the "enter" key
	{
		getElement(targetId).click();
		return false;
	}
	else
	{
		return true;
	}
}

//Show a layer
function hide(idName) 
{
	var thisLevel = getElement(idName);
	thisLevel.style.display = "none";
}

function hideHidden(idName)
{
	var thisLevel = getElement(idName);
	thisLevel.style.visibility = "hidden";

}
function showVisibility(idName)
{
	var thisLevel = getElement(idName);
	thisLevel.style.visibility = "";
}

//Hides a layer
function show(idName) 
{
	var thisLevel = getElement(idName);
	thisLevel.style.display = "";
}

//Gets the object root ID
function getRoot(object)
{
	if (object.id != null)
		return object.id.substr(0,object.id.lastIndexOf("_"));
	else
		return object.substr(0,object.lastIndexOf("_"));
}	

// The idea for this function is that the "TextboxControl_Version2"
// in development could use this function by passing the string of 
// "specialChars" for validation, instead of creating an entirely custom
// javascript validation function for each instance of the control
// on a page.
// However, this function is not yet utilized.
// 6/5/06 JC
function KeystrokeIsSpecialCharacter(event, specialChars)
{
	var key = String.fromCharCode(getKeyCode(event));
	
	for(i =0; i < specialChars.Length; i++)
	{
		//alert(specialChars[i] + " == " + key + " ??");
		
		if(specialChars[i] == key)
			return true;
	}
	
	return false;
}

//Ensures on alpha values are entered
function KeystrokeIsAlpha(event)
{
	//alert("KeystrokeIsAlpha - keyCode: " + event.keyCode + " keyChar: " + event.charCode + " which: " + event.which);			
	
	var blnReturn = false;
	var intKey = getKeyCode(event);
	if ((intKey > 64 && intKey < 91) || (intKey > 96 && intKey < 123) || intKey == 32)
		return true;
	else if (KeystrokeIsCommonKey(event))
		return true;
	else 	
		return false;
}

//Ensures on numeric values are entered
function KeystrokeIsNumeric(event)
{
	//alert("KeystrokeIsNumeric - keyCode: " + event.keyCode + " keyChar: " + event.charCode + " which: " + event.which);
	
	
	var blnReturn = false;
	var intKey = getKeyCode(event);
	if (intKey > 47 && intKey < 58)
		return true;
	else if (KeystrokeIsCommonKey(event))
		return true;
	else
		return false;
	
}

function KeystrokeIsAlphaSpecialChar(event)
{
	var blnReturn = false;
	var intKey = getKeyCode(event);
		switch (intKey)
		{
			case 45: //-
			case 46: //.
			case 44: //,
			case 58: //:
			case 35: //#
			case 39: //'
			case 64: //@
			case 95: //_
				return true;
		}
		
	return false;
}

function KeystrokeIsNumericSpecialChar(event)
{
	//alert("KeystrokeIsNumericSpecialChar - keyCode: " + event.keyCode + " keyChar: " + event.charCode + " which: " + event.which);
	
	var blnReturn = false;
	var intKey = getKeyCode(event);
	switch (intKey)
	{
		case 45: //-
		case 46: //.
			blnReturn = true;		
	}	
	return blnReturn;
}
//Key is null, backspace, delete or an arrow key
function KeystrokeIsCommonKey(event)
{
	// function modified 5/22/06 JC
	// Re-wrote if structure into case structure and added keyCodes for arrows.
	// added many more comparisons for purpose of firefox compatability.
	// When keyCode is 33-46, IE doesn't handle pressing those keys as a keyPressed event,
	// but firefox does. So those keys did not work in firefox without adding extra
	// key codes to the case list here.
	
	//alert("KeystrokeIsCommonKey - keyCode: " + event.keyCode + " keyChar: " + event.charCode + " which: " + event.which);
		
	if (getKeyCode(event) == null || getKeyCode(event) == 0)
		return true;
	
	var intKey = getKeyCode(event);	
	
	switch (intKey)
	{	
		case 8:  // backspace
		case 9:	 // delete/tab (NS)
			return true;
		default:
			break;
	}
	
	// Check for function key use in Firefox and Netscape
	if(IsNetscape && event.charCode == 0) //same as IsFirefox. Firefox displays app name as "Netscape"
	{
		// IE only uses event.KeyCode. Firefox & Netscape use event.KeyCode for keys that aren't chars
		// and event.charCode for keys that are chars. getKeyCode() from this Common.js gets the charCode
		// in the case that keyCode == 0. However, there are many charCodes in Firefox that represent
		// function keys (arrows, delete, etc) that are the same number as the keyCode in IE but represent
		// characters (mostly punctuation), not functions keys.
		
		//	Example:
		//	KEY		IE keyCode	FF keyCode	FF charCode
		//	!		33			0			33
		// 	PgUp	noKeyPress	33			00		
	
		switch(intKey)
		{	
			case 33: // Page Up
			case 34: // Page Down
			case 35: // END
			case 36: // HOME
			case 37: // Left Arrow 
			case 38: // Up Arrow
			case 39: // Right Arrow
			case 40: // Down Arrow
			case 45: // Insert
			case 46: // Delete			
				return true;
			default:
				break;
		}
	}	
	
	//alert("Common: false");	
	//alert("KeystrokeIsNumeric - keyCode: " + event.keyCode + " keyChar: " + event.charCode + " which: " + event.which);
	return false;
}

function IsNetscape()
{	
	var browserName=navigator.appName;
	return browserName == "Netscape";
}

function KeystrokeIsValid(event, allowAlpha, allowNumeric, allowAlphaSpecialChars, allowNumericSpecialChars)
{
	//alert("KewystrokeisValide: " + getKeyCode(event));
	
	if (allowAlpha && KeystrokeIsAlpha(event)) 
		return true;
	if (allowNumeric && KeystrokeIsNumeric(event)) 
		return true;
	if (allowAlphaSpecialChars && KeystrokeIsAlphaSpecialChar(event))
		return true;
	if (allowNumericSpecialChars && KeystrokeIsNumericSpecialChar(event))
		return true;
	if (KeystrokeIsCommonKey(event))
		return true;
					 
	//alert("IsValid: false");
	return false;
}
//alert(navigator.appName);

//Moves cursor to nextField when maxLength is hit
function tabToNextBox(event, object, maxLength, nextField){
	var intKey = getKeyCode(event);
	var intValueLength = object.value.length;
	var frmCurrent = object.form;
	//allow shift-tab
	if (intValueLength >= maxLength && intKey != 9 && intKey != 16){
		frmCurrent.elements[nextField].focus();
	}
}

//Shows the properties of an object (DEBUG)
function show_props(obj, obj_name) {
	var result = "";
	for (var i in obj) result += obj_name + "." + i + " = " + obj[i] + "<br />\n"
	newWindow =	window.open()
	newWindow.document.write("Here is the object "+obj_name+":<br/><br/>");
	newWindow.document.write(result)
}

function isDigit(event)
{
	var intKeyCode = getKeyCode(event);

	if (intKeyCode > 47 && intKeyCode < 58) return;
	if (intKeyCode == 8 || intKeyCode == 9 || intKeyCode == 28 || intKeyCode == 29 || intKeyCode == 127) return;

	event.returnValue = false;
	return false;
}

// ########## Begin Phone Control Scripts ########## //
// also uses isDigit(objEvent)


//checks if the US phone is valid
function validUSPhone(source, arguments)
{
	var reg3digits = /\d{3}/;
	var reg4digits = /\d{4}/;
	var areaCode = getElement(getRoot(source) + '_txtAreaCode');
	var prefix = getElement(getRoot(source) + '_txtPrefix');
	var suffix = getElement(getRoot(source) + '_txtSuffix');
	var extension = getElement(getRoot(source) + '_txtExtension');
	if(areaCode.value.replace(/\s/g, "") == "" && prefix.value.replace(/\s/g, "") == "" && suffix.value.replace(/\s/g, "") == "")
	{
		arguments.IsValid = true;
	}
	else
	{
		if(reg3digits.test(areaCode.value.replace(/\s/g, "")) && reg3digits.test(prefix.value.replace(/\s/g, "")) && reg4digits.test(suffix.value.replace(/\s/g, "")))
		{
			arguments.IsValid = true;
		}
		else
		{
			arguments.IsValid = false;
		}
	}
}

//restricts characters allowed in the international phone to [digits - . ( )] 
function isValidCharInternational(event)
{
	var intKeyCode = getKeyCode(event);

	if (intKeyCode > 47 && intKeyCode < 58) return;
	if (intKeyCode == 8 || intKeyCode == 9 || intKeyCode == 28 || intKeyCode == 29 || intKeyCode == 127 || intKeyCode == 46 || intKeyCode == 45 || intKeyCode == 40 || intKeyCode == 41) return;
	event.returnValue = false;
	return false;
}

	
// ########## End Phone Control Scripts ########## //

// ########## Begin UBI Control Scripts ########## //
function validUBI(source, arguments)
{
	var reg3digits = /\d{3}/;
	var regValidUBI = /^(\d{3}[- ]?\d{3}[- ]?\d{3})$/;
	var UBICombined = getElement(getRoot(source) + '_txtUBICombined');
	if(UBICombined != null)
	{
		if(UBICombined.value.replace(/\s/g, "") == "")
		{
			arguments.IsValid = true;
		}
		else
		{
			if(regValidUBI.test(UBICombined.value.replace(/\s/g, "")))
			{
				arguments.IsValid = true;
			}
			else
			{
				arguments.IsValid = false;
			}
		}
	}
	else
	{
		var UBI1 = getElement(getRoot(source) + '_txtUBI1');
		var UBI2 = getElement(getRoot(source) + '_txtUBI2');
		var UBI3 = getElement(getRoot(source) + '_txtUBI3');
		if(UBI1.value.replace(/\s/g, "") == "" && UBI2.value.replace(/\s/g, "") == "" && UBI3.value.replace(/\s/g, "") == "")
		{
			arguments.IsValid = true;
		}
		else
		{
			if(reg3digits.test(UBI1.value.replace(/\s/g, "")) && reg3digits.test(UBI2.value.replace(/\s/g, "")) && reg3digits.test(UBI3.value.replace(/\s/g, "")))
			{
				arguments.IsValid = true;
			}
			else
			{
				arguments.IsValid = false;
			}
		}
	}
}
// ########## End UBI Control Scripts ########## //

// ########## Begin Zip Control Scripts ########## //
function validZip(source, arguments)
{
//alert("Validating Zip Code");
	var reg5digits = /\d{5}/;
	var reg4digits = /\d{4}/;
	var zip1 = getElement(getRoot(source) + '_txtZip1').value.replace(/\s/g, "");
	var zip2 = getElement(getRoot(source) + '_txtZip2').value.replace(/\s/g, "");
	if(zip1 == "" && zip2 == "")
		arguments.IsValid = true;
	else if (reg5digits.test(zip1))
	{
		if (zip2 == "")
			arguments.IsValid = true;
		else if(zip2.length==4)
			return reg4digits.test(zip2);
		else
		    arguments.IsValid = false;
	}
	else	
		arguments.IsValid = false;	
}
// ########## End UBI Control Scripts ########## //

// ########## Begin Accounting Format Function ########## //

function formatAccounting(strNumber)
{
	if (strNumber != "") {
		strNumber = parseFloat(strNumber).toFixed(2);
		var strFmtValue = "";
		var intCnt = 0;
		var intDecLoc = 0;
		var intNeg = strNumber.indexOf("-",0);
		
		if ( intNeg != -1 )
			strNumber = strNumber.substring(1,strNumber.length);
		
		intDecLoc = strNumber.indexOf(".",0);
		if ( intDecLoc < 0 )
			intDecLoc = strNumber.length;
		
		for ( intCnt = intDecLoc; intCnt > 2; intCnt -= 3 )
		{
			strFmtValue = strNumber.substring(intCnt - 3,intCnt) + strFmtValue;
			if ( intCnt > 3 )
				strFmtValue = "," + strFmtValue;
		}
		
		if ( intCnt > 0 )
			strFmtValue = strNumber.substring(0,intCnt) + strFmtValue;
		
		if ( intDecLoc < strNumber.length )
		{
			while ( strNumber.length < (intDecLoc + 3) )
				strNumber = strNumber + "0";
			
			strFmtValue = strFmtValue + strNumber.substring(intDecLoc,intDecLoc + 3);
			if ( intDecLoc == 0 )
				strFmtValue = "0" + strFmtValue;
		}
		else
			strFmtValue = strFmtValue + ".00";
		
		if ( intNeg != -1 )
			strFmtValue = "(" + strFmtValue + ")";
	
	return strFmtValue;
	} 
	else 
		return "0.00";
}

function removeAccountingFormat(strNumber)
{
	if (strNumber == "0.00" || strNumber == "(0.00)")
		return "";
	if (strNumber != ""){
		var intNeg = strNumber.indexOf("(",0);
		
		if ( intNeg != -1 )
			strNumber = strNumber.substring(1,strNumber.length);
		
		strNumber = strNumber.replace("(","").replace(")","").replace(",","");
		if (intNeg != -1)
			strNumber = "-" + strNumber;
	} 
	return strNumber;
}

// ########## End Accounting Format Function ########## //
