/**
 * Filename: dateselector.js
 *
 * Description: Provides an object for user to select a date in a given period of time.
 *    include following javascript to include it in the form
 *     <script language="javascript">
 *      buildDateSelector(window.document.myform, window.document.myform.date);
 *      curDate = new Date();
 *      setDatePeriod(new Date(curDate.getFullYear() - 10, 0, 1), curDate)
 *     </script>
 *	  add this to body to reload the previous selected date
 *      <BODY onload='resetDate();'>
 *
 * ===============================================================================
 * Version Control Log Messages:
 * $Log: dateselector.js,v $
 * Revision 1.2  2003/08/13 02:01:23  yli
 * Don't reset the date if the dateField is not set.
 *
 * Revision 1.1.1.1  2003/07/30 01:03:41  yli
 * Portal GoTrek 5.2. From GT5.2 m-Vision.
 *
 * Revision 1.1.1.1  2003/03/06 04:13:58  yli
 * no message
 *
 * Revision 1.2  2002/08/06 01:24:27  yli
 * Fix bug with field names.
 *
 * Revision 1.1  2002/08/06 01:01:58  yli
 * Initial revision. For date selection, using drop down box.
 *
 *
 * =======================================================================
 * Version    Date    Programmer  Comment
 * ------- ---------- ----------  ----------------------------------------
 *  1.00   06/08/2002 Yifan Li    Orginal.
 * =======================================================================
 *
 * Copyright (c) 1998-2002 GoConnect Pty Ltd.
 * 6th Floor, 43-51 Queen Street, Melbourne VIC 3000, Australia
 * All Rights Reserved.
 *
 * This software is the confidential and proprietary information of
 * GoConnect Ptr Ltd. ("Confidential Information"). You shall not
 * disclose such Confidential Information and shall use it only in
 * accordance with the terms of the license agreement you entered
 * into with GoConnect.
 *
 *
 */

/*
 */

var dates = new Array("01", "02", "03", "04", "05", "06", "07", "08", "09", "10", "11", "12", "13", "14", "15", "16", "17", "18", "19", "20", "21", "22", "23", "24", "25", "26", "27", "28", "29", "30", "31");
var datevalues = new Array("01", "02", "03", "04", "05", "06", "07", "08", "09", "10", "11", "12", "13", "14", "15", "16", "17", "18", "19", "20", "21", "22", "23", "24", "25", "26", "27", "28", "29", "30", "31");

var months = new Array("January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December");
var monthvalues = new Array("01", "02", "03", "04", "05", "06", "07", "08", "09", "10", "11", "12");

var showYears = 100;

var start_day_value;
var start_year_value;
var start_month_value;

var end_day_value;
var end_year_value;
var end_month_value;

var temp_year_value;
var temp_month_value;
var temp_day_value;

/* 
 * This function writes the date selects in html document and does the initialization.
 *
 * aForm the form which contains this object
 * dateField the form field for date output (output text will be in the format of dd/mm/yyyy)
 */
function buildDateSelector(aForm, dateField)
{
	document.write("Day: <SELECT size = '1' name='selectorday' onChange = 'setSelectedDate();'> -----</SELECT>");
	document.write("&nbsp;Month: <SELECT size = '1' name='selectormonth' onChange = 'monthSetDay(this.form.selectormonth.options[this.form.selectormonth.selectedIndex].value, this.form.selectoryear.options[this.form.selectoryear.selectedIndex].value);'></SELECT>");
	document.write("&nbsp;Year: <SELECT size = '1' name='selectoryear' onChange='yearSetDay(this.form.selectoryear.options[this.form.selectoryear.selectedIndex].value);'></SELECT>");
	this.yearField = aForm.selectoryear;
	this.monthField = aForm.selectormonth;
	this.dayField = aForm.selectorday;
	this.dateField = dateField;
	var now= new Date();
	
	end_day_value = now.getDate();
	start_day_value = 1;
	temp_day_value = now.getDate();
	
	end_month_value = now.getMonth()+1;
	start_month_value = 1;
	temp_month_value = now.getMonth()+1;
	
	end_year_value = now.getFullYear();
	start_year_value = end_year_value - showYears + 1;
	temp_year_value = now.getFullYear();
	
	setYearOption(this.yearField, start_year_value, end_year_value);
	yearSetDay(temp_year_value);
	
	this.yearField.selectedIndex = showYears - 1;
	this.monthField.selectedIndex = end_month_value - 1;
	this.dayField.selectedIndex = end_day_value - 1;
	setSelectedDate();
}

/*
 * Set the selectable period of date, date2 must be later than date1.
 */
function setDatePeriod(date1, date2)
{
	start_day_value= date1.getDate();
	end_day_value= date2.getDate();
	temp_day_value = date2.getDate();
	
	start_month_value = date1.getMonth()+1;
	end_month_value = date2.getMonth()+1;
	temp_month_value = date2.getMonth()+1;
	
	start_year_value = date1.getFullYear();
	end_year_value = date2.getFullYear();
	temp_year_value = date2.getFullYear();

	setYearOption(this.yearField, start_year_value, end_year_value);

	this.yearField.selectedIndex = end_year_value - start_year_value;
	this.monthField.selectedIndex = end_month_value - 1;
	this.dayField.selectedIndex = end_day_value - 1;

	setSelectedDate();
}

/*
 * Reset the selected date to the previous selection after the page is submit.
 * Used at the body tag's onLoad function.
 */

function resetDate()
{
	if (this.dateField) {
		selectDate(this.dateField.value);
	}
}

/*
 * Set the selected date to a given date in the 'yyyymmdd' format.
 */
function selectDate(pDate)
{
	if (typeof(pDate) == 'string' && pDate.length == 8)
	{
		this.yearField.selectedIndex = parseInt(pDate.substr(0, 4), 10) - start_year_value;
		this.monthField.selectedIndex = parseInt(pDate.substr(4, 2), 10) - 1;
		this.dayField.selectedIndex = parseInt(pDate.substr(6, 2), 10) - 1;

		yearSetDay(this.yearField.options[this.yearField.selectedIndex].value);
	}
}

/*
 * Set the day and month selector of the given year.
 */
function yearSetDay(year_pass)
{
	temp_year_value = year_pass;
	setMonth(this.monthField, temp_year_value);
	setDay(this.dayField, temp_month_value, temp_year_value)
	setSelectedDate();
}

/*
 * Set the day selector of the given month and year.
 */
function monthSetDay(month_pass, year_pass)
{
	temp_month_value = month_pass;
	temp_year_value = year_pass;
	setDay(this.dayField, temp_month_value, temp_year_value);
	setSelectedDate();
}

function setSelectedDate()
{
	dateField.value = this.yearField.options[this.yearField.selectedIndex].value + this.monthField.options[this.monthField.selectedIndex].value + this.dayField.options[this.dayField.selectedIndex].value;
}

/*
 * Functions underneath should not be called by the client. They are private and for internal use only.
 */
/*
 * Set the day selector of the given month and year in the given field.
 */
function setDay(dField, the_month, the_year)
{
/*	if(the_month == start_month_value && the_year == start_year_value)
		startDay = start_day_value;
	else*/
		startDay = 1;
/*	if(the_month == end_month_value && the_year == end_year_value)
		lastDay = end_day_value;
	else */if(the_month == 1 || the_month ==3 || the_month == 5 || the_month == 7 || the_month == 8 || the_month == 10 || the_month == 12)
		lastDay = 31;
	else if(the_month == 4 || the_month == 6 || the_month == 9 || the_month == 11)
		lastDay = 30;
	else if(the_month == 2)
	{
		if( (the_year%4 == 0 && the_year % 100 != 0) || (the_year % 400 == 0))
			lastDay = 29;
		else if ( (the_year%4 != 0 && the_year % 100 == 0) || (the_year % 400 != 0))
			lastDay = 28;
	}
	setDayOption(dField, startDay, lastDay);
}

/*
 * Set the month selector of the given year in the given field.
 */
function setMonth(mField, the_year)
{	
/*	if(the_year == start_year_value)
	{
		startMonth = start_month_value;
	}
	else*/
		startMonth = 1;
/*	if(the_year == end_year_value)
	{
		lastMonth = end_month_value;
	}
	else*/
		lastMonth = 12;
	setMonthOption(mField, startMonth, lastMonth);

}

/*
 * Set the year options from the given startyear to endyear in the given field.
 */
function setYearOption(yField, startYear, endYear)
{
	yField.length = endYear - startYear + 1;

	for(i = 0; i <= endYear - startYear; i++)
	{
		yField.options[i].text = startYear + i;
		yField.options[i].value = startYear + i;
	}
}

/*
 * Set the month options from the given startmonth to endmonth in the given field.
 */
function setMonthOption(mField, startMonth, endMonth)
{
	mField.length = endMonth - startMonth + 1;

	for(i = 0; i <= endMonth - startMonth; i++)
	{
		mField.options[i].text = months[startMonth + i - 1];
		mField.options[i].value = monthvalues[startMonth + i - 1];
	}
}

/*
 * Set the day options from the given startday to endday in the given field.
 */
function setDayOption(dField, startDay, endDay)
{
	dField.length = endDay - startDay + 1;

	for(i = 0; i <= endDay - startDay; i++)
	{
		dField.options[i].text = dates[startDay + i - 1];
		dField.options[i].value = dates[startDay + i - 1];
	}
}
