    function isNumberEntero(valor) {
     valor = parseInt(valor)

      if (isNaN(valor)) {
            alert("Debe introducir un número entero (sin puntos ni comas decimales)");
            return ""
      }else{
            return valor
      } 
    }

    function validarCIF(texto){     
	  // Copyright © McAnam.com  
	  // http://www.mcanam.com/articulos/JavaScript.php?id=16   
        var pares = 0;
        var impares = 0;
        var suma;
        var ultima;
        var unumero;
        var uletra = new Array("J", "A", "B", "C", "D", "E", "F", "G", "H", "I");
        var xxx;
        
        texto = texto.toUpperCase();
        
        var regular = new RegExp(/^[ABCDEFGHKLMNPQS]\d\d\d\d\d\d\d[0-9,A-J]$/g);
        if (!regular.exec(texto)) return false;
             
         ultima = texto.substr(8,1);

         for (var cont = 1 ; cont < 7 ; cont ++){
             xxx = (2 * parseInt(texto.substr(cont++,1))).toString() + "0";
             impares += parseInt(xxx.substr(0,1)) + parseInt(xxx.substr(1,1));
             pares += parseInt(texto.substr(cont,1));
         }
         xxx = (2 * parseInt(texto.substr(cont,1))).toString() + "0";
         impares += parseInt(xxx.substr(0,1)) + parseInt(xxx.substr(1,1));
         
         suma = (pares + impares).toString();
         unumero = parseInt(suma.substr(suma.length - 1, 1));
         unumero = (10 - unumero).toString();
         if(unumero == 10) unumero = 0;
         
         if ((ultima == unumero) || (ultima == uletra[unumero]))
             return true;
         else
             return false;

    } 

 	function validarNIF(dni) {
		dni = dni.toUpperCase();
		if (dni.charAt(0)== "X") return true;
		var esDNI = /^\d{8}\w$/.test(dni);
		var esCIF;
		if (!esDNI) {
   			return false;
	  	} else {
			var i = dni.length - 1;
			var letra = dni.charAt(i);
			var numero = parseInt(dni.substr(0, i))%23;
	  		var letras = 'TRWAGMYFPDXBNJZSQVHLCKE';
			if (letra != letras.charAt(numero)) {
				return false;
			}		
		}
   		return true;
 	}

	function validarNIFCIF(texto) {
		if(validarNIF(texto) || validarCIF(texto)) {
			return true;
		}
		alert("¡No es un NIF/CIF/NIE válido!");
		return false;
	}
        
        function validarMAIL(email) {
		var mail = new RegExp("^\\w+(\\.\\w+)*@\\w(\\w*\\.\\w+)+$", "i");
		var resultado = email.search(mail);
                
		if(resultado >= 0) 
			return true;
		else
			alert("Debe introducir un email válido");
		return false;
	}

function getURL(uri) {
    uri.dir = location.href.substring(0, location.href.lastIndexOf('\/'));
    uri.dom = uri.dir; if (uri.dom.substr(0,7) == 'http:\/\/') uri.dom = uri.dom.substr(7);
    uri.path = ''; var pos = uri.dom.indexOf('\/'); if (pos > -1) {uri.path = uri.dom.substr(pos+1); uri.dom = uri.dom.substr(0,pos);}
    uri.page = location.href.substring(uri.dir.length+1, location.href.length+1);
    pos = uri.page.indexOf('?');if (pos > -1) {uri.page = uri.page.substring(0, pos);}
    pos = uri.page.indexOf('#');if (pos > -1) {uri.page = uri.page.substring(0, pos);}
    uri.ext = ''; pos = uri.page.indexOf('.');if (pos > -1) {uri.ext =uri.page.substring(pos+1); uri.page = uri.page.substr(0,pos);}
    uri.file = uri.page;
    if (uri.ext != '') uri.file += '.' + uri.ext;
    if (uri.file == '') uri.page = 'index';
    uri.args = location.search.substr(1).split("?");
    return uri;
}

function getURLAplication() {
    var uri = new Object();
    var aplication;
    uri = getURL(uri);
    pos = uri.path.indexOf('/');
    if(pos > -1) aplication=getURL(uri).path.substring(0,pos);
    urlAplication = "http://" + getURL(uri).dom + "/" + aplication;
    return urlAplication;
}

function getURLParam(strParamName){
  var strReturn = "";
  var strHref = window.location.href;
  if ( strHref.indexOf("?") > -1 ){
    var strQueryString = strHref.substr(strHref.indexOf("?")).toLowerCase();
    var aQueryString = strQueryString.split("&");
    for ( var iParam = 0; iParam < aQueryString.length; iParam++ ){
      if (
aQueryString[iParam].indexOf(strParamName.toLowerCase() + "=") > -1 ){
        var aParam = aQueryString[iParam].split("=");
        strReturn = aParam[1];
        break;
      }
    }
  }
  return unescape(strReturn);
}


// Ajax
var xmlHttp;// global instance of XMLHttpRequest
     function createXmlHttpRequest()
     {
         if(window.ActiveXObject)
         {
             xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
         }
         else if(window.XMLHttpRequest)
         {
             xmlHttp=new XMLHttpRequest();
         }
      }   
      
                function muestraPagina(pagina)
                {
                    var contenido = document.getElementsByName("contenido");                    
                    var html =  "<div id='tituloPagina'>" +
                                "<img src='imagenes/iconos/ajax-loader2.gif' alt='logo' />&nbsp;Cargando ..." +
                                "</div>";
                    document.getElementById("contenido").innerHTML=html;
                    
                                        
                    createXmlHttpRequest();
                   
                    var sel=document.getElementById('contenido').selectedIndex;
                    // document.getElementById("oficios").disabled = 'disabled';                    
                    
                    xmlHttp.open("GET", getURLAplication() + "/" + "paginas?pagina=" + pagina, true);
                    //xmlHttp.open("GET","http://localhost:8080/SCM/checkAvail?user="+u1 ,true)
                    xmlHttp.onreadystatechange=handleStateChange;
//                    setTimeout("xmlHttp.send(null)",4000);
                    xmlHttp.send(null);
                }                

                function muestraAsociacion(asociacion)
                {
                    var contenido = document.getElementsByName("contenido");
                    var html =  "<div id='tituloPagina'>" +
                                "<img src='imagenes/iconos/ajax-loader2.gif' alt='logo' />&nbsp;Cargando ..." +
                                "</div>";
                    document.getElementById("contenido").innerHTML=html;
                    
                                        
                    createXmlHttpRequest();
                   
                    var sel=document.getElementById('contenido').selectedIndex;                   
                    xmlHttp.open("GET", getURLAplication() + "/" + "paginas?asociacion=" + asociacion, true);
                    xmlHttp.onreadystatechange=handleStateChange;
                    xmlHttp.send(null);
                }  

                function handleStateChange()
                {
                    if(xmlHttp.readyState==4)
                    {
                        if(xmlHttp.status==200)
                        {                           
                            var message =  xmlHttp.responseText;
                            document.getElementById("contenido").innerHTML=message;
                            
                            //alert(message);
                            //document.getElementById("oficios").disabled = '';                           
                    }
                    else
                    {
                        alert("Error loading pagen"+ xmlHttp.status +
                                ":"+xmlHttp.statusText);
                    }
                }
            }        
            
            
            function muestraPublicacion() {
                var publicaciones = document.getElementsByName("form1:dataTable1:0:portada");
                for(i = 0; i < publicaciones.length; i++) {
                    alert(publicaciones[i].src);
                    publicaciones[i].src = publicaciones[i].src;
                }
            }
            
                function leeIcono(elemento) {
                    var i = elemento.selectedIndex;
                    var htmlCode = '<img src=\"' + elemento[i].text + '\" alt=\"logo\" height=\"48\" width=\"48\"/>';
                    document.getElementById("icono").innerHTML=htmlCode;
                }            
                

function imprime() {
	attr = 'resizable=no,scrollbars=yes,width=' + 1050 + ',height=' +
	800+ ',screenX=20,screenY=20,left=' + 20 + ',top=' +
	20 + '';
        texto = document.getElementById("contenido").innerHTML;
	popWin=open('', 'new_window', attr);
        popWin.document.body.innerHTML = '';    
        popWin.document.write(texto);
        
        var fileref=document.createElement("link");
        fileref.setAttribute("rel", "stylesheet");
        fileref.setAttribute("type", "text/css");
        fileref.setAttribute("href", "css/fremm.css");
        popWin.document.getElementsByTagName("head")[0].appendChild(fileref);       
        return false;
}                

function limpia (texto) {
    return texto.replace(/\n/g, "").replace(/\t/g, "");
}


function popuponclickContactar(asunto)
{
    var width = 645;
    var height = 470;

    var centerWidth = (window.screen.width - width) / 2;
    var centerHeight = (window.screen.height - height) / 2;
    
     my_window = window.open("http://192.168.0.43:8995/portal/faces/contactar.jsp?asunto=" + asunto,
        "contact","resizable=1,status=1,width=" + width + ",height=" + height + ",left=" + centerWidth + 
        ",top=" + centerHeight);
} 

function popuponclickAsociarse()
{
    //var centerWidth = (window.screen.width - width) / 2;
    //var centerHeight = (window.screen.height - height) / 2;
    
     my_window = window.open("http://192.168.0.43:8995/portal/faces/formNuevoSocio.jsp?asunto=Nuevo",
        "contact","resizable=yes,scrollbars=yes,status=1");
} 

// Corrector ortografico.
// ###################################################################################
var texto;
var letras;
var i = 0;
var inicio;
var fin;

function desnormaliza (buffer) {
        buffer = buffer.replace(/&euro;/g, "€");
        buffer = buffer.replace(/&aacute;/g, "á");
        buffer = buffer.replace(/&eacute;/g, "é");
        buffer = buffer.replace(/&iacute;/g, "í");
        buffer = buffer.replace(/&oacute;/g, "ó");
        buffer = buffer.replace(/&uacute;/g, "ú");
        buffer = buffer.replace(/&Aacute;/g, "Á");
        buffer = buffer.replace(/&Eacute;/g, "É");
        buffer = buffer.replace(/&Iacute;/g, "Í");
        buffer = buffer.replace(/&Oacute;/g, "Ó");
        buffer = buffer.replace(/&Uacute;/g, "Ú");
        buffer = buffer.replace(/&nbsp;/g, " ");
        buffer = buffer.replace(/&hellip;/g, "");

        buffer = buffer.replace(/&Uuml;/g, "Ü");
        buffer = buffer.replace(/&uuml;/g, "ü");

        buffer = buffer.replace(/&Ntilde;/g, "Ñ");
        buffer = buffer.replace(/&ntilde;/g, "ñ");

/*	&Agrave;  	À
 	&Egrave; 	È
 	&Igrave; 	Ì
 	&Ograve; 	Ò
 	&Ugrave; 	Ù
 	&agrave; 	à
 	&egrave; 	è
 	&igrave; 	ì
 	&ograve; 	ò
 	&ugrave; 	ù
&Auml; 	Ä 	&Acirc; 	Â
&Euml; 	Ë 	&Ecirc; 	Ê
&Iuml; 	Ï 	&Icirc; 	Î
&Ouml; 	Ö 	&Ocirc; 	Ô
 	&Ucirc; 	Û
&auml; 	ä 	&acirc; 	â
&euml; 	ë 	&ecirc; 	ê
&iuml; 	ï 	&icirc; 	î
&ouml; 	ö 	&ocirc; 	ô
 	&ucirc; 	û
&Atilde; 	Ã 	&aring; 	å
 	&Aring; 	Å
&Otilde; 	Õ 	&Ccedil; 	Ç
&atilde; 	ã 	&ccedil; 	ç
 	&Yacute; 	Ý
&otilde; 	õ 	&yacute; 	ý
&Oslash; 	Ø 	&yuml; 	ÿ
&oslash; 	ø 	&THORN; 	Þ
&ETH; 	Ð 	&thorn; 	þ
&eth; 	ð 	&AElig; 	Æ
&szlig; 	ß 	&aelig; 	æ
*/
    return buffer;
}


function siguientePalabra() {       
    inicio = i;
    fin = i;
    var palabra = "";
    while(true) {
        if (i >= texto.length) break;
/*        if (texto.charAt(i) == ' ') break;
        if (texto.charAt(i) == '.') break;        
        if (texto.charAt(i) == ',') break;        
        if (texto.charAt(i) == '(') break;          
        if (texto.charAt(i) == ')') break;          */
        
        if(texto.charAt(i) == '<') {   
            while(texto.charAt(i) != '>' && i < texto.length) i++;       
            i++;
            inicio = i;
            continue;
        }        
        
        if((texto.charAt(i) < 'a' || texto.charAt(i) > 'z') && (texto.charAt(i) < 'A' || texto.charAt(i) > 'Z') && (
                texto.charAt(i) != 'ñ' && texto.charAt(i) != 'Ñ' && texto.charAt(i) != 'á' && texto.charAt(i) != 'Á'
                && texto.charAt(i) != 'é' && texto.charAt(i) != 'É' && texto.charAt(i) != 'í' && texto.charAt(i) != 'Í'
                && texto.charAt(i) != 'ó' && texto.charAt(i) != 'Ó' && texto.charAt(i) != 'ú' && texto.charAt(i) != 'Ú'
                && texto.charAt(i) != '€' && texto.charAt(i) != 'ü' && texto.charAt(i) != 'Ü'
                )) break;
        if(palabra == "") inicio = i;
        fin = i;
        palabra += texto.charAt(i);  
        i++;        
    }

   
    if(palabra == "" && i < (texto.length + 1)) {
        i++;
        siguientePalabra();        
    }
    else
    if(palabra != "" || i < (texto.length + 1)) {    
        
        //document.getElementById("EditorDefault").value.selectionStart = inicio;
        //document.getElementById("EditorDefault").value.selectionEnd = fin; 
        document.getElementById("correctorMensaje").innerHTML= "<img src='imagenes/iconos/ajax-loader2.gif' alt='logo' /> Analizando ...";    
        document.getElementById("cambioUsuario").value = palabra;

        createXmlHttpRequest();
                   
        var sel=document.getElementById('contenido').selectedIndex;                   
        xmlHttp.open("GET", getURLAplication() + "/" + "correctorortografico?palabra=" + palabra, true);
        xmlHttp.onreadystatechange=manejadorCorrectorStateChange;
        xmlHttp.send(null);        
    } else {
        document.getElementById("correctorMensaje").innerHTML= "<span style='font-size: medium'>Fin del análisis</span>";    
        document.getElementById("cambioUsuario").value = "";
        i = 0;
    } 
}

function manejadorCorrectorStateChange() {
    if(xmlHttp.readyState==4) {
        if(xmlHttp.status==200) {                           
            var message =  xmlHttp.responseXML;
            var correcto = message.getElementsByTagName("Correcto")[0].firstChild.nodeValue
            var palabra = message.getElementsByTagName("Palabra");
            var lista = document.getElementById("opcionesCorrector"); 
            lista.options.length=0;

            if(limpia(correcto) == "false") {
                        document.getElementById("correctorMensaje").innerHTML= "No se encontró la palabra <b><i><span style='font-size: medium'>" + palabra[0].firstChild.nodeValue + "</span></i></b>, le sugerimos:";    
                        var sugerencias = message.getElementsByTagName("Sugerencia");                        
                        
                        for(j = 0; j < sugerencias.length; j++) {
                                var opcion = limpia(sugerencias[j].firstChild.nodeValue);
                                lista.options[j] = new Option(opcion, opcion, false, false);
                        }                       
                        i++;
            } else {
                i++;                                                
                siguientePalabra();
            }
        } else {
            alert("Error loading pagen"+ xmlHttp.status +
                                ":"+xmlHttp.statusText);
        }
    }
}  


function iniciaCorrecccion() {
    alert(document.getElementById("EditorDefault").value);
    corregir();
}


function corregir(){    
      var oEditor = FCKeditorAPI.GetInstance('EditorDefault') ;

      var capa = document.getElementById('correctorOrtografico');
      document.getElementById('correctorOrtografico').style.display="block"; 

//      if (i == 0) {
//        var pattern = /<[^>]+>/g;  
        // texto = document.getElementById("EditorDefault").value;       
        texto = desnormaliza(oEditor.GetData());
        i=0;
//        texto = texto.replace(pattern, "");   
//        letras = texto.split(" ");
//        i = 0;    
//      }
      siguientePalabra();      
}
    
function cerrarCorreccion() {
      document.getElementById('correctorOrtografico').style.display="none";
      i = 0;     
      return true;
}

function ponTextoCorrector(selectobj) {
    if(selectobj.selectedIndex >= 0 && selectobj.selectedIndex < selectobj.size) {
        document.getElementById("cambioUsuario").value = selectobj.options[selectobj.selectedIndex].text;
    }
}

function cambia() {
    var cambia = document.getElementById("cambioUsuario").value;
   
    var textoInicio = texto.substr(0,inicio);
    var textoFin = texto.substr(fin+1);
    texto = textoInicio + cambia + textoFin;    

    var oEditor = FCKeditorAPI.GetInstance('EditorDefault') ;
    oEditor.SetData(texto);

    i = inicio;   
    siguientePalabra();
}


