//
// $Id$
//
function checkBookingInfo(form) {
	var arrDate = form.arrDate.value;
	var arrMonth = form.arrMonth.value;
	var arrYear = form.arrYear.value;
	var depDate = form.depDate.value;
	var depMonth = form.depMonth.value;
	var depYear = form.depYear.value;
	var email = form.email.value;
	var no_rooms = form.no_rooms.value;
	var room_type = form.room_type.value;
	var timeDiff;
	var dayDif;

	// Optional fields
	var cname = escape(form.cname.value); 
	var address = escape(form.address.value); 
	var city = escape(form.city.value); 
	var postcode = escape(form.postcode.value); 
	var phone = escape(form.phone.value); 
	var fax = escape(form.fax.value); 
	var adults = escape(form.adults.value); 
	var children = escape(form.children.value); 
	var comment = escape(form.comment.value); 
	var property = form.property.value; 

	var adate = arrDate + "/" + arrMonth + "/" + arrYear;
	var ddate = depDate + "/" + depMonth + "/" + depYear;

	if ( adate == ddate) {
		msg = "Sorry, there is a problem with your dates.\nYour arrival and departure dates must be different.";
		alert(msg);
		return(false);
	}

	if (!isValidDate(arrDate, arrMonth, arrYear)) {
		alert("Your arrival date " + adate + " appears not to be valid. Please re-enter it.");
		return(false);
	}

	if (!isValidDate(depDate, depMonth, depYear)) {
		alert("Your departure date " + ddate + " appears not to be valid. Please re-enter it.");
		return(false);
	}

	today = new Date();
	arrivalDate = new Date(arrYear, (arrMonth - 1), arrDate);
	departureDate = new Date(depYear, (depMonth - 1), depDate);


	// Check if the date is in the past
	timeDiff = (arrivalDate.getTime() - today.getTime());
	dayDiff = milliSecondsToDays(timeDiff);
	
	if (dayDiff < 0) {
		alert("Sorry, your arrival date corresponds to a date in the past. Please re-enter your date.");
		return(false);
	}

	// Check if the departure date preceeds the arrival date
	timeDiff = (departureDate.getTime() - arrivalDate.getTime());
	dayDiff = milliSecondsToDays(timeDiff);
	
	if (dayDiff < 0) {
		alert("Sorry, your departure date preceeds your arrival date. Please re-enter your dates.");
		return(false);
	}

	if (!hasValue(email)) {
		alert("Please supply your email address.");
		form.email.focus();
		return(false);
	}

	if (!isEmail(email)) {
		alert("Your email address appears to be invalid, please re-enter it.");
		form.email.focus();
		return(false);
	}

	var qstr = "?email="+email;
	qstr +=	"&no_rooms="+no_rooms;
	qstr +=	"&room_type="+room_type;
	qstr += "&arrDate="+arrDate+"&arrMonth="+arrMonth+"&arrYear="+arrYear;
	qstr += "&depDate="+depDate+"&depMonth="+depMonth+"&depYear="+depYear;
	qstr += "&cname="+cname;
	qstr += "&address="+address;
	qstr += "&city="+city;
	qstr += "&postcode="+postcode;
	qstr += "&phone="+phone;
	qstr += "&fax="+fax;
	qstr += "&adults="+adults;
	qstr += "&children="+children;
	qstr += "&comment="+comment;
	qstr += "&property="+property;

	dest = "/booking.php"+qstr;
	win = window.open(dest, "Booking", "toolbar=no,scrollbars=yes,width=350,height=200,resizable")
}


var daysInMonth = new Array();
daysInMonth[1] = 31;
daysInMonth[2] = 29;
daysInMonth[3] = 31;
daysInMonth[4] = 30;
daysInMonth[5] = 31;
daysInMonth[6] = 30;
daysInMonth[7] = 31;
daysInMonth[8] = 31;
daysInMonth[9] = 30;
daysInMonth[10] = 31;
daysInMonth[11] = 30;
daysInMonth[12] = 31;


function trim(str) {
	return str.replace(/^\s+|\s+$/g, '')
};

function hasValue(str) {
	if (str == null) {
		return(false);
	}

	tstr = trim(str);
	return(tstr.length > 0);
}

function isNumeric(str) {
	var validChars = "0123456789.";
	var isNumber=true;
	var cchar;
 
	for (i=0; i<str.length; i++) { 
		cchar = str.charAt(i); 
		if (validChars.indexOf(cchar) == -1) {
			isNumber = false;
			break;
		}
	}

	return(isNumber);
}

function milliSecondsToDays(mSeconds) {
	return Math.ceil( mSeconds/(1000 * 60 * 60 * 24));
}

function isLeapYear(year) {
	// A year is a leap year if year % 4 == 0
	// BUT: this does not apply to years that fall on 100
	// unless the are a multiple of 400
	//
	if ( ((year%400) == 0) || (((year%4) == 0) && (year%100 != 0)) ) {
		return(true);
	}
	return(false);
}

function isValidDate(day, month, year) {
	if (month != 2) {
		return(day <= daysInMonth[month]);
	}

	if (isLeapYear(year)) {
		maxdays = 29;
	} else {
		maxdays = 28;
	}	

	return(day <= maxdays);
}



function vfy_contact(form) {
	var cname = form.cname.value;
	var email = form.email.value;
	var comment = form.comment.value;

	if (!hasValue(cname)) {
		alert("Please supply your name.");
		form.cname.focus();
		return(false);
	}

	if (!hasValue(email)) {
		alert("Please supply your email address.");
		form.email.focus();
		return(false);
	}

	if(!isEmail(email)) {
		warning = "Your email address appears to be invalid, please re-enter it.";
		alert(warning);
		form.email.focus();
		return(false);
	}
	
	if (!hasValue(comment)) {
		warning = "Please provide a comment.";
		alert(warning);
		form.comment.focus();
		return(false);
	}

	form.submit();
}

function isEmail(s) {
	var i = 1;
	var sLength = s.length;
	  
	// look for @
	while ((i < sLength) && (s.charAt(i) != "@")) { i++; }
	  
	if ((i >= sLength) || (s.charAt(i) != "@")) return false;
	else i += 2;
	  
	// look for .
	while ((i < sLength) && (s.charAt(i) != ".")) { i++; }
	  
	// there must be at least one character after the .
	if ((i >= sLength - 1) || (s.charAt(i) != ".")) return false;
	else return true;
}

function qsearch(form) {
	var keywords = form.keywords.value;
	//var range_idx = form.range.selectedIndex
	var area_idx = form.area.selectedIndex
	var acctype_idx = form.acctype.selectedIndex
	var lettype_idx = form.lettype.selectedIndex

	if ( ((keywords == null) || (keywords.length == 0)) && (area_idx == 0) && (acctype_idx == 0) && (lettype_idx == 0) ) {
		alert("Please select a property type, area or search term");
		return(false);
	}

	form.submit();
}

function propertyCloseup(name, image) {
	target =  "closeup.php?name="+name+"&image="+image;
	win = window.open(target, "Closeup", "toolbar=no,scrollbars=yes,width=400,height=350,resizable")
}

function book(form) {
	var cname = form.elements["cname"].value;
	var email = form.email.value;

	if (!hasValue(cname)) {
		alert("Please provide your contact name");
		return(false);
	}

	if (!hasValue(email)) {
		alert("Please provide your contact email address");
		form.email.focus();
		return(false);
	}

	if(! isEmail(email)) {
		alert("Your email address appears to be invalid, please re-enter it.");
		form.email.focus();
		return(false);
	}

	form.submit();
}

function virtualTour(link) {
	win = window.open(link, "Tour", "toolbar=no,scrollbars=no,width=400,height=355,resizable=0");
}

function sendVoucherForm(form) {
	var cname = form.cname.value;
	var phone = form.phone.value;
	var email = form.email.value;
	var vname = form.vname.value;
	var vvalue = form.vvalue.value;


	if (!hasValue(cname)) {
		alert("Please enter your name.");
		form.cname.focus();
		return(false);
	}

	if (!hasValue(email)) {
		alert("Please enter your email address.");
		form.email.focus();
		return(false);
	}

	if (!isEmail(email)) {
		alert("Your email address appears to be invalid, please re-enter it.");
		form.email.focus();
		return(false);
	}

	if (!hasValue(vname)) {
		alert("Please enter the name you wish to appear on your voucher.");
		form.vname.focus();
		return(false);
	}

	if (!hasValue(vvalue)) {
		alert("Please enter the value you require for your voucher.");
		form.vname.focus();
		return(false);
	}


	var cname_enc = escape(cname);
	var phone_enc = escape(phone);
	var email_enc = escape(email);
	var vname = escape(vname);
	var vvalue = escape(vvalue);

	form.submit();
}