/*Javascript para colocar los calendarios en los formularios de reserva*/
var url_buscador_rapido = "ControlCasasRuralesXML.php"; //URL donde está el servidor de XMLs 
var isWorking = false;//Bandera para saber si el proceso ajax está trabajando (true) o ya ha parado (false).

/**
* Función que recoge los datos que se pasa por XML
*/
function handleHttpResponseCCAA()
{
	if (http.readyState == 4)
	{
		if (http.responseText.indexOf("invalid") == -1)
		{
			xmlDoc=http.responseXML;
			
			var x=xmlDoc.getElementsByTagName('comunidad');
			var NumeroComunidades = x.length;

			NuevoElementoOption=document.createElement('option');
			NuevoElementoOption.value=0;
			NuevoElementoOption.text='------';
			try
			{
				//Version mozilla
				document.getElementById('Comunidad').add(NuevoElementoOption,null);
			}
			catch(e)
			{
				//Version IE
				document.getElementById('Comunidad').add(NuevoElementoOption);
			}
		
			for(i=0;i<NumeroComunidades;i++)
			{
				var id = xmlDoc.getElementsByTagName('comunidad').item(i).getElementsByTagName('identificador').item(0).firstChild.data;
				var nombre = xmlDoc.getElementsByTagName('comunidad').item(i).getElementsByTagName('nombre').item(0).firstChild.data;
			
				//Contruimos el select (persiana)
				NuevoElementoOption=document.createElement('option');
				NuevoElementoOption.value=id;
				NuevoElementoOption.text=nombre;
				try
				{
					document.getElementById('Comunidad').add(NuevoElementoOption,null);
				}
				catch(e)
				{
					document.getElementById('Comunidad').add(NuevoElementoOption);				
				}
			}
			document.getElementById('Comunidad').selectedIndex=0;
			
			
			//Cargamos los tipos de establecimiento
			DisparadorTipoEstablecimiento();			
		}
		isWorking = false;
	}
}

function handleHttpResponseProvincia()
{
	if (http.readyState == 4)
	{
		if (http.responseText.indexOf("invalid") == -1)
		{
			xmlDoc=http.responseXML;
			
			var x=xmlDoc.getElementsByTagName('provincia');
			var NumeroComunidades = x.length;

			NuevoElementoOption=document.createElement('option');
			NuevoElementoOption.value=0;
			NuevoElementoOption.text='------';
			try
			{
				//Version Mozilla
                                try{
                                    document.getElementById('Provincia').add(NuevoElementoOption,null);
                                }
                                catch(e){
                                    document.getElementById('provincia').add(NuevoElementoOption,null);
                                }
			}
			catch(e)
			{
				//Version IE
                                try{
                                    document.getElementById('Provincia').add(NuevoElementoOption);
                                }
                                catch(e){
                                    document.getElementById('provincia').add(NuevoElementoOption);
                                }
			}
		
			for(i=0;i<NumeroComunidades;i++)
			{
				var id = xmlDoc.getElementsByTagName('provincia').item(i).getElementsByTagName('identificador').item(0).firstChild.data;
				var nombre = xmlDoc.getElementsByTagName('provincia').item(i).getElementsByTagName('nombre').item(0).firstChild.data;
			
				//Contruimos el select (persiana)
				NuevoElementoOption=document.createElement('option');
				NuevoElementoOption.value=id;
				NuevoElementoOption.text=nombre;
				try
				{
					//Version Mozilla
                                        try{
                                            document.getElementById('Provincia').add(NuevoElementoOption,null);
                                        }
                                        catch(e){
                                            document.getElementById('provincia').add(NuevoElementoOption,null);
                                        }
				}
				catch(e)
				{
					//Version IE
                                        try{
                                            document.getElementById('Provincia').add(NuevoElementoOption);
                                        }
                                        catch(e){
                                            document.getElementById('provincia').add(NuevoElementoOption);
                                        }
				}
			}
                        try{
                            document.getElementById('Provincia').selectedIndex=0;
                        }
                        catch(e){
                            document.getElementById('provincia').selectedIndex=0;
                        }
		}
		isWorking = false;
	}
}


function handleHttpResponseComarca()
{
	if (http.readyState == 4)
	{
		if (http.responseText.indexOf("invalid") == -1)
		{
			xmlDoc=http.responseXML;
			
			var x=xmlDoc.getElementsByTagName('comarca');
			var NumeroComunidades = x.length;

			NuevoElementoOption=document.createElement('option');
			NuevoElementoOption.value=0;
			NuevoElementoOption.text='------';
			try
			{
				//Version Mozilla
                                try{
                                    document.getElementById('Comarca').add(NuevoElementoOption,null);
                                }
                                catch(e){
                                    document.getElementById('comarca').add(NuevoElementoOption,null);
                                }
			}
			catch(e)
			{
				//Version IE
                                try{
                                    document.getElementById('Comarca').add(NuevoElementoOption);
                                }
                                catch(e){
                                    document.getElementById('comarca').add(NuevoElementoOption);
                                }
			}
		
			for(i=0;i<NumeroComunidades;i++)
			{
				var id = xmlDoc.getElementsByTagName('comarca').item(i).getElementsByTagName('identificador').item(0).firstChild.data;
				var nombre = xmlDoc.getElementsByTagName('comarca').item(i).getElementsByTagName('nombre').item(0).firstChild.data;
			
				//Contruimos el select (persiana)
				NuevoElementoOption=document.createElement('option');
				NuevoElementoOption.value=id;
				NuevoElementoOption.text=nombre;
				try
				{
					//Version Mozilla
                                        try{
                                            document.getElementById('Comarca').add(NuevoElementoOption,null);
                                        }
                                        catch(e){
                                            document.getElementById('comarca').add(NuevoElementoOption,null);
                                        }
				}
				catch(e)
				{
					//Version IE
                                        try{
                                            document.getElementById('Comarca').add(NuevoElementoOption);
                                        }
                                        catch(e){
                                            document.getElementById('comarca').add(NuevoElementoOption);
                                        }
				}
			}
                        try{
                            document.getElementById('Comarca').selectedIndex=0;
                        }
                        catch(e){
                            document.getElementById('comarca').selectedIndex=0;
                        }
		}
		isWorking = false;
	}
}


/**
* Recoge todos los tipos de establecimientos y sus valores.
*/
function handleHttpTipoEstablecimiento()
{
	if (http.readyState == 4)
	{
		if (http.responseText.indexOf("invalid") == -1)
		{
			xmlDoc=http.responseXML;
			
			var x=xmlDoc.getElementsByTagName('tipoestablecimiento');
			var NumeroComunidades = x.length;

			NuevoElementoOption=document.createElement('option');
			NuevoElementoOption.value=0;
			NuevoElementoOption.text='------';
			document.getElementById('TipoAlojamiento').add(NuevoElementoOption,null);
		
			for(i=0;i<NumeroComunidades;i++)
			{
				var id = xmlDoc.getElementsByTagName('tipoestablecimiento').item(i).getElementsByTagName('identificador').item(0).firstChild.data;
				var nombre = xmlDoc.getElementsByTagName('tipoestablecimiento').item(i).getElementsByTagName('nombre').item(0).firstChild.data;
			
				//Contruimos el select (persiana)
				NuevoElementoOption=document.createElement('option');
				NuevoElementoOption.value=id;
				NuevoElementoOption.text=nombre;
				document.getElementById('TipoAlojamiento').add(NuevoElementoOption,null);
			}
			document.getElementById('TipoAlojamiento').selectedIndex=0;
		}
		isWorking = false;
	}
}

/**
* El disparador debe llamarse cuando se activa el evento que debe generarlo.
*/
function DisparadorCCAA()
{
	if (!isWorking && http)
	{
		/*
		//Tomamos el indice más adecuado.
		var Establecimiento = document.getElementById("form_select").options[document.getElementById("form_select").selectedIndex].value;
		*/

		//Envio por post
		http.open("POST", url_buscador_rapido , true);
		http.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
		http.onreadystatechange = handleHttpResponseCCAA;
		isWorking = true;
		http.send("Evento=COMUNIDADES");
	}
}


/**
* El disparador debe llamarse para recuperar las provincias de una CCAA
*/
function DisparadorProvincia()
{
	BorrarProvincias();
	BorrarComarcas();

	if (!isWorking && http){
		//Tomamos el indice.
		var Comunidad = document.getElementById("Comunidad").options[document.getElementById("Comunidad").selectedIndex].value;

		//Envio por post
		http.open("POST", url_buscador_rapido , true);
		http.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
		http.onreadystatechange = handleHttpResponseProvincia;
		isWorking = true;
		http.send("Evento=PROVINCIAS&Comunidad="+eval(Comunidad));
	}
}

/**
* El disparador debe llamarse para recuperar las provincias de una CCAA
*
* @param int Comunidad Indice de la comunidad a explorar.
*/
function DisparadorProvinciaParametro(Comunidad)
{
        //Si cambia la provincia debemos cargar las comarcas
        document.getElementById('provincia').onchange = DisparadorComarca;

        //Borramos la información para preparar el cambio.
	BorrarProvincias();
	BorrarComarcas();

	if (!isWorking && http){
		//Envio por post
		http.open("POST", url_buscador_rapido , true);
		http.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
		http.onreadystatechange = handleHttpResponseProvincia;
		isWorking = true;
		http.send("Evento=PROVINCIAS&Comunidad="+eval(Comunidad));
	}
}



/**
* El disparador debe llamarse para recuperar las comarcas de una provincia
*/
function DisparadorComarca()
{
	BorrarComarcas();
	
	if (!isWorking && http)
	{
		//Tomamos el indice.
                var Comunidad = null;
                try{
                    Comunidad = document.getElementById("Provincia").options[document.getElementById("Provincia").selectedIndex].value;
                }
                catch(e){
                    Comunidad = document.getElementById("provincia").options[document.getElementById("provincia").selectedIndex].value;
                }

		//Envio por post
		http.open("POST", url_buscador_rapido , true);
		http.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
		http.onreadystatechange = handleHttpResponseComarca;
		isWorking = true;
		http.send("Evento=COMARCAS&Provincia="+eval(Comunidad));
	}
}


/**
* El disparador debe llamarse para recuperar los tipos de establecimientos
*/
function DisparadorTipoEstablecimiento()
{
	BorrarComarcas();
	
	if (!isWorking && http)
	{
		//Envio por post
		http.open("POST", url_buscador_rapido , true);
		http.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
		http.onreadystatechange = handleHttpTipoEstablecimiento;
		isWorking = true;
		http.send("Evento=TIPOS_ESTABLECIMIENTO");
	}
}



function BorrarComarcas()
{
	var x=document.getElementById("Comarca");
        if (null == x)
            var x=document.getElementById("comarca");
	while (x.length>0)
	  x.remove(x.length-1);
}

function BorrarProvincias()
{
	//Hay que eliminar los elementos presentes en la lista de Provincia y Comarca
	var x=document.getElementById("Provincia");
        if (null == x)
            var x=document.getElementById("provincia");
	while (x.length>0)
	  x.remove(x.length-1);
}



/**
* Función básica para el manejo de llamadas AJAX
*/
function getHTTPObject()
{
	var xmlhttp;
	/*@cc_on
	@if (@_jscript_version >= 5)
	try
	{
		xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
	}
	catch (e)
	{
		try
		{
			xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
		}
		catch (E)
		{
			xmlhttp = false;
		}
	}
	@else
		xmlhttp = false;
	@end @*/
	if (!xmlhttp && typeof XMLHttpRequest != "undefined")
	{
		try
		{
			xmlhttp = new XMLHttpRequest();
			xmlhttp.overrideMimeType("text/xml");
		}
		catch (e)
		{
			xmlhttp = false;
		}
	}
	return xmlhttp;
}
var http = getHTTPObject(); // We create the HTTP Object

/**
* Cargador por defecto del buscador rápido.
*/
function CargaBuscadorRapido()
{
	//alert(isWorking); 
	
	//Lo primero que hacemos cuando cargamos el sistema es preguntar por las CCAA
	DisparadorCCAA();	
	
	//Si hay un cambio en el listado debemos cargar las provincias
	document.getElementById('Comunidad').onchange = DisparadorProvincia;
	
	//Si hay un cambio en el listado debemos cargar las comarcas
	document.getElementById('Provincia').onchange = DisparadorComarca;	
}
