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

                              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="calendarpopup.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>

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

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.getYear;
//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() { curDate.setYear( curDate.getTheYear() - 1 ); doReDraw(); }
function MWJ_raiseYear() { 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()); }

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: #99ff99; }\n--></style></head>'+
		'<body bgcolor="#bfbfbf" 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">&lt;&lt;</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">&gt;&gt;</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">&lt;&lt;</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">&gt;&gt;</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="#6ab49f">Sun</th><th bgcolor="#6ab49f">Mon</th><th bgcolor="#6ab49f">Tue</th><th bgcolor="#6ab49f">Wed</th><th bgcolor="#6ab49f">Thu</th><th bgcolor="#6ab49f">Fri</th><th bgcolor="#6ab49f">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.getYear()==(new Date()).getYear()&&x==(new Date()).getDate())?'ffbbdd':((!curDate.getDay()||curDate.getDay()==6)?'ffffbb':'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></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(); } }; }