/**
* This is the Input Validation Framework
*
* It consists of a base Validator class which can be extended, a Validator Framework/Controller
* and a Cross Feild Validator which can be extended.
*
* Current Validators: 'all', 'number', 'date'
* Current Cross Feild Validators : 'date'
*
*/


/**
* Base Validator
*/
BWF.Validator		= Class.create();

BWF.Validator.prototype	= {

	type		:	"all",

	initialize	: function( validators ) {
		validators[ this.type ] = this;
	},

	doValidate	: function( input ) {
		return "";
	},

	validate	: function( input , errordiv ) {
		var errorMsg		= this.doValidate( input );
		errordiv.innerHTML	= errorMsg;
		return ( errorMsg.length == 0 );
	}


};



/**
* Number Validator extends Base Validator
*/
BWF.NumberValidator	= Class.create();

Object.extend( BWF.NumberValidator.prototype , BWF.Validator.prototype );

Object.extend( BWF.NumberValidator.prototype , {

		type		: "number",

		doValidate	: function( input ) {
			var numberpattern	= /(^\d+$)|(^\d+\.\d+$)/;
			if ( numberpattern.test( input ) ) {
				return "";
			} else {				
				//return "NUMBER";
				return "!";
			}
		}
	}
);



/**
* Email Validator extends Base Validator
*/
BWF.EmailValidator	= Class.create();

Object.extend( BWF.EmailValidator.prototype , BWF.Validator.prototype );

Object.extend( BWF.EmailValidator.prototype , {

		type		: "email",

		doValidate	: function( input ) {
			var emailpattern	= /^.+@.+\..{2,3}$/;
			if ( emailpattern.test( input ) ) {
				return "";
			} else {				
				//return "INVALID EMAIL";
				return "!";
			}
		}
	}
);


BWF.EmptyValidator = Class.create();

Object.extend( BWF.EmptyValidator.prototype , BWF.Validator.prototype );

Object.extend( BWF.EmptyValidator.prototype , {
	
        type: "empty",
   
        doValidate: function( input ) {
           if ( input != "" ) {
              return "";
           } else {              
              //return "REQUIRED";
              return "!";
           }
        }
   }

);





/**
* Date Validator extends Base Validator
*/
BWF.DateValidator = Class.create();

Object.extend( BWF.DateValidator.prototype, BWF.Validator.prototype);

Object.extend( BWF.DateValidator.prototype, {
  type: "date",

   doValidate: function(input) {
    var value = Date.parse(input);
    if(value <= 0) {
      return "'" + input + "' is not a date.";
    } else {
      return "";
    }
  }
});


/**
* Validation Framework / Controller
*/
BWF.ValidationFramework	= Class.create();

BWF.ValidationFramework.prototype	= {

	validators		: 0,
	crossValidators : 0,

	validateInput	: function( input ) {
		var retval = true;

		type		 = input.getAttribute("valid");
		errorDivName = input.getAttribute("name") + "_err";

		if ( type == null || errorDivName == null) {
			//
		} else {
			valid = this.validate( type, input.value, $(errorDivName) );

			if( !valid ) {
				retval = false;
			}
		}

		return retval;

	},

	validateForm	: function( form ) {
		var retval	= true;

		for( var i = 0; i < form.length; i++ ) {
			currentInput = form[i];
			type		 = currentInput.getAttribute("valid");
			errorDivName = currentInput.getAttribute("name") + "_err";

			if ( type == null || errorDivName == null) {
				continue;
			} else {
				valid = this.validate( type, currentInput.value, $(errorDivName) );

				if( !valid ) {
					retval = false;
				}
			}
		}
		for(i = 0;
		    i < this.crossValidators.length; i++) {
		  this.crossValidators[i].clearErrors();
		}
		if (retval) {
		  for(i = 0; i < this.crossValidators.length; i++)  {
		    valid = this.crossValidators[i].validate();
		    if(!valid) {
		      retval = false;
		    }
		  }
		}
		return retval;
	},

	validate	: function( type, input, errordiv ) {
		var validator = this.validators[type];
	    if(!validator) {
	      alert("No validator for type '" + type + "'.");
	      return "";
	    }
	    return validator.validate(input, errordiv);
	},

	initialize	: function() {
		this.validators 	 = [];
    	this.crossValidators = [];

		new BWF.Validator( this.validators );
		new BWF.NumberValidator( this.validators );
		new BWF.DateValidator( this.validators );
		new BWF.EmptyValidator( this.validators );
		new BWF.EmailValidator( this.validators );
	}

};


/**
* Base Cross Validator
*/
BWF.CrossValidator = Class.create();
Object.extend(BWF.CrossValidator.prototype, {
    type: "none",
    crossError: 0,
    crossInputs: 0,

    initialize: function(framework,
                         p_crossInputs,
                         p_crossError) {
      framework.crossValidators.push(this);
      this.crossError = p_crossError;
      this.crossInputs = p_crossInputs;
    },

    validate: function() {
      errorMsg = this.doValidate(
        this.crossInputs);
      this.crossError.innerHTML = errorMsg;
      return (errorMsg.length == 0);
    },

    clearErrors: function() {
      this.crossError.innerHTML = "";
    }
});


/**
* Date Range Cross Validator extends Base Cross Validator
*/
BWF.DateRangeCrossValidator =
  Class.create();
Object.extend(BWF.DateRangeCrossValidator.prototype,
              BWF.CrossValidator.prototype);
Object.extend(BWF.DateRangeCrossValidator.prototype, {

  doValidate: function(inputs) {
    var startDate = Date.parse(inputs[0].value);
    var endDate = Date.parse(inputs[1].value);
    if (startDate > endDate) {
      return "The start date cannot be after the end date.";
    } else {
      return "";
    }
  }

});