/* ********************************
Script que inicialitza el cercador.
Requereix: accUtils.js 
*********************************** */

var desti;
var poblacio;
var diaini;
var mesini;
var anyini;
var nits;
var adults;
var nens;
var botoCercaHotel;
var idioma;
var tipusHotel;
var cadenaHotel;
var uaavv;
var referer;

function initCercador() {

	uaavv=document.getElementById("uaavv");
	idioma=document.getElementById("lang");

	if (document.getElementById("rdini")){
		//Es un cercador de reservas.
		initCercadorReservas();
		return;
	}

	desti=document.getElementById("desti");
	poblacio=document.getElementById("poblacio");
	diaini=document.getElementById("diaini");
	mesini=document.getElementById("mesini");
	anyini=document.getElementById("anyini");
	nits=document.getElementById("nits");
	adults=document.getElementById("adults");
	nens=document.getElementById("nens");
	botoCercaHotel=document.getElementById("botoCercaHotel");
	referer=document.getElementById("referer");
	tipusHotel=document.getElementById("tipusHotel");
	cadenaHotel=document.getElementById("cadenaHotel");

	if (!diaini) return;

	//Parametres seleccionats
	var di=false;   //diaini
	var mi=false;   //mesini
	var ai=false;   //anyini
	var nt=false;   //nits
	var ad=false;   //Adults
	var ch=false;   //Adults

	if ( document.getElementById("di") ) di=document.getElementById("di").value
	if ( document.getElementById("mi") ) mi=document.getElementById("mi").value
	if ( document.getElementById("ai") ) ai=document.getElementById("ai").value
	if ( document.getElementById("nt") ) nt=document.getElementById("nt").value
	if ( document.getElementById("ad") ) ad=document.getElementById("ad").value
	if ( document.getElementById("ch") ) ch=document.getElementById("ch").value

	addAnEvent( desti, "change", readPoblacions );
	var d=new Date();

	nits.options.length=0;
	for (var i=1;i<31;i++) {
		nits.options[ i-1 ]=new Option(""+i, ""+i);
		if (nt) { 
			if ( parseInt( nt )==i ) nits.options[ i-1 ].selected=true; 
		}else{ 
			if ( 7==i ) nits.options[ i-1 ].selected=true; 
		}
	}

	adults.options.length=0;
	for (var i=1;i<10;i++) {
		adults.options[ i-1 ]=new Option(""+i, ""+i);
		if (ad) { 
			if (parseInt( ad )==i) adults.options[ i-1 ].selected=true; 
		}else{ 
			if (2==i) adults.options[ i-1 ].selected=true; 
		}
	}

	nens.options.length=0;
	for (var i=0;i<9;i++) {
		nens.options[i]=new Option(""+i, ""+i);
		if (ch){
			if (parseInt(ch)==i) nens.options[i].selected=true; 
		}else{
			if (0==i) nens.options[i].selected=true; 
		}
	}

	diaini.options.length=0;
	for (var i=1;i<=31;i++) {
		diaini.options[ i-1 ]=new Option(""+i, ""+i);
		if (di) { 
			if ( parseInt( di )==i ) diaini.options[ i-1 ].selected=true; 
		}else{ 
			if ( d.getDate()==i )  diaini.options[ i-1 ].selected=true; 
		}
	}

	mesini.options.length=0;
	for (var i=1;i<=12;i++) {
		mesini.options[ i-1 ]=new Option(""+i, ""+i);
		if (mi) { 
			if ( parseInt( mi )==i ) mesini.options[ i-1 ].selected=true; 
		}else { 
			if ( d.getMonth()+1==i )  mesini.options[ i-1 ].selected=true;
		}
	}

	anyini.options.length=0;
	for (var i=0;i<=2;i++) {
		anyini.options[ i ]=new Option(""+(d.getFullYear()+i), ""+(d.getFullYear()+i));
		if (ai) { 
			if ( parseInt( ai ) == d.getFullYear()+i ) anyini.options[ i ].selected=true; 
		}
	}

	if (document.getElementById("resultat")){
		botoCercaHotel.href="javascript: cercaHotels();";
	}else{
		document.getElementById("botoCercaHotel").href="javascript: document.form_buscador.submit();";
	}

	readDestins();
	if ( tipusHotel ) readTipusHotel();
	if ( cadenaHotel ) {
		readCadenaHotel();
		setVisibleTipusHotel(true);
	}
}

function setTipusHotel( i ) {
	tipusHotel.value=i;
}

function setVisibleTipusHotel( b ) {
	if ( document.getElementById("tipusHotelContainer") ) {
		if ( b )  {
			show( document.getElementById("tipusHotelContainer") );
			hide( document.getElementById("cadenaHotelContainer") );
		} else {
			show( document.getElementById("cadenaHotelContainer") );
			hide( document.getElementById("tipusHotelContainer") );
		}
	}
}

function readCadenaHotel() {
	var s="files/cercador/readCadenaHotels.php";
	if (cadenaHotel.nodeName=="SELECT") postAjax(s,"");
}

function readTipusHotel() {
	var s="files/cercador/readTipusHotels.php?idioma="+idioma.value;
	if (tipusHotel.nodeName=="SELECT") postAjax(s,"");
}

function readDestins() {
	desti.options.length=0;
	var s="files/cercador/readDestins.php?hostbuscador=www.webhotels.org";
	postAjax(s,"");
}

function readPoblacions( e,id ) {
	poblacio.options.length=0;
	var s="files/cercador/readPoblacions.php?hostbuscador=www.webhotels.org";
	var a=desti.options[ desti.selectedIndex ].value;

	var ds=false;   //desti
	var pb=false;   //poblacio
	if ( document.getElementById("ds") ) ds=document.getElementById("ds").value
	if ( document.getElementById("pb") ) pb=document.getElementById("pb").value
	if ( ds ) if (ds!="false") a=ds;
	if ( pb ){
		if (pb!="false") {
			id=pb;
			s+="&cercar=true;";
		}
	}
	if (id) s+="&poblacio="+id;
	else s+="&zona="+a;

	if ( document.getElementById("ds") ) document.getElementById("ds").value=false;
	if ( document.getElementById("pb") ) document.getElementById("pb").value=false;

	postAjax(s,"");
}

function selectPoblacio( id ) {
	readPoblacions( null, id );
}

function cercaHotels() {
	cercarHolidays();
}

addAnEvent( window, "load", initCercador );

