//js de vuelos
var someVariable = GetCookie('your_cookie_name');
var expDays = 2;
var exp = new Date();
exp.setTime(exp.getTime() + (expDays * 24 * 60 * 60 * 1000));
var flightForm = {};

function getCookieVal(offset){
    var endstr = document.cookie.indexOf(";", offset);
    if (endstr == -1) {
        endstr = document.cookie.length;
    }
    return unescape(document.cookie.substring(offset, endstr));
}

function GetCookie(name){
    var arg = name + "=";
    var alen = arg.length;
    var clen = document.cookie.length;
    var i = 0;
    while (i < clen) {
        var j = i + alen;
        if (document.cookie.substring(i, j) == arg) return getCookieVal(j);
        i = document.cookie.indexOf(" ", i) + 1;
        if (i == 0) break;
    }
    return null;
}

function SetCookie(name, value){
    var argv = SetCookie.arguments;
    var argc = SetCookie.arguments.length;
    var expires = (argc > 2) ? argv[2] : null;
    var path = (argc > 3) ? argv[3] : null;
    var domain = (argc > 4) ? argv[4] : null;
    var secure = (argc > 5) ? argv[5] : false;
    document.cookie = name + "=" + escape(value) +
    ((expires == null) ? "" : ("; expires=" + expires.toGMTString())) +
    ((path == null) ? "" : ("; path=" + path)) +
    ((domain == null) ? "" : ("; domain=" + domain)) +
    ((secure == true) ? "; secure" : "");
}

function cookieForms(){
    var mode = cookieForms.arguments[0];
    for (f = 1; f < cookieForms.arguments.length; f++) {
        formName = cookieForms.arguments[f];
        if (mode == 'open') {
            cookieValue = GetCookie('saved_' + formName);
            if (cookieValue != null) {
                var cookieArray = cookieValue.split('#cf#');
                if (cookieArray.length == document[formName].elements.length) {
                    for (i = 0; i < document[formName].elements.length; i++) {
                        if (cookieArray[i].substring(0, 6) == 'select') {
                            document[formName].elements[i].options.selectedIndex = cookieArray[i].substring(7, cookieArray[i].length - 1);
                        } else if ((cookieArray[i] == 'cbtrue') || (cookieArray[i] == 'rbtrue')) {
                            document[formName].elements[i].checked = true;
                        } else if ((cookieArray[i] == 'cbfalse') || (cookieArray[i] == 'rbfalse')) {
                            document[formName].elements[i].checked = false;
                        } else {
                            document[formName].carrier1.value = (cookieArray[i]);
                            document[formName].elements[i].value = (cookieArray[i]) ? cookieArray[i] : '';
                        }
                    }
                }
            }
        }
        if (mode == 'save') {
            cookieValue = '';
            for (i = 0; i < document[formName].elements.length; i++) {
                fieldType = document[formName].elements[i].type;
                if (fieldType == 'password') {
                    passValue = '';
                } else if (fieldType == 'checkbox') {
                    passValue = 'cb' + document[formName].elements[i].checked;
                } else if (fieldType == 'radio') {
                    passValue = 'rb' + document[formName].elements[i].checked;
                } else if (fieldType == 'select-one' && name == 'fe_dia' && name == 'fs_dia') {
                    passValue = 'select' + document[formName].elements[i].options.selectedIndex;
                } else {
                    passValue = document[formName].elements[i].value;
                }
                cookieValue = cookieValue + passValue + '#cf#';
            }
            cookieValue = cookieValue.substring(0, cookieValue.length - 4); // Remove last delimiter
            SetCookie('saved_' + formName, cookieValue, exp);
        }
    }
}

var sysdate = new Date();
var anio = sysdate.getFullYear();
var mes = sysdate.getMonth();
var locCodeHolder = "";
var locDescHolder = "";
var index_air;
var companyHolder = "";
var numpaxes = 9;

function goSubmit(dat){
    var str = "";
    // Quito los acentos
    flightForm.depCity.value = QuitarAcentos(flightForm.depCity.value);
    flightForm.arrCity.value = QuitarAcentos(flightForm.arrCity.value);
    var msg = "";
    msg = doconcat(msg, groupval(flightForm.depCity.value, 'Ciudad de salida', 1, 1, 0));
    msg = doconcat(msg, groupval(flightForm.arrCity.value, 'Ciudad de llegada', 1, 1, 0));
    msg = doconcat(msg, groupval(flightForm.fe_dia.options[flightForm.fe_dia.selectedIndex].value, 'Día de salida', 0, 1, 1));
    msg = doconcat(msg, groupval(flightForm.fe_mesanyo.options[flightForm.fe_mesanyo.selectedIndex].value, 'Mes de salida', 0, 1, 1));
    var fechadep = flightForm.fe_mesanyo.options[flightForm.fe_mesanyo.selectedIndex].value + "" + flightForm.fe_dia.options[flightForm.fe_dia.selectedIndex].value;
    if (flightForm.queryType[0].checked) {
        msg = doconcat(msg, groupval(flightForm.fs_dia.options[flightForm.fs_dia.selectedIndex].value, 'Día de llegada', 0, 1, 1));
        msg = doconcat(msg, groupval(flightForm.fs_mesanyo.options[flightForm.fs_mesanyo.selectedIndex].value, 'Mes de llegada', 0, 1, 1));
        var fecharet = flightForm.fs_mesanyo.options[flightForm.fs_mesanyo.selectedIndex].value + "" + flightForm.fs_dia.options[flightForm.fs_dia.selectedIndex].value;
        if (fecharet < fechadep) {
            msg = doconcat(msg, "La fecha de llegada tiene que ser mayor que la de salida \n");
        }
    }
    //me fijo que la fecha de salida sea mayor a 2 dias
    // SACA LA HORA DEL SERVIDOR
    var now = new Date();
    now.setTime(dat);

    now.setDate(now.getDate());
    var strnow = now.getFullYear() + "" + addcero(now.getMonth() + 1) + "" + addcero(now.getDate());
    //if (fechadep < strnow)
    //{msg = doconcat(msg,"Error. No puede buscar vuelos para fechas pasadas. \n");}

    var nAdt = 1 * flightForm.paxAdt.options[flightForm.paxAdt.selectedIndex].value;
    var nChd = 1 * flightForm.paxChd.options[flightForm.paxChd.selectedIndex].value;
    var nInf = 1 * flightForm.paxInf.options[flightForm.paxInf.selectedIndex].value;
    if (nInf > nAdt) msg = doconcat(msg, "El número bebés no puede superar al de adultos \n");
    if ((nAdt + nChd) > 9) msg = doconcat(msg, "El número total de adultos y niños no puede ser mayor que nueve. \n");
    if ((nInf + nChd) > (nAdt * 2)) msg = doconcat(msg, "El número bebés y niños no puede superar al doble del número de adultos \n");
    if (flightForm.depTime.value != "" || flightForm.retTime.value != "") {
        flightForm.resultType.value = "H";
    } else {
        flightForm.resultType.value = "P";
    }
    if (msg != '') {
        alert(msg);
    } else {
        str = flightForm.fe_mesanyo.value;
        flightForm.depDate.value = flightForm.fe_dia.value + "/" + str.substring(4) + "/" + str.substring(0, 4);
        str = flightForm.fs_mesanyo.value;
        flightForm.retDate.value = flightForm.fs_dia.value + "/" + str.substring(4) + "/" + str.substring(0, 4);
        //grabo las cookies
        try {
            cookieForms('save', 'flightForm');
        } catch (err) {
        }
        flightForm.submit();
    }
}


function goSubmit2007(dat){

    var str = "";
    // Quito los acentos
    flightForm.depCity.value = QuitarAcentos(flightForm.depCity.value);
    flightForm.arrCity.value = QuitarAcentos(flightForm.arrCity.value);
    var msg = "";
    //groupval(str, nom, islg, isln, isnm)
    msg = doconcat(msg, groupval(flightForm.depCity.value, 'Ciudad de salida', 1, 1, 0));
    msg = doconcat(msg, groupval(flightForm.arrCity.value, 'Ciudad de llegada', 1, 1, 0));
    msg = doconcat(msg, groupval(flightForm.fe_dia.value, 'Día de salida', 0, 1, 1));
    msg = doconcat(msg, groupval(flightForm.fe_mesanyo.value, 'Mes de salida', 0, 1, 1));
    var fechadep = flightForm.fe_mesanyo.value + "" + flightForm.fe_dia.value;
    //comprobamos vuelos directos
    if (flightForm.direct.checked) {
        flightForm.directOnly.value = 'S';
    } else {
        flightForm.directOnly.value = 'N';
    }

    if (flightForm.queryType[0].checked) {
        msg = doconcat(msg, groupval(flightForm.fs_dia.value, 'Día de llegada', 0, 1, 1));
        msg = doconcat(msg, groupval(flightForm.fs_mesanyo.value, 'Mes de llegada', 0, 1, 1));
        var fecharet = flightForm.fs_mesanyo.value + "" + flightForm.fs_dia.value;
        if (fecharet < fechadep) {
            msg = doconcat(msg, "La fecha de llegada tiene que ser mayor que la de salida \n");
        }
    }
    //me fijo que la fecha de salida sea mayor a 2 dias
    // SACA LA HORA DEL SERVIDOR
    var now = new Date();
    now.setTime(dat);

    now.setDate(now.getDate());
    var strnow = now.getFullYear() + "" + addcero(now.getMonth() + 1) + "" + addcero(now.getDate());
    //if (fechadep < strnow)
    //{msg = doconcat(msg,"Error. No puede buscar vuelos para fechas pasadas. \n");}

    var nAdt = 1 * flightForm.paxAdt.options[flightForm.paxAdt.selectedIndex].value;
    var nChd = 1 * flightForm.paxChd.options[flightForm.paxChd.selectedIndex].value;
    var nInf = 1 * flightForm.paxInf.options[flightForm.paxInf.selectedIndex].value;
    if (nInf > nAdt) msg = doconcat(msg, "El número bebés no puede superar al de adultos \n");
    if ((nAdt + nChd + nInf) > 9) msg = doconcat(msg, "El número total de pasajeros no puede ser mayor que nueve. \n");
    if ((nInf + nChd) > (nAdt * 2)) msg = doconcat(msg, "El número bebés y niños no puede superar al doble del número de adultos \n");
    if (flightForm.depTime.value != "" || flightForm.retTime.value != "") {
        flightForm.resultType.value = "H";
    } else {
        flightForm.resultType.value = "P";
    }
    if (msg != '') {
        alert(msg);
    } else {
        str = flightForm.fe_mesanyo.value;
        flightForm.depDate.value = flightForm.fe_dia.value + "/" + str.substring(4) + "/" + str.substring(0, 4);
        str = flightForm.fs_mesanyo.value;
        flightForm.retDate.value = flightForm.fs_dia.value + "/" + str.substring(4) + "/" + str.substring(0, 4);
        //grabo las cookies
        try {
            cookieForms('save', 'flightForm');
        } catch (err) {
        }
        flightForm.submit();
        //alert("LLEGOOO");
    }
}

function addcero(str){
    var str2 = "0" + str;
    return str2.substring(str2.length - 2);
}
function triptypeVuelos(){
    if (flightForm.rrt.checked) {
        flightForm.fs_dia.disabled = false;
        flightForm.fs_mesanyo.disabled = false;
        flightForm.retTime.disabled = false;
    }
    if (flightForm.row.checked) {
        flightForm.fs_dia.disabled = true;
        flightForm.fs_mesanyo.disabled = true;
        flightForm.retTime.disabled = true;
    }
}

function triptypeVuelos2007(){
    if (flightForm.rrt.checked) {
        flightForm.fs_dia.disabled = false;
        flightForm.fs_mesanyo.disabled = false;
        flightForm.retTime.disabled = false;
        flightForm.fechaRetorno.disabled = false;
    }
    if (flightForm.row.checked) {
        flightForm.fs_dia.disabled = true;
        flightForm.fs_mesanyo.disabled = true;
        flightForm.retTime.disabled = true;
        flightForm.fechaRetorno.disabled = true;
    }
}

function Location(url, elementcode, elementname){
    locCodeHolder = elementcode;
    locDescHolder = elementname;
    window.open(url, 'Aeropuertos', 'resizable=yes,toolbars=no,scrollbars=yes,screenX=0,screenY=0,width=633,height=550');
}


function cambiaVH(){
    showVH2007();
    //pasamos variables de uno a otro
    flightHotelForm.depCity.value = flightForm.depCity.value;
    flightHotelForm.arrCity.value = flightForm.arrCity.value;
    flightHotelForm.fe_dia.value = flightForm.fe_dia.value;
    flightHotelForm.fe_mesanyo.value = flightForm.fe_mesanyo.value;
    flightHotelForm.fechaSalida.value = flightForm.fechaSalida.value;
    flightHotelForm.fs_dia.value = flightForm.fs_dia.value;
    flightHotelForm.fs_mesanyo.value = flightForm.fs_mesanyo.value;
    flightHotelForm.fechaRetorno.value = flightForm.fechaRetorno.value;
    flightHotelForm.paxAdt.value = flightForm.paxAdt.value;
    flightHotelForm.paxChd.value = flightForm.paxChd.value;
    flightHotelForm.paxInf.value = flightForm.paxInf.value;
}

function initFechasIEVuelos(formAux){
    // el formulario sobre el que se inician las fechas
    var form = formAux || 'flightForm';
    flightForm = document.getElementById(form) || document[form];

    var fecInicial = new Date();
    flightForm.fe_mesanyo.value = fecInicial.getFullYear() + addcero(fecInicial.getMonth() + 1);
    flightForm.fe_dia.value = addcero(fecInicial.getDate());
    flightForm.fechaSalida.value = flightForm.fe_dia.value + "/" + flightForm.fe_mesanyo.value.substring(4) + "/" + flightForm.fe_mesanyo.value.substring(0, 4);
    //fecha de vuelta
    fecInicial.setDate(fecInicial.getDate() + 1);
    flightForm.fs_mesanyo.value = fecInicial.getFullYear() + addcero(fecInicial.getMonth() + 1);
    flightForm.fs_dia.value = addcero(fecInicial.getDate());
    flightForm.fechaRetorno.value = flightForm.fs_dia.value + "/" + flightForm.fs_mesanyo.value.substring(4) + "/" + flightForm.fs_mesanyo.value.substring(0, 4);
}

function initFechasOtrosVuelos(){
    var fecInicial = new Date();
    flightForm.fe_mesanyo.value = fecInicial.getFullYear() + addcero(fecInicial.getMonth() + 1);
    flightForm.fe_dia.value = addcero(fecInicial.getDate());

    fecInicial.setDate(fecInicial.getDate() + 1);
    flightForm.fs_mesanyo.value = fecInicial.getFullYear() + addcero(fecInicial.getMonth() + 1);
    flightForm.fs_dia.value = addcero(fecInicial.getDate());
}
