// Global variables
var MonthsNamesArray = new Array("Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec");




// 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");
		

	// 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)
	{
		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 id="' + DateSelectorName + '_Day" name="' + DateSelectorName + '_Day">');
	for(intDays=1; intDays<=31; intDays++)
	{
		if(intDays == CurrentDay)
			document.write('<option value="' + intDays + '" selected>' + intDays + '</option>');
		else
			document.write('<option value="' + intDays + '">' + intDays + '</option>');
	}
	document.write("</select>&nbsp;");
	
	// Prints months
	document.write('<select id="' + DateSelectorName + '_Month" name="' + DateSelectorName + '_Month">');
	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>&nbsp;");

    var d = new Date();
    var curr_year = d.getFullYear();
    var DisplayYear=0;
    
    if(DateSelectorName=="BirthDate")
    {
        document.write('<select class="DateSelector_Year"  id="' + DateSelectorName + '_Year" name="' + DateSelectorName + '_Year"');
	    for(intYears=17; intYears<100; intYears++)
	    {
		    DisplayYear= curr_year-intYears;
		    if (DisplayYear==CurrentYear)
		        document.write("<option value=" + DisplayYear.toString() + " selected>" + DisplayYear.toString() + "</option>");
	        else
	            document.write("<option value=" + DisplayYear.toString() + ">" + DisplayYear.toString() + "</option>");
	    }
	    document.write("</select>");
    }
    else if(DateSelectorName=="PassIssueDate")
    {
        document.write('<select class="DateSelector_Year"  id="' + DateSelectorName + '_Year" name="' + DateSelectorName + '_Year"');
	    for(intYears=0; intYears<20; intYears++)
	    {
		    DisplayYear= curr_year-intYears+1;
		    if (DisplayYear==CurrentYear)
		        document.write("<option value=" + DisplayYear.toString() + " selected>" + DisplayYear.toString() + "</option>");
	        else
	            document.write("<option value=" + DisplayYear.toString() + ">" + DisplayYear.toString() + "</option>");
	    }
	    document.write("</select>");
    }
    else if(DateSelectorName=="DriIssueDate")
    {
        document.write('<select class="DateSelector_Year"  id="' + DateSelectorName + '_Year" name="' + DateSelectorName + '_Year"');
	    for(intYears=0; intYears<100; intYears++)
	    {
		    DisplayYear= curr_year-intYears+1;
		    if (DisplayYear==CurrentYear)
		        document.write("<option value=" + DisplayYear.toString() + " selected>" + DisplayYear.toString() + "</option>");
	        else
	            document.write("<option value=" + DisplayYear.toString() + ">" + DisplayYear.toString() + "</option>");
	    }
	    document.write("</select>");
    }
    else if(DateSelectorName=="DriExpDate")
    {
        document.write('<select class="DateSelector_Year"  id="' + DateSelectorName + '_Year" name="' + DateSelectorName + '_Year"');
	    for(intYears=0; intYears<50; intYears++)
	    {
		    DisplayYear= curr_year+intYears-1;
		    if (DisplayYear==CurrentYear)
		        document.write("<option value=" + DisplayYear.toString() + " selected>" + DisplayYear.toString() + "</option>");
	        else
	            document.write("<option value=" + DisplayYear.toString() + ">" + DisplayYear.toString() + "</option>");
	    }
	    document.write("</select>");
    }
    else
    {
        document.write('<select class="DateSelector_Year"  id="' + DateSelectorName + '_Year" name="' + DateSelectorName + '_Year"');
	    for(intYears=0; intYears<12; intYears++)
	    {
		    DisplayYear= curr_year+intYears-1;
		    if (DisplayYear==CurrentYear)
		        document.write("<option value=" + DisplayYear.toString() + " selected>" + DisplayYear.toString() + "</option>");
	        else
	            document.write("<option value=" + DisplayYear.toString() + ">" + DisplayYear.toString() + "</option>");
	    }
	    document.write("</select>");
    }

	
			
	document.write('<input type="hidden" id="' + DateSelectorName + '" name="' + DateSelectorName + '" value="' + DateValue + '">');
}