﻿function initInfantCountDependent(infantOptions, optionLabel) {
    if (optionLabel == null)
        optionLabel = "";

    infantOptions["1"] = { "0": "0 " + optionLabel, "1": "1 " + optionLabel };
    infantOptions["2"] = { "0": "0 " + optionLabel, "1": "1 " + optionLabel, "2": "2 " + optionLabel };
    infantOptions["3"] = { "0": "0 " + optionLabel, "1": "1 " + optionLabel, "2": "2 " + optionLabel, "3": "3 " + optionLabel };
    infantOptions["4"] = { "0": "0 " + optionLabel, "1": "1 " + optionLabel, "2": "2 " + optionLabel, "3": "3 " + optionLabel, "4": "4 " + optionLabel };
    infantOptions["5"] = { "0": "0 " + optionLabel, "1": "1 " + optionLabel, "2": "2 " + optionLabel, "3": "3 " + optionLabel, "4": "4 " + optionLabel, "5": "5 " + optionLabel };
    infantOptions["6"] = { "0": "0 " + optionLabel, "1": "1 " + optionLabel, "2": "2 " + optionLabel, "3": "3 " + optionLabel, "4": "4 " + optionLabel, "5": "5 " + optionLabel, "6": "6 " + optionLabel };
    infantOptions["7"] = { "0": "0 " + optionLabel, "1": "1 " + optionLabel, "2": "2 " + optionLabel, "3": "3 " + optionLabel, "4": "4 " + optionLabel, "5": "5 " + optionLabel, "6": "6 " + optionLabel, "7": "7 " + optionLabel };
    infantOptions["8"] = { "0": "0 " + optionLabel, "1": "1 " + optionLabel, "2": "2 " + optionLabel, "3": "3 " + optionLabel, "4": "4 " + optionLabel, "5": "5 " + optionLabel, "6": "6 " + optionLabel, "7": "7 " + optionLabel, "8": "8 " + optionLabel };
    infantOptions["9"] = { "0": "0 " + optionLabel, "1": "1 " + optionLabel, "2": "2 " + optionLabel, "3": "3 " + optionLabel, "4": "4 " + optionLabel, "5": "5 " + optionLabel, "6": "6 " + optionLabel, "7": "7 " + optionLabel, "8": "8 " + optionLabel, "9": "9 " + optionLabel };
    infantOptions["x"] = { "0": "0 " + optionLabel };
}

function initChildCountDependent(childOptions, optionLabel) {
    if (optionLabel == null)
        optionLabel = "";

    childOptions["1"] = { "0": "0 " + optionLabel, "1": "1 " + optionLabel, "2": "2 " + optionLabel, "3": "3 " + optionLabel, "4": "4 " + optionLabel, "5": "5 " + optionLabel, "6": "6 " + optionLabel, "7": "7 " + optionLabel, "8": "8 " + optionLabel };
    childOptions["2"] = { "0": "0 " + optionLabel, "1": "1 " + optionLabel, "2": "2 " + optionLabel, "3": "3 " + optionLabel, "4": "4 " + optionLabel, "5": "5 " + optionLabel, "6": "6 " + optionLabel, "7": "7 " + optionLabel };
    childOptions["3"] = { "0": "0 " + optionLabel, "1": "1 " + optionLabel, "2": "2 " + optionLabel, "3": "3 " + optionLabel, "4": "4 " + optionLabel, "5": "5 " + optionLabel, "6": "6 " + optionLabel };
    childOptions["4"] = { "0": "0 " + optionLabel, "1": "1 " + optionLabel, "2": "2 " + optionLabel, "3": "3 " + optionLabel, "4": "4 " + optionLabel, "5": "5 " + optionLabel };
    childOptions["5"] = { "0": "0 " + optionLabel, "1": "1 " + optionLabel, "2": "2 " + optionLabel, "3": "3 " + optionLabel, "4": "4 " + optionLabel };
    childOptions["6"] = { "0": "0 " + optionLabel, "1": "1 " + optionLabel, "2": "2 " + optionLabel, "3": "3 " + optionLabel };
    childOptions["7"] = { "0": "0 " + optionLabel, "1": "1 " + optionLabel, "2": "2 " + optionLabel };
    childOptions["8"] = { "0": "0 " + optionLabel, "1": "1 " + optionLabel };
    childOptions["9"] = { "0": "0 " + optionLabel };
    childOptions["x"] = { "0": "0 " + optionLabel };
}

function leapYear(year) {
    return (((year % 4 == 0) && (year % 100 != 0)) || (year % 400 == 0)) ? true : false;
}

function setDeparture(origins, value) {
    if (value != null && value != "")
        $('#' + origins.id + ' option[value=' + value + ']:first').attr('selected', 'selected');
    else
        origins.selectedIndex = 0;
}

function setArrival(destinations, value) {

    if (value != null && value != "") {
        $('#' + destinations.id + ' option[value=' + value + ']:first').attr('selected', 'selected');
    }
    else if (destinations.selectedIndex > 0) {
        $('#' + destinations.id + ' option[value=' + destinations.options[destinations.selectedIndex].value + ']:first').attr('selected', 'selected');
    }
    else {
        destinations.selectedIndex = 0;
    }
}


/**
*	Check if date is valid	
*
*	DateValue = 
*	ddmmyy (171201) or 
*	ddmmyyyy (17122001) or 
*	ddXmmXyy (17-12-01 or 17y12q01 ... ) or 
*	ddXmmXyyyy (17.12.2001 or 17,12,2001 ...) 
*	where 'X' is any sign not in 0..9, i.e. '-' or '/'
*
**/
function checkDateGeneral(DateValue) {
    var checkstr = '0123456789';
    var NewDatevalue = '';
    var DateTemp = '';
    var seperator = '.';
    var day;
    var month;
    var year;
    var leap = 0;
    var err = 0;
    var i;
    err = 0;

    /* Delete all chars except 0..9 */
    for (i = 0; i < DateValue.length; i++) {
        if (checkstr.indexOf(DateValue.substr(i, 1)) >= 0) {
            DateTemp = DateTemp + DateValue.substr(i, 1);
        }
    }
    DateValue = DateTemp;
    /* Always change date to 8 digits - string*/
    /* if year is entered as 2-digit / always assume 20xx */
    if (DateValue.length == 6) {
        DateValue = DateValue.substr(0, 4) + '20' + DateValue.substr(4, 2);
    }
    if (DateValue.length != 8) {
        err = 19;
    }
    /* year is wrong if year = 0000 */
    year = DateValue.substr(4, 4);
    if (year == 0) {
        err = 20;
    }
    /* Validation of month*/
    month = DateValue.substr(2, 2);
    if ((month < 1) || (month > 12)) {
        err = 21;
    }
    /* Validation of day*/
    day = DateValue.substr(0, 2);
    if (day < 1) {
        err = 22;
    }
    /* Validation leap-year / february / day */
    if ((year % 4 == 0) || (year % 100 == 0) || (year % 400 == 0)) {
        leap = 1;
    }
    if ((month == 2) && (leap == 1) && (day > 29)) {
        err = 23;
    }
    if ((month == 2) && (leap != 1) && (day > 28)) {
        err = 24;
    }
    /* Validation of other months */
    if ((day > 31) && ((month == '01') || (month == '03') || (month == '05') || (month == '07') || (month == '08') || (month == '10') || (month == '12'))) {
        err = 25;
    }
    if ((day > 30) && ((month == '04') || (month == '06') || (month == '09') || (month == '11'))) {
        err = 26;
    }
    /* if 00 ist entered, no error, deleting the entry */
    if ((day == 0) && (month == 0) && (year == 00)) {
        err = 0; day = ''; month = ''; year = ''; seperator = '';
    }
    /* if no error, write the completed date to Input-Field (e.g. 13.12.2001) */
    if (err == 0) {
        NewDatevalue = day + seperator + month + seperator + year;
    }

    return err;
}

function updateSelectedItemInSelectBox(selectId, value) {
    $('#' + selectId + ' option:selected').removeAttr('selected');
    $('#' + selectId + ' option[value=' + value + ']').attr('selected', 'selected');

}

function updateSelectDates(dateText, inst, selectdayid, selectmonthid) {
    var selectedMonth = dateText.toString().substr(0, 6); // -0 is a hack to convert to int. parseInt returns 0 for parseInt('08')! Bug in javascript ?
    var selectedDay = dateText.toString().substr(6, 2);
    updateSelectedItemInSelectBox(selectdayid, selectedDay);
    updateSelectedItemInSelectBox(selectmonthid, selectedMonth);
    $('#' + selectmonthid).change();
}

function setDynamicOptions(selectElement, availableOptions, selectedValueInAvailableOptions, preselectedValue) {
    selectElement.children().remove();
    selectElement.addOption(availableOptions[selectedValueInAvailableOptions], false);
    if (preselectedValue != '') {
        updateSelectedItemInSelectBox(selectElement.attr('id'), preselectedValue);
    }
}

function toggleTripType(radioName, arrivalDivId) {
    if ($('input:radio[name=' + radioName + ']:checked').val() == "1") {
        $('#' + arrivalDivId).children().each(function() {
            $(this).children('select').each(function() {
                $(this).attr("disabled", "disabled");
            });

            $(this).children("input[type='hidden']").each(function() {
                $(this).datepicker('disable');
            });
        });

    }
    else {
        $('#' + arrivalDivId).children().each(function() {
            $(this).children('select').each(function() {
                if ($(this).attr("disabled") != undefined) {
                    $(this).removeAttr("disabled");
                }
            });

            $(this).children("input[type='hidden']").each(function() {
                $(this).datepicker('enable');
            });
        });
    }
}

function checkDate(selectDepartureDayId, selectDepartureMonthId, selectReturnDayId, selectReturnMonthId) {
    var objSelectDepartureDay = document.getElementById(selectDepartureDayId);
    var objSelectDepartureMonth = document.getElementById(selectDepartureMonthId);
    var objSelectReturnDay = document.getElementById(selectReturnDayId);
    var objSelectReturnMonth = document.getElementById(selectReturnMonthId);
    // set return date
    if (!validateDates(selectDepartureDayId, selectDepartureMonthId, selectReturnDayId, selectReturnMonthId, true)) {
        objSelectReturnDay.selectedIndex = objSelectDepartureDay.selectedIndex;
        objSelectReturnMonth.selectedIndex = objSelectDepartureMonth.selectedIndex;
    }
}

function checkDateDisplay(selectDepartureDayId, selectDepartureMonthId, selectReturnDayId, selectReturnMonthId, hiddenDepartureDateId, hiddenReturnDateId) {
    var objSelectDepartureDay = document.getElementById(selectDepartureDayId);
    var objSelectDepartureMonth = document.getElementById(selectDepartureMonthId);
    var objSelectReturnDay = document.getElementById(selectReturnDayId);
    var objSelectReturnMonth = document.getElementById(selectReturnMonthId);
    var objHiddenDepartureDate = document.getElementById(hiddenDepartureDateId)
    var objHiddenReturnDate = document.getElementById(hiddenReturnDateId)

    var changeReturn = false;
    var changeDeparture = false;

    //Check valid departure date
    var validDepartureDate = checkAndUpdateToValidDate(objSelectDepartureDay, objSelectDepartureMonth);
    objHiddenDepartureDate.value = validDepartureDate; //for datepicker

    //Check valid return date
    var validReturnDate = checkAndUpdateToValidDate(objSelectReturnDay, objSelectReturnMonth);
    objHiddenReturnDate.value = validReturnDate; //for datepicker

    //Check if return or departure must change
    if (validReturnDate != "" && validReturnDate != "0" && (validDepartureDate > validReturnDate)) {
        changeReturn = true;
    }

    if (changeReturn) {
        // set return date
        updateSelectedItemInSelectBox(selectReturnDayId, objSelectDepartureDay.options[objSelectDepartureDay.selectedIndex].value);
        updateSelectedItemInSelectBox(selectReturnMonthId, objSelectDepartureMonth.options[objSelectDepartureMonth.selectedIndex].value);
        objHiddenReturnDate.value = validDepartureDate;
    }
}

function checkAndUpdateToValidDate(objSelectDay, objSelectMonth) {
    var dateValue = objSelectMonth.options[objSelectMonth.selectedIndex].value;
    var selectedYear = dateValue.substring(0, 4);
    var selectedMonth = dateValue.substring(4, dateValue.length);
    var selectedDate = objSelectDay.options[objSelectDay.selectedIndex].value + selectedMonth + selectedYear;

    var err = checkDateGeneral(selectedDate);

    if (err != 0) {
        var newDayIndex = 0;
        if (err == 23) {
            newDayIndex = 28; //Leap year
        }
        else if (err == 24) {
            newDayIndex = 27; //February
        }
        else if (err == 25) {
            newDayIndex = 30;
        }
        else if (err == 26) {
            newDayIndex = 29;
        }
        objSelectDay.selectedIndex = newDayIndex;
        return selectedYear + '' + selectedMonth + '' + newDayIndex;
    }
    return selectedYear + '' + selectedMonth + '' + objSelectDay.options[objSelectDay.selectedIndex].value;
}

function isValidDate(date) {
    var year = date.getFullYear();
    var month = date.getMonth() + 1; //Add 1 - first month is 0 (january is 0)
    var day = date.getDate();

    if (!leapYear(year) && month == 2 && (day > 28))
        return false;
    else if ((month == 4
		        || month == 6
		        || month == 9
		        || month == 11
		        ) && (day > 30)) {
        return false;
    }
    else if (leapYear(year) && month == 2 && (day > 29))
        return false;
    return true;
}

function extractDate(objSelectDay, objSelectMonth) {

    var dateValue = objSelectMonth.options[objSelectMonth.selectedIndex].value;
    var selectedYear = dateValue.substring(0, 4);
    var selectedMonth = dateValue.substring(4, dateValue.length);
    return new Date(selectedYear, selectedMonth - 1, objSelectDay.options[objSelectDay.selectedIndex].value);
}

function extractDateAsString(objSelectDay, objSelectMonth) {
    var dateValue = objSelectMonth.options[objSelectMonth.selectedIndex].value;
    var selectedYear = dateValue.substring(0, 4);
    var selectedMonth = dateValue.substring(4, dateValue.length);
    return objSelectDay.options[objSelectDay.selectedIndex].value + selectedMonth + selectedYear;
}