// Global variables
var MonthsNamesArray = new Array("jan", "feb", "mar", "apr", "may", "jun", "jul", "aug", "sep", "oct", "nov", "dec");
var CurrentYear = new Date().getFullYear();
var YearsArray = new Array(CurrentYear, CurrentYear + 1, CurrentYear + 2, CurrentYear + 3);


// Javascript combobox date selector class
function DateSelectorObject(DateSelectorName, DateFormat, DateValue, StartDate, EndDate)
{
	// Class members
	this._dateSelectorName = DateSelectorName;
	this.hiddenFieldName = DateSelectorName;
	this.selectYearListID = DateSelectorName + "_Year";
	this.selectMonthListID = DateSelectorName + "_Month";
	this.selectDayListID = DateSelectorName + "_Day";
	this._dateFormat = DateFormat;
	this._dateValue = DateValue;
	this._dateSeparator ="/";
	this._startDate = StartDate;
	this._endDate = EndDate;
	this._validDateFormat = new Array("dd/mm/yyyy", "yyyy-mm-dd");
	this._hasChanged = 0;
		

	// Public Class Methods
	this.getHiddenField = function() { return document.getElementById(this.hiddenFieldName); }
	this.getSelectYearList = function() { return document.getElementById(this.selectYearListID); }		
	this.getSelectMonthList = function() { return document.getElementById(this.selectMonthListID); }
	this.getSelectDayList = function() { return document.getElementById(this.selectDayListID); }
	this.getDateSelectorName = function() {	return this._dateSelectorName; }
	this.getStartDate = function() { return this._startDate; }
	this.getEndDate = function() { return this._endDate; }

	this.setDateValues = function()
	{
		if(this._dateFormat == this._validDateFormat[0])
		{
			this._dayValue = this._dateValue.split("/")[0];
			this._monthValue = parseInt(this._dateValue.split("/")[1], 10);
			this._yearValue =  parseInt(this._dateValue.split("/")[2], 10);	
		}
		else if(this._dateFormat == this._validDateFormat[1])
		{
			this._yearValue = this._dateValue.split("-")[0];
			this._monthValue = parseInt(this._dateValue.split("-")[1], 10);
			this._dayValue =  parseInt(this._dateValue.split("-")[2], 10);
		}
		else // In the case that no valid format has been passed use default. Use today values
		{
			this._dayValue = new Date().getDate();
			this._monthValue = new Date().getMonth() + 1; // Month is a zero based array
			this._yearValue =  new Date().getYear();
		}
	}
	
	
	this.getDaysInMonth=getDaysInMonth;
	this.getDaysInCurrentMonth = getDaysInCurrentMonth;
	this.UpdateDate = UpdateDate;
	this.populateDaysSelector = populateDaysSelector;

	
	// Class Methods Definitions
	function getDaysInCurrentMonth() { return 32 - new Date(this._yearValue, this._monthValue-1, 32).getDate();	}

	// Class Methods Definitions
	function getDaysInMonth(iMonth, iYear) { return 32 - new Date(iYear, iMonth-1, 32).getDate(); }
	
	
	function UpdateDate(obj)
	{
	  this._hasChanged = 1;
	  
		if(obj.name == (this.selectYearListID))
		{
			this._yearValue = obj.options[obj.selectedIndex].value;
			this.populateDaysSelector();
		}
			
		if(obj.name == (this.selectMonthListID))
		{
			this._monthValue = obj.options[obj.selectedIndex].value
			this.populateDaysSelector();
		}
			
		if(obj.name == (this.selectDayListID))
		{
			this._dayValue = obj.options[obj.selectedIndex].value;
		}
		
		this.getHiddenField().value = this._dayValue +  this._dateSeparator + this._monthValue + this._dateSeparator + this._yearValue;
	}
	
	
	function populateDaysSelector()
	{
		var _selectDayList = this.getSelectDayList();
		// Clear any earlier list items
		_selectDayList.options.length = 0;
		for(var i=0; i<this.getDaysInCurrentMonth(); i++)
		{
			_selectDayList.options[i] = new Option(i+1,i+1);
		}
		
		if(this._dayValue > this.getDaysInCurrentMonth())
		{	
			this._dayValue = this.getDaysInCurrentMonth();
		}
	
		_selectDayList.selectedIndex = this._dayValue - 1;	
	}
	
	// Initialize Object
	this.setDateValues();
	
}



function DateSelectorInput(DateSelectorName, DateFormat, DateValue)
{

	eval(DateSelectorName + '_Object = new DateSelectorObject(\'' + DateSelectorName + '\',\'' + DateFormat + '\',\'' + DateValue + '\')');
		
	var CurrentYear =  eval(DateSelectorName + '_Object._yearValue');
	var CurrentMonth = eval(DateSelectorName + '_Object._monthValue');
	var CurrentDay = eval(DateSelectorName + '_Object._dayValue');
	var DaysInMonth = eval(DateSelectorName + '_Object.getDaysInCurrentMonth()');
	var StartDate = eval(DateSelectorName + '_Object.getStartDate()');
	var EndDate = eval(DateSelectorName + '_Object.getEndDate()');

	
	// Prints days
	document.write('<select class="DateSelector_Day" id="' + DateSelectorName + '_Day" name="' + DateSelectorName + '_Day" onchange="javascript:' + DateSelectorName + '_Object.UpdateDate(this);">');
	for(intDays=1; intDays<=DaysInMonth; intDays++)
	{
		if(intDays == CurrentDay)
			document.write('<option value="' + intDays + '" selected>' + intDays + '</option>');
		else
			document.write('<option value="' + intDays + '">' + intDays + '</option>');
	}
	document.write("</select>");

		
	// Prints months
	document.write('<select class="DateSelector_Month" id="' + DateSelectorName + '_Month" name="' + DateSelectorName + '_Month" onchange="javascript:' + DateSelectorName + '_Object.UpdateDate(this);">');
	for(intMonths=0; intMonths<MonthsNamesArray.length; intMonths++)
	{
		if(intMonths+1==CurrentMonth)
			document.write("<option value=" + (intMonths + 1) + " selected>" + MonthsNamesArray[intMonths] + "</option>");
		else
			document.write("<option value=" + (intMonths + 1) + ">" + MonthsNamesArray[intMonths] + "</option>");
	}
	document.write("</select>");
			

	document.write('<select class="DateSelector_Year"  id="' + DateSelectorName + '_Year" name="' + DateSelectorName + '_Year" onchange="javascript:' + DateSelectorName + '_Object.UpdateDate(this);">');
	for(intYears=0; intYears<YearsArray.length; intYears++)
	{
		if(YearsArray[intYears] == CurrentYear)
			document.write("<option value=" + YearsArray[intYears] + " selected>" + YearsArray[intYears] + "</option>");
		else
			document.write("<option value=" + YearsArray[intYears] + ">" + YearsArray[intYears] + "</option>");
	}
	document.write("</select>");
			
	document.write('<input type="hidden" id="' + DateSelectorName + '" name="' + DateSelectorName + '" value="' + DateValue + '">');
}

