/**
 * Bright Interactive, calendar.js
 *
 * Copyright 2003 Bright Interactive, All Rights Reserved.
 * 
 * Contains JavaScript functions used by the date selector window.
 *
 */
/*
Ver  Date	        Who					Comments
--------------------------------------------------------------------------------
d1  24-Oct-2003     Martin Wilson       Created.
d2  14-Jul-2004     Chris Preager       Changed to bring calendar window to focus if already open.
d3  30-Jun-2005     Martin Wilson		Changed to set the month to the value in the field
--------------------------------------------------------------------------------
*/

var today = new Date();
var day   = today.getDate();
var month = today.getMonth();
var year  = y2k(today.getYear());
var dateFieldBeingChanged;

var bDaysBeforeMonths = true;

function y2k(number)    { return (number < 1000) ? number + 1900 : number; }

function padout(number) { return (number < 10) ? '0' + number : number; }

function restart() 
{
	if(bDaysBeforeMonths)
	{
		dateFieldBeingChanged.value = '' + padout(day) + '/' + padout(month - 0 + 1) + '/' + year;
	}
	else
	{
		dateFieldBeingChanged.value = '' + padout(month - 0 + 1)  + '/' + padout(day) + '/' + year;
	}

	mywindow.close();
}

function openDatePicker(a_dateField) 
{	
	openDatePickerSupportUS(a_dateField, true);
}

function openDatePickerSupportUS(a_dateField,a_bDaysBeforeMonths) 
{		
	bDaysBeforeMonths = a_bDaysBeforeMonths;

	// Set the date picker values to be the values in the field, if set:

	var dtStart;

	// Try to work out the current day, month, year:
	var sDate = a_dateField.value;
	var iPos = 0;
	var iLastPos = 0;
	var sDay = "";
	var sMonth = "";
	var sYear = "";

	// Try to set the current month in the date picker to the month in the field:
	if (sDate && sDate.length>0)
	{
		// Get the occurrence of the first /:
		iPos = sDate.indexOf("/", 0);

		if (iPos>0)
		{
			if (a_bDaysBeforeMonths)
			{
				sDay = sDate.substring(0, iPos);
			}
			else
			{
				sMonth = sDate.substring(0, iPos);
			}
		}

		// Get the occurrence of the next /:
		iLastPos = iPos;
		iPos = sDate.indexOf("/", iLastPos+1);

		if (iPos>0)
		{
			if (a_bDaysBeforeMonths)
			{
				sMonth = sDate.substring(iLastPos+1, iPos);
			}
			else
			{
				sDay = sDate.substring(iLastPos+1, iPos);
			}

			// Finally, get the year:
			sYear = sDate.substring(iPos+1, sDate.length);
		}

	}
	
	var dtStart;

	if (isValidNumber(sYear) && isValidNumber(sMonth) && isValidNumber(sDay))
	{
		// Initialise the calendar to show the date:
		dtStart = new Date(sYear, sMonth-1, sDay);
	}
	else
	{
		// Otherwise set the calendar to this month:
		dtStart = new Date();
	}

	// set the starting values:
	day   = dtStart.getDate();
	month = dtStart.getMonth();
	year  = y2k(dtStart.getYear());	

	dateFieldBeingChanged = a_dateField;

	// Close existing window, if it's open
	try {
		mywindow.close();
	}
	catch (error) {
		if (error instanceof ReferenceError || error instanceof TypeError) {
			//that's fine, nothing to do
		}
		else {
			alert("Unexpected error : " + error);
		}
	}

	mywindow=open('../calendar/calendar.html','myname','resizable=yes,status=no,width=300,height=230,left=100,top=30');
	mywindow.location.href = '../calendar/calendar.html';
	if (mywindow.opener == null) 
	{
		mywindow.opener = self;
	}
}

function isValidNumber(numval)
{
	if (numval==""){return false;}
	var myRegExp = new RegExp("^[/+|/-]?[0-9]*[/.]?[0-9]*$");
	return myRegExp.test(numval);
}

function writeDatePickerIcon(field) 
// document.write the right code to present the date picker icon
// field is the name of the text input that will receive the 
// selected date.
{
	document.write('<a href="javascript:;" title="Date chooser"><img class="icon" src="/calendar/calendar.gif" alt="Date chooser" onclick="this.value=\'\'; openDatePicker(document.getElementsByName(\'' + field + '\')[0])" width="16" height="15" border="0" style="padding-right: 0;" ><\/a>');
}
