// UPDATE LOG
// **********************
// Date  04/03/2008
// Fixed date diff to dynamically show the correct number automaticaly
// **********************
// Date  07/11/2006
// Added date gap checker to look for number of days between 2 dates (dateGap = x)
// **********************
// Date  05/10/2006
// Added dropdown checker
// Added 2 variables, 1 for turning input field a different colour on failure, and 1 for showing text in inout filed
// **********************


function sendForm(nameOfForm) {

//-->> Do not forget to set up the following CSS styles <<---
// --> 1) #warningText	{ color: red; font-size: 14px; font-family: Arial, Helvetica, Geneva, Swiss, SunSans-Regular; margin-top: 8px; display: none }
// --> 2) .formText		{ color: #797979; font-weight: bold; font-size: 11px; font-family: Arial, Helvetica, Geneva, Swiss, SunSans-Regular }
// --> 3) .FormTextFail	{ color: red; font-weight: bold; font-size: 11px; font-family: Arial, Helvetica, Geneva, Swiss, SunSans-Regular }
// --> 4) .formTextInput  { color: #061947; font-weight: bold; font-size: 12px; font-family: Arial, Helvetica, Geneva, Swiss, SunSans-Regular; background-color: #ced1db; border: solid 1px #061947 }
// --> 5) .formTextInputFail   { color: #fff; font-weight: bold; font-size: 12px; font-family: Arial, Helvetica, Geneva, Swiss, SunSans-Regular; background-color: #ffb13c; border: solid 1px #061947 }



//Set all variables--------
send = "yes"
formList = ""
x = 0
formElementName = ""
emailError = "no"
filter = /^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/
FormNameUsed = nameOfForm
tempRadioName = ""

colorInputField = "yes"
showTextInField = "no"


//-------------------------

// Check all elements

while (x < document.forms[FormNameUsed].elements.length) {
	 
	formElementName = document.forms[FormNameUsed].elements[x].name + "C"  // Needed to fix crappy IE
	formElementNameR = document.forms[FormNameUsed].elements[x].name       // Holds the original name of field
	formElementValue = document.forms[FormNameUsed].elements[x].value	   // Holds the value of the field	
	formElementType = document.forms[FormNameUsed].elements[x].type		   // Holds the element type

	if (formElementNameR != tempRadioName) {
		emailError = "no" }
		              
    // Lets change the colour on blank fields    
    if (document.getElementById(formElementName)) {
        
        // Check to see if a tick box needs checking
        if (formElementType == "checkbox") {
        
        	toCheck = eval("document.forms[FormNameUsed]."+formElementNameR+".checked")
        	
        	if (toCheck) {
						emailError = "no"   }  // Passed
					else {
						emailError = "yes"  } // Error
						        
			} // verification complete
        
        
        // Check to see if a dropdown box needs checking
        if (formElementType == "select-one") {
                
        	toCheck = eval("document.forms[FormNameUsed]."+formElementNameR+".selectedIndex")
        	
        	if (toCheck != 0) {
						emailError = "no"	}  // Passed
					else {
						emailError = "yes"  } // Error
						        
			} // verification complete

        
    
		// Check to see if it's a radio button AND that it has not yet been checked
		if (formElementType == "radio" && formElementNameR != tempRadioName) {	
														
			fieldLength = eval("document.forms[FormNameUsed]."+formElementNameR+".length")	
			
			for (var i=0; i< fieldLength ; i++) {

				toCheck = eval("document.forms[FormNameUsed]."+formElementNameR+"[i].checked")
				
					if (toCheck) {
						emailError = "no" 
						break				}  // Passed
					else {
						emailError = "yes"  } // Error
						
						}
						
					tempRadioName = formElementNameR // Store the radio group name to prevent re-checking
			} // verification complete
				
    
		//Check to see if an email address needs verifying
		if (formElementName.match("email")) {
    	
    		// We have got an email to verify		
			str = eval("document.forms[FormNameUsed]."+formElementNameR+".value")
												
			if (filter.test(str)){
				emailError = "no" }  // Passed
			else {
				emailError = "yes" } // Error
    		} // verification complete
    		
    		    		
		//Check to see if field should be numeric
		if (formElementName.match("num")) {
    	    	
    		// We have got mumeric field to verify		
			valueToCheck = eval("document.forms[FormNameUsed]."+formElementNameR+".value")
			
			if (isNaN(valueToCheck)){
				emailError = "yes" }  // Error
    		} // verification complete
    		
    	
    	//Check to see if passwords should match
		if (formElementName.match("pswd")) {
    	    	    	    
    		// We have got a password field to verify		
			valueToCheck = eval("document.forms[FormNameUsed]."+formElementNameR+".value")
			valueToCheck2 = eval("document.forms[FormNameUsed].pswd2.value")
						
			if (valueToCheck != valueToCheck2){
				emailError = "yes" }  // Error
    		} // verification complete


		//Check for date gap setting
		if (formElementName.match("dateGap")) {
				
			//Set 1 day in milliseconds
			var one_day = 1000*60*60*24
    	    	    	    
    		// We have got a date gap field to verify	
			dg1 = new Date(eval("document.forms[FormNameUsed].numYearFrom.value"),eval("document.forms[FormNameUsed].numMonthFrom.value"),eval("document.forms[FormNameUsed].numDayFrom.value"))
			dg2 = new Date(eval("document.forms[FormNameUsed].numYearTo.value"),eval("document.forms[FormNameUsed].numMonthTo.value"),eval("document.forms[FormNameUsed].numDayTo.value"))
			
			// Work out the difference
			dateDiffer = Math.ceil((dg2.getTime()-dg1.getTime())/(one_day))
			
			// Max difference
			maxDiff = eval("document.forms[FormNameUsed].dateGap.value")		
			
			// Check to see if its to big a gap
			if (dateDiffer > maxDiff){
				emailError = "yes" 
				document.getElementById("dateGapC").innerHTML = "<BR>Events can only be " + maxDiff + " days in length. Click <a href='mailto:ice@iti.org.uk'>here</a> to email ICE."
				}  // Error
				
			else {
				document.getElementById("dateGapC").innerHTML = ""
				}
		
    		} // verification complete



    	// Check for uncompleted fields
    		if (document.forms[FormNameUsed].elements[x].value == "" || emailError == "yes") {
    		
    			// means the field has failed and the colour set to error
    			document.getElementById(formElementName).className = "FormTextFail"
    			
    			// color input field
    			if (colorInputField == "yes") {
    				if (formElementType != "select-one") {
       					document.forms[FormNameUsed].elements[x].className = "formTextInputFail"
       					}
       				}   			
    			
    			// show field name in input box
    			if (showTextInField == "yes") {
    				document.getElementById(formElementName).value = formElementNameR
    				}
    				
    			// set send to no 	
    			send = "no"
    			
    				}
    		else {
    			// means the field has passed so set the colour to normal
    			document.getElementById(formElementName).className = "FormText"
    			document.forms[FormNameUsed].elements[x].className = "formTextInput"
    		}
		}
	x ++   // Get the next form element  
	}
   
// Send the form if it has passed OR display error if it hasn't
if (send=="no") {
document.getElementById('warningText').style.display="block"
return false  // form failed

}

if (send == "yes") {
return true  // form sent

}
}