/*************************************************************************
PREDICTIVO
*************************************************************************/
var focused=-1;
var lastFocused;
var filter;
var finded=false;

function predictiveBlur (capa) {
	// cerrar la capa
	setTimeout("displayCapa ('"+capa+"','none');",3000);
	//displayCapa (capa,'none');
}
function selectFromPredictive(field, TRpattern, capaT){
	if(focused>=0){
		var capa=TRpattern+"_"+focused+"0";
		field.value=document.getElementById(capa).innerHTML;
		displayCapa (capaT,"none");
		//carga_url('ajax.php?ac='+action+'&id='+document.getElementById(TRpattern+focused).value,capa);

	}
}
function getFocus(lastTR, TRpattern, lastFocused,capa){
	/////////////////ojo
	document.getElementById(capa).scrollTop = lastTR*110;
	changeColor('on',lastTR, TRpattern);
	if((typeof(lastFocused)!="undefined") && (lastFocused>=0)){ changeColor('off',lastFocused, TRpattern); }
}
function moveFocus(n,  TRpattern, capa) {
	if(typeof(focused)=="undefined"){ focused=0; getFocus(focused,TRpattern, 0,capa); return true; }
	if(n){
		focused++;
		if(focused<totalTR){
			lastFocused=focused-1;
			getFocus(focused,TRpattern,lastFocused,capa);
		}else{ focused--; }
	}else{
		focused--;
		if(focused>=0){
			lastFocused=focused+1;
			getFocus(focused,TRpattern,lastFocused,capa);
		}else{ focused++; }
	}
	//msg_change ("borrar","lastFocused"+lastFocused+" focused"+focused)
}

function checkArrows (field, evt, action, capa,TRpattern ){
	var keyCode =
	document.layers ? evt.which :
	document.all ? event.keyCode :
	document.getElementById ? evt.keyCode : 0;
	if (keyCode == 40){
		if(finded==false){ predict(field, action, capa, TRpattern); }
		else{ moveFocus(1,TRpattern,capa); }
	}else if (keyCode == 38){
		if(finded==true){ moveFocus(0,TRpattern,capa); }
	}else if ((keyCode != 37 && keyCode != 39 && keyCode != 13)){
		lastFocused=0;
		focused=-1;
		predict(field, action, capa, TRpattern);
	}
	return true;
}
function noSend (field, evt,  TRpattern, capaT) {
	var keyCode =
	document.layers ? evt.which :
	document.all ? event.keyCode :
	document.getElementById ? evt.keyCode : 0;
	if(keyCode==13) {
		selectFromPredictive(field, TRpattern, capaT);
		return false;
	}
	return true;
}
function predict(el, action, capa, TRpattern){
	capaP=document.getElementById(capa);
	//alert (capa);
	if (!$(capaP).length>0) {
		//alert ("la ponemos");
		//$(el).append("<div id=\"resultPredict\" style=\"position: absolute; margin-top: 50px; z-index: 100; width: 250px; border: 1px solid #000000; display: none;\">Predictivo</div>");

	} else {
		//alert ("existe");
		//$("#resultPredict").remove();
		//$(capaP).show("fast");
	}

	//capaP.style.display="block";
	//alert ("LONGITUD:"+el.value.length)
    if(el.value.length>1){
        var q = el.value;

		if (q.length>1) {
	        carga_url('extranet.php?TRpattern='+TRpattern+'&capa='+capa+'&field='+el.name+'&aE=x&ac='+action+'&q='+escape(q),capa);
		}
    }
}
function changeColor(color,i, TRpattern){
	capaM=TRpattern+"_"+i+"0";
	if (color=="on") {
		giveCSS (capaM,'predictiveTDselected');
	} else if (color=="off") {
		removeCSS (capaM,'predictiveTDselected');
	}
    //document.getElementById(TRpattern+i+"0").style.background="#"+color;
	// giveCSS (capa,'predictiveTDselected')
	// removeCSS (capa,'predictiveTDselected')

    /*if(color=="0a246a"){
        document.getElementById("capaRel3_"+i).style.color="white";
        document.getElementById("linkRef_"+i).style.color="white";
        if(document.getElementById("linkRef2_"+i)) document.getElementById("linkRef2_"+i).style.color="white";
    }else{
        document.getElementById("capaRel3_"+i).style.color="black";
        document.getElementById("linkRef_"+i).style.color="blue";
        if(document.getElementById("linkRef2_"+i)) document.getElementById("linkRef2_"+i).style.color="blue";
    }*/
}
/*************************************************************************
COMPROBACION DE NAVEGADOR
*************************************************************************/
function BrowserCheck() {
  var b = navigator.appName;
  if (b=="Netscape") this.b = "ns";
  else if (b=="Microsoft Internet Explorer") this.b = "ie";
  else this.b = b;
  this.version = navigator.appVersion;
  this.v = parseInt(this.version);
  this.ns = (this.b=="ns" && this.v>=4);
  this.ns4 = (this.b=="ns" && this.v==4);
  this.ns6 = (this.b=="ns" && this.v==5);
  this.ie = (this.b=="ie" && this.v>=4);
  this.ie4 = (this.version.indexOf('MSIE 4')>0);
  this.ie5 = (this.version.indexOf('MSIE 5')>0);
  this.ie7 = (this.version.indexOf('MSIE 7')>0);
  this.min = (this.ns||this.ie);
}
is = new BrowserCheck();
/*************************************************************************
CARGA AJAX
*************************************************************************/
function carga_url (URL,capa,noOcultar) {
	if(typeof($)!="undefined"){
	    if(!noOcultar){ document.getElementById(capa).style.display="none"; }
		$(document.getElementById(capa)).html('');
		  $(document.getElementById(capa)).load(URL,function(){
			$("#"+capa).slideToggle("slow");
		  });
    }
}
function carga_urlOpener (URL,capa,atthened) {
	$(window.opener.document.getElementById(capa)).html('');
	  $(window.opener.document.getElementById(capa)).load(URL,function(){
		$(window.opener.document.getElementById(capa)).slideDown("slow");
		eval(atthened);
	  });
}
/*************************************************************************
CAPAS
*************************************************************************/
function msg_change (capa,htmltext) {
	$(document.getElementById(capa)).html(htmltext);
}

function displayCapa (capa,status) {
	if (status!=undefined && status!='') {
		document.getElementById(capa).style.display=status;
	} else {
		if (document.getElementById(capa).style.display=="block") {
			document.getElementById(capa).style.display="none";
		} else {
			document.getElementById(capa).style.display="block";
		}
	}
}
function displayCapaSlide (capa,status) {
	if (status!=undefined && status!='') {
		document.getElementById(capa).style.display=status;
	} else {
		if (document.getElementById(capa).style.display=="block") {
			$(document.getElementById(capa)).slideUp("slow");
			//document.getElementById(capa).style.display="none";
		} else {
			//document.getElementById(capa).style.display="block";
			$(document.getElementById(capa)).slideDown("slow");
		}
	}
}

/*************************************************************************
CSS
*************************************************************************/

function giveCSS (capa,clase) {
	$(document.getElementById(capa)).addClass(clase);
}
function removeCSS (capa,clase) {
	$(document.getElementById(capa)).removeClass(clase);
}
/**********************************************************************************************************************************************
GOOGLE MAPS
************************************************************************************************************************************************/
function locateGPS (direccion_tipo,direccion_field,municipio_field,provincia_field, latitud, longitud, formularioNombre) {
	var formulario=document.forms[formularioNombre];
	//var tipovia=formulario[direccion_tipo].value;
	var tipovia='';
	var direccion=formulario[direccion_field].value;
	var municipio=formulario[municipio_field].value;
	var provincia=formulario[provincia_field].value;
	var url="popup.php?ac=geolocate&tipovia="+tipovia+"&direccion="+direccion+"&municipio="+municipio+"&provincia="+provincia+"&formulario="+formularioNombre+"&latitud="+latitud+"&longitud="+longitud;
	popUp (url,800,600,'geolocate')	;
}
function locateGPS2Form (latitud, longitud, formularioNombre, lat_value, long_value, formularioNombre2) {
	var formulario=document.forms[formularioNombre2];
	var latitud_value=formulario[lat_value].value;
	var longitud_value=formulario[long_value].value;

	var formulario2=window.opener.document.forms[formularioNombre];
	formulario2[latitud].value=latitud_value;
	formulario2[longitud].value=longitud_value;
	window.close();
}
function drawPoligonGPS (lat_value,long_value, poligon_field, formularioNombre) {
	var formulario=document.forms[formularioNombre];
	var lat_value=formulario[lat_value].value;
	var long_value=formulario[long_value].value;
	var url="popup.php?ac=drawpoligon2&lat_value="+lat_value+"&long_value="+long_value+"&formulario="+formulario+"&poligon_field="+poligon_field;
	popUp (url,800,600,'geolocate')	;
}
/*************************************************************************
SHOWS
*************************************************************************/
function showGestImgs (show, accion, id, mod, filter, target) {
	var url="admin.php?aE=pu&ac=gestShowImgs&show="+show+"&acc="+accion+"&rid="+id;
	if (mod != "" && mod != null) {
		url += "&mod="+mod;
	}
	if (filter != "" && filter != null) {
		url += "&filter="+filter;
	}
	if (target != "" && target != null) {
		url += "&target="+target;
	}
	popup_w = popUp (url,800,600,'gestshow');
}

/*************************************************************************
NOTICIAS
*************************************************************************/
function noticiasGestImgs (noticia, accion, id, mod, filter, target) {
	var url="admin.php?aE=pu&ac=gestImgs&noticia="+noticia+"&acc="+accion+"&rid="+id;
	if (mod != "" && mod != null) {
		url += "&mod="+mod;
	}
	if (filter != "" && filter != null) {
		url += "&filter="+filter;
	}
	if (target != "" && target != null) {
		url += "&target="+target;
	}
	popup_w = popUp (url,800,600,'gestnoticias');
}
function noticiasGestDownloads (noticia, accion, id, mod, filter, target) {
	var url="admin.php?aE=pu&ac=gestDownloads&noticia="+noticia+"&acc="+accion+"&rid="+id;
	if (mod != "" && mod != null) {
		url += "&mod="+mod;
	}
	if (filter != "" && filter != null) {
		url += "&filter="+filter;
	}
	if (target != "" && target != null) {
		url += "&target="+target;
	}
	popup_w = popUp (url,800,600,'gestnoticias');
}
function noticiasGestLinks (noticia, accion, id, mod, filter, target) {
	var url="admin.php?aE=pu&ac=gestLinks&noticia="+noticia+"&acc="+accion+"&rid="+id;
	if (mod != "" && mod != null) {
		url += "&mod="+mod;
	}
	if (filter != "" && filter != null) {
		url += "&filter="+filter;
	}
	if (target != "" && target != null) {
		url += "&target="+target;
	}
	popup_w = popUp (url,800,600,'gestnoticias');
}
/**
  * FUNCIONES PARA
  * GESTI�N DE ILUSTRADORES
  * EN GESTOR
  */
function ilustradoresGestIlustraciones (il_ID, accion, mod, target) {
	var url="admin.php?aE=pu&ac=gestIlustraciones&ilustracion="+il_ID+"&acc="+accion;
	if (mod != "" && mod != null) {
		url += "&mod="+mod;
	}
	if (target != "" && target != null) {
		url += "&target="+target;
	}
	popup_w = popUp (url,800,600,'gestilustraciones');
}
function ilustradoresGestNoticias (noticia_ID, accion, mod, target) {
	var url="admin.php?aE=pu&ac=gestNoticias&noticia="+noticia_ID+"&acc="+accion;
	if (mod != "" && mod != null) {
		url += "&mod="+mod;
	}
	if (target != "" && target != null) {
		url += "&target="+target;
	}
	popup_w = popUp (url,800,600,'gestnoticias');
}
/******************************************************************************************
PREDICTIVO DE CIUDADES
******************************************************************************************/
function locCityPredictive (string, predDiv, cityField, stateField, countryField) {
	if (string.length>3) {
		carga_url ('ajax.php?ac=locCityPredictive&string='+string+'&predDiv='+predDiv+'&cityField='+cityField+'&stateField='+stateField+'&countryField='+countryField,predDiv);
	}
	return true;
}
function locCityAutocomplete (predDiv, cityField, stateField, countryField, city, cityID, state, stateID, country, countryID) {
	var predictiveDiv = document.getElementById (predDiv);
	var ciudad = document.getElementById (cityField);
	var provincia = document.getElementById (stateField);
	var pais = document.getElementById (countryField);
	ciudad.value=city;
	provincia.value=state;
	pais.value=country;
	predictiveDiv.style.display="none";
}
/****************************************************************************************
JS DAMIA
****************************************************************************************/
function makeURL(str) {
    var strAux = str.toLowerCase();
    strAux = strAux.replace(/-/g," ");
    strAux = strAux.replace(/^\s*|\s*$/g,"");
    while(strAux.indexOf("/")!=-1) strAux = strAux.replace("/","-");
    while(strAux.indexOf("(")!=-1) strAux = strAux.replace("(","");
    while(strAux.indexOf(")")!=-1) strAux = strAux.replace(")","");
    while(strAux.indexOf("*")!=-1) strAux = strAux.replace("*","");
    strAux = strAux.replace(/ /g,"-");
    strAux = strAux.replace(/á/g,"a");
    strAux = strAux.replace(/é/g,"e");
    strAux = strAux.replace(/í/g,"i");
    strAux = strAux.replace(/ó/g,"o");
    strAux = strAux.replace(/ú/g,"u");
    strAux = strAux.replace(/ñ/g,"n");
    while(strAux.indexOf("--")!=-1) strAux = strAux.replace("--","-");
    return strAux;
}
function changeSection(){
    var pregunta = window.confirm("Atención! Está a punto de cambiar de sección, los datos que tiene escritos no se guardarán.\n¿Desea redirigirse a la nueva sección?");
    if(pregunta){ return true; }
    else { return false; }
}
function cs(){ return changeSection(); }
function getCheckedValue(radioObj) {
	if(!radioObj)
		return "";
	var radioLength = radioObj.length;
	if(radioLength == undefined)
		if(radioObj.checked)
			return radioObj.value;
		else
			return "";
	for(var i = 0; i < radioLength; i++) {
		if(radioObj[i].checked) {
			return radioObj[i].value;
		}
	}
	return "";
}
function displayErrorSQL(){
    $(document.getElementById("blockError")).fadeIn("slow");
}
function html_entity_decode(str) {
  var ta=document.createElement("textarea");
  ta.innerHTML=str.replace(/</g,"&lt;").replace(/>/g,"&gt;");
  return ta.value;
}
