var W3CDOM=(document.getElementsByTagName && document.createElement);

window.onload=function() {
document.forms[0].onsubmit=function() {
  return validate();
 } 
}

function validate(){

 isValid=true;
 firstError=null;
 errorString=""; //for non-W3C DOM browsers
 
 var x=document.forms[0];
 
   //Check first name field is not empty
 
 if (!x.fname.value) {
	 writeError(x.fname,"This field is required.");
 }
 
   //Check family name field is not empty
 
 if (!x.famname.value) {
	 writeError(x.famname,"This field is required.");
 }
 
   //Check email field is not empty
 
 if (!x.email.value) {
	 writeError(x.email,"This field is required.");
 }else{
 
 //Check email is valid
  var re=/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,4})+$/;
  if (!re.test(x.email.value)){
   writeError(x.email,"This does not appear to be a valid email address.");
  }
 }
 
  //Check phone is valid if given
 
 var re=/^([\+|00])?[\s|-]?(\d+[\s|-]?)+$/;
 if (x.phone.value && !re.test(x.phone.value)){
  writeError(x.phone,"This does not appear to be a valid phone number.");
 } else {
  //Rewrite phone number in standard format (no spaces)
	x.phone.value=x.phone.value.replace(/[\s|-]/g,"");
 }
 
 //Check date exists
 if (!isDate(x.arryear.value,x.arrmonth.value,x.arrday.value)) {
  writeError(x.arrday,"This is not a valid date.");
 }
 
 //Check date is in the future
 
 var arrDate=new Date(x.arryear.value,(x.arrmonth.value-1),x.arrday.value);
 var now=new Date();
 
 if (arrDate.getTime()<now.getTime()) {
  writeError(x.arrday,"This date has passed.");
 }
 
 //Check room type selected
 if(x.roomtype.value=="none"){
  writeError(x.roomtype,"This field is required.");
 }
 
 //Check no.nights field is not empty
 
 if (!x.nonights.value) {
	 writeError(x.nonights,"This field is required.");
 }

 if (!W3CDOM) {
  alert(errorString);//for non-W3C browsers
 }
 if (firstError) {
  firstError.focus();
 }
 	
 return isValid;

}

function writeError(obj,message) {
 
 isValid=false;
 
 if (obj.hasError) {
  return;
 }
 if (W3CDOM) {
  
  obj.className+=" errormsg";
	obj.onchange=removeError;
	var span=document.createElement("span");
	span.className="errormsg";
	span.appendChild(document.createTextNode(message));
	
	//put error message in right place - within label tag
	var labels=document.getElementsByTagName("label");
	objID=obj.getAttribute("id");
	labelID=objID+"label";
	labels.namedItem(labelID).appendChild(span);
	obj.hasError=span;
 }else{
 errorString+=obj.name+": "+message+"\n";
 obj.hasError=true;
 }
 
 if(!firstError){
  firstError=obj;
 }
}

function removeError(){
 
 this.className=this.className.substring(0,this.className.lastIndexOf(" ")); //removes error class from form field
 var labels=document.getElementsByTagName("label");
 objID=this.getAttribute("id");
 labelID=objID+"label";
 
 labels.namedItem(labelID).removeChild(this.hasError); //removes the error message;
 this.hasError=null;
 this.onchange=null;
 
}

function isDate(inYear,inMonth,inDay) {

 var monthLength=new Array(31,28,31,30,31,30,31,31,30,31,30,31);
 isValidDate=true;
 
 year=parseInt(inYear);
 month=parseInt(inMonth);
 day=parseInt(inDay);
 
 //Check if leap year
 if((year%4)==0) {
	monthLength[1]=29;
 }
	
 //Check if entered day does not exist for entered month
 if (day>monthLength[month-1]) {
	isValidDate=false;
 } 
 
 return isValidDate;
}
