/* jQuery code for contact us form    It uses the .ajax function to asynchronously send the quicknote message   and the blockUI.js to disable the form while sending the message.   It first checks the form for errors and puts up the appropriate message   using the *//*** general purpose functions used in the script ***/// number formatting function// copyright Stephen Chapman 24th March 2006, 22nd August 2008// permission to use this function is granted provided// that this copyright notice is retained intactfunction formatNumber(num,dec,thou,pnt,curr1,curr2,n1,n2) {	var x = Math.round(num * Math.pow(10,dec));		if (x >= 0) n1=n2='';		var y = (''+Math.abs(x)).split('');	var z = y.length - dec; 		if (z<0) z--; 		for(var i = z; i < 0; i++) 		y.unshift('0'); 		if (z<0) 		z = 1; 		y.splice(z, 0, pnt); 		if(y[0] == pnt) 		y.unshift('0'); 	while (z > 3) {		z-=3; 		y.splice(z,0,thou);	}			var r = curr1+n1+y.join('')+n2+curr2;		return r;}/* This function checks if the input string is empty. */function isEmpty(inputStr){  if(inputStr == null || inputStr == "")     return true;  return false;}/* This function checks for valid email addresses. */function isEmailAddr(inputStr) {  var hasAtSign = false;  var hasDotRightOfAt = false;    for(var i = 0; i < inputStr.length; i++) {     if(i > 0 && inputStr.charAt(i) == "@") {	    hasAtSign = true;		continue;	 }	 if(inputStr.charAt(i) == "." && hasAtSign) 	    hasDotRightOfAt = true;  }  if(hasAtSign && hasDotRightOfAt)     return true;	   return false;}/* first check that JavaScript is enabled */if(document.getElementById && document.createTextNode) { 	/* run jQuery if JavaScript is enabled */	$(document).ready(function()  {			/* initialize dates */		var todaysDate = new Date();  //today's date		var firstDate = new Date(); //initialize		firstDate.setDate(todaysDate.getDate() + 2); //set to 2 days later		var lastDate = new Date(); //initialize		lastDate.setDate(todaysDate.getDate() + 365*2); //set to two years later		/* set date strings */		var mth = "";		var origMth = todaysDate.getMonth() + 1;		if(origMth < 10) { mth = "0" + origMth; } else { mth = "" + origMth; }		var dte = "";		var origDate = todaysDate.getDate();		if(origDate < 10) { dte = "0" + origDate; } else { dte = "" + origDate; }		var td = todaysDate.getFullYear() + "-" + mth + "-" + dte; //the today's date string		origMth = firstDate.getMonth() + 1;		if(origMth < 10) { mth = "0" + origMth; } else { mth = "" + origMth; }		origDate = firstDate.getDate();		if(origDate < 10) { dte = "0" + origDate; } else { dte = "" + origDate; }		var nwd = firstDate.getFullYear() + "-" + mth + "-" + dte; //the next week's date string		/* set date on form */		$("#Arrive").val(td);		$("#Depart").val(nwd);		/* call date picker for 'Arrival Date' on the form */		$("#Arrive").datepicker({minDate: todaysDate,maxDate: lastDate});		$("#Arrive").datepicker('option', 'dateFormat', 'yy-mm-dd');		/* call date picker for 'Departure Date' on the form */		$("#Depart").datepicker({minDate: firstDate,maxDate: lastDate});		$("#Depart").datepicker('option', 'dateFormat', 'yy-mm-dd');		/* setup .ajax */		$.ajaxSetup({			type: "POST",			dataType: "html"		});		/* hide the errMsg <p> tag */		$("#errMsgRsv").hide();		/* process the change event for the arrival date */		$("#Arrive").change(function(){			var arrival = $("#Arrive").val();    //initialize						//var firstDate = new Date();          //initialize						//firstDate.setDate(arrivalDate.getDate() + 3); //set to 3 days later			/* validate arrival date */			if(isEmpty(arrival)) {				$("#lblArrive").css("color","#f00"); //change the text colour				$("#Arrive").css("color","#f00");    //change the text colour			}			else {				if(!isDate(arrival,"yyyy-MM-dd")) {					$("#lblArrive").css("color","#f00"); //change the text colour					$("#Arrive").css("color","#f00");    //change the text colour				}				else { //valid arrival date so update dates, etc.					var departureDate = new Date(parseInt(arrival.substr(0,4)),parseInt(arrival.substr(5,2)) == 0?parseInt(arrival.substr(6,1))-1:parseInt(arrival.substr(5,2))-1,parseInt(arrival.substr(8,2)) == 0?parseInt(arrival.substr(9,1)):parseInt(arrival.substr(8,2)));      					departureDate.setDate(departureDate.getDate() + 2);					/* setup departure date format */					var mth = "";					var dte = "";					var origMth = departureDate.getMonth() + 1;					if(origMth < 10) { mth = "0" + origMth; } else { mth = "" + origMth; }					var origDate = departureDate.getDate();					if(origDate < 10) { dte = "0" + origDate; } else { dte = "" + origDate; }					var nwd = departureDate.getFullYear() + "-" + mth + "-" + dte; //the departure date date string					$("#Depart").val(nwd);					$("#Depart").datepicker('option', 'minDate', departureDate);					/* reset duration */					$("#Duration").val("2");					//reset cost					if($("#Cottage").val() == "2") { //double bedroom						$("#Cost").val("US$671.00");					}					else { //single bedroom						$("#Cost").val("US$414.80");					}				}			}		});				/* process the change event for the departure date */		$("#Depart").change(function(){			var arrival = $("#Arrive").val();    //initialize			var departure = $("#Depart").val();    //initialize			/* validate departure date */			if(isEmpty(departure)) {				$("#lblDepart").css("color","#f00"); //change the text colour				$("#Depart").css("color","#f00");    //change the text colour			}			else {				if(!isDate(departure,"yyyy-MM-dd")) {					$("#lblDepart").css("color","#f00"); //change the text colour					$("#Depart").css("color","#f00");    //change the text colour				}				else { //valid arrival date so update departure, etc.					var dur = "0";					var costDollars = "US$0.00";					var cost = 0.0;					var costSingle = 207.4;					var costDouble = 335.5;					var validDates = compareDates(departure,"yyyy-MM-dd",arrival,"yyyy-MM-dd");	  					if(validDates != 1) { //the end date is NOT after the start date						$("#lblDepart").css("color","#f00"); //change the text colour						$("#Depart").css("color","#f00");    //change the text colour					}					else { /* recalculate duration and cost */						$("#lblDepart").css("color","#0036ab"); //change the text colour						$("#Depart").css("color","#0036ab");    //change the text colour						$("#lblArrive").css("color","#0036ab"); //change the text colour						$("#Arrive").css("color","#0036ab");    //change the text colour						/* recalculate duration */						var curDate=new Date();						curDate.DateDiff({interval:"d",date1:arrival,date2:departure});						dur = curDate.difference;						$("#Duration").val(dur);						/* recalculate cost */						if($("#Cottage").val() == "2") { //double bedroom							cost = costDouble * parseInt(dur);							costDollars = formatNumber(cost,2,',','.','US$','','-','');							$("#Cost").val(costDollars);						}						else { //single bedroom							cost = costSingle * parseInt(dur);							costDollars = formatNumber(cost,2,',','.','US$','','-','');							$("#Cost").val(costDollars);						}					}				}			}		});		/* process the change event for the departure date */		$("#Cottage").change(function(){					var costDollars = "US$0.00";			var cost = 0.0;			var costSingle = 207.4;			var costDouble = 335.5;			/* recalculate cost */			if($("#Cottage").val() == "2") { //double bedroom				cost = costDouble * parseInt($("#Duration").val());				costDollars = formatNumber(cost,2,',','.','US$','','-','');				$("#Cost").val(costDollars);			}			else { //single bedroom				cost = costSingle * parseInt($("#Duration").val());				costDollars = formatNumber(cost,2,',','.','US$','','-','');				$("#Cost").val(costDollars);			}		});		/* setup the form click action */		$("#btnSubmit").click(function() {			/* reset colour and background for labels and message */			$("#reservation label, #reservation input").each(function() {				$(this).css("color","#0036ab");			});			$("#errMsgRsv").addClass("errMsgRsv");			$("#errMsgRsv").css("color","#f00");						/* setup variables for form inputs */			var fullname = $("#FullName").val();			var email = $("#Email").val();			var msg = $("#Note").val();			var arrival = $("#Arrive").val();			var departure = $("#Depart").val();			var duration = $("#Duration").val();			var cottage = $("#Cottage").val();			var cost = $("#Cost").val();			var frmErrors = false;  //flag to indicate if there are errors on the form			var validDates = compareDates(departure,"yyyy-MM-dd",arrival,"yyyy-MM-dd");	  			/* validate the form inputs */			if(isEmpty(fullname)) {				$("#lblFullName").css("color","#f00"); //change the text colour				$("#FullName").css("color","#f00");    //change the text colour				frmErrors = true;			}			if(isEmpty(email)) {				$("#lblEmail").css("color","#f00"); //change the text colour				$("#Email").css("color","#f00");    //change the text colour				frmErrors = true;			}			else {   //field not empty so check email address				if(!isEmailAddr(email)) {					$("#lblEmail").css("color","#f00");    //change the text colour					$("#Email").css("color","#f00");    //change the text colour					frmErrors = true;				}			}			if(isEmpty(arrival)) {				$("#lblArrive").css("color","#f00"); //change the text colour				$("#Arrive").css("color","#f00");    //change the text colour				frmErrors = true;			}			if(isEmpty(departure)) {				$("#lblDepart").css("color","#f00"); //change the text colour				$("#Depart").css("color","#f00");    //change the text colour				frmErrors = true;			}			if(validDates != 1) { //the end date is NOT after the start date				$("#lblDepart").css("color","#f00"); //change the text colour				$("#Depart").css("color","#f00");    //change the text colour				$("#lblArrive").css("color","#f00"); //change the text colour				$("#Arrive").css("color","#f00");    //change the text colour				frmErrors = true;			}			/* inform user of form errors if any */			if(frmErrors) {				$("#errMsgRsv").text("There are errors on the form! Please correct them.");				$("#errMsgRsv").slideDown("slow");				$("#errMsgRsv").vkfade();			}			else {  /* no errors so  block the form, put up message,and post data using .ajax */				$("#myrsv").block();				$("#errMsgRsv").css("color","#0036ab");				$("#errMsgRsv").text("Sending your Reservation... Please wait...");				$("#errMsgRsv").slideDown("slow");				$("#errMsgRsv").vkfade();				$.ajax({					url: "reserve_a.php",					data: "FullName=" + fullname + "&Email=" + email + "&Arrive=" + arrival + "&Depart=" + departure + "&Duration=" + duration + "&Cottage=" + cottage + "&Cost=" + cost + "&Note=" + msg,					success: function(responseText) {						/* clear the text boxes */						$("#FullName").val("");						$("#Email").val("");						$("#Duration").val("2");						$("#Cost").val("US$414.80");						$("#Cottage").val("1");						$("#Note").val("");						/* reset date text boxes */						var todaysDate = new Date();  //today's date						var firstDate = new Date(); //initialize						firstDate.setDate(todaysDate.getDate() + 2); //set to 3 days later						var lastDate = new Date(); //initialize						lastDate.setDate(todaysDate.getDate() + 365*2); //set to two years later						/* set date strings */						var mth = "";						var origMth = todaysDate.getMonth() + 1;						if(origMth < 10) { mth = "0" + origMth; } else { mth = "" + origMth; }						var dte = "";						var origDate = todaysDate.getDate();						if(origDate < 10) { dte = "0" + origDate; } else { dte = "" + origDate; }						var td = todaysDate.getFullYear() + "-" + mth + "-" + dte; //the today's date string						origMth = firstDate.getMonth() + 1;						if(origMth < 10) { mth = "0" + origMth; } else { mth = "" + origMth; }						origDate = firstDate.getDate();						if(origDate < 10) { dte = "0" + origDate; } else { dte = "" + origDate; }						var nwd = firstDate.getFullYear() + "-" + mth + "-" + dte; //the next week's date string						/* set date on form */						$("#Arrive").val(td);						$("#Depart").val(nwd);						/* call date picker for 'Departure Date' on the form */						$("#Depart").datepicker('option', 'minDate', firstDate);						/* unblock then put up message */						$("#myrsv").unblock();						$("#errMsgRsv").html(responseText);						$("#errMsgRsv").slideDown("slow");						$("#errMsgRsv").vkfade();					},					error: function() {						$("#myrsv").unblock();						$("#errMsgRsv").css("color","#f00");						$("#errMsgRsv").text("Error sending your Reservation. Please retry.");						$("#errMsgRsv").slideDown("slow");						$("#errMsgRsv").vkfade();					}				});			}						return false; // cancel the default form click action		});		/* setup the form reset action */		$("#btnReset").click(function() {			$("#errMsgRsv").slideUp("slow");  //hide the error message			/* reset colour and background for labels */			$("#reservation label, #reservation input").each(function() {				$(this).css("color","#0036ab");			});			/* clear the text boxes */			$("#FullName").val("");			$("#Email").val("");			$("#Duration").val("2");			$("#Cost").val("US$414.80");			$("#Cottage").val("1");			$("#Note").val("");			/* reset date text boxes */			var todaysDate = new Date();  //today's date			var firstDate = new Date(); //initialize			firstDate.setDate(todaysDate.getDate() + 2); //set to 3 days later			var lastDate = new Date(); //initialize			lastDate.setDate(todaysDate.getDate() + 365*2); //set to two years later			/* set date strings */			var mth = "";			var origMth = todaysDate.getMonth() + 1;			if(origMth < 10) { mth = "0" + origMth; } else { mth = "" + origMth; }			var dte = "";			var origDate = todaysDate.getDate();			if(origDate < 10) { dte = "0" + origDate; } else { dte = "" + origDate; }			var td = todaysDate.getFullYear() + "-" + mth + "-" + dte; //the today's date string			origMth = firstDate.getMonth() + 1;			if(origMth < 10) { mth = "0" + origMth; } else { mth = "" + origMth; }			origDate = firstDate.getDate();			if(origDate < 10) { dte = "0" + origDate; } else { dte = "" + origDate; }			var nwd = firstDate.getFullYear() + "-" + mth + "-" + dte; //the next week's date string			/* set date on form */			$("#Arrive").val(td);			$("#Depart").val(nwd);			/* call date picker for 'Departure Date' on the form */			$("#Depart").datepicker('option', 'minDate', firstDate);						return false; // cancel the default form click action		});	 });}