/*
* Validation Rules (clientValidationRules.js)
*
* This file contains all the JS validation rule objects
* which do the validation for each individual rule
*
* You can add your own rule objects in this file
* or add them to another file. The only thing
* your custom rule object must have is a validate()
* function, similar to the functions below.
*/

function FormBuilder_RequiredRule (field) {
	this.validate = function() {
		// get value of field
		var value = this.parent.getValue(field, false);

		// got a value?
		if (value == false) {
			return false;
		} else {
			return true;
		}
	}
}

function FormBuilder_NumericRule (field, optional) {
	this.validate = function() {
		// get value of field
		var value = this.parent.getValue(field, false);

		if (optional == true && value.length == 0) {
			return true;
		}

		// value is numeric?
		var RegExp = /^(-)?(\d*)(\.?)(\d*)$/;
		var ret = value.match(RegExp);
		if (ret == null || value.length == 0) {
			return false;
		} else {
			return true;
		}
	}
}

function FormBuilder_RegexRule (field, regex, optional) {
	this.field = field;
	this.regex = regex;

	this.validate = function() {
		// get value of field
		var value = this.parent.getValue(this.field, false);

		if (optional == true && value.length == 0) {
			return true;
		}

		// create regex, and test if value matches
		var re = new RegExp(this.regex);
		return re.test(value);
	}
}

function FormBuilder_CallbackRule (field, callback) {
	this.field = field;
	this.callback = callback;

	this.validate = function() {
		// get value of field
		var value = this.parent.getValue(this.field, false);

		return this.callback(value);
	}
}

function FormBuilder_MatchRule (field, field2) {
	this.field = field;
	this.field2 = field2;

	this.validate = function() {
		// get values of both fields
		var value1 = this.parent.getValue(this.field, false);
		var value2 = this.parent.getValue(this.field2, false);

		// values match?
		if (value1 == value2) {
			return true;
		} else {
			return false;
		}
	}
}

function FormBuilder_EmailRule (field, optional) {
	this.field = field;

	this.validate = function() {
		// get value of field
		var value = this.parent.getValue(this.field, false);

		if (optional == true && value.length == 0) {
			return true;
		}

		// return result of check_email function
		return this.check_email(value);
	}

	this.check_email = function(email) {
		// check if '@' is in there and each part has right length
		regex = /^[^@]{1,64}@[^@]{1,255}$/;
		var re = new RegExp(regex);

		if (re.test(email) == false) {
			return false;
		}

		// Split it into sections to make life easier
		var email_array = email.split('@');
		var local_array = email_array[0].split('.');

		// check each piece in the local array (local array = everything before '@')
		var regex = /^(([A-Za-z0-9!#$%&'*+\/=?^_`{|}~-][A-Za-z0-9!#$%&'*+\/=?^_`{|}~\.-]{0,63})|(\"[^(\\|\")]{0,62}\"))$/;
		var re = new RegExp(regex);
		for(var i=0; i < local_array.length; i++) {
			if (re.test(local_array[i]) == false) {
				return false;
			}
		}

		// check if domain is IP (if not, it should be a valid domain name)
		var regex = /^\[?[0-9\.]+\]?$/;
		var re = new RegExp(regex);
		if (re.test(email_array[1]) == false) {
			var domain_array = email_array[1].split('.');

			// domain has enough parts (at least 2; name + tld)
			if (domain_array.length < 2) {
				return false;
			}

			// check each part of the domain
			var regex = /^(([A-Za-z0-9][A-Za-z0-9-]{0,61}[A-Za-z0-9])|([A-Za-z0-9]+))$/;
			var re = new RegExp(regex);
			for(var i=0; i < domain_array.length; i++) {
				if (re.test(domain_array[i]) == false) {
					return false;
				}
			}
		}

		// all done; e-mail is valid!
		return true;
	}
}

function FormBuilder_LengthRule (field, min, max) {
	this.field = field;
	this.min = min;
	this.max = max;

	this.validate = function() {
		// get value of field
		var value = this.parent.getValue(this.field, false);
		var len = value.length;

		// check min length
		if (this.min != '' && len < this.min) {
			return false;
		}

		// check max length
		if (this.max != '' && len > this.max) {
			return false;
		}

		// all passed
		return true;
	}
}

function FormBuilder_AlphaNumericRule (field) {
	this.field = field;

	this.validate = function() {
		// get value of field
		var value = this.parent.getValue(this.field, false);

		// value is numeric?
		var RegExp = /^[a-zA-Z0-9]+$/;
		var ret = value.match(RegExp);
		if (ret == null || value.length == 0) {
			return false;
		} else {
			return true;
		}
	}
}
