// $Id: utilities.js,v 1.12 2006/12/07 19:20:09 jvitelli Exp $  

/*
this is a generic function used to validate any form.
requires three parameters
	1.  pass in form object
	2.  string of field names that are required example:  'field1, field2, field3'
	2.  string of fields that require email email values: 'email,'emailconfirm'
*/

//global variable.  Use to check if a form has already been sent
g_bolFormSent = false
var g_strLastObjName = ""

var oElement = null;
var oInput = null;
var bRequest = false;
var aryResultStore = new Array();
var bRequest = false;  //set to true during a request.
var nAction = null;
var nFunctionID = 0;


function frmValidate(p_strFormName, p_strFields, p_strEmails){
	
	var l_aryFields
	var l_aryEmails
	var l_intFieldLen
	var l_intEmailLen
	var l_objForm
	
	var l_bolFields
	var l_bolEmail
	var l_bolError
	
	l_bolError = false		//set this to true once an error is found.
	
	if(p_strFields == 0){
		//no fields to validate
		l_bolFields = false
	} else {
		l_bolFields = true
	}		
		
	if(p_strEmails == 0){
		//no emails to validate
		l_bolEmail = false
		l_bolValidEmail = true
	} else {
		l_bolEmail = true
	}
	
	var l_objForm = document.forms[p_strFormName]
	
	if(l_bolFields){

		//create arrays for each string
		l_aryFields = p_strFields.split(",")	
		l_intFieldLen = l_aryFields.length 
		//loop through the supplied array of required field names
		for(x=0; x < l_intFieldLen; x++){				
			
			//loop through each element in the form
			for (y=0; y< l_objForm.elements.length; y++){
				
				//check if the name in the form matches the name in the array
				if(l_objForm.elements[y].name == l_aryFields[x]){
					
					//alert(l_objForm.elements[y].name + ' is required')
					//create an object of the input then get its type
					l_objInput = l_objForm.elements[y]
					l_strType = l_objInput.type
										
					//based on the type... test if the object has data					
					switch(l_strType){
						
						//TextArea
						case "textarea":
							if(l_objInput.value == ""){
								//alert('Please fill in all required fields')
								highlight(l_objInput,1)
								l_bolError = true
							} else {
								highlight(l_objInput,0)
							}
						break
			
					
						//select box					
						case "select-one":
							l_intSelected = l_objInput.selectedIndex
							
							if(l_intSelected < 1){
								//alert('Please select from the drop down')
								highlight(l_objInput,1)
								l_bolError = true
							} else {
								highlight(l_objInput,0)
							}
						break
								
						case "text":
						case "password":
							if(l_objInput.value == ""){
								//alert('Please fill in all required fields')
								highlight(l_objInput,1)
								l_bolError = true
							} else {
								highlight(l_objInput,0)
							}
						break
						
						case "radio":
						case "checkbox":
						
							//get the length of the radio object and test each one.
							l_objInp = l_objForm.elements[l_aryFields[x]]
							l_intLen = l_objInp.length
							l_bolChecked = false
							
							l_strObjName = l_aryFields[x]
							//check to see if this obj belongs to the same group name
							
							if(l_strObjName == g_strLastObjName){
									//do nothing
							} else {
								
								for(r = 0; r < l_intLen; r++){
									
									if(l_objInp[r].checked == true){
										l_bolChecked = true
									}
								}
							
								if(!l_bolChecked){
									//alert('Please choose one')
									for(r = 0; r < l_intLen; r++){									
										//highlight each radio button in the group.  take the first radio object and add 1 each loop
										l_intCount = y + r //first time through y is at the first radio element..
										highlight(l_objForm.elements[l_intCount], 1)
									}								
									l_bolError = true
								} else {
									//make sure they are white.
									for(r = 0; r < l_intLen; r++){									
										//highlight each radio button in the group.  take the first radio object and add 1 each loop
										l_intCount = y + r //first time through y is at the first radio element..
										highlight(l_objForm.elements[l_intCount], 0)
									}
								}
							
								g_strLastObjName = l_strObjName 
							}

						break						
					}
				}
			}
		}
	}
	
	//now validate all emails, loop through the array
	if(l_bolEmail == true){
		
		l_aryEmails = p_strEmails.split(",")
		l_intEmailLen = l_aryEmails.length 
		
		for(x=0; x < l_intEmailLen; x++){		
						
			l_objEmail = l_objForm.elements[l_aryEmails[x]]
			var l_bolValidEmail = emailCheck(l_objEmail.value)   //returns false if not a valid email.
			if(!l_bolValidEmail){
				//alert('Please enter a valid email address');
				highlight(l_objEmail,1)				
			} else {
				highlight(l_objEmail,0)
			}
		}
	}
	
	//check for errors
	if(l_bolError || !l_bolValidEmail){
		if(!l_bolValidEmail  && !l_bolError){
			alert('Please supply valid email information')
			l_objEmail.focus()
			return false
		} 
		if(!l_bolValidEmail  && l_bolError){
			alert('Please supply valid email information and complete all required fields')
			return false
		} 
		if(l_bolValidEmail  && l_bolError){
			alert('Please complete all required fields')
			return false
		} 
		
	}
	//if here, ready to send form.  Make sure it is only sent once.
	if(!g_bolFormSent){
		g_bolFormSent = true
		return true
		//l_objForm.submit()
		
	} else {		
		alert('Data has already been submit')
	}
	
}

//email validation
function emailCheck(p_strEmail){
	var emailReg = "[-A-Za-z0-9_+.]+@([-A-Za-z0-9]([-_A-Za-z0-9]*[A-Za-z0-9])?\\.)+[A-Za-z]{2,}\\.?";
	var regex = new RegExp(emailReg);
	return regex.test(p_strEmail);
}

//call this function to highlight an input object
function highlight(p_objInput, p_state){
	if(p_state == 1){
		p_objInput.style.backgroundColor= "#9cf"
	} else {
		p_objInput.style.backgroundColor= "#fff"
	}
}

//call this to clear the form
function startOver(p_strFormName){

	var l_objForm	
	var l_objForm = document.forms[p_strFormName]
	
	if(confirm('Empty all form fields?')){
		
		for (y=0; y< l_objForm.elements.length; y++){
			//set the color back to white			
			highlight(l_objForm.elements[y],0)
			//clear each field
			l_objForm.elements[y].value = ""
		}
	
	}
	
	return false
}
 

function isValidCurrency(p_intTip){

	var l_rxCurrency = /^\d*(\.?\d{0,2})?$/;
	if (l_rxCurrency.test(p_intTip)){			
		return true				
	} else {
		return false
	}
}

function isAlphaNumeric(p_strValue,pAllowSpace){

	// pAllowSpace = true/false
	var rxAlphaNum 
	
	if(pAllowSpace){
		rxAlphaNum = /^\w*[a-zA-Z0-9 ]*$/
	} else {
		rxAlphaNum = /^\w*$/
	}
	if (rxAlphaNum.test(p_strValue)){			
		return true				
	} else {
		return false
	}
}

function isNumeric(p_strValue){

	var l_rxNumeric = /^\d*$/;
	if (l_rxNumeric.test(p_strValue)){			
		return true				
	} else {
		return false
	}
}

//call this to make sure the credit card type matches the number
//and that the number is the proper length based on card type.
//use the array values
function CheckCardDetails(p_oForm,p_inpPayMethod,p_intCardNumber){
		
	//set the default card array.
	var g_aryCard = new Array(4);
	
	// paytype values
	//visa = 15; mastercard=16; Discover=17; Amex=18
	g_aryCard[0] = new Array(15, "4", 16);
	g_aryCard[1] = new Array(16, "51,52,53,54,55", 16);
	g_aryCard[2] = new Array(17, "60", 16);
	g_aryCard[3] = new Array(18, "34,37", 15);		
	

	//get the value of both inputs using the past in input names
	l_oCard = p_oForm.elements[p_inpPayMethod]
	l_oCardNumber = p_oForm.elements[p_intCardNumber]
	
	l_strCard = l_oCard.value			
	l_strCardNumber = l_oCardNumber.value
	
	if (l_strCard == '')
	{
		alert('Select Card Type');
		l_oCard.focus();
		return false;
	}

	//loop through each card in the array to find a paytype match.
	for(x = 0; x < g_aryCard.length; x++){
		
		if(l_strCard == g_aryCard[x][0]){
			//make sure the cardnumber length is correct for the selected card.
			if(l_strCardNumber.length !=  g_aryCard[x][2]){
				//alert(l_strCardNumber.length)
				alert('Card number is not the correct length')
				l_oCardNumber.focus()
				return false
			}
			
			//if the length is correct, check the prefix of the card.
			//some cards have multiple prefixes so make an array and loop
			l_aryPrefix = g_aryCard[x][1].split(',')
			l_bolHasMatch = false
			
			//loop through each prefix to find a match
			for(y = 0; y < l_aryPrefix.length; y++){
				//get the length of each prefix so we know how many digits to compare
				l_intLen = l_aryPrefix[y].length

				if(l_strCardNumber.substring(0,l_intLen) ==	l_aryPrefix[y]){												
					l_bolHasMatch = true
					break
				}						
			}
			
			//check if we found an error
			if(!l_bolHasMatch){
				alert('Payment Type does not match Card Number')
				l_oCardNumber.focus()
				return false
			}

		}
	}			
	return true;
}



