/*
 *  ena.travel http://www.ena.travel/
 *  list.js
 *
 *  Copyright (c) NSK http://www.nsk.co.jp/
 *
 *  version 1.1
 *  $Date: 2010/07/20 05:07:17 $
 */
document.write("<script type='text/javascript' src='/js/ena/common.js'><\/script>");
document.write("<script type='text/javascript' src='/js/ena/queries.js'><\/script>");
document.write("<script type='text/javascript' src='/js/ena/calendar.js'><\/script>");
document.write("<script type='text/javascript' src='/js/ena/searchparts.js'><\/script>");
document.write("<script type='text/javascript' src='/js/ena/menuparts.js'><\/script>");
document.write("<script type='text/javascript' src='/js/ena/empty_mail_web.js'><\/script>");
document.write("<script type='text/javascript' src='/js/ena/searchMenu.js'><\/script>");

function callSearchGoods(params, item, agent)
{
    // call back function
    var callBackFunc = function (htmlResp) {

        // check error
        if(htmlResp == null) {
            return;
        }

        updateElement(item, htmlResp);
    }
    httpRequestWithParams(agent, params, true, callBackFunc);
}

function callSearchGoods2(item, agent)
{
    // call back function
    var callBackFunc = function (htmlResp) {

        // check error
        if(htmlResp == null) {
            return;
        }

        updateElement(item, htmlResp);
    }
    httpRequest(agent, true, callBackFunc);
}

function submitMenuForm_list()
{
    if(checkSearchLeftForm()) {
        var dep_month = $('select[@name=dep_month] option:selected').val();
        var dep_day   = $('select[@name=dep_day] option:selected').val();
        var dst_month = $('select[@name=dst_month] option:selected').val();
        var dst_day   = $('select[@name=dst_day] option:selected').val();

        var url = document.menu_form.action;
        url += ("/" + encodeURI($('select[@name=departure] option:selected').val()));
        url += ("/" + encodeURI($('select[@name=dst_area] option:selected').val()));
        url += ("/" + encodeURI($('select[@name=dst_country] option:selected').val()));
        url += ("/" + encodeURI($('select[@name=destination] option:selected').val()));
        url += ("/" + encodeURI($('select[@name=dep_year] option:selected').val()));
        url += ("." + encodeURI(dep_month.length == 1 ? '0' + dep_month : dep_month));
        url += ("." + encodeURI(dep_day.length == 1 ? '0' + dep_day : dep_day));
        url += ("-" + encodeURI($('select[@name=dst_year] option:selected').val()));
        url += ("." + encodeURI(dst_month.length == 1 ? '0' + dst_month : dst_month));
        url += ("." + encodeURI(dst_day.length == 1 ? '0' + dst_day : dst_day));
        url += "/-/";

        $("#adultNum").val($("#ps_adt").val());
        $("#childNum").val($("#ps_chd").val());
        $("#infantNum").val($("#ps_inf").val());
        document.dmy_menu_form.action = url;
        document.dmy_menu_form.submit();
    }
    return false;
}

function checkSearchLeftForm() {

    var departure = getValueFromElement('departure');

    if(departure == null || departure.length == 0) {
        alert('出発地を選択してください。');
        return false;
    }

    var dst_area = getValueFromElement('dst_area');
    if(dst_area == null || dst_area.length == 0) {
        alert('目的地：方面を選択してください。');
        return false;
    }

    var dst_country = getValueFromElement('dst_country');
    if(dst_country == null || dst_country.length == 0) {
        alert('目的地：国を選択してください。');
        return false;
    }

    var destination = getValueFromElement('destination');
    if(destination == null || destination.length == 0) {
        alert('目的地：都市を選択してください。');
        return false;
    }

    var depYear = Number(getValueFromElement('dep_year'));
    var depMonth = Number(getValueFromElement('dep_month'));
    var depDay = Number(getValueFromElement('dep_day'));

    var dstYear = Number(getValueFromElement('dst_year'));
    var dstMonth = Number(getValueFromElement('dst_month'));
    var dstDay = Number(getValueFromElement('dst_day'));

    if(depYear < calCurrentYear || (depYear == calCurrentYear && (depMonth < calCurrentMonth || (depMonth == calCurrentMonth && depDay < calCurrentDay)))) {
        alert('出発日の日付が本日より前になっています。');
        return false;
    }

    if(dstYear < depYear || (dstYear == depYear && (dstMonth < depMonth || (dstMonth == depMonth && dstDay < depDay)))) {
        alert('現地出発日の日付が出発日の日付より前になっています。');
        return false;
    }
    return checkPassenger('ps_adt', 'ps_chd', 'ps_inf', 'ps_adt', 'ps_chd', 'ps_inf');
}

function checkPassenger(adt_id, chd_id, inf_id, cp_adt_id, cp_chd_id, cp_inf_id) {

    var adt = document.getElementById(adt_id).value;
    var chd = document.getElementById(chd_id).value;
    var inf = document.getElementById(inf_id).value;
    adt = Number((!adt.match(/[^0-9]+/) ? adt : "0"));
    var chd = Number((!chd.match(/[^0-9]+/) ? chd : "0"));
    var inf = Number((!inf.match(/[^0-9]+/) ? inf : "0"));
    document.getElementById(adt_id).value = adt;
    document.getElementById(chd_id).value = chd;
    document.getElementById(inf_id).value = inf;
    document.getElementById(cp_adt_id).value = adt;
    document.getElementById(cp_chd_id).value = chd;
    document.getElementById(cp_inf_id).value = inf;

    if ((adt + chd + inf) > 9) {
        alert('合計人数は9人以内でなければなりません。');
        return false;
    }
    if (adt < inf) {
        alert('幼児の人数は大人の人数以下でなければなりません。');
        return false;
    }
    if (adt <= 0) {
        alert('大人の人数は最低１名以上選択してください。');
        return false;
    }
    $.cookie("adultNum",encodeURI(adt),{path:"/"});
    $.cookie("childNum",encodeURI(chd),{path:"/"});
    $.cookie("infantNum",encodeURI(inf),{path:"/"});

    return true;
}

function submitRefineForm_list()
{
    if(checkTopForm()) {
        var dep_month = $('select[@name=dep_month] option:selected').val();
        var dep_day = $('select[@name=dep_day] option:selected').val();
        var dst_month = $('select[@name=dst_month] option:selected').val();
        var dst_day = $('select[@name=dst_day] option:selected').val();

        var url = document.menu_form.action;
        url += ("/" + encodeURI($('select[@name=departure] option:selected').val()));
        url += ("/" + encodeURI($('select[@name=dst_area] option:selected').val()));
        url += ("/" + encodeURI($('select[@name=dst_country] option:selected').val()));
        url += ("/" + encodeURI($('select[@name=destination] option:selected').val()));
        url += ("/" + encodeURI($('select[@name=dep_year] option:selected').val()));
        url += ("." + encodeURI(dep_month.length == 1 ? '0' + dep_month : dep_month));
        url += ("." + encodeURI(dep_day.length == 1 ? '0' + dep_day : dep_day));
        url += ("-" + encodeURI($('select[@name=dst_year] option:selected').val()));
        url += ("." + encodeURI(dst_month.length == 1 ? '0' + dst_month : dst_month));
        url += ("." + encodeURI(dst_day.length == 1 ? '0' + dst_day : dst_day));
        url += "/";

        var airline = getValueFromElement('airline');
        if(airline != null && airline.length != 0)
        {
            url = url + airline + "/";
        }
        else
        {
            url = url + "-/";
        }

        url = url + "?seatclass=" + getValueFromElementOfForm(document.refine_form,'seatclass');
        url = url + "&roottype=" + getValueFromElementOfForm(document.refine_form,'roottype');
        url = url + "&timezone=" + getValueFromElementOfForm(document.refine_form,'timezone');
        url = url + "&tickettype=" + getValueFromElementOfForm(document.refine_form,'tickettype');

        document.dmy_menu_form.action = url;
        document.dmy_menu_form.submit();
    }
    return false;
}

function getValueFromElement(elem)
{
    elem = getElementByName(elem);
    if(elem == null) {
        return null;
    }
    if(elem.type == 'select-one' || elem.type == 'select-multiple') {
        var opts = elem.options;
        for(var i = 0; i < opts.length; i++) {
            if(opts[i].selected) {
                return opts[i].value;
            }
        }
        return null;
    } else {
        return elem.value;
    }
}

function setDepCalendar()
{
    var year  = getValueFromElementOfForm(document.menu_form, "dep_year");
    var month = getValueFromElementOfForm(document.menu_form, "dep_month");
    depCalendar.setCalendar(year, month);
    nextDstDay();
}

function setDstCalendar()
{
    var year  = getValueFromElementOfForm(document.menu_form, "dst_year");
    var month = getValueFromElementOfForm(document.menu_form, "dst_month");
    dstCalendar.setCalendar(year, month);
    setBetweenDays();
}

