/* This script and many more are available free online at
The JavaScript Source :: http://javascript.internet.com
Created by: Shaun Bailey :: http://www.sbwebdevelopment.co.uk */

function validate() {
  if(!document.getElementById) return;
  
  var firstname = document.getElementById("Text1").value;
  var lastname = document.getElementById("Text2").value;
  var address = document.getElementById("Text3").value;
  var email = document.getElementById("Text4").value;
  var telephone = document.getElementById("Text5").value;
 // var from = document.getElementById("Departing").value;
 // var arriving = document.getElementById("arriving").value;
  
  var incorrect = new Array();
  var no = 0;
  var regExp = /[A-Za-z]{2,6}/;
  
   regExp = /[A-Za-z]/;
  if(regExp.test(firstname)) {
   	firstname = firstname.charAt(0).toUpperCase() + firstname.substring(1,firstname.length).toLowerCase();
 } else {
   	incorrect[no] = "1";
  	 no++;
   	firstname = "";
  }
  
  regExp = /[A-Za-z]{2,}-?[A-Za-z]?/;
  if(regExp.test(lastname)) {
   	lastname = lastname.charAt(0).toUpperCase() + lastname.substring(1,lastname.length).toLowerCase();
  } else {
   	incorrect[no] = "2";
  	 no++;
   	lastname = "";
  }

  if(address.length < 3) {
   	incorrect[no] = "3";
   	no++;
   	street = "";
  }

    
  invalidChars = " /:,;"
			if (email == "") {
			 incorrect[no] = "4";
			 no++;
			 email = "";	
				}
			if (email != "") {
				for (i=0; i<invalidChars.length; i++) {
					badChar = invalidChars.charAt(i)
					if (email.indexOf(badChar,0) > -1) {
					incorrect[no] = "4";
					 no++;
					 email = "";
					}
				}
				atPos = email.indexOf("@",1)
				if (atPos == -1) {
					incorrect[no] = "4";
					 no++;
					 email = "";				}
				if (email.indexOf("@",atPos+1) > -1) {
					 incorrect[no] = "4";
					 no++;
					 email = "";
				}
				periodPos = email.indexOf(".",atPos)
				if (periodPos == -1) {
					 incorrect[no] = "4";
					 no++;
					 email = "";
				}
				if (periodPos+3 > email.length)	{
					incorrect[no] = "4";
					no++;
					email = "";
				}
			}
		
	 if(telephone.length < 3) {
   	incorrect[no] = "5";
   	no++;
    }
//regExp = /\(?\d{5}\)?\s?\d{6}/;
	regExp = /[0-9]/;
if(!regExp.test(telephone)) {
   	incorrect[no] = "5";
   	no++;
    }
	
  for(i=1;i<6;i++) {
  		document.getElementById(i).style.color="#000000";
  }

  for(j=0;j<no;j++) {
  		document.getElementById(incorrect[j]).style.color="#E01111";
  }

  if(no > 0) {
	  document.getElementById("Text"+incorrect[0]).focus()
	  document.getElementById("errors").innerHTML = "<span class=\"error\">There was an error with your form submission. Please correct fields highlighted in red.</span>";
return false
  }
  
  
//  document.getElementById("title").value = title;
  document.getElementById("Text1").value = firstname;
  document.getElementById("Text2").value = lastname;
  document.getElementById("Text3").value = address;
  //document.getElementById("postcode").value = postcode;
  document.getElementById("Text5").value = telephone;
  document.getElementById("Text4").value = email;
 // document.getElementById("departing").value = from;
//  document.getElementById("arriving").value = arriving;
//  document.getElementById("county").value = county;

if (location.href.indexOf("villa") !=-1)
{
var theform = document.form1
var inputdatea //car date 
inputdatea =  (theform.asDay.options[theform.asDay.selectedIndex].value + '/' +  theform.asMonth.options[theform.asMonth.selectedIndex].value + '/' +  theform.asYear.options[theform.asYear.selectedIndex].value)
returndatea = (theform.aeDay.options[theform.aeDay.selectedIndex].value + '/' +  theform.aeMonth.options[theform.aeMonth.selectedIndex].value + '/' +  theform.aeYear.options[theform.aeYear.selectedIndex].value)

if (isDate(inputdatea,'d/M/yyyy') == false) {
alert("Please enter a valid date")
theform.asDay.focus()
return false;
}
if (isDate(returndatea,'d/M/yyyy') == false) {
alert("Please enter a valid date")
theform.aeDay.focus()
return false;
}
} //end if 
}	//end function 


