// JavaScript home.asp

function agafa_data_anterior()
{
	var fecha_inicial=document.cerca_hotel.DataIni.value;
	if (fecha_inicial==""){
		alert('<%=traduccio("reservas01","error08")%>');
		return false;
	} else {
		if ( (isNaN(parseInt (fecha_inicial.substr(0,2),10) )) || (fecha_inicial.substr(2,1)!="/") || (isNaN(parseInt(fecha_inicial.substr(3,2),10))) || (fecha_inicial.substr(5,1)!="/") || (isNaN(parseInt(fecha_inicial.substr(6,4),10))) ){
			alert('<%=traduccio("reservas01","error08")%>');
			return false;
		} else {
			if ( (parseInt(fecha_inicial.substr(0,2),10)>31) || (parseInt(fecha_inicial.substr(0,2),10)<1) || (parseInt(fecha_inicial.substr(3,2),10)>12) || (parseInt(fecha_inicial.substr(3,2),10)<1) || (parseInt(fecha_inicial.substr(6,4),10)<2002) ){
				alert('<%=traduccio("reservas01","error08")%>');
				return false;
			} else {
			array=fecha_inicial.split("/");

			var dia=array[0];
			var mes=array[1];
			var any=array[2];

			var leftPos=0,topPos=0, w = 480, h = 340;
			if (document.all || document.layers || document.getElementById) {w = screen.availWidth; h = screen.availHeight;}
			topPos = (h-213)/2;
			leftPos = (w-252)/2;

			calendario2=window.open ("calendari.asp?c=2&fecha="+dia+"/"+mes+"/"+any+"&mes="+ (mes-1) +"&any="+ any,"Calendario","fullscreen=no,toolbar=no,location=no,status=no,menubar=no,scrollbars=no,resizable=no,width=252,height=213");
			calendario2.moveTo(leftPos, topPos);
			return false;
			}
		}
	}
	return false;
}

function CapacitatNens(){
	var selObj1 = document.cerca_hotel.hab1;
	var selObj2 = document.cerca_hotel.hab2;
	var selObj3 = document.cerca_hotel.hab3;
	var selObj4 = document.cerca_hotel.hab4;

	var valor1 = parseInt(selObj1.options[selObj1.selectedIndex].value)*2;
	var valor2 = parseInt(selObj2.options[selObj2.selectedIndex].value)*2;
	var valor3 = parseInt(selObj3.options[selObj3.selectedIndex].value)*2;
	var valor4 = parseInt(selObj4.options[selObj4.selectedIndex].value);

	var numNens = parseInt(document.cerca_hotel.nens.options[document.cerca_hotel.nens.selectedIndex].value)

	if ( (valor1+valor2+valor3+valor4) < numNens ){
		MM_showHideLayers('cercant','','hide');
		alert('<%=traduccio("home","alertaNens")%>')
		document.cerca_hotel.nens.focus();
		return false;
	}else{
		return true;
	}
}

function SumarDies_Home(fechaIni,num){
	var msPorDia = 24 * 60 * 60 * 1000
	var fechaFin = Date.parse(fechaIni)+(msPorDia*num);
	var fechaFinNew = new Date(fechaFin);
	var Fdia=fechaFinNew.getDate();
	if (Fdia < 10 ) {
		Fdia = '0' + Fdia;
	}
	var Fmes=fechaFinNew.getMonth()+1;
	if (Fmes < 10 ) {
		Fmes = '0' + Fmes;
	}
	var Fany=fechaFinNew.getYear();
	if (Fany < 1000) { //Modificaci� necess�ria per Firefox
		Fany+=1900
	}	
	document.cerca_hotel.DataFi.value = Fdia +'/'+ Fmes +'/'+ Fany;
	var fechaFinDia = new Date( Fmes +'/'+ Fdia +'/'+ Fany );
	//document.getElementById('divDiaSortida').innerHTML = weekDay[fechaFinDia.getDay()];
	document.cerca_hotel.numNits.value = num;
}

function posarNits(){
	fecha_inicial = document.cerca_hotel.DataIni.value;
	fecha_final = document.cerca_hotel.DataFi.value;
	
	var fechaIni = new Date( fecha_inicial.substr(3,2) +'/'+ fecha_inicial.substr(0,2) +'/'+ fecha_inicial.substr(6,4) );
	var fechaFin = new Date( fecha_final.substr(3,2) +'/'+ fecha_final.substr(0,2) +'/'+ fecha_final.substr(6,4) );
	msPorDia = 24 * 60 * 60 * 1000
	
	NumNits = Math.round((fechaFin - fechaIni) / msPorDia)
	if (isNaN(NumNits) || NumNits<1){
		SumarDies_Home(fechaIni,1);
	}else if (NumNits>15){
		alert('<%=traduccio("reservas01","error06")%>');
		SumarDies_Home(fechaIni,15);
	} else {
		//document.cerca_hotel.numNits.value = NumNits;
	}
}

function posarNits_LastMinute(){
	fecha_inicial = document.cerca_hotel.DataIni.value;
	fecha_final = document.cerca_hotel.DataFi.value;
	
	var fechaIni = new Date( fecha_inicial.substr(3,2) +'/'+ fecha_inicial.substr(0,2) +'/'+ fecha_inicial.substr(6,4) );
	var fechaFin = new Date( fecha_final.substr(3,2) +'/'+ fecha_final.substr(0,2) +'/'+ fecha_final.substr(6,4) );
	msPorDia = 24 * 60 * 60 * 1000
	
	NumNits = Math.round((fechaFin - fechaIni) / msPorDia)
	if (isNaN(NumNits) || NumNits<1){
		SumarDies_LastMinute(fechaIni,1);
	}else if (NumNits>15){
		alert('<%=traduccio("reservas01","error06")%>');
		SumarDies_LastMinute(fechaIni,15);
	} else {
		//document.cerca_hotel.numNits.value = NumNits;
	}
}

function tipusSubmit(id)
{  
  window.location = "/_prgr01/validar_dadesMenu.asp?tipusSubmit=" + id;
}

/*********************************************************************************************************************************/


function AbrirHotel(x){
    //alert(document.cerca_hotel.DataFi.value);
	//document.location.href='hotels_infohotel.asp?id=' + x
	//document.location.href='hotels_infohotel2.asp?id=' + x + "&Ini=" + document.cerca_hotel.DataIni.value + "&Fin=" + document.cerca_hotel.DataFi.value
	document.cerca_hotel.miHotel.value=x
	
    document.cerca_hotel.detall.value=-1;
	document.cerca_hotel.submit();
}


function envia(){
	if (CapacitatNens()){
		registrarAceptacion("/_prgr01/clickBuscarHome.asp");
		document.cerca_hotel.detall.value=0;
		document.cerca_hotel.submit();
	}
}

function detallada(){
	if (CapacitatNens()){
		document.cerca_hotel.detall.value=1;
		document.cerca_hotel.submit();
	}
}

function GP_AdvOpenWindow(theURL,winName,features,popWidth,popHeight,winAlign,ignorelink,alwaysOnTop,autoCloseTime,borderless) { //v2.0
  var leftPos=0,topPos=0,autoCloseTimeoutHandle, ontopIntervalHandle, w = 480, h = 340;
  if (popWidth > 0) features += (features.length > 0 ? ',' : '') + 'width=' + popWidth;
  if (popHeight > 0) features += (features.length > 0 ? ',' : '') + 'height=' + popHeight;
  if (winAlign && winAlign != "" && popWidth > 0 && popHeight > 0) {
    if (document.all || document.layers || document.getElementById) {w = screen.availWidth; h = screen.availHeight;}
		if (winAlign.indexOf("center") != -1) {topPos = (h-popHeight)/2;leftPos = (w-popWidth)/2;}
		if (winAlign.indexOf("bottom") != -1) topPos = h-popHeight; if (winAlign.indexOf("right") != -1) leftPos = w-popWidth;
		if (winAlign.indexOf("left") != -1) leftPos = 0; if (winAlign.indexOf("top") != -1) topPos = 0;
    features += (features.length > 0 ? ',' : '') + 'top=' + topPos+',left='+leftPos;}
  if (document.all && borderless && borderless != "" && features.indexOf("fullscreen") != -1) features+=",fullscreen=1";
  if (window["popupWindow"] == null) window["popupWindow"] = new Array();
  var wp = popupWindow.length;
  popupWindow[wp] = window.open(theURL,winName,features);
  if (popupWindow[wp].opener == null) popupWindow[wp].opener = self;
  if (document.all || document.layers || document.getElementById) {
    if (borderless && borderless != "") {popupWindow[wp].resizeTo(popWidth,popHeight); popupWindow[wp].moveTo(leftPos, topPos);}
    if (alwaysOnTop && alwaysOnTop != "") {
    	ontopIntervalHandle = popupWindow[wp].setInterval("window.focus();", 50);
    	popupWindow[wp].document.body.onload = function() {window.setInterval("window.focus();", 50);}; }
    if (autoCloseTime && autoCloseTime > 0) {
    	popupWindow[wp].document.body.onbeforeunload = function() {
  			if (autoCloseTimeoutHandle) window.clearInterval(autoCloseTimeoutHandle);
    		window.onbeforeunload = null;	}
   		autoCloseTimeoutHandle = window.setTimeout("popupWindow["+wp+"].close()", autoCloseTime * 1000); }
  	window.onbeforeunload = function() {for (var i=0;i<popupWindow.length;i++) popupWindow[i].close();}; }
  document.MM_returnValue = (ignorelink && ignorelink != "") ? false : true;
}

function SumarDies(fechaIni,num){
	var msPorDia = 24 * 60 * 60 * 1000
	var fechaFin = Date.parse(fechaIni)+(msPorDia*num);
	var fechaFinNew = new Date(fechaFin);
	var Fdia=fechaFinNew.getDate();
	if (Fdia < 10 ) {
		Fdia = '0' + Fdia;
	}
	var Fmes=fechaFinNew.getMonth()+1;
	if (Fmes < 10 ) {
		Fmes = '0' + Fmes;
	}
	var Fany=fechaFinNew.getYear();
	if (Fany < 1000) { //Modificaci� necess�ria per Firefox
		Fany+=1900
	}	
	document.cerca_hotel3.DataFi.value = Fdia +'/'+ Fmes +'/'+ Fany;
	var fechaFinDia = new Date( Fmes +'/'+ Fdia +'/'+ Fany );
	//document.getElementById('divDiaSortida').innerHTML = weekDay[fechaFinDia.getDay()];
	document.cerca_hotel3.numNits.value = num;
}

function SumarDies_LastMinute(fechaIni,num){
	var msPorDia = 24 * 60 * 60 * 1000
	var fechaFin = Date.parse(fechaIni)+(msPorDia*num);
	var fechaFinNew = new Date(fechaFin);
	var Fdia=fechaFinNew.getDate();
	if (Fdia < 10 ) {
		Fdia = '0' + Fdia;
	}
	var Fmes=fechaFinNew.getMonth()+1;
	if (Fmes < 10 ) {
		Fmes = '0' + Fmes;
	}
	var Fany=fechaFinNew.getYear();
	if (Fany < 1000) { //Modificaci� necess�ria per Firefox
		Fany+=1900
	}	
	document.cerca_hotel.DataFi.value = Fdia +'/'+ Fmes +'/'+ Fany;
	var fechaFinDia = new Date( Fmes +'/'+ Fdia +'/'+ Fany );
	//document.getElementById('divDiaSortida').innerHTML = weekDay[fechaFinDia.getDay()];
	document.cerca_hotel.numNits.value = num;
}


function setFecha(c,fecha)
{
	switch(parseInt(c))
	{
	case 1: document.cerca_hotel.DataIni.value=fecha;
			var fechaIni = new Date( fecha.substr(3,2) +'/'+ fecha.substr(0,2) +'/'+ fecha.substr(6,4) );
			//document.getElementById('divDiaEntrada').innerHTML = weekDay[fechaIni.getDay()];
			fecha_final = document.cerca_hotel.DataFi.value;
			var fechaFin = new Date( fecha_final.substr(3,2) +'/'+ fecha_final.substr(0,2) +'/'+ fecha_final.substr(6,4) );
			if (isNaN(document.cerca_hotel.numNits.value) || isNaN(fechaFin) || fechaIni>=fechaFin ){
				SumarDies(fechaIni,1);
			}else{
				posarNits();
			}
			break;
	case 2: document.cerca_hotel.DataFi.value=fecha;
			posarNits();
			break;
	}
}

function getFecha(c){
	switch(parseInt(c)){
	case 1: return document.cerca_hotel.DataIni.value;
			//document.cerca_hotel.DataFi.value=fecha;
			break;
	case 2: return document.cerca_hotel.DataFi.value;
			break;
	}
}

function MM_reloadPage(init) {  //reloads the window if Nav4 resized
  if (init==true) with (navigator) {if ((appName=="Netscape")&&(parseInt(appVersion)==4)) {
    document.MM_pgW=innerWidth; document.MM_pgH=innerHeight; onresize=MM_reloadPage; }}
  else if (innerWidth!=document.MM_pgW || innerHeight!=document.MM_pgH) location.reload();
}
MM_reloadPage(true);

function MM_findObj(n, d) { //v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}

function MM_showHideLayers() { //v6.0
  var i,p,v,obj,args=MM_showHideLayers.arguments;
  for (i=0; i<(args.length-2); i+=3) if ((obj=MM_findObj(args[i]))!=null) { v=args[i+2];
    if (obj.style) { obj=obj.style; v=(v=='show')?'visible':(v=='hide')?'hidden':v; }
    obj.visibility=v; }
}

var MINUTE = 60 * 1000;
var HOUR = 60 * MINUTE;
var DAY = 24 * HOUR;

function isDisabledIni(date) 
{
	var today = new Date();

	// Es menor a hoy
	if (date.getTime() < (today.getTime()-DAY))
	{	
		return true;
	}

	// Es mayor a un a�o
	if (((date.getTime() - today.getTime()) / DAY) > 366)
	{
		return true;
	}

	return false;
}

function isDisabledFi(date) 
{
//    var today = new Date();
	
	fecha_inicial = document.cerca_hotel.DataIni.value;
	var today = new Date( fecha_inicial.substr(3,2) +'/'+ fecha_inicial.substr(0,2) +'/'+ fecha_inicial.substr(6,4) );

	// Es menor a hoy
	if (date.getTime() < (today.getTime()+1))
	{	
		return true;
	}

	// Es mayo a un a�o
	if (((date.getTime() - today.getTime()) / DAY) > 15)
	{
		return true;
	}

	return false;
}

  function closeCalendar(cal) 
  {
    cal.hide();
	  posarNits();
    return true;
  };
	
	
function isDisabledIni2(date) 
{
	var today = new Date();

	// Es menor a hoy
	if (date.getTime() < (today.getTime()-DAY))
	{	
		return true;
	}

	// Es mayor a un a�o
	if (((date.getTime() - today.getTime()) / DAY) > 366)
	{
		return true;
	}

	return false;
}

function isDisabledFi2(date) 
{
//    var today = new Date();
	
	fecha_inicial = document.cerca_hotel3.DataIni.value;
	var today = new Date( fecha_inicial.substr(3,2) +'/'+ fecha_inicial.substr(0,2) +'/'+ fecha_inicial.substr(6,4) );

	// Es menor a hoy
	if (date.getTime() < (today.getTime()+1))
	{	
		return true;
	}

	// Es mayo a un a�o
	if (((date.getTime() - today.getTime()) / DAY) > 15)
	{
		return true;
	}

	return false;
}

  function closeCalendar2(cal) 
  {
    cal.hide();
	  posarNits2();
    return true;
  };
	
function AbrirListadoHome(orden,categoria,FechaIni, FechaFin, paginaRegla)
{
		
	document.cerca_hotel.DataIni.value = FechaIni;
	document.cerca_hotel.DataFi.value = FechaFin;

	posarNits_LastMinute();

  document.cerca_hotel.hab1.value=0;
	document.cerca_hotel.hab2.value=0;
	document.cerca_hotel.hab3.value=1;
	document.cerca_hotel.hab4.value=0;
	
	document.cerca_hotel.nens.value=0;
  document.cerca_hotel.miOrden.value = orden
	document.cerca_hotel.miCategoria.value = categoria
  document.cerca_hotel.detall.value=-4;
	document.getElementById("paginaRegla").value = paginaRegla;

	document.cerca_hotel.submit();
}

