function llamarServletObtenerDocumento()
{
	var winName='Documento';
	var features = 'menubar=yes scrollbars=yes,resizable=yes,width=710,height=475,status=yes';		 		
	window.open("/FundMM/ServletObtenerDocumento");
	//window.close();
}

function evaluarNumerico(cadena, obj, event)
{ 
    var opc = false;  
    
    if (cadena == "%d") 
     if ((event.keyCode > 47 && event.keyCode < 58) || (event.which > 47 && event.which < 58)) 
      opc = true; 
      
    if (cadena == "%f")
    {  
		 if ((event.keyCode > 47 && event.keyCode < 58) || (event.which > 47 && event.which < 58))
		  opc = true; 
		 if (obj.value.search("[.*]") == -1 && obj.value.length != 0) 
		  if ((event.keyCode == 46) || (event.which == 46))
		   opc = true; 
    } 
    
    if(opc == false) 
     event.returnValue = false;  
}

function posicionCursor(obj)
{
    var tb = obj;
    var cursor = -1;
    
    // IE
    if (document.selection && (document.selection != 'undefined'))
    {
        var _range = document.selection.createRange();
        var contador = 0;
        while (_range.move('character', -1))
            contador++;
        cursor = contador;
    }
   // FF
    else if (tb.selectionStart >= 0)
        cursor = tb.selectionStart;

   return cursor;
}

function evaluarSoloNumLetras(obj, event)
	{ 
    	var opc = false;  
    
     	if ((event.keyCode > 64 && event.keyCode < 91) || (event.which > 64 && event.which < 91))
      		opc = true; 
     
     	if ((event.keyCode > 96 && event.keyCode < 123) || (event.which > 96 && event.which < 123))
      		opc = true; 
      		
    	if ((event.keyCode > 47 && event.keyCode < 58) || (event.which > 47 && event.which < 58))
      		opc = true; 
    
   		 if(opc == false) 
    		event.returnValue = false;  
   }
   function calcularPorcentaje(ano,campoImporteConcedido,maxDecimales,campoPorcentaje,campoTotalCantidad)
{
 	//var sImporteConcedido = document.getElementById('<%=Constantes.importeConcedido%>').value;
 	var sImporteConcedido = document.getElementById(campoImporteConcedido).value;
 	
 	//convertimos el formato visual de la caja de texto con el importe, a formato float para poder hacer el parseFloat
 		sImporteConcedido = invertirFormatoDecimales(sImporteConcedido,'.',',');
 		while(sImporteConcedido.indexOf(',') != -1)		
 			sImporteConcedido = sImporteConcedido.replace(',', '');
 	
 	//alert(sImporteConcedido);
 	var iImporteConcedido = parseFloat(sImporteConcedido);
 	//var sPortentaje = document.getElementById('<%=Constantes.porcentaje%>'+ano).value;		
 	var sPortentaje = document.getElementById(campoPorcentaje+ano).value;
 	var iPortentaje = parseFloat(sPortentaje);
 	
 	var importeTotal = (iImporteConcedido * iPortentaje) / 100;
 	//alert(importeTotal);
 	
 	if(""+importeTotal != "NaN")
 	{
 		//volvemos a convertir al formato visual
 		importeTotal = invertirFormatoDecimales(importeTotal,'.',',');
 		//importeTotal = NumberFormat(importeTotal, '<%=Constantes.MAXIMO_DECIMALES%>', '.', ',');
 		importeTotal = NumberFormat(importeTotal, maxDecimales, '.', ',');
 		//alert(importeTotal);
 		
 		document.getElementById(campoTotalCantidad+ano+"_visual").value = importeTotal;
 		document.getElementById(campoTotalCantidad+ano).value = importeTotal;		
 		//document.getElementById('<%=Constantes.totalCantidad%>'+ano+"_visual").value = importeTotal;
 		//document.getElementById('<%=Constantes.totalCantidad%>'+ano).value = importeTotal;		
 	}
}

function validarPorcentaje(obj,totalAnos,idCampo,campoPorcenaje)
{
	var iTotalPorcentaje = 0;
	var sTotalPorcentaje;
	var opc = false; 
	var caracterTecla = String.fromCharCode(event.keyCode);
 	
 	var posCursor = posicionCursor(obj);
 	
 	var sPortentaje;
 	if(posCursor != 0)
 		sPortentaje=obj.value+caracterTecla;		
 	else
 		sPortentaje=caracterTecla+obj.value;	
 		
 	var iPortentaje = parseInt(sPortentaje);	
 	//alert(posCursor);
	 
    if (iPortentaje <= 100 || ""+iPortentaje == "NaN") 	opc = true;
       
    //alert("totalAnos: "+totalAnos+", idCampo: "+idCampo);   
    for(var i=1; i<=totalAnos; i++)
    {
    	//sTotalPorcentaje = document.getElementById('<%=Constantes.porcentaje%>'+i).value;
    	sTotalPorcentaje = document.getElementById(campoPorcenaje+i).value;
    	
    	//alert(sTotalPorcentaje);
    	if(sTotalPorcentaje != "")
    	{
    		var numero = parseInt(sTotalPorcentaje);
			if(i == idCampo)
				iTotalPorcentaje = iTotalPorcentaje + iPortentaje;	
			else    	
    			iTotalPorcentaje = iTotalPorcentaje + numero;	
    	}
    }
    
    //alert(iTotalPorcentaje);
    
    if (iTotalPorcentaje > 100)	opc = false;
    
    //obj.value = parseInt(obj.value);

    if(opc == false) 
     	event.returnValue = false;
    //else
    //{
    //	alert(obj.value);
    	//obj.value = sPortentaje;
    //}
}

 function maxLengthTextarea(obj,longitudMax)
   {
   		var opc = false;
   		
   		if(obj.value.length < longitudMax)	
   			opc = true;
   		
   		if(opc == false) 
    		event.returnValue = false;  
   }
   
 function formatearDecimalesBBDDtoJS(numDecimal,maximoDecimales)
 {
 	numDecimal = invertirFormatoDecimales(numDecimal,'.',',');
	numDecimal = NumberFormat(numDecimal, maximoDecimales, '.', ',');
	return numDecimal;
 }
 
 function invertirFormatoDecimales(num,separadorMiles,separadorDecimales)
{
	var numero = num.toString();
	
	while(numero.indexOf(separadorMiles) != -1)
		numero = numero.replace(separadorMiles, 'x');
	
	while(numero.indexOf(separadorDecimales) != -1)
		numero = numero.replace(separadorDecimales, separadorMiles);
	
	while(numero.indexOf('x') != -1)
		numero = numero.replace('x', separadorDecimales);
	
	return numero;
}

function evaluarNumericoDecimal(event)
{ 
    var opc = false;  
    
    
     if ((event.keyCode > 47 && event.keyCode < 58) || (event.which > 47 && event.which < 58))
      opc = true; 
     
    //coma
    if (event.keyCode == 44 || event.which == 44) 
      opc = true;
      
    
    
    if(opc == false) 
     event.returnValue = false;  
}


function NumberFormat(num, numDec, decSep, thousandSep)
{ 
	num = invertirFormatoDecimales(num,'.',','); 
	
    var arg; 
    var Dec; 
    Dec = Math.pow(10, numDec);  
    if (typeof(num) == 'undefined') return;  
    if (typeof(decSep) == 'undefined') decSep = '.'; 
    if (typeof(thousandSep) == 'undefined') thousandSep = ','; 
    
    if (thousandSep == '.') 	 arg=/./g; 
    else if (thousandSep == ',') arg=/,/g; 
    
    if (typeof(arg) != 'undefined') num = num.toString().replace(arg,''); 
    
    num = num.toString().replace(/,/g, '.');  
    if (isNaN(num)) num = "0"; 
    sign = (num == (num = Math.abs(num))); 
    num = Math.floor(num * Dec + 0.50000000001); 
    cents = num % Dec; 
    num = Math.floor(num/Dec).toString();  
    if (cents < (Dec / 10)) cents = "0" + cents;  
    
    for (var i = 0; i < Math.floor((num.length - (1 + i)) / 3); i++) 
     num = num.substring(0, num.length - (4 * i + 3)) + thousandSep + num.substring(num.length - (4 * i + 3)); 
     
    //if (Dec == 1) 
    // return (((sign)? '': '-') + num); 
    //else 
    //return (((sign)? '': '-') + num + decSep + cents); 
    
    if (Dec == 1) 
     return invertirFormatoDecimales((((sign)? '': '-') + num),',','.'); 
    else 
     return invertirFormatoDecimales((((sign)? '': '-') + num + decSep + cents),',','.'); 
}
function reemplazarXblanco(cad,controlName){

	var cadena;
	cadena = cad.replace("-","");
	document.getElementById(controlName).value = cadena.toUpperCase();

}




function validaNIF(txtCodId)
{
	var F = txtCodId.value;
	var result = false;
	var len = F.length;

  	if (len==10 && F.substr(0,1)=='0') F = F.substr(1,10);
	if (F.substr(0,1).toUpperCase()!='X') 
	{
		result = validaId(txtCodId,"NIF");
		len = F.length;
		if (len==10) F = F.substr(1,10);
		
	}	
	F=txtCodId.value;
	len=F.length;
	if (len==10 && F.substr(0,1)=='0'){
		F = F.substr(1,10);
		txtCodId.value=F;
	} 

	return result;
}

function validaId(txtCodId,tipo){
	if(txtCodId.value == ""){
		return false;
	}else {
	  	if (txtCodId.value.length==10){
			if(txtCodId.value.substring(0,1)=="0"){
				txtCodId.value=txtCodId.value.substring(1);
			}else{
				return false;
			}
		}				
		 var re="";
		 var resultado=validarDNI(txtCodId.value.toUpperCase());
		 var resultado2="";
		 
			 if (parseInt(resultado)>=0){ 
					if (parseInt(resultado)==1){					
							resultado2=compruebaLetraDNI2(txtCodId.value.toUpperCase(),tipo);		
					   	if (parseInt(resultado2)=="1"){	
					   		if (txtCodId.value.length<10){
									for(i=0;i<10-txtCodId.value.length;i++){
										re="0"+re;
									}
									txtCodId.value=re+txtCodId.value.toUpperCase();		
								}
							}	
							else if(parseInt(resultado2)=="0"){	
				   			txtCodId.value = NIF;
								if (txtCodId.value.length<10){
									for(i=0;i<10-txtCodId.value.length;i++){
										re="0"+re;
									}
									txtCodId.value=re+txtCodId.value.toUpperCase();		
								}
							}						
							else if(parseInt(resultado2)=="2"){	
				   			txtCodId.value = NIF;
								if (txtCodId.value.length<10){
									for(i=0;i<10-txtCodId.value.length;i++){
										re="0"+re;
									}
									txtCodId.value=re+txtCodId.value.toUpperCase();		
								}
							}else if(parseInt(resultado2) ==-1){
											return false;}						
					}else if (parseInt(resultado)==2){													
							if(validaCIF(txtCodId)==-55) return false;if (txtCodId.value.length<10){
								//Componemos el CIF correctamente para poder enviarlo al host de vida.
								var cadena =  txtCodId.value.toUpperCase();
								var cadena2 = cadena.substr(1,cadena.length-1);
								var cadena1="0"+cadena.substr(0,1);
								for(i=0;i<8-cadena2.length;i++){
									cadena2 = "0"+cadena2;
								}
								cadena = cadena1 + cadena2;	
								txtCodId.value=cadena;							
							}	
				}else if(parseInt(resultado)==9){
				
				var cadena = txtCodId.value.toUpperCase();
				cadena = cadena.substr(1,9);
				resultado2=compruebaLetraDNI2(cadena,tipo);		
					   	if (parseInt(resultado2)=="1"){	
					   		if (txtCodId.value.length<10){
									for(i=0;i<10-txtCodId.value.length;i++){
										re="0"+re;
									}
									txtCodId.value=re+txtCodId.value.toUpperCase();		
								}
							}	
							else if(parseInt(resultado2)=="0"){
				   			//txtCodId.value = NIF;
								if (txtCodId.value.length<10){
									for(i=0;i<10-txtCodId.value.length;i++){
										re="0"+re;
									}
									txtCodId.value=re+txtCodId.value.toUpperCase();
								}
							}						
							else if(parseInt(resultado2)=="2"){	
				   			txtCodId.value = NIF;
								if (txtCodId.value.length<10){
									for(i=0;i<10-txtCodId.value.length;i++){
										re="0"+re;
									}
									txtCodId.value=re+txtCodId.value.toUpperCase();		
								}
							}else if(parseInt(resultado2) == "-1"){
							txtCodId.focus();	
							return false;}																							
				}else{
							return false;}						
			}else{	
				return false;}				
		}
		return true;
	}
	
	/*****************************************************/
/*****        FUNCION VALIDAR DNI               ******/
/*****************************************************/

function validarDNI(theFormtxtNombre)
{

	var Numeros = '0123456789';
	var Letras = 'ABCDEFGHIJKLMNÑOPQRSTUVWXYZabcdefghijklmnñopqrstuvwxyz';
	var Letras_Posibles = 'TRWAGMYFPDXBNJZSQVHLCKE';
	var Alfabeticos = 'ABCDEFGHKLMNPQSXabcdefghklmnpqsx';
	var Alfajur ='ABEFGHSabefghs';
	var Alfafis ='KLMXklmx';
	var Alfaposicion = 'ABDCEFGHNPQSabcdefghnpqs';
	var digito ="0";
	var cuarta ="3";

  //Obligatorio al menos un caracter

  	if (theFormtxtNombre.length < 1)
		return (-1);
    //Máximo nueve caracteres
	if (theFormtxtNombre.length < 2)
		return (-1);

  	if (theFormtxtNombre.length > 10)
		return (-2);

 	//Primer digito de los 10 siempre 0
	if ( ( theFormtxtNombre.charAt(0) != '0' ) && ( theFormtxtNombre.length == 10) )
	return(-3);


//Segundor caracter numérico o letra


	var cadena = theFormtxtNombre.toString();
	var digito1="0";
	var ch1 = cadena.charAt(0);
  
    		for (j = 0;  j < Alfabeticos.length;  j++)
      		{
			if (ch1 == Alfabeticos.charAt(j))
        			break;//es una letra
      		} 
		if (j==Alfabeticos.length){ 
			for (k = 0; k < Numeros.length; k++)
			{
      				if (ch1 == Numeros.charAt(k))
        				break;		//es un número
			}
			if (k == Numeros.length){
				return (-4);
			}else{
				digito1="11";
			}
		}
		else{
				digito1="10";
		}
		var re="";
		if ( digito1=="10" ) {
		   	if (cadena.length<10){
					for(i=0;i<10-cadena.length;i++){
						re="0"+re;
					}
					cadena=re+cadena;				
				}
		}

  		var ch = cadena.charAt(1);

    		for (j = 0;  j < Alfabeticos.length;  j++)
      		{
			if (ch == Alfabeticos.charAt(j))
        			break;
      		} 
		if (j==Alfabeticos.length){ 
			for (k = 0; k < Numeros.length; k++)
			{
      				if (ch == Numeros.charAt(k))
        				break;		
			}
			if (k == Numeros.length){
				return (-4);
			}else{
				digito="11";
			}
		}else{
			// miramos si el digito es A,B,S,E,F,G,H lo cual nos indicaria
			// que en la 10ª posicion habra un numero.
			for (c = 0; c < Alfajur.length; c++){
				if (ch==Alfajur.charAt(c)){
					digito="10";
					break;
				}
			}
			for (d = 0; d < Alfafis.length; d++){
				if (ch==Alfafis.charAt(d)){
					digito="99";
					break;
				}
			}
			if( ( c==Alfajur.length ) && (d ==  Alfafis.length) ) digito="14";
			for (e = 0; e < Alfaposicion.length; e++){
				if (ch==Alfaposicion.charAt(e)){
				   if ( (cadena.charAt(2)==8)||(cadena.charAt(2)==6) ){
						cuarta="1";
						break;
					}
					if (cadena.charAt(2)==9){
						cuarta="0";
						break;
					}
				}
			}
		}
  //Todos números excepto el último que es número o letra
	//var cadena = theFormtxtNombre.toString();
	
	var error = "0";
  	for (i=2; i< cadena.length; i++)
  	{
  		ch = cadena.charAt(i);
    		if (i < cadena.length-1)
    		{
	      		for (j = 0;  j < Numeros.length;  j++)
        			{
        				if (ch == Numeros.charAt(j)){
					if ( (i==3) && (cuarta=="0") && (ch=="0") ){
							error="1";
							break;
					}
					if ( (i==3) && (cuarta=="1") && ( (ch=="5") || (ch=="6") || (ch=="7") || (ch=="8") || (ch=="9") ) ){
							error="1";
							break;
					}
					break;					
				}
		     	}  
	
    			if (j == Numeros.length){
          				return (-5);
			}else{
				if (error=="1"){
					return(-6);
				}
			}
  		}  
   		else
    		{
      			for (j = 0; j < Letras.length; j++)
			{
        				if (ch == Letras.charAt(j))
          					break;
	        		}
     
      			if (j != Letras.length)
	//Si ha llegado hasta aquí, el último caracter es una letra del conjunto 'Letras'//
			{
				if (digito=="11"){
	         			return (1);
				}else if (digito=="99"){
	         			return (9);
				}else if (digito=="14"){
	         			return (2);
				//}else if (digito=="10"){
	         	//		return (2);
				}else{	return (-10);}
			}

      			else
	        		{
          				for (n = 0;  n < Numeros.length;  n++)
            				{
            					if (ch == Numeros.charAt(n))
               					{              
                					if (cadena.length < 9)
						//Si ha llegado hasta aquí, el último caracter es un número y hay menos de 9 caracteres//
		  				{
		  					return (-1);
		  				}
						else
							//Si ha llegado hasta aquí hay diez dígitos//
		  				{
							if (digito=="10"){
								return(2);
							}else{
			  					return (-5);
							}
		  				}

 	       				}
	            			}  

	          			if (n == Numeros.length)
					//Si ha llegado hasta aquí el último no es ni número ni letra
              					return (-4);
		        	}
    		}    
  	}  
	return(0);

}//End validarDNI 
// FUNCIÓN QUE COMPRUEBA LA LETRA DEL DNI (BIS)

function compruebaLetraDNI2(nif,tipo){
	
	//var nif = theForm.txtNIF.value.toUpperCase(); CAMBIO RESPECTO A VALIDARLETRADNI
	nif=nif.toUpperCase();
	//var nif = theForm.value.toUpperCase();
	//var nif = theForm.value;
	var Letras_Posibles = 'TRWAGMYFPDXBNJZSQVHLCKE';
  //Caso en que se ha introducido una letra: se comprueba si es correcta
  	if (compruebaFormatoDNI(nif) == 0)
  	{
  //separo la letra del nif 
    		letra_documento = nif.charAt(nif.length-1);

  //separo el número del nif
    		numero_documento = '';
  
      		for (i=0; i < nif.length-1; i++)
      		{
        		numero_documento = numero_documento + nif.charAt(i);
      		}
  //calculo la letra que le correspondería a ese número
    		pos_corr = (numero_documento%23);
    		letra_correcta = Letras_Posibles.charAt(pos_corr);

  //compruebo que sean iguales
      		if (letra_documento != letra_correcta)
        	{       
        		NIF = numero_documento + letra_correcta;        
			return (-1);    
        	}
      		else
        		return (1);
  	}//end if
  	
  //Caso en que todo sean números: Calculo la letra correspondiente a ese número

  	if (compruebaFormatoDNI(nif) == 1)
  	{
  		var pos = (nif%23);
  		letra = Letras_Posibles.charAt(pos);
  		NIF = nif.toString() + letra;
  		return (-1);
  	}  

}//END compruebaLetraDNI2

//FUNCION COMPROBACION FORMATO DNI

function compruebaFormatoDNI(dni)
{

	var Numeros = '0123456789';
	var Letras = 'ABCDEFGHIJKLMNÑOPQRSTUVWXYZabcdefghijklmnñopqrstuvwxyz';
	var Letras_Posibles = 'TRWAGMYFPDXBNJZSQVHLCKE';

  //Obligatorio al menos un caracter

  	if (dni.length < 1)
		return (-1);
  
  //Primer caracter numérico

	var cadena = dni.toString();

  	for (i = 0; i < cadena.length; i++)
  	{
  		ch = cadena.charAt(0);

    		for (j = 0;  j < 10;  j++)
      		{
      			if (ch == Numeros.charAt(j))
        			break;
      		}  

    		if (j == 10)
      			return (-2);
  	}    

  //Máximo nueve caracteres

  	if (dni.length > 9)
		return (-3);

  //Todos números excepto el último que es número o letra

  	for (i=0; i< cadena.length; i++)
  	{
  		ch = cadena.charAt(i);
  
    		if (i != cadena.length-1)
    		{
      			for (j = 0;  j < Numeros.length;  j++)
        		{
        			if (ch == Numeros.charAt(j))
          				break;
        		}  

      			if (j == Numeros.length)
          			return (-4);
    		}  
    		else
    		{
      			for (j = 0; j < Letras.length; j++)
			{
        			if (ch == Letras.charAt(j))
          				break;
        		}
      
      			if (j != Letras.length)
	//Si ha llegado hasta aquí, el último caracter es una letra del conjunto 'Letras'//
			{
         			return (0);
			}

      			else
        		{
          			for (n = 0;  n < Numeros.length;  n++)
            			{
            				if (ch == Numeros.charAt(n))
               				{              
                				if (cadena.length < 9)
	//Si ha llegado hasta aquí, el último caracter es un número y hay menos de 9 caracteres//
		  				{
		  					return (1);
		  				}
						else
	//Si ha llegado hasta aquí hay nueve dígitos//
		  				{
		  					return (-5);
		  				}

 	       				}
            			}  

          			if (n == Numeros.length)
	//Si ha llegado hasta aquí el último no es ni número ni letra
              				return (-4);
        		}
    		}    
  	}  
	return(0);

}//End compruebaFormatoDNI 
function validaNIE(txtCodId){
	var temp=txtCodId.value;	
 	temp=temp.toUpperCase();
	if (temp!==''){
		//si no tiene un formato valido devuelve error
		if (!/^[XYZ]{1}[0-9]{7}[A-Z]{1}$/.test(temp))	{
			return false;
		}
		var cadenadni="TRWAGMYFPDXBNJZSQVHLCKE";
	 
		var ch = "0";
		switch (temp.charAt(0)) {
				case "Y": ch = "1"; break;
				case "Z": ch = "2"; break;
		}
		temp = temp.replace(/\S/,ch);

 		var posicion = temp.substring(0,8) % 23;

		return (temp.charAt(8) == cadenadni.charAt(posicion));
	}

}
/*function validaNIE(txtCodId)
{
	var F = txtCodId.value;
	var result = false;
	var len = F.length;
  	if (len==10 && F.substr(0,1)=='0') F = F.substr(1,10);
	if (F.substr(0,1).toUpperCase()=='X') // puede ser un NIE
	{
		result = validaId(txtCodId,"NIE"); 
		len = F.length;
		if (len==10) F = F.substr(1,10);
	} 
	return result;
}*/
