﻿/* Form Validation routines */
if (Validator == null || typeof(Validator) != "object") { var Validator = new Object(); }

Validator.ClearFieldError = function(element) {
    ///<summary>Clears error properties from a field</summary>
    ///<param name="element">Field (or id) to remove error properties from</param>
    ///<returns>Returns the element</returns>
    return $(element).removeClassName("error");
}
Validator.CheckRequired = function(element, errorMessage, errorLabel) {
	///<summary>Checks if a required field is present. If not, displays an error message</summary>
	///<param name="element">The element or id of the field to check</param>
	///<param name="errorMessage">Error messsage to display if validation fails</param>
	///<param name="errorLabel">Element (or id) to display the error message on</param>
	///<returns type="Boolean">True if validation succeeds; otherwise false</returns>
	element = Validator.ClearFieldError(element);
	if (element.type == "text" || element.type == "password") {
		if (!element.present()) {
			Validator.ShowError(element, errorLabel, errorMessage);
			return false;
		}
	} else if (element.type == "checkbox" || element.type == "radio") {
		if (!element.checked) {
			Validator.ShowError(element, errorLabel, errorMessage);
			return false;
		}
	}
	
	return true;
}
Validator.CheckEqual = function(element, compareTo, errorMessage, errorLabel) {
	///<summary>Validates that two fields contain the same value</summary>
	///<param name="element">The field (or id) to check</param>
	///<param name="compareTo">The field (or id) to compare the value to</param>
	///<param name="errorMessage">Error messsage to display if validation fails</param>
	///<param name="errorLabel">Element (or id) to display the error message on</param>
	///<returns type="Boolean">True if validation succeeds; otherwise false</returns>
	element = Validator.ClearFieldError(element);
	compareTo = $(compareTo);
	Validator.ClearFieldError(element);
	if (element.getValue() != compareTo.getValue()) {
		Validator.ShowError(element, errorLabel, errorMessage);
		return false;
	}
	
	return true;
}
Validator.CheckEmail = function(element, errorMessage, errorLabel) {
	///<summary>Validates that the field contains a valid email address</summary>
	///<param name="element">Element (or id) to check</param>
	///<param name="errorMessage">Error messsage to display if validation fails</param>
	///<param name="errorLabel">Element (or id) to display the error message on</param>
	///<returns type="Boolean">True if validation succeeds; otherwise false</returns>
	element = Validator.ClearFieldError(element);
	var filter = /^([\w-]+(?:\.[\w-]+)*)@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/i;
	if (!filter.test(element.getValue())) {
	  Validator.ShowError(element, errorLabel, errorMessage);
	  return false;
	} 
	return true; 
}
Validator.CheckPassword = function(element, options, errorMessage, errorLabel) {
	///<summary>Validates that a field conforms to the rules for a valid password</summary>
	///<param name="element">Element (or id) to check</param>
	///<param name="options">Options object, containing minLength (Minimum length)</param>
	///<param name="errorMessage">Error messsage to display if validation fails</param>
	///<param name="errorLabel">Element (or id) to display the error message on</param>
	///<returns type="Boolean">True if validation succeeds; otherwise false</returns>
	element = Validator.ClearFieldError(element);
	if (element.getValue().length < options.minLength) {
	  Validator.ShowError(element, errorLabel, errorMessage);
	  return false;
	} 
	return true; 
}
Validator.CheckPhoneNumber = function(element, errorMessage, errorLabel) {
	///<summary>Validates a phone number field</summary>
	///<param name="element">Element (or id) to check</param>
	///<param name="errorMessage">Error messsage to display if validation fails</param>
	///<param name="errorLabel">Element (or id) to display the error message on</param>
	///<returns type="Boolean">True if validation succeeds; otherwise false</returns>
	element = Validator.ClearFieldError(element);
	var filter = /(^\d{8,8}$)/;
	if (!filter.test(element.getValue())) {
	  Validator.ShowError(element, errorLabel, errorMessage);
	  return false;
	} 
	return true; 
}
Validator.ShowError = function(element, errorLabel, errorMessage) {
	///<summary>Displays an error message for a specific field</summary>
	///<param name="element">Element (or id) that has the error</param>
	///<param name="errorMessage">Error messsage to display if validation fails</param>
	///<param name="errorLabel">Element (or id) to display the error message on</param>
	if (!errorLabel) return;

	errorLabel = $(errorLabel);
	errorLabel.update(errorMessage);

	element = $(element);		
	if (element) {
		element.addClassName("error");
		element.focus();
	}
	
	Effect.Appear(errorLabel, { duration: 0.3 });
}
Validator.ResetErrorLabel = function(element) {
	///<summary>Resets an error label</summary>
	///<param name="element">The error label (or id)</param>
	$(element).update("").hide();
}