var mistring;

var mensaje = " ";

var primero = " ";

function Valida_Correo(email) {

/*    if (required==undefined) {   // if not specified, assume it's required

        required=true;

    }

    if (email==null) {

        if (required) {

            return false;

        }

        return true;

    }

    if (email.length==0) {  

        if (required) {

            return false;

        }

        return true;

    }*/

    if (! allValidChars(email)) {  // check to make sure all characters are valid

        return false;

    }

    if (email.indexOf("@") < 1) { //  must contain @, and it must not be the first character

        return false;

    } else if (email.lastIndexOf(".") <= email.indexOf("@")) {  // last dot must be after the @

        return false;

    } else if (email.indexOf("@") == email.length) {  // @ must not be the last character

        return false;

    } else if (email.indexOf("..") >=0) { // two periods in a row is not valid

	return false;

    } else if (email.indexOf(".") == email.length) {  // . must not be the last character

	return false;

    }

    return true;

}



function allValidChars(email) {

  var parsed = true;

  var validchars = "abcdefghijklmnñopqrstuvwxyz0123456789@.-_";

  for (var i=0; i < email.length; i++) {

    var letter = email.charAt(i).toLowerCase();

    if (validchars.indexOf(letter) != -1)

      continue;

    parsed = false;

    break;

  }

  return parsed;

}

function valida_tel(t)

{

	var miRegExp = /\d\d-\d\d\d-/;

	return !(miRegExp.test(t));	

}





function valida_texto(x)

{
	var miRegExp = /[^a-z\ñ\á\é\í\ó\ú ]/i;

	return !(miRegExp.test(x));	

}





function valida_num(n)

{

	var invalido = false;

	var ch;

	for (k=0; k < n.length; k++)

		{

		 ch = n.charAt(k);

		 if (ch < "0" || ch > "9"){

			 invalido = true;

		 }			

		}

		return invalido;

}

/*function Valida_Clave(cla, clar) {

	var literalcla = cla.name;

	var largocla = literalcla.length;

	var nombrecla = cla.name.substring(2,largocla);

	var literalclar = clar.name;

	var largoclar = literalclar.length;

	var nombreclar = clar.name.substring(2,largoclar);

	var valida2cla = cla.name.substring(2,1);

	var valida2clar = clar.name.substring(2,1);

	if (valida2cla == "c" && validaclar =="c") {

		if (cla.value.length < 4 || cla.value.length > 8) {

				mensaje += "- " + nombrecla  + " invalido minimo 4 y maximo 8\n";

		}

		if (clar.value.length < 4 || clar.value.length > 8) {

				mensaje += "- " + nombreclar  + " invalido minimo 4 y maximo 8\n";

		}

		if (cla.value != clar.value) {

				mensaje += "- " + " las claves deben ser iguales\n";

		}			

	}

}*/

function ValidaCampo(campo) {

var literal = campo.name;

var largo = literal.length;

var nombre = campo.name.substring(2,largo);

var valida1  = campo.name.substring(0,1);

var valida2 = campo.name.substring(2,1);



if (valida1=="1"  && campo.value.length ==0){

	if (primero == " ") {

		primero = campo.name;

	}

	mensaje += "- " + nombre + "\n";

}

if (campo.value != "") {

	if ((valida2 == "t") && (valida_texto(campo.value) == false)){

		if(primero == " "){

			primero = campo.name;

		}	  

	mensaje += "- " + nombre + " '" + campo.value + "'"+ " invalido (solo letras)\n";

	}

	if ((valida2 == "n") && (valida_num(campo.value) == true)){

		if(primero == " "){

			prinero = nombre;	

		}   

	mensaje += "- " + nombre + " '" + campo.value + "'" + " invalido(solo numeros)\n";

	}

	if ((valida2 == "p") && (valida_tel(campo.value) == true)){

		if(primero == " "){

			prinero = nombre;	

		}   

	mensaje += "- " + nombre + " '" + campo.value + "'" + " invalido(solo xx-xxx-xxxxxxxx)\n";

	}



	if ((valida2 == "e") && (Valida_Correo(campo.value) == false)){

		if(primero == " "){

			prinero = nombre;	

		}   

	mensaje += "- " + nombre + " '" + campo.value + "'" + " invalido(solo nombre@dominio)\n";

	}

	if ((valida2 == "c") && (campo.value.length < 4 || campo.value.length > 8) ){

		mensaje += "- " + nombre +  " invalido(minimo 4 maximo 8)\n";

	}	

}

}

function ValidaForma(forma){

var returnValue = true; 

var i;

if (document.images){

	for (i=0;i<forma.length;i++){

		var elementoForma = forma.elements[i];

		if (elementoForma.type == "text" || elementoForma.type == "textarea" || elementoForma.type=="password") {

 			ValidaCampo(elementoForma);

		}

		if (elementoForma.selectedIndex ==-1){

			mensaje += "- " + " Debe Seleccionar " + elementoForma.name + "\n";

		}

		

	}	

}

/*if (clave && claver) {

	Valida_Clave(clave, claver);

} */

 if (mensaje != " "){

   mensaje = "DEBE INGRESAR LOS CAMPOS:\n" + mensaje;

   alert(mensaje);

   mensaje = " ";

//   eval("forma." + primero + ".focus()");

   returnValue = false;

 }

return returnValue;

}
