/********************************************
 *** Villa Agne website and booking system ***
 ** Written by Ian Collier, Autumn/Winter 2008 **
 ******** This work copyright Ian Collier ********
 *** Please do not copy or modified this work ***
 ** without seeking the express permission of **
 ** the author beforehand.  Thank you *********
 ********************************************/

// function to get the displayed date 
function getdisplayeddate() {
	// get the date
	return document.forms["frmbooking"].txthiddenselecteddate.value;
}

// function to change to previous year
function prevyear() {
	// get displayeddate
	var displayeddate=getdisplayeddate();
	// det the dateparts
	var dateparts=displayeddate.split("/");
	// show the new date, subtracting 1 from the year
	updatedate(dateparts[0], dateparts[1], (parseInt(dateparts[2])-1));
}

// function to change to next year
function nextyear() {
	// get displayeddate
	var displayeddate=getdisplayeddate();
	// det the dateparts
	var dateparts=displayeddate.split("/");
	// show the new date, adding 1 from the year
	updatedate(dateparts[0], dateparts[1], (parseInt(dateparts[2])+1));
}

// function to change to previous month
function prevmonth() {
	// get displayeddate
	var displayeddate=getdisplayeddate();
	// det the dateparts
	var dateparts=displayeddate.split("/");
	// show the previous year and the last month in the year if the current month is 1
	if(dateparts[1]=="01") {		
		updatedate(dateparts[0], "12", (parseInt(dateparts[2])-1));
	}
	else {
		updatedate(dateparts[0], (Math.round(dateparts[1]*100)/100)-1, dateparts[2]);
	}
}

// function to change to previous month
function nextmonth() {
	// get displayeddate
	var displayeddate=getdisplayeddate();
	// det the dateparts
	var dateparts=displayeddate.split("/");
	// show the previous year and the last month in the year if the current month is 1
	if(dateparts[1]=="12") {
		updatedate(dateparts[0], "01", (parseInt(dateparts[2])+1));
	}
	else {
		updatedate(dateparts[0], (Math.round(dateparts[1]*100)/100)+1, dateparts[2]);
	}
}

// function to change the day 
function changeday(newday) {
	// get displayeddate
	var displayeddate=getdisplayeddate();
	var dateparts=displayeddate.split("/");
	// update the displayed date
	updatedate(newday, dateparts[1], dateparts[2]);
}

// function to update the date displayed depending on what the user selected
function updatedate(cday, cmonth, cyear) {
	// check lengths
	if(cday.toString().length==1) {
		var rday="0"+cday.toString();
	}
	else {
		var rday=cday.toString();
	}
	if(cmonth.toString().length==1) {
		var rmonth="0"+cmonth.toString();
	}
	else {
		var rmonth=cmonth.toString();
	}
	// check rday for erroneous leapyears
	// or 30 day months
	if(rday>"28") {
		if(rmonth=="02") {
			if((cyear % 4)==0) {
				rday="29";
			}
			else {
				rday="28";
			}
		}
		else {
			if(rday=="31" && (rmonth=="04" || rmonth=="06" || rmonth=="09" || rmonth=="11")) {
				rday="30";
			}
		}
	}
	// set opencell string
	var openpara="<p style='font-weight : bold;text-align : center;'>";
	// set closepara string
	var closepara="</p>";
	// set newdate string
	var newdatestring=rday+"/"+rmonth+"/"+cyear;
	// set innerhtmlstring
	var innerhtmlstring=openpara+newdatestring+closepara;
// alert(innerhtmlstring);
	// update display with innerhtmlstring
	document.getElementById("changedate").innerHTML=innerhtmlstring;
	// update hidden textbox with newdatestring
	document.forms["frmbooking"].txthiddenselecteddate.value=newdatestring;
	// redraw the grid
	redrawgrid(rday, rmonth, cyear);
}

// redraw the grid for the selected year and month
function redrawgrid(cday, cmonth, cyear) {
	// set number of days in the month
	// if month is 1, 3, 5, 7, 8, 10 or 12, the number of days is 31
	// if month is 4, 6, 9 or 11, the number of days is 30
	// if month is 2 then there are 28 or 29 days, depending on whether or not the year is divisible by 4 with no remainder
	switch(cmonth) {
		case "01" :
			var lastday=31;
			break;
		case "02" :
			// test if year is divisible by 4 with out a remainder
			if((cyear % 4)==0) {
				// leap year
				var lastday=29;
			}
			else {
				// not a leap year
				var lastday=28;
			}
			break;
		case "03" :
			var lastday=31;
			break;
		case "04" :
			var lastday=30;
			break;
		case "05" :
			var lastday=31;
			break;
		case "06" :
			var lastday=30;
			break;
		case "07" :
			var lastday=31;
			break;
		case "08" :
			var lastday=31;
			break;
		case "09" :
			var lastday=30;
			break;
		case "10" :
			var lastday=31;
			break;
		case "11" :
			var lastday=30;
			break;
		case "12" :
			var lastday=31;
			break;
		default : 
			var lastday=0;
			break;
	}
	// check cday
	if(cday>lastday) {
		rday=lastday;
	}
	else {
		rday=cday;
	}
	// initialise a daycount
	var daycount=0;
	// initialize innerthmlstring	
	var innerhtmlstring="<table class='cal'>";		
	// need 5 rows, containg the dates, each with up to 7 days
	for(var x=1;x<=5;x++) {
		innerhtmlstring+="<tr>";
			// draw up to 7 cells for this row		
			for(var y=1;y<=7;y++) {
				// increment daycount
				daycount++;
				innerhtmlstring+="<td>";
					// show the day number
					if(daycount<=lastday) {
						if(daycount==rday) {
							innerhtmlstring+="<p style='text-align : center;'><span class='choosedark' onClick='JavaScript:changeday("+daycount+");'>"+daycount+"</span></p>";
						}
						else {						
							innerhtmlstring+="<p style='text-align : center;'><span class='choose' onClick='JavaScript:changeday("+daycount+");'>"+daycount+"</span></p>";
						}
					}
					else {
						innerhtmlstring+="&nbsp;";
					}
				innerhtmlstring+="</td>";
			}
		innerhtmlstring+="</tr>";
	}
	innerhtmlstring+="</table>";	
	// update the page, replacing the daygrid placeholder witgh innerhtmlstring
	document.getElementById("daygrid").innerHTML=innerhtmlstring;
}

// update previouse page's text box with the newly selected date
function updatepage(frmno,datebox) {
	// get displayeddate
	var displayeddate=getdisplayeddate();
	// update the parent document's textbox with the displayed date
	opener.document.forms[frmno].elements[datebox].value=displayeddate;
}
