var calendarWidth = 245;
var startDate;
var stopDate;
var numberGuestsChosen;
var numberRoomsChosen;
var numberBedsChosen;

var strMsgFirst = "<TABLE WIDTH='795' BORDER=0 CELLSPACING=0 CELLPADDING=0><TR><TD CLASS='messageToUser'><h2>To book online click '<b>arr</b>' below your day of Arrival.</h2></TD></TR><TR><TD CLASS='messageToUser'><b>Payment Options:</b><BR><b>Credit Card Online</b> - Secure Transactions via the Commonwealth Bank<BR><b>Call Centre</b> - Offline Transaction via Telephone</TD></TR></TABLE>";

var monthNamesArray = new Array();
var monthsArray = new Array();
var currentLeftMonth = 0;
var cellId = 0;
var cellIdStart;
var cellIdStop;

var leftMonth = ""
var rightMonth = ""

var start_flag = 0; 
var stop_flag = 0;

var systemDate = new Date(); //might be the same as hereNow 
var hereNow = new Date();
var thisMonth = hereNow.getMonth();
var thisYear = hereNow.getFullYear();
var bookedDates = new Array();
var bookedDates_counter = 0;

var NS6 = (document.getElementById && !document.all);
var NS4 = (document.layers);
var IE = (document.all);
	
months = ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"];
daysInMonths = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31];

function write_first_calendar2(){
		alert('The date on your pc appears to be wrong.');
}
function write_first_calendar(){

	if(checkPcDateVServerDate()){
		alert('The date on your pc appears to be wrong. \n Please reset the date and begin again');
		return;
	}
	zIIz();
	zIz();
		document.getElementById("monthLeftName").innerHTML = monthNamesArray[0];	
		document.getElementById("monthRightName").innerHTML = monthNamesArray[1];	
		document.getElementById("left").innerHTML = monthsArray[0];	
		document.getElementById("right").innerHTML = monthsArray[1];
}

function zIz(){ 
	for(monthSpan=0;monthSpan<monthsToShow;monthSpan++){
		writeSingleMonth(monthSpan);
	}
}

function writeSingleMonth(next){//writes the month name into one array, and calendar into another
	if(IE){
			monthNamesArray[next] = months[hereNow.getMonth()] +"&nbsp;&nbsp;"+ hereNow.getYear().toString();
	}else{
		var mnthName = hereNow.getYear() + 1900;
		monthNamesArray[next] = months[hereNow.getMonth()] +"&nbsp;&nbsp;"+ mnthName.toString();
	}
	strSingleMonth = writeTableTop();
	strSingleMonth += writeTableContent();
	monthsArray[next] = strSingleMonth;
}//end of writeSingleMonth

function writeInnerHTML(){
		document.getElementById("monthLeftName").innerHTML = monthNamesArray[currentLeftMonth];	
		document.getElementById("monthRightName").innerHTML = monthNamesArray[(currentLeftMonth + 1)];	
		document.getElementById("left").innerHTML = monthsArray[currentLeftMonth];
		document.getElementById("right").innerHTML = monthsArray[(currentLeftMonth + 1)] 
}

function zIIz(){ 
var currentMonth = new Date();
if(currentMonth.getDate() == 1){return;} 
var dateStr = 1 + ' ' + months[currentMonth.getMonth()] + ' ' + currentMonth.getFullYear();
	fromTo[fromTo.length] = new Date(dateStr);
	dateStr = (currentMonth.getDate() -1) + ' ' + months[currentMonth.getMonth()] + ' ' + currentMonth.getFullYear();
	fromTo[fromTo.length] = new Date(dateStr);
}

function writeTableTop (){
	var top = "<TABLE WIDTH='"+calendarWidth+"px' BORDER=0 CELLSPACING=0 CELLPADDING=0><TR><TD CLASS='tblHeader' WIDTH='14%' COLSPAN=2>Sun</TD><TD>&nbsp;</TD><TD CLASS='tblHeader' WIDTH='14%' COLSPAN=2>Mon</TD><TD>&nbsp;</TD><TD CLASS='tblHeader' WIDTH='14%' COLSPAN=2>Tue</TD><TD>&nbsp;</TD><TD CLASS='tblHeader' WIDTH='14%' COLSPAN=2>Wed</TD><TD>&nbsp;</TD><TD CLASS='tblHeader' WIDTH='14%' COLSPAN=2>Thu</TD><TD>&nbsp;</TD><TD CLASS='tblHeader' WIDTH='14%' COLSPAN=2>Fri</TD><TD>&nbsp;</TD><TD CLASS='tblHeader' WIDTH='14%' COLSPAN=2>Sat</TD><TD>&nbsp;</TD></TR><TR><TD></TD></TR>";
//	var top = "<TABLE WIDTH='"+calendarWidth+"px' BORDER=1 CELLSPACING=0 CELLPADDING=0><TR><TD CLASS='tblHeader' WIDTH='14%' COLSPAN=2>Sun</TD><TD CLASS='tblHeader' WIDTH='14%' COLSPAN=2>Mon</TD><TD CLASS='tblHeader' WIDTH='14%' COLSPAN=2>Tue</TD><TD CLASS='tblHeader' WIDTH='14%' COLSPAN=2>Wed</TD><TD CLASS='tblHeader' WIDTH='14%' COLSPAN=2>Thu</TD><TD CLASS='tblHeader' WIDTH='14%' COLSPAN=2>Fri</TD><TD CLASS='tblHeader' WIDTH='14%' COLSPAN=2>Sat</TD></TR>";
	 return top;
}

// Adds to a date.
/*function dateadd(per,n,d) {
   switch(per) {
      case "yyyy": d.setYear(d.getFullYear()+n); break
      case "m": d.setMonth(d.getMonth()+n); break
      case "ww": n*=7
      case "d": d.setDate(d.getDate()+n); break
      case "h": d.setHours(d.getHours()+n); break
      case "n": d.setMinutes(d.getMinutes()+n); break
      case "s": d.setSeconds(d.getSeconds()+n)
   }
   return d
}*/


//-------------------------------------------------------------------
//Displays the appropriate message in the information dialog when a mouse over occurres
function showEventName(theEventName)
{
	document.getElementById("informationDialog").innerHTML = "<br><font color='#FF0000' face='Tahoma, Impact, Arial'><strong>"+theEventName+"</strong></font>" + "<font color='#009900' face='Tahoma, Impact, Arial'><strong> - Available </strong></font>";
}

function hideDialog()
{
	document.getElementById("informationDialog").innerHTML = "";
}

function showBooked()
{
	document.getElementById("informationDialog").innerHTML = "<br><font color='#FF0000' face='Tahoma, Impact, Arial'><strong>Booked</strong></font>";	
}

function showAvailable()
{
	document.getElementById("informationDialog").innerHTML = "<br><font color='#009900' face='Tahoma, Impact, Arial'><strong>Available</strong></font>";	
}

function showAvailableArrival()
{
	document.getElementById("informationDialog").innerHTML = "<br><font color='#009900' face='Tahoma, Impact, Arial'><strong>Available for Arrival</strong></font>";	
}

function showAvailableDeparture()
{
	document.getElementById("informationDialog").innerHTML = "<br><font color='#009900' face='Tahoma, Impact, Arial'><strong>Available for Departure</strong></font>";	
}
//-------------------------------------------------------------------

function writeTableContent(){
	var content = "<TR><TD></TD></TR><TR>";
	var nextRow = " ";
	hereNow.setDate(1);
	var specialFlag = false;
	var eventName = "";
	var daysSoFar = 0;
	var firstDay = hereNow.getDay();
	var tableContentCurrentMonth = hereNow.getMonth();
	var tableContentCurrentYear = hereNow.getFullYear();
	var daysInMonth = monthLength(tableContentCurrentMonth,tableContentCurrentYear);
	for(x=0;x<firstDay;x++){
		content = content + "<TD CLASS='tblEmpty' COLSPAN=2 ROWSPAN=2>&nbsp;</TD><TD rowspan=2></TD>";
	}
			
	//this prints out the first row of days in the calendar
	for(y=firstDay;y<7;y++){
		daysSoFar++;
		
		//these are used to check if the arr or dep should be coloured or not
		loopDate =  (tableContentCurrentMonth+1)+'/'+daysSoFar+'/'+tableContentCurrentYear; //find the current loop date
		DatePlusOne = AddDays(loopDate,1); //add one day to the looped date
		DateMinusOne = AddDays(loopDate,-1); //add one day to the looped date
		
		if((checkIfBooked(daysSoFar,tableContentCurrentMonth,tableContentCurrentYear) == 1))
		{
			//if the next day is booked colour all the TDs yellow
				content = content + "<TD CLASS='tblBooked' COLSPAN=2 onMouseOver=\"javascript:showBooked()\" onMouseOut=\"javascript:hideDialog()\">"+daysSoFar+"</TD>";
					//if the day before is booked colour the dep yellow
					if( checkIfBooked( DateMinusOne.getDate(),DateMinusOne.getMonth(),DateMinusOne.getFullYear() ) == 1 )
					{
					nextRow +="<TD ID='cellDep"+cellId+"' CLASS='tblBookedText'  onMouseOver=\"javascript:showBooked()\" onMouseOut=\"javascript:hideDialog()\">&nbsp;dep&nbsp;</TD>"
					}
					//else colour the cell blue
					else
					{
						//this will elliminate the problem of the dep being available on the 1st of the starting month
						if(systemDate.getMonth() == tableContentCurrentMonth && daysSoFar == 1)
						{
						nextRow +="<TD ID='cellDep"+cellId+"' CLASS='tblBookedText'  onMouseOver=\"javascript:showBooked()\" onMouseOut=\"javascript:hideDialog()\">&nbsp;dep&nbsp;</TD>"
						}
						else
						{
						nextRow +="<TD ID='cellDep"+cellId+"' CLASS='tblContent'  onMouseOver=\"javascript:showAvailableDeparture()\" onMouseOut=\"javascript:hideDialog()\"><A HREF='javascript:setDeparture("+daysSoFar +","+ hereNow.getMonth()+","+hereNow.getFullYear()+","+ cellId +");' ID='ancDep"+cellId+"' CLASS='tblContentText'>&nbsp;dep&nbsp;</A></TD>"
						}
					}	
				nextRow +="<TD ID='cellArr"+cellId+"' CLASS='tblBookedText'  onMouseOver=\"javascript:showBooked()\" onMouseOut=\"javascript:hideDialog()\">&nbsp;arr&nbsp;</TD>"
		}else
		{
			specialFlag = false;
			for(x=0;x<specialDates.length;x++)
			{
				if(specialDates[x][0].getFullYear() == hereNow.getFullYear() &&  specialDates[x][0].getMonth() == hereNow.getMonth() && specialDates[x][0].getDate() == daysSoFar)
					{	
						specialFlag = true;
						xReference = x; //this is used for the mouse over event
					}
			}
			
			if(specialFlag == true)
			{
					/*--------------------------------------
					Note about specialEventNames[specialDates[xReference][1]]
					- specialEventNames contains an array of event names
					- specialDates contains a 2d array with a date in [index,0] and a reference to the name of the event at [index,1], [index,1] is a number and refers to the place in specialEventNames where the event name is located
					- A rather complex way for a fairly simple process I know, but this was the only way I could figure out that would enable a truely dynamic system of special event retrieval
					--------------------------------------*/
					switch(specialDates[xReference][2])
					{
					case "start":
						eventName = specialEventNames[specialDates[xReference][1]];
						content = content + "<TD ID='cell"+cellId+"' COLSPAN=2 CLASS='tblSpecial' onMouseOver=\"javascript:showEventName('"+eventName+"')\" onMouseOut=\"javascript:hideDialog()\">"+daysSoFar+"</TD>";

						if( checkIfBooked( DateMinusOne.getDate(),DateMinusOne.getMonth(),DateMinusOne.getFullYear()) == 1 )
							nextRow +="<TD ID='cellDep"+cellId+"' CLASS='tblBookedText' onMouseOver=\"javascript:showBooked()\" onMouseOut=\"javascript:hideDialog()\">&nbsp;dep&nbsp;</TD>"
						else
							nextRow +="<TD ID='cellDep"+cellId+"' CLASS='tblContent' onMouseOver=\"javascript:showAvailableDeparture()\" onMouseOut=\"javascript:hideDialog()\"><A HREF='javascript:setDeparture("+daysSoFar +","+ hereNow.getMonth()+","+hereNow.getFullYear()+","+ cellId +");' ID='ancDep"+cellId+"' CLASS='tblContentText'>&nbsp;dep&nbsp;</A></TD>"

						nextRow +="<TD ID='cellArr"+cellId+"' CLASS='tblSpecial' onMouseOver=\"javascript:showEventName('"+eventName+"')\" onMouseOut=\"javascript:hideDialog()\"><A HREF='javascript:setArrival("+daysSoFar +","+ hereNow.getMonth()+","+hereNow.getFullYear()+","+ cellId +");' ID='ancArr"+cellId+"' CLASS='tblContentText'>&nbsp;arr&nbsp;</A></TD>"
					break;
					case "finish":
					eventName = specialEventNames[specialDates[xReference][1]];
					content = content + "<TD ID='cell"+cellId+"' COLSPAN=2 CLASS='tblSpecial' onMouseOver=\"javascript:showEventName('"+eventName+"')\" onMouseOut=\"javascript:hideDialog()\">"+daysSoFar+"</TD>";
					nextRow +="<TD ID='cellDep"+cellId+"' CLASS='tblSpecial' onMouseOver=\"javascript:showEventName('"+eventName+"')\" onMouseOut=\"javascript:hideDialog()\"><A HREF='javascript:setDeparture("+daysSoFar +","+ hereNow.getMonth()+","+hereNow.getFullYear()+","+ cellId +");' ID='ancDep"+cellId+"' CLASS='tblContentText'>&nbsp;dep&nbsp;</A></TD>"
					nextRow +="<TD ID='cellArr"+cellId+"' CLASS='tblContent' onMouseOver=\"javascript:showAvailableArrival()\" onMouseOut=\"javascript:hideDialog()\"><A HREF='javascript:setArrival("+daysSoFar +","+ hereNow.getMonth()+","+hereNow.getFullYear()+","+ cellId +");' ID='ancArr"+cellId+"' CLASS='tblContentText'>&nbsp;arr&nbsp;</A></TD>"
					break;
					case "middle":
					eventName = specialEventNames[specialDates[xReference][1]];
					content = content + "<TD ID='cell"+cellId+"' COLSPAN=2 CLASS='tblSpecial' onMouseOver=\"javascript:showEventName('"+eventName+"')\" onMouseOut=\"javascript:hideDialog()\">"+daysSoFar+"</TD>";
					nextRow +="<TD ID='cellDep"+cellId+"' CLASS='tblSpecial' onMouseOver=\"javascript:showEventName('"+eventName+"')\" onMouseOut=\"javascript:hideDialog()\"><A HREF='javascript:setDeparture("+daysSoFar +","+ hereNow.getMonth()+","+hereNow.getFullYear()+","+ cellId +");' ID='ancDep"+cellId+"' CLASS='tblContentText'>&nbsp;dep&nbsp;</A></TD>"
					nextRow +="<TD ID='cellArr"+cellId+"' CLASS='tblSpecial' onMouseOver=\"javascript:showEventName('"+eventName+"')\" onMouseOut=\"javascript:hideDialog()\"><A HREF='javascript:setArrival("+daysSoFar +","+ hereNow.getMonth()+","+hereNow.getFullYear()+","+ cellId +");' ID='ancArr"+cellId+"' CLASS='tblContentText'>&nbsp;arr&nbsp;</A></TD>"
					break;
					case "startAndFinish": //the date is both the starn and finish date such that you can both arrive and depart on the day
					eventName = specialEventNames[specialDates[xReference][1]];
					content = content + "<TD ID='cell"+cellId+"' COLSPAN=2 CLASS='tblSpecial' onMouseOver=\"javascript:showEventName('"+eventName+"')\" onMouseOut=\"javascript:hideDialog()\">"+daysSoFar+"</TD>";
					nextRow +="<TD ID='cellDep"+cellId+"' CLASS='tblContent' onMouseOver=\"javascript:showAvailableDeparture()\" onMouseOut=\"javascript:hideDialog()\"><A HREF='javascript:setDeparture("+daysSoFar +","+ hereNow.getMonth()+","+hereNow.getFullYear()+","+ cellId +");' ID='ancDep"+cellId+"' CLASS='tblContentText'>&nbsp;dep&nbsp;</A></TD>"
					nextRow +="<TD ID='cellArr"+cellId+"' CLASS='tblContent' onMouseOver=\"javascript:showAvailableArrival()\" onMouseOut=\"javascript:hideDialog()\"><A HREF='javascript:setArrival("+daysSoFar +","+ hereNow.getMonth()+","+hereNow.getFullYear()+","+ cellId +");' ID='ancArr"+cellId+"' CLASS='tblContentText'>&nbsp;arr&nbsp;</A></TD>"
					break;
					}
			}
			else
			{
				content = content + "<TD ID='cell"+cellId+"' CLASS='tblContent' COLSPAN=2 onMouseOver=\"javascript:showAvailable()\" onMouseOut=\"javascript:hideDialog()\">"+daysSoFar+"</TD>";
						//if the day before is booked colour the dep yellow
						if( checkIfBooked( DateMinusOne.getDate(),DateMinusOne.getMonth(),DateMinusOne.getFullYear()) == 1 )
						{
							nextRow +="<TD ID='cellDep"+cellId+"' CLASS='tblBookedText' onMouseOver=\"javascript:showBooked()\" onMouseOut=\"javascript:hideDialog()\">&nbsp;dep&nbsp;</TD>"
							nextRow +="<TD ID='cellArr"+cellId+"' CLASS='tblContent' onMouseOver=\"javascript:showAvailableArrival()\" onMouseOut=\"javascript:hideDialog()\"><A HREF='javascript:setArrival("+daysSoFar +","+ hereNow.getMonth()+","+hereNow.getFullYear()+","+ cellId +");' ID='ancArr"+cellId+"' CLASS='tblContentText'>&nbsp;arr&nbsp;</A></TD>"

						}
						//else colour the cell blue
						else
						{
						nextRow +="<TD ID='cellDep"+cellId+"' CLASS='tblContent' onMouseOver=\"javascript:showAvailable()\" onMouseOut=\"javascript:hideDialog()\"><A HREF='javascript:setDeparture("+daysSoFar +","+ hereNow.getMonth()+","+hereNow.getFullYear()+","+ cellId +");' ID='ancDep"+cellId+"' CLASS='tblContentText'>&nbsp;dep&nbsp;</A></TD>"
						nextRow +="<TD ID='cellArr"+cellId+"' CLASS='tblContent' onMouseOver=\"javascript:showAvailable()\" onMouseOut=\"javascript:hideDialog()\"><A HREF='javascript:setArrival("+daysSoFar +","+ hereNow.getMonth()+","+hereNow.getFullYear()+","+ cellId +");' ID='ancArr"+cellId+"' CLASS='tblContentText'>&nbsp;arr&nbsp;</A></TD>"
						}
			}
		}
		content += "<td rowspan=2>&nbsp;</td>";
	cellId++;
	
	}

	content += "</TR><TR>"+nextRow+"</TR><TR><TD></TD></TR><TR>"
	nextRow = " ";
	
	//this prints out the remaining rows of the calendar
	for(h=0;h<5;h++){
		content = content + "<tr><td></td></tr>";
		if(daysSoFar == daysInMonth)
			break;
		for(i=0;i<7;i++){
			daysSoFar++;
			
			//these are used to check if the arr or dep should be coloured or not
			loopDate =  (tableContentCurrentMonth+1)+'/'+daysSoFar+'/'+tableContentCurrentYear; //find the current loop date
			
			DatePlusOne = AddDays(loopDate,1); //add one day to the looped date
			DateMinusOne = AddDays(loopDate,-1); //add one day to the looped date
//alert("LOOP DATE: "+loopDate+"/nDATE PLUS ONE: "+DatePlusOne+"DATE MINUS ONE: "+DateMinusOne);
			if( (checkIfBooked(daysSoFar,tableContentCurrentMonth,tableContentCurrentYear) == 1) ){
					//if the next day is booked colour all the TDs yellow (ie the booking is continuing)
					content = content + "<TD CLASS='tblBooked' COLSPAN=2 onMouseOver=\"javascript:showBooked()\" onMouseOut=\"javascript:hideDialog()\">"+daysSoFar+"</TD>";
						//if the day before is booked colour the dep yellow						
						if( checkIfBooked( DateMinusOne.getDate(),DateMinusOne.getMonth(),DateMinusOne.getFullYear()) == 1 )
						{
						nextRow +="<TD ID='cellDep"+cellId+"' CLASS='tblBookedText'  onMouseOver=\"javascript:showBooked()\" onMouseOut=\"javascript:hideDialog()\">&nbsp;dep&nbsp;</TD>"
						}
						//else colour the cell blue
						else
						{
						nextRow +="<TD ID='cellDep"+cellId+"' CLASS='tblContent'  onMouseOver=\"javascript:showAvailableDeparture()\" onMouseOut=\"javascript:hideDialog()\"><A HREF='javascript:setDeparture("+daysSoFar +","+ hereNow.getMonth()+","+hereNow.getFullYear()+","+ cellId +");' ID='ancDep"+cellId+"' CLASS='tblContentText'>&nbsp;dep&nbsp;</A></TD>"
						}
					nextRow +="<TD ID='cellArr"+cellId+"' CLASS='tblBookedText'  onMouseOver=\"javascript:showBooked()\" onMouseOut=\"javascript:hideDialog()\">&nbsp;arr&nbsp;</TD>"
			}else{
				specialFlag = false;
				for(x=0;x<specialDates.length;x++)
				{
					if(specialDates[x][0].getFullYear() == hereNow.getFullYear() &&  specialDates[x][0].getMonth() == hereNow.getMonth() && specialDates[x][0].getDate() == daysSoFar)
					{	
						specialFlag = true;
						xReference = x; //this is used for the mouse over event
					}
				}
				
				if(specialFlag == true)
				{
					/*--------------------------------------
					Note about specialEventNames[specialDates[xReference][1]]
					- specialEventNames contains an array of event names
					- specialDates contains a 2d array with a date in [index,0] and a reference to the name of the event at [index,1], [index,1] is a number and refers to the place in specialEventNames where the event name is located
					- A rather complex way for a fairly simple process I know, but this was the only way I could figure out that would enable a truely dynamic system of special event retrieval
					--------------------------------------*/
					switch(specialDates[xReference][2])
					{
					case "start":
						eventName = specialEventNames[specialDates[xReference][1]];
						content = content + "<TD ID='cell"+cellId+"' COLSPAN=2 CLASS='tblSpecial' onMouseOver=\"javascript:showEventName('"+eventName+"')\" onMouseOut=\"javascript:hideDialog()\">"+daysSoFar+"</TD>";

						if( checkIfBooked( DateMinusOne.getDate(),DateMinusOne.getMonth(),DateMinusOne.getFullYear()) == 1 )
							nextRow +="<TD ID='cellDep"+cellId+"' CLASS='tblBookedText' onMouseOver=\"javascript:showBooked()\" onMouseOut=\"javascript:hideDialog()\">&nbsp;dep&nbsp;</TD>"
						else
							nextRow +="<TD ID='cellDep"+cellId+"' CLASS='tblContent' onMouseOver=\"javascript:showAvailableDeparture()\" onMouseOut=\"javascript:hideDialog()\"><A HREF='javascript:setDeparture("+daysSoFar +","+ hereNow.getMonth()+","+hereNow.getFullYear()+","+ cellId +");' ID='ancDep"+cellId+"' CLASS='tblContentText'>&nbsp;dep&nbsp;</A></TD>"

						nextRow +="<TD ID='cellArr"+cellId+"' CLASS='tblSpecial' onMouseOver=\"javascript:showEventName('"+eventName+"')\" onMouseOut=\"javascript:hideDialog()\"><A HREF='javascript:setArrival("+daysSoFar +","+ hereNow.getMonth()+","+hereNow.getFullYear()+","+ cellId +");' ID='ancArr"+cellId+"' CLASS='tblContentText'>&nbsp;arr&nbsp;</A></TD>"

					break;
					case "finish":
					eventName = specialEventNames[specialDates[xReference][1]];
					content = content + "<TD ID='cell"+cellId+"' COLSPAN=2 CLASS='tblSpecial' onMouseOver=\"javascript:showEventName('"+eventName+"')\" onMouseOut=\"javascript:hideDialog()\">"+daysSoFar+"</TD>";
					nextRow +="<TD ID='cellDep"+cellId+"' CLASS='tblSpecial' onMouseOver=\"javascript:showEventName('"+eventName+"')\" onMouseOut=\"javascript:hideDialog()\"><A HREF='javascript:setDeparture("+daysSoFar +","+ hereNow.getMonth()+","+hereNow.getFullYear()+","+ cellId +");' ID='ancDep"+cellId+"' CLASS='tblContentText'>&nbsp;dep&nbsp;</A></TD>"
					nextRow +="<TD ID='cellArr"+cellId+"' CLASS='tblContent' onMouseOver=\"javascript:showAvailableArrival()\" onMouseOut=\"javascript:hideDialog()\"><A HREF='javascript:setArrival("+daysSoFar +","+ hereNow.getMonth()+","+hereNow.getFullYear()+","+ cellId +");' ID='ancArr"+cellId+"' CLASS='tblContentText'>&nbsp;arr&nbsp;</A></TD>"
					break;
					case "middle":
					eventName = specialEventNames[specialDates[xReference][1]];
					content = content + "<TD ID='cell"+cellId+"' COLSPAN=2 CLASS='tblSpecial' onMouseOver=\"javascript:showEventName('"+eventName+"')\" onMouseOut=\"javascript:hideDialog()\">"+daysSoFar+"</TD>";
					nextRow +="<TD ID='cellDep"+cellId+"' CLASS='tblSpecial' onMouseOver=\"javascript:showEventName('"+eventName+"')\" onMouseOut=\"javascript:hideDialog()\"><A HREF='javascript:setDeparture("+daysSoFar +","+ hereNow.getMonth()+","+hereNow.getFullYear()+","+ cellId +");' ID='ancDep"+cellId+"' CLASS='tblContentText'>&nbsp;dep&nbsp;</A></TD>"
					nextRow +="<TD ID='cellArr"+cellId+"' CLASS='tblSpecial' onMouseOver=\"javascript:showEventName('"+eventName+"')\" onMouseOut=\"javascript:hideDialog()\"><A HREF='javascript:setArrival("+daysSoFar +","+ hereNow.getMonth()+","+hereNow.getFullYear()+","+ cellId +");' ID='ancArr"+cellId+"' CLASS='tblContentText'>&nbsp;arr&nbsp;</A></TD>"
					break;
					case "startAndFinish": //the date is both the starn and finish date such that you can both arrive and depart on the day
					eventName = specialEventNames[specialDates[xReference][1]];
					content = content + "<TD ID='cell"+cellId+"' COLSPAN=2 CLASS='tblSpecial' onMouseOver=\"javascript:showEventName('"+eventName+"')\" onMouseOut=\"javascript:hideDialog()\">"+daysSoFar+"</TD>";
					nextRow +="<TD ID='cellDep"+cellId+"' CLASS='tblContent' onMouseOver=\"javascript:showAvailableDeparture()\" onMouseOut=\"javascript:hideDialog()\"><A HREF='javascript:setDeparture("+daysSoFar +","+ hereNow.getMonth()+","+hereNow.getFullYear()+","+ cellId +");' ID='ancDep"+cellId+"' CLASS='tblContentText'>&nbsp;dep&nbsp;</A></TD>"
					nextRow +="<TD ID='cellArr"+cellId+"' CLASS='tblContent' onMouseOver=\"javascript:showAvailableArrival()\" onMouseOut=\"javascript:hideDialog()\"><A HREF='javascript:setArrival("+daysSoFar +","+ hereNow.getMonth()+","+hereNow.getFullYear()+","+ cellId +");' ID='ancArr"+cellId+"' CLASS='tblContentText'>&nbsp;arr&nbsp;</A></TD>"
					break;
					}
				}
				else //This date is not a special period so use normal colour
				{
					content = content + "<TD ID='cell"+cellId+"' CLASS='tblContent' COLSPAN=2 onMouseOver=\"javascript:showAvailable()\" onMouseOut=\"javascript:hideDialog()\">"+daysSoFar+"</TD>";
					
						//if the day before is booked colour the dep yellow
						if( checkIfBooked( DateMinusOne.getDate(),DateMinusOne.getMonth(),DateMinusOne.getFullYear()) == 1 )
						{
							nextRow +="<TD ID='cellDep"+cellId+"' CLASS='tblBookedText' onMouseOver=\"javascript:showBooked()\" onMouseOut=\"javascript:hideDialog()\">&nbsp;dep&nbsp;</TD>"
							nextRow +="<TD ID='cellArr"+cellId+"' CLASS='tblContent' onMouseOver=\"javascript:showAvailableArrival()\" onMouseOut=\"javascript:hideDialog()\"><A HREF='javascript:setArrival("+daysSoFar +","+ hereNow.getMonth()+","+hereNow.getFullYear()+","+ cellId +");' ID='ancArr"+cellId+"' CLASS='tblContentText'>&nbsp;arr&nbsp;</A></TD>"

						}
						//else colour the cell blue
						else
						{
						nextRow +="<TD ID='cellDep"+cellId+"' CLASS='tblContent' onMouseOver=\"javascript:showAvailable()\" onMouseOut=\"javascript:hideDialog()\"><A HREF='javascript:setDeparture("+daysSoFar +","+ hereNow.getMonth()+","+hereNow.getFullYear()+","+ cellId +");' ID='ancDep"+cellId+"' CLASS='tblContentText'>&nbsp;dep&nbsp;</A></TD>"
						nextRow +="<TD ID='cellArr"+cellId+"' CLASS='tblContent' onMouseOver=\"javascript:showAvailable()\" onMouseOut=\"javascript:hideDialog()\"><A HREF='javascript:setArrival("+daysSoFar +","+ hereNow.getMonth()+","+hereNow.getFullYear()+","+ cellId +");' ID='ancArr"+cellId+"' CLASS='tblContentText'>&nbsp;arr&nbsp;</A></TD>"
						}

					}

			}
			
			
			content += "<td rowspan=2>&nbsp;</td>";
			
			
			//Write in spaces for end of calendar formatting
			if(daysSoFar == daysInMonth){
				for(j=(i + 1);j<7;j++){
					content = content + "<TD CLASS='tblEmpty' COLSPAN=2 ROWSPAN=2>&nbsp;</TD>";
				}
				cellId++;
				content += "</TR>"			
				break;
			}
		cellId++;
		}
	content += "</TR><TR>"+nextRow+"</TR><TR><TD></TD></TR>"
	nextRow = " ";
	}
	content += "</TABLE>";
	hereNow.setMonth(tableContentCurrentMonth + 1);
	return content;
}

function checkIfBooked(day,month,year){
	var dateStr = day + ' ' + months[month] + ' ' + year;
	var dateToCheck = new Date(dateStr);
	var rtnVal = 0;
	for(q=0;q<fromTo.length;q=q+2){
		if((dateToCheck.getTime() >= fromTo[q].getTime()) && (dateToCheck.getTime()  <= fromTo[q+1].getTime()))
		{
			bookedDates[bookedDates_counter] = dateToCheck; //used to validate the condition which states that a person can book a one night on a weekend if there is no two night available
			bookedDates_counter++;
			rtnVal = 1;
			return rtnVal;
		}
	}
	return rtnVal;
}

function monthLength(monthNo, yearNo) {
if(monthNo == 1){
	if ((yearNo % 4) == 0) {
		if ((yearNo % 100) == 0 && (yearNo % 400) != 0)
			return daysInMonths[monthNo];
		return daysInMonths[monthNo] + 1;
	} 
	else
		return daysInMonths[monthNo];
}
		return daysInMonths[monthNo];
}

function previous_month(){
	if(currentLeftMonth == 0){
		alert('Sorry, you cannot see availability for months that have passed.');
		return;
	}
	if(start_flag == 1 && stop_flag == 1){
		reset_dates();	
	}
	if(start_flag == 1){
		cancelStartDate();	
	}

	currentLeftMonth--;
	writeInnerHTML();
}

function next_month(){ 
	if(currentLeftMonth == (monthsToShow -2)){
			alert('Sorry, bookings for this property are not being taken this far in advance. \n Please phone the Daylesford Getaways office on 03 5348 4422 \n if you wish to book this property this far in advance.');
			return;
		}
	
	if(start_flag == 1 && stop_flag == 1){
		reset_dates();	
	}
	if(start_flag == 1){
		cancelStartDate();	
	}

	currentLeftMonth++;
	writeInnerHTML();
}

function tell_booked(){alert('Sorry. The property has been booked for this night. \n Please select another arrival date.');}

function alertIfInSpecial(date){
	var underline = '__________________________________________________________________';
	var dateToCheck = new Date(date);
	for(q=0;q<specials.length;q=q+4){
		if((dateToCheck.getTime() >= specials[q].getTime()) && (dateToCheck.getTime()  <= specials[q+1].getTime()))
		{
			alert(underline +'\n'+underline+'\n\nA special holiday period includes all the nights from '+ specials[q].toString().slice(0,10) + ' to ' + specials[q+1].toString().slice(0,10) +'.\nIf you stay in ' + propertyName +  ' for any part of this period you will pay for the whole period.\nTo get the best value for your money, you should book for the whole period.\n'+underline+'\n'+underline+'\n\nYou can cancel your booked dates and enter new ones by clicking the cancel\nbutton, just below the calendars.');
		}
	}
}

function setArrival(day,month,year,cellId){
	if(stop_flag == 1){
		alert('Your arrival and departure dates have been chosen. \nPlease use the link in the box below the calendars if \nyou want to to cancel these dates.');
		return;
	}
	if(start_flag == 1){
		alert('You have chosen your arrival date. \n Please click on a departure date.');
		return;
	}
	startDate = day + ' ' + months[month] + ' ' + year; 
	cellIdStart = cellId;
	document.getElementById("cellArr" + cellId).style.backgroundColor = '#FFFF99';
	document.getElementById("ancArr" + cellId).style.color = '#000000';
	document.getElementById("messageDiv").innerHTML = "<TABLE WIDTH='795'  CLASS='messageToUser'><TR><TD align='center'><h2>Click 'dep' below your day of departure.</h2><BR><BR><div style='color:#000000;background-color:#CCCCCC;width:100; border: solid; border-width: thin;'><A HREF='javascript:cancelStartDate();' CLASS='messageToUser' STYLE='color:#000000;background-color:#CCCCCC'><b>cancel</b></A></div></TD></TR></TABLE>";
	start_flag = 1;
}

function setDeparture(day,month,year,cellId){
	if(start_flag == 0){
		alert('You must enter an arrival date by clicking \non one day\'s "arr" before you can set a \ndeparture date.');
		return;
	}
	if(stop_flag == 1){	return;	}
	stopDate = day + ' ' + months[month] + ' ' + year; 
	var a = new Date(startDate);
	var b = new Date(stopDate);
	if (b.getTime() < a.getTime())
	{	
		alert('You have tried to enter an end date that is before the start date. Please enter new dates.');
		reset_dates();
		return;
	}
	document.getElementById("cellDep" + cellId).style.backgroundColor = '#FFFF99';
	document.getElementById("ancDep" + cellId).style.color = '#000000';
	cellIdStop = cellId;
	stop_flag = 1;
	switch(checkValidBooking()){
		case 0:
		alert('Some of your desired booking is already booked. \nPlease enter new dates.');
		reset_dates();
		break;
		case 1:
		fill();
		writeNumberGuests(maxNoGuests);
		break;
		case 2:
			alert('You can not depart the same day that you arrive.\nPlease enter new dates');
			cancelSameArrAndDep();
		break;
		default:
			alert('We are sorry, there has been a problem and this property cannot be booked. \nTravel Network is proud of the quality of their product and must apologise for not forseeing this problem. \nPlease phone the office to report this problem and we will be happy to take your booking over the phone.');
		break;
	}
}

function cancelStartDate(){
	start_flag = 0;
	document.getElementById("cellArr" + cellIdStart).style.backgroundColor = '#6600CC';
	document.getElementById("ancArr" + cellIdStart).style.color = '#ffffff';
	document.getElementById("messageDiv").innerHTML = strMsgFirst;
}

function cancelSameArrAndDep(){
start_flag = 0;
stop_flag = 0;
	document.getElementById("cellArr" + cellIdStart).style.backgroundColor = '#6600CC';
	document.getElementById("ancArr" + cellIdStart).style.color = '#ffffff';
	document.getElementById("cellDep" + cellIdStop).style.backgroundColor = '#6600CC';
	document.getElementById("ancDep" + cellIdStop).style.color = '#ffffff';
	document.getElementById("messageDiv").innerHTML = strMsgFirst;
}

function reset_dates(){
if(start_flag == 0){return;}
start_flag = 0;
stop_flag = 0;
	document.getElementById("cellArr" + cellIdStart).style.backgroundColor = '#6600CC';
	document.getElementById("ancArr" + cellIdStart).style.color = '#ffffff';
	document.getElementById("cellDep" + cellIdStop).style.backgroundColor = '#6600CC';
	document.getElementById("ancDep" + cellIdStop).style.color = '#ffffff';
	for(i=cellIdStart; i<=cellIdStop; i++){
		if(document.getElementById("cell" + i)){
			document.getElementById("cell" + i).style.backgroundColor = '#6600CC'; 
			document.getElementById("cell" + i).style.color = '#ffffff';
		}
	}
		document.getElementById("messageDiv").innerHTML = strMsgFirst;
}

function fill(){
	if(stop_flag == 0){return;}
	for(i=cellIdStart; i<cellIdStop; i++){
		document.getElementById("cell" + i).style.backgroundColor = '#FFFF99';
		document.getElementById("cell" + i).style.color = '#000000';
	}
	return;
}

function checkValidBooking(){
	var dateToCheck = new Date(startDate);
	var endDate = new Date(stopDate);
	var rtnVal = 0;
	if(dateToCheck.getTime() == endDate.getTime()){
		rtnVal = 2;
		return rtnVal;
	}
	while (dateToCheck.getTime() < endDate.getTime())
	{
		for(q=0;q<fromTo.length;q=q+2){	
			if((dateToCheck.getTime() >= fromTo[q].getTime()) && (dateToCheck.getTime()  <= fromTo[q+1].getTime()))//booking overlaps a previous booking
			{
				return rtnVal;
			}
		}
		dateToCheck.setDate(dateToCheck.getDate() + 1);
	}
	rtnVal = 1;
	return rtnVal;
}

function writeNumberGuests(size){
	
	var strNumberGuests ="<TABLE WIDTH='795'  CLASS='messageToUser'><TR><TD align='center'><h2>Please select the number of guests.</h2><BR><BR>"
	for(i=1;i<=size;i++){
		strNumberGuests += "<A HREF='javascript:setNumberGuests("+i+");' CLASS='messageToUser'><b>" + i + "</b></A>";
		if(i<size)
		{strNumberGuests += "&nbsp;&nbsp;or&nbsp;&nbsp;"}
	}
	strNumberGuests += "</TD></TR><TR><TD align='center'><div style='color:#000000;background-color:#CCCCCC;width:100; border: solid; border-width: thin;'><A HREF='javascript:reset_dates();' CLASS='messageToUser' STYLE='color:#000000;background-color:#CCCCCC'><b>Cancel</b></A></div></TD></TR></TABLE>";
	document.getElementById("messageDiv").innerHTML = strNumberGuests; 
	specialPeriod();
}

function setNumberGuests(given){
	numberGuestsChosen = given;
	writeNumberRooms(maxNoRooms);
}

function writeNumberRooms(size){
	if(size == 1){
		setNumberRooms(1);
		return;
	}
	var strNumberRooms ="<TABLE WIDTH='795'  CLASS='messageToUser'><TR><TD align='center'><h2>Please select how many rooms you require.</h2><BR><BR>"
	for(i=1;i<=size;i++){
		strNumberRooms += "<A HREF='javascript:setNumberRooms("+i+");' CLASS='messageToUser'><b>" + i + "</b></A>";
		if(i<size)
		{strNumberRooms += "&nbsp;&nbsp;or&nbsp;&nbsp;"}
	}

	strNumberRooms += "</TD></TR><TR><TD align='center'><div style='color:#000000;background-color:#CCCCCC;width:100; border: solid; border-width: thin;'><A HREF='javascript:reset_dates();' CLASS='messageToUser' STYLE='color:#000000;background-color:#CCCCCC'><b>Cancel</b></A></div></TD></TR></TABLE>";
	document.getElementById("messageDiv").innerHTML = strNumberRooms; 
}

function setNumberRooms(given){
	numberRoomsChosen = given;
	writeNumberBeds(maxNoBeds);
}

function writeNumberBeds(size){
	if(size == 1){
		setNumberBeds(1);
		return;
	}
	var strNumberBeds ="<TABLE WIDTH='795'  CLASS='messageToUser'><TR><TD align='center'><h2>Please select how many beds you require.</h2><BR><BR>"

	for(i=1;i<=size;i++){
		strNumberBeds += "<A HREF='javascript:setNumberBeds("+i+");' CLASS='messageToUser'><b>" + i + "</b></A>";
		if(i<size)
		{strNumberBeds += "&nbsp;&nbsp;or&nbsp;&nbsp;"}
	}
	strNumberBeds += "</TD></TR><TR><TD align='center'><div style='color:#000000;background-color:#CCCCCC;width:100; border: solid; border-width: thin;'><A HREF='javascript:reset_dates();' CLASS='messageToUser' STYLE='color:#000000;background-color:#CCCCCC'><b>Cancel</b></A></div></TD></TR></TABLE>";
	document.getElementById("messageDiv").innerHTML = strNumberBeds; 

}

function setNumberBeds(given){
	numberBedsChosen = given;
	confirmDetails();
}

function confirmDetails(){
	var strCost = showCost();

	if (strCost == 'justReset') 
	{
		reset_dates();
		return;
	}
	/*
	
	
	
	
	
	
	
	
	THIS BEIND ENABLED IS ONLY TEMPORARY
	
	
	
	
	
	
	
	
	
	
	
	//NOW HANDLED BY specailPeriod() function in bookingOne.asp
	if (strCost == 'specialPeriod')
	{
		alert('All or part of the dates selected include a special period, therefore conditions may apply.\n\nTo make a reservation for this or other properties during the requested dates, please call\nDaylesford Getaways on (03)5348 4422 or email a request to bookings@dayget.com.au');
		reset_dates();
		return;
	}*/

	if (strCost == 'moreThan10Nights')
	{
		alert('Stays that are longer than 10 nights attract special discounts.\n Please phone the Dayget office on 03 5348 4422');
		reset_dates();
		return;
	}
	if (strCost == '1nightWend')
	{
		//if the next day is booked the client can book a one night weekend	
		if( bookedDates.toString().indexOf(AddDays(startDate,1)) != -1 )
		{
			//the client can book becuase the other weekend dates are booked
		}
		else
		{
		alert('You can only book a one-night weekend \nif a two night weekend is not available.');
		reset_dates();
		return;
		
		}
	}
	//THE BELOW CODE WAS NOT WORKING IN NS SO I CHANGED IT TO THE CODE BELOW
	//document.getElementById("datesEtc").cost.value = strCost;
	//document.getElementById("datesEtcRegister").cost.value = strCost;
	document.datesEtc.cost.value = strCost;
	document.datesEtcRegister.cost.value = strCost;
	
	var  strConfirm ="<TABLE CLASS='messageToUser'><TR><TD><b>Your booking at "+propertyName+"</B><UL><LI>Arriving "+startDate+"</LI><LI>Departing "+stopDate+"</LI><LI>"+numberGuestsChosen+" guests</LI><LI>"+numberBedsChosen+" bed(s)</LI><LI>"+numberRoomsChosen+" room(s).</LI><LI><b>Cost $"+strCost+".00</b></LI></UL></TD><TD align='left' valign='bottom'><div style='color:#000000;background-color:#CCCCCC;width:100; border: solid; border-width: thin;'><A HREF='javascript:logon();' CLASS='messageToUser' STYLE='color:#000000;background-color:#CCCCCC'><center><b>OK</b></center></A></div><BR><div style='color:#000000;background-color:#CCCCCC;width:100; border: solid; border-width: thin;'><A HREF='javascript:reset_dates();' CLASS='messageToUser' STYLE='color:#000000;background-color:#CCCCCC'><center><b>Cancel</b></center></A></div></TD></TR></TABLE>"

	document.getElementById("messageDiv").innerHTML = strConfirm; 
}

function validOneNightBooking()
{
		var weekendDayValues = "4560"; //these values represent the days thursday - sunday
		var booWeekendDay; //boolean value - ture or false
		var dateMinusOneDay;
		dateMinusOneDay = AddDays(startDate,-1).getDay();
		
		//if the day minus one is a weekend date check booWeekendDay as true
		if(weekendDayValues.indexOf(dateMinusOneDay))
			booWeekendDay = true;
			
			
		//if the next day is booked the client can book a one night weekend	
		if( (bookedDates.toString().indexOf(AddDays(startDate,1)) != -1) && (booWeekendDay != true) )
		{
			//the client can book becuase the other weekend dates are booked
			return true;
		}
		
		//if the day is in the middle of two weekend dates
		if( (bookedDates.toString().indexOf(AddDays(startDate,1)) != -1) && (bookedDates.toString().indexOf(AddDays(startDate,-1)) != -1) && (booWeekendDay == true) )
		{
			return true;
		}
		
	return false;
}

function logon(){
	var strLogon = "<FORM METHOD=POST NAME='nameAndpwd' ACTION='bookingTwo.asp'><TABLE WIDTH='795'  CLASS='messageToUser'><TR><TD align='center' STYLE='border-bottom-style:solid; border-bottom-width:thin;' COLSPAN=2><h2>New Guests<div style='color:#000000;background-color:#CCCCCC;width:100; border: solid; border-width: thin;'><center><A HREF='javascript:register();' CLASS='messageToUser' STYLE='color:#000000;background-color:#CCCCCC;'><b>Register</b></center></A></div></H2></TD></TR><TR><TD colspan='2' align='center'><H2>Returning Guests</H2><TR><TD align='right' width='50%'><b>Last name</b></TD><TD align='left'><INPUT TYPE='text' NAME='lastName' SIZE=12></TD></TR><TR><TD align='right'><b>Postcode</b></TD><TD align='left'><INPUT TYPE='password' NAME='postcode' SIZE=12></TD></TR><TR><TD align='right' width='50%'><b>Last four digits of home phone number</b></TD><TD align='left'><INPUT TYPE='password' NAME='phoneNumber' SIZE=12></TD></TR><TR><TD align='center' colspan='2'><div style='color:#000000;background-color:#CCCCCC;width:100; border: solid; border-width: thin;'><center><A HREF='javascript:book();' CLASS='messageToUser' STYLE='color:#000000;background-color:#CCCCCC; '><b>Continue</b></div></A></TD><TD>&nbsp;</TD></TR></TABLE></FORM>"
	document.getElementById("messageDiv").innerHTML = strLogon; 
}

function register(){
		prepareAndSubmit("datesEtcRegister");	
}

function book(){
	if(validateNameAndPwd()){
		prepareAndSubmit("datesEtc");
	}
}

function emptyTextDetector(fieldName){
	for(var i=0; i<fieldName.length; i++)
	{
		var c = fieldName.charAt(i);
		if ((c != ' ' ) && (c!= '\n') && (c != '\t')) return false;
	}
	return true;
}

function validateNameAndPwd(){
	if ((document.nameAndpwd.lastName.value == null)||(document.nameAndpwd.lastName.value == ' ')||(emptyTextDetector(document.nameAndpwd.lastName.value)) )
	{
		alert('You have not entered your last name.');
		return false;
	}
	if ((document.nameAndpwd.postcode.value == null)||(document.nameAndpwd.postcode.value == ' ')||(emptyTextDetector(document.nameAndpwd.postcode.value)) )
	{
		alert('You have not entered your postcode.');
		return false;
	}
	return true;
}


function prepareAndSubmit(which){
//FORM VALIDATION
	if(NS6)
	{

		document.forms[which].PropName.value = propertyName;
		document.forms[which].PropID.value = propertyID; 
		document.forms[which].TariffID.value = tariffID;
		document.forms[which].startDate.value = startDate;
		document.forms[which].endDate.value = stopDate;
		document.forms[which].noGuests.value = numberGuestsChosen;
		document.forms[which].noRooms.value = numberRoomsChosen;
		document.forms[which].noBeds.value = numberBedsChosen;
		document.forms[which].lastName.value = document.nameAndpwd.lastName.value;
		document.forms[which].postcode.value = document.nameAndpwd.postcode.value;
		document.forms[which].phoneNumber.value = document.nameAndpwd.phoneNumber.value;
		document.forms[which].timeStamp.value = new Date();
		document.forms[which].submit();
	}
	else
	{
	document.getElementById(which).PropName.value = propertyName;
	document.getElementById(which).PropID.value = propertyID; 
	document.getElementById(which).TariffID.value = tariffID;
	document.getElementById(which).startDate.value = startDate;
	document.getElementById(which).endDate.value = stopDate;
	document.getElementById(which).noGuests.value = numberGuestsChosen;
	document.getElementById(which).noRooms.value = numberRoomsChosen;
	document.getElementById(which).noBeds.value = numberBedsChosen;
	document.getElementById(which).lastName.value = document.getElementById("nameAndpwd").lastName.value;
	document.getElementById(which).postcode.value = document.getElementById("nameAndpwd").postcode.value;
	document.getElementById(which).phoneNumber.value = document.getElementById("nameAndpwd").phoneNumber.value;
	document.getElementById(which).timeStamp.value = new Date();
	document.getElementById(which).submit();
	}
}

function checkTomorrowSpecial(dateInMilliseconds){
	var rtnVal = 0;
	dateInMilliseconds += 86400000;
	for(q=0;q<specials.length;q=q+4){
		if(dateInMilliseconds == specials[q].getTime()){
			rtnVal = 1;
		}
	}
	return rtnVal;
}

function showNightsStayed(){
var specialPeriodDuration = 0;
var specialPeriodCost = 0;
var specialPeriodAlert = ' ';
var messageString = ' ';
var currentDate = new Date(startDate).getTime();
var finalDate = new Date(stopDate).getTime();
var specialEndDate = new Date(stopDate);
var midweekNightsCounter = 0;
var weekendNightsCounter = 0;
var totalNightsCounter = 0;
var zone1 = parseInt(new Date(currentDate).getTimezoneOffset()); 
var zone2 = parseInt(new Date(finalDate).getTimezoneOffset()); 
	if(zone1 != zone2){
		finalDate -= 60 * (zone2 - zone1) * 1000;
	}
	while (currentDate < finalDate){
		for(i=0; i<specials.length; i+=4){
			if(currentDate == specials[i].getTime()){
				midweekNightsCounter -= 1;
				totalNightsCounter -= 1;
				specialPeriodCost = specials[i+2];
				specialEndDate = specials[i+1].getTime();
				specialPeriodDuration = (specials[i+1].getTime() - specials[i].getTime() + 86400000) / 86400000;
				if(specialEndDate <= finalDate - 86400000){
					if(specialEndDate < finalDate - 86400000){
						currentDate = specialEndDate;
						specialPeriodAlert += ' ';
					}else{
						specialPeriodAlert += ' ';
						currentDate = finalDate;
					}
				}else{
					specialPeriodAlert += 'A special holiday period includes all the nights from '+ specials[i].toString().slice(0,10) + ' to ' + specials[i+1].toString().slice(0,10) + ' but your booking ends before the end of this period. To get the best value you should book for the whole of this period.';
					currentDate = specialEndDate;
				}
			}
		}
		for(i=0; i<specials.length; i+=4){
			if((currentDate > specials[i].getTime()) && (currentDate  < specials[i+1].getTime())){
				midweekNightsCounter -= 1;
				totalNightsCounter -= 1;
				specialPeriodCost = specials[i+2];
				specialEndDate = specials[i+1].getTime();
				specialPeriodDuration = (specials[i+1].getTime() - specials[i].getTime() + 86400000) / 86400000;
				if(specialEndDate <= finalDate - 86400000){
					if(specialEndDate == finalDate - 86400000){
						specialPeriodAlert += 'A special holiday period includes all the nights from '+ specials[i].toString().slice(0,10) + ' to ' + specials[i+1].toString().slice(0,10) + ' but your booking begins after the start of this period. To get the best value you should book for the whole of this period.';
						currentDate = finalDate + 86400000;
					}else{
						specialPeriodAlert += 'A special holiday period includes all the nights from '+ specials[i].toString().slice(0,10) + ' to ' + specials[i+1].toString().slice(0,10) + ' but your booking begins after the start of this period. To get the best value you should book for the whole of this period.';
						currentDate = specialEndDate;
					}
				}else{
					specialPeriodAlert += 'A special holiday period includes all the nights from '+ specials[i].toString().slice(0,10) + ' to ' + specials[i+1].toString().slice(0,10) + ' but your booking begins after the start of this period and your booking ends before the end of the special period. To get the best value you should book for the whole of this period.';
					currentDate = finalDate + 86400000;
				}
			}
		}
		if(new Date(currentDate).getDay() == 4){
			if(checkTomorrowSpecial(currentDate)){
			}else{
				if((currentDate + 86400000) <= finalDate - 86400000){
					midweekNightsCounter -= 1;
					totalNightsCounter -= 1;
					if((currentDate + 172800000) <= finalDate - 86400000){
						if((currentDate + 259200000) <= finalDate - 86400000){
							if(checkTomorrowSpecial(currentDate + 86400000) || checkTomorrowSpecial(currentDate + 172800000)){
								specialPeriodDuration = 999;
							currentDate += (86400000 * 3);
							}else{
							totalNightsCounter += 4;
							weekendNightsCounter += 4;
							currentDate += (86400000 * 3);
							}
						}else{
							if(checkTomorrowSpecial(currentDate + 86400000)){
								specialPeriodDuration = 999;
							}else{
							totalNightsCounter += 3;
							weekendNightsCounter += 3;
							currentDate += (86400000 * 3);
							}
						}
					}else{
						totalNightsCounter += 2;
						weekendNightsCounter += 2;
						currentDate += (86400000 * 3);
					}
				}else{
					currentDate += (86400000 * 3);
				}
			}
		}
		if(new Date(currentDate).getDay() == 5){ 
				midweekNightsCounter -= 1;
				totalNightsCounter -= 1;
				if((currentDate + 86400000) <= finalDate - 86400000){
					if((currentDate + 172800000) <= finalDate - 86400000){
						if((currentDate + 259200000) <= finalDate - 86400000){
							if(checkTomorrowSpecial(currentDate + 86400000) || checkTomorrowSpecial(currentDate)){
								specialPeriodDuration = 999; 
							}else{
							totalNightsCounter += 3;
							weekendNightsCounter += 3;
							currentDate += (86400000 * 2);
							}
						}else{
							if(checkTomorrowSpecial(currentDate + 86400000) || checkTomorrowSpecial(currentDate)){
								specialPeriodDuration = 999; 
							}else{
							totalNightsCounter += 3;
							weekendNightsCounter += 3;
							currentDate += (86400000 * 2);
							}
						}
					}else{
						if(checkTomorrowSpecial(currentDate)){
							specialPeriodDuration = 999;
						}else{
						totalNightsCounter += 2;
						weekendNightsCounter += 2;
						currentDate += (86400000 * 2);
						}
					}
				}else{
					totalNightsCounter += 1;
					weekendNightsCounter += 1;
					currentDate += (86400000 * 2);
				}
		}
		if(new Date(currentDate).getDay() == 6){
			midweekNightsCounter -= 1;
			totalNightsCounter -= 1;
			if((currentDate + 86400000) <= finalDate - 86400000){
				if((currentDate + 172800000) <= finalDate - 86400000){
					if(checkTomorrowSpecial(currentDate)){
						specialPeriodDuration = 999; 
					}else{
					totalNightsCounter += 2;
					weekendNightsCounter += 2;
					currentDate += 86400000;
					}
				}else{
					totalNightsCounter += 1;
					weekendNightsCounter += 2;
					midweekNightsCounter -= 1;
				}
			}else{
				if(checkTomorrowSpecial(currentDate)){
					specialPeriodDuration = 999;
				}else{
				totalNightsCounter += 1;
				weekendNightsCounter += 1;
				currentDate += 86400000;
				}
			}
		}
		totalNightsCounter++;
		midweekNightsCounter++;
		currentDate += 86400000;
	}
	messageString = 'Total nights :'+totalNightsCounter+': \nMidweek nights :'+midweekNightsCounter+': \nWeekend nights :'+weekendNightsCounter + ':Special period cost: '+specialPeriodCost+':\n Special period alert :' +specialPeriodAlert + ':Special Period Duration:' + specialPeriodDuration;
		return messageString;
}

function checkPcDateVServerDate(){
	var acceptableTimeLag = 4 * 86400000;
	if(Math.abs(pcToday.getTime() - serverToday.getTime()) > acceptableTimeLag){
		return 1;
	}
	return 0;
}

function AddDays(strDate,intDays){
     strDate = Date.parse(strDate);
     strDate = parseInt(strDate, 10);
     strDate = strDate + intDays*(24*60*60*1000);
     strDate = new Date(strDate);
     //alert(strDate);
	 return strDate;
}
//Copyright Cybermen Solutions 2002.