// JavaScript code to check the register form
function formValidation() {
	//get the input from the user
	var question = document.getElementById("question");
	var first_name = document.getElementById("first_name");
	var email = document.getElementById("email");
	var phone = document.getElementById("phone");
	var postcode = document.getElementById("postcode");
	var country = document.getElementById("country");
	var contactway = document.getElementById("contactway");
	var agreement = document.getElementById("agreement");
	

	//check the data
	
	if(chk_text(question,"please enter a valid question! at least 10 characters.")) {
	if(char(first_name,"Invalid data in first name field! please enter characters only.")) {
	if(chk_email(email,"invalid email address!")) {
	if(chk_phone(phone,"please enter a valid phone! at least 10 digits.")) {
	if(chk_zipcode(postcode,"please enter a valid post code, must be 5 digits!")) {
	if(chk_select(country,"please select your country!")) {
	if(chk_select(contactway,"please select your preferred contact!")) {
	if(chk_textBox(agreement,"you must agree to be contacted by peugeot!")) {
		return true;
	}}}}}}}}
	return false;
}


// function to check all the select options
function chk_textBox(element, helperMsg) {
if(document.form1.agreement.checked == true) {
	return true;
} else {
	alert(helperMsg);
	return false;
}
}


// function to check all the select options
function chk_select(element, helperMsg) {
if(element.value == "") {
	alert(helperMsg);
	element.focus();
	return false;
} else {
	return true;
}
}

// function to check if the input is alphabet and num and spaces
function char(element,helperMsg) {
if(element.value == "") {
	alert("this field is empty please enter a value");
	element.focus();
	return false;
	}
	var charExp = /^[a-zA-Z]+[\s]?[a-zA-Z]+$/;
	if(element.value.match(charExp)) {
		return true;
	} else {
		alert(helperMsg);
		element.focus();
		return false;
	}
}



// function to check if email is valid
function chk_email(element,helperMsg) {
if(element.value == "") {
	alert("this field is empty please enter a value");
	element.focus();
	return false;
	}
	var emailExp = /^[\w\-\.\+]+\@[a-zA-Z0-9\.\-]+\.[a-zA-Z0-9]{2,4}$/;
	if(element.value.match(emailExp)) {
		return true;
	} else {
		alert(helperMsg);
		element.focus();
		return false;
	}
	
}



// function to check if the phone is numeric and 10 digits
function chk_phone(element,helperMsg) {
if(element.value == "") {
	alert("this field is empty please enter a value");
	element.focus();
	return false;
	}
	var numExp = /^[0-9]+$/;
	if ((element.value.match(numExp))&&(element.value.length >= 10)) {
		return true;
	} else {
		alert(helperMsg);
		element.focus();
		return false;
	}
}


// function to check if the phone is numeric and 10 digits
function chk_zipcode(element,helperMsg) {
if(element.value == "") {
	alert("this field is empty please enter a value");
	element.focus();
	return false;
	}
	var numExp = /^[0-9]+$/;
	if ((element.value.match(numExp))&&(element.value.length == 5)) {
		return true;
	} else {
		alert(helperMsg);
		element.focus();
		return false;
	}
}



// function to check if the input is alphabet
function chk_text(element,helperMsg) {
if(element.value == "") {
	alert("this field is empty please enter a value");
	element.focus();
	return false;
	}
	var alphaNumExp = /^.+$/;
	if ((element.value.match(alphaNumExp))&&(element.value.length >= 10)) {
		return true;
	} else {
		alert(helperMsg);
		element.focus();
		return false;
	}
}

