///
/// Gets the selected day from a day select list
///
jQuery.fn.getDay = function() {

    var dayPart = $(this).val();
    var day     = parseInt(dayPart, 10);
    return day;
}

///
/// Gets the selected month from a month year select list
///
jQuery.fn.getMonth = function() {

    var id        = this.id;
    var monthYear = $(this).val();
    var monthPart = monthYear.substr(0, 2);
    var month     = parseInt(monthPart, 10);
    return month;
}

///
/// Gets the selected year from a month year select list
///
jQuery.fn.getYear = function() {

    var monthYear = $(this).val();
    var yearPart  = monthYear.substr(2, 4);
    var year      = parseInt(yearPart, 10);
    return year;
}

jQuery.fn.attachDatePickerSolo = function()
{
    $.datepicker.setDefaults($.datepicker.regional[LANG]);
	$(this).datepicker(
        {
            dateFormat: 'dd/mm/yy',
            firstDay: 1,
            numberOfMonths: 1,
            duration: "",
            showOn: 'both',
            buttonImageOnly: true
        }
    );
	return this;
}

jQuery.fn.attachDatePicker2 = function(daySelector, monthYearSelector, selectionChangedHandler)
{
	$(this).datepicker(
        {
            dateFormat: 'ddmmyy',
            firstDay:1,
            numberOfMonths: 2,
            duration: "",
            showOn: 'button',
            buttonImageOnly: true,
        	onSelect: function(date)
        	{
        		var dateString = date.toString();
        		var day        = parseInt(dateString.substr(0, 2), 10);
        		var monthYear  = dateString.substr(2, dateString.length - 2);

        		$(daySelector).val(day);
        		$(monthYearSelector).val(monthYear);

        		selectionChangedHandler();
        	},
        	beforeShow: function()
        	{
        		var day   = $(daySelector).getDay();
        		var month = $(monthYearSelector).getMonth();
        		var year  = $(monthYearSelector).getYear();
        		var dateString = padDigits(day, 2) + padDigits(month, 2) + padDigits(year, 4);

        		$(this).val(dateString);
        	}
        }
    );
	return this;
}

function selectionChangedHandler() {
    constrainDateRange("#oDay", "#oMonYear", "#rDay", "#rMonYear");
}

function constrainDateRange(originDaySelector, originMonthYearSelector,
                            destinationDaySelector, destinationMonthYearSelector) {

    // Get the origin month day year
    var originDay   = $(originDaySelector).getDay();
    var originMonth = $(originMonthYearSelector).getMonth();
    var originYear  = $(originMonthYearSelector).getYear();
    var originDate  = new Date(originYear, originMonth - 1, originDay);

    // Get the destination month day year
    var destinationDay   = $(destinationDaySelector).getDay();
    var destinationMonth = $(destinationMonthYearSelector).getMonth();
    var destinationYear  = $(destinationMonthYearSelector).getYear();
    var destinationDate  = new Date(destinationYear, destinationMonth - 1, destinationDay);


    if (originDate > destinationDate) {
        var dayValue       = $(originDaySelector).val();
        var monthYearValue = $(originMonthYearSelector).val();
        $(destinationDaySelector).val(dayValue);
        $(destinationMonthYearSelector).val(monthYearValue);
    }

}

function attachSelectorEvents(daySelector, monthYearSelector, callBack) {

    $(daySelector).unbind("change").click(function() {
        constrainToValidDate(daySelector, monthYearSelector);
        callBack();
        return false;
    });

    $(daySelector).unbind("keyup").keyup(function() {
        constrainToValidDate(daySelector, monthYearSelector);
        callBack();
        return false;
    });

    $(monthYearSelector).unbind("change").click(function() {
        constrainToValidDate(daySelector, monthYearSelector);
        callBack();
        return false;
    });

    $(monthYearSelector).unbind("keyup").keyup(function() {
        constrainToValidDate(daySelector, monthYearSelector);
        callBack();
        return false;
    });

}

function constrainToValidDate(daySelector, monthYearSelector) {
    // exit if any part of "no, just one way" has been selected in a return date
    if($(daySelector).val() == '00' || $(monthYearSelector).val() == '00')
        return;
    
    // Get the origin month day year
    var day   = $(daySelector).getDay();
    var month = $(monthYearSelector).getMonth();
    var year  = $(monthYearSelector).getYear();

    // Get the total number of days in the month allow for leap years
    var totalDaysInMonth = Date.getDaysInMonth(year, month - 1);

    // check that the origin selected day is not greater than the number of days in the selected month
    if (day > totalDaysInMonth) {
        //Set the day drop down equal to the
        $(daySelector).val(totalDaysInMonth);
    }

    // Set the day to the first if days is zero and a valid month is selected
    if (day == 0 & month != 0) {
        $(daySelector).val("01");
    }

    // Reset the destination day to none if month is none
    if (day != 0 & month == 0) {
        $(daySelector).val("00");
    }
}

