base_url="http://localhost/mubiloop/";

function muestra_url(elid) {
		
	$.post("http://localhost/index.php/ajax/devuelve_url_ficha", {Id:elid}, function(html) {
		if (html!='KO') {
   			$("#verweb").hide();
			document.getElementById('verweb').innerHTML=html;
   			$("#verweb").fadeIn();
		}
	});
	
	
}

function muestra_telefono(elid) {
		
	$.post("http://localhost/index.php/ajax/devuelve_telefono_ficha", {Id:elid}, function(html) {
		if (html!='KO') {
   			$("#vertelefono").hide();
			document.getElementById('vertelefono').innerHTML=html;
   			$("#vertelefono").fadeIn();
		}
	});
	
	
}

function limpia_contenido(control) {

		control.value=control.value.replace(/<([^>]+)>/gi,'');
		control.value=control.value.replace(/"/gi,'').replace(/'/gi,'');
		
		return control.value;
		//"
	}
function limpia_contenido2(control) {

		aux=control.value.replace(/^[^\w+$|\s+$]/gi,'');
		aux=aux.toLowerCase();
		aux=aux.replace(/]/gi,'');
		aux=aux.replace(/ /gi,'-');
		aux=aux.replace(/ñ/gi,'n');
		
		return aux;
		//'
}
function quitaacentos(t){
        á="a";é="e";í="i";ó="o";ú="u";
        à="a";è="e";ì="i";ò="o";ù="u";
        â="a";ê="e";î="i";ô="o";û="u";
        ä="a";ë="e";ï="i";ö="o";ü="u";
        Á="A";É="E";Í="I";Ó="O";Ú="U";
        À="A";È="E";Ì="I";Ò="O";Ù="U";
        Â="A";Ê="E";Î="I";Ô="O";Û="U";
        Ä="A";Ë="E";Ï="I";Ö="O";Ü="U";
        acentos=/[áéíóúàèìòùâêîôûäëïöüÁÉÍÓÚÀÈÌÒÙÂÊÎÔÛÄËÏÖÜ]/g;
        return t.replace(acentos,
        function($1){
                return eval($1);
         }
   );
}
function Suplantar(alias){
	$.post("http://localhost/index.php/ajax/suplantar", {entidad:alias}, function(html) {
		if (html!='KO') {
			document.location=html;
		}
});}
function Volver_Suplantacion(){
	$.post("http://localhost/index.php/ajax/volver_suplantar", {}, function(html) {
		if (html!='KO') {
			document.location=document.location;
		}
});}
function Logout_Admin(){
	$.post("http://localhost/index.php/ajax/logout_admin", {}, function(html) {
		if (html!='KO') {
			document.location=document.location;
		}
});}

$(document).ready(function ()
{
	
  	$("#vertelefono").click(function(){

   		$("#vertelefono_imagen").hide();
   		$("#vertelefono_info").fadeIn();

	});


	$(".input_div input").blur(function(){
	info=$(this).parent().html();
	id=this.id;
 	$.post(base_url+"index.php/ajax/Comprueba_Errores", {valor:this.value,infocampo:info}, function(html) {
 		if (html=='Incorrecto') {
 			$('#'+id+"_div").addClass('error_div');salto=0;
 		}
 		else {
 			$('#'+id+"_div").removeClass('error_div');
 		}
	});
	});

	$(".input_div select").blur(function(){
	info=$(this).parent().html();
	id=this.id;
 	$.post(base_url+"index.php/ajax/Comprueba_Errores", {valor:this.value,infocampo:info}, function(html) {
 		if (html=='Incorrecto') {
 			$('#'+id+"_div").addClass('error_div');salto=0;
 		}
 		else {
 			$('#'+id+"_div").removeClass('error_div');
 		}
	});
	});
});
   
function Comprueba_Formulario(nombreformulario) {

		infos="";
		tipos="";
		valores="";
		ellid=new Array();
		salto=1;

		form=eval('document.'+nombreformulario);
		for (t=0;t<form.elements.length;t++) {
			aux=form.elements[t].id+'_div';
			if (document.getElementById(aux)!=null && document.getElementById(form.elements[t].id).type!='button' && document.getElementById(form.elements[t].id).type!='submit') {
				infos=infos+document.getElementById(form.elements[t].id+'_div').innerHTML+"~";
			}
			else {infos=infos+"~";}
			tipos=tipos+document.getElementById(form.elements[t].id).type+"~";
			
			if (document.getElementById(form.elements[t].id).type=='select-multiple') {
				val_aux="";
				for (yyy=0;yyy<form.elements[t+1].length;yyy++) {
					val_aux=val_aux+form.elements[t+1][yyy].value+";";
				}
				valores=valores+val_aux+"~";
			}
			else {
				valores=valores+form.elements[t].value+"~";
			}

		}
		$.post("http://localhost/index.php/ajax/Comprueba_Formulario", {valor:valores,infocampo:infos,tipos:tipos}, function(html) {
			resultados=html.split("~");

			for (t=0;t<(resultados.length-3);t++) {
				ellid[t]=form.elements[t].id;
		 		if (resultados[t]=='Incorrecto') {
		 			$('#'+ellid[t]+"_div").addClass('error_div');salto=0;
		 		}
		 		else {
		 			$('#'+ellid[t]+"_div").removeClass('error_div');
		 		}

			}
			
			if (document.getElementById('Condiciones_login')!=null && document.getElementById('Condicionesdeuso').id!=null && form.Condiciones_login.checked==false) {$('#Condicionesdeuso').addClass('error_div');salto=0;}
			else {$('#Condicionesdeuso').removeClass('error_div');}
			
			if (salto==1) {form.submit();}
			else {alert("Datos incorrectos. Revisa los campos marcados.")}

		});

}


function Accion_Post(form,url_post,url_salto,geo_direccion,prefijo) {
	
	if (geo_direccion != undefined) {
	$.post("http://localhost/index.php/ajax/geolocaliza",{direccion:geo_direccion}, function(html) {
		auxi=html.split(',');
		document.getElementById(prefijo+"Latitud").value=auxi[2];
		document.getElementById(prefijo+"Longitud").value=auxi[3];
		for (t=0;t<form.elements.length;t++) {
			if (document.getElementById(form.elements[t].id).type=='select-multiple') {
				val_aux="";
				for (yyy=0;yyy<form.elements[t+1].length;yyy++) {
					val_aux=val_aux+form.elements[t+1][yyy].value+";";
				}
				pp=form.elements[t+1].id+'_aux';
				if (document.getElementById(pp)!=null) {
					document.getElementById(pp).value=val_aux;
				}
			}
		}	
		var aux = $(form).serialize();
		$.post(url_post, aux, function(html) {retorna_resultado(html,form,url_salto);});
	});		
		
	}
	else {
		for (t=0;t<form.elements.length;t++) {
			if (document.getElementById(form.elements[t].id).type=='select-multiple') {
				val_aux="";
				for (yyy=0;yyy<form.elements[t+1].length;yyy++) {
					val_aux=val_aux+form.elements[t+1][yyy].value+";";
				}
				pp=form.elements[t+1].id+'_aux';
				if (document.getElementById(pp)!=null) {
					document.getElementById(pp).value=val_aux;
				}
			}
		}	
		var aux = $(form).serialize();
		$.post(url_post, aux, function(html) {retorna_resultado(html,form,url_salto);});
	}
}


function retorna_resultado(html,form,url_salto) {
	
  	if (html=='KO') {
		$('#error').addClass('error_general');salto=0;document.getElementById('error').innerHTML="Ha ocurrido un error. Por favor revisa los campos.";
	}
	else{
		if (html=='OK') {
			form.action=url_salto;form.submit();
		}
		else 
		{
				if (html>0) {
					
					form.Id.value=html;				
					form.action=url_salto;form.submit();
				}
				else {
					$('#error').addClass('error_general');salto=0;document.getElementById('error').innerHTML="Ha ocurrido un error. Por favor revisa los campos.<br><br>"+html;
				}
		}
}
}

function Desactivar(num,url,id) {
	
	$.post("http://localhost/admin_monazen/duplicados/Desactivar", {id:num}, function(html) {
		if (html=='OK') {
			alert('ENTIDAD DESACTIVADA.');
			document.getElementById(id).innerHTML="<a href='javascript:Activar("+num+",\""+url+"\",\""+id+"\")'>Activar</a>";
		}
		else {alert('Ha ocurrido un error.')}
		
	});		
}

function Activar(num,url,id) {
	
	$.post("http://localhost/admin_monazen/duplicados/Activar", {id:num}, function(html) {
		if (html=='OK') {
			alert('ENTIDAD ACTIVADA.');
			document.getElementById(id).innerHTML="<a href='javascript:Desactivar("+num+",\""+url+"\",\""+id+"\");'>Desactivar</a>";
		}
		else {alert('Ha ocurrido un error.')}
		
	});		
}