function isValidEmail(emailAddress) {
    var re = /^(([^<>()[\]\\.,;:\s@\"]+(\.[^<>()[\]\\.,;:\s@\"]+)*)|(\".+\"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/
    return re.test(emailAddress);
}
var FieldList = "";
var checkboxMessage=""
var HighLightColour = '#FFCCCC';
// Compulsary Field Checker
function checkCompulsory(form) {
    var keyword="_compulsory";
    if (document.images) {
        for (i=0;i<form.length;i++) {
            var tempobj=form.elements[i];
            if (tempobj.name.substring((tempobj.name.length-keyword.length),(tempobj.name.length))==keyword.toString()) {
                //Type checker text / textarea / selectionbox
                //alert(tempobj.name)
                shortFieldName=tempobj.name.substring(0,tempobj.name.length-keyword.length);
                shortFieldName=shortFieldName.slice(5);
                shortFieldName=shortFieldName.replace(/_/g," ")
                if (((tempobj.type=="text"||tempobj.type=="textarea")&&tempobj.value=='')||(tempobj.type.toString().charAt(0)=="s"&&tempobj.selectedIndex==0)) {
                    // Trims 'Form_' and '_compulsory' from names for display to user
                    FieldList += shortFieldName + "\n";
                    tempobj.style.backgroundColor=HighLightColour;
                    } else if (tempobj.type=="checkbox"&&!tempobj.checked){
                    if (checkboxMessage==''){
                        checkboxMessage='\nPlease ensure that you have selected:\n';
                    }
                    tempobj.style.backgroundColor=HighLightColour
                    checkboxMessage += shortFieldName+"\n";
                }
            }
        }
        
    }
}
function titlecase(form) {
    if (document.images) {
        for (i=0;i<form.length;i++) {
            var keyword = "_titlecase";
            var tempobj=form.elements[i];
            if (tempobj.name.substring((tempobj.name.length-keyword.length),(tempobj.name.length))==keyword.toString()) {
                shortFieldName=tempobj.name.substring(0,tempobj.name.length-keyword.length);
                tempobj.name = shortFieldName;
                
                var newtempobj = (tempobj.value.substring(0,1)).toUpperCase() + tempobj.value.substring(1);
                tempobj.value = newtempobj;
            }
    }}
}
function uppercase(form) {
    if (document.images) {
        for (i=0;i<form.length;i++) {
            var keyword = "_uppercase";
            var tempobj=form.elements[i];
            if (tempobj.name.substring((tempobj.name.length-keyword.length),(tempobj.name.length))==keyword.toString()) {
                shortFieldName=tempobj.name.substring(0,tempobj.name.length-keyword.length);
                tempobj.name = shortFieldName;
                
                var newtempobj = (tempobj.value.substring(0)).toUpperCase();
                tempobj.value = newtempobj;
            }
    }}
}
function lowercase(form) {
    if (document.images) {
        for (i=0;i<form.length;i++) {
			var keyword = "form_";
			var tempobj=form.elements[i];
			if(tempobj.type=="text"||tempobj.type=="textarea"){
			if (tempobj.name.substring(0,(keyword.length))==keyword.toString()) {
                    var tempobj=form.elements[i];
                          var newtempobj = (tempobj.value.substring(0)).toLowerCase();
                tempobj.value = newtempobj;   
			}}
    }}
}
function telephone(form) {
    if (document.images) {
        for (i=0;i<form.length;i++) {
            var keyword = "Telephone_compulsory";
            var tempobj=form.elements[i];
            if (tempobj.name.substring((tempobj.name.length-keyword.length),(tempobj.name.length))==keyword.toString()) {
       
                var newtempobj = tempobj.value.substring(0,5)+""+tempobj.value.substring(5,(tempobj.value.length));
                tempobj.value = newtempobj;
            }
    }}
}
function ValidateForm() {
    var emailMessage = "";
    alertMessage = "We did not get all the information we need from you.\n Please fill in the following field(s):\n\n";
    // Check all compulsory fields are filled.
    lowercase(document.contactForm);
	titlecase(document.contactForm);
    uppercase(document.contactForm);
	telephone(document.contactForm);
    checkCompulsory(document.contactForm);
    // Email validation.
    if (document.contactForm.form_Email_address_compulsory.value == ""){
        //FieldList = FieldList + "Email Address\n"; // Message to user.
        } else {
        validEmail = isValidEmail(document.contactForm.form_Email_address_compulsory.value);
        if (validEmail == false) {
            document.contactForm.form_Email_address_compulsory.style.backgroundColor=HighLightColour;
            emailMessage = "\nPlease enter a valid email address"; // Message to user.
        }
    };
    
    // Submit form or display message.
    if (FieldList == "" && emailMessage == ""){
        document.contactForm.submit();
        } else {
        if (FieldList != "") {
            alertMessage += FieldList+checkboxMessage;
        }
        
        if (emailMessage != "") {
            alertMessage += emailMessage;
        }
        // Clear off existing messages
        FieldList = ''
        checkboxMessage = ''
        alert(alertMessage);
    };
}

