﻿// JScript File
// http://www.tips.cl/index.php/2009/06/13/valida-rut-en-javascript/
//http://juque.cl/weblog/2004/07/05/validar-rut-en-javascript.html


function revisarDigito( dvr, ID_objeto)
{	
//    debugger;
	var vacio = "";
	dv = dvr + ""	
	if ( dv != '0' && dv != '1' && dv != '2' && dv != '3' && dv != '4' && dv != '5' && dv != '6' && dv != '7' && dv != '8' && dv != '9' && dv != 'k'  && dv != 'K')	
	{		
		alert("Debe ingresar un digito verificador válido");		
	    AxLB_Mensaje_CIN.SetValue("Debe ingresar un digito verificador válido");
        if (ID_objeto == "AxTB_Empresa")	
        {
            AxTB_Empresa_CIN.SetValue(vacio);
	        AxTB_Empresa_CIN.Focus();	    
        }
        else
        {
            AxTB_Usuario_CIN.SetValue(vacio);
	        AxTB_Usuario_CIN.Focus();	    
        }			
        return false;			
//		window.document.form1.rut.focus();		
//		window.document.form1.rut.select();
//		window.document.form1.AxTB_Empresa.focus();		
//		window.document.form1.AxTB_Empresa.select();				
//		return false;	
	}	
	return true;
}

function revisarDigito2( crut, ID_objeto )
{	
//    debugger;
	var vacio = "";
	largo = crut.length;	
	if ( largo < 2 )	
	{		
		alert("Debe ingresar el rut completo")		
	    AxLB_Mensaje_CIN.SetValue("Debe ingresar el rut completo");
        if (ID_objeto == "AxTB_Empresa")	
        {
            AxTB_Empresa_CIN.SetValue(vacio);
	        AxTB_Empresa_CIN.Focus();	    
        }
        else
        {
            AxTB_Usuario_CIN.SetValue(vacio);
	        AxTB_Usuario_CIN.Focus();	    
        }				
		return false;	
				
//		window.document.form1.rut.focus();		
//		window.document.form1.rut.select();		
//		window.document.form1.AxTB_Empresa.focus();		
//		window.document.form1.AxTB_Empresa.select();		
	}	
	if ( largo > 2 )		
		rut = crut.substring(0, largo - 1);	
	else		
		rut = crut.charAt(0);	
	dv = crut.charAt(largo-1);	
	revisarDigito( dv, ID_objeto);	

	if ( rut == null || dv == null )
		return 0	

	var dvr = '0'	
	suma = 0	
	mul  = 2	

	for (i= rut.length -1 ; i >= 0; i--)	
	{	
		suma = suma + rut.charAt(i) * mul		
		if (mul == 7)			
			mul = 2		
		else    			
			mul++	
	}	
	res = suma % 11	
	if (res==1)		
		dvr = 'k'	
	else if (res==0)		
		dvr = '0'	
	else	
	{		
		dvi = 11-res		
		dvr = dvi + ""	
	}
	if ( dvr != dv.toLowerCase() )	
	{		
		alert("EL rut es incorrecto")		
	    AxLB_Mensaje_CIN.SetValue("EL rut es incorrecto");
        if (ID_objeto == "AxTB_Empresa")	
        {
            AxTB_Empresa_CIN.SetValue(vacio);
	        AxTB_Empresa_CIN.Focus();	    
        }
        else
        {
            AxTB_Usuario_CIN.SetValue(vacio);
	        AxTB_Usuario_CIN.Focus();	    
        }				
		return false	
//		window.document.form1.rut.focus();		
//		window.document.form1.rut.select();		
//		window.document.form1.AxTB_Empresa.focus();		
//		window.document.form1.AxTB_Empresa.select();	
	}
	return true
}


function ClearMensaje(s, e) 
{
	var vacio = "";
    AxLB_Mensaje_CIN.SetValue(vacio);
}

//function Rut(texto)
//function Valida_Rut(texto, origen)
function DigVerif(s, e) 
{
	//debugger;
	var v_name = s.name;
	var texto = "";
	if (v_name == "AxTB_Empresa")	
	{
        texto = AxTB_Empresa_CIN.GetValue();
    }
    else
    {
        texto = AxTB_Usuario_CIN.GetValue();
    }
            
	var vacio = "";
	var tmpstr = "";	
	for ( i=0; i < texto.length ; i++ )		
		if ( texto.charAt(i) != ' ' && texto.charAt(i) != '.' && texto.charAt(i) != '-' )
			tmpstr = tmpstr + texto.charAt(i);	
	texto = tmpstr;	
	largo = texto.length;	

	if ( largo < 2 )	
	{		
		alert("Debe ingresar el rut completo")
		AxLB_Mensaje_CIN.SetValue("Debe ingresar el rut completo");
	    if (v_name == "AxTB_Empresa")	
	    {
            AxTB_Empresa_CIN.SetValue(vacio);
		    AxTB_Empresa_CIN.Focus();	    
        }
        else
        {
            AxTB_Usuario_CIN.SetValue(vacio);
		    AxTB_Usuario_CIN.Focus();	    
        }
		return false;	        
        
        //AxTB_Empresa_CIN.focus();
        //_aspxSetFocus(AxTB_Empresa_CIN);
        //_aspxSetFocus("AxTB_Empresa_CIN");
        //_aspxSetFocus("AxTB_Empresa");
        //AxTB_Empresa_CIN.SetFocus();
        //AxTB_Empresa.SetFocus();        

        //AxTB_Empresa_CIN.focus();
        //AxTB_Empresa_CIN.select();
        //AxTB_Empresa.focus();
        
        //this.AxTB_Empresa_CIN.focus(); 
        //this.AxTB_Empresa_CIN.select();         
        
        //var mytext = document.getElementById("AxTB_Empresa");
        //var mytext = document.getElementById("AxTB_Empresa_CIN");
        //mytext.focus(); 
        //window.document.form1.AxTB_Empresa_CIN.focus();		
        //window.document.form1.AxTB_Empresa.focus();		    
        //window.document.AxTB_Empresa_CIN.focus();
        //window.document.AxTB_Empresa.focus();
        //this.AxTB_Empresa_CIN.focus();
        //this.AxTB_Empresa.focus();  
        //this.SetFocus(AxTB_Empresa);      
        //this.SetFocus(AxTB_Empresa_CIN);  
        //this.SetFocus("AxTB_Empresa");                  
		//this.SetFocus("AxTB_Empresa_CIN");                  		
		        
//		window.document.form1.rut.focus();		
//		window.document.form1.rut.select();	
		//window.document.form1.AxTB_Empresa_CIN.focus();		
		//window.document.form1.AxTB_Empresa.select();
		//window.document.all.AxTB_Empresa_CIN.focus();
		
		//AxTB_Empresa_CIN.hasFocus();
		//AxTB_Empresa_CIN.Focus();
	}	

	for (i=0; i < largo ; i++ )	
	{			
		if ( texto.charAt(i) !="0" && texto.charAt(i) != "1" && texto.charAt(i) !="2" && texto.charAt(i) != "3" && texto.charAt(i) != "4" && texto.charAt(i) !="5" && texto.charAt(i) != "6" && texto.charAt(i) != "7" && texto.charAt(i) !="8" && texto.charAt(i) != "9" && texto.charAt(i) !="k" && texto.charAt(i) != "K" )
 		{			
			alert("El valor ingresado no corresponde a un R.U.T válido");			
		    AxLB_Mensaje_CIN.SetValue("El valor ingresado no corresponde a un R.U.T válido");
	        if (v_name == "AxTB_Empresa")	
	        {
                AxTB_Empresa_CIN.SetValue(vacio);
		        AxTB_Empresa_CIN.Focus();	    
            }
            else
            {
                AxTB_Usuario_CIN.SetValue(vacio);
		        AxTB_Usuario_CIN.Focus();	    
            }			
			return false;					
//			window.document.form1.rut.focus();			
//			window.document.form1.rut.select();		
//			window.document.form1.AxTB_Empresa.focus();			
//			window.document.form1.AxTB_Empresa.select();	
		}	
	}	

	var invertido = "";	
	for ( i=(largo-1),j=0; i>=0; i--,j++ )		
		invertido = invertido + texto.charAt(i);	
	var dtexto = "";	
	dtexto = dtexto + invertido.charAt(0);	
	dtexto = dtexto + '-';	
	cnt = 0;	

	for ( i=1,j=2; i<largo; i++,j++ )	
	{		
		//alert("i=[" + i + "] j=[" + j +"]" );		
		if ( cnt == 3 )		
		{			
			dtexto = dtexto + '.';			
			j++;			
			dtexto = dtexto + invertido.charAt(i);			
			cnt = 1;		
		}		
		else		
		{				
			dtexto = dtexto + invertido.charAt(i);			
			cnt++;		
		}	
	}	

	invertido = "";	
	for ( i=(dtexto.length-1),j=0; i>=0; i--,j++ )		
		invertido = invertido + dtexto.charAt(i);	

	//window.document.form1.rut.value = invertido.toUpperCase()		
	//window.document.form1.AxTB_Empresa.value = invertido.toUpperCase()			
    if (v_name == "AxTB_Empresa")	
    {
        AxTB_Empresa_CIN.SetValue(invertido.toUpperCase());    
    }
    else
    {
        AxTB_Usuario_CIN.SetValue(invertido.toUpperCase());    
    }    

	if ( revisarDigito2(texto, v_name) )		
		return true;	

	return false;
}