// JavaScript Document

//Meses en espaņol
elmes=new Array(12),
elmes[1]='ene';
elmes[2]='feb';
elmes[3]='mar';
elmes[4]='abr';
elmes[5]='may';
elmes[6]='jun';
elmes[7]='jul';
elmes[8]='ago';
elmes[9]='sep';
elmes[10]='oct';
elmes[11]='nov';
elmes[12]='dic';

hoy=new Date();
//poner formulario de fechas en Espaņol
document.write("<form name='f2' method='post' action=''>\n");
document.write("<div class='reserva'>Su reserva</div>\n");
document.write("<p class='texto'>Desde:</p>\n");
document.write("<select name='diaI' class='select' style='width:40px' onchange='javascript:ponNoches();'>\n");
//Los dias
for (d=1;d<=31;d++){
	dia="0"+d.toString();
	ancho=dia.length;
	dia=dia.substring(ancho-2,(ancho-2)+2);
	document.write ("<option value='"+dia+"'>"+d+"</option>\n");
}
document.write("</select>\n");
//Los meses
document.write("<select name='mesI' class='select' style='width:45px' onchange='javascript:ponNoches();'>\n");

for (d=1;d<=12;d++){
	mes="0"+d.toString();
	ancho=mes.length;
	mes=mes.substring(ancho-2,(ancho-2)+2);
	document.write ("<option value='"+mes+"'>"+elmes[d]+"</option>\n");
}
document.write("</select>\n");
//Aņos
document.write("<select name='anyI' class='select' style='width:50px' onchange='javascript:ponNoches();'>\n");
document.write ("<option value='"+hoy.getFullYear()+"'>"+hoy.getFullYear()+"</option>\n");
document.write ("<option value='"+(hoy.getFullYear()+1)+"'>"+(hoy.getFullYear()+1)+"</option>\n");																		
document.write("</select>\n");
//document.write("<img src='img/calendar.gif' border='0' align='top' width='16' height='15'/>");
document.write("<p class='texto'>Hasta:</p>\n");
document.write("<select name='diaF' class='select' style='width:40px' onchange='javascript:ponNoches();'>\n");
//Los dias
for (d=1;d<=31;d++){
	dia="0"+d.toString();
	ancho=dia.length;
	dia=dia.substring(ancho-2,(ancho-2)+2);
	document.write ("<option value='"+dia+"'>"+d+"</option>\n");
}
document.write("</select>\n");
//Los meses
document.write("<select name='mesF' class='select' style='width:45px' onchange='javascript:ponNoches();'>\n");
for (d=1;d<=12;d++){
	
	mes="0"+d.toString();
	ancho=mes.length;
	mes=mes.substring(ancho-2,(ancho-2)+2);
	document.write ("<option value='"+mes+"'>"+elmes[d]+"</option>\n");

}

document.write("</select>\n");
//Aņos
document.write("<select name='anyF' class='select' style='width:50px' onchange='javascript:ponNoches();'>\n");
document.write ("<option value='"+hoy.getFullYear()+"'>"+hoy.getFullYear()+"</option>\n");
document.write ("<option value='"+(hoy.getFullYear()+1)+"'>"+(hoy.getFullYear()+1)+"</option>\n");																		
document.write("</select>\n");
//document.write("<img src='img/calendar.gif' border='0' align='top' width='16' height='15'/>");
document.write("<p class='texto'>Noches:</p>");
document.write("<input name='noches' style='float:left; width:25px' class='inputtext' readonly>");
document.write("<a href='#' onclick='javascript:enviaBusca();' class='buscar'>buscar</a>\n");
document.write("<p class='texto'>&nbsp; </p>\n");
document.write("<p class='texto'>&nbsp; </p>\n");
document.write("</form>\n");

//funciones pa las cookies
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" : "");
}

/*
GetCookie("visit");

expdate.setTime(expdate.getTime() + (24 * 60 * 60 * 1000 * 365)); 
SetCookie("visit", visit, expdate, "/", null, false);
*/


//Poner fechas por defecto o de la cookie
function enviaBusca(){
	//Guarda las cookies
	fini=document.f2.diaI.value+"/"+document.f2.mesI.value+"/"+document.f2.anyI.value;
	ffin=document.f2.diaF.value+"/"+document.f2.mesF.value+"/"+document.f2.anyF.value;
	SetCookie("bfini",fini,null,"/");
	SetCookie("bffin",ffin,null,"/");

	//enviar al buscador
	document.f2.action="/reservas/reservas2.asp?lang=es&fini="+fini+"&ffin="+ffin;
	document.f2.submit();
}

//cargar por defecto
if (GetCookie("bfini")==null){ //poner fecha actual
	hoy=new Date();
	dia="0"+hoy.getDate().toString();
	ancho=dia.length;
	dia=dia.substring(ancho-2,(ancho-2)+2);
	mes="0"+(hoy.getMonth()+1).toString();
	ancho=mes.length;
	mes=mes.substring(ancho-2,(ancho-2)+2);
	document.f2.diaI.value=dia;
	document.f2.mesI.value=mes;
	document.f2.anyI.value=hoy.getFullYear();
	document.f2.diaF.value=dia;
	document.f2.mesF.value=mes;
	document.f2.anyF.value=hoy.getFullYear();
}else{
	fini=GetCookie("bfini");
	diaI=fini.substring(0,2);
	mesI=fini.substring(3,5);
	anyI=fini.substring(6,10);
	document.f2.diaI.value=diaI;
	document.f2.mesI.value=mesI;
	document.f2.anyI.value=anyI;

	ffin=GetCookie("bffin");
	diaF=ffin.substring(0,2);
	mesF=ffin.substring(3,5);
	anyF=ffin.substring(6,10);
	document.f2.diaF.value=diaF;
	document.f2.mesF.value=mesF;
	document.f2.anyF.value=anyF;
}

function ponNoches() {
	//Restar las fechas para saber dias introducidos
	fini=document.f2.diaI.value+"/"+document.f2.mesI.value+"/"+document.f2.anyI.value;
	ffin=document.f2.diaF.value+"/"+document.f2.mesF.value+"/"+document.f2.anyF.value;
	da1=new Date(fini.substring(6,10),fini.substring(3,5)-1,fini.substring(0,2));
    da2=new Date(ffin.substring(6,10),ffin.substring(3,5)-1,ffin.substring(0,2));
  if (da1>=da2 )
    document.f2.noches.value="***";
  else
  	{
	dife=da2.getTime() - da1.getTime()+10800000; //se aņade  10800000 pa evitar el error de marzo
	bnoches=parseInt(dife/ (1000 * 60 * 60 * 24));
    document.f2.noches.value=bnoches;
	}
}

//Poner noches a la primera carga
ponNoches();


