
// Form Validation
//browser detection
var strUserAgent = navigator.userAgent.toLowerCase(); 
var isIE = strUserAgent.indexOf("msie") > -1; 
var isNS6 = strUserAgent.indexOf("netscape6") > -1; 
var isNS4 = !isIE && !isNS6  && parseFloat(navigator.appVersion) < 5; 

function GetMyObjectByID(itemID)
{
// In the future will need to build in the ability to handle v4 netscape browsers.
	if (document.getElementById) // Good browsers
		return document.getElementById(itemID)
	else if (document.all) // medium browsers
		return document.all[itemID];
		
	// Need to handle v4 browsers in the future.
}
function CheckField(itemID)
{
	var o = GetMyObjectByID(itemID);
	if(o.value.length<1)
		return false;
	else
		return true;
}

function CheckFieldLength(itemID, pType, pLen)
{
	var o = GetMyObjectByID(itemID);

	if(pType == ">")
	{
		return (o.value.length >= pLen)
	}
	else
	{
		return (o.value.length <= pLen)
	}	
}

function CheckDD(itemID)
{
	var o = GetMyObjectByID(itemID);
	if(o.selectedIndex < 0)
		return false;
		
	var select = o.options[o.selectedIndex].value;
	
	if(select.length<1 || select == "0")
		return false;
	else
		return true;
}
function CheckCheckBox(itemID)
{
	var o = GetMyObjectByID(itemID);
	if(!o.checked)
		return false;
	else
		return true;
}
function CheckRadio(itemID)
{	
	//var o = GetMyObjectByID(itemID);
	var o = document.forms[0][itemID];
	
	var cnt = -1;
	for (var i=0; i < o.length; i++) {
		if (o[i].checked) {
	   	cnt = i; 
		i = o.length;
	   }
	}
	if (cnt > -1) 
		return true;
	else 
		return false;
}

function CheckEmail(itemID) 
{
	var o = GetMyObjectByID(itemID);	
	if (o != null)
	{	
		// Make sure we let an email go through that is empty... The required field catcher will get that
		if (o.value.length < 1)
		{
			return false;
		}
		var regex = /^[a-zA-Z0-9._-]+@([a-zA-Z0-9.-]+\.)+[a-zA-Z0-9.-]{2,4}$/;		
		if (!regex.test(o.value))
		{
			return false;
		}
		else
		{
			return true;
		}
	}
}
function isSpecialKey(strValue){
// Returns true if one of these special key codes is pressed
//backspace Ctrl + C,enter, Ctrl + X, Ctrl + V, Ctrl + Z
var reKeyboardChars = /[\x00\x03\x08\x0D\x16\x18\x1A]/;
	return reKeyboardChars.test(strValue);
}

/*
To filter the max length here is some example code 
to catch both key down and to catch paste events
onkeypress="return FilterMaxLength(event,this,250)" onpaste="return CheckPaste(this,250)"
*/
function FilterMaxLength(e,o,max){
// we must allow spaces , backspace etc to go through 
	var iKeyCode, strKey;  
	
	if (isIE) {
		iKeyCode = e.keyCode;
	} else {
		iKeyCode = e.which;
	}	
	strKey = String.fromCharCode(iKeyCode);	
	// Make sure we let special keys go through.
		if (isSpecialKey(strKey))
			return true;	
		var maxLength = max;
     	if(o.value.length > maxLength-1){
	 		alert('You are only able to make a maximum payment of $99,999. Please reduce the amount and try again');
        	return false;
     	}
    	return true
	}
function CheckPaste(o,max){
	var maxLength = max;
	// Get from clipboard
	var data = window.clipboardData.getData("Text");

	var CurrentLength = o.value.length + data.length;
    if (CurrentLength > maxLength)
    {
		alert('You are only able to make a maximum payment of $99,999. Please reduce the amount and try again');
		return false;
	}
	else
		return true;
}
//mask input to allow integer only
function CheckNumeric(objEvent) {
	var iKeyCode, strKey;  
	var reValidChars = /\d/;	
	var reKeyboardChars =       /[\x00\x03\x08\x0D\x16\x18\x1A]/;
	if (isIE) {
		iKeyCode = objEvent.keyCode;
	} else {
		iKeyCode = objEvent.which;
	}
	
	strKey = String.fromCharCode(iKeyCode);
	
	if (!reValidChars.test(strKey) && !reKeyboardChars.test(strKey)) {
		return false;
	}
}

function CheckComparison(item1ID, item2ID)
{

	var o = GetMyObjectByID(item1ID);
	var o2 = GetMyObjectByID(item2ID);

	if(o.value.length==0)
		return false;
		
	if (o.value != o2.value)
	{
		return false;
	}
	else
	{
		return true;
	}
}
