/*
Populate the aryFields Array with sub-Arrays.  Each sub array needs 3 elements:
	1. Name of the Field to validate.
	2. Name of validating function **
	3. Message to show on validation error.
You can either call a custom validator OR call one of the following generic validator functions:
	    NAME                DESCRIPTION
    	----                -----------
	1.  IsChecked       	Checks that user has checked at least 1
	  	                    	RADIO or CHECKBOX item from that individual element
	                        	or array of elements
	2.  IsCompleted         Checks that the user has not left a text field blank
	3.  IsEmail     	    Checks that the user has entered a valid email address
	4.  IsNumeric           Checks that the user has entered a number 
	5.  IsPhone				Checks that the user has entered a valid phone number
	6.  IsCompletePhone		Checks that the user has entered a complete phone number(555-555-5555)
	7.  IsPhoneAreaCode		Checks that the user has entered a valid phone number
	8.  IsPhonePrefix		Checks that the user has entered a valid phone number
	9.  IsPhoneNumber		Checks that the user has entered a valid phone number
	10. IsSelected    		Checks that the user has picked an item from a SELECT list
	11. IsSSN  				Checks that the user has entered a valid social security number
	12. IsZipCode    		Checks that the user has entered a valid zip code
	13. MinLength			Checks that the user has entered a string with a minimum length of MinLength
	14. IsURL				Checks that the user has entered a valid url
	15. IsDate				Checks that the user has entered a valid date
	16. IsRoutingNumber		Checks that the user has entered a valid Bank Routing Number
	17. IsAccountNumber		Checks that the user has entered a valid Bank Account Number	
*/
	
// Added DOM detection for Opera which doesn't handle the length property of
//an array of form elements.
isDOM = document.getElementById ? 1:0;
var aryFields;
var aryFieldLengths;
var intFieldLength;
//***************************************************************************************
//Validate Function
//call this function from the ONSUBMIT event of the form you want to validate
//***************************************************************************************
function ValidateForm(f){ 
    for(var i=0; i<aryFields.length; ++i){ 
		//get the field length, used when checking MinLength
		if(aryFieldLengths){
			intFieldLength = aryFieldLengths[i];
			alert(intFieldLength);
		}
		if (!(aryFields[i][1](f,aryFields[i][0],aryFields[i][2]))){
	        return false;
	    }
    }
	return true;
} 
//***************************************************************************************
//IsChecked Function
//***************************************************************************************
function IsChecked(f,fld,msg){
    var e;
    var j = 0; // This is so that we know which RADIO/CHECKBOX element to return to.
    
    if(isDOM){
    	// Added the DOM part for Opera which seems to choke on the length
    	// property of an array of elements.    
        e = document.getElementsByTagName('INPUT'); // returns a Node List
        for(var i=0; i<=(e.length - 1); ++i){
        	if(e[i].name == fld){
                if(j==0) j=i;
                if(e[i].checked) return true; // one is checked after all!
            }
        }
	}
	else{
    	e = f.elements[fld]; // returns an array of elements
        for(var i=0; i < e.length; ++i){ 
	    	if(e[i].checked){ 
	    	    return true; // one is checked after all! 
	    	    break; 
	    	} 
	    }
    }
    Private_ShowError(e,msg);
	return false;
}
//***************************************************************************************
//IsCompleted Function
//***************************************************************************************
function IsCompleted(f,fld,msg){
	var e = f.elements[fld];
    
	if((e.type == 'text') || (e.type == 'password')){
		if(!(Private_IsEmpty(Private_Strip(' ', e.value)))) return true; 
	} else if(e.type == 'textarea'){
		if(!(Private_IsEmpty(Private_Strip(' ', e.innerText)))) return true; 
	}
    Private_ShowError(e,msg);
	return false;
}
//***************************************************************************************
//IsEmail Function
//***************************************************************************************
function IsEmail(f,fld,msg){
	var e = f.elements[fld];
	var strTest = e.value;
	var strUserName, strHost, strExtension;
	var intPosition;
	var blnReturn = false;
	
	if(strTest.length != 0){
		intPosition = strTest.indexOf('@')
		if(intPosition > 0){ //'@' is found at the second or upper position
			strUserName = strTest.slice(0,intPosition);
			if(intPosition+1 < strTest.length){
				strHost = strTest.slice(intPosition+1);
				intPosition = strHost.indexOf('.');
				if(intPosition > 0){ //'.' is found at the second or upper position
					if(intPosition+1 < strHost.length){
						return true;
					}
				}
			}
		}							
	}
	Private_ShowError(e,msg);
	return false;
}
//***************************************************************************************
//IsURL Function
//***************************************************************************************
function IsURL(f,fld,msg){
	var e = f.elements[fld];
	var strTest = e.value;
	var FORMAT = /^(ftp|http|https):\/\/\S+$/i
	// get number of occurrences of '.'
	var intDotCount = Private_GetOccurrences(strTest,'.');
	if(intDotCount == 2){
		// test strURL against the FORMAT
		if(FORMAT.test(strTest)){
			return true;
		}
	}
	Private_ShowError(e,msg);
	return false;
}
//***************************************************************************************
//IsNumeric Function
//***************************************************************************************
function IsNumeric(f,fld,msg){
	var e = f.elements[fld];
    
    if(Private_IsNumeric(e.value)) return true; 
    Private_ShowError(e,msg);
	return false;
}
//***************************************************************************************
//IsDate Function
//***************************************************************************************
function IsDate(f,fld,msg){
	var e = f.elements[fld];
    
	var dtmTemp = new Date(e.value);
	if(!(isNaN(dtmTemp))) return true;
    Private_ShowError(e,msg);
	return false;
}
//***************************************************************************************
//IsOneChecked Function
//***************************************************************************************
function IsOneChecked(f,fld,msg){
	var e = '';
	var aryFields = fld.split(',');
	
	for(var i=0; i<aryFields.length; i++){
		e = f.elements[aryFields[i]];
		if(e.checked == true) return true;
	}
	Private_ShowError(e,msg);
	return false;
}
//***************************************************************************************
//IsSelected Function
//***************************************************************************************
function IsSelected(f,fld,msg){
	var e = f.elements[fld];
    
	if(!(e.value == '') || !(e.value == 0)) return true;
    Private_ShowError(e,msg);
	return false;
}
//***************************************************************************************
//IsPhone Function
//***************************************************************************************
function IsPhone(f,fld,msg){
	var e = f.elements[fld];
	var strValue = '';
	var strFilter = '*() -./_\n\r\t\\';
	
	strValue = Private_Strip(strFilter, e.value);
	if(Private_IsNumeric(strValue) && ((strValue.length == 7) || (strValue.length == 10))) return true;
	Private_ShowError(e,msg);
	return false;
}
//***************************************************************************************
//IsCompletePhone Function
//***************************************************************************************
function IsCompletePhone(f,fld,msg){
	var e = f.elements[fld];
	var strValue = '';
	var strFilter = '*() -./_\n\r\t\\';
	
	strValue = Private_Strip(strFilter, e.value);
	if(Private_IsNumeric(strValue) && strValue.length == 10) return true;
	Private_ShowError(e,msg);
	return false;
}
//***************************************************************************************
//IsPhoneAreaCode Function
//***************************************************************************************
function IsPhoneAreaCode(f,fld,msg){
	var e = f.elements[fld];
	var strValue = '';
	var strFilter = '*() -./_\n\r\t\\';
	
	strValue = Private_Strip(strFilter, e.value);
	if(Private_IsNumeric(strValue) && (strValue.length == 3)) return true;
	Private_ShowError(e,msg);
	return false;
}
//***************************************************************************************
//IsPhonePrefix Function
//***************************************************************************************
function IsPhonePrefix(f,fld,msg){
	var e = f.elements[fld];
	var strValue = '';
	var strFilter = '*() -./_\n\r\t\\';
	
	strValue = Private_Strip(strFilter, e.value);
	if(Private_IsNumeric(strValue) && (strValue.length == 3)) return true;
	Private_ShowError(e,msg);
	return false;
}
//***************************************************************************************
//IsPhoneNumber Function
//***************************************************************************************
function IsPhoneNumber(f,fld,msg){
	var e = f.elements[fld];
	var strValue = '';
	var strFilter = '*() -./_\n\r\t\\';
	
	strValue = Private_Strip(strFilter, e.value);
	if(Private_IsNumeric(strValue) && (strValue.length == 4)) return true;
	Private_ShowError(e,msg);
	return false;
}
//***************************************************************************************
//IsSSN Function
//***************************************************************************************
function IsSSN(f,fld,msg){
	var e = f.elements[fld];
	var strFilter = ' -.\n\r\t';
	var strValue = '';
	
	strValue = Private_Strip(strFilter, e.value);
   	if(Private_IsNumeric(strValue) && strValue.length == 9) return true;
	Private_ShowError(e,msg);
	return false;
}
//***************************************************************************************
//IsZipCode Function
//***************************************************************************************
function IsZipCode(f,fld,msg){
	var e = f.elements[fld];
	var strFilter = ' -.\n\r\t';
	var strValue = '';
	
	strValue = Private_Strip(strFilter, e.value);
	if(Private_IsNumeric(strValue) && ((strValue.length == 5) || (strValue.length == 9))) return true;
	Private_ShowError(e,msg);
	return false;
}
//***************************************************************************************
//MinLength Function
//***************************************************************************************
function MinLength(f,fld,msg){
	var e = f.elements[fld];
	var strFilter = ' -.\n\r\t';
	var strValue = '';
	
	strValue = Private_Strip(strFilter, e.value);
	if(strValue.length >= intFieldLength) return true;
	Private_ShowError(e,msg);
	return false;
}
//***************************************************************************************
//IsRoutingNumber Function
//***************************************************************************************
function IsRoutingNumber(f,fld,msg){
	var e = f.elements[fld];
	var strValue = e.value;
	
	if(Private_IsNumeric(strValue) && Private_IsRoutingNumber(strValue) && strValue.length == 9) return true;
	Private_ShowError(e,msg);
	return false;
}
//***************************************************************************************
//IsAccountNumber Function
//***************************************************************************************
function IsAccountNumber(f,fld,msg){
	var e = f.elements[fld];
	var strValue = e.value;
	
  	if(Private_IsAccountNumber(strValue) && strValue.length > 0 && strValue.length < 18) return true;
	Private_ShowError(e,msg);
	return false;
}
//***************************************************************************************
//Private Functions
//***************************************************************************************
function Private_IsEmpty(pstrTest){
	if(!(pstrTest.length > 0)) return true;
	return false;
}
function Private_IsNumeric(val){
	return(parseFloat(val,10)==(val*1));
}
function Private_ShowError(e,msg){
	alert(msg);
	if((e.type == 'text') || (e.type == 'password')){
		e.focus();
		e.select();
	}
}
function Private_Strip(pstrFilter, pstrTest){
	var i = 0;
	var strChar = '';
    var strReturn = '';
	var intLength = pstrTest.length;
    			
	for(i = 0; i < intLength; i++){
		strChar = pstrTest.charAt(i);
   		if(pstrFilter.indexOf(strChar) < 0){ 
   			strReturn += strChar;
		}
	}
	return strReturn;
}
function Private_GetOccurrences(pstrTest, pstrChar){
	var intCount = 0;
	var intLength = pstrTest.length;
	for(var intPosition = 0; intPosition <= intLength; intPosition++){
		if(pstrTest.substring(intPosition, intPosition + 1) == pstrChar){
			intCount++;
		}
	}
	return intCount;
}
function Private_IsRoutingNumber(pstrTest){
	// CheckSum Routine
	var intSum = 0;
	for (var i = 0; i < pstrTest.length; i += 3) {
		intSum += (parseInt(pstrTest.charAt(i), 10) * 3)
	      	+ (parseInt(pstrTest.charAt(i + 1), 10) * 7)
	      	+ (parseInt(pstrTest.charAt(i + 2), 10));
	}
	if((intSum != 0) && (intSum % 10 == 0)){
		return true;
	} else {
		return false;	
	}
}
function Private_IsAccountNumber(pstrTest){
	var aryValid = new Array(' ','0','1','2','3','4','5','6','7','8','9');
	var blnMatch;
	for(var i = 0; i < pstrTest.length; i++){
		blnMatch = false;
		for(var j = 0; j < aryValid.length; j++){
			if(pstrTest.substr(i,1) == aryValid[j]){
				blnMatch = true;
			}
		}
		if(!(blnMatch)){
			return false;
		}
	}
	return true;
}



