/*
MooValidate 1.2
Description:
This Javascript class makes it easy to implement form validation.  Reduces
the amount of code required on the main page substancially.

Created by: Michael Ruschak
Requirements
- Mootools 1.2
*/

var mooValidate = new Class({
	
	initialize: function () {
		
		this.ecount = 0;
		
	},
	
	chkOptional: function (optional, value) {
		
		if (optional == true && value.length == 0) {
			
			return true;
			
		} else {
			
			return false;
			
		}
		
	},
	
	augments: function(default_args, options) {
		
		for(var index in default_args) {
			if(typeof options[index] == "undefined") options[index] = default_args[index];
		}
		
		return options;
	},
	
	
	vPassword: function(options) {
		
		var field_blank;
		var default_args = {
			'value'       : null,
			'id'          : null,
			'length'      : 7,
			'message'     : 'Invalid!',
			'optional'    : false
		}
		
		var password_length = options['value'].length;
		
		options = this.augments(default_args, options);
		
		field_blank = this.chkOptional(options['optional'], options['value']);
		
		if (password_length >= options['length'] && $(options['id']).get('value').match(/(.*[0-9].*){2,}/) && $(options['id']).get('value').match(/(.*[A-Z].*){1}/)) {
			
			if (field_blank == false) {
				this.removeError(options['id']);
			}
			
		} else {
			
			if (field_blank == false) {
				this.printError(options['id'], options['message']);
			}

		}
	},
	
	vCompare: function (options) {
		var default_args = {
			'value'        : null,
			'with_value'   : null,
			'operator'     : 'eq',
			'id'           : null,
			'message'      : 'Invalid!'
		}
		
		options = this.augments(default_args, options);
		
		if (options['value'] == options['with_value']) {
			if (options['operator'] == 'eq') {
				
			} else {
				this.printError(options['id'], options['message']);
			}
		} else {
			
			if (options['operator'] == 'eq') {
				this.printError(options['id'], options['message']);
			} else {
				this.removeError(options['id']);
			}
		}
	},
	
	vLength: function (options) {
		
		var default_args = {
			'value'           : null,
			'id'              : null,
			'length'          : 1,
			'message'         : 'Invalid!',
			'operator'        : 'gte',
			'optional'        : false
		}
		
		var value_length = options['value'].length;
		
		options = this.augments(default_args, options);
		
		var field_blank = this.chkOptional(options['optional'], options['value']);
		
		if (
			(value_length > options['length'] && options['operator'] == "gt") || 
			(value_length < options['length'] && options['operator'] == "lt") || 
			(value_length >= options['length'] && options['operator'] == "gte") ||
			(value_length <= options['length'] && options['operator'] == "lte") || 
			(value_length == options['length'] && options['operator'] == "eq") ||
			(field_blank == true && options['optional'] == true)
		) {
			
			this.removeError(options['id']);
			
		} else {
			
			if (field_blank == false) {
				this.printError(options['id'], options['message']);
			}

		}
	},
	
	vState: function(options) {
		
		var default_args = {
			'value'     : null,
			'id'        : null,
			'message'   : 'Invalid!',
			'optional'  : false
		}
		
		options = this.augments(default_args, options);
		
		var field_blank = this.chkOptional(options['optional'], options['value']);
		
		if (options['value'].length == 2 || (field_blank == true && options['optional'] == true))
		{
			
			this.removeError(options['id']);
			
		} else {
			
			if (field_blank == false) {
				this.printError(options['id'], options['message']);
			}
			
		}
		
	},
	
	vNumber: function(options) {
		var default_args = {
			'value'      : null,
			'id'         : null,
			'message'    : 'Invalid!',
			'optional'   : false
		}
		
		options = this.augments(default_args, options);
		
		var field_blank = this.chkOptional(options['optional'], options['value']);
		
		if (options['value'].match(/([0-9])+/) || (field_blank == true && options['optional'] == true))
		{
			
			this.removeError(options['id']);
			
		} else {
			
			if (field_blank == false) {
				this.printError(options['id'], options['message']);
			}

		}
		
	},
	
	vEmail: function(options) {
		
		var default_args = {
			'value'      : null,
			'id'         : null,
			'message'    : 'Invalid!',
			'optional'   : false
		}
		
		options = this.augments(default_args, options);
		
		var field_blank = this.chkOptional(options['optional'], options['value']);
		
		if (options['value'].match(/(.)+@(.)+\.(.)+$/) || (field_blank == true && options['optional'] == true))
		{
			
			this.removeError(options['id']);
			
		} else {
			
			if (field_blank == false) {
				this.printError(options['id'], options['message']);
			}

		}
		
	},
	
	vPhoneFax: function(options) {
		
		var default_args = {
			'value'      : null,
			'id'         : null,
			'message'    : 'Invalid!',
			'optional'   : false
		}
		
		options = this.augments(default_args, options);
		
		var field_blank = this.chkOptional(options['optional'], options['value']);
		
		if (options['value'].match(/^1?(-|\.)?(\()?[0-9]{3}(\))?(-|\.)?[0-9]{3}(-|\.)?[0-9]{4}$/) || (field_blank == true && options['optional'] == true))
		{
			
			this.removeError(options['id']);
			
		} else {
			
			if (field_blank == false) {
				this.printError(options['id'], options['message']);
			}
			
		}
		
	},
	
	vChecked: function(options) {
		var error = true;
		var default_args = {
			'checkboxes'    : null,
			'id'            : null,
			'message'       : 'Invalid!',
			'optional'      : false
		}
		
		options = this.augments(default_args, options);
		
		var field_blank = this.chkOptional(options['optional'], options['value']);
		
		
		for (i=0; i < options['checkboxes'].length; i++) {
			if (options['checkboxes'][i].checked == true) {
				error = false;
				break;
			}
			
		}
		
		if (error == false)
		{
			
			if (field_blank == false) {
				this.removeError(options['id']);
			}
			
		} else {
			
			if (field_blank == false) {
				this.printError(options['id'], options['message']);
			}
			
		}
	},
	
	
	vZip: function(options) {
		
		var default_args = {
			'value'     : null,
			'id'        : null,
			'message'   : 'Invalid!',
			'optional'  : false
		}
		
		options = this.augments(default_args, options);
		
		var field_blank = this.chkOptional(options['optional'], options['value']);
		
		if (options['value'].match(/^[1-9][0-9]{4}$/) || (field_blank == true && options['optional'] == true))
		{
			
			this.removeError(options['id']);
			
		} else {
			
			if (field_blank == false) {
				this.printError(options['id'], options['message']);
			}
			
		}
		
	},
	
	formSubmit: function(e) {
		
		if (this.ecount > 0) {
				
			alert("All errors must be corrected before the form can be submitted.");
			
			new Event(e).stop();
			
		}
		
	},
	
	printError: function(ele_id, message) {
		
		if ($(ele_id+"_error") == null) {
			
			var eBox = new Element('span', {
		    	'class': 'error',
		    	'html': message,
				'id': ele_id+"_error"
			});
			
			eBox.inject(ele_id, 'after');
			
			this.ecount += 1;
			
		}
		
	},
	
	removeError: function(ele_id) {
		
		if ($(ele_id+"_error") != null) {
			
			$(ele_id+"_error").dispose();
			this.ecount -= 1;
			
		}
		
	}
	
});