function resetValue(IdElement){   
  if(document.quick_form.elements[IdElement].value == 'Postcode' || document.quick_form.elements[IdElement].value == 'DD' || document.quick_form.elements[IdElement].value == 'MM' || document.quick_form.elements[IdElement].value == 'YYYY' || document.quick_form.elements[IdElement].value == 'Name' || document.quick_form.elements[IdElement].value == 'Email' || document.quick_form.elements[IdElement].value == 'Telephone'){
	document.quick_form.elements[IdElement].value = "";
  }
}
function afterFocus(IdElement){  
  if(document.quick_form.elements[IdElement].value == ""){
	if (IdElement == 'distance' || IdElement == 'to'){ 
	   document.quick_form.elements[IdElement].value = 'Postcode';
	}
	if (IdElement == 'day'){ 
	   document.quick_form.elements[IdElement].value = 'DD';
	}
	if (IdElement == 'month'){ 
	   document.quick_form.elements[IdElement].value = 'MM';
	}
	if (IdElement == 'year'){ 
	   document.quick_form.elements[IdElement].value = 'YYYY';
	}
	if (IdElement == 'name'){ 
	   document.quick_form.elements[IdElement].value = 'Name';
	}
	if (IdElement == 'email'){ 
	   document.quick_form.elements[IdElement].value = 'Email';
	}
	if (IdElement == 'telephone'){ 
	   document.quick_form.elements[IdElement].value = 'Telephone';
	}
  }
}
function checkEmail(mail){
  var reg = new RegExp('[a-z0-9]+([_|\.|-]{1}[a-z0-9]+)*@[a-z0-9]+([_|\.|-]­{1}[a-z0-9]+)*[\.]{1}[a-z]{2,6}$', 'i');  
  if(reg.test(mail)){
    return (true);
  }
  else {
	return (false);
  }
}
function checkDay(day){
  var check = parseInt(day, 10);  
  if(check>= 1 && check <= 31){
    return (true);
  }
  else {
	return (false);
  }
}
function checkMonth(month){
  var check = parseInt(month, 10);  
  if(check >= 1 && check <= 12){ 
    return (true);
  }
  else {
	return (false);
  }
}
function checkYear(year){
  var check = parseInt(year, 10);
  var now = new Date(); 
  var current_year = now.getFullYear();  
  if(check >= current_year){ 
    return (true);
  }
  else {
	return (false);
  }
}
function number_day(day, month){
  var tab = new Array(31,28,31,30,31,30,31,31,30,31,30,31);
  var x = 0; 
  var i = 0;  
  for(i=0; i < month-1; i++){ 
    x+=tab[i];
  }
  x+=day;
  return x;
}
function checkCurrentDate(date, month, year){
  var checkDate = parseInt(date, 10);
  var checkMonth = parseInt(month, 10);
  var checkYear = parseInt(year, 10);  
  var now = new Date();
  if(checkYear > now.getFullYear()){ 
    return (true);
  }
  else if(checkYear == now.getFullYear() && number_day(checkDate, checkMonth) >= number_day(now.getDate(), now.getMonth()+1)){ 
    return (true);
  }
  else {
	return (false);
  }
}
function checkTelephone(telephone){
  var check = telephone.length;  
  if(check >= 8){ 
    return (true);
  }
  else {
	return (false);
  }
}
function form_validation(){ 
  var strMsg = ""; 
  var now = new Date();
  var date = now.getDate();
  var month = now.getMonth() + 1;
  var year = now.getFullYear();
  var form = document.quick_form;    
  // Error message when the "Distance" field is blank
  if(form.distance.value == "" || form.distance.value == "Postcode"){
	strMsg += 'Distance: \t Enter a poscode \n'; 
  }  
  // Error message when the "To" field is blank
  if(form.to.value == "" || form.to.value == "Postcode"){
	strMsg += 'To: \t\t Enter a postcode \n'; 
  }  
  // Error message when the "Or Mileage" field isn't selected
  if(form.mileage.value == 0){
	strMsg += 'Or mileage: \t Select a mileage \n'; 
  }
  // Error message when the "Day" field is blank
  if(form.day.value == "" || form.day.value == "DD"){
	strMsg += 'Day: \t\t Enter a valid day \n'; 
  }
  // Error message when the "Day" field don't contains a number between 1 and 31
  else if(isNaN(form.day.value) || checkDay(form.day.value) == false){
	strMsg += 'Day: \t\t Enter a number between 1 and 31\n';
  }  
  // Error message when the "Month" field is blank
  if(form.month.value == "" || form.month.value == "MM"){
	strMsg += 'Month: \t\t Enter a valid month \n'; 
  }
  // Error message when the "Month" field don't contains a number between 1 and 12
  else if(isNaN(form.month.value) || checkMonth(form.month.value) == false){
	strMsg += 'Month: \t\t Enter a number between 1 and 12 \n';
  } 
  // Error message when the "Year" field is blank
  if(form.year.value == "" || form.year.value == "YYYY"){
	strMsg += 'Year: \t\t Enter a valid year \n'; 
  }
  // Error message when the "Year" field don't contains a number up or egal to the current year 
  else if(isNaN(form.year.value) || checkYear(form.year.value) == false){
	strMsg += 'Year: \t\t Enter a number egal or up to ' + year + ' \n';
  } 
  // Error message when the entered date is up or not egal to the current date
  if(checkCurrentDate(form.day.value, form.month.value, form.year.value) == false){
	strMsg += 'Date: \t\t Enter a date egal or up to ' + date + '/' + month + '/' + year + ' \n';	
  }
  // Error message when the "Property" field isn't selected
  if(form.property.value == 0){
	strMsg += 'Property: \t Select a property \n'; 
  }
  // Error message when the "Name" field is blank
  if(form.name.value == "" || form.name.value == "Name"){
	strMsg += 'Name: \t\t Enter a name \n'; 
  }
  // Error message when the "Email" field is blank
  if(form.email.value == "" || form.email.value == "Email" || checkEmail(form.email.value) == false){
	strMsg += 'Email: \t\t Enter a valid email \n'; 
  }
  // Error message when the "Telephone" field is blank
  if(form.telephone.value == "" || form.telephone.value == "Telephone"){
	strMsg += 'Telephone: \t Enter a valid telephone \n'; 
  }
  // Error message when the "Telephone" field don't contains a number and don't have at least 8 digits
  else if(isNaN(form.telephone.value) || checkTelephone(form.telephone.value) == false ){
	strMsg += 'Telephone: \t Enter a number \n';
  }
  if(strMsg != ""){ 
	var strAlert = "Following fields are missed or wrong\n\n"; 
	strAlert += strMsg; 
	strAlert += "\n\n"; 
	alert(strAlert); 
  }
  else{ 
	form.submit();
  } 
} 
function form_contact_validation(){ 
  var strMsg = ""; 
  var form = document.contact_form;  
  // Error message when the "First Name" field is blank
  if(form.contact_first_name.value == ""){
	strMsg += 'First Name: \t Enter a first name \n'; 
  } 
  // Error message when the "Last Name" field is blank
  if(form.contact_last_name.value == ""){
	strMsg += 'Last Name: \t Enter a last name \n'; 
  }
  // Error message when the "Email Address" field is blank
  if(form.contact_email_address.value == ""){
	strMsg += 'Email Address: \t Enter an email address \n'; 
  } 
  // Error message when the "Email Address" field don't respect email format
  else if(checkEmail(form.contact_email_address.value) == false){
	strMsg += 'Email Address: \t Enter a valid email address \n'; 
  }
  // Error message when the "Contact Number" field is blank
  if(form.contact_number.value == ""){
	strMsg += 'Telephone: \t Enter a contact number \n'; 
  }
  // Error message when the "Contact Number" field don't contains a number and don't have at least 8 digits
  else if(isNaN(form.contact_number.value) || checkTelephone(form.contact_number.value) == false ){
	strMsg += 'Telephone: \t Enter a valid contact number \n';
  }
  // Error message when the "Feedback" field is blank
  if(form.contact_feedback.value == ""){
	strMsg += 'Feedback: \t Enter your feedback \n'; 
  }  
  if(strMsg != ""){ 
	var strAlert = "Following fields are missed or wrong\n\n"; 
	strAlert += strMsg; 
	strAlert += "\n\n"; 
	alert(strAlert); 
  }
  else{ 
	form.submit();
  } 
} 
