/*****************************************************************

validation

*****************************************************************/

function checkEmail(emField){

var fieldValue = emField.value  // store field's value in variable, "fieldValue"

	if(fieldValue != ""){  //if field is not empty
	var atSymbol = 0
	
		for(var a = 0; a < fieldValue.length; a++){ //loop through field value string
			if(fieldValue.charAt(a) == "@"){ //look for @ symbol and for each @ found, increment atSymbol variable by 1
			atSymbol++
			}
		}
		
		if(atSymbol > 1){ // if more than 1 @ symbol exists
		alert("Please Enter A Valid Email Address") // then cancel
		return false
		}
		
		if(atSymbol == 1 && fieldValue.charAt(0) != "@"){  // if @ symbol was found, and it is not the 1st character in string
		var period = fieldValue.indexOf(".",fieldValue.indexOf("@")+2) //look for period at 2nd character after @ symbol
		var twoPeriods = (fieldValue.charAt((period+1)) == ".") ? true : false // "."  immediately following 1st "." ? 
				  
		//if period was not found OR 2 periods together OR field contains less than 5 characters OR period is in last position
			if(period == -1 || twoPeriods || fieldValue.length < period + 2 || fieldValue.charAt(fieldValue.length-1)=="."){
			alert("Please Enter A Valid Email Address") // then cancel
			return false
			}
		  		  
		}
		else{  // no @ symbol exists or it is in position 0 (the first character of the field)
		alert("Please Enter A Valid Email Address")
		return false // then cancel
		}
	}
	else{  // if field is empty
	alert("Please Enter A Valid Email Address")
	return false  // then cancel
	}
	

}


// The below function is needed only when using an image to invoke the function
function submitForm(){
var myForm = document.imageForm
var eField = myForm.email
	if(checkEmail(eField) != false){
	myForm.submit()
	}

}