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

                              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) {
	doReDraw();
}

function closeCalendar(){
    var datePickerHtmlNode = document.getElementById("datePicker");
    datePickerHtmlNode.style.display='none';        
}

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
    var date = oDay+"-"+oMonth+"-"+oYear;
    document.getElementById("remindDate").innerHTML = date;
    document.getElementById("remindDateField").value = date;
    closeCalendar();
}


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() {
	var theContent = 
		'<table>'+
		'<tr class="month"><th>'+
        '<input type="button" value="<<" onclick="MWJ_dropMonth();">'+
        '</th><th colspan="5">'+monthList[curDate.getMonth()]+' '+curDate.getTheYear()+' <a href="#" onclick="closeCalendar(); return false;">[X]</a> <!-- <a href="#" onclick="resetDate(); doReDraw(); return false;">Go to now</a>--></th><th>'+
        '<input type="button" value=">>" onclick="MWJ_raiseMonth();">'+
        '</th></tr>'+
		'<tr><th>Sun</th><th>Mon</th><th>Tue</th><th>Wed</th><th>Thu</th><th>Fri</th><th>Sat</th></tr>'
	;
    var monthDays = [31,((!( curDate.getTheYear() % 4 ) && ( ( curDate.getTheYear() % 100 ) || !( curDate.getTheYear() % 400 ) ))?29:28),31,30,31,30,31,31,30,31,30,31];
    
	for( var x = 1; 
         x <= monthDays[curDate.getMonth()]; 
         x++ ){
             
		curDate.setDate(x);
		if( x == 1 && curDate.getDay() ) { theContent += '<tr><td colspan="'+curDate.getDay()+'"></td>'; }
		theContent += ( ( !curDate.getDay() ) ? '<tr>' : '' ) + '<td class="'+((curDate.getMonth()==(new Date()).getMonth()&&curDate.getYear()==(new Date()).getYear()&&x==(new Date()).getDate())?'today':((!curDate.getDay()||curDate.getDay()==6)?'weekend':'normal'))+'"><a class="MWJCAL" href="#" onclick="datePickerReturn('+x+','+(curDate.getMonth()+1)+','+curDate.getTheYear()+'); return false;">'+x+'</a></td>';
	} curDate.setDate(1);
	theContent += '</table>';

    var datePickerHtmlNode = document.getElementById("datePicker");
    datePickerHtmlNode.style.display='block';

    //oDoc.moveTo(sX,sY);
    datePickerHtmlNode.innerHTML = theContent;
    
}

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