var aRandoms = new Array();
aRandoms[0] = new Array();
aRandoms[1] = new Array();
aRandoms[2] = new Array();
aRandoms[0][0]="Type the fourth word of this sentence";
aRandoms[0][1]="word";
aRandoms[1][0]="What is the sum of 3 + 3?";
aRandoms[1][1]="6";
aRandoms[2][0]="Spell the word \"one\"";
aRandoms[2][1]="one";

var randomnumber=Math.floor(Math.random()*aRandoms.length);

function isEmpty(field) {
    return typeof(field) == undefined || field == null || field.value.length == 0 || field.value == '';
}

function isValidEmailAddress(e) {
    var ok = "1234567890qwertyuiop[]asdfghjklzxcvbnm.@-_QWERTYUIOPASDFGHJKLZXCVBNM";
    for(var i=0; i < e.length; i++) {
        if(ok.indexOf(e.charAt(i))<0){
            return (false);
        }
    }
    var re = /(@.*@)|(\.\.)|(^\.)|(^@)|(@$)|(\.$)|(@\.)/;
    var re_two = /^.+\@(\[?)[a-zA-Z0-9\-\.]+\.([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$/;
    if (!e.match(re) && e.match(re_two)) {
        return (true);
    }

    return false;
}

function validateForm(form) {
	var returnVal = true;
	var email = false;
	var msg = "_______________________________________________________\n\n";
	msg +=    "The form was not submitted because of the following error(s).\n";
	msg +=    "Please correct these error(s) and re-submit.\n";
	msg +=    "_______________________________________________________\n\n";
	
	if(isEmpty(form.contactname)) {
		msg += "Full Name is a required field.\n";
		returnVal = false;
	}
	
	if(isEmpty(form.email)) {
		msg += "Email Address is a required field.\n";
		returnVal = false;
	} else if(!isValidEmailAddress) {
		msg += "Please provide a valid Email Address.\n";
		returnVal = false;
	}
	
	if(isEmpty(form.telephone)) {
		msg += "Telephone is a required field.\n";
		returnVal = false;
	}
	
	if(isEmpty(form.enquiry)) {
		msg += "Tell us your story!\n";
		returnVal = false;
	}

	if(form.security.value != aRandoms[randomnumber][1]) {
		msg += "The answer given for the spam prevention question was incorrect.\n";
		returnVal = false;
	}
	
	if(!form.terms.checked) {
		msg += "You must agree to the terms and conditions.\n";
		returnVal = false;
	}

	if(!returnVal) {
		alert(msg);
	} 
	
	return returnVal;
}