function ShowHide( section, shown )
{
	if ( shown )
	{
		document.getElementById( section ).style.display='block';
		document.getElementById( section ).style.visibility='visible';
	}
	else
	{
		document.getElementById( section ).style.display='none';
		document.getElementById( section ).style.visibility='hidden';	
	}
}

function numberDropdown( name, start, end, interval, defaultValue )
{
	oSelect = document.createElement("select");
	oSelect.setAttribute( "size", 1 );
	oSelect.setAttribute( "name", name );
	oSelect.setAttribute( "id", name );
	
	for ( var i = start; i <= end; i += interval )
	{
		oOption = document.createElement("option");
		oOption.appendChild( document.createTextNode( i ) );
		
		var stri = String( i );
		//if ( stri.length < 2 )
		//{
		//	stri = "0" + i;
		//}
		
		oOption.setAttribute( "value", stri );
		if ( i == defaultValue )
		{
			oOption.setAttribute( "selected", "selected" );
		}
		oSelect.appendChild( oOption );
	}

	return oSelect;	
}

function dateEntryFields( entryNum, oInitialDate, type )
{
	var day = oInitialDate.getDate();
	var month = oInitialDate.getMonth();
	var year = oInitialDate.getFullYear();
	
	var dropdownYear = (new Date()).getFullYear();
	
	var numdays = new Array( 31, ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0 ? 29 : 28), 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 );
		
	var dayDropdown = numberDropdown( "day" + type + "[" + entryNum + "]", 1, numdays[ month ], 1, day );
	var monthDropdown = numberDropdown( "month" + type + "[" + entryNum + "]", 1, 12, 1, month + 1 );
	var yearDropdown = numberDropdown(  "year" + type + "[" + entryNum + "]", dropdownYear, dropdownYear + 5, 1, year );

	if ( document.attachEvent )
	{
		dayDropdown.param1 = entryNum;
		dayDropdown.param2 = "date" + type + "_";
		dayDropdown.param3 = type;
		dayDropdown.attachEvent( "onchange", IEsetDays );
		
		monthDropdown.param1 = entryNum;
		monthDropdown.param2 = "date" + type + "_";
		monthDropdown.param3 = type;
		monthDropdown.attachEvent( "onchange", IEsetDays );
		
		yearDropdown.param1 = entryNum;
		yearDropdown.param2 = "date" + type + "_";
		yearDropdown.param3 = type;
		yearDropdown.attachEvent( "onchange", IEsetDays );
	}
	else
	{
		dayDropdown.setAttribute( "onchange", "setDays( " + entryNum + ", 'date" + type + "_', " + type + " );" );
		monthDropdown.setAttribute( "onchange", "setDays( " + entryNum + ", 'date" + type + "_', " + type + " );" );
		yearDropdown.setAttribute( "onchange", "setDays( " + entryNum + ", 'date" + type + "_', " + type + " );" );
	}
	
	oSpan = document.createElement("span");
	oSpan.setAttribute( "id", "date" + type + "_" + entryNum );		
	oSpan.appendChild( dayDropdown );
	oSpan.appendChild( monthDropdown );
	oSpan.appendChild( yearDropdown );

	return oSpan;
}

function IEsetDays()
{
	sender = event.srcElement;
	setDays( sender.param1, sender.param2, sender.param3 );
}

function setDays( entryNum, spanName, type )
{
	var oldSpan = document.getElementById( spanName + entryNum );
	
	var day = document.getElementById( "day" + type + "[" + entryNum + "]" ).value;
	var month = parseInt( document.getElementById( "month" + type + "[" + entryNum + "]" ).value ) - 1;
	var year = document.getElementById( "year" + type + "[" + entryNum + "]" ).value;
	
	var date = new Date( year, month, day );
	
	//Check the date selected is valid
	if ( date.getMonth() != month )
	{
		date = new Date( year, month, 1 );
	}
	
	oldSpan.parentNode.replaceChild( dateEntryFields( entryNum, date, type ), oldSpan );
	
	//if the range is a fixed date, set the second dates to the new date
	if ( type == 1 )
	{
		var checkInput = document.getElementById( "rangeCheck" + entryNum );
		var oldSpan2 = document.getElementById( "date2_" + entryNum );
		
		if ( !checkInput.checked )
		{
			oldSpan2.parentNode.replaceChild( dateEntryFields( entryNum, date, 2 ), oldSpan2 );
			
			changeIsRange2( entryNum, false );
		}		
	}
}

function dateRangeCheckBox( entryNum, checked )
{
	var checkBox = document.createElement("input");
	checkBox.setAttribute( "name", "rangeCheck" );
	checkBox.setAttribute( "type", "checkbox" );
	checkBox.setAttribute( "id", "rangeCheck" + entryNum );
	
	if ( checked )
	{
		checkBox.setAttribute( "checked", true );
	}

	if ( document.attachEvent )
	{
		checkBox.param1 = entryNum;
		checkBox.attachEvent( "onchange", IEchangeIsRange );
		checkBox.attachEvent( "onclick", IEchangeIsRange );
	}
	else
	{
		checkBox.setAttribute( "onchange", "changeIsRange( " + entryNum + ", this.checked );" );
	}
	
	return checkBox;
}

function newDateRangeEntry( startdate, enddate, checked )
{
	//var now = new Date();
	var numDatesObj = document.getElementById( "numDates" );
	//tracker for how many are left
	var numDatesObjTracker = document.getElementById( "numDatesTracker" );
	var dateEntryDiv = document.getElementById( "dateEntry" );
	var entryNum = parseInt( numDatesObj.value );
	var entryNumTracker = parseInt( numDatesObjTracker.value );

	theDiv = document.createElement("div");
	theDiv.setAttribute( "id", "datediv[" + entryNum + "]" );
	//theDiv.appendChild( removeDateButton( entryNum ) );
	theDiv.appendChild( dateEntryFields( entryNum, startdate, 1 ) );
	theDiv.appendChild( document.createTextNode(" to ") );

	//Checkbox for whether the second set of dates are enabled
	theDiv.appendChild( dateRangeCheckBox( entryNum, checked ) );
	theDiv.appendChild( dateEntryFields( entryNum, enddate, 2 ) );

	dateEntryDiv.appendChild( theDiv );
	
	numDatesObj.value = entryNum + 1;
	numDatesObjTracker.value = entryNumTracker + 1;
	
	//setup the fields
	changeIsRange( entryNum, checked );
}

function IEchangeIsRange()
{
	sender = event.srcElement;
	changeIsRange( sender.param1, sender.checked );
}

function changeIsRange( num, checked )
{
	changeIsRange2( num, checked );
	
	if ( !checked )
	{
		setDays( num, "date1_", 1 )
	}
}

function changeIsRange2( num, checked )
{
	var dayInput = document.getElementById( "day2[" + num + "]" );
	var monthInput = document.getElementById( "month2[" + num + "]" );
	var yearInput = document.getElementById( "year2[" + num + "]" );
	
	dayInput.disabled = !checked;
	monthInput.disabled = !checked;
	yearInput.disabled = !checked;
	
}

function validateDates()
{
	var numDatesObj = document.getElementById( "numDates" );
	var numEntries = parseInt( numDatesObj.value );
	
	for ( var i = 0; i <= numEntries; i++ )
	{
		var day1 = document.getElementById( 'day1[' + i + ']' );
		var month1 = document.getElementById( 'month1[' + i + ']' );
		var year1 = document.getElementById( 'year1[' + i + ']' );
		
		var day2 = document.getElementById( 'day2[' + i + ']' );
		var month2 = document.getElementById( 'month2[' + i + ']' );
		var year2 = document.getElementById( 'year2[' + i + ']' );
		
		if ( day2 != null )
		{
			var y1 = year1.options[ year1.selectedIndex ].value;
			var m1 = month1.options[ month1.selectedIndex ].value;
			var d1 = day1.options[ day1.selectedIndex ].value;
			
			var y2 = year2.options[ year2.selectedIndex ].value;
			var m2 = month2.options[ month2.selectedIndex ].value;
			var d2 = day2.options[ day2.selectedIndex ].value;			
			
			var date1 = new Date( y1, m1, d1 );
			var date2 = new Date( y2, m2, d2 );
			
			if ( date2.getTime() < date1.getTime() )
			{
				return false;
			}
		}
	}
	
	return true;
}