/*
$LOG 09/21/04 - Ed re-wrote the function because the original was not that good v1.0.1
$LOG 10/19/04 - Ed changed the return function to return in different formats v1.0.2
$LOG 10/27/04 - Ed corrected some syntax errors v1.0.3
$LOG 05/19/06 - Matt added getElementId syntax, you must pass 'by_id' in the 'format' variable

version_number = "1.0.3";
*/

function CheckValidEmailFormat(formid, elementid, format){
	var result = new Array(2);
	
	if(format == "by_id"){
		email = document.getElementById(elementid);
		email = email.value;
	}else{
		email = eval("document."+formid+"."+elementid+".value");
	}
	// Get the location of the @ sign
	at_index = email.indexOf("@");

	if(at_index > 0){
		// The email address has an @ sign so now check for a .
		domain = email.split("@");

		if(domain.length == 2){
			// Determine the number of periods we have in the domain
			domain_sections = domain[1].split(".");
			if(domain_sections.length > 1){
				// We need at least 2 parts to make a valid domain and we have it
				// Now check to see if the domain extension has at least 2 characters
				if(domain_sections[domain_sections.length-1].length > 1){
					// The domain extension has at least 2 characters
					if(domain_sections[0].length > 0){
						// The domain has at least 1 character
						result['result'] = 1;
						result['comment'] = "Valid format";
					} else {
						// Domain has less than 1 character
						alert("Error in email address. Domain missing.\nPlease enter a valid email address to submit the form\n\nThe format should be\nusername@domain.extension");
						result['result'] = 0;
						result['comment'] = "Domain missing";
					}
				} else {
					// Domain extension has less than 2 letters
					alert("Error in email address. Domain extension has less than 2 letters.\nPlease enter a valid email address to submit the form\n\nThe format should be\nusername@domain.extension");
					result['result'] = 0;
					result['comment'] = "Domain extension has less than 2 letters";
				}
			} else {
				// Missing parts in domain
				alert("Error in email address. Missing parts in domain.\nPlease enter a valid email address to submit the form\n\nThe format should be\nusername@domain.extension");
				result['result'] = 0;
				result['comment'] = "Missing parts in domain";
			}
		} else {
			// More than 1 @ sign found
			alert("Error in email address. More than 1 @ sign found.\nPlease enter a valid email address to submit the form\n\nThe format should be\nusername@domain.extension");
			result['result'] = 0;
			result['comment'] = "More than 1 @ sign found";
		}
	} else {
		// @ sign not found
		alert("Error in email address. No @ sign found.\nPlease enter a valid email address to submit the form\n\nThe format should be\nusername@domain.extension");
		result['result'] = 0;
		result['comment'] = "No @ sign found";
	}
	
	if(format != "array"){
		if(result['result'] == 1)
			return true;
		else
			return false;
	} else {
		return result;
	}
}

