/* 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 = "";
  }

 // if(from.length < 3) {
 //  	incorrect[no] = "6";
 //  	no++;
 //  	from = "";
 // }
 /// //if(arriving.length < 3) {
 //  	incorrect[no] = "7";
 //  	no++;
 //  	arriving = "";
 // }
  
 
  
  //  regExp = /\(?\d{5}\)?\s?\d{6}/;
//  if(regExp.test(telephone)) {
//   	if(telephone.indexOf("(") < 0) {
//  	  	telephone = "(" + telephone.substring(0,5) + ") " + telephone.substring(telephone.length-6,telephone.length);
//    } else if(telephone.indexOf(" ") < 0) {
//   	 	telephone = telephone.substring(0,7) + " " + telephone.substring(telephone.length-6,telephone.length);
//   	}
//  } else {
//    incorrect[no] = "5";
//    no++;
//    telephone = "";
//  }

//  if(regExp.test(email)) {
//   	if(email.indexOf("(") < 0) {
//  	  	email = "(" + email.substring(0,5) + ") " + email.substring(email.length-6,email.length);
//   	} else if(email.indexOf(" ") < 0) {
//   		email = email.substring(0,7) + " " + email.substring(email.length-6,email.length);
//  	 }
//  } else {
//    incorrect[no] = "4";
//    no++;
//    email = "";
//  }
  
  
  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
  }
  
  else
	{
	return checkdate()
	}


 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;


}


function checkdate()
{
var inputdate 
var theform = document.form1
inputdate =  (theform.sDay.options[theform.sDay.selectedIndex].value + '/' +  theform.sMonth.options[theform.sMonth.selectedIndex].value + '/' +  theform.sYear.options[theform.sYear.selectedIndex].value)
returndate = (theform.eDay.options[theform.eDay.selectedIndex].value + '/' +  theform.eMonth.options[theform.eMonth.selectedIndex].value + '/' +  theform.eYear.options[theform.eYear.selectedIndex].value)

if (isDate(inputdate,'d/M/yyyy') == false) {
alert("Please enter a valid date")
theform.sDay.focus()
return false;
}
if (isDate(returndate,'d/M/yyyy') == false) {
alert("Please enter a valid date")
theform.eDay.focus()
return false;
}

if (location.href.indexOf("travelplanner") !=-1)
{
var inputdatec //car date 
inputdatec =  (theform.csDay.options[theform.csDay.selectedIndex].value + '/' +  theform.csMonth.options[theform.csMonth.selectedIndex].value + '/' +  theform.csYear.options[theform.csYear.selectedIndex].value)
returndatec = (theform.ceDay.options[theform.ceDay.selectedIndex].value + '/' +  theform.ceMonth.options[theform.ceMonth.selectedIndex].value + '/' +  theform.ceYear.options[theform.ceYear.selectedIndex].value)

if (isDate(inputdatec,'d/M/yyyy') == false) {
alert("Please enter a valid date")
theform.csDay.focus()
return false;
}
if (isDate(returndatec,'d/M/yyyy') == false) {
alert("Please enter a valid date")
theform.ceDay.focus()
return false;
}

var inputdatea //accomodation 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 

