var categoriaBuscadorOcio = "";
var edicion = "";

function showDiv(h) {
	document.getElementById('wn_contenttab').style.display = 'block';
	if (h =='MainSearch_lkbTabEventos'){
        document.getElementById('MainSearch_lkbTabEventos').className = 'wn_activetab';
		document.getElementById('MainSearch_lkbTabBares').className = '';
		document.getElementById('MainSearch_lkbTabRestaurantes').className = '';
    }else if(h =='MainSearch_lkbTabBares'){
        document.getElementById('MainSearch_lkbTabBares').className = 'wn_activetab';
		document.getElementById('MainSearch_lkbTabEventos').className = '';
		document.getElementById('MainSearch_lkbTabRestaurantes').className = '';	
	}else if(h =='MainSearch_lkbTabRestaurantes'){
        document.getElementById('MainSearch_lkbTabRestaurantes').className = 'wn_activetab';
		document.getElementById('MainSearch_lkbTabBares').className = '';
		document.getElementById('MainSearch_lkbTabEventos').className = '';	
	}
}


function changeCategoria(pestanaId){

	/*Indica en qué edición está*/
	edicion = document.getElementById("MainSearch_cdd_ddlProvincia_ClientState").value.substring(0,2);

	if (edicion.search(":")==true)
	{	
		edicion = document.getElementById("MainSearch_cdd_ddlProvincia_ClientState").value.substring(0,1);			
	}

	if(pestanaId == 'MainSearch_lkbTabEventos'){
		categoriaBuscadorOcio = "eventos";
		showDiv(pestanaId);
		document.forms['formOcio'].Tipo.value = 'Evento';
		document.forms['formOcio'].MainSearch_txtSearchText.value = 'Evento, nombre del sitio, palabras clave';		
		insertaCapa("SelectProvincia","/includes_m/ocio/buscador/provincias_eventos_"+edicion+".html");
		insertaCapa("SelectCategoria","/includes_m/ocio/buscador/categoria_eventos.html");
//		insertaCapa("Options","/includes_m/ocio/buscador/FechaToFecha.html");
		changeLocalidad();
		document.getElementById("MainSearch_lblError").innerHTML = "";
		document.getElementById("optionsEventos").style.display = "";
		document.getElementById("optionsBares").style.display = "none";
		document.getElementById("optionsRestaurantes").style.display = "none";		

		/*Para saber en que pestaña estoy*/
	}else if(pestanaId == 'MainSearch_lkbTabBares'){
		categoriaBuscadorOcio = "bares";
		showDiv(pestanaId);
		document.forms['formOcio'].Tipo.value = 'Bar';	
		document.forms['formOcio'].MainSearch_txtSearchText.value = 'Nombre del bar, palabras clave';
		insertaCapa("SelectProvincia","/includes_m/ocio/buscador/provincias_bares_"+edicion+".html");
		insertaCapa("SelectCategoria","/includes_m/ocio/buscador/categoria_bares.html");
//		insertaCapa("Options","/includes_m/ocio/buscador/recomendadogps.html");
		changeLocalidad();
		document.getElementById("MainSearch_lblError").innerHTML = "";
		document.getElementById("optionsEventos").style.display = "none";
		ocultar
		document.getElementById("optionsBares").style.display = "";
		document.getElementById("optionsRestaurantes").style.display = "none";
	}else if(pestanaId == 'MainSearch_lkbTabRestaurantes'){
		categoriaBuscadorOcio = "restaurante";
		showDiv(pestanaId);		
		document.forms['formOcio'].Tipo.value = 'Restaurante';
		document.forms['formOcio'].MainSearch_txtSearchText.value = 'Nombre del restaurante, palabras clave';
		insertaCapa("SelectProvincia","/includes_m/ocio/buscador/provincias_restaurantes_"+edicion+".html");
		insertaCapa("SelectCategoria","/includes_m/ocio/buscador/categoria_restaurantes.html");	
//		insertaCapa("Options","/includes_m/ocio/buscador/recomendadomenudia.html");		
		changeLocalidad();
		document.getElementById("MainSearch_lblError").innerHTML = "";
		document.getElementById("optionsEventos").style.display = "none";
		document.getElementById("optionsBares").style.display = "none";
		document.getElementById("optionsRestaurantes").style.display = "";
	}
}


function pinchaCalendario(obj){
	__PopCalShowCalendar("MainSearch$txtSearchDate",obj);
	document.forms['formOcio'].rdSearchDate[0].checked= false;
	document.forms['formOcio'].rdSearchDate[1].checked= false;	
}


function insertCapa(capa, url, preffix, suffix)
{
	// Creo el objeto AJAX
	var ajax=nuevoAjax();
	var capaContenedora = document.getElementById(capa);
	// Abro la conexión, envío cabeceras correspondientes al uso de POST y envío los datos con el método send del objeto AJAX
	
	url +="?ms="+new Date().getTime();
	
	ajax.open("GET", url , true);
	ajax.setRequestHeader("Content-Type", "application/x-www-form-urlencoded;");
	ajax.send(null);

	ajax.onreadystatechange=function()
	{
		if (ajax.readyState==4)
		{
			if(ajax.status==200){
				capaContenedora.innerHTML = preffix+ajax.responseText+suffix;
			}
		}
	}
}

function insertaCapa(capa, url)
{
	insertCapa(capa, url, '', '');
}

function changeLocalidad(){

	prov = document.forms['formOcio'].Provincia.value;
	document.getElementById("Localidad").disabled = false;

    if (document.getElementById("MainSearch_ddlProvincia").value == '-1' )
	{
		//insertaCapa("SelectLocalidad","/includes_m/ocio/buscador/localidades_-1.html");
		document.getElementById("Localidad").disabled = true
	}
	else if (document.forms['formOcio'].MainSearch_ddlProvincia.value  == '99')
	{
		//insertaCapa("SelectLocalidad","/includes_m/ocio/buscador/localidades_99.html");
		document.getElementById("Localidad").disabled = true
	}
	else if (document.forms['formOcio'].Tipo.value == 'Bar')
	{
		insertaCapa("SelectLocalidad","/includes_m/ocio/buscador/localidades_bares_"+prov+".html");	
	}
	else if (document.forms['formOcio'].Tipo.value == 'Restaurante' )
	{
		insertaCapa("SelectLocalidad","/includes_m/ocio/buscador/localidades_restaurantes_"+prov+".html");		
	}	
	else if (document.forms['formOcio'].Tipo.value == 'Evento' ) {		
		insertaCapa("SelectLocalidad","/includes_m/ocio/buscador/localidades_"+prov+".html");
		//insertCapa("SelectLocalidad","/includes_m/ocio/buscador/localidades_"+prov+"_"+categoriaBuscadorOcio+".html","<select name='Localidad' id='Localidad' class='wn_select'><option selected='selected' value=''>Todas las localidades</option>","</select>");
	}
}

function startBuscadorOcio(){
	changeCategoria('MainSearch_lkbTabEventos');
	changeLocalidad();
}

function buscarTexto(){

	document.forms['formOcio'].Categoria.selectedIndex = 0;
	document.forms['formOcio'].Provincia.selectedIndex = 0;
	document.forms['formOcio'].Localidad.selectedIndex = 0;
	if(document.forms['formOcio'].Tipo.value == 'Bar' || document.forms['formOcio'].Tipo.value == 'Restaurante'){
		document.forms['formOcio'].rdSearchDate[0].checked= false;
		document.forms['formOcio'].rdSearchDate[1].checked= false;	
	}
	document.forms['formOcio'].Behaviour.value = "TextSearch";
	document.forms['formOcio'].submit();
}

function buscarCombos() {
	
	if (!validarFechas())
	{	
		return false;
	}
	else if ((document.forms['formOcio'].Texto.value == "Evento, nombre del sitio, palabras clave") || (document.forms['formOcio'].Texto.value == "Nombre del bar, palabras clave") ||
		(document.forms['formOcio'].Texto.value == "Nombre del restaurante, palabras clave")) {
		document.forms['formOcio'].Texto.value = "";
		document.forms['formOcio'].Behaviour.value = "";
		if (document.getElementById("Localidad").disabled = false)
		{
			document.getElementById("Localidad").disabled = true
		}
		document.forms['formOcio'].submit();
		
		return true;
	}
	else
	{	
		document.forms['formOcio'].Behaviour.value = "";
		if (document.getElementById("Localidad").disabled = false)
		{
			document.getElementById("Localidad").disabled = true
		}
		document.forms['formOcio'].submit();
		
	}

}

function validarFechas() {

/*Pestaña de Bares*/
if ( categoriaBuscadorOcio == "bares" )
	{		
		if (document.forms['formOcio'].Texto.value == "Nombre del bar, palabras clave")
		{	
			document.forms['formOcio'].Texto.value == "";			
		}
					
        return true;
	} 
else if (categoriaBuscadorOcio == "restaurante")
	{
		if (document.forms['formOcio'].Texto.value == "Nombre del restaurante, palabras clave")
		{	
			document.forms['formOcio'].Texto.value == "";			
		}
						
        return true;

	}
else if (categoriaBuscadorOcio == "eventos")
	{	
		
		document.getElementById("MainSearch_imgError").style.display = "none";
		var AnioDesde = (document.getElementById("MainSearch_TbFechaDesde").value).substr(6,4);
		var MesDesde = ((document.getElementById("MainSearch_TbFechaDesde").value).substr(3,2))*1 - 1;
		var DiaDesde = (document.getElementById("MainSearch_TbFechaDesde").value).substr(0,2);

		var AnioHasta = (document.getElementById("MainSearch_TbFechaHasta").value).substr(6,4);
		var MesHasta = ((document.getElementById("MainSearch_TbFechaHasta").value).substr(3,2))*1 - 1;
		var DiaHasta = (document.getElementById("MainSearch_TbFechaHasta").value).substr(0,2);

		var Fecha_Desde = new Date(AnioDesde,MesDesde,DiaDesde);
		var Fecha_Hasta = new Date(AnioHasta,MesHasta,DiaHasta);
		var Fecha_Hoy = new Date();

		Fecha_Hoy.setHours(0);
		Fecha_Hoy.setMinutes(0);
		Fecha_Hoy.setSeconds(0);
		Fecha_Hoy.setMilliseconds(0);
		
		/*Pestaña de Eventos: Tratamiento de fechas*/
		if (!document.getElementById("MainSearch_rbToday").checked &&
			!document.getElementById("MainSearch_rbTomorow").checked &&
			!document.getElementById("MainSearch_rbFinSemana").checked &&
			document.getElementById("MainSearch_TbFechaDesde").value == 'Desde(dd-mm-aaaa)' &&
			document.getElementById("MainSearch_TbFechaHasta").value == 'Hasta(dd-mm-aaaa)' )
		{	
			document.getElementById("MainSearch_lblError").innerHTML = document.getElementById("divError_NoFecha").innerHTML;		
			return false;
		}
		 else if (document.getElementById("MainSearch_rbToday").checked ||
			document.getElementById("MainSearch_rbTomorow").checked ||
			document.getElementById("MainSearch_rbFinSemana").checked && (
			document.getElementById("MainSearch_TbFechaDesde").value == 'Desde(dd-mm-aaaa)' ||
			document.getElementById("MainSearch_TbFechaHasta").value == 'Hasta(dd-mm-aaaa)' ))
		{	
            if (document.forms['formOcio'].Texto.value == "Evento, nombre del sitio, palabras clave")
			{	
				document.forms['formOcio'].Texto.value == "";			
			}
			document.forms['formOcio'].Behaviour.value = "";
			document.forms['formOcio'].submit();			
			return true;
		}
		 else if(Fecha_Desde > Fecha_Hasta && document.getElementById("MainSearch_TbFechaDesde").value != "" && document.getElementById("MainSearch_TbFechaHasta").value != "" )
		 {	
			document.getElementById("MainSearch_lblError").innerHTML = document.getElementById("divError_HastaAnterior").innerHTML;			
			return false;
		 }
		
		else if (Fecha_Desde < Fecha_Hoy  && document.getElementById("MainSearch_TbFechaDesde").value != "" && document.getElementById("MainSearch_TbFechaHasta").value != "" )
		{	
			document.getElementById("MainSearch_lblError").innerHTML = document.getElementById("divError_DesdeAnterior").innerHTML;
			return false;
		}
		else if (Fecha_Desde = Fecha_Hoy && document.getElementById("MainSearch_TbFechaDesde").value != "" && document.getElementById("MainSearch_TbFechaHasta").value != "" )
		{	
			if (document.forms['formOcio'].Texto.value == "Evento, nombre del sitio, palabras clave")
			{	
				document.forms['formOcio'].Texto.value == "";			
			}
			document.forms['formOcio'].Behaviour.value = "";
			document.forms['formOcio'].submit();			
			return true;
		}
		else if (document.getElementById("MainSearch_rbToday").checked ||
			document.getElementById("MainSearch_rbTomorow").checked ||
			document.getElementById("MainSearch_rbFinSemana").checked )
		{	
			if (document.forms['formOcio'].Texto.value == "Evento, nombre del sitio, palabras clave")
			{	
				document.forms['formOcio'].Texto.value == "";			
			}
			return true;
		} 
		else
		{	
			if (document.forms['formOcio'].Texto.value == "Evento, nombre del sitio, palabras clave")
			{	
				document.forms['formOcio'].Texto.value == "";			
			}
			document.forms['formOcio'].Behaviour.value = "";
			document.forms['formOcio'].submit();			
			return true;
		}
	}
	
}

function borrarFechas() {
	document.getElementById("MainSearch_TbFechaDesde").value="";
	document.getElementById("MainSearch_TbFechaHasta").value="";
	document.getElementById("MainSearch_lblError").innerHTML = "" ;
	document.getElementById("MainSearch_imgError").style.display = 'none' ;
}
function borrarChecks() {
	document.getElementById("MainSearch_rbToday").checked= false;
	document.getElementById("MainSearch_rbTomorow").checked= false;
	document.getElementById("MainSearch_rbFinSemana").checked= false;

}

/* Objeto AJAX */
function nuevoAjax()
{ 
	var xmlhttp=false; 
	try 
	{ 
		// Creacion del objeto AJAX para navegadores no IE
		xmlhttp=new ActiveXObject("Msxml2.XMLHTTP"); 
	}
	catch(e)
	{ 
		try
		{ 
			// Creacion del objet AJAX para IE 
			xmlhttp=new ActiveXObject("Microsoft.XMLHTTP"); 
		} 
		catch(E) { xmlhttp=false; }
	}
	if (!xmlhttp && typeof XMLHttpRequest!='undefined') { xmlhttp=new XMLHttpRequest(); } 

	return xmlhttp; 
}




