﻿var enviarInfo=false;
var retornoValidaciones=false;
var cajaPaisReturn='';
var city1='';
var city2='';
var rutaMapa='';
//************book travel**********

function setRutaMapa(ruta){
    rutaMapa=ruta;
}


function ciudadesValidas(enviar){//recibe el parametro boolean que indica si se enviara la info
    enviarInfo=enviar;
    validarRuta(city1,city2);
}

function actualizarCyties(){
   // alertaGenerica(city1 + " " + city2);
    if ((city1!='') && (city2!='')){
        //alert("talvez");
        if ($get('initialMap').style.visibility=='visible'){
                       setDeparture(city1);  
            setArrival(city2); 
        }
    }
}
function cerrarPS2(){
    document.getElementById('psContent2').style.visibility='hidden';
}
//crea las cookies para el book travel
function crearCookies()
{
    var days = 1000;
    crearCookie('cTRIP_TYPE1', document.form1.TRIP_TYPE1.checked, days);
    crearCookie('cTRIP_TYPE2', document.form1.TRIP_TYPE2.checked, days);
    crearCookie('cB_LOCATION_1', document.getElementById('B_LOCATION_1').value.toUpperCase(), days);
    crearCookie('cE_LOCATION_1', document.getElementById('E_LOCATION_1').value.toUpperCase(), days);
    crearCookie('cDepart', document.getElementById('Depart').value, days);
    crearCookie('cReturn', document.getElementById('Return').value, days);
    crearCookie('cSEARCH_BY1', document.form1.SEARCH_BY1.checked, days);
    crearCookie('cSEARCH_BY2', document.form1.SEARCH_BY2.checked, days);
    crearCookie('cCABIN', document.getElementById('CABIN').value, days);
    crearCookie('cadults', document.getElementById('adults').value, days);
    crearCookie('cchild', document.getElementById('child').value, days);
    crearCookie('cinfant', document.getElementById('infant').value, days);
    crearCookie('cCuponNo', document.getElementById('CuponNo').value, days);
    crearCookie('cB_LOCATION_1P', document.getElementById('B_LOCATION_1P').value, days);
    crearCookie('cE_LOCATION_1P', document.getElementById('E_LOCATION_1P').value, days);
}

//carga las cookies del booktravel
function cargarCookies()
{
    if (leerCookie('cTRIP_TYPE1') != null)
    { 
        if (leerCookie('cTRIP_TYPE1') == 'true'){
            document.form1.TRIP_TYPE1.checked = 'checked';
         
            }
        if (leerCookie('cTRIP_TYPE1') == 'false'){
               document.form1.TRIP_TYPE2.checked = 'checked';
            }
    }
    else{
        resetB();
    }
    if (leerCookie('cB_LOCATION_1') != null){
     document.getElementById('B_LOCATION_1').value = leerCookie('cB_LOCATION_1');
     city1=leerCookie('cB_LOCATION_1');
    }
    if (leerCookie('cE_LOCATION_1') != null){
     document.getElementById('E_LOCATION_1').value = leerCookie('cE_LOCATION_1');
     city2=leerCookie('cE_LOCATION_1');
       
     }
    if (leerCookie('cDepart') != null) document.getElementById('Depart').value = leerCookie('cDepart');
    if (leerCookie('cReturn') != null) document.getElementById('Return').value = leerCookie('cReturn');
    
   
    if (leerCookie('cSEARCH_BY1') != null)
    { 
        if (leerCookie('cSEARCH_BY1') == 'true'){
            document.getElementById('SEARCH_BY1').checked = 'checked';
        }
        if (leerCookie('cSEARCH_BY1') == 'false'){
          
            document.getElementById('SEARCH_BY2').checked = 'checked';
            }
    }
    if (leerCookie('cCABIN') != null) {document.getElementById('CABIN').value = leerCookie('cCABIN')};
    if (leerCookie('cadults') != null){ document.getElementById('adults').value = leerCookie('cadults')};
    if (leerCookie('cchild') != null){ document.getElementById('child').value = leerCookie('cchild')};
    if (leerCookie('cinfant') != null){ document.getElementById('infant').value = leerCookie('cinfant')};
    if (leerCookie('cCuponNo') != null){ document.getElementById('CuponNo').value = leerCookie('cCuponNo')};
    if (leerCookie('cB_LOCATION_1P') != null){ document.getElementById('B_LOCATION_1P').value = leerCookie('cB_LOCATION_1P')};
    if (leerCookie('cE_LOCATION_1P') != null){ document.getElementById('E_LOCATION_1P').value = leerCookie('cE_LOCATION_1P')}; 
}

//limpia el bookTravel
function resetB(){
  if ((city1!='') && ($get('initialMap').style.visibility=='visible')){
     setDeparture('');
     resetPoints();
    
    }
    city1='';
    city2='';
    document.getElementById('B_LOCATION_1P').value=scripts.textoDepartingFrom;
    document.getElementById('E_LOCATION_1P').value=scripts.textoTravelingTo;
    document.getElementById('CuponNo').value=scripts.textoDiscountCode;
    document.getElementById('Depart').value=scripts.textoDepart;
    document.getElementById('Return').value=scripts.textoReturn;
    document.getElementById('CABIN').selectedIndex=0;
    document.getElementById('adults').selectedIndex=0;
    document.getElementById('child').selectedIndex=0;
    document.getElementById('infant').selectedIndex=0;
    document.getElementById('SEARCH_BY2').checked='checked';
    document.getElementById('TRIP_TYPE1').checked='checked';
  
}

//carga la ciudad de salida y coloca vacio el destino
function cambiarTravel(){
document.getElementById('E_LOCATION_1P').value= scripts.textoTravelingTo;
city2='';
if (city1.length>1){
    setDeparture(city1);
}
else{
    resetPoints();  
  }
}

//valida las rutas
function validarRuta(origen,destino){
    destinosTaca.wsInfoDestinos.validarRuta(origen,destino,exito,fallo,"nada");
}

function fallo(result){
    alertaGenerica(result.get_message);
    retornoValidaciones=false;
}

function exito(result,contexto){
    if (result==false){
        destinosTaca.wsInfoDestinos.getCiudad(city1,callGetCiudad1,fallo,"nada");
        retornoValidaciones=false;
        return false;
    }
    else{
        if (city2.length>1){
            setArrival(city2);
        }
        if (enviarInfo==true){
             crearCookies();
            
            document.getElementById('form1').action=scripts.actionFormBookTravel;//'wfPruebaCalendar.aspx';
            document.getElementById('form1').submit();
       
        
        }
        return true;
    }
}


function callGetCiudad1(result,contexto){
    destinosTaca.wsInfoDestinos.getCiudad(city2,callGetCiudad2,fallo,result);
}
function callGetCiudad2(result,primeraCiudad){
    alertaGenerica(String.format(scripts.textoRutaInvalida,primeraCiudad.nombre,primeraCiudad.nombrePais,primeraCiudad.codigo,result.nombre,result.nombrePais,result.codigo));
    cambiarTravel();    
 }  


//carga los destinos validos para la ciudad de origen seleccionada
function cargarDestinos(){
    destinosTaca.wsInfoDestinos.getListaCiudades(city1,callDestinos,callFalloDest,"nada");
}
function callDestinos(result,context){
            var i;
            var tot=result.length;
            var iTotal = 1;
            var j=0;
            if (tot>0){
             sContent="<div class='borderBlank'><p class='btnCerrar'><a href='javascript:cerrarPS2();'  style='text-decoration:none;' ><img src='calendario/btCerrar.gif' border='0' alt='' /></a></p>"
           
            for(i=0;i<tot;i++){
                if (j==0){ 
                    sContent =  sContent + "<ul>";
                }
                sContent =  sContent + "<li><a href='#' onClick=setCiudadLista2('" + result[i].codigo + "');><span id='" + result[i].codigo + "'>" + result[i].nombre + ", " + result[i].nombrePais + " (" + result[i].codigo + ")</span></a></li>"
                j = j+1;
                if ((j==14) || (iTotal==tot)) {
                   sContent = sContent + "</ul>";
                    j = 0;
                }
                iTotal = iTotal+1;
            
            //alert(String.format('La ciudad es {0} y el codigo es {1}',result[i].nombre,result[i].codigo));
            }
             sContent= sContent + '</div>'
             document.getElementById("psContent2").innerHTML = sContent;
            }
            else{
                alertaGenerica(scripts.textoNoHayDestinos);
            }
}

function callFalloDest(result){
    alertaGenerica(result.get_message);
   
}

//carga la lista de paises a partir del origen
function listaPaises(cajaPais,coorTop){
  cerrarCalendarPaises();
    cajaPaisReturn=cajaPais;
    if (cajaPais=='E_LOCATION_1P'){     
        if (city1!=''){
        document.getElementById('psContent2').style.visibility = 'visible';
        document.getElementById('psContent2').style.top = coorTop;
       cargarDestinos();
       }
        else{
            alertaGenerica(scripts.textoSeleccionarSalida);
        }
        }
    else{
        document.getElementById('psContent').style.visibility = 'visible';
        document.getElementById('psContent').style.top = coorTop;
    }
  
}

function getSalida(){
    return city1; 
}
function getDestino(){
    return city2;
}

//coloca las ciudad seleccionada en el psContent
function setCiudadLista(ciudad){
    if (cajaPaisReturn=='B_LOCATION_1P') {
        document.getElementById('B_LOCATION_1P').value=ciudad;//document.getElementById(ciudad).firstChild.nodeValue;
        city1=ciudad;
        city2='';
        setDeparture(city1);
        document.getElementById('E_LOCATION_1P').value=scripts.textoTravelingTo;
    }
    else if (cajaPaisReturn=='E_LOCATION_1P') {
        document.getElementById('E_LOCATION_1P').value=ciudad; //document.getElementById(ciudad).firstChild.nodeValue;
        city2=ciudad;
        validarRuta(city1,city2);
    }
     else if (cajaPaisReturn=='CityIDP') {
        document.getElementById('CityIDP').value= document.getElementById(ciudad).firstChild.nodeValue;
        document.getElementById('CityID').value=ciudad;
    }
    else if (cajaPaisReturn=='FrAirportP') {
        document.getElementById('FrAirportP').value= document.getElementById(ciudad).firstChild.nodeValue;
        document.getElementById('FrAirport').value=ciudad;
    }
    
    else if (cajaPaisReturn=='cityP') {
        document.getElementById('cityP').value= document.getElementById(ciudad).firstChild.nodeValue;
        document.getElementById('city').value=ciudad;
    }
    else if (cajaPaisReturn=='cityP2') {
        document.getElementById('cityP2').value= document.getElementById(ciudad).firstChild.nodeValue;
        document.getElementById('city2').value=ciudad;
    }
    else if (cajaPaisReturn=='txtOrigenFlightP') {
        document.getElementById('txtOrigenFlightP').value=ciudad; //document.getElementById(ciudad).firstChild.nodeValue;
        document.getElementById('txtOrigenFlight').value=ciudad;
    }
    else if (cajaPaisReturn=='txtDistinoFlightP') {
        document.getElementById('txtDistinoFlightP').value=ciudad; //document.getElementById(ciudad).firstChild.nodeValue;
        document.getElementById('txtDistinoFlight').value=ciudad;
    }
      else if (cajaPaisReturn=='departCity') {
        document.getElementById('departCity').value=ciudad;
    }
    
    if (cajaPaisReturn=='E_LOCATION_1P'){
      document.getElementById('psContent2').style.visibility = 'hidden';
    }
    else{
      document.getElementById('psContent').style.visibility = 'hidden';
   
    }
    
}

function setCiudadLista2(ciudad){
        document.getElementById('E_LOCATION_1P').value=ciudad; //document.getElementById(ciudad).firstChild.nodeValue;
        city2=ciudad;
        validarRuta(city1,city2);
        document.getElementById('psContent2').style.visibility = 'hidden';
  }
// coloca la ciudad seleccionada desde el swf  
function setCiudad(ciudad){
    var indice1=$get('B_LOCATION_1P').value;
    var indice2=$get('E_LOCATION_1P').value;
    if (((indice1!=scripts.textoDepartingFrom)&&(indice2!=scripts.textoTravelingTo)) || (indice1==scripts.textoDepartingFrom)){
        document.getElementById('B_LOCATION_1P').value=ciudad; //document.getElementById(ciudad).firstChild.nodeValue;
        city1=ciudad;
        city2='';
        document.getElementById('E_LOCATION_1P').value=scripts.textoTravelingTo;
    }
    else if (indice2==scripts.textoTravelingTo){
        document.getElementById('E_LOCATION_1P').value=ciudad; //document.getElementById(ciudad).firstChild.nodeValue;
        city2=ciudad;
        validarRuta(city1,city2);
    }
}

function setCiudad1Temp(){
   document.getElementById('B_LOCATION_1P').value= document.getElementById('B_LOCATION_1P').value.toUpperCase();
    city1 =  document.getElementById('B_LOCATION_1P').value ;
    if (city1.length==3){
         setDeparture(city1);
    }
    else  if (city1.length>3){
         document.getElementById('B_LOCATION_1P').value=document.getElementById('B_LOCATION_1P').value.substring(0,3);
     city1 = document.getElementById('B_LOCATION_1P').value ;
   
    }
     
}

function setCiudad2Temp(){
    document.getElementById('E_LOCATION_1P').value=document.getElementById('E_LOCATION_1P').value.toUpperCase();
    city2 =  document.getElementById('E_LOCATION_1P').value;
    if (city2.length==3){
        setArrival(city2);
    }
     else  if (city2.length>3){
         document.getElementById('E_LOCATION_1P').value=document.getElementById('E_LOCATION_1P').value.substring(0,3);
        city2 =  document.getElementById('E_LOCATION_1P').value;
    }
    

}


//ejecuta las validaciones para el book travel
function validaciones(){

   city1 =  document.getElementById('B_LOCATION_1P').value.toUpperCase() ;
   city2 =  document.getElementById('E_LOCATION_1P').value.toUpperCase();


    document.getElementById('B_LOCATION_1').value=city1;
    document.getElementById('E_LOCATION_1').value=city2;

    var retorno2=false;
       
    if (city1.length!=3){
        alertaGenerica(scripts.textoSeleccionarSalida)
        return false;
    }
    else if (city2.length!=3){
      alertaGenerica(scripts.textoSeleccionarDestino)
        return false;
    }
    else{
        retorno2=validarFechas();
        if (retorno2==true){
            retorno2=validarPasajeros();
            if (retorno2==true){
                quitarTexto('CuponNo');
                ciudadesValidas(true);
            }
            else{
                return retorno2
            }
        }
        else{
            return retorno2
        }
    }
}

//valida los tipos de pasajeros

function setInfantCero(){
 var tipoB=$get('CABIN').selectedIndex;
    
     if (tipoB==1) {
      alertaGenerica(scripts.infantadult);
    $get('infant').selectedIndex=0;
    }
}

function validarPasajeros(){
    var aux=true;
    adults=$get('adults').options[$get('adults').selectedIndex].value;
    adults=adults + 1;
    child=$get('child').options[$get('child').selectedIndex].value;
    infant=$get('infant').options[$get('infant').selectedIndex].value;
    var tipoB=$get('CABIN').selectedIndex;
    
    if ((tipoB==1) && (infant>0)){
        alertaGenerica(scripts.infantadult);
        $get('infant').selectedIndex=0;
        aux=false;
    }
    else if (adults<infant){
        alertaGenerica(scripts.textoAdultoXInfante);
        aux=false;
      
    }
    return aux;    
}

//valida las fechas de partida y de regreso del viaje
function validarFechas(){
        var  vDepart= document.getElementById('Depart').value;
        var  vReturn= document.getElementById('Return').value;
        var aux=true;
         var fechaAct = new Date();
         
         var diaAct = fechaAct.getDate()
         
       
         if (vDepart.length<10){
             alertaGenerica(scripts.textoSeleccioneFSalida);
             aux=false
        }
        else if (vReturn.length<10){
             alertaGenerica(scripts.textoSeleccioneFRegreso);
             aux=false
        }
        else{
        
            var soloDia= vDepart.substring(3,5); 
            var soloMes= vDepart.substring(0,2);
            var soloAnio= vDepart.substring(6,10);
            var soloDia2= vReturn.substring(3,5); 
            var soloMes2= vReturn.substring(0,2);
            var soloAnio2= vReturn.substring(6,10);
            var fechaI = new Date(soloAnio,soloMes,soloDia);
            var fechaF = new Date(soloAnio2,soloMes2,soloDia2);
            if ((fechaI.getMonth()==(fechaAct.getMonth()+1)) && (fechaI.getFullYear()== fechaAct.getFullYear()) && (fechaI.getDate()<diaAct)){
                 alertaGenerica(scripts.textoSalidaMayorActual);
                 aux=false
            }
            else if ((fechaI.getMonth()==fechaF.getMonth()) && (fechaI.getFullYear()>= fechaF.getFullYear()) && (fechaF.getDate()< fechaI.getDate())){
                alertaGenerica(scripts.textoSalidaMayorRegreso);
                aux=false
            }
            else if ((fechaI.getMonth()>fechaF.getMonth()) && (fechaI.getFullYear()== fechaF.getFullYear())){
                alertaGenerica(scripts.textoSalidaMayorRegreso);
                aux=false
            }
            else if (fechaI.getFullYear()> fechaF.getFullYear()){
                alertaGenerica(scripts.textoSalidaMayorRegreso);
                aux=false
            }
        }
     return aux;     
}


//******************funciones del mapa*************************
function resetPoints()
{
 if (bookTravelMap(rutaMapa)!= null){
    if (bookTravelMap(rutaMapa).resetPoints != null)
	bookTravelMap(rutaMapa).resetPoints();
 }
}

function setArrival(codeId)
{
if (bookTravelMap(rutaMapa)!= null){
 if (bookTravelMap(rutaMapa).setArrivalPoint != null)
	bookTravelMap(rutaMapa).setArrivalPoint(codeId);
	}
}

function setDeparture(codeId)
{
if (bookTravelMap(rutaMapa)!= null){
if (bookTravelMap(rutaMapa).setDeparturePoint != null)
	    bookTravelMap(rutaMapa).setDeparturePoint(codeId);
 }	    
}


function bookTravelMap(map)
{  

    return document[map];
   // return null;
}


if(typeof(Sys)!=='undefined')Sys.Application.notifyScriptLoaded();