<!--
//FUNCION INICIAL DE VALIDACION DE RUT Y CLAVE
function checkFields( rut, clave )
{
  var tmpstr = "";
  for ( i=0; i < rut.length ; i++ )
	if ( rut.charAt(i) != ' ' && rut.charAt(i) != '.' && rut.charAt(i) != '-' )
		tmpstr = tmpstr + rut.charAt(i);
  rut = tmpstr;
  if ( rut == "")
  {
	alert("Debe completar los datos.");
    window.document.ident.frut.focus();
    window.document.ident.frut.select();
    return false;
  }
  rutdv = rut 
  if ( !checkRutField(rut) )
    return false;
  if ( !checkPinField(rut) )
    return false;
  return true;
}

//FUNCION VALIDACION RUT
function checkRutField(texto)
{
 // texto = texto + dv
  
  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 un Rut válido.")
    window.document.ident.frut.value = "";
    window.document.ident.frut.focus();
    window.document.ident.frut.select();
    return false;
  }
  
  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("Debe ingresar un Rut válido.")
		window.document.ident.frut.value = "";
		window.document.ident.frut.focus();
		window.document.ident.frut.select();
	    return false;
    }
  }
  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++ )
  {
    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>=2; i--,j++ )
	if (dtexto.charAt(i) == "k")
		invertido = invertido + "K";		
	else{
		invertido = invertido + dtexto.charAt(i);
		window.document.ident.frut.value = invertido;  
	}
  if ( checkDV(texto) )
    return true;
  return false;
}

//FUNCION VALIDACION DIGITO VERIFICADOR
function checkDV( crut )
{
  largo = crut.length;
  if ( largo < 2 )
  {
    alert("Debe ingresar un Rut válido.")
    window.document.ident.frut.focus();
    window.document.ident.frut.select();
    return false;
  }
  if ( largo > 2 )
	rut = crut.substring(0, largo - 1);
  else
    rut = crut.charAt(0);
  dv = crut.charAt(largo-1);
  //checkCDV( dv );
  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 + ""
  }
  //window.document.ident.digito.value=dv.toUpperCase()	
  window.document.ident.frut.value = window.document.ident.frut.value + '-' + dv.toUpperCase();
  if ( dvr != dv.toUpperCase() )
  {
    alert("Debe ingresar un Rut válido.")
    window.document.ident.frut.value = "";
   // window.document.ident.digito.value = "";
    window.document.ident.frut.focus();
    return false
  }
  return true
}

//FUNCION VALIDACION CLAVE
function checkPinField(rut)
{

  if ( window.document.ident.clave.value.length == 0 )
  {
    alert("Debe ingresar clave de acceso.");
    window.document.ident.clave.focus();
    window.document.ident.clave.select();
    return false;
  }
  if ( window.document.ident.clave.value.length != 4 )
  {
    alert("Clave debe ser de largo 4.");
    window.document.ident.clave.focus();
    window.document.ident.clave.select();
    return false;
  }
   return true;
}

//FUNCION REMPLAZA CARACTERES EN UN STRING
function Replace(texto,busca,remplaza)
{
  var aux="";
  var letra;
  var i;
  for ( i=0; i < texto.length ; i++ )
  {
	letra = texto.charAt(i)
	if (letra == busca)
	{
		if (remplaza!="")
			aux = aux + remplaza;
	}
	else
		aux = aux + letra;
  }
  return aux;
}

function valida()
{
	if (checkFields(document.ident.frut.value,document.ident.clave.value))
	{
		document.ident.rut.value=Replace(window.document.ident.frut.value.substring(0, window.document.ident.frut.value.length - 2),".","");
		document.ident.digito.value=window.document.ident.frut.value.charAt(window.document.ident.frut.value.length-1);
	    return true;

//		document.ident.rut.value=Replace(document.ident.frut.value,".","");
	}
return false;
}
// Valida un rut
function isEmpty(s) {
	return ( (s==null) || (s.length == 0) )
}

function formatoRut(objrut)
{
  var texto = objrut.value;
  var tmpstr = "";  
  
  if (isEmpty(texto))
	return true;
	
	texto=EliminaCeros(texto);
		
	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 )
	{
    	return false;
	}

  
  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" ) 
		{
		return false;
		}
	}

  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++ )
	{
    	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);

  objrut.value = invertido;
  //alert (invertido)
  
  //if ( checkDV(texto))
  //  	return true;
  return false;
}

function EliminaCeros(texto)
{
		var numero,num,i,dv;
		num="";
		numero="";
		dv="";
		dv=texto.charAt(texto.length-1);
				
		for ( i=0; i < texto.length-1 ; i++ )
		{  	if ( texto.charAt(i) != ' ' && texto.charAt(i) != '.' && texto.charAt(i) != '-' )
					{
						num = num + texto.charAt(i);
					}
		}
		numero=parseFloat(num);
		return numero+dv;		
}
//-->