function checkFields(aux)
{
   checkRutField(aux)
   checktxtLoginField()   
}


//***************************************
// Check Rut field leaving the box STEP 2
//***************************************

function checkDV(crut)

{

  largo = crut.length;
  
  if ( largo >= 8 )
    rut = crut.substring(0, largo - 1);
  else
    rut = crut.charAt(0);
  dv = crut.charAt(largo-1);
  

 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 invalido"); // El rut es invalido
    window.document.frmContacto.rut.value = "";
    window.document.frmContacto.rut.focus();
    return false;
  }

      return true;
}

function checktxtLoginField()
{
  
 if ( window.document.frmLogin.txtPassword.value.length <= 5 )
  {
    alert("La clave debe poseer un largo mínimo de 6 dígitos.");
    window.document.frmLogin.txtPassword.value=""
    window.document.frmLogin.txtPassword.focus();
    return false;
  }
  
  return true;
}

//***************************************
// Check Rut field leaving the box STEP 1
//***************************************

function checkRutField(texto)
{

  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 < 8 )
  {
    alert("Debe ingresar un rut valido");
    window.document.frmContacto.rut.value=""
    window.document.frmContacto.rut.focus();
    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);

  window.document.frmContacto.rut.value = invertido;  

  if ( checkDV(texto) )
    return true;
    
  // El rut es incorrecto
  return false;
}	

// Fin de Funciones Nuevas

function enviar() {

aux = document.frmLogin.txtRUT.value;

checkFields( aux );

if (document.frmLogin.txtRUT.value=="" ){
	alert  ("Por favor digite su RUT ");
	window.document.frmLogin.txtRUT.value="";
	window.document.frmLogin.txtRUT.focus();
    return false;
}else
    if(document.frmLogin.txtPassword.value=="" ){
	alert  ("Por favor digite su Password ");
	window.document.frmLogin.txtPassword.value="";
	window.document.frmLogin.txtPassword.focus();
    return false;
}else{
var tmpstr = "";

rut = document.frmLogin.txtRUT.value;

for ( i=0; i < rut.length ; i++ )
    if ( rut.charAt(i) != ' ' && rut.charAt(i) != '.' && rut.charAt(i) != '-' )
      tmpstr = tmpstr + rut.charAt(i);
      
      document.frmLogin.txtLogin.value = tmpstr;
      document.frmLogin.submit();
}
}
