// This document is used to validate a form before submition.
// Checks if fields are empty or incorrect, if so 
// warning alert pops up and focus gets returned to that field.
// ============================================================

// (C) 2000 www.CodeLifter.com
// Email Address Checker-Validator
// All 4.0 and later browsers

function checkEmailAddress(field) {

//  Note: The next expression must be all on one line...
//        allow no spaces, linefeeds, or carriage returns!

    var goodEmail = field.value.match(/\b(^(\S+@).+((\.com)|(\.net)|(\.edu)|(\.mil)|(\.gov)|(\.org)|(\.biz)|(\.pro)|(\.info)|(\.name)|(\.museum)|(\.coop)|(\.aero)|(\..{2,2}))$)\b/gi);

    if (goodEmail) return true; else return false;
}

function validRequired(formField,fieldLabel) {

    if (formField.value == "") {
        alert("Please enter your '" + fieldLabel +"'");
        formField.focus();
        return false;
    }
    return true;
}

// ============================================================

function checkForm() {

// This doesn't work with only 1 radio button, need at least 2.
    selected = false;
    for (var i = 0; i < document.theForm.Enquiry_type.length; i++)
        if (document.theForm.Enquiry_type[i].checked == true)
            selected = true;
    if (selected == false) {
        alert("Please select from 'Enquiry type'");
        document.theForm.First_name.focus();                // Radio button doesn't support focus() so we return it to other closest input
        return false;
    }

    if (!validRequired(document.theForm.First_name,"First name"))
        return false;

    if (!validRequired(document.theForm.Last_name,"Last name"))
        return false;

    if (!checkEmailAddress(document.theForm.Email)) {
        alert("Please enter a valid email address.");
        document.theForm.Email.focus();
        return false;
    }

    if (!validRequired(document.theForm.Phone,"Phone"))
        return false;

    if (!validRequired(document.theForm.Message,"Message"))
        return false;
}