var dia_inicio=getLyr('lst_dia_fecha_inicio');
var mes_inicio=getLyr('lst_mes_fecha_inicio');
var anyo_inicio=getLyr('lst_anyo_fecha_inicio');
var dia_fin=getLyr('lst_dia_fecha_fin');
var mes_fin=getLyr('lst_mes_fecha_fin');
var anyo_fin=getLyr('lst_anyo_fecha_fin');
var num_noches=getLyr('txt_num_noches');
var num_habitaciones=getLyr('lst_habitaciones');
var hist=getLyr('hdn_hist');
if(!hist){
	hist="";
}
function update_fecha_inicio(){
	var fecha_inicio=new Date();
	fecha_inicio.setDate(1);
	fecha_inicio.setFullYear(anyo_inicio.selectedIndex+2010);
	fecha_inicio.setMonth(mes_inicio.selectedIndex);
	fecha_inicio.setDate(dia_inicio.selectedIndex+1);

	updatePrestigeParams()
}

function update_fecha_fin(){
	var fecha_inicio=new Date();
	fecha_inicio.setDate(1);
	fecha_inicio.setFullYear(anyo_inicio.selectedIndex+2010);
	fecha_inicio.setMonth(mes_inicio.selectedIndex);
	fecha_inicio.setDate(dia_inicio.selectedIndex+1);
	
	var fecha_fin=new Date();
	fecha_fin.setDate(1);
	fecha_fin.setFullYear(anyo_fin.selectedIndex+2010);
	fecha_fin.setMonth(mes_fin.selectedIndex);
	fecha_fin.setDate(dia_fin.selectedIndex+1);
	
	num_noches.value=Date.restaFechas(fecha_inicio,fecha_fin);
	if(parseInt(num_noches.value)<0){
		num_noches.value=0;
	}
	
	updatePrestigeParams()	
}

function update_num_noches(){
	update_fecha_inicio()
}

function updatePrestigeParams(){
	var dia = dia_inicio.selectedIndex+1
	var mes = mes_inicio.selectedIndex+1
	var longDia = new String("'" + dia + "'");
	var longMes = new String("'" + mes + "'");
	if (longDia.length == 3){
		longDia = new String("'0" + dia + "'");
	}
	if (longMes.length == 3){
		longMes = new String("'0" + mes + "'");
	}
	longDia = longDia.substr(1,2)
	longMes = longMes.substr(1,2)
	var anyo = anyo_inicio.selectedIndex+2010
	var shortAnyo = new String("'" + anyo + "'").substr(3, 2);
	document.getElementById("desde").value=longDia + "/" + longMes + "/" + shortAnyo;
	
	dia = dia_fin.selectedIndex+1
	mes = mes_fin.selectedIndex+1
	longDia = new String("'" + dia + "'");
	longMes = new String("'" + mes + "'");
	if (longDia.length == 3){
		longDia = new String("'0" + dia + "'");
	}
	if (longMes.length == 3){
		longMes = new String("'0" + mes + "'");
	}
	longDia = longDia.substr(1,2)
	longMes = longMes.substr(1,2)
	anyo = anyo_fin.selectedIndex+2010
	shortAnyo = new String("'" + anyo + "'").substr(3, 2);
	document.getElementById("hasta").value=longDia + "/" + longMes + "/" + shortAnyo;

url_prestige = '/web/prestige.html?fullintegration=1&channelkey=' + document.getElementById("channelkey").value + '&desde=' + document.getElementById("desde").value + '&hasta=' + document.getElementById("hasta").value + '&idiomaid=' + document.getElementById("idiomaid").value + '&skin=orange2&paisid=1&destinoid=' + document.getElementById("destinoid").value + '&zonaid=' + document.getElementById("zonaid").value + '&codpromo=' + document.getElementById("codpromo").value;

}

function abrirPrestige(){
	var urlPrestige;
	urlPrestige = "channelkey=" + document.getElementById("channelkey").value;
	urlPrestige = urlPrestige + "&desde=" + document.getElementById("desde").value;
	urlPrestige = urlPrestige + "&hasta=" + document.getElementById("hasta").value;
	urlPrestige = urlPrestige + "&skin=orange2&paisid=1";
	urlPrestige = urlPrestige + "&destinoid=" + document.getElementById("destinoid").value;
	urlPrestige = urlPrestige + "&zonaid=" + document.getElementById("zonaid").value;
	document.location = "https://booking.servigroup.com/integracion/webapp.aspx?" + urlPrestige;
	
}

function open_calendar(t){
	var fecha=new Date();
	fecha.setDate(1);
	if(t==1 || t==3){
		fecha.setFullYear(anyo_inicio.selectedIndex+2010);
		fecha.setMonth(mes_inicio.selectedIndex);
		fecha.setDate(dia_inicio.selectedIndex+1);
	}else{
		fecha.setFullYear(anyo_fin.selectedIndex+2010);
		fecha.setMonth(mes_fin.selectedIndex);
		fecha.setDate(dia_fin.selectedIndex+1);
	}
 	window.open('/web/calendario.aspx?idioma=' + idm + '&tipo=' + t + '&fecha=' + fecha.toStr(),'calendario','left=200,top=150,width=250,height=200');
}
function reservar(zh){
	var fecha_inicio=new Date();
	fecha_inicio.setDate(1);
	fecha_inicio.setFullYear(anyo_inicio.selectedIndex+2010);
	fecha_inicio.setMonth(mes_inicio.selectedIndex);
	fecha_inicio.setDate(dia_inicio.selectedIndex+1);
	fecha_inicio.setHours(0,0,0,0); 
	var fecha_fin=new Date();
	fecha_fin.setDate(1);
	fecha_fin.setFullYear(anyo_fin.selectedIndex+2010);
	fecha_fin.setMonth(mes_fin.selectedIndex);
	fecha_fin.setDate(dia_fin.selectedIndex+1);
	fecha_fin.setHours(0,0,0,0);
	
	if(fecha_inicio<=hoy){
		alert(errores['hoy']);
		return false;
	}
	if(fecha_inicio.getDate()!=dia_inicio.selectedIndex+1){
		alert(errores['fecha_inicio']);
		return false;
	}
	if(fecha_fin.getDate()!=dia_fin.selectedIndex+1){
		alert(errores['fecha_fin']);
		return false;
	}
	if(fecha_inicio>fecha_fin){
		alert(errores['num_noches']);
		return false;
	}
	var ruta='https://www.servigroup.es/';
	if(zh==null){
		var zona_hotel=getLyr('lst_zonas_hoteles');
		zh=zona_hotel.options[zona_hotel.selectedIndex].value;
		ruta='';
	}
	hist.value="1";
	if(zh==""){
		if(idm=="en"){
			document.location='https://www.servigroup.com/reservas/reservas.aspx?fe='+fecha_inicio.toStr()+'&fs='+fecha_fin.toStr()+'&nn='+num_noches.value;
		}else{
			document.location='https://www.servigroup.es/reservas/reservas.aspx?fe='+fecha_inicio.toStr()+'&fs='+fecha_fin.toStr()+'&nn='+num_noches.value;
		}
	}else{
		if(idm=="en"){
			document.location='https://www.servigroup.com/reservas/reservas.aspx?zh='+ zh +'&fe='+fecha_inicio.toStr()+'&fs='+fecha_fin.toStr()+'&nn='+num_noches.value;
		}else{
			document.location='https://www.servigroup.es/reservas/reservas.aspx?zh='+ zh +'&fe='+fecha_inicio.toStr()+'&fs='+fecha_fin.toStr()+'&nn='+num_noches.value;
		}
	}
}


document.onreadystatechange=_Document_onload;

addEvent("DOMContentLoaded", _Document_onload, null);


function _Document_onload(){
	if(hist.value!=1){
		var manyana=new Date(hoy);
		
		dia_inicio.selectedIndex=manyana.getDate()-1;
		mes_inicio.selectedIndex=manyana.getMonth();
		anyo_inicio.selectedIndex=manyana.getFullYear()-2010;
		manyana.addDay();
		dia_fin.selectedIndex=manyana.getDate()-1;
		mes_fin.selectedIndex=manyana.getMonth();
		anyo_fin.selectedIndex=manyana.getFullYear()-2010;
	}
	updatePrestigeParams();
}

function checkDates(){
	var fecha_inicio=new Date();
	fecha_inicio.setDate(1);
	fecha_inicio.setFullYear(anyo_inicio.selectedIndex+2010);
	fecha_inicio.setMonth(mes_inicio.selectedIndex);
	fecha_inicio.setDate(dia_inicio.selectedIndex+1);
	fecha_inicio.setHours(0,0,0,0); 
	var fecha_fin=new Date();
	fecha_fin.setDate(1);
	fecha_fin.setFullYear(anyo_fin.selectedIndex+2010);
	fecha_fin.setMonth(mes_fin.selectedIndex);
	fecha_fin.setDate(dia_fin.selectedIndex+1);
	fecha_fin.setHours(0,0,0,0);
	hoy.setHours(0,0,0,0);
	
	if(fecha_inicio<hoy){
		alert(errores['hoy']);
		return false;		
	}
	if(fecha_inicio.getDate()!=dia_inicio.selectedIndex+1){
		alert(errores['fecha_inicio']);
		return false;
	}
	if(fecha_fin.getDate()!=dia_fin.selectedIndex+1){
		alert(errores['fecha_fin']);
		return false;
	}
	if(fecha_inicio>=fecha_fin){
		alert(errores['num_noches']);
		return false;
	}
	
	hist.value="1";
}

function addEvent(e,f){
	if(document.attachEvent){
		eval('document.attachEvent("on'+e+'",'+f+');');
	}else if(window.addEventListener){
		eval('window.addEventListener("'+e+'",'+f+',true);');
	}else{
		eval('window["on'+e+'"]='+f+';');
	}

}



