﻿function reemplaza_caracter(texto,caracter,newcaracter){
    var aux=0;
    do {texto=texto.replace(caracter,newcaracter);}
    while (texto.indexOf(caracter) != -1);
return texto; }	

/*
----------------------------------------------------------------------------------
FUNCTION FormateaRut que formatea entrada de rut */
function elimina_caracter(texto,caracter){
    var aux=0;
    do {texto=texto.replace(caracter,"");}
    while (texto.indexOf(caracter) != -1);
return texto; }	 
/*----------------------------------------------------------------------------------*/


/*
----------------------------------------------------------------------------------
FUNCTION Valida_rut que valida rut */
function Valida_rut(source, arguments){
    var rut=elimina_caracter(arguments.Value,".");suma=0;mul=2;i=0;
    if(rut.length>2){
        for (i=rut.length-3;i>=0;i--){
	        suma=suma+parseInt(rut.charAt(i)) * mul;
	        mul= mul==7 ? 2 : mul+1;}
	    var dvr = ''+(11 - suma % 11);
	    if (dvr=='10') dvr = 'K'; else if (dvr=='11') dvr = '0';
	    if (rut.charAt(rut.length-2)!="-"||rut.charAt(rut.length-1).toUpperCase()!=dvr) 		     
	        arguments.IsValid = false;
	    else
	        arguments.IsValid = true;}
	 else
	   arguments.IsValid = false;
}
/*
----------------------------------------------------------------------------------*/
            
/*
----------------------------------------------------------------------------------
FUNCTION FormateaRut que formatea entrada de rut
*/

function FormateaRut(fld, milSep, decSep, e) { 
    var sep = 0; 
    var i = j = 0; 
    var len = len2 = 0; 
    var strCheck = '0123456789-Kk'; 
    var aux = aux2 = ''; 
    var hasta=0;
    var textaux='';
    var textaux1='';
    var jj=0;
    hasta=0;
    textaux = fld.value;
    var whichCode = (window.Event) ? e.which : e.keyCode; 
    var key = String.fromCharCode(whichCode); // Recupera codigo de la tecla pulsada
    if (whichCode == 13) return true; // Presiono [ ENTER ]
    if (strCheck.indexOf(key) == -1){
    if (whichCode != 0) return false; // codigo de tecla no valida (no encontrada en la cadena)
        else
       key=fld.value.substr(fld.value.length-1,1);
    }
    if (textaux.length>11) return true;
        textaux = textaux + key;
        len = textaux.length ;
        textaux = elimina_caracter(textaux,'.');
        hasta = textaux.indexOf('-');    
        if (hasta != -1){
            for (jj=hasta;jj<textaux.length;jj++){
                  textaux1 = textaux1 + textaux.charAt(jj);
             }   
            if(textaux1.lenght>1)textaux1 = textaux1.substr(0,1);
            len = hasta;
        }
          
        for(i = 0; i < len; i++) 
              if ((textaux.charAt(i) != '0') && (textaux.charAt(i) != decSep)) break; 
              aux = ''; 
              for(; i < len; i++) 
              if (strCheck.indexOf(textaux.charAt(i))!=-1) aux += textaux.charAt(i); 
              var sRes = '';
              if(aux.length>3){  
	          for (var j, i = aux.length-1, j = 0; i >= 0; i--, j++) 
	              sRes = aux.charAt(i) + ((j > 0) && (j % 3 == 0)? ".": "") + sRes; 
	             
	  	  if (textaux.indexOf('-') !=-1)
	  	    {
	  	     fld.value = sRes.substr(0,sRes.length);
	 	     if (textaux1.length>1){
	 	         if (whichCode == 0) 
	 	            fld.value = sRes.substr(0,sRes.length) + '-' + key;
	 	           else
	 	            {
	 	            fld.value = sRes.substr(0,sRes.length) + '-' + key;
	 	            fld.value=fld.value.substr(0,fld.value.length-1);
	 	            }
	 	        }
	 	     }
		    else
		    if (whichCode == 0) fld.value = sRes.substr(0,sRes.length)+ textaux1;
		    else
	         fld.value = sRes.substr(0,sRes.length-1) + textaux1;
			}
} 
/*----------------------------------------------------------------------------------*/

/*
----------------------------------------------------------------------------------
PLUG-IN PARA FIRMAR LIBRO DTE, se podria ampliar para otros tipo de documentos
completa los cuadros con los datos de la llave publica-privada de la firma electronica
*/

function firmar_libro(){
		Firma.xml_firmado.value = SIIOISignedXML.signedXMLid(
				Firma.xml_firmado.value,
				Firma.NAMENODO.value,
				Firma.NAMEID.value,
				Firma.NAMESPACE.value);
		}		
/*----------------------------------------------------------------------------------*/


/*
----------------------------------------------------------------------------------
Funcion para validar si un campo fecha es valido
devuelve foco al control si es incorrecto y lo deja pasar si esta en blanno
*/
function esfecha(dateStr) {
		 var datePat = /^(\d{1,2})(\/|-)(\d{1,2})\2(\d{4})$/;
		 var i;
		 var pp,k1,k2,k3;
		 var matchArray = dateStr.value.match(datePat); // is the format ok?
		 retorno=true;
		 if (matchArray == null) {
			if(dateStr.value.length==0){
			   return true;
			}
			else
			{
			alert("El formato de fecha no es válido. Recuerde el formato:dd/mm/yyyy")
			dateStr.focus();dateStr.select();
			return false
			}
		 }
		 day=matchArray[1];
		 month = matchArray[3];
		 year = matchArray[4];
         if (month < 1 || month > 12) { // check month range
		    alert("Los meses deben ir entre 1 y 12.");
			dateStr.focus();dateStr.select();return false
		 }
		 if (day < 1 || day > 31) {
		    alert("Los dias deben ir entre 1 y 31.");
			dateStr.focus();dateStr.select();return false
		 }
		 if ((month==4 || month==6 || month==9 || month==11) && day==31) {
		    alert("El mes " + month + " no tiene 31 dias!");
			dateStr.focus();dateStr.select();return false
		 }
		 if (month == 2) { // check for february 29th
		 	var isleap = (year % 4 == 0 && (year % 100 != 0 || year % 400 == 0));
			if (day>29 || (day==29 && !isleap)) {
				alert("Febrero del " + year + " no tiene " + day + " dias!");
				dateStr.focus();dateStr.select();return false
			}
		 } 		
		}
		
function formatNmb(nNmb){ 
			var sRes = ""; 
			nNmb = elimina_caracter(nNmb,'.');
		    for (var j, i = nNmb.length - 1, j = 0; i >= 0; i--, j++) 
		    sRes = nNmb.charAt(i) + ((j > 0) && (j % 3 == 0)? ".": "") + sRes; 
			if(sRes.length==0)sRes=nNmb;
			return sRes;  }
		
	
function cargaDocumento1(url) {   

			if (window.XMLHttpRequest) {
				peticion = new XMLHttpRequest();
				peticion.onreadystatechange = procesaPeticion1;
				peticion.open("GET", url, false);
				peticion.send(null);    
				} else if (window.ActiveXObject) {
				isIE = true;
				try {
					peticion = new ActiveXObject("Msxml2.XMLHTTP");
					} catch (e) {
				try {
					peticion = new ActiveXObject("Microsoft.XMLHTTP");
					} catch (E) {
					peticion = false;
				}
			}
			if (peticion) {
				peticion.onreadystatechange = procesaPeticion1;
				peticion.open("GET", url, false);
				peticion.send();
			}
		}
		}
		function procesaPeticion1() {
			if (peticion.readyState == 4) {
				if (peticion.status == 200) {
					muestraDocumento1();
					} else {
					alert('Problemas para cargar el documento solicitado: \n  El Servidor no responde ' + peticion.statusText);
					}
					}   
			}		
		
			function muestraDocumento1(){
			var doc = peticion.responseText;
			if(document.getElementById(el_control).type=='text'){
			//alert('aqui voy');
			document.getElementById(el_control).value= doc;
			//if((document.getElementById(el_control).value=="0" || document.getElementById(el_control).value== 0)){
			 //preguntar por el nombre del control; si fuese nueva resolucion seria asi
			 if(el_control=='n_res')window.open('/SYS_SERVIU/OrdenPago/det_resolucion.aspx', 'Resolucion','scrollbars=yes,status=yes,width=950,height=550,top=0,left=0');
			//}
			
			}
			else{
			//alert('aqui voy');
			document.getElementById(el_control).innerHTML= doc;
			if(el_control=='n_res')window.open('/SYS_SERVIU/OrdenPago/det_resolucion.aspx', 'Resolucion','scrollbars=yes,status=yes,width=950,height=550,top=0,left=0');
			//if((document.getElementById(el_control).value=="0") || (document.getElementById(el_control).value== 0)){
			 //preguntar por el nombre del control; si fuese nueva resolucion seria asi
			//   window.open('/Intranet08/SYS_SERVIU/OrdenPago/det_resolucion.aspx', 'Resolucion','scrollbars=yes,status=yes,width=950,height=550,top=0,left=0');
			//}
			}
		}		


function buscador(valores,numero,valor,titulo,el_titulo){
		var paso;
		var el_id1,el_id2,el_id3,el_id4;
		if (NS4) {
			    if (!win.find(numero))
					while(win.find(numero, false, true))
					n++;
				else
					n++;
					}
		  if (IE4) {
			txt = win.document.body.createTextRange();
			found = txt.findText(numero);
			txt.moveEnd("textedit");
				if (found) {
					txt.moveStart("character", -1);
					txt.findText(numero);
					txt.scrollIntoView();
					paso = reemplaza_caracter(valor,'$','_');
					//pintar nuevo click
					document.getElementById(paso + '_sub_cla_n1').style.background ="#ffffcc";
					document.getElementById(paso).style.background ="#ffffcc";
					//dejar en blanco click anterior
					document.getElementById(document.getElementById('ctl00_Contenido_el_control_an').value).style.background ="#FFFFFF";
					document.getElementById(document.getElementById('ctl00_Contenido_el_control_an').value + "_sub_cla_n1").style.background ="#FFFFFF";
					//dejar como control actual el valor del control
					document.getElementById('ctl00_Contenido_el_control_an').value=paso;
					el_id1 =valores.substring(0,15); //id_plancuenta
			        el_id2 =valores.substring(15,30); //id_clasificador
			        el_id3 =valores.substring(30,45); //id_requerimeinto
			        el_id4 = valores.substring(45,55); //id_insumo
                    
                    
                    document.getElementById('ctl00_Contenido_TextBox1').value = el_id1;			//id_plancuenta
                    document.getElementById('ctl00_Contenido_TextBox1').value = el_id1;			//id_plancuenta
			        document.getElementById('ctl00_Contenido_TextBox2').value = parseFloat(el_id2); //id_clasificador
			        document.getElementById('ctl00_Contenido_TextBox3').value = parseFloat(el_id3); //id_requerimeinto
			        document.getElementById('ctl00_Contenido_insumo1').value = parseFloat(el_id4);  //id_insumo	
			        
			        document.getElementById('ctl00_Contenido_insumo_titulo').value=titulo;
			        document.getElementById('ctl00_Contenido_cuenta_titulo').value=el_titulo;								
					
					document.getElementById('ctl00_Contenido_el_control_an2').value = document.getElementById(paso + '_sub_cla_n1').value;
					
					docu="/sys_serviu/ordenpago/Cuentas_Todas_detppto.aspx?id1=" + parseFloat(el_id1) + "&id2=" + parseFloat(el_id3) + "&id3=" + parseFloat(el_id4);
					el_control='ctl00_Contenido_panel_2';
					//alert(docu);
					cargaDocumento1(docu);
					
					n++;
				}
				//else {
				//	if (n > 0) {
				//		n = 0;
				//		findInPage(numero);
				//	}
				//}
			}
		}		
		
		
function buscador2(valores,numero,valor,titulo,el_titulo){
		var paso;
		var el_id1,el_id2,el_id3,el_id4;
		if (NS4) {
			    if (!win.find(numero))
					while(win.find(numero, false, true))
					n++;
				else
					n++;
					}
		  if (IE4) {
			txt = win.document.body.createTextRange();
			found = txt.findText(numero);
			txt.moveEnd("textedit");
				if (found) {
					txt.moveStart("character", -1);
					txt.findText(numero);
					txt.scrollIntoView();
					paso = reemplaza_caracter(valor,'$','_');
					//pintar nuevo click
					
					document.getElementById(paso + '_sub_cla_n1').style.background ="#ffffcc";
					document.getElementById(paso).style.background ="#ffffcc";
					//dejar en blanco click anterior
					document.getElementById(document.getElementById('el_control_an').value).style.background ="#FFFFFF";
					
					document.getElementById(document.getElementById('el_control_an').value + "_sub_cla_n1").style.background ="#FFFFFF";
					
					//dejar como control actual el valor del control
					document.getElementById('el_control_an').value=paso;
					
					el_id1 =valores.substring(0,15); //id_plancuenta
			        el_id2 =valores.substring(15,30); //id_clasificador
			        el_id3 =valores.substring(30,45); //id_requerimeinto
			        el_id4 = valores.substring(45,55); //id_insumo
                    
                    
                    document.getElementById('TextBox1').value = el_id1;			//id_plancuenta
                    
                    document.getElementById('TextBox1').value = el_id1;			//id_plancuenta
                    
			        document.getElementById('TextBox2').value = parseFloat(el_id2); //id_clasificador
			        
			        document.getElementById('TextBox3').value = parseFloat(el_id3); //id_requerimeinto
			        
			        document.getElementById('insumo1').value = parseFloat(el_id4);  //id_insumo	
			        
			        
			        document.getElementById('insumo_titulo').value=titulo;
			        
			        document.getElementById('cuenta_titulo').value=el_titulo;								
					
					document.getElementById('el_control_an2').value = document.getElementById(paso + '_sub_cla_n1').value;
					
					
					//docu="/sys_serviu/ordenpago/Cuentas_Todas_detppto.aspx?id1=" + parseFloat(el_id1) + "&id2=" + parseFloat(el_id3) + "&id3=" + parseFloat(el_id4);
					//cargaDocumento1(docu);
					
					n++;
				}
				//else {
				//	if (n > 0) {
				//		n = 0;
				//		findInPage(numero);
				//	}
				//}
			}
		}				
		
		
							
								
		
		
function devuelve_cuenta(fila,tipo){
			var el_campo,el_campo1,el_campo2,el_campo3,el_control;
//alert(fila);
//alert(tipo);
			if(tipo==0){
			   el_campo = window.opener.document.getElementById("grilla_oc_R" + fila + "_la_cuenta");  
		       el_campo1 = window.opener.document.getElementById("grilla_oc_R" + fila + "_id_press");  
		       el_campo2 = window.opener.document.getElementById("grilla_oc_R" + fila + "_id_req");  
		       el_campo3 = window.opener.document.getElementById("grilla_oc_R" + fila + "_id_insumo");        
		       if(document.form1.insumo_titulo.value!='&nbsp;'){
		       //alert('aquivoy 1');
		          el_campo.value = document.form1.cuenta_titulo.value + ":" + document.form1.el_control_an2.value + "," + document.form1.insumo_titulo.value;   
		          }
		        else
		          {
		         // alert('aquivoy 2');
		           el_campo.value = document.form1.cuenta_titulo.value + ":" + document.form1.el_control_an2.value;
		          }
		        el_campo1.value =document.form1.TextBox1.value;
		        //alert('paso 1');
		        el_campo2.value =document.form1.TextBox3.value;
		        //alert('paso 2');
		        el_campo3.value =document.form1.insumo1.value;
		        //alert('paso 3');
			  }
					  
			if(tipo==1){
			   el_campo = window.opener.document.getElementById("text_buscar"); 
			   el_campo1 = window.opener.document.getElementById("id_programa");  
			   el_campo2 = window.opener.document.getElementById("id_req");  
			   el_campo3 = window.opener.document.getElementById("id_insumo"); 
			   if(document.form1.insumo_titulo.value!='&nbsp;'){
				  el_campo.value = document.form1.cuenta_titulo.value + ":" + document.form1.el_control_an2.value + "," + document.form1.insumo_titulo.value;   
				  }
				else
				  {
				   el_campo.value = document.form1.cuenta_titulo.value + ":" + document.form1.el_control_an2.value;
				  }
				el_campo1.value =document.form1.TextBox1.value;
		        el_campo2.value =document.form1.TextBox3.value;
		        el_campo3.value =document.form1.insumo1.value;    
		        }
			 

			  
			if(tipo==2){
			    
		        el_campo = window.opener.document.getElementById("Sigfe_nomcuenta");  
		        el_campo1 = window.opener.document.getElementById("Sigfe_idpress");  
		        el_campo2 = window.opener.document.getElementById("Sigfe_idreq");  
		        el_campo3 = window.opener.document.getElementById("Sigfe_idinsumo");
		        if(document.form1.insumo_titulo.value!='&nbsp;'){
		           el_campo.value = document.form1.cuenta_titulo.value + ":" + document.form1.el_control_an2.value + "," + document.form1.insumo_titulo.value;   
		          }
		        else
		          {
		           el_campo.value = document.form1.cuenta_titulo.value + ":" + document.form1.el_control_an2.value;
		          }
		          //alert(document.Form1.cuenta_titulo.value & ":" & document.Form1.el_control_an2.value);
		        el_campo1.value =document.form1.TextBox1.value;
		        el_campo2.value =document.form1.TextBox3.value;
		        el_campo3.value =document.form1.insumo1.value;
		    		
			    //if(window.opener.document.getElementById("el_menu").value==1)
			     //{
			    //  el_control="Texbox4";
			    //  window.opener.document.getElementById(el_control).focus();
			     // window.opener.document.getElementById(el_control).select();
			    // }
			  }
			  
			  if(tipo==3){
			    
		        el_campo = window.opener.document.getElementById("C1WEBGRID2_R" + fila + "_la_cuenta_1");  
		        el_campo1 = window.opener.document.getElementById("C1WEBGRID2_R" + fila + "_id_press_1");  
		        el_campo2 = window.opener.document.getElementById("C1WEBGRID2_R" + fila + "_id_req_1");  
		        el_campo3 = window.opener.document.getElementById("C1WEBGRID2_R" + fila + "_id_insumo_1");
		        if(document.form1.insumo_titulo.value!='&nbsp;'){
		           el_campo.value = document.form1.cuenta_titulo.value + ":" + document.form1.el_control_an2.value + "," + document.form1.insumo_titulo.value;   
		          }
		        else
		          {
		           el_campo.value = document.form1.cuenta_titulo.value + ":" + document.form1.el_control_an2.value;
		          }
		          //alert(el_campo.value);
		          //alert(document.Form1.cuenta_titulo.value & ":" & document.Form1.el_control_an2.value);
		        el_campo1.value =document.form1.TextBox1.value;
		        el_campo2.value =document.form1.TextBox3.value;
		        el_campo3.value =document.form1.insumo1.value;
		    		
			    //if(window.opener.document.getElementById("el_menu").value==1)
			     //{
			    //  el_control="Texbox4";
			    //  window.opener.document.getElementById(el_control).focus();
			     // window.opener.document.getElementById(el_control).select();
			    // }
			  }
			  
			if(tipo==4){
			    el_campo = window.opener.document.getElementById("C1WEBGRID2_R" + fila + "_la_cuenta");  
		        el_campo1 = window.opener.document.getElementById("C1WEBGRID2_R" + fila + "_id_press");  
		        el_campo2 = window.opener.document.getElementById("C1WEBGRID2_R" + fila + "_id_req");  
		        el_campo3 = window.opener.document.getElementById("C1WEBGRID2_R" + fila + "_id_insumo");
		        if(document.form1.insumo_titulo.value!='&nbsp;'){
		           el_campo.value = document.form1.cuenta_titulo.value + ":" + document.form1.el_control_an2.value + "," + document.form1.insumo_titulo.value;   
		          }
		        else
		          {
		           el_campo.value = document.form1.cuenta_titulo.value + ":" + document.form1.el_control_an2.value;           
		          }
		        el_campo1.value =document.form1.TextBox1.value;
		        el_campo2.value =document.form1.TextBox3.value;
		        el_campo3.value =document.form1.insumo1.value;
			  }
			  
			  
			  
			  if(tipo==5){
			    
		        el_campo = window.opener.document.getElementById("C1WebGrid1_R" + fila + "_la_cuenta");  
		        el_campo1 = window.opener.document.getElementById("C1WebGrid1_R" + fila + "_id_press");  
		        el_campo2 = window.opener.document.getElementById("C1WebGrid1_R" + fila + "_id_req");  
		        el_campo3 = window.opener.document.getElementById("C1WebGrid1_R" + fila + "_id_insumo");
		        if(document.form1.insumo_titulo.value!='&nbsp;'){
		           el_campo.value = document.form1.cuenta_titulo.value + ":" + document.form1.el_control_an2.value + "," + document.form1.insumo_titulo.value;   
		          }
		        else
		          {
		           el_campo.value = document.form1.cuenta_titulo.value + ":" + document.form1.el_control_an2.value;
		          }
		        el_campo1.value =document.form1.TextBox1.value;
		        el_campo2.value =document.form1.TextBox3.value;
		        el_campo3.value =document.form1.insumo1.value;
			  }
			  
			  if(tipo==10){
				el_campo = window.opener.document.getElementById("ctl00_Contenido_cuentacargo");  
				el_campo1 = window.opener.document.getElementById("ctl00_Contenido_cuenta1");  
				el_campo2 = window.opener.document.getElementById("ctl00_Contenido_cuenta2");  
				el_campo3 = window.opener.document.getElementById("ctl00_Contenido_cuenta3");  
		        if(document.form1.insumo_titulo.value!='&nbsp;'){
		           el_campo.value = document.form1.cuenta_titulo.value + ":" + document.form1.el_control_an2.value + "," + document.form1.insumo_titulo.value;   
		          }
		        else
		          {
		           el_campo.value = document.form1.cuenta_titulo.value + ":" + document.form1.el_control_an2.value;           
		          }
		        el_campo1.value =document.form1.TextBox1.value;
		        el_campo2.value =document.form1.TextBox3.value;
		        el_campo3.value =document.form1.insumo1.value;
			  }
			  
			  
			window.close();
			}		


    
		
