﻿// Goodies.importGoodies(Goodies.V_PATTERNS);

BaseValidator = function() {};

RequiredField = function(fieldName, message) {
	if (typeof(fieldName) !== "string") throw new TypeError();
	
	this.pageField = fieldName;
	this.errorMessage = message;
	this.router = null;
};

RequiredField.prototype = new BaseValidator();

RequiredField.prototype.validate = function() {
	return (this.pageField.value.length != 0);
};

PatternField = function(fieldName, regexPattern, message, checkBlank) {
	this.pageField = fieldName;
	this.errorMessage = message;
	this.pattern = regexPattern;
	this.checkBlank = (checkBlank == null ? false : checkBlank);
	this.router = null;
};

PatternField.prototype = new BaseValidator();

PatternField.prototype.validate = function() {
	var x = this.pageField.value;
	if (String.isNullOrEmpty(x) && !this.checkBlank) return (true);
	else {
		if (this.checkBlank) return (x.length != 0 && x.search(this.pattern) != -1);
		else return (x.search(this.pattern) != -1);
	}
};


MatchField = function(fieldName, fieldName2, message, checkBlank, fn) {
	this.pageField = fieldName;
	this.pageField2 = fieldName2;
	this.errorMessage = message;
	this.checkBlank = (checkBlank == null ? false : checkBlank);
	this.formName = fn;
	this.router = null;
};

MatchField.prototype = new BaseValidator();

MatchField.prototype.validate = function() {
	var x = this.pageField.value;
	var y = document.forms[this.formName].elements[this.pageField2].value;
	if (String.isNullOrEmpty(x) && !this.checkBlank) return (true);
	else {
		if (this.checkBlank) return (x.length != 0 && x == y);
		else return (x == y);
	}
};

FieldMethod = function(fieldName, callbackMethod, message) {
	this.pageField = fieldName;
	this.errorMessage = message;
	this.validate = callbackMethod;
	this.router = null;
};

FieldMethod.prototype = new BaseValidator();

FormValidator = function(formName) {
	this.pageForm = document.forms[formName];
	this.pageForm.validator = this;
	this.validators = [];
	this.disabled = false;
};

FormValidator.prototype.addValidator = function(validator) {
	try {
		if (!(validator instanceof BaseValidator)) throw new TypeError("FormValidator: Failed attempt to add an invalid object type to the validators collection of this instance.");
		validator.router = this;
		this.validators.push(validator);
	} catch (e) {
		alert(e.message);
	}
};

FormValidator.prototype.addValidators = function(validator) {
	this._addValidators(arguments);
};

FormValidator.prototype._addValidators = function(validators) {
	for (var a = 0; a < validators.length; a++) this.addValidator(validators[a]);
};

FormValidator.prototype.validate = function() {
	var router = this.validator;
	var passed = true;
	
	if (router.disabled)
		return (true);
		
	var messages = [];
	router.validators.forEach(function() {	
		if (this instanceof FieldMethod) {
			if (!this.validate.call(this.pageField)) {
				passed = false;
				messages.push(" - " + this.errorMessage);
			}
		} else {
			if (!this.validate()) {
				passed = false;
				messages.push(" - " + this.errorMessage);
			}
		}
	});
	
	if (!passed) {
		var msg = "Cannot proceed due to the following error(s):\n";
		msg += messages.join("\n");
		alert(msg);
	}
	
	return (passed);
};

FormValidator.prototype.bind = function() {
	this.pageForm.onsubmit = this.validate;
	var theName;
	this.validators.forEach(function() {
		theName = this.pageField;
		this.pageField = this.router.pageForm.elements[this.pageField];
		if (this.pageField == null) {
			alert("Binding failed for validator against the form field: \"" + theName + "\"\nPlease verify this form field exists within the specified form element.\n\nWARNING: Due to a configuration error, the FormValidator has been disabled.");
			this.router.disabled = true;
		}
	});
};
