function validarRUT(rut)
{
  var rut_tmp;
  var dv_tmp;
  
  //Remover caracteres especiales
  rut = checkTexto (rut, false, false, true, true);
  rut_tmp = rut.substring(0,rut.length-1);
  dv_tmp = rut.charAt(rut.length - 1);

  if ( rut.length < 2 )
    return false;

  if ( !checkRut(rut_tmp) )
    return false;

  if ( !checkDV(dv_tmp) )
    return false;

  if ( !val_rut(rut_tmp, dv_tmp) )
      return false;

  //asignación en duro
  //document.formulario.campo1.value = rut;
  return true;
}

function checkRut(rut)
{
  var verify=false;
  //Verificar rut si son numeros
  verify = checkTexto (rut, true, false, false, false);
  if (!verify)
	return false;
  return true;
}

function checkDV(dv)
{

  var verify=false;
  //verificar DV si es numero o k
  verify = checkTexto (dv, false, true, false, false);
  if (!verify)
	return false;
  return true;
}

function val_rut(numrut, dgvrut) {

        var rut1 = new MakeArray(8);
        var rutori = numrut;

        var j = 8;
        var len = rutori.length - 1;
        for (var i = len; i >= 0; i--) {
                if (rutori.charAt(i) != " ") {
                        rut1[j] = parseInt(rutori.charAt(i), 10);
                        j--;
                }
        }
        var total =     rut1[1] * 3 + rut1[2] * 2 + rut1[3] * 7 +
                        rut1[4] * 6 + rut1[5] * 5 + rut1[6] * 4 +
                        rut1[7] * 3 + rut1[8] * 2;

        var aux = total / 11;
        if (aux < 1) aux = 0;
        var parteent = parseInt(aux, 10); // parte entera
        if ((total - parteent * 11) == 0)
                var dig = 0;
        else
                var dig = 11 - (total - parteent * 11);

        if (dig < 10) {
                if (dig == parseInt(dgvrut, 10))
                        return (true);
        }
        else {
                if (dgvrut == "K" || dgvrut == "k" )
                        return (true);
        }
        return (false);

}

function MakeArray(n) {

   this.length = n;
   for (var i = 1; i <= n; i++)
        this[i] = 0;

    return this;
}

function formato_rut(texto, activo) {
var invertido = "";
var dtexto = "";
var cnt = 0;
var i=0;
var j=0;
var largo = "";	
	if (activo) {
		  texto = formato_rut(texto, false)
		  largo = texto.length;
		  for ( i=(largo-1),j=0; i>=0; i--,j++ )
		    invertido = invertido + texto.charAt(i);		  
		  dtexto = dtexto + invertido.charAt(0);
		  dtexto = dtexto + '-';		  
		  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);
		  if (invertido == '-') invertido = ""
		  texto = invertido;
	} else {
		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;
	}
	return texto;
}function checkTexto (texto, bNumeros, bDv, bEspeciales, bByRef)
{
  var tmpstr = "";
  var estado;
  var i = 0;
  if (bEspeciales) {
	for ( i=0; i < texto.length ; i++ )
	    if ( texto.charAt(i) != '-' )
			tmpstr = tmpstr + texto.charAt(i);
	if (bByRef) {
		texto = tmpstr;	
		return texto;
	}
  }

  if (bNumeros || bDv)
	if (texto != null)
	  for (i=0; i < texto.length ; i++ )
	  {
		if (bNumeros)
			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" ) 
				return false;
		if (bDv)
			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" ) 
				return false;
	  }  
  return (true);
}

//Utilizar esta función sólo para evento onkeypress
function TeclearSoloNumeros() 
{
	if (document.all || document.layers) {
		var tecla = window.event.keyCode;			
		if (!(tecla >= 48 && tecla <= 57))
			return false;
		else	
			return true;
	}
}
//#######################################
function limpiar() {
	document.formulario.reset();
	document.formulario.campo1.focus();	
}

function redirect() {
	if (document.all || document.layers)
		var tecla = window.event.keyCode;
		if (tecla == 13)
			if (validar())
				formulario.submit();
}

function validar() {
	var rut   = document.formulario.campo1.value;
	if (!validarRUT(formato_rut(rut, false))) {
		alert("El Nº de RUT es incorrecto. Por favor, reingrese su RUT");
		document.formulario.campo1.focus();
		return false;	
	}
	else {
		document.formulario.rut.value = formato_rut(rut, false)
	}
	return true;
}
