/*
**
**	Title:			FormValidator Class
**	Author:			Keith Foster
**	Description:	Developed to easily implement javascript form validation on the fly, by setting a few parameters to the function.
**	Parameters:		1. Required Fields - { field name, validation type ( string, number, email, phone number ).
**					2. Error display mode
**
*/

var FormValidator = function(form, required, display_mode){
	this.form = document.getElementById(form);
	this.required = (required)?required:null;
	this.invalid_fields = new Array();
	this.display_mode = display_mode ? display_mode : 'box_above';
};
FormValidator.prototype = {
	reset:function(){
		this.invalid_fields = new Array();
		var n = this.required.length;
		
		for(var i=0; i<n; i++) {
			this.form[this.required[i].name].className = this.form[this.required[i].name].className.replace('error', '');
			
		}
		//var errant_fields = document.getElementById('errant_fields');
		//errant_fields.innerHTML = '';
		//errant_fields.parentNode.className = 'hidden';
	},
	set_states:function(){
		var value = this.form['country'].options[this.form['country'].selectedIndex].value;
		this.form['state_region'].className = value.replace(' ', '-');
	},
	validate:function(){
		this.reset();
		var alpha = function(element, title) {
			var msg = 'Only alphabetic characters allowed';
			var regex = /\d/g;
			
			if(empty(element.value)) {
				msg = 'Please enter a value';
				this.invalid_fields.push({element:element, message:title+' - '+msg+'\n' });
			} else if(element.value.search(regex)!=-1) { 
				this.invalid_fields.push({element:element, message:title+' - '+msg+'\n' });	
			}
		};
		var numeric = function(element, title) {
			var msg = 'Only numeric characters allowed';
			var regex = /\D/g;
			
			if(empty(element.value)) {
				msg = 'Please enter a value';
				this.invalid_fields.push({element:element, message:title+' - '+msg+'\n' });
			} else if(element.value.search(regex)!=-1) {
				this.invalid_fields.push({element:element, message:title+' - '+msg+'\n' });	
			}
		};
		var alphanumeric = function(element, title) {
			var msg = 'Please enter a value';
			if(empty(element.value)) {
				this.invalid_fields.push({element:element, message:title+' - '+msg+'\n' });
			}
		};
		var selected = function(element, title) {
			var msg = 'Please select an option';
			var selected = element.options[element.selectedIndex];
			if(selected.value == -1) {
				this.invalid_fields.push({element:element, message:title+' - '+msg+'\n' });	
			}
		};		
		var fileinput = function(element, title) {
			var msg = 'You need to choose a file to upload';
			if(empty(element.value)) {
				this.invalid_fields.push( {element:element, message:title+' - '+msg+'\n' } );
			}
		};
		var privacy = function(element, title) {
			var msg = 'You must accept the Privacy Policy to submit your application.';
			if(!element.checked) {
				this.invalid_fields.push( {element:element, message:msg+'\n' } );
			}
		};
		var email = function(element, title) {
			var msg = 'Email not in the correct format';
			var regex = /^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*\.(\w{2,})$/;
			if(element.value == "") {
				this.invalid_fields.push({element:element, message:title+' - '+msg+'\n' });
			} else if(element.value.search(regex) == -1) {
				this.invalid_fields.push({element:element, message:title+' - '+msg+'\n' });
			}
		};
		var phone = function(element, title) {
			var msg = 'Phone number is not in correct format (123-456-7890)';
			var regex = /^([0-9]{3})-([0-9]{3})-([0-9]{4})$/;
			
			if(element.value == "") {
				msg = 'Please enter a value';
				this.invalid_fields.push({element:element, message:title+' - '+msg+'\n' });
			} else if(element.value.search(regex) == -1) {
				this.invalid_fields.push({element:element, message:title+' - '+msg+'\n' });
			}
		
		};
		var empty = function(value){
			if(value == '') {
				return true;
			} else {
				return false;
			}
		};		
		
		/* validate the form */
		if(this.required) {
			/* required fields is not null */
			var n = this.required.length;
			for(var i=0; i<n; i++) {
				eval(this.required[i].type).call(this, this.form[this.required[i].name], this.required[i].title);
				//email.call(this, HTMLElement, String);
			}			
			var n = this.invalid_fields.length;
			if(n > 0) {
				//var errant_fields = document.getElementById('errant_fields');
				//var li = document.createElement('li'); var temp = null;
				
				var temp = "";
				
				for(var i=0; i<n; i++) {
					//temp = li.cloneNode(false);
					//temp.appendChild(document.createTextNode(this.invalid_fields[i].message));
					
					temp += this.invalid_fields[i].message;
					
					//errant_fields.appendChild(temp);
					this.invalid_fields[i].element.className += ' error'
				}
				
				alert(temp);
				
				//document.getElementById("error_notification").className = '';
			} else {
				this.form.submit();	
			}
		}
		return false;
	}
};