// JavaScript Document
/*
F: yzak_email_isValidEmail( emailchk ) // Devuelve true si lo considera un email correcto
	F: yzak_email_allValidChars(email) // Devuelve true si todos las letras de una cadena forman parte de un email
F: yzak_email_trim( string ) // --> devuelve una cadena sin espacios a los lados


*/

// -------------------------------------------------------------------------------------------------------
function yzak_email_isValidEmail( emailchk ) {
	var email =  new String( emailchk );
	
	if ( email == null  ) { return false; }
	if ( email.length < 5 ) { return false; }
	if ( ! yzak_email_allValidChars( email ) ) { return false; } // check to make sure all characters are valid
	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;
	}
	return true;
}

// -------------------------------------------------------------------------------------------------------
function yzak_email_allValidChars( email ) {
	var parsed = true;
	var validchars = "abcdefghijklmnopqrstuvwxyz0123456789@.-_";
	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;
}

// Elimina los espacios de una cadena
// -------------------------------------------------------------------------------------------------------
function yzak_email_trim( string ) {
	if ( string ) { return string.replace(/^\s*|\s*$/g, ""); } else { return ""; } // Si la cadena no esta vacia
}

// Dependencias: trim
// Comprueba si una direccion de correo es correcta
// -------------------------------------------------------------------------------------------------------
function isValidEmailAddress(emailAddress) {
	// Si es una expresion regular no funciona
	if(!new RegExp(/^test$/).test("test")) { return true; }

	// Si la direccion esta vacia
	if(yzak_email_trim(emailAddress) == "") {
			return false;
	} else {
			return new RegExp(/^[^@]+@[^@]+\.[^@]{2,}$/).test(trim(emailAddress));
	}
}

// -------------------------------------------------------------------------------------------------------
function isValidEmail( email, required ) {
	if ( required == undefined ) { required = true; } // Si no se especifica, se asume que es requerido
	
	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;
	}
	return true;
}

// -------------------------------------------------------------------------------------------------------
function allValidChars(email) {
	var parsed = true;
	var validchars = "abcdefghijklmnopqrstuvwxyz0123456789@.-_";
	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 esEmail(cadena,obligatorio,nombre_campo,mensajes) {
	valor=trim(cadena);
	if (mensajes==1) {msj=true;} else {msj=false;}
	if (obligatorio==1){if(valor==""){if (msj){alert("El Campo \""+nombre_campo+"\" es obligatorio.");}return (false);}}
	if ((valor.indexOf(" ") != -1) ||(valor.indexOf("	") != -1))  {if (msj){alert("El Campo \""+nombre_campo+"\" no es correcto.");} return (false);} // tiene " " o "	"
	if ((valor.indexOf("@.") != -1) ||(valor.indexOf(".@") != -1))  {if (msj){alert("El Campo \""+nombre_campo+"\" no es correcto.");} return (false);} // tiene "@." o ".@"
	if (valor.indexOf("@") == -1 || valor.lastIndexOf(".") == -1) {if (msj){alert("El Campo \""+nombre_campo+"\" no es correcto.");} return (false);} // debe tener "@" y "."
	if (valor.lastIndexOf(".") < valor.indexOf("@")) {if (msj){alert("El Campo \""+nombre_campo+"\" no es correcto.");} return (false);} //debe haber algun punto despues de la "@"
	if ( valor.indexOf(".")==0 || (valor.lastIndexOf(".")+1)==valor.length) {if (msj){alert("El Campo \""+nombre_campo+"\" no es correcto.");} return (false);} //hay un "." al principio o al final
	if ( valor.indexOf("@")==0 || (valor.lastIndexOf("@")+1)==valor.length) {if (msj){alert("El Campo \""+nombre_campo+"\" no es correcto.");} return (false);} //hay un "@" al principio o al final
	trozos=valor.split("@");
	trozos2=trozos[1].split(".");
	if (trozos2[trozos2.length-1].length>4 || trozos2[trozos2.length-1].length<2 ){if (msj){alert("El Campo \""+nombre_campo+"\" no es correcto.");} return (false);} // el dominio es demasiado largo o corto
	return (true);
}
