// 	Vladimir Kagan Form Verification API(JavaScript)
// 	Author : EJFIIIWebDesign.com

// Validate user inputs
// Discover particular errors and add to errorCatch container
	function verifyForm() {
		errorMsgClear();
		errorCatch="";

		// Check Minimum Requirements
		if (document.getElementsByName("Name")[0].value.length < 1) {errorCatch+="A";}			//error if Name not provided
		if (document.getElementsByName("Telephone")[0].value.length < 1) {errorCatch+="B";}		//error if Telephone Number not provided
		if (document.getElementsByName("Email")[0].value.length < 1) {errorCatch+="C";}			//error if Email not provided
		if (document.getElementsByName("Challenge")[0].value.length < 1) {errorCatch+="D";}		//error if Captcha Answer not provided

		// Check for Proper Email Address
		fieldValue=document.getElementsByName("Email")[0].value;
		if (fieldValue.length>0) {
			present = fieldValue.search("@");
			if (present == -1) errorCatch += "E";												//error if @ not present in e-mail
			if (fieldValue.indexOf("@") < 1) errorCatch += "E";									//error if @ in first position
			if (fieldValue.charAt(fieldValue.length - 4) != ".") {
				if (fieldValue.charAt(fieldValue.length - 3) != ".") errorCatch += "E";			//error if . not placed correctly
				}
			if (fieldValue.indexOf("@") == fieldValue.lastIndexOf(".")-1) errorCatch += "E";	//error if domain not present in e-mail
			}
		
		// Check for Proper Telephone Number
		fieldValue=document.getElementsByName("Telephone")[0].value;
		if (fieldValue.length>0) {
			validChars = "0123456789 ().-+/";
			for (i = 0; i <= fieldValue.length-1; i++) {
				if(validChars.indexOf(fieldValue.charAt(i)) == -1) errorCatch += "F";			//error if telephone contains invalid characters 
				}
			}

		// Check for Captcha Answer
		fieldValue=document.getElementsByName("Challenge")[0].value;
		if (fieldValue < 1) errorCatch+="G";													//error if Captcha Question not answered 
			
	// Display errors stored in errorCatch container, if any
		if (errorCatch != "") {
			for (i=0; i < errorCatch.length; i++) {
				errorCode = errorCatch.charAt(i);
				switch(errorCode) {
					case "A":
						document.getElementById("name").style.backgroundColor = "#FFFFAD";			//Display Name not provided error
						document.getElementById("emptyError").style.visibility = "visible";
						break;
					case "B":
						document.getElementById("telephone").style.backgroundColor = "#FFFFAD";		//Display Name not provided error
						document.getElementById("emptyError").style.visibility = "visible";
						break;
					case "C":
						document.getElementById("email").style.backgroundColor = "#FFFFAD";			//Display Email not provided error
						document.getElementById("emptyError").style.visibility = "visible";
						break;
					case "D":
						document.getElementById("challenge").style.backgroundColor = "#FFFFAD";		//Display Captcha Answer not provided error
						break;
					case "E":
						if (errorCatch.indexOf("A") == -1 && errorCatch.indexOf("B") == -1 && errorCatch.indexOf("C") == -1) {										//Display error only if no prior error
							document.getElementById("formatError").style.visibility = "visible";	//Display Email Invalid error
							document.getElementById("email").style.backgroundColor = "#FFFFAD";
							}
						break;
					case "F":
						if (errorCatch.indexOf("A") == -1 && errorCatch.indexOf("B") == -1 && errorCatch.indexOf("C") == -1 && errorCatch.indexOf("E") == -1) {		//Display error only if no prior error
							document.getElementById("telephone").style.backgroundColor = "#FFFFAD";	//Display Telephone Invalid error
							document.getElementById("charError").style.visibility = "visible";
							}
						break;
					case "G":
						if (errorCatch.indexOf("A") == -1 && errorCatch.indexOf("B") == -1 && errorCatch.indexOf("C") == -1 && errorCatch.indexOf("E") == -1 && errorCatch.indexOf("F") == -1) {		//Display error only if no prior error
						document.getElementById("captchaError").style.visibility = "visible";		//Display Captcha Question not answered error
						}
						break;
					}
				}
				document.visitor.Name.focus();
			}
//		else alert("Test Successful - Form Data would have been submitted.");
//		else document.visitor.submit();
		else channelVerify();
		}

// Clear any and all error messages
	function errorMsgClear() {
		document.getElementById("emptyError").style.visibility = "hidden";
		document.getElementById("formatError").style.visibility = "hidden";
		document.getElementById("charError").style.visibility = "hidden";
		document.getElementById("captchaError").style.visibility = "hidden";
		document.getElementById("name").style.backgroundColor = "#1F1F1C";
		document.getElementById("email").style.backgroundColor = "#1F1F1C";
		document.getElementById("telephone").style.backgroundColor = "#1F1F1C";
		document.getElementById("challenge").style.backgroundColor = "#1F1F1C";
		document.visitor.Name.focus();
		}

// 	Basic Form Submission Security Function (JavaScript)
//	Part of formMail.php by EJFIIIWebDesign.com May 2008
//	All Rights Reserved.

	document.write("<link rel='stylesheet' href='fM_Style.css' type='text/css' />");
	function channelVerify() {
		document.getElementsByName("Channel")[0].value = "channeled";
		document.visitor.submit();
		}
