
function autoFill()
{
	if ( document.getElementById("AutoFill").checked  ) 
	{
		for (n=1; n <=8; n++ ) {	
				s = ( n + 8 );
				document.getElementById("Validate" + s).value = document.getElementById("Validate" + n).value;
		}

	} else {

		for (n=8; n <=14; n++ ) {	
				document.getElementById("Validate" + n).value = "";
		}

	}
}

function validate_form(tx) {	
	
	// Reset the php validation error for email
	document.getElementById("Err").innerHTML = "";
	
	var check = 1;
	
	for ( x=1; x <= tx; x++ )
	{ 
	  var chk = document.getElementById("Validate" + x);
	  
		   if ( chk != null ) {
		    
				if ( document.getElementById("Validate" + x).value == ""  ) {					
					document.getElementById("Validate" + x + "Chk").style.visibility = "visible";
					document.getElementById("Validate" + x + "Chk").style.display = "inline";
					check = 0;
					
				} else {
					document.getElementById("Validate" + x + "Chk").style.visibility = "hidden";
					document.getElementById("Validate" + x + "Chk").style.display = "none";
				}
				
		    }
		
	 }	
	  
	 if ( check == 1 ) {

		 check = check_email();
	 }
	 
	 if ( check == 0 ) {
	
		 document.getElementById("Err").innerHTML = "<font color=\"red\">** There are errors in the form. See below.</font><br><br>";
		
		 return false;
	
	 } else {
		 
		 return true;
	 }
	 
}





function isEmail(formInput) {

    if (typeof(formInput) != "object") {
        alert("Validation not supported on this browser.");
        check = 0;
    }

    var message;
    var check = 1;
 

    if (stringEmpty(formInput.value)) {    	
    	alert("formInput.value");
        message = "Error! There is no input value entered.";
        check = 0;
    } else if (noAtSign( formInput.value )) {
        message = "Error! The address \"" + formInput.value + "\" does not contain an '@' character.";
        check = 0;
    } else if (nothingBeforeAt(formInput.value)) {
        message = "Error! The address \"" + formInput.value;
        message += "\" must contain at least one character before the '@' character";
        check = 0;
    } else if (noLeftBracket(formInput.value)) {
        message = "Error! The address \"" + formInput.value;
        message += "\" contains a right square bracket ']',\nbut no corresponding left square bracket '['.";
        check = 0;
    } else if (noRightBracket(formInput.value)) {
        message = "Error! The address \"" + formInput.value;
        message += "\" contains a left square bracket '[',\nbut no corresponding right square bracket ']'.";
        check = 0;
    } else if (noValidPeriod(formInput.value)) {
        message = "Error! The address \"" + formInput.value + "\" must contain a period ('.') character.";
        check = 0;
    } else if (noValidSuffix(formInput.value)) {
        message = "Error! The address \"" + formInput.value;
        message += "\" must contain a two, three or four character suffix.";
        check = 0;
    } else {
        message = "Success! The email address \"" + formInput.value + "\" validates OK.";
        check = 1;
    }

    var objType = typeof(formInput.focus);
    if (objType == "object" || objType == "function") {
         formInput.focus();
    }
    
    if (check == 0) {    	
    	document.getElementById("Validate3Chk").innerHTML = "<span style=\"color:Red\">" + message + "</span>";
    	document.getElementById("Validate3Chk").style.visibility = "visible";
    	document.getElementById("Validate3Chk").style.display = "inline";
    	return message;
    } else {
    	return false;
    }
}


function stringEmpty (formField) {
    // CHECK THAT THE STRING IS NOT EMPTY
    if ( formField.length < 1 ) {
        return ( true );
    } else {
        return ( false );
    }
}

function noAtSign (formField) {
    // CHECK THAT THERE IS AN '@' CHARACTER IN THE STRING
    if (formField.indexOf ('@', 0) == -1) {
        return ( true )
    } else {
        return ( false );
    }
}

function nothingBeforeAt (formField) {
    // CHECK THERE IS AT LEAST ONE CHARACTER BEFORE THE '@' CHARACTER
    if ( formField.indexOf ( '@', 0 ) < 1 ) {
        return ( true )
    } else {
        return ( false );
    }
}

function noLeftBracket (formField) {
    // IF EMAIL ADDRESS IN FORM 'user@[255,255,255,0]', THEN CHECK FOR LEFT BRACKET
    if ( formField.indexOf ( '[', 0 ) == -1 && formField.charAt (formField.length - 1) == ']') {
        return ( true )
    } else {
        return ( false );
    }
}

function noRightBracket (formField) {
    // IF EMAIL ADDRESS IN FORM 'user@[255,255,255,0]', THEN CHECK FOR RIGHT BRACKET
    if (formField.indexOf ( '[', 0 ) > -1 && formField.charAt (formField.length - 1) != ']') {
        return ( true );
    } else {
        return ( false );
    }
}

function noValidPeriod (formField) {
    // IF EMAIL ADDRESS IN FORM 'user@[255,255,255,0]', THEN WE ARE NOT INTERESTED
    if (formField.indexOf ( '@', 0 ) > 1 && formField.charAt (formField.length - 1 ) == ']')
        return ( false );

    // CHECK THAT THERE IS AT LEAST ONE PERIOD IN THE STRING
    if (formField.indexOf ( '.', 0 ) == -1)
        return ( true );

    return ( false );
}

function noValidSuffix(formField) {
    // IF EMAIL ADDRESS IN FORM 'user@[255,255,255,0]', THEN WE ARE NOT INTERESTED
    if (formField.indexOf('@', 0) > 1 && formField.charAt(formField.length - 1) == ']') {
        return ( false );
    }

    // CHECK THAT THERE IS A TWO OR THREE CHARACTER SUFFIX AFTER THE LAST PERIOD
    var len = formField.length;
    var pos = formField.lastIndexOf ( '.', len - 1 ) + 1;
    if ( ( len - pos ) < 2 || ( len - pos ) > 4 ) {
        return ( true );
    } else {
        return ( false );
    }
}


function check_email() {
	
	var check = 1;
	var email = document.getElementById("Validate3");
	var emailconfirm = document.getElementById("Validate4");
	var message = isEmail(email);
	
	if( message ) {
		document.getElementById("Validate3Chk").innerHTML = message;
		document.getElementById("Validate3Chk").style.visibility = "visible";
		document.getElementById("Validate3Chk").style.display = "inline";
		email.focus();
	    check = 0;
	} else if( email.value != emailconfirm.value ) {
		document.getElementById("Validate4Chk").innerHTML = "* email addresses do not match";
		document.getElementById("Validate4Chk").style.visibility = "visible";
		document.getElementById("Validate4Chk").style.display = "inline";
		emailconfirm.focus();
		check = 0;
	} 

	return check;
	
		

}