/*
* validation_collection class
* this class slightly differs from the ValidationCollection class in common.js
* the difference prevents fields passed validations to override failed validations if they are validating the same fields
*/
validation_collection = Class.create();
validation_collection.prototype = {
	initialize: function() {
		// member variables
		this.elements = new Array();
		this.ids = new Array();		

		this.alert = arguments[1];
		this.alert_message = arguments[2];
		this.isIE = navigator.userAgent.indexOf("compatible") > -1 && navigator.userAgent.indexOf("MSIE") > -1 && !(navigator.userAgent.indexOf("Opera") > -1)
		
		// validate method
		this.validate = function(required_array) {
			var passed_validation = false;
			var errors_found = 0;
			var focus_id = 0;			
			
			if($('error_div'))
      {
        $('error_div').innerHTML = "";
      }

			// Reset All Styles
			for (var i=0; i<this.elements.length; i++) 
			{
				// If there is just a string passed in, change it into an array
				if(typeof this.elements[i].highlited_id == 'string')
				{
					var tmp_string = new Array();
					tmp_string[0] = this.elements[i].highlited_id;
					this.elements[i].highlited_id = tmp_string;
				}
				
				// Unset any additional elements	(will only work with simple elements)					
				if(this.elements[i].additional_highlited_ids != 0 && this.elements[i].additional_highlited_ids != undefined)
				{
					var additional_length = this.elements[i].additional_highlited_ids.length;
					for (var z=0; z<additional_length; z++)
					{
						if($(this.elements[i].additional_highlited_ids[z]))
							$(this.elements[i].additional_highlited_ids[z]).style.border = "";
					}
				}
				
				for (var j=0; j<this.elements[i].highlited_id.length; j++)
				{
					if($(this.elements[i].highlited_id[j]))
					{
						
						// Reset error message labels
						var default_error_div = this.elements[i].highlited_id[j] + "_error";
						if($(default_error_div))
						{
							$(default_error_div).innerHTML = "";
						}	
						
						if($(this.elements[i].highlited_id[j]).style.border)
							$(this.elements[i].highlited_id[j]).style.border = "1px solid #7F9DB9";
					  
            			//Added by brendan to unhighlight selects
						if (this.isIE)
						{
							if ($(this.elements[i].highlited_id[j]).type == 'select-one' || $(this.elements[i].highlited_id[j]).type == 'select-multiple')
							{
							  $(this.elements[i].highlited_id[j] + '_div').style.border = "";
							}
						}
						
						if(this.elements[i].parent_tab != 0 && $(this.elements[i].parent_tab) != null)
						{							
							if($(this.elements[i].parent_tab).style.backgroundColor == "red")
							{
								$(this.elements[i].parent_tab).style.backgroundColor = "";
							}
						}
					}
				}	
			}		
			
			// validate each element
			for (var i=0; i<this.elements.length; i++) {				
				var passed_validation = this.elements[i].validate(required_array);
				if (!passed_validation) 
				{ 
					for (var j=0; j<this.elements[i].highlited_id.length; j++)
					{
						if (errors_found == 0) { focus_id = this.ids[i]; }
						
						// Mark the element
						if($(this.elements[i].highlited_id[j]) && $(this.elements[i].highlited_id[j]).style.display != 'none')
						{
							$(this.elements[i].highlited_id[j]).style.border = "2px solid #FF0000";	
	
							if (this.isIE)
							{
								if ($(this.elements[i].highlited_id[j]).type == 'select-one' || $(this.elements[i].highlited_id[j]).type == 'select-multiple')
								{
									$(this.elements[i].highlited_id[j] + '_div').style.border = "2px solid #FF0000";
								}
							}
						}
						
						//alert(this.elements[i].additional_highlited_ids);
						// Mark any additional elements	(will only work with simple elements)					
						if(this.elements[i].additional_highlited_ids != 0 && this.elements[i].additional_highlited_ids != undefined)
						{
							for (var z=0; z<this.elements[i].additional_highlited_ids.length; z++)
							{
								if($(this.elements[i].additional_highlited_ids[z]))
									$(this.elements[i].additional_highlited_ids[z]).style.border = "2px solid #FF0000";
							}		
						}
						
						// If the element is within a tab, mark the tab
						if(this.elements[i].parent_tab != 0 && $(this.elements[i].parent_tab) != null)
							$(this.elements[i].parent_tab).style.backgroundColor = "red";
						
						// Where are we going to place the error message
						var default_error_div = this.elements[i].highlited_id[j] + "_error";
						

						// Show errors if asked
						if($(default_error_div))
						{
							// How do we want to display the error message
							if (this.elements[i].show_message == 1 || this.elements[i].show_message == 2) 
							{ 
								$(default_error_div).innerHTML = $(default_error_div).innerHTML="&nbsp;"+this.elements[i].msg;								
							}
						
							if (this.elements[i].show_message == 3) 
							{ 
								$(default_error_div).innerHTML = "&nbsp;<img src='/images/help_icon.gif' width='15' height='14' border='0' alt='' onmouseover=\"aztt(this, event, '" + this.elements[i].msg + "'/>"; 
							}

							if (this.elements[i].show_message == 4)
              {
                if($('error_div'))
                {
                  if(!$('error_div').innerHTML.match(this.elements[i].msg))
                  {
                    $('error_div').innerHTML += this.elements[i].msg + "<br>";
                  }
                }
              }

						}	
					}
					
					errors_found++;
				}
			}
			
			// If there is nothing to check then pass
			if(this.elements.length == 0) { passed_validation = true; }
				
			if (errors_found == 0) {							
				return passed_validation;
			} else {
				if (this.alert) { alert (this.alert_message); }
				//$(focus_id).focus(); // if there are tabs on the page, it is hard to determine which to focus and will fail in IE
				return false;
			}
		}

		// addElement method
		this.addElement = function(id, element) {
			this.ids[this.ids.length] = id;
			this.elements[this.elements.length] = element;
		}
	}
}

/**
 * This method will capture errors returned from ajax error checking
 * @param string id the id of the element in question
 * @param string msg the message associated with the error
 * @param string highlited_id if the element has multiple fields, this one will be highlited on error
 * @param string parent_tab, if the element is within a tab, tab will be highlited if necessary
 */
add_validation_check_standard = Class.create();
add_validation_check_standard.prototype = {
	initialize: function(id, msg, highlited_id, parent_tab, show_message, additional_highlited_ids) {
		// member variables
		this.id = id;
		this.msg = msg;
		this.highlited_id = highlited_id ? highlited_id : id;
		this.additional_highlited_ids = additional_highlited_ids ? additional_highlited_ids : 0;
		this.parent_tab = parent_tab ? parent_tab : 0;
		this.show_message = show_message ? show_message : 0;
		
		// this
		this.validate = function(required_array) {
			var passed_validation = true;			
			for (var j=0; j < required_array.length; j++) {				
				if(required_array[j] == this.id || required_array[j].indexOf(this.id + '_') != -1)
					passed_validation = false;
			}
			
			return passed_validation;
		}
	}
}

/*
* validation_presence_of class
*/
validate_presence_of = Class.create();
validate_presence_of.prototype = {
	initialize: function(field, msg, highlited_id, parent_tab, show_message) {
		// member variables
		this.field = field;
		this.msg = msg;
		this.highlited_id = highlited_id ? highlited_id : field;
		this.parent_tab = parent_tab ? parent_tab : 0;		
		this.show_message = show_message ? show_message : 0;

		// validate method
		this.validate = function() {			
			if ($(this.field).value == '') { return false; } else { return true; } 
		}
	}
}

/*
* validation_presence_of_option class
*/
validation_presence_of_option = Class.create();
validation_presence_of_option.prototype = {
	initialize: function(field, msg, highlited_id, parent_tab, show_message) {
		// member variables
		this.field = field;
		this.msg = msg;
		this.highlited_id = highlited_id ? highlited_id : field;
		this.parent_tab = parent_tab ? parent_tab : 0;
		this.show_message = show_message ? show_message : 0;

		// validate method
		this.validate = function() {
			if ($(this.field).options.length <= 0) { return false; } else { return true; } 
		}
	}
}

/*
* validation_email class 
*/
validate_email = Class.create();
validate_email.prototype = {
	initialize: function(field, msg, highlited_id, parent_tab, show_message) {
		// member variables
		this.field = field;
		this.msg = msg;
		this.regEx = /^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/i;
		this.highlited_id = highlited_id ? highlited_id : field;
		this.parent_tab = parent_tab ? parent_tab : 0;
		this.show_message = show_message ? show_message : 0;

		// validate method
		this.validate = function() {
			return this.regEx.test($(this.field).value);
		}
	}
}

/*
* validation_zipcode class -- UNTESTED
*/
validate_zipcode = Class.create();
validate_zipcode.prototype = {
	initialize: function(field, msg, highlited_id, parent_tab, show_message) {
		// member variables
		this.field = field;
		this.msg = msg;
		this.regEx = /(^\d{5}$)|(^\d{5}-\d{4}$)/;
		this.highlited_id = highlited_id ? highlited_id : field;
		this.parent_tab = parent_tab ? parent_tab : 0;
		this.show_message = show_message ? show_message : 0;

		// validate method
		this.validate = function() {
			return this.regEx.test($(this.field).value); 
		}
	}
}

/*
* validation_postalcode class -- UNTESTED
*/
validate_postalcode = Class.create();
validate_postalcode.prototype = {
	initialize: function(field, msg, highlited_id, parent_tab, show_message) {
		// member variables
		this.field = field;
		this.msg = msg;
		this.regEx = /^[A-Z]\d[A-Z]([\s\-])?\d[A-Z]\d$/i;
		this.highlited_id = highlited_id ? highlited_id : field;
		this.parent_tab = parent_tab ? parent_tab : 0;
		this.show_message = show_message ? show_message : 0;

		// validate method
		this.validate = function() {
			return this.regEx.test($(this.field).value);
		}
	}
}

/*
* validation_telephone class -- UNTESTED
*/
validate_telephone = Class.create();
validate_telephone.prototype = {
	initialize: function(field, msg, highlited_id, parent_tab, show_message) {
		// member variables
		this.field = field;
		this.msg = msg;
		this.regEx = /^(\+\d{1,3}?)? (\(\d{2,5}\)|\d{2,5}) ?\d{3}( -)?\d{0,7}$/;
		this.highlited_id = highlited_id ? highlited_id : field;
		this.parent_tab = parent_tab ? parent_tab : 0;
		this.show_message = show_message ? show_message : 0;

		// validate method
		this.validate = function() {
			return this.regEx.test($(this.field).value);
		}
	}
}

/*
* validation_is_greater_than class
*/
validate_is_greater_than = Class.create();
validate_is_greater_than.prototype = {
	initialize: function(field, column2, msg, highlited_id, parent_tab, show_message) {
		// member variables
		this.field = field;
		this.column1 = this.field;
		this.column2 = column2;
		this.msg = msg;
		this.highlited_id = highlited_id ? highlited_id : field;
		this.parent_tab = parent_tab ? parent_tab : 0;
		this.show_message = show_message ? show_message : 0;

		// validate method	
		this.validate = function() {
			if ( Number($(this.column1).value) > Number($(this.column2).value)) { return true; }
			else { return false; }
		}
	}
}

/*
* validation_is_less_than class
*/
validate_is_less_than = Class.create();
validate_is_less_than.prototype = {
	initialize: function(field, column2, msg, highlited_id, parent_tab, show_message) {
		// member variables
		this.field = field;
		this.column1 = this.field;
		this.column2 = column2;
		this.msg = msg;
		this.highlited_id = highlited_id ? highlited_id : field;
		this.parent_tab = parent_tab ? parent_tab : 0;
		this.show_message = show_message ? show_message : 0;

		// validate method
		this.validate = function() {
			if ($(this.column1).value < $(this.column2).value) { return true; }
			else { return false; } 
		}
	}
}

/*
* validation_is_equal_to class
*/
validate_is_equal_to = Class.create();
validate_is_equal_to.prototype = { 
	initialize: function(field, field2, msg, highlited_id, parent_tab, show_message) {
		// member variables 
		this.field = field;
		this.field2 = field2;
		this.msg = msg;
		this.highlited_id = highlited_id ? highlited_id : field;
		this.parent_tab = parent_tab ? parent_tab : 0;
		this.show_message = show_message ? show_message : 0;

		// validate method
		this.validate = function() {
			if ($(this.field).value == $(this.field2).value) { $(this.field2).className = "FormInputStyle"; return true; }
			else { $(this.field2).className = "FormInputStyleAlert"; return false; } 
		}
	}
}

/*
* validation_is_integer class
*/
validate_is_integer = Class.create();
validate_is_integer.prototype = {
	initialize: function(field, msg, highlited_id, parent_tab, show_message) {
		// member variables
		this.field = field;
		this.msg = msg;
		this.regEx = /^((\+|-)\d)?\d*$/;
		this.highlited_id = highlited_id ? highlited_id : field;
		this.parent_tab = parent_tab ? parent_tab : 0;
		this.show_message = show_message ? show_message : 0;

		// validate method
		this.validate = function() {
			return this.regEx.test($(this.field).value);
		}
	}
}

/*
* validation_is_double class
*/
validate_is_double = Class.create();
validate_is_double.prototype = {
	initialize: function(field, msg, highlited_id, parent_tab, show_message) {
		// member variables
		this.field = field;
		this.msg = msg;
		this.regEx = /^((\+|-)\d)?\d*(\.\d+)?$/;
		this.highlited_id = highlited_id ? highlited_id : field;
		this.parent_tab = parent_tab ? parent_tab : 0;
		this.show_message = show_message ? show_message : 0;

		// validate method
		this.validate = function() {
			return this.regEx.test($(this.field).value);
		}
	}
}

/*
* validation_format_of class
*/
validate_format_of = Class.create();
validate_format_of.prototype = {
	initialize: function(field, regEx, msg, highlited_id, parent_tab, show_message) {
		// member variables 
		this.field = field;
		this.regEx = regEx;
		this.msg = msg;
		this.highlited_id = highlited_id ? highlited_id : field;
		this.parent_tab = parent_tab ? parent_tab : 0;
		this.show_message = show_message ? show_message : 0;

		// validate method
		this.validate = function() {
			return this.regEx.test($(this.field).value);
		}
	}
}

/*
* validation_not_negative
*/
validate_not_negative = Class.create();
validate_not_negative.prototype = {
	initialize: function(field, msg, highlited_id, parent_tab, show_message) {
		// member variables
		this.field = field;
		this.return_value = false;
		this.msg = msg;
		this.highlited_id = highlited_id ? highlited_id : field;
		this.parent_tab = parent_tab ? parent_tab : 0;
		this.show_message = show_message ? show_message : 0;

		// validate method
		this.validate = function() {
			if ( Number($(this.field).value) >= 0) { return true; }
			else { return false; }
		}
	}
}

/*
* validation_length_of class
*/
validate_length_of = Class.create();
validate_length_of.prototype = {
	initialize: function(field, length_type, reference_length, msg, highlited_id, parent_tab, show_message) {

		// member variables
		this.field = field;
		this.length_type = length_type;
		this.reference_length = reference_length;
		this.return_value = false;
		this.msg = msg;
		this.highlited_id = highlited_id ? highlited_id : field;
		this.parent_tab = parent_tab ? parent_tab : 0;
		this.show_message = show_message ? show_message : 0;

		// validate method 
		this.validate = function() {
			switch (this.length_type) {
				case "max":
					if( $(this.field).value.length <= this.reference_length) { return true; }
					else { return false; } 
					break;
				case "min":
					if ( $(this.field).value.length >= this.reference_length) { return true; }
					else { return false; } 
					break;
			}
		}
	}
}
