﻿
function validarHoteles() 
{

    document.getElementById("layerError").style.visibility="hidden";
    document.getElementById("destinoAlert").style.visibility="hidden";
    document.getElementById("fechaAlert").style.visibility="hidden";
    document.getElementById("edadesAlert").style.visibility="hidden";
	
	if(!hotel_poblacion_check()) {
 	    document.getElementById("layerError").style.visibility="visible";
 	    document.getElementById("destinoAlert").style.visibility="visible";
 	    document.getElementById("layerError").style.display="";
 	    document.getElementById("destinoAlert").style.display="";
		return;
    }
    if(!hotel_combo_check()) {
 	    document.getElementById("layerError").style.visibility="visible";
 	    document.getElementById("fechaAlert").style.visibility="visible";
 	    document.getElementById("layerError").style.display="";
 	    document.getElementById("fechaAlert").style.display="";
		return;
    }
    if(!hotel_o_paquete_edad_check()) {
 	    document.getElementById("layerError").style.visibility="visible";
 	    document.getElementById("edadesAlert").style.visibility="visible";
 	    document.getElementById("layerError").style.display="";
 	    document.getElementById("edadesAlert").style.display="";
		return;
    }
    var msg='';
    var poblacion = quitarTilde(document.getElementById("inputString").value)
	var fechahotel = cambiarFechaHotel(document.getElementById("fechaEntrada").value)
	var comboNoches = document.getElementById('ctl00_cphPrincipal_Buscador1_noches');            
    var noches = comboNoches.options[comboNoches.selectedIndex].value;	
	var comboCat = document.getElementById('ctl00_cphPrincipal_Buscador1_categoria');
	var categoria = comboCat.options[comboCat.selectedIndex].value;	
	//var categoria = document.getElementById("categoria").value;
	var comboReg = document.getElementById('ctl00_cphPrincipal_Buscador1_regimen');
	var regimen = comboReg.options[comboReg.selectedIndex].value;	
	//var regimen =document.getElementById("regimen").value ;
	var comboHab = document.getElementById("ctl00_cphPrincipal_Buscador1_habitaciones");
	var habitaciones = comboHab.options[comboHab.selectedIndex].value;	
	//var habitaciones = document.getElementById("habitaciones").value;
    var ocupacion='';
	var edades='';
	var edadTempAux = "";
	
//	var isBancotel = false;
//	var isDebug = false;
	
//	if(document.getElementById("flagBancotel").value!='') {
//		isBancotel = true;
//	}
//	if(document.getElementById("flagDebug").value!='') {
//		isDebug = true;
//	}
	for(var i=1;i<=habitaciones;i++) {
	
		//obtener ocupaciones y edades
		ocupacion += document.getElementById("ctl00_cphPrincipal_Buscador1_habitacion_" + i).value + "/";
		//ocupacion += document.getElementById("habitacion-" + i).value + "/";
		var n = document.getElementById("ctl00_cphPrincipal_Buscador1_habitacion_" + i).value.split("-")[1]; //cuantos ninos
		//var n = document.getElementById("habitacion-" + i).value.split("-")[1]; //cuantos ninos
		n = (!n ? 0 : parseInt(n));
		var ed=(n == 0 ? "--" : "");
		for(var j=1;j<=n;j++) {
		edadTempAux =document.getElementById("edad"+i+"-"+j).value + "-";
			//Si peta al cogerlo por JQuery, lo intentamos coger por el metodo tradicional que nunca falla
			if(edadTempAux == "undefined-") {
				edadTempAux = document.getElementById("edad" + i + "-" + j)[document.getElementById("edad" + i + "-" + j).selectedIndex].text + "-";
			}
			ed += edadTempAux;
		}
		ed = ed.substring(0,ed.length-1);
		edades += ed + "/";
	}
	
	ocupacion = ocupacion.substring(0,ocupacion.length-1);
	//loading2();
  
    poblacion=trimBuscador(poblacion);
   
	var url='/disponibilidad/resultados.aspx?pob=' + poblacion + '&cat=' + categoria + '&ocupa=' + ocupacion + '&edades=' + edades + '&numhab=' + habitaciones + '&ureg=' + regimen + '&fent=' + fechahotel + '&n=' + noches;
 	
    var uri=document.location.href;
	uri = uri.substring(0,uri.lastIndexOf("/"));
	//alert(uri + url);
	
    document.location.href= uri + url;
	return false;
    }


function hotel_poblacion_check() {

if (document.getElementById('inputString').value == '') 
  	{
     return false;
    }
  	else
  	{
    return true;
  	}
   
   
}
function hotel_o_paquete_edad_check() {

    //segurar que los edades estan rellenados
    var allgood = true;
    for(var i=1;i<=$("#habitaciones").attr("value");i++) {
				n = $("#habitacion-" + i).attr("value").split("-")
			n= (n[1]? n[1] : 0);
				for(var j=1;j<=n;j++) {
					if(document.getElementById("edad" + i + "-" + j).selectedIndex == 0) { allgood = false; }
				}
            }
   return allgood;
}


function trimBuscador(cadena) {

    for(i=0; i<cadena.length; )
    {
    if(cadena.charAt(i)==String.fromCharCode(160))
        cadena=cadena.substring(i+1, cadena.length);
    else
    break;
    }

    for(i=cadena.length-1; i>=0; i=cadena.length-1)
    {
    if(cadena.charAt(i)== String.fromCharCode(160))
        cadena=cadena.substring(0,i);
    else
    break;
    }
   
    return cadena;
}


function LTrim(value) {
	var re = /\s*((\S+\s*)*)/;
    return value.replace(re, "$1");
}

function RTrim(value) {
   
	var re = /((\s*\S+)*)\s*/;
	return value.replace(re, "$1");
}

function trim(value) {
    
	return LTrim(RTrim(value));
}

function GetDiaNombre(fecha) {
	//fecha tiene que estar en formato dd/mm/yy o d/m/yy o yyyy
	m = fecha.match(/^(\d*)(\/?)(\d*)(\2)(\d\d)?(\d\d)$/);
	m[1] = mLength(m[1]); // 6 -> 06 dia
	m[3] = mLength(m[3]); // 6 -> 06 mes
	m[5] = yLength(m[5]); // 07 -> 2007 anio
	d =["Domingo","Lunes","Martes","Miércoles","Jueves","Viernes","Sábado","Domingo"];
	return d[new Date(m[5] + m[6] + "/" + m[3] + "/" + m[1]).getDay()];
}
function yLength(y) {
	y = (parseInt(y) < 100 ? "20" + y : y);
	return y;
}
function mLength(m) {
	m = (m.length == 1 ? "0" + m : m);
	return m;
}

function cambiarFechaHotel(fecha) {
	var iArr = fecha.split('/');
	y = yLength(iArr[2]);
	m = mLength(iArr[1]);
	d = mLength(iArr[0]);
	fecha = y + '-' + m + '-' + d;
	return fecha;
}
function quitarTilde(str) {
	rep ={"á":"-aacute","é":"-eacute","í":"-iacute","ó":"-oacute","ú":"-uacute","Á":"-Aacute","É":"-Eacute","Í":"-Iacute","Ó":"-Oacute","Ú":"-Uacute","ñ":"-ntilde","Ñ":"-Ntilde"};
	for(var p in rep) {
		str=str.replace(p,rep[p]);
	}
	return str;
}

//funciones para probar si el formulario puede submit o no
function hotel_combo_check() {
	probarFecha = ($("#ctl00_cphPrincipal_Buscador1_entradaD").attr("value") + "/" + $("#ctl00_cphPrincipal_Buscador1_entradaM").attr("value") + "/" + $("#entradaY").attr("value"));
	//probarFecha = ($("#entradaD").attr("value") + "/" + $("#entradaM").attr("value") + "/" + $("#entradaY").attr("value"));
    
	var reg = /^((((0?[1-9]|[12]\d|3[01])[\.\-\/](0?[13578]|1[02])[\.\-\/]((1[6-9]|[2-9]\d)?\d{2}))|((0?[1-9]|[12]\d|30)[\.\-\/](0?[13456789]|1[012])[\.\-\/]((1[6-9]|[2-9]\d)?\d{2}))|((0?[1-9]|1\d|2[0-8])[\.\-\/]0?2[\.\-\/]((1[6-9]|[2-9]\d)?\d{2}))|(29[\.\-\/]0?2[\.\-\/]((1[6-9]|[2-9]\d)?(0[48]|[2468][048]|[13579][26])|((16|[2468][048]|[3579][26])00)|00)))|(((0[1-9]|[12]\d|3[01])(0[13578]|1[02])((1[6-9]|[2-9]\d)?\d{2}))|((0[1-9]|[12]\d|30)(0[13456789]|1[012])((1[6-9]|[2-9]\d)?\d{2}))|((0[1-9]|1\d|2[0-8])02((1[6-9]|[2-9]\d)?\d{2}))|(2902((1[6-9]|[2-9]\d)?(0[48]|[2468][048]|[13579][26])|((16|[2468][048]|[3579][26])00)|00))))$/;
    if (probarFecha.match(reg)) {
		//probar fecha es mas que ayer y menos que hoy+334
		f =  new Date($("#ctl00_cphPrincipal_Buscador1_entradaM").attr("value") + "/" + $("#ctl00_cphPrincipal_Buscador1_entradaD").attr("value") + "/" + $("#entradaY").attr("value"));
		//f =  new Date($("#entradaM").attr("value") + "/" + $("#entradaD").attr("value") + "/" + $("#entradaY").attr("value"));
		var fArr = $("#fechaHoy").attr("value").split("/");
		hoy = new Date(fArr[1] + "/" + fArr[0] + "/" + fArr[2]);
		diferenciaDias = Math.round((f - hoy)/(24*60*60*1000));
		if(diferenciaDias < -1 ) {
			return false;
		} else {
			return true
        }
    } else {
        return false;
    } 
}


function loading2() {
    document.getElementById("global").style.visibility="hidden";
 	document.getElementById("Espera").style.visibility="visible";
    document.getElementById("Espera").style.display="inline";

}
function pop(pg,t,w,h) {
	window.open(pg,t,'scrollbars=yes,toolbar=no,status=no,resizable=no,width=' + w + ',height=' + h);
}
//manejar popups ej calendarios 
function ninosybebes(callback) {
	if(callback){
		hidepop();
	} else {
		//combo('none');
		load('includes/ninosybebes.aspx', 'formspop');//METER EN EL PROYECTOP ESTA PÁGINA
	}
}
function combo(disp) {
    //apagar o mostrar combos (en ie no puedes flotar elementos encima de un combo)
    for (var f = 0; f < document.forms.length; f++){
        for (var e = 0; e < document.forms[f].length; e++) {
			if(disp =="none") {
				if (document.forms[f].elements[e].options) document.forms[f].elements[e].style.visibility = "hidden";
			} else {
				if (document.forms[f].elements[e].options) document.forms[f].elements[e].style.visibility = "visible";
			}
        }
    }
}
function submitshowhide(val) {
    if(val) {
            document.getElementById("buscarDis").style.visibility="hidden";
            document.getElementById("buscar").style.visibility="visible";
            document.getElementById("buscar").style.display="";
    } else {
            document.getElementById("buscar").style.visibility="hidden";
            document.getElementById("buscarDis").style.visibility="visible";
            document.getElementById("buscarDis").style.display="";
    }
}
//funciones generals para manejar fechas etc
function select(id, indice) {
    //alert(id + " " + indice);
    if(document.getElementById(id) != null) {
	    document.getElementById(id).selectedIndex=indice;
	}
}
function prepDest(cual) {
    v = cual.value
    if (v == "Origen" || v=="Población" || v=="Destino") {
		cual.value="";
		submitshowhide(false);
    }
}

function resetCombos() {
	fecha =document.getElementById("fechaEntrada").value ;
    var fArr = fecha.split("/");
    var d = fArr[0]-1;
    var m = fArr[1]-1;
    //var y = fArr[2]-2008;
    
    var fechaHoy = document.getElementById("fechaHoy").value.split("/");
	var hoy = new Date(fechaHoy[1] + "/" + fechaHoy[0] + "/" + fechaHoy[2]);
	
	//alert(hoy.getFullYear());
    
    var y = fArr[2]-hoy.getFullYear();
    select('ctl00_cphPrincipal_Buscador1_entradaD', d)
//    select('entradaD', d);
    select('ctl00_cphPrincipal_Buscador1_entradaM', m)
    //select('entradaM', m);
    select('entradaY', y);
}
//funciones para probar si el formulario puede submit o no

function fechaCombo() {
	if (hotel_combo_check()) {
		//fecha1 = $("#entradaD").attr("value") + "/" + $("#entradaM").attr("value") + "/" + $("#entradaY").attr("value");
		fecha1 = $("#ctl00_cphPrincipal_Buscador1_entradaD").attr("value") + "/" + $("#ctl00_cphPrincipal_Buscador1_entradaM").attr("value") + "/" + $("#entradaY").attr("value");		
//		fecha2 = $("#ctl00_cphPrincipal_Buscador1_entradaM").attr("value") + "/" + $("#ctl00_cphPrincipal_Buscador1_entradaD").attr("value") + "/" + $("#entradaY").attr("value");
		$("#fechaEntrada").attr("value", fecha1);
		$("#fechaEntradaMos").html(fecha1 + ", "+ GetDiaNombre(fecha1));
		fechaHotelVuelta();
		
		//f =  new Date(fecha2);
		//calendarioCtrl.SetSelectedDate(f,true)
	} else {
		//alert('Fecha no valida');
	}
	submitCheck();
}
function submitCheck() {
//alert('sumit');
//    //if(document.getElementById("hotelform").name) {
//    if (hotel_poblacion_check() && hotel_o_paquete_edad_check() && hotel_combo_check())
//    { submitshowhide(true)}
//    else
//    {submitshowhide(false)}
//    //}
}
function cambiarOcupacion(which, val) 
{
	var n = val.split('-')[1]; //cuantos niños
    n = (n ?  parseInt(n):0 );
    if (n == 0) 
    {
	    document.getElementById("edades" + which).style.visibility="hidden";
   		for(var i =1;i<4;i++) 
		{ 
		    document.getElementById("edad" + which + "-" + i).style.visibility="hidden";
		}
    }
    else 
    {
	    document.getElementById("edades" + which ).style.visibility="visible";
	    document.getElementById("edades" + which ).style.display="";
	    
		for(var i =1;i<4;i++) 
		{ 
		    document.getElementById("edad" + which + "-" + i).style.visibility="hidden";
		}
		for(i=1;i<=n;i++) 
		{ 
	    document.getElementById("edad" + which + "-" + i).style.visibility="visible";
		    document.getElementById("edad" + which + "-" + i).style.display="";
		}
	} 
	submitCheck()
}
function cambiarHabitaciones(val) 
{
	for(var i =1;i<5;i++) 
	{
	    document.getElementById("hab" + i).style.visibility="hidden";
	    document.getElementById("edades" + i).style.visibility="hidden";
	    document.getElementById("edad" + i + "-" + 1).style.visibility="hidden";
	    document.getElementById("edad" + i + "-" + 2).style.visibility="hidden";
	    document.getElementById("edad" + i + "-" + 3).style.visibility="hidden";
	}
	for(i =1;i<=val;i++) 
	{
	    document.getElementById("hab" + i).style.visibility="visible";
	    document.getElementById("hab" + i).style.display="";
	    document.getElementById("ctl00_cphPrincipal_Buscador1_habitacion_" + i).selectedIndex=4;

	}
	submitCheck()
}
function fechaHotelCB(fecha) {
    if(fecha){
    
        document.getElementById("fechaEntrada").value= fecha;
        resetCombos();
        document.getElementById("fechaEntradaMos").value=fecha + ", "+ GetDiaNombre(fecha);
        fechaHotelVuelta();
    };
    hidepop();
	submitCheck();
}
function fechaHotelVuelta() {

    fecha =document.getElementById("fechaEntrada").value;
    var comboNoches = document.getElementById('ctl00_cphPrincipal_Buscador1_noches');            
    n = parseInt(comboNoches.options[comboNoches.selectedIndex].value);
    var fArr = fecha.split("/");
    var d = fArr[0];
    var m = fArr[1]-1;
    var y = fArr[2];
    var fVuelta=new Date();
    fVuelta.setFullYear(y,m,d);
    fVuelta.setDate(fVuelta.getDate()+n);
    fVuelta = fVuelta.getDate() + "/" + (fVuelta.getMonth()+1) + "/" + fVuelta.getFullYear();
    $("#fechaVuelta").html(fVuelta + ", "+ GetDiaNombre(fVuelta));

}




function load(src, dest) {
        //document.getElementById("dest").style.visibility="hidden";
       window.open(src,"xcvb",'scrollbars=yes,toolbar=no,status=no,resizable=no,width=300,height=300');
}
function hidepop() {
        combo('');
        document.getElementById("formspop").style.visibility="hidden";

}

//// para pestañas pueblo
//function CargarTabPueblo(tab)
//{
//	for(var i=1;i<=5;i++)
//	{
//		
//			if(document.getElementById("tabhotel"+i) && document.getElementById("tabhotel"+i).className == "tabvisible")
//			{
//				document.getElementById("tabhotel"+i).className = "tabnovisible"
//			}
//			
//			if(document.getElementById("ctl00_cphPrincipal_tabinfohotel"+i) && document.getElementById("ctl00_cphPrincipal_tabinfohotel"+i).className == "tabinfohotelsel")
//				document.getElementById("ctl00_cphPrincipal_tabinfohotel"+i).className = "tabinfohotel"
//		
//	}
//	document.getElementById("tabsih").className = "tabinfohotelesp"
//    if(document.getElementById("tabhotel"+tab))
//	    document.getElementById("tabhotel"+tab).className = "tabvisible"
//	if(document.getElementById("ctl00_cphPrincipal_tabinfohotel"+tab))
//	    document.getElementById("ctl00_cphPrincipal_tabinfohotel"+tab).className = "tabinfohotelsel"		
//}


////para pestañas
//function CargarTabTiempo(tab)
//{
//	for(var i=1;i<=3;i++)
//	{
//		
//			if(document.getElementById("tabinfohotel"+i) && document.getElementById("calendarios"+i).className == "contenedorCalendariosOculto")
//			{
//				document.getElementById("calendarios"+i).className = "contenedorCalendarios"
//			}
//			
//		
//	}
//	
//}

//function PTabMover()
//{
//	if(document.getElementById("tabsih").className != "tabsinfohotelesp")
//		document.getElementById("tabsih").className = "tabsinfohotelesp"
//}

//function PTabMout()
//{
//	if(document.getElementById("tabsih").className != "tabsinfohotelesp")
//		document.getElementById("tabsih").className = "tabsinfohotelesp"
//}

//function PTabMoverN(tab)
//{
//	if(tab.className != "tabsinfohotelsel")
//		tab.className='tabsinfohotelres'
//}

//function PTabMoutN(tab)
//{
//	if(tab.className != "tabsinfohotelsel")
//		tab.className='tabsinfohotel'
//}

////para pestañas foto. vídeo
//function CargarTab2(tab)
//{
//	for(var i=1;i<=2;i++)
//	{
//		
//			if(document.getElementById("tabfotos"+i) && document.getElementById("tabfotos"+i).className == "tabvisible")
//			{
//				document.getElementById("tabfotos"+i).className = "tabnovisible"
//			}
//			
//			if(document.getElementById("ctl00_cphPrincipal_multimedia"+i) && document.getElementById("ctl00_cphPrincipal_tabinfofotos"+i).className == "multimediasel")
//				document.getElementById("ctl00_cphPrincipal_multimedia"+i).className = "multimedia"
//		
//	}
//	document.getElementById("tabsih2").className = "multimediaesp"
//    if(document.getElementById("tabfotos"+tab))
//	    document.getElementById("tabfotos"+tab).className = "tabvisible"
//	if(document.getElementById("ctl00_cphPrincipal_multimedia2"+tab))
//	    document.getElementById("ctl00_cphPrincipal_multimedia2"+tab).className = "multimediasel"		
//}

//function PTabMover2()
//{
//	if(document.getElementById("tabsih2").className != "multimediaesp")
//		document.getElementById("tabsih2").className = "multimediaesp"
//}

//function PTabMout2()
//{
//	if(document.getElementById("tabsih2").className != "multimediaesp")
//		document.getElementById("tabsih2").className = "multimediaesp"
//}

//function PTabMoverN2(tab)
//{
//	if(tab.className != "multimediasel")
//		tab.className='multimediares'
//}

//function PTabMoutN2(tab)
//{
//	if(tab.className != "multimediasel")
//		tab.className='multimedia'
//}

////para pestañas
//function CargarTabTiempo(tab)
//{
//	for(var i=1;i<=3;i++)
//	{
//		
//			if(document.getElementById("calendarios"+i) && document.getElementById("calendarios"+i).className == "tabvisible")
//			{
//				document.getElementById("calendarios"+i).className = "tabnovisible"
//			}
//			
//			if(document.getElementById("ctl00_cphPrincipal_pestana"+i) && document.getElementById("ctl00_cphPrincipal_pestana"+i).className == "pestanaSeleccionada")
//				document.getElementById("ctl00_cphPrincipal_pestana"+i).className = "pestana"
//		
//	}
//	document.getElementById("tabsih").className = "cl menuPestanas"
//    if(document.getElementById("calendarios"+tab))
//	    document.getElementById("calendarios"+tab).className = "tabvisible"
//	if(document.getElementById("ctl00_cphPrincipal_pestana"+tab))
//	    document.getElementById("ctl00_cphPrincipal_pestana"+tab).className = "pestanaSeleccionada"		
//}

//function CargarTabCCenter(tab)
//{
//	for(var i=1;i<=2;i++)
//	{
//		
//			if(document.getElementById("tabfotos"+i) && document.getElementById("tabfotos"+i).className == "tabvisible")
//			{
//				document.getElementById("tabfotos"+i).className = "tabnovisible"
//			}
//			
//			if(document.getElementById("ctl00_cphPrincipal_tabinfohotel"+i) && document.getElementById("ctl00_cphPrincipal_tabinfohotel"+i).className == "tabinfohotelsel")
//				document.getElementById("ctl00_cphPrincipal_tabinfohotel"+i).className = "tabinfohotel"
//		
//	}
//	document.getElementById("tabsih2").className = "tabinfohotelesp"
//    if(document.getElementById("tabfotos"+tab))
//	    document.getElementById("tabfotos"+tab).className = "tabvisible"
//	if(document.getElementById("ctl00_cphPrincipal_tabinfohotel2"+tab))
//	    document.getElementById("ctl00_cphPrincipal_tabinfohotel2"+tab).className = "tabinfohotelsel"		
//}
