/*************************************************************************
PREDICTIVO
*************************************************************************/
var focused=-1;
var lastFocused;
var filter;
var finded=false;
function selectFromPredictive(field, TRpattern){
	if(focused>=0){
		var capa=TRpattern+focused+"0";
		
		field.value=document.getElementById(capa).innerHTML;
		displayCapa ("resultPredict","none");
		//carga_url('ajax.php?ac='+action+'&id='+document.getElementById(TRpattern+focused).value,capa);
		
	}
}
function getFocus(lastTR, TRpattern, lastFocused){
		
	document.getElementById('resultPredict').scrollTop = lastTR*110;
	changeColor('0a246a',lastTR, TRpattern);
	if((typeof(lastFocused)!="undefined") && (lastFocused>=0)){ changeColor('e5e5e5',lastFocused, TRpattern); }
}
function moveFocus(n,  TRpattern) {
	if(typeof(focused)=="undefined"){ focused=0; getFocus(focused,TRpattern, 0); return true; }
	if(n){
		focused++;
		if(focused<totalTR){
			lastFocused=focused-1;
			getFocus(focused,TRpattern,lastFocused);
		}else{ focused--; }
	}else{
		focused--;
		if(focused>=0){
			lastFocused=focused+1;
			getFocus(focused,TRpattern,lastFocused);
		}else{ 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); }
		else{ moveFocus(1,TRpattern); }
	}else if (keyCode == 38){
		if(finded==true){ moveFocus(0,TRpattern); }
	}else if ((keyCode != 37 && keyCode != 39 && keyCode != 13)){
		predict(field, action, capa);
	}
	return true;
}
function noSend (field, evt,  TRpattern) {
	var keyCode =
	document.layers ? evt.which :
	document.all ? event.keyCode :
	document.getElementById ? evt.keyCode : 0;
	if(keyCode==13) {
		selectFromPredictive(field, TRpattern);
		return false;
	}
	return true;
}
function predict(el, action, capa){
	if (!$("#resultPredict").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();
		$("#resultPredict").show("fast");
	}
	
	document.getElementById("resultPredict").style.display="block";
    if(el.value.length>0){
        var q = el.value;
        carga_url('ajax.php?ac='+action+'&q='+escape(q),capa);
    }
}
function changeColor(color,i, TRpattern){
    document.getElementById(TRpattern+i+"0").style.background="#"+color;
    /*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,fade) {
	if(typeof($)!="undefined"){
	    // if(document.getElementById(capa).style.display=="block"){ document.getElementById(capa).style.display="none"; }
		$(document.getElementById(capa)).html('');
		  $(document.getElementById(capa)).load(URL,function() {
	  		// $(document.getElementById(capa)).fadeIn(1000);
	  		$(document.getElementById(capa)).show();
		  });
    }
	/*
	if(typeof($)!="undefined"){
	    if(fade && document.getElementById(capa).style.display=="block"){ document.getElementById(capa).style.display="none"; }
		$(document.getElementById(capa)).html('');
		  $(document.getElementById(capa)).load(URL,function() {
		  	if (fade) {
		  		// $(document.getElementById(capa)).fadeIn(1000);
		  		$(document.getElementById(capa)).show();
		  	}
		  	else {
				$(document.getElementById(capa)).show();
		  	}
		  });
    }
    */
}
/*
function carga_url (URL,capa) {
	$(document.getElementById(capa)).html('');
	  $(document.getElementById(capa)).load(URL,function(){
		$(document.getElementById(capa)).slideDown("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 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')	;
}


/*************************************************************************
NOTICIAS
*************************************************************************/
function noticiasGestImgs (noticia, accion, id, mod, filter, target, lang) {
	var url="popup.php?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;
	}
	if (lang != "" && lang != null) {
		url += "&lang="+lang;
	}
	popup_w = popUp (url,800,600,'gestnoticias');
}
function noticiasGestDownloads (noticia, accion, id, mod, filter, target, lang) {	
	var url="popup.php?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;
	}
	if (lang != "" && lang != null) {
		url += "&lang="+lang;
	}
	popup_w = popUp (url,800,600,'gestnoticias');
}
function noticiasGestLinks (noticia, accion, id, mod, filter, target, lang) {
	var url="popup.php?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;
	}
	if (lang != "" && lang != null) {
		url += "&lang="+lang;
	}
	popup_w = popUp (url,800,600,'gestnoticias');
}
/**
  * FUNCIONES PARA
  * GESTI�N DE ILUSTRADORES
  * EN GESTOR
  */
function ilustradoresGestIlustraciones (il_ID, accion, mod, target) {
	var url="popup.php?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="popup.php?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");
}
