/**
* @author     	paul campbell <pauljamescampbell@hotmail.com>
* @desc     	form validation class (object)
**/
function FormManager() {
	this._form 			= null;
	this._errortag	 	= null;
	this._errormessage	= null;
	this._errorclass 	= null;
	this._fields 		= null;
	this._invalid		= new Array();

	//initialize class methods
	if (typeof(_formmanager_prototype_called) == 'undefined'){
		_formmanager_prototype_called = true;
		//intiate the protoype methods
		FormManager.prototype.validateFields 	= validateFields;
		FormManager.prototype.outputErrorClass 	= outputErrorClass;
		FormManager.prototype.outputErrorMessage = outputErrorMessage;
		FormManager.prototype.browserDetect		= browserDetect;
		FormManager.prototype.setFields 		= setFields;
		FormManager.prototype.setForm 			= setForm;
		FormManager.prototype.setErrorTag 		= setErrorTag;
		FormManager.prototype.setErrorMessage 	= setErrorMessage;
		FormManager.prototype.setErrorClass 	= setErrorClass;
		FormManager.prototype.checkEmail 		= checkEmail;
		FormManager.prototype.checkObject 		= checkObject;
		FormManager.prototype.checkArray 		= checkArray;
		FormManager.prototype.checkString 		= checkString;
		FormManager.prototype.checkNumber 		= checkNumber;
		FormManager.prototype.checkByte 		= checkByte;
		FormManager.prototype.checkBoolean 		= checkBoolean;
		FormManager.prototype.checkExist 		= checkExist;
	}
}

function validateFields() {
	if(this._fields != null) {
		var fieldValue = null;
		for(var key in this._fields) {
			fieldType = this._fields[key];
			fieldValue = this._form[key].value;

			switch(fieldType) {
				case "string":
				if(!this.checkString(fieldValue)){
					this._invalid[this._invalid.length] = key;
				}
				break;
				case "number":
				/* revise number validation -- doesnt work!!! */
				if(!this.checkNumber(fieldValue)){
					this._invalid[this._invalid.length] = key;
				}
				break;
				case "date":
				if(!this.checkDate(fieldValue)){
					this._invalid[this._invalid.length] = key;
				}
				break;

				case "email":
				if(!this.checkEmail(fieldValue)){
					this._invalid[this._invalid.length] = key;
				}

				case "equal":
				if(fieldValue[0] != fieldValue[1]){
					this._invalid[this._invalid.length] = key;
				}
				break;

				default:
				alert("ERROR: An incompatible variable type was processed for field: " + key);
				break;
			}
		}

		if(this._invalid.length == 0){
			alert('passed');
			return true;
		} else {
			if(this._errorclass != null) this.outputErrorClass();
			if(this._errortag != null) this.outputErrorMessage();
		}
	}
	return false;
}

function outputErrorMessage() {
	if(this._errormessage != null){
		var message = this._errormessage;
	} else {
		var message = 'Please correct the incorrect, highlighted fields. Thank You.';
	}
	/* print the error message */
	var browser = this.browserDetect();

	switch(browser) {
		case "byid" :
		var errorObject = document.getElementById(this._errortag);
		errorObject.innerHTML = message;
		errorObject.style['display'] = 'block';
		break;

		case "bylayer" :
		break;

		default:
		var errorObject = document.getElementById(this._errortag);
		errorObject.innerHTML = message;
		errorObject.style['display'] = 'block';
		break;
	}
}

function outputErrorClass() {
	/* reset other fields before (note _fields is an object */
	for(var key in this._fields) {
		this._form[key].className = '';

	}
	for(var k=0; k<this._invalid.length; k++) {
		this._form[this._invalid[k]].className = this._errorclass;
	}
}

function browserDetect() {
	return "byid";
}

function setFields(sfields){
	if(this.checkObject(sfields)){
		for(var key in sfields) {
			if(!this.checkString(sfields[key])){
				alert("ERROR: Each fields object value is required to be a string, this keys value is incorrect: " + key);
				return;
			}
		}
		this._fields = sfields;
		return;
	}
	alert("ERROR: An object (faux associative array) has not been passed to the fields parameter. VALUE: " + sfields);
}

function setForm(sform){
	if(this.checkString(sform)) {
		sform = document.forms[sform];
	}
	if(this.checkObject(sform)) {
		this._form = sform;
		return;
	}
	alert("ERROR: An object has not been passed to the form parameter. VALUE: " + sform);
}

function setErrorTag(stag){
	if(this.checkString(stag)){
		this._errortag = stag;
		return;
	}
	alert("ERROR: A string was not passed to the erroroutput parameter. VALUE: " + stag);
}

function setErrorMessage(smessage){
	if(this.checkString(smessage)){
		this._errormessage = smessage;
		return;
	}
	alert("ERROR: A string was not passed to the erroroutput parameter. VALUE: " + smessage);
}

function setErrorClass(sclass){
	if(this.checkString(sclass)){
		this._errorclass = sclass;
		return;
	}
	alert("ERROR: A string was not passed to the errorclass parameter. VALUE: " + sclass);
}

function checkEmail(vemail) {
	if(this.checkString(vemail)) {
		var pattern = /^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/
		/* remove any odd characters or spaces */
		vemail = escape(vemail);
		if(pattern.test(vemail)) {
			return true;
		}
	}
	return false;
}

function checkObject(vobject) {
	if(this.checkExist(vobject)) {
		if(typeof(vobject)=="object") {
			return true;
		}
	}
	return false;
}

function checkArray(varray) {
	if(this.checkExist(varray)) {
		if(typeof(varray)=="object") {
			if(varray.length!=0){
				return true;
			}
		}
	}
	return false;
}

function checkString(vstring) {
	if(this.checkByte(vstring)) {
		if(typeof(vstring)=="string"){
			return true;
		}
	}
	return false;
}

function checkNumber(vnumber) {
	if(this.checkByte(vnumber)) {
		if(typeof(vnumber)=="number"){
			return true;
		}
	}
	return false;
}

function checkByte(vbyte) {
	if(this.checkExist(vbyte)) {
		if(vbyte.length!=0) {
			return true;
		}
		return false;
	}
}

function checkExist(vexist) {
	if(vexist) {
		return true;
	}
	return false;
}

function checkBoolean(vboolean) {
	if(typeof(vboolean)=="boolean") {
		return true;
	}
	return false;
}