/********************************************************************************************************
                                         Valid month script
                               Written by Mark Wilton-Jones, 6-7/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

This script monitors years and months and makes sure that the correct number of days are provided.

To use:

Inbetween the <head> tags, put:

	<script src="PATH TO SCRIPT/validmonth.js" type="text/javascript" language="javascript1.2"></script>

To have a static year box (only allows the years defined in the HTML)

	Day of month select box should be in the format:
		<select name="day" size="1">
		<option value="1" selected>1</option>
		<option value="2">2</option>
		...
		<option value="31">31</option>
		</select>

	Month select box should be in the format:
		<select name="month" size="1" onchange="dateChange('day','month','year');">
		<option value="January" selected>January</option>
		<option value="February">February</option>
		...
		<option value="December">December</option>
		</select>

	Year select box should be in the format:
		<select name="year" size="1" onchange="dateChange('day','month','year');">
		<option value="1980" selected>1980</option>
		<option value="1981">1981</option>
		...
		<option value="2010">2010</option>
		</select>

	You can now use:
		setToday('day','month','year');
	to set the date to today's date (after the page has loaded)

To have an extendible year box (creates dates lower/higher than the current range)

	Year select box should be in the format:
		<select name="year" size="1" onchange="checkMore( this, 1980, 2005, 1840, 2010 );dateChange('day','month','year');">
		<option value="MWJ_DOWN">Lower ...</option>
		<option value="1980" selected>1980</option>
		<option value="1981">1981</option>
		...
		<option value="2005">2005</option>
		<option value="MWJ_UP">Higher ...</option>
		</select>
	If you do not want to have higher / lower values, simply omit the relevant option

	Function format:
		checkMore( this, CURRENT LOWEST YEAR, CURRENT HIGHEST YEAR, LOWEST POSSIBLE YEAR, HIGHEST POSSIBLE YEAR )

	You can now use:
		reFill( 'year', 1980, 2005, true, true );setToday('day','month','year');
	to set the date to today's date (after the page has loaded)

	Function format (make sure the range of years includes the current year):
		reFill( name of year select box, LOWEST YEAR, HIGHEST YEAR, ALLOW HIGHER (true/false), ALLOW LOWER (true/false) )
_____________________________________________________________________________________________________________________*/

//Opera 7 has a bug making it fail to set selectedIndex after dynamic generation of options unless there is a 0ms+ delay
//I have put fixes in in all necessary places

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, m, y ) {
	d = MWJ_findSelect( d ), m = MWJ_findSelect( m ), y = MWJ_findSelect( y );
	//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
	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); }
}

// REMOVED THE ADDING MORE YEARS FUNCTION (NOT USING IT)

function reFill( y, oBot, oTop, oDown, oUp ) {
	y = MWJ_findSelect( y ); y.nowBot = oBot; y.nowTop = oTop;
	//empty and refill the select box using the range of numbers specified
	while( y.options.length ) { y.options[0] = null; }
	if( oDown ) { y.options[0] = new Option('Lower ...','MWJ_DOWN'); }
	for( var x = oBot; x <= oTop; x++ ) { y.options[y.options.length] = new Option(x,x); }
	if( oUp ) { y.options[y.options.length] = new Option('Higher ...','MWJ_UP'); }
}
