// Validator class.
// Depends on prototype.

var Validator = Class.create();
Validator.prototype = {
  initialize: function(formId, fieldClass, errorClass) {    
		this.form = $(formId);
    this.fieldClass = fieldClass;
    this.errorClass = errorClass;
  },

  // Description: Input element must have a value. Cannot be null or ""	
	// Arguments: id of element on page.
  validatesPresenceOf: function(element) {
    element = $(element);
		var val = $F(element);
		if ( (val != "") && (val != null) && (val != '-1') ) {
			this.clearError(element);
		  return true;		
		}
		else {
			this.setError(element);
		  return false;
		}		
  },

  // Description: Input element must be a checkbox, and must be checked.
	// Arguments: id of checkbox element on page
	validatesAcceptanceOf: function(element) {
		element = $(element);
		if (element.type.toLowerCase() == "checkbox") {
		  if (element.checked) {
				this.clearError(element);
			  return true;
			}
		}
		this.setError(element);
		return false;
  },
	
  // Description: Tests two elements to make sure they contain the same value. Does not check for empty or null values
	// Arguments: element = id of confirm element, toconfirm = id of element confirming against
	validatesConfirmationOf: function(element, toconfirm) {
    element = $(element);
		toconfirm = $(toconfirm);
			 
	  if ( $F(element) == $F(toconfirm) ) {
			this.clearError(element);
		  return true;
		}
		else {
			this.setError(element);
		  return false;	
		}		
	},
	
	// Description: Makes sure an element has a specific length;
	// Arguments: element = id of element to check, iMin = minimum length, iMax = maximum length
	validatesLengthOf: function(element, iMin, iMax) {
		element = $(element);
	  if ( ($F(element).length >= iMin) && ($F(element).length <= iMax) )  {
			this.clearError(element);
			return true;
		}
		else {
			return false;
			this.setError(element);
		}
	},
	
	// Description: Makes sure an element is a numbers
	// Arguments: element = id of element to check
	validatesNumericalityOf: function(element) {
	  element = $(element);
		if (!isNaN($F(element)) && this.validatesPresenceOf(element)) {				
		  this.clearError(element);
			return true;
		}
		else {
		  this.setError(element);
		  return false;		
		}
	},	
	
	// Description: Makes sure an element has a particular format.
	// Arguments: element = id of element to check, regEx = a regular expression to match. 
	// Notes: Stop whining about regular expressions. They really are the best way to do this sort of thing. Learn them, Use them.
	// Loooove them.	
	validatesFormatOf: function(element, regEx) {
	  element = $(element);
		var val = $F(element);
		if (val.match(regEx))  {
		  this.clearError(element);
			return true;
		}	
		else {
			this.setError(element);
			return false;	
		}
	},
	
	validatesEligibilityFor: function(element, participantElement, webService, webServiceMethod, httpMethod, kwargs) {
	    var returnValue = false;
	    element = $(element);
	    validator.clearError(element);
	    var participantId = $F(participantElement);
	    var params = $H(kwargs);	    
	    params.set('ParticipantId', participantId);
	    webServiceMethod = escape(webServiceMethod);
	    httpMethod = httpMethod || 'get';
	    
	    var succeed = function(transport) {
	        var response = transport.responseText;
            if (response.include('true'))
                returnValue = true;
            else 
                validator.setError(element);
	    };
	    
	    var fail = function(transport) {            
	        alert('Failed to contact WebService to validate your eligibility. Please contact IIL.');
	    };
	    
	    new Ajax.Request(webService + '/' + webServiceMethod, {
	        method: httpMethod,
	        asynchronous: false, 
	        parameters: params.toQueryString(),
	        onSuccess: succeed,
	        onFailure: fail
        });
        return returnValue;        
	},	
	
	// only used from within Validator. Hides the error label item, and sets the input field back to it's normal appearance
	setError: function(element) {        
	    if ($(element.id + "_error"))  {
            $(element.id + "_error").removeClassName("hidden");
            $(element.id + "_error").addClassName(this.errorClass);
        }	    
	},
	
	// only used from within Validator. Shows the error label item, and sets the input field back to the Validator object's errorClass.
	clearError: function(element) {
	    if ($(element.id + "_error")) {
    		$(element.id + "_error").addClassName("hidden");
    		$(element.id + "_error").removeClassName(this.errorClass);
	    }
	},	
	
	// check all required events
	// this is going to become a conglomeration of all controls' validationEvents.
	validate: function() {		
	}
	
}
