var idconsulta = 0;  
  
  var residente_avisado = false;
  
  //deshabilitamos la fecha y hora de vuelta
  //document.getElementById('bloqueregreso').disabled=false;
    document.getElementById('origen').focus();
  


  var tipocalendario = 'index';

  var daysBefore = 3;
  var daysVuelta = 7;
  var currentDate = new Date();
  currentDate.setDate(currentDate.getDate() + daysBefore);
  var salidaDate;
  var regresoDate ;

  salidaDate=new Date();
  salidaDate=currentDate;
  regresoDate=new Date(salidaDate.getFullYear(),salidaDate.getMonth(),salidaDate.getDate()+daysVuelta)


  window.document.getElementById('salida').value = (salidaDate.getDate()) + '/' + (salidaDate.getMonth()+1).toString() + '/' + salidaDate.getFullYear();
  if(document.formulariohome.idavuelta.value == 1)
  {
  window.document.getElementById('regreso').value=(regresoDate.getDate()) + '/' + (regresoDate.getMonth()+1).toString() + '/' + regresoDate.getFullYear();
  }

  function dateToLocaleString(dt, cal)
  {
  return dt.getDate().toString() + "/" + (dt.getMonth() + 1).toString() + "/" + dt.getFullYear().toString();
  }

  function stringToDate(strdate)
  {
  var arr=strdate.split("/");
  var date=new Date();
  if (arr.length==3)
  {
  /*var mes =parseInt(arr[1]);
  date.setFullYear(arr[2]);
  date.setMonth(mes-1);
  date.setDate(arr[0]);*/
  date.setTime( Date.parse(arr[1]+"/"+arr[0]+"/"+arr[2]) );
  return date;
  }
  else
  return currentDate;
  }


//PARA EL ENLACE CON LAS OFERTAS
function enlaza_ofertas(origen,destino)
{
    document.location="ofertas.aspx?origenofertas=" + origen + "&checkintrodestino=unchecked&destinofertas=" + destino; 
}

function lanza_busqueda()
{
    var lanzar = true;
    lanzar = verificar_residente(residente_avisado);
    if (lanzar==true)
    {
        validar_campos();
    }
    else
    {
        residente_avisado=true;
    }
}

function lanza_busqueda_spaininfo() {
    var lanzar = true;
    if (document.getElementById('radioSpain') != undefined) {
        if (document.getElementById('radioSpain').checked == true) {
            document.getElementById('destino').value = document.getElementById('selectDestino').value;
        }
    }
    lanzar = verificar_residente(residente_avisado);
    if (lanzar == true) {
        validar_campos_spaininfo();
    }
    else {
        residente_avisado = true;
    }
}
function lanza_busqueda_spaininfo2() {
    var lanzar = true;
    if (document.getElementById('radioSpain') != undefined) {
        if (document.getElementById('radioSpain').checked == true) {
            document.getElementById('destino').value = document.getElementById('selectDestino').value;
        }
    }
    comprobar_validacion_spaininfo2();
}

function lanza_busqueda_blog() {
    //lanzar = verificar_residente(residente_avisado);
    validar_campos_orange();

}
function lanza_busqueda_orange() {
    //lanzar = verificar_residente(residente_avisado);
    validar_campos_orange();

}
function validar_campos_orange() {
    if (window.XMLHttpRequest) //Non-IE Browsers
        GlobalAjaxObjectVal = new XMLHttpRequest(); //Global Variable
    else if (window.ActiveXObject) //IE Browsers
        GlobalAjaxObjectVal = new ActiveXObject("Msxml2.XMLHTTP");  //Global Variable
    else { //Your browsers is not compatible with AJAX
        alert('Your browser is not supported.')
        return; //exit
    }
    //Initialized the Ajax object to access the URL
    var cadNivel = "";

    url = cadNivel + "validaciones.php?origen=" + document.formulariohome.origen.value.toString() + "&destino=" + document.formulariohome.destino.value.toString() + "&salida=" + document.formulariohome.salida.value.toString() + "&regreso=" + document.formulariohome.regreso.value.toString() + "&idayvuelta=" + document.formulariohome.tipovuelo[0].checked.toString() + "&ofertas=0";

    var url_encode = encodeURI(url);

    GlobalAjaxObjectVal.open("GET", url_encode);
    //GlobalAjaxObject.open("POST", document.formulariohome);
    //Sets the function that is run many times.
    GlobalAjaxObjectVal.onreadystatechange = comprobar_validacion_orange;
    //Executes the request
    GlobalAjaxObjectVal.send(null);
    
}

function comprobar_validacion_orange() {
    if (GlobalAjaxObjectVal.readyState == 4) {
        if (GlobalAjaxObjectVal.status == 200) { //200 = success
            text = GlobalAjaxObjectVal.responseText;
            var element = document.getElementById('validaciones');
            element.innerHTML = text;

            var totalninos = 0;
            var totaladultos = 0;
            var error = 0;

            //mostramos los mensajes de error
            if (document.formulariohome.horigen.value == 0) {
                error = 1;
                document.getElementById('avisoorigen').style.display = 'block';
            }
            else {
                document.getElementById('avisoorigen').style.display = 'none';
                document.formulariohome.origen.value = document.getElementById('horigenvalue').value; 
            }

            document.getElementById('avisodestino').style.display = 'none';


            if (document.formulariohome.hdestino.value == 0) {
                error = 1;
                document.getElementById('destinoLabel').innerText = 'Destino no encontrado!!';
                document.getElementById('avisodestino').style.display = 'block';
            }
            else {
                document.formulariohome.destino.value = document.getElementById('hdestinovalue').value; 
            }

            if (document.getElementById('hdistintos').value == 0) {
                error = 1;
                document.getElementById('destinoLabel').innerText = 'Origen igual a destino!!';
                document.getElementById('avisodestino').style.display = 'block';
            }

            if (document.formulariohome.hsalida.value == 0) {
                error = 1;
                document.getElementById('avisosalida').style.display = 'block';
            }
            else {
                document.getElementById('avisosalida').style.display = 'none';
            }

            if (document.formulariohome.hregreso.value == 0) {
                error = 1;
                document.getElementById('avisoregreso').style.display = 'block';
            }
            else {
                document.getElementById('avisoregreso').style.display = 'none';
            }

            if (document.formulariohome.regreso.value.length != 11) {
                document.formulariohome.idavuelta.value = 0;
            }
            else {
                document.formulariohome.idavuelta.value = 1;
            }
            totalninos = parseInt(document.formulariohome.ninos.value) + parseInt(document.formulariohome.bebes.value);
            totaladultos = (document.formulariohome.adultos.value * 2);

            if (error == 0) {
                if (totalninos <= totaladultos) {
                }
                else {
                    alert('El número de bebes mas niños no puede ser mayor que el doble de adultos!!');
                    error = 1
                }
            }
            if (error == 0) {

                componerURL();

            }
        }
    }


}


function componerURL() {

    var url = 'http://viajes.orange.es/index_resultados.html?';

    var origen = document.formulariohome.origen.value.toString();
    origen = Right(origen, 3);
    var destino = document.formulariohome.destino.value.toString();
    destino = Right(destino, 3)

    url = url + '&origen=' + origen;

    var salida = document.getElementById('salida').value.split('/');
    var regreso = document.getElementById('regreso').value.split('/');

    url = url + '&dia_salida=' + salida[0];
    url = url + '&mes_salida=' + salida[1] + '-' + salida[2]; //mes_salida=10-2009
    url = url + '&destino=' + destino;
    url = url + '&dia_regreso=' + regreso[0];
    url = url + '&mes_regreso=' + regreso[1] + '-' + regreso[2];
    url = url + '&adultos=1';
    url = url + '&ninios=0';
    url = url + '&bebes=0';


    if (document.getElementById('checkbox_edreams').checked) {
        url = url + '&edreams=on';
    }
    if (document.getElementById('checkbox_lastmin').checked) {
        url = url + '&lastminute=on';
    }
    if (document.getElementById('checkbox_rumbo').checked) {
        url = url + '&rumbo=on';
    }


    url = url + '&utm_medium=ORANGE&utm_source=ORANGE&utm_campaign=ORA&utm_content=ORA&utm_term=&xtor=AL-21';
    url = url + '&id_marca=1';
 parent.location.href=url;
    /*document.formulariohome2.action = url;
    document.formulariohome2.submit()
    */
    //document.location.href = url ;

    //window.open(url, 'buscador', 'toolbar=no,location=no,directories=no,status=no,menubar=no, resizable=no, fullscreen=yes', '');

}

function comprobar_validacion() {
    if (GlobalAjaxObjectVal.readyState == 4) {
        if (GlobalAjaxObjectVal.status == 200) { //200 = success
            text = GlobalAjaxObjectVal.responseText;
            var element = document.getElementById('validaciones');
            element.innerHTML = text;

            var totalninos = 0;
            var totaladultos = 0;
            var error = 0;

            //mostramos los mensajes de error
            if (document.formulariohome.horigen.value == 0) {
                error = 1;
                document.getElementById('avisoorigen').style.display = 'block';
            }
            else {
                document.getElementById('avisoorigen').style.display = 'none';
                document.formulariohome.origen.value = document.getElementById('horigenvalue').value; //document.formulariohome.horigenvalue.value;
            }

            document.getElementById('avisodestino').style.display = 'none';


            if (document.formulariohome.hdestino.value == 0) {
                error = 1;
                document.getElementById('destinoLabel').innerText = 'Destino no encontrado!!';
                document.getElementById('avisodestino').style.display = 'block';
            }
            else {
                document.formulariohome.destino.value = document.getElementById('hdestinovalue').value; //document.formulariohome.hdestinovalue.value;
            }

            if (document.getElementById('hdistintos').value == 0) {
                error = 1;
                document.getElementById('destinoLabel').innerText = 'Origen igual a destino!!';
                document.getElementById('avisodestino').style.display = 'block';
            }

            if (document.formulariohome.hsalida.value == 0) {
                error = 1;
                document.getElementById('avisosalida').style.display = 'block';
            }
            else {
                document.getElementById('avisosalida').style.display = 'none';
            }

            if (document.formulariohome.hregreso.value == 0) {
                error = 1;
                document.getElementById('avisoregreso').style.display = 'block';
            }
            else {
                document.getElementById('avisoregreso').style.display = 'none';
            }

            if (document.formulariohome.regreso.value.length != 11) {
                document.formulariohome.idavuelta.value = 0;
            }
            else {
                document.formulariohome.idavuelta.value = 1;
            }
            totalninos = parseInt(document.formulariohome.ninos.value) + parseInt(document.formulariohome.bebes.value);
            totaladultos = (document.formulariohome.adultos.value * 2);

            if (error == 0) {
                if (totalninos <= totaladultos) {
                }
                else {
                    alert('El número de bebes mas niños no puede ser mayor que el doble de adultos!!');
                    error = 1
                }
            }
            if (error == 0) {
                //asignamos el valor de incluir gastos
                document.getElementById('hIncluirGastos').value = '0';
                if (document.getElementById('chkincluirgastos').checked) {
                    document.getElementById('hIncluirGastos').value = '1';
                }

                //para que las ventanas se abran en resultados

                
                
                //actualizar_action();

                //se lanza la búsqueda en las webs de las agencias chequeadas

                //lanzar_paralela();
                //se lanza la búsqueda en el portal

                var utms = obtenerUtm();
                var params = '';
                var oneway = 0;
                if (document.formulariohome.tipovuelo[1].checked == true) {
                    oneway = 1;
                }
                params = params + "&oneway=" + oneway;
                var lastminute = document.getElementById('checkbox_lastmin').checked;
                var edreams = document.getElementById('checkbox_edreams').checked;
                var rumbo = document.getElementById('checkbox_rumbo').checked;

                if (lastminute) {
                    params = params + "&slm=1";
                } else {
                    params = params + "&slm=0";
                }

                if (edreams) {
                    params = params + "&sed=1";
                } else {
                    params = params + "&sed=0";
                }

                if (rumbo) {
                    params = params + "&sru=1";
                } else {
                    params = params + "&sru=0";
                }
                
                document.formulariohome.action = 'http://www.mirayvuela.com/resultados.aspx?q=vuelos_baratos&acabado=0' + utms + params;
                
                document.formulariohome.submit()
                var cadNivel = "";


                if (document.getElementById('hNivel') != null) {
                    var nivel = document.getElementById('hNivel').value;
                    switch (nivel) {
                        case "0": cadNivel = ""; break;
                        case "1": cadNivel = "../"; break;
                        case "2": cadNivel = "../../"; break;
                        case "3": cadNivel = "../../../"; break;
                    }

                }

                //tendriamos que ver el nombre del boton y en base a ello poner el verde o el naranja
                //origen.indexOf(_cadpais)>0

                var rutaboton = document.getElementById('btnBuscar').src;

                if (rutaboton.indexOf('new') > 0) {
                    //document.getElementById('btnBuscar').src=cadNivel +'images/ES/botones/btn-buscando-verde.png';
                    document.getElementById('btnBuscar').src = 'http://www.mirayvuela.com/images/ES/botones/btn-buscando-big.gif';
                } else {
                    document.getElementById('btnBuscar').src = 'http://www.mirayvuela.com/images/ES/botones/btn-buscando.png';
                }


                //obtener_id_consulta();

                //registrar_optimizer();


            }
        }
    }
}

function obtener_id_consulta() {

    var url = 'lanzar_busqueda.aspx?';

    //&aeropuertos=1&adultos=1&ninos=0&bebes=0
    url = url + '&origen=' + document.getElementById('origen').value.substring(document.getElementById('origen').value.lastIndexOf(',') + 1);
    url = url + '&destino=' + document.getElementById('destino').value.substring(document.getElementById('destino').value.lastIndexOf(',') + 1);
    url = url + '&salida=' + document.getElementById('salida').value;
    url = url + '&regreso=' + document.getElementById('regreso').value;
    url = url + '&aeropuertos=' + document.getElementById('aeropuertos').value;
    url = url + '&adultos=' + document.getElementById('adultos').value;
    url = url + '&ninos=' + document.getElementById('ninos').value;
    url = url + '&bebes=' + document.getElementById('bebes').value;
    url = url + '&aeropuertos=' + document.getElementById('aeropuertos').value;
    url = url + '&directos=' + document.getElementById('directos').value;
    url = url + '&hIncluirGastos=' + document.getElementById('chkincluirgastos').value;

    if (window.XMLHttpRequest) //Non-IE Browsers
        GlobalAjaxObject = new XMLHttpRequest(); //Global Variable
    else if (window.ActiveXObject) //IE Browsers
        GlobalAjaxObject = new ActiveXObject("Msxml2.XMLHTTP");  //Global Variable
    else { //Your browsers is not compatible with AJAX
        alert('Your browser is not supported.')
        return; //exit
    }
    //Initialized the Ajax object to access the URL
    GlobalAjaxObject.open("GET", url );
    //Sets the function that is run many times.
    GlobalAjaxObject.onreadystatechange = recoger_idconsulta;
    //Executes the request
    GlobalAjaxObject.send(null);

}


function recoger_idconsulta() {

    if (GlobalAjaxObject.readyState == 4) {
        if (GlobalAjaxObject.status == 200) {

            text = GlobalAjaxObject.responseText;
            idconsulta = text;
            document.location = "/resultados.aspx?q=vuelos+baratos&id_consulta=" + idconsulta; 
        }
    }

}
function comprobar_validacion_spaininfo() {
    if (GlobalAjaxObjectVal.readyState == 4) {
        if (GlobalAjaxObjectVal.status == 200) { //200 = success
            text = GlobalAjaxObjectVal.responseText;
            var element = document.getElementById('validaciones');
            element.innerHTML = text;

            var totalninos = 0;
            var totaladultos = 0;
            var error = 0;

            //mostramos los mensajes de error
            if (document.formulariohome.horigen.value == 0) {
                error = 1;
                document.getElementById('avisoorigen').style.display = 'block';
            }
            else {
                document.getElementById('avisoorigen').style.display = 'none';
                document.formulariohome.origen.value = document.getElementById('horigenvalue').value; //document.formulariohome.horigenvalue.value;
            }

            document.getElementById('avisodestino').style.display = 'none';


            if (document.formulariohome.hdestino.value == 0) {
                error = 1;
                document.getElementById('destinoLabel').innerText = 'Destino no encontrado!!';
                document.getElementById('avisodestino').style.display = 'block';
            }
            else {
                document.formulariohome.destino.value = document.getElementById('hdestinovalue').value; //document.formulariohome.hdestinovalue.value;
            }

            if (document.getElementById('hdistintos').value == 0) {
                error = 1;
                document.getElementById('destinoLabel').innerText = 'Origen igual a destino!!';
                document.getElementById('avisodestino').style.display = 'block';
            }

            if (document.formulariohome.hsalida.value == 0) {
                error = 1;
                document.getElementById('avisosalida').style.display = 'block';
            }
            else {
                document.getElementById('avisosalida').style.display = 'none';
            }

            if (document.formulariohome.hregreso.value == 0) {
                error = 1;
                document.getElementById('avisoregreso').style.display = 'block';
            }
            else {
                document.getElementById('avisoregreso').style.display = 'none';
            }

            if (document.formulariohome.regreso.value.length != 11) {
                document.formulariohome.idavuelta.value = 0;
            }
            else {
                document.formulariohome.idavuelta.value = 1;
            }
            totalninos = parseInt(document.formulariohome.ninos.value) + parseInt(document.formulariohome.bebes.value);
            totaladultos = (document.formulariohome.adultos.value * 2);

            if (error == 0) {
                if (totalninos <= totaladultos) {
                }
                else {
                    alert('El número de bebes mas niños no puede ser mayor que el doble de adultos!!');
                    error = 1
                }
            }
            if (error == 0) {
                //asignamos el valor de incluir gastos
                document.getElementById('hIncluirGastos').value = '0';
                if (document.getElementById('chkincluirgastos').checked) {
                    document.getElementById('hIncluirGastos').value = '1';
                }

                //para que las ventanas se abran en resultados
                //actualizar_action();

                //se lanza la búsqueda en las webs de las agencias chequeadas

                //lanzar_paralela();
                //se lanza la búsqueda en el portal

                //document.formulariohome.submit()

                var utms = '&utm_source=SPA&utm_medium=SPAININFO'; //obtenerUtm();

                var url = 'http://www.mirayvuela.com/resultados.aspx?id_af=1&id_marca=1&q=vuelos+baratos&oneway=0&slm=0&sed=0&sru=0' + utms;

                //&aeropuertos=1&adultos=1&ninos=0&bebes=0
                url = url + '&origen=' + document.getElementById('origen').value.substring(document.getElementById('origen').value.lastIndexOf(',') + 1);
                url = url + '&destino=' + document.getElementById('destino').value.substring(document.getElementById('destino').value.lastIndexOf(',') + 1);
                url = url + '&salida=' + document.getElementById('salida').value;
                url = url + '&regreso=' + document.getElementById('regreso').value;
                url = url + '&aeropuertos=' + document.getElementById('aeropuertos').value;
                url = url + '&adultos=' + document.getElementById('adultos').value;
                url = url + '&ninos=' + document.getElementById('ninos').value;
                url = url + '&bebes=' + document.getElementById('bebes').value;
                url = url + '&aeropuertos=' + document.getElementById('aeropuertos').value;
                url = url + '&directos=' + document.getElementById('directos').value;
                url = url + '&hIncluirGastos=' + document.getElementById('chkincluirgastos').value;

                var utms = obtenerUtm();

                url = url + utms;

                window.open(url, 'busqueda', 'fullscreen=yes,scrollbars=yes', '');

                var cadNivel = "";


                if (document.getElementById('hNivel') != null) {
                    var nivel = document.getElementById('hNivel').value;
                    switch (nivel) {
                        case "0": cadNivel = "/"; break;
                        case "1": cadNivel = "/"; break;
                        case "2": cadNivel = "/"; break;
                        case "3": cadNivel = "/"; break;
                    }

                }

                //tendriamos que ver el nombre del boton y en base a ello poner el verde o el naranja
                //origen.indexOf(_cadpais)>0

                var rutaboton = document.getElementById('btnBuscar').src;

                //	            if(rutaboton.indexOf('new')>0){
                //	                //document.getElementById('btnBuscar').src=cadNivel +'images/ES/botones/btn-buscando-verde.png';    
                //	                document.getElementById('btnBuscar').src=cadNivel +'images/ES/botones/btn-buscando-big.gif';
                //	            }else{
                //	                document.getElementById('btnBuscar').src=cadNivel +'images/ES/botones/btn-buscando.png';
                //	            }




                //registrar_optimizer();


            }
        }
    }
}

function comprobar_validacion_spaininfo2() {
    //asignamos el valor de incluir gastos
    document.getElementById('hIncluirGastos').value = '1';
    if (document.getElementById('chkincluirgastos').checked) {
        document.getElementById('hIncluirGastos').value = '1';
    }

    var utms = '&utm_source=SPA&utm_medium=SPAININFO'; //obtenerUtm();

    var url = 'http://www.mirayvuela.com/resultados.aspx?id_af=1&id_marca=1&q=vuelos+baratos&oneway=0&slm=0&sed=0&sru=0' + utms;

    //&aeropuertos=1&adultos=1&ninos=0&bebes=0
    url = url + '&origen=' + document.getElementById('origen').value.substring(document.getElementById('origen').value.lastIndexOf(',') + 1);
    url = url + '&destino=' + document.getElementById('destino').value.substring(document.getElementById('destino').value.lastIndexOf(',') + 1);
    url = url + '&salida=' + document.getElementById('salida').value;
    url = url + '&regreso=' + document.getElementById('regreso').value;
    url = url + '&aeropuertos=' + document.getElementById('aeropuertos').value;
    url = url + '&adultos=' + document.getElementById('adultos').value;
    url = url + '&ninos=' + document.getElementById('ninos').value;
    url = url + '&bebes=' + document.getElementById('bebes').value;
    url = url + '&aeropuertos=' + document.getElementById('aeropuertos').value;
    url = url + '&directos=' + document.getElementById('directos').value;
    url = url + '&hIncluirGastos=' + document.getElementById('chkincluirgastos').value;

    url = url + utms;

    window.open(url, 'busqueda', 'fullscreen=yes,scrollbars=yes', '');

    var cadNivel = "";


    if (document.getElementById('hNivel') != null) {
        var nivel = document.getElementById('hNivel').value;
        switch (nivel) {
            case "0": cadNivel = "/"; break;
            case "1": cadNivel = "/"; break;
            case "2": cadNivel = "/"; break;
            case "3": cadNivel = "/"; break;
        }

    }

    var rutaboton = document.getElementById('btnBuscar').src;

}

function validar_campos()
{
	if (window.XMLHttpRequest) //Non-IE Browsers
		GlobalAjaxObjectVal = new XMLHttpRequest(); //Global Variable
	else if(window.ActiveXObject) //IE Browsers
		GlobalAjaxObjectVal = new ActiveXObject("Msxml2.XMLHTTP");  //Global Variable
	else 
	{ //Your browsers is not compatible with AJAX
		alert('Your browser is not supported.')
		return; //exit
	}
	//Initialized the Ajax object to access the URL
	var cadNivel="";
	
	if(document.getElementById('hNivel')!=null){
	                var nivel =  document.getElementById('hNivel').value;
	                switch(nivel){
	                    case "0":cadNivel="/";break;
	                    case "1":cadNivel="/";break;
	                    case "2":cadNivel="/";break;
	                    case "3":cadNivel="/";break;
	                }
	                
	}

	if (document.getElementById('radioSpain') != undefined) {
	    if (document.getElementById('radioSpain').checked == true) {
	        document.getElementById('destino').value = document.getElementById('selectDestino').value;
	    }
	}
	
    url=cadNivel + "validaciones.php?origen=" + document.formulariohome.origen.value.toString() + "&destino=" + document.formulariohome.destino.value.toString() + "&salida=" + document.formulariohome.salida.value.toString() + "&regreso=" + document.formulariohome.regreso.value.toString() + "&idayvuelta=" + document.formulariohome.tipovuelo[0].checked.toString() + "&ofertas=0";

	var url_encode= encodeURI(url);
	
	GlobalAjaxObjectVal.open("GET", url_encode );
	//GlobalAjaxObject.open("POST", document.formulariohome);
	//Sets the function that is run many times.
    GlobalAjaxObjectVal.onreadystatechange = comprobar_validacion;
	//Executes the request
	GlobalAjaxObjectVal.send(null);
}

function validar_campos_spaininfo() {
    if (window.XMLHttpRequest) //Non-IE Browsers
        GlobalAjaxObjectVal = new XMLHttpRequest(); //Global Variable
    else if (window.ActiveXObject) //IE Browsers
        GlobalAjaxObjectVal = new ActiveXObject("Msxml2.XMLHTTP");  //Global Variable
    else { //Your browsers is not compatible with AJAX
        alert('Your browser is not supported.')
        return; //exit
    }
    //Initialized the Ajax object to access the URL
    var cadNivel = "";

    if (document.getElementById('hNivel') != null) {
        var nivel = document.getElementById('hNivel').value;
        switch (nivel) {
            case "0": cadNivel = "/"; break;
            case "1": cadNivel = "/"; break;
            case "2": cadNivel = "/"; break;
            case "3": cadNivel = "/"; break;
        }

    }

    if (document.getElementById('radioSpain') != undefined) {
        if (document.getElementById('radioSpain').checked == true) {
            document.getElementById('destino').value = document.getElementById('selectDestino').value;
        }
    }

    url = cadNivel + "validaciones.php?origen=" + document.formulariohome.origen.value.toString() + "&destino=" + document.formulariohome.destino.value.toString() + "&salida=" + document.formulariohome.salida.value.toString() + "&regreso=" + document.formulariohome.regreso.value.toString() + "&idayvuelta=" + document.formulariohome.tipovuelo[0].checked.toString() + "&ofertas=0";

    var url_encode = encodeURI(url);

    GlobalAjaxObjectVal.open("GET", url_encode);
    //GlobalAjaxObject.open("POST", document.formulariohome);
    //Sets the function that is run many times.
    GlobalAjaxObjectVal.onreadystatechange = comprobar_validacion_spaininfo;
    //Executes the request
    GlobalAjaxObjectVal.send(null);
}

function validar_campos_spaininfo2() {
    if (window.XMLHttpRequest) //Non-IE Browsers
        GlobalAjaxObjectVal = new XMLHttpRequest(); //Global Variable
    else if (window.ActiveXObject) //IE Browsers
        GlobalAjaxObjectVal = new ActiveXObject("Msxml2.XMLHTTP");  //Global Variable
    else { //Your browsers is not compatible with AJAX
        alert('Your browser is not supported.')
        return; //exit
    }
    //Initialized the Ajax object to access the URL
    var cadNivel = "";

    if (document.getElementById('hNivel') != null) {
        var nivel = document.getElementById('hNivel').value;
        switch (nivel) {
            case "0": cadNivel = "/"; break;
            case "1": cadNivel = "/"; break;
            case "2": cadNivel = "/"; break;
            case "3": cadNivel = "/"; break;
        }

    }

    if (document.getElementById('radioSpain') != undefined) {
        if (document.getElementById('radioSpain').checked == true) {
            document.getElementById('destino').value = document.getElementById('selectDestino').value;
        }
    }

    url = cadNivel + "validaciones.php?origen=" + document.formulariohome.origen.value.toString() + "&destino=" + document.formulariohome.destino.value.toString() + "&salida=" + document.formulariohome.salida.value.toString() + "&regreso=" + document.formulariohome.regreso.value.toString() + "&idayvuelta=" + document.formulariohome.tipovuelo[0].checked.toString() + "&ofertas=0";

    var url_encode = encodeURI(url);

    GlobalAjaxObjectVal.open("GET", url_encode);
    //GlobalAjaxObject.open("POST", document.formulariohome);
    //Sets the function that is run many times.
    GlobalAjaxObjectVal.onreadystatechange = comprobar_validacion_spaininfo;
    //Executes the request
    GlobalAjaxObjectVal.send(null);
}

function Left(str, n) {
	if (n <= 0)
	    return "";
	else if (n > String(str).length)
	    return str;
	else
	    return String(str).substring(0,n);
}
function Right(str, n){
    if (n <= 0)
       return "";
    else if (n > String(str).length)
       return str;
    else {
       var iLen = String(str).length;
       return String(str).substring(iLen, iLen - n);
    }
}

function actualizar_action(){
    var origen=document.formulariohome.origen.value.toString();
    origen=Right(origen,3);
    var destino=document.formulariohome.destino.value.toString();
    destino=Right(destino,3)
    var fecha_ida=document.formulariohome.salida.value.toString();
    var fecha_vuelta= document.formulariohome.regreso.value.toString();
    var ninos=parseInt(document.formulariohome.ninos.value);
    var bebes=parseInt(document.formulariohome.bebes.value );
    var adultos=parseInt(document.formulariohome.adultos.value);
    var oneway = 0;
    if (document.formulariohome.tipovuelo[1].checked == true)
    {
        oneway = 1;
        fecha_vuelta = "";
    }
//    if ((document.formulariohome.regreso.value.toString())!= "")
//        oneway=0;
    var params="";
    //"&origen="+origen+"&destino="+destino+"&salida="+fecha_ida+"&regreso="+fecha_vuelta+"&adultos="+adultos+"&ninos="+ninos+"&bebes="+bebes;

    params= params+"&oneway="+oneway;
    
     var lastminute=document.getElementById('checkbox_lastmin').checked;
    var edreams=document.getElementById('checkbox_edreams').checked;
    var rumbo=document.getElementById('checkbox_rumbo').checked;
    
    if(lastminute){
        params=params+"&slm=1";
    }else{
        params=params+"&slm=0";
    }
    
    if(edreams){
        params=params+"&sed=1";
    }else{
        params=params+"&sed=0";
    }
    
    if(rumbo){
        params=params+"&sru=1";
    }else{
        params=params+"&sru=0";
    }
    //alert(idconsulta);
    //params = params & "&id_consulta=" + idconsulta.toString();
    //alert(params);
    document.formulariohome.action="/resultados3.aspx?q=vuelos+baratos"+params;
    
}

function lanzar_paralela()
{
    var origen=document.formulariohome.origen.value.toString();
    origen=Right(origen,3);
    var destino=document.formulariohome.destino.value.toString();
    destino=Right(destino,3)
    var fecha_ida=document.formulariohome.salida.value.toString();
    var fecha_vuelta= document.formulariohome.regreso.value.toString();
    var ninos=parseInt(document.formulariohome.ninos.value);
    var bebes=parseInt(document.formulariohome.bebes.value );
    var adultos=parseInt(document.formulariohome.adultos.value);
    var oneway=1;
    if ((document.formulariohome.regreso.value.toString())!= "")
        oneway=0;
    
    var params="origen="+origen+"&destino="+destino+"&fecha_ida="+fecha_ida+"&fecha_vuelta="+fecha_vuelta+"&adultos="+adultos+"&ninos="+ninos+"&bebes="+bebes;

    params= params+"&soloida="+oneway;
    
    var lastminute=document.getElementById('checkbox_lastmin').checked;
    var edreams=document.getElementById('checkbox_edreams').checked;
    var rumbo=document.getElementById('checkbox_rumbo').checked;
    
    var h = 600; //screen.availHeight;
    var w = 860; screen.availWidth / 2;

    var browser=navigator.appName;
    var b_version=navigator.appVersion;

    if (browser == "Microsoft Internet Explorer")
    {
        h = 666; document.body.offsetHeight;
        w = 900; document.body.offsetWidth / 2;
        
    }
    
    var cadNivel="";
	            if(document.getElementById('hNivel')!=null){
	                var nivel =  document.getElementById('hNivel').value;
	                switch(nivel){
	                    case "0":cadNivel="";break;
	                    case "1":cadNivel="../";break;
	                    case "2":cadNivel="../../";break;
	                    case "3":cadNivel="../../../";break;
	                }
	                
	            }
    
    if (lastminute)
        window.open(cadNivel + "Salto-a-Viajar.aspx?"+params,"viajar.com", "width=" + w + ",height=" + h +  ",toolbar=1,scrollbars=1,top=0,resizable=yes","");
    if (edreams)    
        window.open(cadNivel + "Salto-a-Edreams.aspx?"+params,"edreams","width=" + w + ",height=" + h +  ",toolbar=1,scrollbars=1,top=0,resizable=yes","");
    if (rumbo)
        window.open(cadNivel + "Salto-a-Rumbo.aspx?"+params,"rumbo","width=" + w + ",height=" + h +  ",toolbar=1,scrollbars=1,top=0,resizable=yes","");
    
}

var avanzadas=false;

function ver_avanzadas()
{
    if (!avanzadas)
    {
        avanzadas=true;
        document.getElementById("desplegada-opciones-avanzadas").style.display="block";
    }
    else
    {
        avanzadas=false;
        document.getElementById("desplegada-opciones-avanzadas").style.display="none";
    }
    
}

/* Añadido en referencia al radio button de idayvuelta/soloida */
function Habilitar_vuelta()
{
    document.getElementById('fechavuelta').style.display='';
    document.getElementById('regreso').style.display='';
    document.getElementById('regreso').disabled = false;
    document.getElementById('horaregreso').disabled = false; 
    document.getElementById('btnCalendarioRegreso').style.display = 'inline';
    //document.getElementById('idayvuelta').style.display = 'inline';
    //document.getElementById('soloida').style.display = 'none';
    document.formulariohome.idavuelta.value=1;
    
}

function Deshabilitar_vuelta()
{
    //fechavuelta
    document.getElementById('fechavuelta').style.display='none';
    document.getElementById('regreso').style.display='none';
    document.getElementById('regreso').disabled = true;
    document.getElementById('regreso').value = '';
    document.getElementById('horaregreso').value = 0; 
    document.getElementById('horaregreso').disabled = true; 
    document.getElementById('btnCalendarioRegreso').style.display = 'none'; 
    //document.getElementById('idayvuelta').style.display = 'none';
    //document.getElementById('soloida').style.display = 'inline';
    document.formulariohome.idavuelta.value=0;
    
}

/**/
function MiCuenta()
    {
        window.open('UserLogin.aspx');
    }
    
function Recargar_alertas(cerrar)
{
    if (cerrar==1)
        window.location.href=window.location.href;
    else
        window.location.href="Alertas.aspx?mode=insert";
    
}    

function Abrir_Gadget()
{
    window.open('http://www.google.com/ig/adde?moduleurl=aplicaciones.t2o.es/mirayvuela/nrj/gadget/myvgadget_gadget_nrj.xml');
}

function Abrir_Newsletter()
{
    window.open('alta-newsletter.aspx','','toolbar=0,location=0,directories=0,status=1,menubar=0,scrollbars=1,resizable=0,width=800,height=500');
}

/************ MANEJO DE LISTADO DE AEROPUERTOS *************/

function elegir_aeropuerto(control)
{
    document.getElementById('hcontrol').value=control;
    document.getElementById('listado-aeropuertos').style.display='block';
    if(control=='origen')
    {
    	if(window.ActiveXObject)
		    document.getElementById('listado-aeropuertos').style.left='400px';
	    else 
            document.getElementById('listado-aeropuertos').style.left='-100px';
    }
    else
    {
        if(control=='destino')
        {
    	    if(window.ActiveXObject)
		        document.getElementById('listado-aeropuertos').style.left='700px';
	        else
                document.getElementById('listado-aeropuertos').style.left='200px';
        }
    }
}

function elegir_aeropuerto_landing(control)
{
    document.getElementById('hcontrol').value=control;
    document.getElementById('listado-aeropuertos').style.display='block';
    if(control=='origen')
        document.getElementById('listado-aeropuertos').style.left='-200px';
    else
    {
        if(control=='destino')
    	    document.getElementById('listado-aeropuertos').style.left='100px';
    }
}

function cerrar_session()
    {
        window.open('UserLogin.aspx?mode=cerrar');
    }

function muestrausuario(){
    if(document.getElementById('hlogado').value != ''){
            document.getElementById('idusuario').innerHTML=document.getElementById('hlogado').value;
            document.getElementById('idusuario').style.display='';
            document.getElementById('micuenta').style.display='none';
            document.getElementById('lisalir').style.display='';
        }
}
function selecciona_value(objInput) { 

    var valor_input = objInput.value; 
    var longitud = valor_input.length; 

    if (objInput.setSelectionRange) { 
        objInput.focus(); 
        objInput.setSelectionRange (0, longitud); 
    } 
    else if (objInput.createTextRange) { 
        var range = objInput.createTextRange() ; 
        range.collapse(true); 
        range.moveEnd('character', longitud); 
        range.moveStart('character', 0); 
        //range.select(); 
    } 
} 

function cerrar_calendario(container)
{
    if (document.getElementById(container)!=null)
    {
        if (document.getElementById(container).style.display=='block')
        {
            document.getElementById(container).style.display='none';
        }
    }
}

function mostrar_formresidentes()
{
    var url='';
    url='residentes.aspx?';    
    
    //llamar por ajax a la pagina alertasform, pasandole como parametro los datos de la busqueda de ofertas
    if (window.XMLHttpRequest) //Non-IE Browsers
	    GlobalAjaxObject = new XMLHttpRequest(); //Global Variable
    else 
        if(window.ActiveXObject) //IE Browsers
	        GlobalAjaxObject = new ActiveXObject("Msxml2.XMLHTTP");  //Global Variable
        else 
        { //Your browsers is not compatible with AJAX
	        alert('Your browser is not supported.')
	        return; //exit
        }
        //Initialized the Ajax object to access the URL
    var url_encode= encodeURI(url)
    GlobalAjaxObject.open("GET", url_encode); 
    //Sets the function that is run many times.
    GlobalAjaxObject.onreadystatechange = recibir_formresidentes; 
    //Executes the request
    GlobalAjaxObject.send(null);
}

function recibir_formresidentes()
{
    if(GlobalAjaxObject.readyState == 4)
    {
	    if(GlobalAjaxObject.status == 200)
	    { //200 = success
	        //asigna la respuesta al contenido del div lbox_alertas
	        //y pone una capa de velado para inhabilitar el resto de la pagina
		    text = GlobalAjaxObject.responseText;
		    document.getElementById('lbox_residentes').innerHTML=text;
		    document.getElementById('lbox_residentes').style.display='block';
		    document.getElementById('conjuntovelado').style.display='block';
		    document.getElementById('velado').style.display='block';
		    //inicializar_form_alerta();
		    //centrar_lbox('lbox_alertas');
	    }
    }
}

function aplicar_residente()
{
    document.getElementById('lbox_residentes').style.display='none';
    document.getElementById('conjuntovelado').style.display='none';
	document.getElementById('velado').style.display='none';
    
    document.getElementById("descuento-residentes").checked = document.getElementById("rblresidente1").checked;
    lanza_busqueda();
    
}

function registrar_optimizer(){
    try {
        _uacct = 'UA-6066882-1';
        urchinTracker("/2151169958/goal");
        } catch (err) { alert(err); }
}

function abrir_enlace(tipo,destino)
{
    var opciones="width=900px,height=600px,toolbar=1,scrollbars=1,top=0,resizable=yes";
    //pageTracker._trackEvent(tipo , "abrir_ventana", destino, 0);
    pageTracker._trackPageview("/jump_patrocinados.aspx?destino=" + destino);
    if(destino=="terminala"){
        window.open("http://clk.tradedoubler.com/click?p(20683)a(1451534)g(16980870)epi(" + tipo + ")epi2("+REF+")url(http://www.terminala.es)","enlace_resultados",opciones,"");
    }
    if(destino=="edreams"){
        window.open("http://clk.tradedoubler.com/click?p(77923)a(1567670)g(17557540)epi(" + tipo + ")epi2("+REF+")url(http://www.edreams.es)","enlace_resultados",opciones,"");
    }
    if(destino=="marsans"){
        window.open("http://clk.tradedoubler.com/click?p(41423)a(1451534)g(17172352)epi(" + tipo + ")epi2("+REF+")url(http://www.marsans.com)","enlace_resultados",opciones,"");
    }

    if (destino=="rumbo")
    {
        window.open("http://clk.tradedoubler.com/click?p(48184)a(1451534)g(16382842)epi(" + tipo + ")epi2("+REF+")url(http://www.rumbo.es/)","enlace_resultados",opciones,"");
    }
    if (destino=="atrapalo")
    {
        window.open("http://clk.tradedoubler.com/click?p(31881)a(1451534)g(16752772)epi(" + tipo + ")epi2("+REF+")url(http://www.atrapalo.com/)","enlace_resultados",opciones,"");
    }
    if (destino=="viajesiberia")
    {
        window.open("http://clk.tradedoubler.com/click?p(57039)a(1451534)g(16796424)epi(" + tipo + ")epi2("+REF+")url(http://viajesiberia.es)","enlace_resultados",opciones,"");
    }
    if (destino=="ebookers")
    {
        window.open("http://clk.tradedoubler.com/click?p(18857)a(1451534)g(16655238)epi(" + tipo + ")epi2("+REF+")url(http://www.ebookers.es)","enlace_resultados",opciones,"");
    }
    
    if (destino=="lastminute")
    {
        window.open("http://clk.tradedoubler.com/click?p(21645)a(1451534)g(12469739)epi(" + tipo + ")epi2("+REF+")url(http://www.es.lastminute.com/)","enlace_resultados",opciones,"");
    }
    
    if (destino=="supersaver")
    {
        window.open("http://clk.tradedoubler.com/click?p(58365)a(1451534)g(17059630)epi(" + tipo + ")epi2("+REF+")url(http://www.supersaver.es)","enlace_resultados",opciones,"");
    }

    if (destino=="destinia")
    {
        window.open("http://clk.tradedoubler.com/click?p(17509)a(1451534)g(16191500)epi(" + tipo + ")epi2("+REF+")url(http://www.destinia.com)","enlace_resultados",opciones,"");
    }

     if (destino=="barcelo")
    {
        window.open("http://clk.tradedoubler.com/click?p(53119)a(1451534)g(16650750)epi(" + tipo + ")epi2("+REF+")url(http://www.barceloviajes.com/)","enlace_resultados",opciones,"");
    }
}

function seleccionarEspana() {
    document.getElementById('destino').style.display = 'none';
    document.getElementById('selectDestino').style.display = 'inline';
}
function seleccionarResto() {
    document.getElementById('destino').style.display = 'inline';
    document.getElementById('selectDestino').style.display = 'none';
}
function obtenerUtm() {
    //alert(window.location.search);
    //&utm_medium=ORANGE&utm_source=ORANGE&utm_campaign=ORA&utm_content=ORA&utm_term=
    
    var cadena_utm=''
    
    var utm_medium = '';
    var utm_source = '';
    var utm_campaign = '';
    var utm_content = '';

    utm_medium = querySt('utm_medium');
    
    utm_source = querySt('utm_source');
    
    utm_campaign = querySt('utm_campaign');
    
    utm_content = querySt('utm_content');
    

    if (utm_medium != '')
        cadena_utm = cadena_utm + 'utm_medium=' + utm_medium;
    if (utm_source !='')
        cadena_utm = cadena_utm + '&utm_source=' + utm_source;
    if (utm_campaign != '')
        cadena_utm = cadena_utm + '&utm_campaign=' + utm_campaign; 
    if (utm_content != '')
        cadena_utm = cadena_utm + '&utm_content=' + utm_content;

    cadena_utm = '&' + cadena_utm;
        
    return cadena_utm;
    
    
}
function querySt(ji) {
    /*if (document.getElementById(ji) != null) 
        return document.getElementById(ji).value;
     else
        return '';
    */
    var rpta = '';
    hu = window.location.search.substring(1);
    gy = hu.split("&");
    for (i = 0; i < gy.length; i++) {
        ft = gy[i].split("=");
        if (ft[0] == ji) {
            rpta = ft[1];
        }
    }
    return rpta;
    
}


function cargarAeropuertos() {
    if (window.XMLHttpRequest) //Non-IE Browsers
        GlobalAjaxObjectVal = new XMLHttpRequest(); //Global Variable
    else if (window.ActiveXObject) //IE Browsers
        GlobalAjaxObjectVal = new ActiveXObject("Msxml2.XMLHTTP");  //Global Variable
    else { //Your browsers is not compatible with AJAX
        alert('Your browser is not supported.')
        return; //exit
    }

    var url;
    url = "/listado-aeropuertos.txt";

    var url_encode = encodeURI(url);

    GlobalAjaxObjectVal.open("GET", url_encode);
    //GlobalAjaxObject.open("POST", document.formulariohome);
    //Sets the function that is run many times.
    GlobalAjaxObjectVal.onreadystatechange = mostrarAeropuertos;
    //Executes the request
    GlobalAjaxObjectVal.send(null);

};

function mostrarAeropuertos() {
    if (GlobalAjaxObjectVal.readyState == 4) {
        if (GlobalAjaxObjectVal.status == 200) { //200 = success
            text = GlobalAjaxObjectVal.responseText;

            var element = document.getElementById('listado-aeropuertos');
            			element.innerHTML=text;
            //			document.getElementById("volverfavoritos").style.display="inline";
            //			document.getElementById("countfavoritos").style.display="none";
            //			document.getElementById("cajapaginadorsuperior").style.display="none";
            //			SearchResultsResetLayers();
        }
    }
};