// JavaScript Document
function getCookieVal (offset){
	var endstr = document.cookie.indexOf (";", offset);
	if (endstr == -1)
		endstr = document.cookie.length;

	return unescape(document.cookie.substring(offset, endstr));
}

function GetCookie (name){
	var arg = name + "=";
	var alen = arg.length;
	var clen = document.cookie.length;
	var i = 0;
	while (i < clen){
		var j = i + alen;
		if (document.cookie.substring(i, j) == arg)
			return getCookieVal (j);
		i = document.cookie.indexOf(" ", i) + 1;
		if (i == 0) 
			break; 
	}
	return null;
}

function SetCookie (name, value){
	var argv = SetCookie.arguments;
	var argc = SetCookie.arguments.length;
	var expires = (2 < argc) ? argv[2] : null;
	var path = (3 < argc) ? argv[3] : null;
	var domain = (4 < argc) ? argv[4] : null;
	var secure = (5 < argc) ? argv[5] : false;
	document.cookie = name + "=" + escape (value) +
	((expires == null) ? "" : ("; expires=" + expires.toGMTString())) +
	((path == null) ? "" : ("; path=" + path)) +
	((domain == null) ? "" : ("; domain=" + domain)) +
	((secure == true) ? "; secure" : "");
}

function fechaWeb(lafecha){
	Fdia=lafecha.getDate();
	if (Fdia<10)
		Fdia="0"+Fdia;
	Fmes=lafecha.getMonth()+1;
	if (Fmes<10)
		Fmes="0"+Fmes;
	Fany=lafecha.getFullYear();
	return Fdia+"/"+Fmes+"/"+Fany;
}

//Cargar desde las cookies
if (GetCookie("badultos")!=null)
	document.buscadorCR.ad.value=GetCookie("badultos");

if (GetCookie("bninos")!=null)		
	document.buscadorCR.ni.value=GetCookie("bninos");

if (GetCookie("bregimen")!=null)		
	document.buscadorCR.tr.value=GetCookie("bregimen");

if (GetCookie("bfini")==null){ //poner fecha actual

	hoy=new Date();
	ndias=1;
	inicial= new Date(Date.UTC(hoy.getFullYear(),hoy.getMonth(),hoy.getDate(),0,0,0) + ndias *86400000);
	ndias=4;
	final= new Date(Date.UTC(hoy.getFullYear(),hoy.getMonth(),hoy.getDate(),0,0,0) + ndias *86400000);

	document.buscadorCR.fini.value=fechaWeb(inicial);
	document.buscadorCR.ffin.value=fechaWeb(final);

}else{
	document.buscadorCR.fini.value=GetCookie("bfini");
	document.buscadorCR.ffin.value=GetCookie("bffin");
}

function cargaRegimen(){
	elhotel=document.buscadorCR.est.value;
	document.buscadorCR.tr.length=1; //deja cualquiera
	linea=0;
	for (s=0;s<codregi.length;s++){
		if (hotel[s]==elhotel){ //esta isla
			linea++;
			var selOpcion=new Option(nomregi[s],codregi[s]); 
			document.buscadorCR.tr.options[linea]=selOpcion;
		}
	}
}

function cambioHotel(ese,idioma){
  hotel=ese.value;
  document.getElementById('paProcesos').src="eHotecRegimen_saintMichel.asp?est="+hotel+"&lang="+idioma;
  //document.getElementById('paProcesos').src="eHotecCapacidad_saintMichel.asp?idhotel="+hotel+"&lang="+idioma;
  //alert(document.getElementById('paProcesos').src);
}

function buscadorhoteles()
{
	//Guarda las cookies
	SetCookie("bfini",document.buscadorCR.fini.value,null,"/");
	SetCookie("bffin",document.buscadorCR.ffin.value,null,"/");
	SetCookie("bninos",document.buscadorCR.ni.value,null,"/");
	SetCookie("badultos",document.buscadorCR.ad.value,null,"/");
	SetCookie("bregimen",document.buscadorCR.tr.value,null,"/");
	
	laEspera(); //imagen de espera
	
	//aquí imagino que habrá que validar algún campo
	document.buscadorCR.submit();
}

function abreCalendar(esaFecha,elObj,idioma){
	lafecha=eval("document.buscadorCR."+esaFecha+".value");
	laurl="calendario_SMichel.asp?lang="+idioma+"&elForm=buscadorCR&valor="+esaFecha+"&"+esaFecha+"="+lafecha;
	ptop=300;
	pleft=12;
	palIframe(document.getElementById('verCalendario'),220,120,ptop,pleft,laurl);
}

function ponNoches() {
	//Restar las fechas para saber dias introducidos
	fi=document.f1.fini.value;
	ff=document.f1.ffin.value;
	da1=new Date(fi.substring(6,10),fi.substring(3,5)-1,fi.substring(0,2));
    da2=new Date(ff.substring(6,10),ff.substring(3,5)-1,ff.substring(0,2));
  if (da1>=da2 )
    document.f1.txtdias.value="***";
  else
  	{
	dife=da2.getTime() - da1.getTime()+10800000; //se añade 10800000 pa evitar el error de marzo, años bisiestos
	bnoches=parseInt(dife/ (1000 * 60 * 60 * 24));
    document.f1.txtdias.value=bnoches;
	}
}

function CompruebaCap(mensaje){
	limite=4;
	ad=parseInt(document.buscadorCR.ad.value,10);
	ni=parseInt(document.buscadorCR.ni.value,10);
	if (limite<(ad+ni)) {
		alert(mensaje+": "+limite);
		document.buscadorCR.ni.value='0'; //dejo cantidad a cero
		return;
	}
}