//----------------------------EJEMPLOS DE USO------------------------------

// CAMPO COMPLETO    ESCOMPLETO(OBJETO.VALUE,NOMBRE DEL OBJETO)
// CAMPO NUMERO      ESNUMERO(OBJETO.VALUE,NOMBRE DEL CAMPO)
// TODOS COMPLETOS   TODOSCOMPLETOS(FORM)
// TODOS COMILLAS    TODOSCOMILLAS(FORM)
// CAMPO RUT         ESRUT(RUT.VALUE,DV.VALUE)
// CAMPO MAIL        ESMAIL(MAIL.VALUE)
// LARGO MINIMO      LARGOMINIMO(OBJETO,LARGO)
// LARGO MAXIMOO     LARGOMAXIMO(OBJETO,LARGO)
// PAGINA WEB        ESWEB(OBJETO)

var errores = new Array();
var n = 0;
var error = "";
//------------------------------------------LARGO MINIMO-------------------------------------------------
function largominimo(texto,largo){
	if (texto.value.length < largo){
		errores[n] = "El campo "+texto.name.toUpperCase()+" debe tener mas de "+largo+" carácteres";
		n++;
	}
}


//------------------------------------------LARGO MAXIMO-------------------------------------------------
function largomaximo(texto,largo){
	if (texto.value.length > largo){
		errores[n] = "El campo "+texto.name.toUpperCase()+" debe tener menos de "+largo+" carácteres";
		n++;
	}
}


//----------------------------------------CAMPO COMPLETO-------------------------------------------------
function escompleto(texto){
	if (texto.value==""){
		errores[n] = "El campo "+texto.name.toUpperCase()+" está vacío";
		n++;
	}
}


//----------------------------------------VERIFICACION DE NUMEROS----------------------------------------------
function esnumero(numero){
		var number = parseFloat(numero.value);
		if (isNaN(number)==true){			
			errores[n] = "El campo "+numero.name.toUpperCase()+" no es un numero válido";
			n++;
			}
	}

	
//------------------------------------------VERIFICACION DE PAGINA WEB-------------------------------------------------	
function esweb(web){
	web=web.value;
		if ((web.indexOf('.') == -1) || (web.length < 4)){
			errores[n] = "La página web ingresada no es válida";
			n++;
			}
	}	
	
	
//---------------------------------------VERIFICACION DE TODOS LOS CAMPOS COMPLETOS---------------------------------------------	
function todoscompletos(form){
	for (e=0;e < form.length-1 ; e++)	{
		if (form.elements[e].value == ""){
			error="si";		 
		}			
	}
	if (error=="si"){
		errores[n] = "Todos los campos deben ser completados";
		n++;	
		error="no"
	}	
}


//------------------------------------------AVISO DE COMILLAS-------------------------------------------------
function todoscomillas(form){
	for (e=0;e < form.length-1 ; e++)	{
		valor=form.elements[e].value;
		if (valor.indexOf('"') != -1 || valor.indexOf("'") != -1){		
			error="si";	 
			
		}			
	}	
	if (error=="si"){
		errores[n] = "Ningún campo debe contener comillas, ni simples, ni dobles.";
		n++;	
		error="no"
	}		
}


//------------------------------------------VERIFICACION DE RUT-------------------------------------------------
function esrut(ruti,digi)
  	{
	ruti=ruti.value;
	digi=digi.value;
	
	var mult = "234567234";
	var i;
	var Sum, Rest, Verif;
	
	digi= digi.toUpperCase();
	Sum = 0;
	if ( ruti.length == 7 )
	NuevoRut = "0" + ruti;
	else
	NuevoRut = ruti;

	for (i = NuevoRut.length; i > 0; i-- )
	{
	Sum += parseInt(mult.charAt(8-i)) * parseInt(NuevoRut.substring(i-1,i));
	}

	Rest = 11 - (Sum % 11);
	if ( Rest == 11 ) 
	Verif = "0";
	else 
	{
	if ( Rest == 10 )
		Verif = "K";
	else
		Verif = Rest;
	}	
	if (ruti.length == 7)
	{	 
		f.rut.value="0"+ruti; 
		ruti=="0"+ruti;
	}
	else	
	if (ruti.length != 8)
	{	 
		errores[n] = "Rut ingresado es erroneo, deben ser 8 digitos";
		n++;			 
	}
	
	if ( digi != Verif )
	{
		errores[n] = "Rut ingresado es erroneo, chequee digito verificador";
		n++;
	}		
}
	
	
//------------------------------------------VERIFICACION DE EMAIL-------------------------------------------------	
function esmail(email){

	email=email.value;
    if (email.indexOf('@') == -1)
    {
      errores[n] = "No hay un signo arroba (@) en el email";
      n++;
    }

    if (email.indexOf('@') >0 && email.indexOf('@') != email.lastIndexOf('@'))
    {
      errores[n] = "Hay mas de un signo arroba (@) en el email";
      n++;
    }

    if (email.indexOf('@') == 0 || email.indexOf('@') == email.length - 1)
    {
      errores[n] = "Faltan el usuario o el dominio en el e-mail";
      n++;
    }

    if (email.indexOf('@') >0)
    {
      ladoderecho = email.substring(email.indexOf('@'));
      if (ladoderecho.indexOf('.') == -1)
      {
        errores[n] = "No hay un punto despues del arroba";
        n++;
      }
    }
}	
		


//------------------------------------------RUT EN UN CAMPO-------------------------------------------------	
function rutChk2(rut){
		  if (rut.length<9){
		   return false;
		   }
		  i1=rut.indexOf("-");  
		  dv=rut.substr(i1+1);  
		  dv=dv.toUpperCase();  
		  nu=rut.substr(0,i1);  
		  cnt=0;  
		  suma=0;
		  
		  for (i=nu.length-1; i>=0; i--){
			dig=nu.substr(i,1);
			fc=cnt+2;
			suma += parseInt(dig)*fc;
			cnt=(cnt+1) % 6;
		  }
		  
		  dvok=11-(suma%11);
		  if (dvok==11) dvokstr="0";
		  if (dvok==10) dvokstr="K";
		  if ((dvok!=11) && (dvok!=10)) dvokstr=''+dvok;
		  if (dvokstr==dv) 	return true;
		  else{return false;}
	}


function esrut1(rut)
{
	if ( rut.value == "" || !rutChk2(rut.value))
			{
				 errores[n] = "Por favor ingresa tu RUT en forma correcta\nEj. 123456789-K";
				 n++;
			}
}
























//------------------------------------------FINALIZA LA VALIDACION-------------------------------------------------  
function finform(){
  	if (n>0)
	{
    mensaje = "Se han encontrado los siguientes errores:\n\n";
    for (j=0; j<n; j++)
    	{
    	mensaje = mensaje + (j+1)+".- " + errores[j]+"\n";
    	}
    	alert (mensaje);
    	return false;
  	}
}
