﻿function validate() {
    // validate
    var name, tel, email, company, postcode;
    name = document.getElementById("Name").value;
    tel = document.getElementById("Telephone").value;
    email = document.getElementById("Email_Address").value;
    company = document.getElementById("Company_Name").value;
    postcode = document.getElementById("Postcode").value;

    // Validate form
    var valForm = new Array();
    if (name == "") {
        valForm[valForm.length] = '\r\n Name is a required field';
    }
    if (tel == "") {
        valForm[valForm.length] = '\r\n Telephone is a required field';
    }
    if (email == "") {
        valForm[valForm.length] = '\r\n E-mail Address is a required field';
    }
    else if (!ValidateItem('email', email)) {
        valForm[valForm.length] = '\r\n Please enter a valid e-mail address';
    }
    if (company == "") {
        valForm[valForm.length] = '\r\n Company is a required field';
    }
    if (postcode == "") {
        valForm[valForm.length] = '\r\n Postcode is a required field';
    }

    // Check for invalid entries
    if (valForm.length > 0) {
        alert('Some errors occurred in your submission: \r\n' + valForm.toString());
        return false;
    }
    else {
        return true;
	}        
}

/*
    Validations
*/
function ValidateItem(ftype, fvalue)
{
    switch(ftype)
    {
	    case "email":
            var objRegExp = /^.+@[^\.].*\.[a-z]{2,}$/;
            //check for format
            return objRegExp.test(fvalue);
    }
} 


