function validate_enquiry() {
    // First name is compulsary
    if ($("#enquiry").val().length == 0) {
	    $("#enquiry").effect("highlight", { color: "#d31145" }, 800);
	    $("p[id$='enquiry_error']").show();
       	return(false);       	
	} else {
	    $("p[id$='enquiry_error']").hide();
       	return(true);    	
	}
}

function validate_first_name() {
    // First name is compulsary
    if ($("#first_name").val().length == 0) {
	    $("#first_name").effect("highlight", { color: "#d31145" }, 800);
	    $("p[id$='first_name_error']").show();
       	return(false);       	
	} else {
	    $("p[id$='first_name_error']").hide();
       	return(true);    	
	}
}

function validate_last_name() {
    // Last name is compulsary
    if ($("#last_name").val().length == 0) {
        $("#last_name").effect("highlight", { color: "#d31145" }, 800);
        $("p[id$='last_name_error']").show();
        return (false);
    } else {
        $("p[id$='last_name_error']").hide();
        return (true);
    }
}

function validate_phone() {
    // Default to a valid phone number
    var valid_phone = true;

    // Phone is compulsary
    if ($("#phone").val().length == 0) {
        valid_phone = false;
    } else {
        // Check that the phone number only contains specific chars
        // (i.e. numbers and '-', '+'
        for (i = 0; i < $("#phone").val().length && valid_phone; i++){
            // If any character does not match our permitted
            // chars then we set valid = false
            var current_char = $("#phone").val().charAt(i);
            if (current_char != '-' && current_char != '+' && current_char != '(' && current_char != ')' && isNaN(current_char)) {
                valid_phone = false;
            }            
        }
    }

    // Depending on the validation, return true or false
    if (valid_phone) {
        $("p[id$='phone_error']").hide();
        return (true);
    } else {
        $("#phone").effect("highlight", { color: "#d31145" }, 800);
        $("p[id$='phone_error']").show();
        return (false);
    }
}

function validate_email(){
    // Email address must contain an ampersand and period
	if ($("#email").val().indexOf("@") == -1 || $("#email").val().indexOf(".") == -1){
    	$("#email").effect("highlight", { color: "#d31145" }, 800);
    	$("p[id$='email_error']").show();
       	return(false);        	
	} else {
	    $("p[id$='email_error']").hide();
       	return(true);     	
	}
}

function validate_emailverify(){
    // The verify email address field must match the email field
	if ($("#email").val() != $("#emailverify").val()){
    	$("#emailverify").effect("highlight", { color: "#d31145" }, 800);
    	$("p[id$='emailverify_error']").show();
       	return(false);        	
	} else {
	    $("p[id$='emailverify_error']").hide();
       	return(true);     	
	}
}

function validate_prefered_language() {
    // prefered_language is compulsary
    if ($("#prefered_language").val().length == 0) {
        $("#prefered_language").effect("highlight", { color: "#d31145" }, 800);
        $("p[id$='prefered_language_error']").show();
        return (false);
    } else {
        $("p[id$='prefered_language_error']").hide();
        return (true);
    }
}

function validate_all(){
	valid = true;

	if (!validate_first_name()) {
	    valid = false;
	}

	if (!validate_last_name()) {
	    valid = false;
	}

	if (!validate_phone()) {
	    valid = false;
	}

	if (!validate_email()) {
	    valid = false;
	}

	if (!validate_emailverify()) {
	    valid = false;
	}

	if (!validate_prefered_language()) {
	    valid = false;
	}

	if (!validate_enquiry()) {
	    valid = false;
	}
	
	return(valid);
}

$(document).ready(function() {
    $("#first_name").blur(function() {
        validate_first_name();
    });
    $("#last_name").blur(function() {
        validate_last_name();
    });
    $("#phone").blur(function() {
        validate_phone();
    });
    $("#email").blur(function() {
        validate_email();
    });
    $("#emailverify").blur(function() {
        validate_emailverify();
    });
    $("#prefered_language").blur(function() {
        validate_prefered_language();
    });
    $("#prefered_language").blur(function() {
        validate_enquiry();
    });

    // Register global error handler
    window.onerror = function(message, uri, line) {
        // An unplanned exception occured, it should not stop
        // the form working however, as we've caught it here.  We know
        // that typically it is a cosmetic UI error in JQuery on IE8.
        // Return true to prevent the browser from raising an exception.
        return true;
    }
});     	