/**
 * Validate all of the elements in a given form according to their
 * specified requirements.
 * Field IDs/names must be of the format
 * 		[req_][type_]name
 * req_ Denotes the field is required.
 * type_ Signifies the type of checking to be employed and can be one of
 *		dat_ Date.
 *		eml_ Email address.
 * 		num_ Numeric.
 * 		txt_ Regular text.
 *		url_ URL.
 * @param form frm The form whose fields are to be validated.
 * @return Whether the fields validated.
 */
function validate(frm)
{
	var formValid = true;
	var elementValid = true;
	// Find each input element on the form.
	$(frm).find("input").each(function(){
		// If the field is required or has been completed, validate it.
		if ($(this).attr("id").search("req_") == 0 || IsFilled($(this).attr("value")))
		{
			// Reset the element.
			elementValid = true;
			// If the field has been validated before, remove the error class.
			$(this).removeClass();
			if ($(this).attr("id").search("dat_") != -1)
			{
				elementValid = IsDate($(this).attr("value"));
			}
			else if ($(this).attr("id").search("num_") != -1)
			{
				elementValid = IsNumeric($(this).attr("value"));
			}
			else if ($(this).attr("id").search("eml_") != -1)
			{
				elementValid = IsEmail($(this).attr("value"));
			}
			else if ($(this).attr("id").search("url_") != -1)
			{
				elementValid = IsURL($(this).attr("value"));
			}
			else
			{
				elementValid = IsFilled($(this).attr("value"));
			}				
			// If the element is invalid, add the error class and invalidate the form.
			if (!elementValid)
			{
				formValid = false;
				$(this).addClass("form_error");
			}
			else
			{
				$(this).addClass("text");
			}
		}
	});
	
	// Find all select elements.
	$(frm).find("select").each(function(){
		if ($(this).attr("id").search("req_") == 0)
		{
			$(this).removeClass();
			if (this.selectedIndex == 0)
			{
				formValid = false;
				$(this).addClass("form_error");
			}
		}
	});
		
	// Find each textarea element on the form.
	$(frm).find("textarea").each(function(){
		// If the field is required or has been completed, validate it.
		if ($(this).attr("id").search("req_") == 0 || IsFilled($(this).attr("value")))
		{
			// Reset the element.
			elementValid = true;
			// If the field has been validated before, remove the error class.
			$(this).removeClass();
			elementValid = IsFilled($(this).attr("value"));
			// If the element is invalid, add the error class and invalidate the form.
			if (!elementValid)
			{
				formValid = false;
				$(this).addClass("form_error");
			}
		}
	});
	
	// Warn on errors.
	if (!formValid)
	{
		alert("Unable to Proceed", "Your submission contained errors. Please correct the highlighted fields.");
	}
	return formValid;
}

/**
 * Checks if a supplied string is a valid email address.
 * @param string str The address to check.
 * @return Whether the address was valid.
 */
function IsEmail(str)
{
	if (IsFilled(str))
	{
		var email = /^([\w]+)(.[\w]+)*@([\w]+)(.[\w]{2,4}){1,3}$/;
		return email.test(str);
	}
	else
	{
		return false;
	}
}

/**
 * Checks if a supplied string contains data.
 * @param string str The string to check.
 * @return True if string contained data.
 */
function IsFilled(str)
{
	if (str == "" || str == null)
	{
		return false;
	}
	else
	{
		return true;
	}
}

/**
 * Checks if a supplied string is numeric.
 * @param string sText The string to check.
 * @return Whether the string was numeric.
 */
function IsNumeric(sText)
{
	var ValidChars = "0123456789.";
	var IsNumber = true;
	var Char;
	
	if (IsFilled(sText))
	{
		for (i = 0; i < sText.length && IsNumber == true; i++) 
		{ 
			Char = sText.charAt(i); 
			if (ValidChars.indexOf(Char) == -1) 
			{
				IsNumber = false;
			}
		}
	}
	else
	{
		IsNumber = false;
	}
	return IsNumber;
}

/**
 * Checks if a supplied string is a valid URL.
 * @param string str The URL to check.
 * @return Whether the URL was valid.
 */
function IsURL(str)
{
	if (IsFilled(str))
	{
		var url = /^(http:\/\/www.|https:\/\/www.|ftp:\/\/www.|www.){1}([\w]+)(.[\w]+){1,2}\/$/;
		return url.test(str);
	}
	else
	{
		return false;
	}
}

/**
 * Checks if a supplied string is a valid date (##-MMM-####)
 @param string str The date to check.
 @return Whether the date was valid.
 */
function IsDate(str)
{
	if (IsFilled(str))
	{
		var date = /^(\d){2}\/(\d){2}\/(\d){4}$/;
		return date.test(str);
	}
	else
	{
		return false;
	}
}