function clickButton(boton)
  {
  document.getElementById(boton).click();
  }

function cargar(enlace)
{
	document.location.href=enlace;
}

function ejecutar(objetivo,formulario)
{	
		
	formulario.submit();	
}

function enviar(a,b) 
{
		b.action = a;
		b.target="_self";
		b.submit();
}

function ejecutar_tarea(tarea, formulario, clid) 
{
		var doSubmit = false;
		var doPrivileged = false;
		var browser = getBrowser();	
		
		if (tarea=="guardar")
		{	
			doSubmit = firmarPagina(document.getElementById("pagina_firmada"),document.getElementById("pagina_firmada_encoded64"));
				
		}
		else if(tarea=="upload")
		{	
			window.document.getElementById(clid+':uploadFileReclFirma').value = ''; 
		
			//Según navegador
			if(browser!= false){
			
				if(browser == "M"){
				//Mozilla Firefox
					try{
					netscape.security.PrivilegeManager.enablePrivilege('UniversalFileRead');
					doPrivileged=true;				
					}
					catch(err){
						alert('No se ha podido firmar y subir el fichero.\r Revise la configuración de su navegador y/o permita el acceso');
						doPrivileged=false;	
					}
			 	}
			 	else{
			 	//Browser es IExplorer
			 	doPrivileged=true;
			 	}
			}
			if(doPrivileged!=false){
					doSubmit = asig_firm_arch_campo(document.getElementById(clid+":uploadfileRecl").value,clid);					
			}
		}		
		else
		{
			doSubmit=true;
		}
		
		
		
		if(browser!="FALSE"){
			document.getElementById("browser").value=browser;
			
			if(doSubmit == true){
				
				document.getElementById("tarea").value=tarea;
				if(tarea=="imprimir")
				{
					formulario.target="_blank";
				}
				else
				{	
								
					formulario.target="_self";
				}
				
				formulario.submit();			
			}			
		}
	
		return doSubmit;

}

function sobreponer(elemento1,elemento2)
{
		
		document.getElementById(elemento1).style.display='visible';
		document.getElementById(elemento2).style.display='block';
		
}

function nuevof() 
{
			
		var tabla=document.getElementById("tabla"); 
				
		var cont=1;
		
		
		while(document.getElementById("file"+cont)!=null)
		{
			cont++;
			
		}
		
		
		tr = document.createElement("tr"); 
		var td1=document.createElement("td");
		var td2=document.createElement("td");  
		var td3=document.createElement("td"); 
		var textbox1=document.createElement("input"); 
		var textbox2=document.createElement("input"); 
		var filebox1=document.createElement("input");
		var filehidden=document.createElement("input");
		
		textbox1.setAttribute("type","text"); 
		textbox2.setAttribute("type","text");
		filebox1.setAttribute("type","file");
		filehidden.setAttribute("type","hidden");
		
		textbox1.setAttribute("readOnly","readonly"); 
		textbox2.setAttribute("readOnly","readonly");
						
		textbox1.style.width='100%';
		textbox1.style.fontFamily='Arial';
		textbox1.style.fontSize='8pt';
		textbox1.style.fontWeight='normal';
		
		textbox2.style.width='100%';
		textbox2.style.fontFamily='Arial';
		textbox2.style.fontSize='8pt';
		textbox2.style.fontWeight='normal';
		
		filebox1.style.width='100%';
		filebox1.style.fontFamily='Arial';
		filebox1.style.fontSize='8pt';
		filebox1.style.fontWeight='normal';
		
		
		textbox1.setAttribute("name","nfile"+cont); 
		textbox2.setAttribute("name","tfile"+cont);
		filebox1.setAttribute("name","file"+cont);
		
		textbox1.setAttribute("id","nfile"+cont); 
		textbox2.setAttribute("id","tfile"+cont);
		filebox1.setAttribute("id","file"+cont);
		filehidden.setAttribute("id","file"+cont+"_hidden");
	
			
		filebox1.onchange=function()
		{
			cambiofich(filebox1);
		};
		
		
		td1.appendChild(textbox1); 
		td2.appendChild(textbox2); 
		td3.appendChild(filebox1);
		td3.appendChild(filehidden); 
		tr.appendChild(td1); 
		tr.appendChild(td2);
		tr.appendChild(td3);
		
		tabla.appendChild(tr);

		
		if (cont==5)
		{
			document.getElementById("cmdAdjuntar").onclick=function()
			{
				alert('No puede adjuntar más de 5 ficheros');
			};
		}
		
		
}

function cambiofich(fich)
{
		var punto=0;
		var barra=0;
		for (i = fich.value.length-1; i >-1 ; i--) 
		{
			ch = fich.value.charAt(i);
			if(ch=='.')
			{
				punto=i;
				break;
			}
			
		}
		
		for (i = fich.value.length-1; i >-1 ; i--) 
		{
			ch = fich.value.charAt(i);
			if(ch=='\\' || ch=='/')
			{
				barra=i;
				break;
			}
			
		}
		
		document.getElementById("n"+fich.id).value=fich.value.substr(barra,(punto-barra));
		document.getElementById("t"+fich.id).value=fich.value.substr(punto,fich.value.length-1);
}

function ltrim(s) {     
	return s.replace(/^\s+/, ""); 
}  
function rtrim(s) {
     return s.replace(/\s+$/, ""); 
}  
function trim(s) {
     return rtrim(ltrim(s)); 
}

function firmar_js(texto_a_firmar) {
	//alert("firmar texto JS");
	var firmado = window.crypto.signText(texto_a_firmar, "ask");
	if (firmado.substring(0,5) =="error") {
		alert("Su navegador no ha generado una firma valida");
		return false;
	}
	else if (firmado =="no generada") {
		alert("No ha generado la firma.");
		return false;
	}
	else {
		return firmado ;
		alert("Firma generada correctamente. Pulse enviar para comprobarlos en servidor.");
	}
	//alert("TEXTO FIRMADO="+firmado);
} 

function asig_firm_arch_campo(url, clid){
    //alert("URL ARCHIVO->"+url);
  	if(url==null || url==""){
  	    alert("¡No se ha especificado un path!");
  		return false;
  	}
    var firma = firmar_archivo(url,clid);
    if(firma==false || firma==null || firma==""){
    	return false;
    }
    
 	window.document.getElementById(clid+':uploadFileReclFirma').value = firma; 
 	
 	return true;	
}
/*
function limitarTamano(url, clid)
{
	var isWin32 = (navigator.appName =="Microsoft Internet Explorer");
 	var isMozilla = (navigator.appName =="Netscape"); 
 	
 	window.document.getElementById(clid+':uploadFileReclFirma').value = ''; 
 	
 	 if(url==null || url=="")
 	 {
  	    alert("¡No se ha especificado un path!");
  		return false;
  	}
  	
  	if(isWin32)
  	{	
  		
  		tam = GetSizeFile(url);
  			
  		if(tam == null || tam == "" || tam==false)
  		{  	 
  	 		var f = document.getElementById(clid+":uploadfileRecl");      
       		f.parentNode.removeChild(f);
       	 	document.forms[0].submit()
  			return false;
  		}
  		
  		doSubmit = asig_firm_arch_campo(document.getElementById(clid+":uploadfileRecl").value,clid);	
  		return false;
  	}
   	else if(isMozilla)
   	{    
		var applet = window.document.applets[0];
		texto = applet.ReadFileAsString64(url);   
		
  		var firma = firmar_js(texto); 
    	if(firma == false || firma == "")
    	{
    		document.getElementById(clid+":uploadfileRecl").value=''; 
    		return false;
    	}  
  		//alert("FIRMADO!"+firma); 
  		return false;
  	}
  	else
  	{
  		alert('Utilice navegadores soportados: IE o Mozilla');
  		return false;
  	}  
}
*/

function limitarTamano(url,clid)
{
	var isWin32 = (navigator.appName =="Microsoft Internet Explorer");
 	var isMozilla = (navigator.appName =="Netscape"); 
 	var is_chrome = /chrome/.test( navigator.userAgent.toLowerCase() );
 	window.document.getElementById(clid+':uploadFileReclFirma').value = ''; 
 	
 	 if(url==null || url=="")
 	 {
  	    alert("¡No se ha especificado un path!");
  		return false;
  	}
  	  	
  	if(isWin32)
  	{	
  	
  		tam = GetSizeFile(url);  	
  		if(tam == null || tam == "" || tam==false)
  		{
  	 		var f = document.getElementById(clid+":uploadfileRecl");
       		f.parentNode.removeChild(f);
       	 	document.forms[0].submit()
  			return false;
  		}
  		
  		doSubmit = asig_firm_arch_campo(document.getElementById(clid+":uploadfileRecl").value,clid);	
  		return false;
  	}
   	else if(isMozilla && !is_chrome)
   	{
		////var applet = window.document.applets[0];
		netscape.security.PrivilegeManager.enablePrivilege("UniversalFileRead");
		//alert("uploadfileRecl->"+(document.getElementById("SAC00:uploadfileRecl").value).replace(/\\/g,"\\\\"));
		//texto = applet.ReadFileAsString64(url);   
		////texto = applet.ReadFileAsString64((document.getElementById("SAC00:uploadfileRecl").value).replace(/\\/g,"\\\\")); 
		//texto = applet.ReadFileAsString64(document.getElementById("SAC00:uploadfileRecl").value);
  		////var firma = firmar_js(texto); 
    	////if(firma == false || firma == "")
    	////{
    		////document.getElementById("SAC00:uploadfileRecl").value=''; 
    		////return false;
    	///}
    	doSubmit = asig_firm_arch_campo((document.getElementById(clid+":uploadfileRecl").value).replace(/\\/g,"\\\\"),clid);
  		return false;
  	}
  	else
  	{
  		alert('Utilice navegadores soportados: IE o Mozilla');
  		return false;
  	}  
}


function firmar_archivo(url,clid){
  var isWin32 = (navigator.appName =="Microsoft Internet Explorer");
  var isMozilla = (navigator.appName =="Netscape");
  var is_chrome = /chrome/.test( navigator.userAgent.toLowerCase() );
  //alert("url="+url);
  var texto;
  var firma;
  if(url==null || url==""){
  	    alert("¡No se ha especificado un path!");
  	return false;
  }
  alert("La firma del archivo puede durar unos instantes en función del tamaño");
  if(isWin32){

	//alert("FIRMAR CAPICOM");
  	firma = SimpleSignFileToText(url);
  	if(firma == null || firma == ""){
  		document.getElementById(clid+":uploadfileRecl").value=''; 
  		return false;
  	}
  	//alert("FIRMADO!"+firma);	
  	return firma;
  }
  else if(isMozilla && !is_chrome){
    //alert("Firmar archivo is mozilla");  
	var applet = window.document.applets[0];
	texto = applet.ReadFileAsString64(url);   
	//alert("CONTENIDO ARCHIVO64="+texto);
  	firma = firmar_js(texto); 
    if(firma == false || firma == ""){
    	document.getElementById(clid+":uploadfileRecl").value=''; 
    	return false;
    }  
  	//alert("FIRMADO!"+firma); 
  	return firma;
  }
  else{
  	alert('Navegador indefinido');
  	return false;
  }  
  
}
function getBrowser(){
  var isWin32 = (navigator.appName =="Microsoft Internet Explorer");
  var isMozilla = (navigator.appName =="Netscape");
  var is_chrome = /chrome/.test( navigator.userAgent.toLowerCase() );
  if(isWin32){
  	return "I";
  }else if(isMozilla && !is_chrome){
  	return "M";
  }else if(is_chrome){
  	return "C";
  }else{
    alert('Navegador no reconocido\r Utilice Internet Explorer o Mozilla.');
    window.document.location.href="";	
    return "FALSE";
  }
  
}

function firmarPagina(pagina_firmada,pagina_firmada64){
	  var isWin32 = (navigator.appName =="Microsoft Internet Explorer");
	  var isMozilla = (navigator.appName =="Netscape");
	  var is_chrome = /chrome/.test( navigator.userAgent.toLowerCase() );
	  var texto = window.document.getElementsByTagName("html")[0].innerHTML;
	  var texto_encoded64;
	  texto = '<html>'+texto+'</html>';
	  //alert(texto);
	  if(isWin32){
	  	try{
		    texto_encoded64 = encode64(texto);    
		    if(texto_encoded64 == ""){
		    	return false;
		    }
		    //alert("encode64="+texto_encoded64);
		    var firma = firmar_vb(texto_encoded64);
		    if(firma == null || firma ==""){
		    	return false;
		    }
		  	pagina_firmada.value = firma;
		    pagina_firmada64.value =  texto_encoded64;
		  	return true;
		  	//alert("paginafirmada="+pagina_firmada.value);
		 }catch(ex){
		 	return false;
		 }
	  }
	  else if(isMozilla && !is_chrome){
	    texto_encoded64 = encode64(texto); 
	    if(texto_encoded64 == ""){
	    	return false;
	    }    
	    var firma = firmar_js(texto_encoded64);  
	    if(firma == false || firma == ""){
	    	return false;
	    }    
	  	pagina_firmada.value = firma;  
	    pagina_firmada64.value = texto_encoded64;
	  	return true;
	  	//alert("paginafirmada="+pagina_firmada.value); 	
	
	  }  
	  else{
	  	alert('Navegador no reconocido\r Utilice Internet Explorer o Mozilla.');
	  }
}


function putAppletLeerArch(div_applet){
	var nav = getBrowser();
	if(nav!=null){
		if(nav=="M"){
			window.document.getElementById(''+div_applet+'').innerHTML='<applet id="LeerArchivoApplet" alt="Algo no es correcto. Java no funciona." archive="signReadFileUtil.jar,commons-codec-1.3.jar,plugin.jar" code="fileloader.FileReadtoByte" codebase="http://arbitrajedeconsumo.mspsi.es/" height="1" width="1" border=1><PARAM NAME="TAMANO_MAX" VALUE="5242880"></applet>';
		}
	}
}


function subrayar(elemento)
{
	document.getElementById(elemento).style.color= "white";
	document.getElementById(elemento).style.backgroundColor= "#828A92";
}

function subrayar2(elemento)
{
	document.getElementById(elemento).style.color= "white";
}

function blanco(elemento)
{
	document.getElementById(elemento).style.color= "white";
}

function amarillo(elemento)
{
	document.getElementById(elemento).style.color= "yellow";
}

function deshabilitarBotones(){
	inputItems = document.getElementsByTagName('input');
	buttonItems = document.getElementsByTagName('button');
	for(var x = 0; x<inputItems.length; x++){
		if(inputItems[x].type=='submit'){
			inputItems[x].disabled=true;
		}
	}
	for(var x = 0; x<buttonItems.length; x++){
		buttonItems[x].disabled=true;
	}
}

function vaciarInputFileUpload(objeto){
	if(navigator.appName=='Microsoft Internet Explorer'){
		objeto.outerHTML = objeto.outerHTML;
	}else{
		objeto.value='';
	}
}

function deshabilitarEnter(){
	try{
		document.onkeypress = function(e) {
			if(navigator.appName =="Microsoft Internet Explorer") { //it's IE 
				if(window.event.keyCode==13){
					return false;
				}
			}else{
		  		if(e.which==13){
		  			return false;
		  		}
		  	}
		}
	}catch(ex){
		return false;
	}
}



