var WEB_ROOT="http://"+window.location.hostname+"/";
var arrayTextosAut=new Array();
arrayTextosAut['0']='cursos de';
arrayTextosAut['1']='cursos en';
arrayTextosAut['2']='curso de';
arrayTextosAut['3']='curso en';
arrayTextosAut['4']='academias de';
arrayTextosAut['5']='academias en';
arrayTextosAut['6']='academia de';
arrayTextosAut['7']='academia en';
arrayTextosAut['8']='clases de';
arrayTextosAut['9']='clases en';
arrayTextosAut['10']='clase de';
arrayTextosAut['11']='clase en';

function ajax(fichero, posicion, arrayVariables){
	var nowtime = new Date();
	var tiempo = nowtime.getTime();
	var variables='';
	var cargando='si';
	if (arrayVariables instanceof Array){
		for(clave in arrayVariables){ 
			if (arrayVariables[clave]!=='' && clave!='funcion') variables+="&"+clave+"="+arrayVariables[clave];
			if (clave=='cargando') cargando=arrayVariables[clave];
		}
	}else variables="&valor="+arrayVariables;
	if (arrayVariables['directorioAjax']) var directorioAjax='/'+arrayVariables[clave];
	else var directorioAjax='/ajax';
	if (posicion!='' && cargando=='si') $(posicion).html("<img src='"+WEB_ROOT+"/imagenes/loading.gif' alt='Cargando'>");
	$.ajax( {
		type:"POST", url:WEB_ROOT+directorioAjax+"/"+fichero,
		data:"t="+tiempo+variables,
		success: function (msg) {
			if (posicion!='') $(posicion).html(msg);
		}
	} )
	if (posicion!='') $(posicion).ajaxStop(function(){})
}

function cambia(archivo, expositor){
	var nowtime = new Date();
	var tiempo = nowtime.getTime();
	var variables='';

	$("div#contenido").html("<img src='"+WEB_ROOT+"/imagenes/loading.gif' alt='Cargando'>");
	$.ajax( {
		type:"POST", url:WEB_ROOT+"/clientes/templates/"+archivo+".php",
		data:"t="+tiempo+"&expositor="+expositor,
		success: function (msg) {
			$("div#contenido").html(msg);
		}
	} )
	$("div#contenido").ajaxStop(function(){})
}

function busca(centros, pagina){
	if (document.getElementById('resultados')){
		if (centros=='no') centros=false;

		var arrayVariables=new Array();

		if (document.getElementById('textoBuscador')) var texto=document.getElementById('textoBuscador').value;
		else var texto='';
		if (document.getElementById('selectCategorias')) var materia=document.getElementById('selectCategorias').value;
		else var materia='';
		if (document.getElementById('selectLocalizacion')) var localizacion=document.getElementById('selectLocalizacion').value;
		else var localizacion='';
		if (document.getElementById('selectModalidad')) var modalidad=document.getElementById('selectModalidad').value;
		else var modalidad='';

		arrayVariables['texto']=texto;
		arrayVariables['materia']=materia;
		arrayVariables['localizacion']=localizacion;
		arrayVariables['modalidad']=modalidad;
		arrayVariables['centros']=centros;
		arrayVariables['pagina']=pagina;
		ajax('buscador.php', 'div#resultados', arrayVariables);
	}else{
		if (document.getElementById('textoBuscador')) var texto=document.getElementById('textoBuscador').value;
		else var texto='';
		location.href=WEB_ROOT+'?textoBuscado='+texto;
	}
}
function busquedaLugar(direccion, provincia, distancia, centros){
	var arrayVariables=new Array();
	if (centros=='no') centros=false;
	if (direccion==''){
		alert('Es necesario especificar la dirección');
		return false;
	}
	if (provincia==''){
		alert('Es necesario especificar la provincia');
		return false;
	}
	arrayVariables['direccionLugar']=direccion;
	arrayVariables['provinciaLugar']=provincia;
	arrayVariables['distanciaLugar']=distancia;
	arrayVariables['centros']=centros;

	if (document.getElementById('textoBuscador')) var texto=document.getElementById('textoBuscador').value;
	else var texto='';
	if (document.getElementById('selectCategorias')) var materia=document.getElementById('selectCategorias').value;
	else var materia='';
	if (document.getElementById('selectLocalizacion')) var localizacion=document.getElementById('selectLocalizacion').value;
	else var localizacion='';
	if (document.getElementById('selectModalidad')) var modalidad=document.getElementById('selectModalidad').value;
	else var modalidad='';

	arrayVariables['texto']=texto;
	arrayVariables['materia']=materia;
	arrayVariables['localizacion']=localizacion;
	arrayVariables['modalidad']=modalidad;

	ajax('busquedaLugar.php', 'div#aux', arrayVariables);
}
function generaContacto(elemento, tipo){
	var arrayVariables=new Array();
	arrayVariables['elemento']=elemento;
	arrayVariables['tipo']=tipo;
	ajax('contacto.php', 'div#cuadroContacto', arrayVariables);
}
function desplegables(categoria, subcategoria, provincia, poblacion, modalidad, centros){
	var arrayVariables=new Array();
	arrayVariables['categoria']=categoria;
	arrayVariables['subcategoria']=subcategoria;
	arrayVariables['provincia']=provincia;
	arrayVariables['poblacion']=poblacion;
	arrayVariables['modalidad']=modalidad;
	arrayVariables['centros']=centros;
	ajax('desplegables.php', 'div#desplegables', arrayVariables);
}
function banners(categoria, provincia, tipo, posicion, num){
	var arrayVariables=new Array();
	arrayVariables['categoria']=categoria;
	arrayVariables['provincia']=provincia;
	arrayVariables['tipo']=tipo;
	arrayVariables['posicion']=posicion;
	arrayVariables['num']=num;
	ajax('banners.php', 'div#banners'+posicion, arrayVariables);
}
function suscribe(email){
	if (!compruebaEmail(email)){
		alert('El email introducido no es correcto');
		return false;
	}
	var arrayVariables=new Array();
	arrayVariables['email']=email;
	ajax('suscribe.php', 'div#moduloSuscribe', arrayVariables);
}
function cuentaClick(tipo, id){
	var arrayVariables=new Array();
	arrayVariables['tipo']=tipo;
	arrayVariables['id']=id;
	ajax('cuentaClick.php', '', arrayVariables);
}
function cuentaContacto(expositor, tipo){
	var arrayVariables=new Array();
	arrayVariables['tipo']=tipo;
	arrayVariables['expositor']=expositor;
	ajax('contacta.php', '', arrayVariables);
}

function social(urlRSS){
	var arrayVariables=new Array();
	arrayVariables['urlRSS']=urlRSS;
	ajax('social.php', 'div#social', arrayVariables);
}

function contacta(expositor, nombre, email, telefono, texto, agregado, check){
	var arrayVariables=new Array();
	if (trim(nombre)==''){
		alert('Debes de indicar tu nombre');
		return false;
	}
	if (!compruebaEmail(email)){
		alert('Debes de indicar un email correcto');
		return false;
	}
	if (trim(texto)==''){
		alert('Debes de indicar tus comentarios');
		return false;
	}
	if (!check){
		alert('Es necesario aceptar el aviso legal para poder enviar el formulario');
		return false;
	}
	arrayVariables['nombre']=nombre;
	arrayVariables['texto']=texto;
	arrayVariables['email']=email;
	arrayVariables['tipo']='contacto';
	arrayVariables['telefono']=telefono;
	arrayVariables['expositor']=expositor;
	arrayVariables['agregado']=agregado;
	ajax('contacta.php', 'div#contacto', arrayVariables);
}

function verMapa(){
	ajax('mapa.php', 'div#mapa', document.getElementById('expositoresMapa').value);
}

function autocompletar(texto, capa, campo){
	posicionAutocompletar=0;
	texto=trim(texto);
	if (texto.length<=2) return false;
	for (i=0; i<arrayTextosAut.length; i++){
		if (arrayTextosAut[i].indexOf(texto)>=0 && arrayTextosAut[i].replace(texto, '')!='') return false;
	}
	$(capa).css("display", "block");
	var arrayVariables=new Array();
	arrayVariables['texto']=texto;
	arrayVariables['capa']=capa;
	arrayVariables['campo']=campo;
	arrayVariables['cargando']='no';
	ajax('autocompletar.php', capa, arrayVariables);
}
