/*********************************************************************************************************************

                              Date picker written by Mark Wilton-Jones 5-8/10/2002

Please see http://www.howtocreate.co.uk/jslibs/ for details and a demo of this script
Please see http://www.howtocreate.co.uk/jslibs/termsOfUse.html for terms of use

To use:

	Put this in between the <head> tags:

<script src="calendar_popup.js" type="text/javascript" language="javascript1.2"></script>

You can now run the calendar using:

<script type="text/javascript" language="javascript1.2"><!--

openCalendar(); //opens or redraws the calendar window

resetDate();    //does NOT open/redraw the calendar window, just resets it to this year, this month

//you can also change the current date using:

curDate.setYear(yearIn4DigitFormat);
curDate.setMonth(monthFrom0To11);

//that does NOT open/redraw the calendar window, just resets it to the relevant year/month

//Note that with Escape, there is a bug that may require users to close then open the popup window
//for the calendar to refresh after they change month/year. Resetting the date every time the calendar
//is opened will make it impossible to use.

function datePickerReturn( oDay, oMonth, oYear ) {
	//this function will be called when the user finishes picking a date
	//you will want to do something with the day, month and year
	alert( 'The user chose ' + oDay + '/' + oMonth + '/' + oYear );
}

//--></script>

If you use inline events, this script can position the popup as close to the mouse as possible:

<a href="#" onclick="return false;" onmouseup="openCalendar(arguments[0]);return false;">Choose date</a>

*********************************************************************************************************************/
function datePickerReturn( oDay, oMonth, oYear ) {
	//this function will be called when the user finishes picking a date
	//you will want to do something with the day, month and year
	//alert( 'The user chose ' + oDay + '/' + oMonth + '/' + oYear );
	document.hotelSearch.day.value = oDay;
	document.hotelSearch.month.value = oMonth;
	document.hotelSearch.year.value = oYear;
}

function MWJ_findSelect( oName, oDoc ) { //get a reference to the select box using its name
	if( !oDoc ) { oDoc = window.document; }
	for( var x = 0; x < oDoc.forms.length; x++ ) { if( oDoc.forms[x][oName] ) { return oDoc.forms[x][oName]; } }
	for( var x = 0; document.layers && x < oDoc.layers.length; x++ ) { //scan layers ...
		var theOb = MWJ_findObj( oName, oDoc.layers[x].document ); if( theOb ) { return theOb; } }
	return null;
}

function dateChange() {
	d = document.hotelSearch.day;
	m = document.hotelSearch.month;
	y = document.hotelSearch.year;
	//work out if it is a leap year
	var IsLeap = parseInt( y.options[y.selectedIndex].value );
	IsLeap = !( IsLeap % 4 ) && ( ( IsLeap % 100 ) || !( IsLeap % 400 ) );
	//find the number of days in that month
	IsLeap = [31,(IsLeap?29:28),31,30,31,30,31,31,30,31,30,31][m.selectedIndex];
	//store the current day - reduce it if the new month does not have enough days
	var storedDate = ( d.selectedIndex > IsLeap - 1 ) ? ( IsLeap - 1 ) : d.selectedIndex;
	//while( d.options.length ) { d.options[0] = null; } //empty days box then refill with correct number of days
	d.options.length = 0;
	for( var x = 0; x < IsLeap; x++ ) { d.options[x] = new Option( x + 1, x + 1 ); }
	d.options[storedDate].selected = true; //select the number that was selected before
	if( window.opera && document.importNode ) { window.setTimeout('MWJ_findSelect( \''+d.name+'\' ).options['+storedDate+'].selected = true;',0); }
	curDate.setMonth(m.value - 1);
	curDate.setYear(y.value);
}

var monthList = ['January','February','March','April','May','June','July','August','September','October','November','December'];
var curDate = new Date(); curDate.setDate(1); curDate.getTheYear = curDate.getFullYear ? curDate.getFullYear : curDate.getFullYear;
//the form buttons don't work in NS4 because of bugs
var MWJNS4 = document.layers && !document.all && navigator.mimeTypes['*'];
//Opera 7, Opera 6 Mac/Linux, Konqueror and Safari's security means that you cannot write to about:blank as it is counted as a different domain
//Escape does not recognise about:blank (it uses its own, but it occasionally makes the popup go blank) but it does understand ''
var opOrEscape = window.opera || ( document.layers && !navigator.mimeTypes['*'] ) || navigator.vendor == 'KDE' || ( document.childNodes && !document.all && !navigator.taintEnabled ) || ( window.ScriptEngine && ScriptEngine() == 'JScript' && navigator.platform == 'Win32' && window.ActiveXObject && !navigator.__ice_version );

function openCalendar(e) {
	var sX = 0, sY = 0; if( !e ) { e = window.event; }
	if( e && ( e.screenX || e.screenY ) && screen.availWidth ) { sX = e.screenX; sY = e.screenY; }
	if( sX > screen.availWidth - 271 ) { sX = screen.availWidth - 271; }
	if( sY > screen.availHeight - 308 ) { sY = screen.availHeight - 308; }
	doReDraw(sX,sY);
}

function MWJ_dropYear() {
	if (curDate.getTheYear() > 2006) {
		curDate.setYear( curDate.getTheYear() - 1 );
		doReDraw();
	}
}
function MWJ_raiseYear() {
	if (curDate.getTheYear() < 2015) {
		curDate.setYear( curDate.getTheYear() + 1 );
		doReDraw();
	}
}
function MWJ_dropMonth() { if( curDate.getMonth() ) { curDate.setMonth( curDate.getMonth() - 1 ); } else { curDate.setMonth(11); curDate.setYear( curDate.getTheYear() - 1 ); } doReDraw(); }
function MWJ_raiseMonth() { if( curDate.getMonth() < 11 ) { curDate.setMonth( curDate.getMonth() + 1 ); } else { curDate.setMonth(0); curDate.setYear( curDate.getTheYear() + 1 ); } doReDraw(); }
function resetDate() {
	curDate.setTime((new Date()).getTime());
	document.hotelSearch.day.value = curDate.getDate();
	document.hotelSearch.month.value = curDate.getMonth() + 1;
	document.hotelSearch.year.value = curDate.getFullYear();
}

function doReDraw(sX,sY) {
	var theContent = 
		'<html><head><title>Date picker</title><style type="text/css"><!--\n'+
		'a { text-decoration: none; }\n'+
		'td, th { font-family: \'Times New Roman\', Times, Serif; }\n'+
		'a.MWJCAL { display: block; width: 100%; font-weight: bold; }\n'+
		'a.MWJCAL:hover { background-color: #ba313c; color: #ffffff; }\n--></style></head>'+
		'<body bgcolor="#ebead7" text="#ffffff" link="#00007f" alink="#00007f" vlink="#00007f">'+
		'<form onsubmit="return false;"><table border="1" cellpadding="2" cellspacing="1" width="233">'+
		'<tr><td bgcolor="#ffffff" colspan="7"><table border="0" cellpadding="2" cellspacing="1" width="100%">'+
		'<tr><td bgcolor="#323c76">'+(MWJNS4?'<a href="javascript:if( window.opener && window.opener.MWJ_dropYear ) { window.opener.MWJ_dropYear(); } else { window.alert( \'Error:\\nCould not communicate with opening window.\' ); }"><font color="#ffffff"><<</font></a>':'<input type="button" value="<<" onclick="if( window.opener && window.opener.MWJ_dropYear ) { window.opener.MWJ_dropYear(); } else { window.alert( \'Error:\\nCould not communicate with opening window.\' ); }">')+'</td><td bgcolor="#323c76" align="center"><b>'+curDate.getTheYear()+'</b></td><td bgcolor="#323c76" align="right">'+(MWJNS4?'<a href="javascript:if( window.opener && window.opener.MWJ_raiseYear ) { window.opener.MWJ_raiseYear(); } else { window.alert( \'Error:\\nCould not communicate with opening window.\' ); }"><font color="#ffffff">>></font></a>':'<input type="button" value=">>" onclick="if( window.opener && window.opener.MWJ_raiseYear ) { window.opener.MWJ_raiseYear(); } else { window.alert( \'Error:\\nCould not communicate with opening window.\' ); }">')+'</td></tr>'+
		'<tr><td bgcolor="#323c76">'+(MWJNS4?'<a href="javascript:if( window.opener && window.opener.MWJ_dropMonth ) { window.opener.MWJ_dropMonth(); } else { window.alert( \'Error:\\nCould not communicate with opening window.\' ); }"><font color="#ffffff"><<</font></a>':'<input type="button" value="<<" onclick="if( window.opener && window.opener.MWJ_dropMonth ) { window.opener.MWJ_dropMonth(); } else { window.alert( \'Error:\\nCould not communicate with opening window.\' ); }">')+'</td><td bgcolor="#323c76" align="center"><b>'+monthList[curDate.getMonth()]+'</b> <font size="-2">[<a href="javascript:if( window.opener && window.opener.resetDate ) { window.opener.resetDate(); window.opener.doReDraw(); } else { window.alert( \'Error:\\nCould not communicate with opening window.\' ); }"><font color="#bbbbff">Go to now</font></a>]</font></td><td bgcolor="#323c76" align="right">'+(MWJNS4?'<a href="javascript:if( window.opener && window.opener.MWJ_raiseMonth ) { window.opener.MWJ_raiseMonth(); } else { window.alert( \'Error:\\nCould not communicate with opening window.\' ); }"><font color="#ffffff">>></font></a>':'<input type="button" value=">>" onclick="if( window.opener && window.opener.MWJ_raiseMonth ) { window.opener.MWJ_raiseMonth(); } else { window.alert( \'Error:\\nCould not communicate with opening window.\' ); }">')+'</td></tr>'+
		'</table></td></tr>'+
		'<tr><th bgcolor="#0066ff">Sun</th><th bgcolor="#0066ff">Mon</th><th bgcolor="#0066ff">Tue</th><th bgcolor="#0066ff">Wed</th><th bgcolor="#0066ff">Thu</th><th bgcolor="#0066ff">Fri</th><th bgcolor="#0066ff">Sat</th></tr>'
	;
	for( var x = 1; x <= [31,((!( curDate.getTheYear() % 4 ) && ( ( curDate.getTheYear() % 100 ) || !( curDate.getTheYear() % 400 ) ))?29:28),31,30,31,30,31,31,30,31,30,31][curDate.getMonth()]; x++ ) {
		curDate.setDate(x);
		if( x == 1 && curDate.getDay() ) { theContent += '<tr><td colspan="'+curDate.getDay()+'"></td>'; }
		theContent += ( ( !curDate.getDay() ) ? '<tr>' : '' ) + '<td align="center" bgcolor="#'+((curDate.getMonth()==(new Date()).getMonth()&&curDate.getFullYear()==(new Date()).getFullYear()&&x==(new Date()).getDate())?'ffff99':((!curDate.getDay()||curDate.getDay()==6)?'cccccc':'ffffff'))+'"><a class="MWJCAL" href="javascript:if( window.opener && window.opener.datePickerReturn ) { void(window.opener.setTimeout(\'datePickerReturn('+x+','+(curDate.getMonth()+1)+','+curDate.getTheYear()+');\',1)); window.close(); } else { window.alert( \'Error:\\nCould not communicate with opening window.\' ); }">'+x+'</a></td>';
	} curDate.setDate(1);
	theContent += '</table><table width=\"100%\"><tr><td align=\"center\"><a class=\"MWJCAL\" href="javascript:window.close();">Close Window</a></td></tr></table></form></body></html>';
	var oDoc = window.open( ( opOrEscape ? '' : 'about:blank' ), 'MWJ_TOFR', 'width=259,height=277'+(sY?(',top='+sY):'')+(sX?(',left='+sX):'')+',resizable=1,location=0,menubar=0,scrollbars=0,status=0,toolbar=0' );
	oDoc.document.open(); oDoc.document.write(theContent); oDoc.document.close(); window.MWJwinStore = oDoc;
	//these must be done after open-write-close or the window cannot be written to in IE4 Mac
	if( oDoc.focus ) { oDoc.focus(); } if( ( sX || sY ) && oDoc.moveTo ) { oDoc.moveTo(sX,sY); }
}

if( !window.onunload ) { window.onunload = function () { if( window.MWJwinStore && !window.MWJwinStore.closed ) { window.MWJwinStore.close(); } }; }
