
//----------------------------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="";
var campos="";

//------------------------------------------LARGO MINIMO-------------------------------------------------
function largominimo(texto,largo){
	if (texto.value.length < largo){
		errores[n] = "El campo "+texto.name.toUpperCase()+" debe tener mas de "+largo+" carcteres";
		n++;
	}
}


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


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


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

	
//------------------------------------------VERIFICACION DE PAGINA WEB-------------------------------------------------	
function esweb(web){
	web=web.value;
		if ((web.indexOf('.') == -1) || (web.length < 4)){
			errores[n] = "La pgina web ingresada no es vlida";
			n++;
			}
	}	
	
	
//------------------------------------------VERIFICACION DE CLAVE IGUAL-------------------------------------------------	
function claveigual(p1,p2){
	c1=p1.value;
	c2=p2.value;	
		if (c1 != c2){
			errores[n] = "La claves ingresadas no son iguales.";
			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";		 
			campos=campos + form.elements[e].name + " ";
		}			
	}
		
	if (error=="si"){
		errores[n] = "Todos los campos deben ser completados ("+campos+")";
		n++;	
		error="no"
			campos="";
			
	}	
}


//------------------------------------------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] = "Ningn campo debe contener comillas, ni simples, ni dobles.";
		n++;	
		error="no"
	}		
}


//------------------------------------------VERIFICACION DE RUT-------------------------------------------------
function esrut(ruti,digi)
  	{
  	//alert("validandorut");
	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++;
      }
    }
}	
		

//------------------------------------------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;
  	}
}







