/**
 * Form field object for validation
 * @param {string} fieldID
 * @param {string} fieldLabel
 * @param {string} validationStr
 * @param {string} validationAlert
 */
function FormField(fieldID, fieldLabel, validationStr, validationAlert) {
	this.fieldID = fieldID;
	this.fieldLabel = (fieldLabel == null) ? fieldID : fieldLabel;
	this.validationStr = (validationStr == null) ? '' : validationStr;
	this.validationAlert = (validationAlert == null) ? '' : validationAlert;
	this.confirmField = '';
	
	this.validate = function() {
		var returnMessage = '';
		var systemMessage = '';
		var thisValue = getFieldValue(this.fieldID);
		switch (this.validationStr) {
			case 'email':
				if (thisValue.length == 0) {
					systemMessage = ' is a mandatory field';
				}
				if (thisValue.length > 0) {
					if (!thisValue.match(/^[\w.!#$%&'*\/=?^_`}{|~+-]+@[\w-]+\.[\w.-]+$/)) {
						systemMessage = ' does not appear to be valid';
					}
				}
				break;
			case 'confirm':
				var compareValue = getFieldValue(this.confirmField);
				if (thisValue != compareValue) {
					systemMessage = ' does not match confirmation';
				}
				break;
			case 'nonempty':
			default:
				if (thisValue.length == 0) {
					systemMessage = ' is a mandatory field';
				}
		}
		
		if (systemMessage.length > 0) {
			returnMessage = this.fieldLabel;
			returnMessage += (this.validationAlert.length > 0) ? this.validationAlert : systemMessage;
		}
		
		return returnMessage;
	}
	
	this.setConfirmField = function(confirmField) {
		this.confirmField = confirmField;
	}
	
}


function getFieldValue(fieldID) {
	var thisObj = new getObj(fieldID);
	return thisObj.obj.value;
}


function validateForm(fields) {
	var alerts = new Array();
	isValid = true;
	
	for (fieldCount in fields) {
		thisField = fields[fieldCount];
		error = thisField.validate();
		if (error.length > 0) {
			alerts.push(error);
		}
	}
	
	if (alerts.length > 0) {
		alert('Please note the following information:\n' + alerts.join('\n'));
		isValid = false;
	}
	
	return isValid;

}


function confirmCancel() {
	msg = 'Cancelling will lose any unsaved information.\nAre you sure you want to continue?';
	return confirm(msg);
}
