/* -------------------------------------------------------------------------------------------------------------------
		
	Por hacer:	Usar un bucle para buscar los selects
	(21/01)	  	Intentar unificar funciones
			  	Desactivar selects en el inicio o cargar un país, ciudad y hotel por defecto
				Precarga de banner e info
			  	Pasar variables por POST
				Rehacer función AJAX
				Seguridad PHP
				Diseño de cuadro de contacto
				Problema con emails y webs demasiado largos				
				Iconos en contacto
				Que aparezca seleccionada la opción que tiene más visitas
				Arreglar espacio en blanco que genera la capa de info del hotel en Hotels Search en IE6
				Formatear descripciones de los hotels: quitar caracteres raros, añadir \n, nl2br, etc.
				Caché consultas Ajax
				
	A mejorar:	- Desactivación de video, GMaps, satelite cuando están vacios
	(21/01)		- Info de contacto: ampliar y mostrar Nombre, dirección, país, etc.
				- Ocultar capa de info cuando no hay ningún hotel cargado, y expandirla y mostrarla
				  cuando se carga (poner un true/false para q no lo haga cada vez)
								
	Hecho: 		- Renombrar funciones (21/01)
				- Desactivar video, GMaps, satelite cuando están vacíos (21/01)
				- Info de contacto: de momento sólo email, teléfono y fax (21/01)
				- Ocultar capa de info cuando no hay ningún hotel cargado, y expandirla y mostrarla
				  cuando se carga (22/01)
				- Pasa contacto a <li> (10/03)
				- Problema con campos de contacto vacíos: añadido <li> y retocado rpc.php (10/03)
				- JS en archivos externo (10/03)
				- CSS en archivos externo (10/03)
				- Ordenados los combos por orden alfabético (17/03)
				- Cambiado el menú de hoteles cuando aún no se muestran hoteles --> "Start by selecting..." (18/03)
				- Quitar ActiveX (18/03)
			  
------------------------------------------------------------------------------------------------------------------- */

var ajax;
function newAjax()
{ 
 if (window.XMLHttpRequest) { // Intento de crear el objeto para Mozilla, Safari,...

ajax = new XMLHttpRequest();

}
	/* Crea el objeto AJAX. Esta funcion es generica para cualquier utilidad de este tipo, por
	lo que se puede copiar tal como esta aqui */
	try
	{
		// Creacion del objeto AJAX para navegadores no IE
		ajax=new ActiveXObject("Msxml2.XMLHTTP");
	}
	catch(e)
	{
		try
		{
			// Creacion del objet AJAX para IE
			ajax=new ActiveXObject("Microsoft.XMLHTTP");
		}
		catch(E)
		{
			if (!ajax && typeof XMLHttpRequest!='undefined') ajax=new XMLHttpRequest();
		}
	}
	return ajax; 
}

var menu = false;

window.onload = function () {
	countries = document.getElementById("country");
	cities = document.getElementById("city");
	hotels = document.getElementById("hotel");
}

function loading(selectId) {
	var option = document.createElement("option");
		option.appendChild(document.createTextNode("Loading..."));
		selectId.appendChild(option);
}

function ok(selectId) {
	var option = document.createElement("option");
		option.appendChild(document.createTextNode("OK"));
		option.value = "1";
		selectId.appendChild(option);	
}

function vaciar(selectId) {
	element = document.getElementById(selectId);
	while(element.firstChild) {
		element.removeChild(element.firstChild);
	}
}

function expand() {
	document.getElementById("info").style.height = "auto";
}

function show() {
	document.getElementById("info").style.visibility = "visible";
}

function showMenu() {
	document.getElementById("banner_menu").innerHTML = "<ul><li id=\"url_book\"><a href=\"#\">Book now into hotel's web page</a></li>	<li id=\"url_web\"><a href=\"#\">Visit hotel's web page</a></li><li id=\"url_map\"><a href=\"#\">Get directions</a></li><li id=\"satellite\"><a href=\"#\">View satellite image</a></li><li id=\"url_video\"><a href=\"#\">View video</a></li></ul>";
}

function changeCities() {
	if(menu == false) {
		showMenu();
		menu = true;		
	}	
	newAjax();
	ajax.open("GET","http://www.hotelsfirstclass.com/rpc.php?p=" + countries.value + "&t=" + countries.id);
	ajax.onreadystatechange = function() {
		if(ajax.readyState == 1) {
			vaciar(cities.id);
			loading(cities);
		} else if(ajax.readyState == 4) {
			vaciar(cities.id);
			datos = ajax.responseXML.documentElement;
			total = ajax.responseXML.getElementsByTagName("dato");
			option = null; // Hay q definirlas sin var delante (globales) pq si no las lee desde dentro del bucle
			for(i=0; i<total.length; i++){
				option = document.createElement("option");
					option.appendChild(document.createTextNode(datos.getElementsByTagName("dato")[i].getAttribute("name")));
					option.value = datos.getElementsByTagName("dato")[i].getAttribute("id");
				document.getElementById(cities.id).appendChild(option);
		}
		document.getElementById(cities.id).disabled = false;
		changeHotels();	
		}
	}
	ajax.send(null);
}

function changeHotels() {
	newAjax();
	// ajax.open("GET","rpc.php?v=" + id + "&t=" + cities.id);
	ajax.open("GET","http://www.hotelsfirstclass.com/rpc.php?c=" + document.getElementById("city").value + "&t=" + cities.id);
	ajax.onreadystatechange = function() {
		if(ajax.readyState == 1) {
			vaciar(hotels.id);
			loading(hotels);
		} else if(ajax.readyState == 4) {
			vaciar(hotels.id);
			datos = ajax.responseXML.documentElement;
			total = ajax.responseXML.getElementsByTagName("dato");
			option = null; // Hay q definirlas sin var delante (globales) pq si no las lee desde dentro del bucle
			for(i=0; i<total.length; i++){
				option = document.createElement("option");
					option.appendChild(document.createTextNode(datos.getElementsByTagName("dato")[i].getAttribute("name")));
					option.value = datos.getElementsByTagName("dato")[i].getAttribute("id");
				document.getElementById(hotels.id).appendChild(option);	
			}
		document.getElementById(hotels.id).disabled = false;
		loadHotel();		
		}
	}
	ajax.send(null);
}

function loadHotel() {
	newAjax();
	ajax.open("GET","http://www.hotelsfirstclass.com/rpc.php?h=" + document.getElementById("hotel").value + "&c=" + document.getElementById("city").value + "&p=" + document.getElementById("country").value + "&t=hotel");
	ajax.onreadystatechange = function () {
		if(ajax.readyState == 1) {
		} else if(ajax.readyState == 4) {
			datos = ajax.responseXML.documentElement;
			document.getElementById("banner").innerHTML = "<object classid=\"clsid:d27cdb6e-ae6d-11cf-96b8-444553540000\" codebase=\"http://fpdownload.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=8,0,0,0\" id=\"banner_hotel\" width=\"700\" height=\"160\" align=\"middle\"><param name=\"allowScriptAccess\" value=\"always\" /><param name=\"movie\" value=\"" + datos.getElementsByTagName("banner")[0].firstChild.data + "\" /><param name=\"quality\" value=\"best\" /><param name=\"wmode\" value=\"transparent\" /><param name=\"bgcolor\" value=\"#666666\" /><embed src=\"" + datos.getElementsByTagName("banner")[0].firstChild.data + "\" quality=\"best\" wmode=\"transparent\" bgcolor=\"#666666\" width=\"700\" height=\"160\" swLiveConnect=true id=\"banner_hotel\" name=\"banner_hotel\" align=\"middle\" allowScriptAccess=\"always\" type=\"application/x-shockwave-flash\" pluginspage=\"http://www.macromedia.com/go/getflashplayer\" /></object>";
			document.getElementById("name").innerHTML = "<h1>" + datos.getElementsByTagName("name")[0].firstChild.data + "</h1>";
			document.getElementById("description").innerHTML = datos.getElementsByTagName("description")[0].firstChild.data.replace("&lt;/g","<");				
			if(datos.getElementsByTagName("url_book")[0].firstChild.data == "Empty") {
				document.getElementById("url_book").innerHTML = "<span>Book now into hotel's web page</span>";			
			} else {
				document.getElementById("url_book").innerHTML = "<a href=\"" + datos.getElementsByTagName("url_book")[0].firstChild.data + "\" id=\"book\" target=\"_blank\" onclick=\"contar(this.id)\">Book now into hotel's web page</a>";
			}			
			document.getElementById("url_web").innerHTML = "<a href=\"" + datos.getElementsByTagName("url_web")[0].firstChild.data + "\" id=\"web\" target=\"_blank\" onclick=\"contar(this.id)\">Visit hotel's web page</a>";			
			// Get Directions			
			if(datos.getElementsByTagName("url_map")[0].firstChild.data == "Empty") {
				document.getElementById("url_map").innerHTML = "<span>Get directions</span>";			
			} else {
				document.getElementById("url_map").innerHTML = "<a href=\"javascript\:void(0)\" id=\"map\" onclick=\"window.open('http://www.ywol.es/ft/hotels/open_iframe.php?id=" + document.getElementById("hotel").value + "',  'windowname','width=440,height=380,toolbar=no,scrollbars=no'); contar(this.id); return false;\">Get directions</a>";
			}	
			// View Satellite Image					
			if(datos.getElementsByTagName("satellite")[0].firstChild.data == "Empty") {
				document.getElementById("satellite").innerHTML = "<span>View satellite image</span>";			
			} else {
				document.getElementById("satellite").innerHTML = "<a href=\"javascript\:void(0)\" id=\"sat\" onclick=\"window.open('http://www.ywol.es/ft/hotels/open_satellite.php?id=" + document.getElementById("hotel").value + "',  'windowname','width=350,height=350,toolbar=no,scrollbars=no'); contar(this.id); return false;\">View satellite image</a>";
			}
			// View Video		
			if(datos.getElementsByTagName("url_video")[0].firstChild.data == "Empty") {
				document.getElementById("url_video").innerHTML = "<span>View video</span>";			
			} else {
				document.getElementById("url_video").innerHTML = "<a href=\"" + datos.getElementsByTagName("url_video")[0].firstChild.data + "\"id=\"vid\" target=\"_blank\" onclick=\"contar(this.id)\">View video</a>";
			}
			
			// Info Contacto
			
			var lista = "<ul>" 			
			if(datos.getElementsByTagName("email")[0].firstChild.data == "Empty") {
				lista += "";
			} else {
				lista += "<li><b>Email: </b><a href=mailto:" + datos.getElementsByTagName("email")[0].firstChild.data + ">" + datos.getElementsByTagName("email")[0].firstChild.data + "</a></li>";
			}
			if(datos.getElementsByTagName("phone")[0].firstChild.data == "Empty") {
				lista += "";				
			} else {
				lista += "<li><b>Phone: </b>" + datos.getElementsByTagName("phone")[0].firstChild.data + "</li>";
			}
			if(datos.getElementsByTagName("fax")[0].firstChild.data == "Empty") {
				lista += "";
			} else {
				lista += "<li><b>Fax: </b>" + datos.getElementsByTagName("fax")[0].firstChild.data + "</li>";
			}		
			lista += "</ul>";			
			document.getElementById("contact").innerHTML = lista;						
			expand();
			show();
		}
	}
	ajax.send(null);
}


function contar(secc) {
	newAjax();
	ajax.open("GET","http://www.hotelsfirstclass.com/rpc.php?t=c&s=" + secc + "&h=" + document.getElementById("hotel").value);
	ajax.onreadystatechange = function () {
		if(ajax.readyState == 1) {
			var state = "Loanding";
		} else if(ajax.readyState == 4) {
			var state = "ok";
		}
	}
	ajax.send(null);
}
