
// Member variables
var thisYear = new Date();
var cmbDays;
var cmbMonths;
var cmbYears;
var arrMonths = new Array;
var arrDays = new Array;

arrMonths[0] = new Option("January", 31);
arrMonths[1] = new Option("February", 28);
arrMonths[2] = new Option("March", 31);
arrMonths[3] = new Option("April", 30);
arrMonths[4] = new Option("May", 31);
arrMonths[5] = new Option("June", 30);
arrMonths[6] = new Option("July", 31);
arrMonths[7] = new Option("August", 31);
arrMonths[8] = new Option("September", 30);
arrMonths[9] = new Option("October", 31);
arrMonths[10] = new Option("November", 30);
arrMonths[11] = new Option("December", 31);

function renderCalendar(c)
{

    // *******************************************************************************************************************************************
    // NOTE: c is intended to be the LANSA hidden field to bind the control to, this will also be pre-fixed to the combo names to keep them unique
    // *******************************************************************************************************************************************
    
    // Bind to the combo boxes for the current control instance
    bind(c);
 
    // Render the day combo
    document.writeln("<select name=\"" + cmbDays + "\" onchange=\"syncToField('" + c + "')\"></select>");
    
    // Render the month combo
    document.writeln("<select name=\"" + cmbMonths + "\" onchange=\"daysInSelectedMonth('" + c + "');syncToField('" + c + "')\">");
    for(i = 0; i < 12; i++)
    {
        document.writeln("<option value=\"" + arrMonths[i].value + "\">" + arrMonths[i].text + "</option>");
    }
    document.writeln("</select>");
    
    // Render the year combo (note: you can change the upper and lower bounds for the year here)
    document.writeln("<select name=\"" + cmbYears + "\" onchange=\"daysInSelectedMonth('" + c + "');syncToField('" + c + "')\">");
    for(i = 2000; i <= thisYear.getFullYear(); i++)
    {
        document.writeln("<option value=\"" + i + "\">" + i + "</option>");
    }
    document.writeln("</select>");
    
    // Initialise the days based upon the default month and year
    selectedMonth = document.LANSA.elements[cmbMonths][document.LANSA.elements[cmbMonths].selectedIndex].value;
    daysInSelectedMonth(c);
    
    syncFromField(c);
	  
}

function daysInSelectedMonth(c)
{

    // Bind to the combo boxes for the current control instance
    bind(c);

    // Snapshot the currently selected date elements
    var selectedDay = document.LANSA.elements[cmbDays].selectedIndex;
    var selectedMonth = document.LANSA.elements[cmbMonths][document.LANSA.elements[cmbMonths].selectedIndex].value;
    var selectedYear = document.LANSA.elements[cmbYears].options[document.LANSA.elements[cmbYears].selectedIndex].value;
    
    for(i = arrDays.length; i >= 28; i--)
    {
        arrDays[i] = null;
        document.LANSA.elements[cmbDays].options[i] = null;
    }
    
    if(selectedMonth == 28)
    {
        if ((selectedYear % 4 == 0 && selectedYear % 100 != 0) || selectedYear % 400 == 0)
        {
            selectedMonth = 29;
        }
    }       
    
    for(i = 0; i < selectedMonth; i++)
    {
        arrDays[i] = new Option(i + 1, i + 1);
        document.LANSA.elements[cmbDays].options[i] = arrDays[i];
    }
    
    if (selectedDay == -1)
        document.LANSA.elements[cmbDays].options[0].selected = true;
    else
       document.LANSA.elements[cmbDays].options[selectedDay].selected = true;
             
}

function syncFromField(c)
{

    // Bind to the combo boxes for the current control instance
    bind(c);
        
    // NOTE: There is no validation here at the moment, so if the LANSA program issues an invalid DDMMYYYY value then BOOM!
    var strValue = document.LANSA.elements[c].value;      
    document.LANSA.elements[cmbYears].value = parseFloat(strValue.substr(4, 4));
    document.LANSA.elements[cmbMonths].selectedIndex = (parseFloat(strValue.substr(2, 2)) - 1);
    document.LANSA.elements[cmbDays].selectedIndex = (parseFloat(strValue.substr(0, 2)) - 1);
    
}

function syncToField(c)
{
    
    // Bind to the combo boxes for the current control instance
    bind(c);
    
    // Snapshot the currently selected date elements
    var selectedDay = parseFloat(document.LANSA.elements[cmbDays].selectedIndex) + 1;
    var selectedMonth = parseFloat(document.LANSA.elements[cmbMonths].selectedIndex) + 1;
    var selectedYear = parseFloat(document.LANSA.elements[cmbYears].options[document.LANSA.elements[cmbYears].selectedIndex].value);
    
    // Convert to a DDMMYYYY formatted date to pass to LANSA
    var strValue = new String((((selectedDay * 1000000) + (selectedMonth * 10000) + selectedYear) + 100000000));
    document.LANSA.elements[c].value = strValue.substr(1, 8);
    
}

function bind(c)
{

    // Ensure that the UI components being worked with are the correct ones for the requested instance
    cmbDays = c + "_days";
    cmbMonths = c + "_months";
    cmbYears = c + "_years";

}
