// JavaScript Document

//date validation - eror messages

	var ErrDate1="Please enter Arrival / Departure Dates."
	var ErrDate2="Please enter a valid Arrival Date."
	var ErrDate3="The Date entered is in the past. Please enter a valid Arrival Date."
	var ErrDate3a="I am afraid a mimimum of 24 hours is required to process your request."
	var ErrDate4="Please enter a valid Departure Date."
	var ErrDate5="Your period of stay appears to be incorrect as it is longer than a year."
	var ErrDate6="The Departure Date should be later than the Arrival Date."
	var ErrDate7="The villa rental only starts as off 1st of March 2006."
	
function validDate(year, month, day) {

	var tempDate = new Date(year, month, day)
	return tempDate.getDate() == day
}


//Cehck the dates before submitting the form

function checkDate(form) {

	//Dates entered?
	if (cInDay.selectedIndex == 0 || cInMonth.selectedIndex == 0 || cInYear.selectedIndex == 0 || cOutDay.selectedIndex == 0 || cOutMonth.selectedIndex == 0 || cOutYear.selectedIndex == 0) {
		alert(ErrDate1)
		cInDay.focus()
		return	false
	}
	//Rental start per 1 Apr 2006, check if date is not before that?
	if (cInMonth.selectedIndex < 3 && cInYear[cInYear.selectedIndex].text == "2006"){
		alert(ErrDate7)
		cInDay.focus()
		return false
	}
	//Arrival date valid?
	if (!validDate(cInYear[cInYear.selectedIndex].text, cInMonth.selectedIndex - 1, cInDay.	selectedIndex)) {
		alert(ErrDate2)
		cInDay.focus()
		return	false
	}
	var todayDate = new Date()
	var checkinDate= new Date(cInYear[cInYear.selectedIndex].text, cInMonth.selectedIndex - 1, cInDay.selectedIndex, 0,1,0)
	todayDate.setHours(0)
	todayDate.setMinutes(0)
	// Date is in the past?
	if (checkinDate < todayDate) {
		alert(ErrDate3)
		cInDay.focus()
		return	false
	}
	// Today?
	checkinDate.setMinutes(0)
	if (checkinDate < todayDate) {
		alert(ErrDate3a)
		cInDay.focus()
		return	false}
	//Departure Date Valid?
	if (!validDate(cOutYear[cOutYear.selectedIndex].text, cOutMonth.selectedIndex - 1, cOutDay.selectedIndex)) {
		alert(ErrDate4)
		cOutDay.focus()
		return	false
	}
	// Departure - Arrival >1 year?
	var msDifference = Date.UTC(cOutYear[cOutYear.selectedIndex].text, cOutMonth.selectedIndex - 1, cOutDay.selectedIndex) - Date.UTC(cInYear[cInYear.selectedIndex].text, cInMonth.selectedIndex - 1, cInDay.selectedIndex)
	if (msDifference > 31536000000) {  //1 year in milliseconds
		alert(ErrDate5)
		cOutDay.focus()
		return	false
	}
	// Departure < Arrival?
	if (msDifference <= 0) {
		alert(ErrDate6)
		cOutDay.focus()
		return	false
	}
//	after all these checks at least the dates are ok
	return true
}

//set checkout date to checkin date + 1 day

function setDate() {

	var checkoutDate = new Date(cInYear[cInYear.selectedIndex].text, cInMonth.selectedIndex - 1, 		cInDay.selectedIndex + 7)
	var newOutDay = checkoutDate.getDate()
	var newOutMonth = checkoutDate.getMonth()
	var newOutYear = checkoutDate.getYear().toString()

	if (cInDay.selectedIndex !=0) cOutDay.selectedIndex = newOutDay
	if (cInMonth.selectedIndex != 0) cOutMonth.selectedIndex = newOutMonth + 1
	for (var i=1; i<4; i++) {
		if (cOutYear[i].text.charAt(3) == newOutYear.charAt(newOutYear.length - 1)) cOutYear.selectedIndex = i
	}
}

<!-- Error Messages

var FErr1 = "Email Address seems Incorrect, please check if '@' or '.' are missing. Bv. info@villa-branie.nl";
var FErr2 = "Email: You may have made a typo error, the username doesn't seem to be valid. Bv. info@villa-branie.nl";
var FErr3 = "Email: You may have made a typo error, destination IP address is invalid! Bv. info@villa-branie.nl";
var FErr4 = "Email: You may have made a typo error, the domain name doesn't seem to be valid. Bv. info@villa-branie.nl";
var FErr5 = "Email: You may have made a typo error, the email must end in a three-letter domain,\n or two letter country. Bv. info@villa-branie.nl OR villa-branie@hotmail.com";
var FErr6 = "Email: You may have made a typo error, the email is missing a hostname. Bv. info@villa-branie.nl";
var FErr7 = "The villa has a maximum capacity of 8 persons."
var FErr8 = "Please enter a valid Telephone number.\nWe will only call you if we were unable to reach you by Email or Fax.";
var FErr10 = " field is required. Please select or fill in completely.";

// Validate Email

function emailCheck (emailS) {

	var emailStr=emailS.value;
	var emailPat=/^(.+)@(.+)$/
	var specialChars="\\(\\)<>@,;:\\\\\\\"\\.\\[\\]"
	var validChars="\[^\\s" + specialChars + "\]"
	var quotedUser="(\"[^\"]*\")"
	var ipDomainPat=/^\[(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})\]$/
	var atom=validChars + '+'
	var word="(" + atom + "|" + quotedUser + ")"
	var userPat=new RegExp("^" + word + "(\\." + word + ")*$")
	var domainPat=new RegExp("^" + atom + "(\\." + atom +")*$")
	var matchArray=emailStr.match(emailPat)

	if (matchArray==null) {
		alert(FErr1)
		emailS.focus();
		emailS.select();
		return false
	}

	var user=matchArray[1]
	var domain=matchArray[2]

	if (user.match(userPat)==null) {
		alert(FErr2)
		emailS.focus();
		emailS.select();
		return false
	}

	var IPArray=domain.match(ipDomainPat)

	if (IPArray!=null) {
 		for (var i=1;i<=4;i++) {
			if (IPArray[i]>255) {
				alert(FErr3)
				emailS.focus();
				emailS.select();
				return false
			}
		}
    return true
	}

	var domainArray=domain.match(domainPat)

	if (domainArray==null) {
		alert(FErr4)
		emailS.focus();
		emailS.select();
		return false
	}

	var atomPat=new RegExp(atom,"g")
	var domArr=domain.match(atomPat)
	var len=domArr.length

	if (domArr[domArr.length-1].length<2 || domArr[domArr.length-1].length>3) {
	   	alert(FErr5)
	   	emailS.focus();
		emailS.select();
		return false
	}

	if (len<2) {
	   	alert(FErr6)
	   	emailS.focus();
		emailS.select();
		return false
	}

	return true;

}



// Validate Telephone Number

function validatephone(field) {

	var valid = "0123456789()-+ "
	var ok = "yes";
	var temp;
	var i=0;
	for (; i<field.value.length; i++) {
		temp = "" + field.value.substring(i, i+1);
		if (valid.indexOf(temp) == "-1") ok = "no";
	}
	if (ok == "no" || i < 5) {
		alert(FErr8);
		field.focus();
		field.select();
		return false;
   }

   return true;

}

// Check rq

function checkrq(which) {

	var pass=true;
	var guests=cAdults.selectedIndex + cChildren.selectedIndex + 1;
	
	if (!checkDate(this)) return false;
	if (document.images) {
		for (i=0;i<which.length;i++) {
			var tempobj=which.elements[i];
			if (tempobj.name=="rqTel") {
				if (validatephone(tempobj)==false) return false;
			}
			if (tempobj.name=="rqEmail") {
				if (emailCheck(tempobj)==false) return false;
			}
			if (guests > 8){
				alert(FErr7);
				cAdults.focus;
				return false;
			}
			if (tempobj.name.substring(0,2)=="rq") {
				if (((tempobj.type=="text" || tempobj.type=="textarea")&&
					tempobj.value=='')||(tempobj.type.toString().charAt(0)=="s"&&
					tempobj.selectedIndex==0)) {
						pass=false;
						break;
				}
			}
	   }
	}
	if (!pass) {
		shortFieldName=tempobj.name.substring(2,22);
		alert(shortFieldName+FErr10);
		tempobj.focus();
		return false;
	}
}
function checkcontactrq(which) {

	var pass=true;
	
	if (document.images) {
		for (i=0;i<which.length;i++) {
			var tempobj=which.elements[i];
			if (tempobj.name=="rqTel") {
				if (validatephone(tempobj)==false) return false;
			}
			if (tempobj.name=="rqEmail") {
				if (emailCheck(tempobj)==false) return false;
			}

			if (tempobj.name.substring(0,2)=="rq") {
				if (((tempobj.type=="text" || tempobj.type=="textarea")&&
					tempobj.value=='')||(tempobj.type.toString().charAt(0)=="s"&&
					tempobj.selectedIndex==0)) {
						pass=false;
						break;
				}
			}
	   }
	}
	if (!pass) {
		shortFieldName=tempobj.name.substring(2,22);
		alert(shortFieldName+FErr10);
		tempobj.focus();
		return false;
	}
}