// la protection  '// ¤¤¤¤¤¤¤¤¤¤¤' devant certaine lignes est pour bloquer le "double date"

// define months in an array: shortname month, longname month, number of days in month
//var whatsamonth = [["Jan", "January", "31"],["Feb", "February", "28"],["Mar", "March", "31"],["Apr", "April", "30"],["May", "May", "31"],["Jun", "June","30"],["Jul", "July", "31"],	["Aug", "August", "31"],	["Sep", "September", "30"],	["Oct", "October", "31"],["Nov", "November", "30"],["Dec", "December", "31"]];
var whatsamonth = [["Jan", "Janvier", "31"],["Fev", "Fevrier", "28"],["Mar", "Mars", "31"],["Avr", "Avril", "30"],["Mai", "Mai", "31"],["Jui", "Juin","30"],["Jui", "Juillet", "31"],	["Aou", "Août", "31"],	["Sep", "Septembre", "30"],	["Oct", "Octobre", "31"],["Nov", "Novembre", "30"],["Dec", "Decembre", "31"]];
	
// days of the week in an array: week starts on Sunday: shortname day, longname day
//var whatsaday = [["Sun", "Sunday"],["Mon", "Monday"],["Tue", "Tuesday"],["Wed", "Wednesday"],["Thu", "Thursday"],["Fri", "Friday"],["Sat", "Saturday"]];
var whatsaday = [["Dim", "Dimanche"],["Lun", "Lundi"],["Mar", "Mardi"],["Mer", "Mercredi"],["Jeu", "Jeudi"],["Ven", "Vendredi"],["Sam", "Samedi"]];


var path_to_image="./images/"

// variable that disables the calendar until it is properly loaded in.
var initDone = false;

// Zeller's algorithm for day of week: pass day month year to the function and you get day of the week back, zero based, starting on Sunday	
function whatDay(day,month,year) {
	if (month >= 3){month -= 2;}
	else{month += 10;}
	if ( (month == 11) || (month == 12) ){year -= 1;}
	var centuries = parseInt(year / 100);
	var x = year % 100;
	var weekday = parseInt(2.6 * month - .2);open
	weekday +=  parseInt(day + x);
	weekday += x / 4;	
	weekday = parseInt(weekday);
	weekday += parseInt(centuries / 4);
	weekday -= parseInt(2 * centuries);
	weekday %= 7;
	if(year >= 1700 && year <= 1751) {weekday -= 3;}
	else {if(year <= 1699) {weekday -= 4;}}
	if(weekday < 0){weekday += 7;}
	return weekday;
}

// function that gets todays date from the select boxes set by the bookingdates weblet. does this only ones, when the page loads
// it then stores todays date in a separate form for use later on
function setToday() {
	if (document.form_store.done.value == '0') {
		document.form_store.init_day.value = document.form_res.inDateDay.selectedIndex;
		document.form_store.init_month.value = parseInt(document.form_res.inDateMonth.options[document.form_res.inDateMonth.selectedIndex].value, 10) - 1;
		document.form_store.done.value = '1';
	}
}

// function gets todays date from the form that stores todays date, and turns it into a useful format. it returns an array containing:
// day of the week, day, month, year, month as a number (numbers are all zero-based)
// there is also a little subroutine that adjusts the number of days in February should this year be a leap year.
function getToday() {
	
	var ladate=new Date();
	var thisday = parseInt(document.form_store.init_day.value) + 1; 
	var thismonthint =  parseInt(document.form_store.init_month.value) + 1;
	var thismonth = (thismonthint ? whatsamonth[thismonthint - 1][1] : 1);
	var thisyear = ladate.getFullYear(); //document.form_store.init_year_SELECTED.value;
	if ( thisyear % 4 == 0 ) {
		whatsamonth[1][2] = 29;
		if ( thisyear % 100 == 0 && thisyear % 400 != 0 ) {
			whatsamonth[1][2] = 28;
		}
	}
	var dayofweekint = whatDay(thisday,thismonthint,thisyear);
	var dayofweek = whatsaday[dayofweekint][1];
	var today =[dayofweek,thisday,thismonth,thisyear,thismonthint];
	initDone = true;
	return today;
}
	
// function prepares a new pop up window for the calendar to be opened in. The content is prepared
// by callin makeCalString function
function openCalendar ( whichmonth , arrivalOrDeparture , doSelected ) {
	var calstring = makeCalString( whichmonth , arrivalOrDeparture , doSelected );
	var calwindow = window.open("","Calendar","width=180,height=200,resizable=0,scrollbars=no,location=no,top=350,left=300");
	calwindow.document.open();
	calwindow.document.write(calstring);
	calwindow.document.close();
	calwindow.focus();
}

// This is basically where you set lots of the HTML, ie what the calendar should look like.
// The actual content of the calendar is called by writeCalendar()
// Stylesheet has to be in line because of a bug in IE 5 on Mac: the popped up window doesn't actually have a URL associated with it. Other option is to hardcode a full link to the
// the stylesheet, but this causes maintenance hassle.
function makeCalString( whichmonth , arrivalOrDeparture , doSelected ) {
	var thismonth = parseInt(getToday()[4]) - 1;
	var themonth = parseInt(whichmonth);
	var thisyear = parseInt(getToday()[3]);
	var linkpast = '<a href="javascript:opener.openCalendar(' + ( themonth - 1) + ' , \'' + arrivalOrDeparture + '\' , \'do\');"><font size=1 face=verdana><b>&#171;</b></font></a>';
	var linkfuture = '<a href="javascript:opener.openCalendar(' + ( themonth + 1) + ' , \'' + arrivalOrDeparture + '\' , \'do\');"><font size=1 face=verdana><b>&#187;</b></font></a>';
	var theyear= thisyear;
	if (themonth== thismonth) {
		linkpast ='<img src="'+path_to_image+'trsp.gif" width="16" height="16" border="0">';
	}
	if (themonth>10) {
		linkfuture = '<a href="javascript:opener.openCalendar(' + ( themonth -11 ) + ' , \'' + arrivalOrDeparture + '\' , \'do\');"><font size=1 face=verdana><b>&#187;</b></font></a>';
	}
	if (themonth< thismonth) {
		theyear = theyear + 1;
	}
	if (themonth == (thismonth -1)) {
		linkfuture = '<img src="'+path_to_image+'trsp.gif" width="16" height="16" border="0">';
	}
	if (themonth == 0) {
		var linkpast = '<a href="javascript:opener.openCalendar( 11 , \'' + arrivalOrDeparture + '\');"><font size=1 face=verdana><b>&#171;</b></font></a>';
	}
	var calstring = '<html><head><title>' + getToday()[0] + ' ' + getToday()[1] + ' ' + getToday()[2].substring(0,3) +  '</title></head><style>.text{color:#000000;font-size:8pt;font-family:Verdana,Arial,Helvetica;font-weight:normal;}.textPast{color:#777777;font-size:8pt;font-family:Verdana,Arial,Helvetica;font-weight:normal;}.textTitle{color:#878979;font-size:8pt;font-family:Arial,Helvetica,sans-serif;}.textToday{color:#000000;font-size:8pt;font-family:Verdana;font-weight:bold;}.textSelected{color:#FFFFFF;font-size:8pt;font-family:Verdana;}A:link{COLOR:#000000;text-decoration:none;}A:active{COLOR:#000000;text-decoration:none;}A:visited{COLOR:#000000;text-decoration:none;}A:hover{COLOR:#000000;text-decoration:none;}A.white:link{COLOR:#FFFFFF;text-decoration:none;}A.white:active{COLOR:#FFFFFF;text-decoration:none;}A.white:visited{COLOR:#FFFFFF;text-decoration:none;}A.white:hover{COLOR:#FFFFFF;text-decoration:none;}A.blueLink:link{COLOR:#336699;text-decoration:none;font-size:8pt;font-family:Arial,Helvetica,sans-serif;}A.blueLink:active{COLOR:#336699;text-decoration:none;font-size:8pt;font-family:Arial,Helvetica,sans-serif;}A.blueLink:visited{COLOR:#336699;text-decoration:none;font-size:8pt;font-family:Arial,Helvetica,sans-serif;}A.blueLink:hover{COLOR:#336699;text-decoration:none;font-size:8pt;font-family:Arial,Helvetica,sans-serif;}.hdrA{border-width:1px;border-color:black;border-style:solid;}</style><body bgcolor="#FFFFFF"><div align="center"><table width="150" border="0" cellspacing="0" cellpadding="0" bgcolor="#E0E2CC"><tr><td bgcolor="#C7C9B5" align="center" class="hdrA" width="16">' + linkpast + '</td><td><img src="'+path_to_image+'trsp.gif" width="1" height="1" border="0"></td><td bgcolor="#C7C9B5" align="center" class="hdrA" width="114"><span class="textTitle">' + ' ' + whatsamonth[whichmonth][1] + ' ' + theyear + '</span><br><img src="'+path_to_image+'trsp.gif" width="114" height="1" border="0"></td><td><img src="'+path_to_image+'trsp.gif" width="1" height="1" border="0"></td><td bgcolor="#C7C9B5" align="center" class="hdrA" width="16">' + linkfuture + '</td></tr><tr><td colspan="5"><table width="150" border="0" cellspacing="2" cellpadding="2"><tr><td align="center"><span class="text">D</span></td><td align="center"><span class="text">L</span></td><td align="center"><span class="text">M</span></td><td align="center"><span class="text">M</span></td><td align="center"><span class="text">J</span></td><td align="center"><span class="text">V</span></td><td align="center"><span class="text">S</span></td></tr></table></td></tr><tr><td bgcolor="#000000" height="1" colspan="5"><img src="'+path_to_image+'trsp.gif" width="1" height="1" border="0"></td></tr><td><tr><td colspan="5"><table width="150" border="0" cellspacing="2" cellpadding="2">';
	calstring = calstring + writeCalendar( whichmonth , arrivalOrDeparture , doSelected );
	calstring = calstring + '</table></td></tr><tr><td colspan="5" bgcolor="#000000" height="1"><img src="'+path_to_image+'trsp.gif" width="1" height="1" border="0"></td></tr>';
	calstring = calstring + '<tr><td colspan="5" align="center"><table width="100" border="0" cellspacing="2" cellpadding="2"><tr><td align="center"><a href="javascript:void(self.close());" class="blueLink">Fermer la fenêtre</a></td></tr></table></td></tr>';
	calstring = calstring + '</table></div></body></html>';
	return calstring;
}
	
// writes the dynamic bit of the calendar
function writeCalendar( themonth , arrivalOrDeparture , doSelected ) {
	var start = 0;
	var linkClass = '';
	if ( doSelected ) {
		var selectedDay = 898; // because we then know it will never be equal to start + 1 - startDay, so none of the days will show up as selected
	} else {
		// ¤¤¤¤¤¤¤¤¤¤¤ var selectedDay = ( arrivalOrDeparture == 'arrival' ) ? document.form_res.inDateDay.selectedIndex + 1 : document.form_res.outDateDay.selectedIndex + 1;
	}
	var today = getToday()[1] - 1;
	var thismonth = getToday()[4] - 1;
	var whichmonth =parseInt(themonth);
	var whichyear = parseInt(getToday()[3]);
	if (whichmonth < thismonth) {
		whichyear = whichyear + 1;
	}
	var numberofdays = whatsamonth[whichmonth][2];
	var startday = whatDay(1,(whichmonth + 1),whichyear);
	var endmonth = startday + parseInt(numberofdays) - 1;
	var calstring = '<tr>';
	var setmonth = whichmonth - thismonth;
	if ( setmonth < 0 ) {
		setmonth = setmonth + 12;
	}
	if (thismonth != whichmonth) {
		for (start ; start < 42 ; start++) {		
			if ( start == 7 || start == 14 || start == 21 || start == 28 || start == 35) {
				calstring = calstring + '</tr><tr>';
			}
			if  ( start < startday || start > endmonth ) {
				calstring = calstring + '<td bgcolor="#FFFFFF" align="center"><span class="text">&nbsp;</span></td>';
			} else if ( ( start + 1 - startday ) == selectedDay ) {
				calstring = calstring + '<td bgcolor="#068EBB" align="center"><span class="textSelected"><a href="javascript:opener.setDropDown( \'' + arrivalOrDeparture + '\' , ' + (start  - startday) + ' ,  ' + setmonth + ' );self.close();" class="white">' + (start + 1 - startday) + '</a></span></td>';
			} else {
				calstring = calstring + '<td bgcolor="#FFFFFF" align="center"><span class="text"><a href="javascript:opener.setDropDown( \'' + arrivalOrDeparture + '\' , ' + (start  - startday) + ' ,  ' + setmonth + ' );self.close();">' + (start + 1 - startday) + '</a></span></td>';					
			}			
		}
	} else {
		for (start ; start < 42 ; start++) {		
			if ( start == 7 || start == 14 || start == 21 || start == 28 || start == 35) {
				calstring = calstring + '</tr><tr>';
			}
			if ( ( start + 1 - startday ) == selectedDay ) {
				calstring += '<td bgcolor="#068EBB" align="center">';
				linkClass = ' class="white"';
			} else {
				calstring += '<td bgcolor="#FFFFFF" align="center">';
				linkClass = '';
			}
			if  ( start < startday || start > endmonth ) {
				calstring = calstring + '<span class="text">&nbsp;</span></td>';
			} else if ( start < ( today + startday ) ) {
				calstring = calstring + '<span class="textPast">' + (start + 1 - startday) + '</span></td>';
			} else if (start > ( today + startday ) ) {
				calstring = calstring + '<span class="text"><a href="javascript:opener.setDropDown( \'' + arrivalOrDeparture + '\' , ' + (start  - startday) + ' ,  ' + setmonth + ' );self.close();"' + linkClass + '>' + (start + 1 - startday) + '</a></span></td>';					
			} else {
				calstring = calstring + '<span class="textToday"><a href="javascript:opener.setDropDown( \'' + arrivalOrDeparture + '\' , ' + (start  - startday) + ' ,  ' + setmonth + ' );self.close();"' + linkClass + '>' + (start + 1 - startday) + '</a></span></td>';
			}			
		}
	}
	calstring = calstring + '</tr>';
	return calstring ;
}	

// sets the right dropdownbox according to what you've chosen in the calendar, and if the calendar is supposed to set departure or arrival.
function setDropDown ( arrivalOrDeparture , day , month ) {
	//alert(day+'---'+month);
	if ( arrivalOrDeparture == 'arrival' ) {
		document.form_res.inDateDay.selectedIndex = day;
		document.form_res.inDateMonth.selectedIndex = month;
		if ( changeDate( document.form_res.inDateDay , document.form_res.inDateMonth , document.form_res.inDayOfWeek ) ) {
			// ¤¤¤¤¤¤¤¤¤¤¤ ensureBefore( document.form_res.inDateDay , document.form_res.inDateMonth , document.form_res.outDateDay , document.form_res.outDateMonth , document.form_res.outDayOfWeek );
		}
	} else if ( arrivalOrDeparture == 'departure' ) {
				// ¤¤¤¤¤¤¤¤¤¤¤ document.form_res.outDateDay.selectedIndex = day;
				// ¤¤¤¤¤¤¤¤¤¤¤ document.form_res.outDateMonth.selectedIndex = month;
				// ¤¤¤¤¤¤¤¤¤¤¤ changeDate( document.form_res.outDateDay , document.form_res.outDateMonth , document.form_res.outDayOfWeek );
	}
}	

// this is specific to Le Meridien ... calls the necessary function to ensure the arrival date is before the departure date (but only in some cases?), 
// and to make sure the day of week box is updated
function handleOnChange () {
	if ( changeDate( this , document.form_res.inDateMonth , document.form_res.inDayOfWeek ) ) {
				// ¤¤¤¤¤¤¤¤¤¤¤ ensureBefore( this , document.form_res.outDateMonth , document.form_res.outDateDay , document.form_res.outDateMonth , document.form_res.outDayOfWeek );
	}
}


// function that checks whether you have chosen a valid date. If the date you have chosen is invalid, throws an error message and reverts to todays date
function checkValidDate(formDay,formMonth) {
	 
	var chosenMonth = parseInt(formMonth.options[formMonth.selectedIndex].value, 10);
	var chosenDay =  parseInt(formDay.options[formDay.selectedIndex].value, 10);	
	//alert(chosenDay+'---'+chosenMonth);
	var daysInChosenMonth = whatsamonth[chosenMonth - 1][2];
	
	if ( daysInChosenMonth < chosenDay ) {
		alert(whatsamonth[chosenMonth - 1][1] + ' a seulement ' + daysInChosenMonth + ' jours.\nMerci de choisir une date valide.' );
		formDay.selectedIndex = document.form_store.init_day.value;
		for ( var i = 0 ; i < formMonth.options.length ; i++ ) {
			if ( ( formMonth.options[i].value - 1 ) == document.form_store.init_month.value ) {
				formMonth.selectedIndex = i;
			}
		}
	}
}
