/**
 * 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 'isNumeric':
				if (thisValue.length == 0) {
					systemMessage = ' is a mandatory field';
				}
				if (!thisValue.match(/^[-]?\d*\.?\d*$/)) {
						systemMessage = ' does not appear to be valid';
				}
				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 pushCheckBox(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() {
		switch (this.validationStr) {
			case 'check':
			if (new getObj(fieldID).obj.checked == true) {
				returnMessage = 1;
			} else {
				returnMessage = 0;
			}
			break;
			case 'nonempty':
			default:
			break;
		}
		return returnMessage;
	}
}

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);
		}
	}
	
	numChecked = 0;
	for (fieldCount in validateCheckBox) {
		thisField = validateCheckBox[fieldCount];
		numChecked += thisField.validate();
	}
	
	if (numChecked == 0) {
		alerts.push("Please select more than one checkbox");
	}
	
	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);
}

function enableDisable(cObject,fieldName,groupNum) {
	switch (fieldName) {
		case 'corporateGolfDay':
			totalInput = 14;
			startInput = 0;
			break;
		case 'friendlyGamesAtBeattyPark':
		case 'workabilityChristmasTreePerth':
		case 'workabilityChristmasTreeHallsHead':
			totalInput = 4;
			startInput = 0;
			break;
	}
	for (i=startInput; i<totalInput; i++) {
		if (cObject.checked == true) {
			document.event[fieldName+i].disabled = false;
			document.event[fieldName+i].readonly = false;
			document.event[fieldName+i].style.backgroundColor = '#FFFFFF';
		} else {
			document.event[fieldName+i].disabled = true;
			document.event[fieldName+i].readonly = true;
			document.event[fieldName+i].style.backgroundColor = '#EEEEEE';
		}
	}
	if (fieldName == 'corporateGolfDay') {
		enableDisableGroup(cObject,fieldName,groupNum);
	}
}

function enableDisableGroup(cObject,fieldName,groupNum){
	totalInput = 10;
	startInput = 2;
	fieldName = 'corporateGolfDay';
	for (i=startInput; i<totalInput; i++) {
		if (cObject.checked == true && groupNum == 0) {
			document.event[fieldName+i].disabled = false;
			document.event[fieldName+i].readonly = false;
			document.event[fieldName+i].style.backgroundColor = '#FFFFFF';
			document.event[fieldName+i].style.color = '#003366';
		} else {
			document.event[fieldName+i].disabled = true;
			document.event[fieldName+i].readonly = true;
			document.event[fieldName+i].style.backgroundColor = '#EEEEEE';
			document.event[fieldName+i].style.color = '#999999';
		}
	}
}
