/* 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("Text3").value;
//  var telephone = document.getElementById("Text4").value;
//  var eveningTel = 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 = "";
  }
  
  
  invalidChars = " /:,;"
			if (email == "") {
			 incorrect[no] = "3";
			 no++;
			 email = "";	
				}
			if (email != "") {
				for (i=0; i<invalidChars.length; i++) {
					badChar = invalidChars.charAt(i)
					if (email.indexOf(badChar,0) > -1) {
					incorrect[no] = "3";
					 no++;
					 email = "";
					}
				}
				atPos = email.indexOf("@",1)
				if (atPos == -1) {
					incorrect[no] = "3";
					 no++;
					 email = "";				}
				if (email.indexOf("@",atPos+1) > -1) {
					 incorrect[no] = "3";
					 no++;
					 email = "";
				}
				periodPos = email.indexOf(".",atPos)
				if (periodPos == -1) {
					 incorrect[no] = "3";
					 no++;
					 email = "";
				}
				if (periodPos+3 > email.length)	{
					incorrect[no] = "3";
					no++;
					email = "";
				}
			}
			  
	
  for(i=1;i<4;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.<br/><br/></span>";
return false
  }
  


 document.getElementById("Text1").value = firstname;
  document.getElementById("Text2").value = lastname;
  document.getElementById("Text3").value = email;


}

