// valida que el formato del email sea el correcto
function validarmail(correo) {
   var mail = correo.value;
   var puntos = 1;

   for (i = 0; i < mail.length ; i++ ) {
      if (mail.charAt(i) == "@" || mail.charAt(i) == ".") {
         puntos++;
      }
   }
   if (puntos <= 2) {
      alert("No es una casilla de correo electrónico válida\ndebe ser del tipo: usuario@servidor.dom");
      correo.focus();
      correo.select();
      return false;
   }
   return true;
}

function revisarDigito( dvr, rut ) {
   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 dígito verificador válido");
      rut.focus();
      rut.select();
      return false;
   }
   return true;
}

function revisarDigito2( crut, rut ) {
   largo = crut.length;
   if ( largo < 2 ) {
      alert("Debe ingresar el RUT completo")
      rut.focus();
      rut.select();
      return false;
   }
   if ( largo > 2 )
      rut = crut.substring(0, largo - 1);
   else
      rut = crut.charAt(0);
   dv = crut.charAt(largo-1);
   revisarDigito( 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 + ""
   }
   if ( dvr != dv.toLowerCase() ) {
      alert("El número de RUT es incorrecto")
      rut.focus();
      rut.select();
      return false;
   }

   return true
}

function Rut(rut) {
   var texto = rut.value;
   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")
      rut.focus();
      rut.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("El valor ingresado no corresponde a un RUT válido");
	 rut.focus();
	 rut.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>=0; i--,j++ )
      invertido = invertido + dtexto.charAt(i);

   rut.value = invertido;

   if ( revisarDigito2(texto, rut) )
      return true;
   return false;
}

function validarform(formulario) {
   var pass=true;
   if (document.images) {
      for (i=0;i<formulario.length;i++) {
	 var tempobj=formulario.elements[i];
	 if (tempobj.name.substring(0,9)=="requerido") {
	    if (((tempobj.type=="text"||tempobj.type=="textarea")&&
	       tempobj.value=='')||(tempobj.type.toString().charAt(0)=="s"&&
	       tempobj.selectedIndex==0)) {
	       pass=false;
	       break;
	    }
	 }
      }
   }
   if (!pass) {
      shortFieldName=tempobj.name.substring(9,50).toUpperCase();
      alert("El campo "+shortFieldName+" debe ser completado.");
      return false;
   }
   else
      return true;
}


function validarformcampos(formulario) {
   var pass=true;
   if (formulario.length > 0) {
      for (i=0;i<formulario.length;i++) {
         var tempobj=formulario.elements[i];

         if (tempobj.id.substring(0,15)=="campo_requerido") {
            if (((tempobj.type=="text"||tempobj.type=="textarea")&&
               tempobj.value=='')||(tempobj.type.toString().charAt(0)=="s"&&
               tempobj.selectedIndex==0)) {
               pass=false;
               break;
            }
         }
      }
   }
   if (!pass) {
      shortFieldName=tempobj.name.substring(15,50).toUpperCase();
      alert("El campo "+shortFieldName+" debe ser completado.");
      return false;
   }
   else
      return true;
}

function contarcaracteres(texto, contador, maximoTexto) {
  contador.value = maximoTexto-texto.value.length;
  if (contador.value < 0) {
	texto.value = texto.value.substr (0, maximoTexto);
	contador.value = maximoTexto-texto.value.length;
  }
}

