function SyncDates(depDayObject, depMonthObject, arrDayObject, arrMonthObject, eventType){
	(eventType == "depart")? setOtherDates('departureDay', 'searchfrm', 3) : setOtherDates('returnDay', 'searchfrm', 3);
}

function mySetDate(date) {
    var calendar = $('calendar');
    var month = date[0].getMonth()+1;
    if (month < 10) month = ""+ "0" + month;
    month = ""+ date[0].getFullYear() + month;
    day = date[0].getDate();
    if (currentCalendar == 0) {
        copyDate = new Date(date[0]);
        $("departureDay").date = copyDate;
        $("departureDay").value = day;
        $("departureMonth").value = month;
        date[0].setDate(day+3);
    }
    var month = date[0].getMonth()+1;
    if (month < 10) month = ""+ "0" + month;
    month = ""+ date[0].getFullYear() + month;
    $("returnDay").value = date[0].getDate();
    $("returnMonth").value = month;
    $("returnDay").date = date[0];
    calendar.style.display='none';
}

function setDays(month, day, year) {
    if(month.constructor != Number) {
        var date = month.options[month.selectedIndex].value;
        var month = getMonth(date) - 1;
        var year = getYear(date);
    }
    Date.DAYSINMONTH[1] = (year && !isLeapYear(year)) ? 28 : 29;
    var index = day.length;
    if(index == Date.DAYSINMONTH[month]) return;
    else if(index < Date.DAYSINMONTH[month]) {
        for(var i = index; i < Date.DAYSINMONTH[month]; i++) {
            day[i] = new Option(i + 1);
        }
    } else if(index > Date.DAYSINMONTH[month]) {
        for(var i = index; i >= Date.DAYSINMONTH[month]; i--) {
            day[i] = null;
        }
    }
    if(day.selectedIndex > index) day.selectedIndex = 0;
}
function getDateArray(length) {
    var arr = [];
    var date = new Date();
    var startYear = date.getFullYear();
    var startMonth = date.getMonth();
    for(var i = 0; i < 12; i++) {
        var num = ((startMonth + i) >= Date.MONTHS.length) ? (startMonth + i) - Date.MONTHS.length : startMonth + i;
        var year = ((startMonth + i) >= Date.MONTHS.length) ? startYear + 1 : startYear;
        var month = (length) ? Date.MONTHS[num].substring(0, 3) : Date.MONTHS[num];
        var value = String(num + 1);
        if(value.length == 1) value = "0" + value;
        arr[i] = [year + value, month + " " + year];
    }
    return arr;
}
function getMonth(date) {
    return Number(date.substring(4, 6));
}
function getYear(date) {
    return Number(date.substring(0, 4));
}
function isLeapYear(year) {
	return (year % 4 ==0);
}
function isValidDate(type, startMonth, startDay, endMonth, endDay) {
    var msg, start, end;
    var now = new Date();
    var valid = function(a, b, msg) {
        if(a.getTime() > b.getTime()) {
            alert(msg);
            return false;
        }
        return true;
    }
    if(type == "creditcard") {
        msg = "Your card valid from date must be before your card valid to date.";
        start = getDate(startDay.options[startDay.selectedIndex].text, startMonth.selectedIndex, 0);
        end = getDate(endDay.options[endDay.selectedIndex].text, endMonth.selectedIndex);
        if(!valid(start, now, "Your card valid from date must be before the current date.")) return false;
        now.setMonth(now.getMonth() - 1);
        if(!valid(now, end, "Your card valid to date must be after the current date.")) return false;
    } else if(type == "departure") {
        var sm = startMonth.options[startMonth.selectedIndex].value;
        var month = (sm.length == 6) ? getMonth(sm) - 1 : sm - 1;
        var year = (sm.length == 6) ? getYear(sm) : now.getFullYear();
        var sDay = (startDay.type == "select-one") ? startDay.options[startDay.selectedIndex].text : startDay.value;
        if(sDay > Date.DAYSINMONTH[month]) {
            alert("There are only " + Date.DAYSINMONTH[month] + " days in " + Date.MONTHS[month]);
            startDay.value = 1;
            return false;
        }

        msg = "Your departure date must be before your return date.";
        start = getDate(year, month, sDay);
        if(!valid(now, start, "Your departure date must be after the current date.")) return false;
        if(!endMonth) return true;
        var em = endMonth.options[endMonth.selectedIndex].value;
        month = (em.length == 6) ? getMonth(em) - 1 : em - 1;
        year = (em.length == 6) ? getYear(em) : year;
        var eDay = (endDay.type == "select-one") ? endDay.options[endDay.selectedIndex].text : endDay.value;
        if(eDay > Date.DAYSINMONTH[month]) {
            alert("There are only " + Date.DAYSINMONTH[month] + " days in " + Date.MONTHS[month]);
            endDay.value = 1;
            return false;
        }
        end = getDate(year, month, eDay);
    } else if (type == "switch") {
        end = getDate(endDay.options[endDay.selectedIndex].text, endMonth.selectedIndex);
        now.setMonth(now.getMonth() - 1);
        if(!valid(now, end, "Your card valid to date must be after the current date.")) return false;
        if (startDay.options[startDay.selectedIndex].text == "----" || startMonth.selectedIndex.text == "--") {
            start = getDate(startDay.options[1].text, startMonth.options[1], 0)
        } else {
            start = getDate(startDay.options[startDay.selectedIndex].text, startMonth.selectedIndex, 0);
            if(!valid(start, now, "Your card valid from date must be before the current date.")) return false;
        }
    }
    return valid(start, end, msg);
}

function dateCompare(low, high, msg, field) {
    if(low.getTime() > high.getTime()) {
        alert(msg);
        field.value = "";
        field.focus();
    }
}
function getDate(year, month, day) {
    var d = new Date();
    d.setDate(1);
    if(typeof(year) != "undefined") d.setYear(year);
    if(typeof(month) != "undefined") d.setMonth(month);
    if(typeof(day) != "undefined") d.setDate(day);
    return d;
}

if(!Date.MONTHS) {
    Date.MONTHS = ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December'];
}
Date.DAYSINMONTH = [31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31];

function setSelected(select, name, bool) {
    for(var i = 0; i < select.options.length; i++) {
        var text = (bool) ? select.options[i].value : select.options[i].text;
        if(text == name) {
            select.selectedIndex = i;
            return;
        }
    }
}
function getSelected(select) {
    return select.options[select.selectedIndex];
}
function deleteOptions(select) {
    select.options.length = 0;
}
function writeOptions(select, arr) {
    select.options.length = 0;
    for(var i = 0; i < arr.length; i++) {
        if(arr[i].constructor == Array) select.options[select.options.length] = new Option(arr[i][1], arr[i][0]);
        else select.options[select.options.length] = new Option(arr[i]);
    }
}
