<!--
/*
  -------------------------------------------------------------------------------
 |  The content of this file constitutes Licensed Code.                          |
 |  Copyright (C) 2005-2007 Azalea Technology, LLC. All rights reserved.         |
 |-------------------------------------------------------------------------------|
 |  Unauthorized removal of this notice is considered a violation of the         |
 |  license agreement under which this Code may be used. This work is protected  |
 |  under United States copyright law and the similar law(s) of other countries  |
 |  under which such as work is afforded legal protection, and upon conviction   |
 |  of such a violation in a court of applicable jurisdiction, such person(s)    |
 |  may be subject to the maximum allowable penalty as permitted under such law. |
 |-------------------------------------------------------------------------------|
 |  You acknowledge and agree that information presented to you through this     |
 |  site (the "Web Site") is protected by all applicable copyrights, trademarks, |
 |  service marks, patents or other proprietary rights and laws, and by virtue   |
 |  of accessing the Web Site, except as expressly authorized by the Azalea      |
 |  Technology, LLC., you agree not to modify, rent, lease, loan, sell,          |
 |  distribute, store, or create derivative works based on the Web Site, in      |
 |  whole or in part.                                                            |
  -------------------------------------------------------------------------------|
 |  Decrypting or otherwise decoding the following programming language code is  |
 |  strictly prohibited except as expressly authorized by Azalea Technology,     |
 |  LLC. Upon conviction of such a violation in a court of applicable            |
 |  jurisdiction, such person(s) may be subject to the maximum allowable penalty |
 |  as permitted under such law.                                                 |
  -------------------------------------------------------------------------------
         Purpose: Form/field-independent field validation
      Programmer: Benjamin Roberts
                  Azalea Technology, LLC.
                  P.O. Box 131150
                  Tyler, TX 75713-1150
  -------------------------------------------------------------------------------
*/

// Unobtrusive implementation
// Do everything in this one anonymous function to avoid cluttering the global namespace
(function(){

	function validate_field(form_obj, form_obj_element){

		var suppress_warnings = false;
		var validate = false;
		var is_required = false;
		var expected_format = "";
		var is_valid = false;
		var error_msg = "";
		var visualcue = false;
		var setcase = null;

		// Check to make sure the target form exists
		if(!form_obj){
			alert("FORM ERROR: The form supplied does not exist. Please check the form name.");
			return false;
		}

		// Check to see if the form element argument exists
		if(!form_obj_element){
			alert("FORM ERROR: The form element supplied does not exist. Please check the form element name.");
			return false;
		}

		// Check to see if the Web developer has given a directive to suppress warning messages; warning conditions
		// are conditions that are not essential to the execution of the script, but may inhibit certain beneficial
		// functionality such as visual validation, etc.
		if(form_obj._config_javascript_suppress_warnings){

			suppress_warnings = form_obj._config_javascript_suppress_warnings.value;

			// Check to make sure input is an expected value
			if(suppress_warnings != "" && suppress_warnings != null){

				// Force attribute value to lowercase before checking value
				suppress_warnings = suppress_warnings.toLowerCase();

				if(suppress_warnings == "true")
					suppress_warnings = true;
				else
					suppress_warnings = false;
			}
			else{
				suppress_warnings = false;
			}
		}
		else suppress_warnings = false;

		// Check to see if the form element argument ID exists
		if(!suppress_warnings){
			if(!form_obj_element.id){
				alert("FORM ERROR: The form element \"" + form_obj_element.name + "\" supplied does not have an \"id\" attribute. Please check the form element.");
			}
		}

		// Read value of custom tag attribute indicating if validation should be performed
		validate = form_obj_element.getAttribute("_validate");

		// Check the attribute value
		if(validate != "" && validate != null){

			// Force attribute value to lowercase before checking value
			validate = validate.toLowerCase();

			if(validate=="true")
				validate = true;
			else
				validate = false;
		}
		else{
			validate = false;
		}

		// If directive given to validate the field, do so
		if(validate){

			// Check to see if field is required
			is_required = form_obj_element.getAttribute("_required")

			// Check to make sure input is an expected value
			if(is_required != "" && is_required != null){

				// Force attribute value to lowercase before checking value
				is_required = is_required.toLowerCase();

				if(is_required == "true"){
					is_required = true;
				}
				else{
					is_required = false;
				}

			}
			else{
				is_required = false;
			}

			// Get the type of value that we're expecting
			expected_format = form_obj_element.getAttribute("_expectedformat");

			// Check the expected format for an acceptable value to the program
			if(expected_format != "" && expected_format != null){

				// Force attribute value to lowercase before checking value
				expected_format = expected_format.toLowerCase();

				switch(expected_format){
					case "numeric":
					case "alphanumeric":
						break;

					case "string":
					case "text":
						expected_format = "string";
						break;

					case "email":
					case "email address":
						expected_format = "email";
						break;

					case "phone":
					case "phone number":
					case "telephone":
					case "telephone number":
						expected_format = "phone";
						break;

					case "zip":
					case "zip code":
					case "postal code":
						expected_format = "zip";
						break;

					case "dob":
					case "date of birth":
					case "birthday":
						expected_format = "dob";
						break;

					case "date":
						expected_format = "date";
						break;

					case "currency":
					case "money":
					case "usd":
						expected_format = "currency";
						break;

					default:
						expected_format = "string";
						break;
				}
			}
			else{
				expected_format = "string";
			}

			// Check to see if field is required
			visualcue = form_obj_element.getAttribute("_visualcue")

			// Check to make sure input is an expected value
			if(visualcue != "" && visualcue != null){

				// Force attribute value to lowercase before checking value
				visualcue = visualcue.toLowerCase();

				if(visualcue == "true"){
					visualcue = true;
				}
				else{
					visualcue = false;
				}

			}
			else{
				visualcue = false;
			}

			// Check to see if field value's case should be transformed
			setcase = form_obj_element.getAttribute("_setcase")

			// Check to make sure input is an expected value
			if(setcase != "" && setcase != null){

				// Force attribute value to lowercase before checking value
				setcase = setcase.toLowerCase();

				switch(setcase){
					case "u":
					case "uc":
					case "up":
					case "upper":
					case "uppercase":
					case "upper case":
						setcase = "upper";
						break;

					case "l":
					case "lc":
					case "low":
					case "lower":
					case "lowercase":
					case "lower case":
						setcase = "lower";
						break;

					case "p":
					case "pc":
					case "proper":
					case "propercase":
					case "proper case":
						setcase = "proper";
						break;

					default:
						setcase = "none";
						break;
				}
			}
			else{
				setcase = "none";
			}

			// Validate field if field
			switch(form_obj_element.type){

				case "text":
				case "password":
				case "textarea":

					switch(expected_format){
						case "numeric":
							// Nothing at this time
							break;
						case "alphanumeric":
							// Nothing at this time
							break;
						case "string":
							if(is_required){
								is_valid = !isSpace(form_obj_element.value);
							}
							else{
								is_valid = true;
							}
							break;
						case "email":
							if(is_required){
								is_valid = validEmail(form_obj_element.value);
							}
							else{
								is_valid = ( isSpace(form_obj_element.value) || validEmail(form_obj_element.value) );
							}
							break;
						case "phone":
							if(is_required){
								is_valid = validTelephoneNumber(form_obj_element.value);
							}
							else{
								is_valid = ( isSpace(form_obj_element.value) || validTelephoneNumber(form_obj_element.value) );
							}
							break;
						case "zip":
							if(is_required){
								is_valid = isValidPostalCode("United States", form_obj_element.value);
							}
							else{
								is_valid = ( isSpace(form_obj_element.value) || isValidPostalCode("United States", form_obj_element.value) );
							}
							break;
						case "dob":
							
							break;
						case "date":
							if(is_required){
								is_valid = validDate(form_obj_element.value);
							}
							else{
								is_valid = ( isSpace(form_obj_element.value) || validDate(form_obj_element.value) );
							}
							break;
						case "currency":
							if(is_required){
								is_valid = !isNaN(parseFloat(form_obj_element.value));
							}
							else{
								is_valid = ( isSpace(form_obj_element.value) || ( !isNaN(parseFloat(form_obj_element.value)) ) );
							}
							break;
						default:
							if(is_required){
								is_valid = !isSpace(form_obj_element.value);
							}
							else{
								is_valid = true;
							}
							break;
					}

					if(!is_valid){

						if(visualcue) set_class_name(form_obj_element.id, "textbox_error");

						// Read error message from the _errormsg attribute of the field
						error_msg_scrn = form_obj_element.getAttribute("_errormsg");

						// Write error message to the message container
						write_text_to_element(form_obj_element.name+"_msg_box", error_msg_scrn);
					}
					else{
						if(visualcue) set_class_name(form_obj_element.id, "textbox");

						// Transform case if directive given to do so
						switch(setcase){
							case "upper":
								form_obj_element.value = form_obj_element.value.toUpperCase();
								break;
							case "lower":
								form_obj_element.value = form_obj_element.value.toLowerCase();
								break;
							case "proper":
								form_obj_element.value = toProperCase(form_obj_element.value);
								break;
						}

						// Clear the error message container
						write_text_to_element(form_obj_element.name+"_msg_box", "&nbsp;");
					}
					break;
			}
		}
		return is_valid;
	}

	// Assign appropriate "onblur" event handler for all form elements within the form requiring validation
	function validate_field_initialize(){

		// Local variables
		var d = document;
		var v_form = null;
		var v_field = null;

		// Loop through all the forms within the document
		for(var x = 0; d.forms[x]; x++){

			// Get the value of custom, non-standard FORM tag attribute named _validate
			v_form = d.forms[x].getAttribute("_validate");

			// Check _validate attribute value to see if form elements should even be considered
			if(v_form != "" && v_form != null){
				v_form = v_form.toLowerCase();

				switch(v_form){
					case "true":
					case "yes":
					case "1":
					case "validate":

						// Shortcut to the elements array of the current form
						e = d.forms[x].elements;

						// Loop through all the elements of the current form
						for(var y = 0; y < e.length; y++){

							// Determine the type of form element being processed and select only certain types
							switch(e[y].type){
								case "text":
								case "textarea":
								case "file":
								case "password":

									v_field = e[y].getAttribute("_validate");

									if(v_field != "" && v_field != null){
										v_field = v_field.toLowerCase();

										// Determine if validation was requested on the current form element
										switch(v_field){
											case "true":
											case "yes":
											case "1":
											case "validate":

												realtime = e[y].getAttribute("_realtime");

												// Determine if "realtime" validation was requested on the current form element and, if so,
												// add "onblur" event handler
												if(realtime != "" && realtime != null){
													realtime = realtime.toLowerCase();

													switch(realtime){
														case "true":
														case "yes":
														case "1":
														case "validate":
														case "realtime":
															Handler.add(e[y], "blur", function(){ validate_field(this.form, this); });
															break;
													}
												}
												break;
										}
									}

									break;
							}
						}
						break;
				}
			}

		}
	}

	// Add initialization to onload event handler (execute after document is loaded)
	Handler.add(window, "load", function(){ validate_field_initialize(); });

})();

//-->
