/*
MooValidate 1.1
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, id) {
		
		if (optional == true && $(id).get('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;
	},
	
	vDropdown: function (options) {
		var fieldBlank;
		var default_args = {
			'id'              : null,
			'error_container' : null,
			'value'           : null,
			'message'         : 'Invalid!',
			'optional'        : false
		}
		
		var eleValue = $(options['id']).get('value');
		
		options = this.augments(default_args, options);
		
		fieldBlank = this.chkOptional(options['optional'], options['id']);
		
		if (eleValue != options['value']) {
			if (fieldBlank == false) {
				if (options['error_container'] != null) {
					this.removeError(options['error_container']);
				} else {
					this.removeError(options['id']);
				}
			}
			
		} else {
			
			if (fieldBlank == false) {
				if (options['error_container'] != null) {
					this.printError(options['error_container'], options['message']);
				} else {
					this.printError(options['id'], options['message']);
				}
			}
			
		}
	},
	
	vPassword: function(options) {
		
		var fieldBlank;
		var default_args = {
			'id'              : null,
			'length'          : 7,
			'error_container' : null,
			'message'         : 'Invalid!',
			'optional'        : false
		}
		
		var eleStrLen = $(options['id']).get('value').length;
		
		options = this.augments(default_args, options);
		
		fieldBlank = this.chkOptional(options['optional'], options['id']);
		
		if (eleStrLen >= options['length'] && $(options['id']).get('value').match(/(.*[0-9].*){2,}/) && $(options['id']).get('value').match(/(.*[A-Z].*){1}/)) {
			
			if (fieldBlank == false) {
				if (options['error_container'] != null) {
					this.removeError(options['error_container']);
				} else {
					this.removeError(options['id']);
				}
			}
			
		} else {
			
			if (fieldBlank == false) {
				if (options['error_container'] != null) {
					this.printError(options['error_container'], options['message']);
				} else {
					this.printError(options['id'], options['message']);
				}
			}

		}
	},
	
	vPasswordVerify: function (options) {
		var default_args = {
			'id'              : null,
			'id_verify'       : null,
			'error_container' : null,
			'message'         : 'Invalid!'
		}
		
		options = this.augments(default_args, options);
		
		if ($(options['id']).get('value') == $(options['id_verify']).get('value')) {
		
			this.removeError(options['error_container']);
			
		} else {
			
			this.printError(options['error_container'], options['message']);
			
		}
	},
	
	vLength: function (options) {
		
		var fieldBlank;
		var default_args = {
			'id'              : null,
			'length'          : 1,
			'error_container' : null,
			'message'         : 'Invalid!',
			'operator'        : 'gte',
			'optional'        : false
		}
		
		var eleStrLen = $(options['id']).get('value').length;
		
		options = this.augments(default_args, options);
		
		fieldBlank = this.chkOptional(options['optional'], options['id']);
		
		if (
			(eleStrLen > options['length'] && options['operator'] == "gt") || 
			(eleStrLen < options['length'] && options['operator'] == "lt") || 
			(eleStrLen >= options['length'] && options['operator'] == "gte") ||
			(eleStrLen <= options['length'] && options['operator'] == "lte") || 
			(eleStrLen == options['length'] && options['operator'] == "eq")
		) {
			
			if (fieldBlank == false) {
				if (options['error_container'] != null) {
					this.removeError(options['error_container']);
				} else {
					this.removeError(options['id']);
				}
			}
			
		} else {
			
			if (fieldBlank == false) {
				if (options['error_container'] != null) {
					this.printError(options['error_container'], options['message']);
				} else {
					this.printError(options['id'], options['message']);
				}
			}

		}
	},
	
	vState: function(options) {
		
		var fieldBlank;
		var default_args = {
			'id'              : null,
			'error_container' : null,
			'message'         : 'Invalid!',
			'optional'        : false
		}
		
		options = this.augments(default_args, options);
		
		fieldBlank = this.chkOptional(options['optional'], options['id']);
		
		if ($(options['id']).get('value').length == 2)
		{
			
			if (fieldBlank == false) {
				if (options['error_container'] != null) {
					this.removeError(options['error_container']);
				} else {
					this.removeError(options['id']);
				}
			}
			
		} else {
			
			if (fieldBlank == false) {
				if (options['error_container'] != null) {
					this.printError(options['error_container'], options['message']);
				} else {
					this.printError(options['id'], options['message']);
				}
			}
			
		}
		
	},
	
	vEmail: function(options) {
		
		var fieldBlank;
		var default_args = {
			'id'              : null,
			'error_container' : null,
			'message'         : 'Invalid!',
			'optional'        : false
		}
		
		options = this.augments(default_args, options);
		
		fieldBlank = this.chkOptional(options['optional'], options['id']);
		
		if ($(options['id']).get('value').match(/(.)+@(.)+\.(.)+$/))
		{
			
			if (fieldBlank == false) {
				if (options['error_container'] != null) {
					this.removeError(options['error_container']);
				} else {
					this.removeError(options['id']);
				}
			}
			
		} else {
			
			if (fieldBlank == false) {
				if (options['error_container'] != null) {
					this.printError(options['error_container'], options['message']);
				} else {
					this.printError(options['id'], options['message']);
				}
			}

		}
		
	},
	
	vPhone: function(options) {
		
		var fieldBlank;
		var default_args = {
			'id'              : null,
			'error_container' : null,
			'message'         : 'Invalid!',
			'optional'        : false
		}
		
		options = this.augments(default_args, options);
		
		fieldBlank = this.chkOptional(options['optional'], options['id']);
		
		if ($(options['id']).get('value').match(/^1?(-|\.)?(\()?[0-9]{3}(\))?(-|\.)?[0-9]{3}(-|\.)?[0-9]{4}$/))
		{
			
			if (fieldBlank == false) {
				if (options['error_container'] != null) {
					this.removeError(options['error_container']);
				} else {
					this.removeError(options['id']);
				}
			}
			
		} else {
			
			if (fieldBlank == false) {
				if (options['error_container'] != null) {
					this.printError(options['error_container'], options['message']);
				} else {
					this.printError(options['id'], options['message']);
				}
			}
			
		}
		
	},
	
	vFax: function(options) {
		
		var fieldBlank;
		var default_args = {
			'id'              : null,
			'error_container' : null,
			'message'         : 'Invalid!',
			'optional'        : false
		}
		
		options = this.augments(default_args, options);
		
		fieldBlank = this.chkOptional(options['optional'], options['id']);

		if ($(options['id']).get('value').match(/^1?(-|\.)?(\()?[0-9]{3}(\))?(-|\.)?[0-9]{3}(-|\.)?[0-9]{4}$/))
		{
			
			if (fieldBlank == false) {
				if (options['error_container'] != null) {
					this.removeError(options['error_container']);
				} else {
					this.removeError(options['id']);
				}
			}
			
		} else {
			
			if (fieldBlank == false) {
				if (options['error_container'] != null) {
					this.printError(options['error_container'], options['message']);
				} else {
					this.printError(options['id'], options['message']);
				}
			}
			
		}
		
	},
	
	selectedCheckboxes: function(options) {
		var default_args = {
			'id'              : 'apples oranges pears',
			'error_container' : null,
			'total_selected'  : 1,
			'optional'        : false
		}
		
		options = this.augments(default_args, options);
		
		var message = 'Please select at least ' + options['total_selected'] + ".";
		var numChecked = 0;
		var fieldChecked;
		
		options['id'] = options['id'].split(' ');
		
		
		for (var i=0; i < options['id'].length; i++) {
			if ($(options['id'][i]).checked == true)
				numChecked += 1;
		}
		
		if (options['optional'] == true && numChecked == 0) {
			fieldChecked = true;
		} else {
			fieldChecked = false;
		}
		
		if (numChecked >= options['total_selected'])
		{
			if (fieldChecked == false) {
				this.removeError(options['error_container']);
			}
			
		} else {
			
			if (fieldChecked == false) {
				this.printError(options['error_container'], message);
			}
			
		}
		
	}, 
	
	vZip: function(options) {
		
		var fieldBlank;
		var default_args = {
			'id'              : null,
			'error_container' : null,
			'message'         : 'Invalid!',
			'optional'        : false
		}
		
		options = this.augments(default_args, options);
		
		fieldBlank = this.chkOptional(options['optional'], options['id']);
		
		if ($(options['id']).get('value').match(/^[1-9][0-9]{4}$/))
		{
			
			if (fieldBlank == false) {
				if (options['error_container'] != null) {
					this.removeError(options['error_container']);
				} else {
					this.removeError(options['id']);
				}
			}
			
		} else {
			
			if (fieldBlank == false) {
				if (options['error_container'] != null) {
					this.printError(options['error_container'], options['message']);
				} else {
					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;
			
		}
		
	}
	
});