function jsPonCapaDisplay(eleId, valor){
  var cual = document.getElementById(eleId);
  cual.style.display = valor;
  return false;
}
function jsSuf(alter) {
  return ((alter==1) ? ('_alt') : (''));
}
function jsDiv(alter) {
  return ((alter==1) ? ('_B') : (''));
}
function jsCambiaTipo(alter) {
  var was = jsSuf(alter);
  var wad = jsDiv(alter);
  var t = $F('tip' + was);
  switch(t) {
    case '2':
      jsPonCapaDisplay('divhab' + wad, 'block');
      jsPonCapaDisplay('divapa' + wad, 'none');
      break;
    case '3':
      jsPonCapaDisplay('divhab' + wad, 'none');
      jsPonCapaDisplay('divapa' + wad, 'block');
      break;
    default:
      jsPonCapaDisplay('divhab' + wad, 'block');
      jsPonCapaDisplay('divapa' + wad, 'none');
      break;
  }
}
function jsCambiaNumHab(alter) {
  var was = jsSuf(alter);
  var wad = jsDiv(alter);
  var nt = $F('numh' + was);
  var nn = parseInt(nt);
  for (i=1; i<=3; ++i) {
    if (i<=nn) {
      jsPonCapaDisplay('divhab' + i + wad, 'block');
    } else {
      jsPonCapaDisplay('divhab' + i + wad, 'none');
    }
  }
}
function jsCambiaNumApa(alter) {
  var was = jsSuf(alter);
  var wad = jsDiv(alter);
  var nt = $F('numa' + was);
  var nn = parseInt(nt);
  for (i=1; i<=3; ++i) {
    if (i<=nn) {
      jsPonCapaDisplay('divapa' + i + wad, 'block');
    } else {
      jsPonCapaDisplay('divapa' + i + wad, 'none');
    }
  }
}
function jsCambiaForHab(h, alter) {
  var was = jsSuf(alter);
  var wad = jsDiv(alter);
  var ft = $F('forh' + h + was);
  var fn = parseInt(ft);
  switch (fn) {
    case 1:
    case 2:
    case 3:
    case 4:
    case 8:
    case 9:
    case 10:
    case 11:
      jsPonCapaDisplay('diveda' + h + '1' + wad, 'none');
      jsPonCapaDisplay('diveda' + h + '2' + wad, 'none');
      break;
    case 5:
    case 7:
    case 12:
      jsPonCapaDisplay('diveda' + h + '1' + wad, 'inline');
      jsPonCapaDisplay('diveda' + h + '2' + wad, 'none');
      break;
    case 6:
    case 13:
      jsPonCapaDisplay('diveda' + h + '1' + wad, 'inline');
      jsPonCapaDisplay('diveda' + h + '2' + wad, 'inline');
      break;
  }
}
function jsChkFormBusqueda(v, p, alter) {
  var was = jsSuf(alter);
  var wad = jsDiv(alter);
  var r = false;

  if ($F('lug' + was)=='') {
    alert(txtA[0]);
  } else if (!$F('ffee' + was)) {
    alert(txtA[1]);
  } else if (!$F('ffes' + was)) {
    alert(txtA[2]);
  } else {
    var fechaHoy = new Date();
    fechaHoy = new Date(fechaHoy.getFullYear(), fechaHoy.getMonth(), fechaHoy.getDate());
    
    var arrFee = $F('ffee' + was).split('-');
    var feea = parseInt(arrFee[2], 10);
    var feem = parseInt(arrFee[1], 10);
    var feed = parseInt(arrFee[0], 10);
    feem -= 1;
    var fechaFee = new Date(feea, feem, feed);
    
    var arrFes = $F('ffes' + was).split('-');
    var fesa = parseInt(arrFes[2], 10);
    var fesm = parseInt(arrFes[1], 10);
    var fesd = parseInt(arrFes[0], 10);
    fesm -= 1;
    var fechaFes = new Date(fesa, fesm, fesd);
    
    if (fechaFee < fechaHoy) {
      alert(txtA[3]);
    } else if (fechaFes <= fechaFee) {
      alert(txtA[4]);
    } else {
      var tFee = fechaFee.getTime();
      var tFes = fechaFes.getTime();
      if ((tFee + (15 * 86400000)) < tFes) {
        alert(txtA[12]);
      } else if ((fechaHoy.getTime() + (365 * 86400000)) < tFee)  {
        alert(txtA[19]);
      } else {
        r = true;
      }
    }
    if (r) {
      if ($F('tip' + was) == '2') {
        var i;
        var t = $F('numh' + was);
        for (i=1; i<=t; ++i) {
          var f = $F('forh' + i + was);
          switch (f) {
            case '5':
            case '7':
            case '12':
              if ($F('eda' + i + '1' + was) == '0') {
                r = false;
                alert(txtA[18]);
              }
              break;
            case '6':
            case '13':
              if ($F('eda' + i + '1' + was) == '0') {
                r = false;
                alert(txtA[18]);
              } else if ($F('eda' + i + '2' + was) == '0') {
                r = false;
                alert(txtA[18]);
              }
              break;
          }
        }
      }
    }
    if (r) {
      // url_target se define en el formulario (global)
      //var url = url_target;
      //var url = 'http://php.aviajes.com/online/index.php?';
      var url = 'http://www.abalatravel.com/index.php?';
      //var url = 'http://192.168.50.3/index.php?';
      //var url = 'http://83.165.47.212:5902/index.php?';
      var ser = $('frmBusqueda' + was).serialize();
      //alert(ser);
      ser = ser.replace(/_alt=/g, '=');
      //alert(ser);
      url += ser;
      if (v) {
        window.open(url, 'v', 'scrollbars=yes,width=800,height=725');
      } else {
        window.location = url;
      }
    }
  }
  return false;
}
function jsChkFormCliente() {
  var r = false;

  if ($F('nom')=='') {
    alert(txtA[5]);
  } else if ($F('ape')=='') {
    alert(txtA[6]);
  } else if ($F('dni')=='') {
    alert(txtA[7]);
  } else if ($F('mai')=='') {
    alert(txtA[8]);
  } else if (!mailValidoOnline($F('mai'))) {
    alert(txtA[11]);
  } else if ($F('tel')=='') {
    alert(txtA[9]);
  } else if (!($F('chkAcepto'))) {
    alert(txtA[10]);
  } else {
    if (($F('opt') == 1) || ($F('opt') == 3)) {
      if ($F('tpt') < 1) {
        alert(txtA[13]);
      } else if ($F('fmt') == 0) {
        alert(txtA[14]);
      } else if ($F('fat') == 0) {
        alert(txtA[15]);
      } else if ($F('cvt')=='') {
        alert(txtA[16]);
      } else if ($F('tit')=='') {
        alert(txtA[17]);
      } else {
        r = true;
      }
    } else if (($F('opt') == 5) || ($F('opt') == 6) || ($F('opt') == 7) || ($F('opt') == 9) || ($F('opt') == 10) || ($F('opt') == 11)) {
      if ($F('tpt') < 1) {
        alert(txtA[13]);
      } else {
        r = true;
      }
    } else {
      r = true;
    }
  }
  if (r) {
    $('cmdPagar').hide();
    jsPonCapaDisplay('coche_fantastico', 'block');
  }
  return r;
}
function jsPago(cual) {
  switch (cual) {
    case 1:
      $('opt_1').show();
      $('opt_2').hide();
      $('opt_3').hide();
      $('opt_4').hide();
      $('opt_1_3').show();
      $('opt_3_4').hide();
      break;
    case 2:
      $('opt_1').hide();
      $('opt_2').show();
      $('opt_3').hide();
      $('opt_4').hide();
      $('opt_1_3').hide();
      $('opt_3_4').hide();
      $('opt_5').hide();
      $('opt_6').hide();
      $('opt_7').hide();
      $('opt_9').hide();
      $('opt_10').hide();
      $('opt_11').hide();
      $('opt_5_7').hide();
      break;
    case 3:
      $('opt_1').hide();
      $('opt_2').hide();
      $('opt_3').show();
      $('opt_4').hide();
      $('opt_1_3').show();
      $('opt_3_4').show();
      break;
    case 4:
      $('opt_1').hide();
      $('opt_2').hide();
      $('opt_3').hide();
      $('opt_4').show();
      $('opt_1_3').hide();
      $('opt_3_4').show();
      $('opt_5').hide();
      $('opt_6').hide();
      $('opt_7').hide();
      $('opt_9').hide();
      $('opt_10').hide();
      $('opt_11').hide();
      $('opt_5_7').hide();
      break;
    case 5:
      $('opt_5').show();
      $('opt_6').hide();
      $('opt_7').hide();
      $('opt_9').hide();
      $('opt_10').hide();
      $('opt_11').hide();
      $('opt_2').hide();
      $('opt_4').hide();
      $('opt_5_7').show();
      $('opt_3_4').hide();
      break;
    case 6:
      $('opt_5').hide();
      $('opt_6').show();
      $('opt_7').hide();
      $('opt_9').hide();
      $('opt_10').hide();
      $('opt_11').hide();
      $('opt_2').hide();
      $('opt_4').hide();
      $('opt_5_7').show();
      $('opt_3_4').hide();
      break;
    case 7:
      $('opt_5').hide();
      $('opt_6').hide();
      $('opt_7').show();
      $('opt_9').hide();
      $('opt_10').hide();
      $('opt_11').hide();
      $('opt_2').hide();
      $('opt_4').hide();
      $('opt_5_7').show();
      $('opt_3_4').hide();
      break;
    case 9:
      $('opt_5').hide();
      $('opt_6').hide();
      $('opt_7').hide();
      $('opt_9').show();
      $('opt_10').hide();
      $('opt_11').hide();
      $('opt_2').hide();
      $('opt_4').hide();
      $('opt_5_7').show();
      $('opt_3_4').hide();
      break;
    case 10:
      $('opt_5').hide();
      $('opt_6').hide();
      $('opt_7').hide();
      $('opt_9').hide();
      $('opt_10').show();
      $('opt_11').hide();
      $('opt_2').hide();
      $('opt_4').hide();
      $('opt_5_7').show();
      $('opt_3_4').hide();
      break;
    case 11:
      $('opt_5').hide();
      $('opt_6').hide();
      $('opt_7').hide();
      $('opt_9').hide();
      $('opt_10').hide();
      $('opt_11').show();
      $('opt_2').hide();
      $('opt_4').hide();
      $('opt_5_7').show();
      $('opt_3_4').hide();
      break;
  }
}
function jsEnviarReserEnOnline() {
  var pagina = 'http://server.aviajes.com/php/web/procform_ajax.php?' + $('frmReserva').serialize();
  pagina = base64_encode(pagina);
  new Ajax.Updater('maq_res', 'ax_autocompleter.php?pag=' + pagina, { method: 'post', evalScripts: true });
  return false;
}
function mailValidoOnline(valor) {
  var emailfilter = /^\w+[\+\.\w-]*@([\w-]+\.)*\w+[\w-]*\.([a-z]{2,4}|\d+)$/i;
  var returnval = emailfilter.test(valor);
  return returnval;
}
/********************************************************************************************/
// Control calendario v1
/********************************************************************************************/
function feeAct0 (cal) {
  feeAct(cal, 0);
}
function feeAct1 (cal) {
  feeAct(cal, 1);
}
function feeAct (cal, alter) {
  var was = jsSuf(alter);
  var wad = jsDiv(alter);
  var date = cal.date;
  var time = date.getTime();
  time += Date.DAY;
  var masundia = new Date(time);
  if ($F('ffes' + was) == '') {
    $('ffes' + was).value = masundia.print('%d-%m-%Y');
  } else {
    var arrFes = $F('ffes' + was).split('-');
    var fesa = parseInt(arrFes[2], 10);
    var fesm = parseInt(arrFes[1], 10);
    var fesd = parseInt(arrFes[0], 10);
    fesm -= 1;
    var dateFes = new Date(fesa, fesm, fesd);
    if (dateFes <= date) {
      $('ffes' + was).value = masundia.print('%d-%m-%Y');
    }
  }
}
function fesAct0 (cal) {
  fesAct(cal, 0);
}
function fesAct1 (cal) {
  fesAct(cal, 1)
}
function fesAct (cal, alter) {
  var was = jsSuf(alter);
  var wad = jsDiv(alter);
  var date = cal.date;
  var time = date.getTime();
  time -= Date.DAY;
  var menosundia = new Date(time);
  if ($F('ffee' + was) == '') {
    $('ffee' + was).value = menosundia.print('%d-%m-%Y');
  } else {
    var arrFee = $F('ffee' + was).split('-');
    var feea = parseInt(arrFee[2], 10);
    var feem = parseInt(arrFee[1], 10);
    var feed = parseInt(arrFee[0], 10);
    feem -= 1;
    var dateFee = new Date(feea, feem, feed);
    if (dateFee >= date) {
      $('ffee' + was).value = menosundia.print('%d-%m-%Y');
    }
  }
}
/********************************************************************************************/
// Control calendario v2 - JQuery
/********************************************************************************************/
function feeAct0JQ(dateT) {
  feeActJQ(dateT, 0);
}
function feeAct1JQ(dateT) {
  feeActJQ(dateT, 1);
}
function feeActJQ(dateT, alter) {
  if (dateT != '') {
    var was = jsSuf(alter);
    var wad = jsDiv(alter);
    var date = Text2Date(dateT);
    var time = date.getTime();
    time += 86400000;
    var masundia = new Date(time);
    if ($F('ffes' + was) == '') {
      $('ffes' + was).value = PrintDate(masundia);
    } else {
      var dateFes = Text2Date($F('ffes' + was));
      if (dateFes <= date) {
        $('ffes' + was).value = PrintDate(masundia);
      }
    }
  }
}

function fesAct0JQ(dateT) {
  fesActJQ(dateT, 0);
}
function fesAct1JQ(dateT) {
  fesActJQ(dateT, 1);
}
function fesActJQ(dateT, alter) {
  if (dateT != '') {
    var was = jsSuf(alter);
    var wad = jsDiv(alter);
    var date = Text2Date(dateT);
    var time = date.getTime();
    time -= 86400000;
    var menosundia = new Date(time);
    if ($F('ffee' + was) == '') {
      $('ffee' + was).value = PrintDate(menosundia);
    } else {
      var dateFee = Text2Date($F('ffee' + was));
      if (dateFee >= date) {
        $('ffee' + was).value = PrintDate(menosundia);
      }
    }
  }
}

function Text2Date(dateT) {
  var arrF = dateT.split('-');
  var fa = parseInt(arrF[2], 10);
  var fm = parseInt(arrF[1], 10);
  var fd = parseInt(arrF[0], 10);
  fm -= 1;
  var dateF = new Date(fa, fm, fd);
  return dateF;
}

function PrintDate(dateF) {
  var d = dateF.getDate();
  var m = dateF.getMonth() + 1;
  var a = dateF.getFullYear();
  var f = ((d < 10) ? ('0' + d) : (d)) + '-' + ((m < 10) ? ('0' + m) : (m)) + '-' + a;
  return f;
}

/********************************************************************************************/
function base64_encode( data ) {
    // Encodes data with MIME base64
    // 
    // +    discuss at: http://kevin.vanzonneveld.net/techblog/article/javascript_equivalent_for_phps_base64_encode/
    // +       version: 809.522
    // +   original by: Tyler Akins (http://rumkin.com)
    // +   improved by: Bayron Guevara
    // +   improved by: Thunder.m
    // +   improved by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)        
    // -    depends on: utf8_encode
    // *     example 1: base64_encode('Kevin van Zonneveld');
    // *     returns 1: 'S2V2aW4gdmFuIFpvbm5ldmVsZA=='

    // mozilla has this native
    // - but breaks in 2.0.0.12!
    //if (typeof window['atob'] == 'function') {
    //    return atob(data);
    //}
        
    var b64 = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";
    var o1, o2, o3, h1, h2, h3, h4, bits, i = ac = 0, enc="", tmp_arr = [];
    data = utf8_encode(data);
    
    do { // pack three octets into four hexets
        o1 = data.charCodeAt(i++);
        o2 = data.charCodeAt(i++);
        o3 = data.charCodeAt(i++);

        bits = o1<<16 | o2<<8 | o3;

        h1 = bits>>18 & 0x3f;
        h2 = bits>>12 & 0x3f;
        h3 = bits>>6 & 0x3f;
        h4 = bits & 0x3f;

        // use hexets to index into b64, and append result to encoded string
        tmp_arr[ac++] = b64.charAt(h1) + b64.charAt(h2) + b64.charAt(h3) + b64.charAt(h4);
    } while (i < data.length);
    
    enc = tmp_arr.join('');
    
    switch( data.length % 3 ){
        case 1:
            enc = enc.slice(0, -2) + '==';
        break;
        case 2:
            enc = enc.slice(0, -1) + '=';
        break;
    }

    return enc;
}
function utf8_encode ( string ) {
    // Encodes an ISO-8859-1 string to UTF-8
    // 
    // +    discuss at: http://kevin.vanzonneveld.net/techblog/article/javascript_equivalent_for_phps_utf8_encode/
    // +       version: 811.1414
    // +   original by: Webtoolkit.info (http://www.webtoolkit.info/)
    // +   improved by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // +   improved by: sowberry
    // +    tweaked by: Jack
    // +   bugfixed by: Onno Marsman
    // +   improved by: Yves Sucaet
    // +   bugfixed by: Onno Marsman
    // *     example 1: utf8_encode('Kevin van Zonneveld');
    // *     returns 1: 'Kevin van Zonneveld'

    string = (string+'').replace(/\r\n/g, "\n").replace(/\r/g, "\n");

    var utftext = "";
    var start, end;
    var stringl = 0;

    start = end = 0;
    stringl = string.length;
    for (var n = 0; n < stringl; n++) {
        var c1 = string.charCodeAt(n);
        var enc = null;

        if (c1 < 128) {
            end++;
        } else if((c1 > 127) && (c1 < 2048)) {
            enc = String.fromCharCode((c1 >> 6) | 192) + String.fromCharCode((c1 & 63) | 128);
        } else {
            enc = String.fromCharCode((c1 >> 12) | 224) + String.fromCharCode(((c1 >> 6) & 63) | 128) + String.fromCharCode((c1 & 63) | 128);
        }
        if (enc != null) {
            if (end > start) {
                utftext += string.substring(start, end);
            }
            utftext += enc;
            start = end = n+1;
        }
    }

    if (end > start) {
        utftext += string.substring(start, string.length);
    }

    return utftext;
}

