/* ==========================================================
Mazda/Hyundai Site Javascript

This file deals with form validation

author:    Sam Hocking
website:   http://www.canterburymazda/hyundai.co.uk
============================================================ */

// Helper function to display alert with text for empty/null fields.
function validateRequired(field,alertTxt) {
    with (field) {
    	if (value==null||value==""||value==" "||value==undefined) {
    		alert(alertTxt);
    		return false;
    	} else {
    		return true;
    	}
	}
}

// Helper function to validate email address
function validateEmail(address, alertTxt){
	var emailRegEx = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
	if(address.match(emailRegEx)){
		//valid
		return true;
	}else{
		alert(alertTxt);
		return false;
	}
}

// Validates any UK postcode (excludes British Forces postcodes)
function validatePostcode(postcode, alertTxt){
	var postcodeRegEx = /^([A-PR-UWYZ0-9][A-HK-Y0-9][AEHMNPRTVXY0-9]?[ABEHMNPRVWXY0-9]? {1}[0-9][ABD-HJLN-UW-Z]{2}|GIR 0AA)$/;
	if(postcode.toUpperCase().match(postcodeRegEx)){
		//valid
		return true;
	}else{
		alert(alertTxt);
		return false;
	}
}

function validateFieldLength (value, minLength, alertTxt) {
	if(value.length < minLength) {
		alert(alertTxt);
		return false;
	} else {
		return true;
	}
}

//Main function to validate Search Form
function validateSearchForm(thisForm) {
	with (thisForm) {
		if (make.value == "Select Make") {
			alert("Please select a Make!");
			make.focus();return false;
		}
		if (model.value == "Select Model") {
			alert("Please select a Model!");
			model.focus();return false;
		}
	}
}

//Main function to validate Search Form
function validateContactForm(thisForm) {
	with (thisForm) {
		if (validateRequired(name,"Name must be filled out!")==false) {
			name.focus();return false;
		}
		if (validateRequired(email,"Email must be filled out!")==false) {
			email.focus();return false;
		}
		if (validateRequired(telephone,"Telephone must be filled out!")==false) {
			telephone.focus();return false;
		}
		if(isNaN(telephone.value.replace(/\s/g,''))) {
			alert("Please enter a numerical Telephone number only!");
			telephone.focus();return false;
		}
		
		if(contacttype[0].checked && comments.value.length < 6) {
			alert("Please type your query into the comments section if you wish to ask a question!");
			comments.focus();return false;
		}
		if(contacttype[0].checked && comments.value.length > 500) {
			alert("Please keep comments below 500 characters.\n\nYou have used " + comments.value.length);
			comments.focus();return false;
		}
		if(contacttype[2].checked) {
			if (validateRequired(model,"Please select the model you wish to test drive!")==false) {
				model.focus();return false;
			}
		}
		if(contacttype[3].checked) {
			if (validateRequired(registration,"Please enter the registration of the vehicle you are interested in!")==false) {
				registration.focus();return false;
			}
		}
		if(contacttype[4].checked) {
			if (validateRequired(brochureModel,"Please select the model you would like a brochure of!")==false) {
				brochureModel.focus();return false;
			}
			if (validateRequired(address,"Address must be filled out in order to receive a brochure in the post!")==false) {
				address.focus();return false;
			}
			if (validatePostcode(postcode.value,"Invalid Postcode. To receive a brochure you must enter your postcode with a space between the two parts e.g. CT1 1HD.")==false) {
				postcode.focus();return false;
			}
		}
	}
}
