//this function checks if an email have a right syntax
function validateMail(string){
	if(string != ""){
		//try to split email by @
		email_arr = string.split("@");
		//check if array have two elements and they are not empty
		if(email_arr.length != 2 || (email_arr[0] == "" || email_arr[1] == "")){
			return false;
		}
		//check if chars before @ are legal
		if(!checkChars(email_arr[0])){
			return false;
		}
		//split domain by .
		domain_arr = email_arr[1].split(".");
		//check if domain prefix haves two or three letters
		if(domain_arr[domain_arr.length-1].length != 2 && domain_arr[domain_arr.length-1].length != 3){
			return false;
		}
		//check if domain elements have ilegal chars
		for(z=0; z<domain_arr.length; z++){
			if(!checkChars(domain_arr[z])){
				return false;
				break;
			}
			//check if there are any empty element, this means that the user had typed ..
			if(domain_arr[z] == ""){
				return false;
				break;
			}
		}
	} else {
		return false;
	}
	return true;
}

//this function checks if a string is legal
function checkChars(string){
	var legal_chars = Array("a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z", "0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "_", "-", ".");
	string_arr = string.split("");
	for(i=0; i<string_arr.length; i++){
		for(x=0; x<legal_chars.length; x++){
			if(string_arr[i] == legal_chars[x]){
				founded = true;
				break;
			} else {
				founded = false;
			}
		}
		if(founded == false){
			return false;
		}
	}
	return true;
}
