var btnSubmitClicked = false;

function IsHex(aValue) {
	var regex = /^#?([a-f]|[A-F]|[0-9]){3}(([a-f]|[A-F]|[0-9]){3})?$/;
	return (regex.test(aValue));
}

function defineElementRules(aFormStr, aFieldStr, aRule, aConstraints) {
	if (eval('document.' + aFormStr + '.' + aFieldStr)) {
		if (aConstraints == empty) {
			eval('document.' + aFormStr + '.' + aFieldStr + '.' + aRule + ' = true');
		} else {
			eval('document.' + aFormStr + '.' + aFieldStr + '.' + aRule + ' = true');
			eval('document.' + aFormStr + '.' + aFieldStr + '.constraints = \'' + aConstraints + '\'');
		}
	}
}

function empty(aElement) {
	var bool = false;
	var strCurrType = '';
	strCurrType = aElement.type;
	if (strCurrType == 'password' || strCurrType == 'text' || strCurrType == 'textarea' || strCurrType == 'hidden') {
		bool = (aElement.value == '');
	} else {
		if (strCurrType == 'select-one' || strCurrType == 'select-multiple') {
			bool = (aElement.selectedIndex == 0);
		} else {
		    if (strCurrType == 'radio') {
				bool = (!aElement.checked);
			} else {
				if (strCurrType == 'checkbox') {
					bool = (!aElement.checked);
				}
			}
        }
	}
	return bool;
}

function fieldValue(aElement) {
	var strCurrType = '';
	strCurrType = aElement.type;
	if (strCurrType == 'password' || strCurrType == 'text' || strCurrType == 'textarea') {
		return aElement.value;
	} else {
		if (strCurrType == 'select-one' || strCurrType == 'select-multiple') {
			return aElement.selectedIndex;
		} else {
		    if (strCurrType == 'radio' || strCurrType == 'checkbox') {
				return aElement.checked;
			}
        }
	}
}

function wordcount(string) {
	var a = string.split(/\s+/g); // split the sentence into an array of words
	return a.length;
}

function validateForm(aForm) {
	var undefined;
	var bool = true;
	var strRequiredSeparator = '';
	var strEmailSeparator = '';
	var strRequiredFields = '';
	var strEmailFields = '';
	var strCurrType = '';
	var strErrorMessage = '';
	var strAtLeast
	var arrayAtLeast
	var boolAtLeast
	var strDependants;
	var arrayDependants;
	var boolDependantsAllFilled;
	var strMatch
	var arrayMatch
	var boolMatch
	var tempString = '';
	var i = 0;
	var j = 0;

    for (i = 0; i < aForm.elements.length; i++) {
		strCurrType = aForm.elements[i].type;

		if (aForm.elements[i].required) {
			if (empty(aForm.elements[i])) {
				strErrorMessage = strErrorMessage + 'The required field \'' + aForm.elements[i].name + '\' has not been filled in.\n'
				bool = false;
			}
        }

		if (aForm.elements[i].alphanumeric) {
			tempString = aForm.elements[i].value;
			if (!isAlphanumeric(tempString)) {
				strErrorMessage = strErrorMessage + 'The field \'' + aForm.elements[i].name + '\' must contain only alphanumeric characters.\n'
				bool = false;
			}
        }

        if (aForm.elements[i].email) {
			if (aForm.elements[i].value != '') {
				if (!isEmail(aForm.elements[i].value)) {
					strErrorMessage = strErrorMessage + 'The email field \'' + aForm.elements[i].name + '\' has an invalid format.\n'
					bool = false;
				}
			}
        }

        if (aForm.elements[i].hexadecimal) {
			if (aForm.elements[i].value != '') {
				if (!IsHex(aForm.elements[i].value)) {
					strErrorMessage = strErrorMessage + 'The field \'' + aForm.elements[i].name + '\' has to contain a hexadecimal number.\n'
					bool = false;
				}
			}
        }

        if (aForm.elements[i].dependants) {
			if (!empty(aForm.elements[i])) {
				strDependants = aForm.elements[i].constraints;
				arrayDependants = strDependants.split(",");
				boolDependantsAllFilled = true;
				for (j = 0; j < arrayDependants.length; j++) {
					boolDependantsAllFilled = boolDependantsAllFilled && !empty(eval('aForm.'+ arrayDependants[j]));
				}
				if (!boolDependantsAllFilled) {
					if (arrayDependants.length == 1) {
						strErrorMessage = strErrorMessage + 'The field \'' + aForm.elements[i].name + '\' has been filled in. Therefore, the following field: \'' + strDependants + '\' is also required.\n'
					} else {
						strErrorMessage = strErrorMessage + 'The field \'' + aForm.elements[i].name + '\' has been filled in. Therefore, the following fields: \'' + strDependants + '\' are also required.\n'
					}
					bool = false;
				}
			}
        }

        if (aForm.elements[i].atleast) {
			strAtLeast = aForm.elements[i].constraints;
			arrayAtLeast = strAtLeast.split(",");
			boolAtLeast = false;
			for (j = 0; j < arrayAtLeast.length; j++) {
				boolAtLeast = boolAtLeast || !empty(eval('aForm.'+ arrayAtLeast[j]));
			}
			if (!boolAtLeast) {
				strErrorMessage = strErrorMessage + 'You must fill in at least one of the following fields: \'' + strAtLeast + '\'.\n'
				bool = false;
			}
        }

        if (aForm.elements[i].match) {
			strMatch = aForm.elements[i].constraints;
			arrayMatch = strMatch.split(",");
			boolMatch = true;
			for (j = 0; j < arrayMatch.length; j++) {
				if (fieldValue(aForm.elements[i]) != fieldValue(eval('aForm.' + arrayMatch[j]))) {
					strErrorMessage = strErrorMessage + 'The field \'' + arrayMatch[j] + '\' must match the field \'' + aForm.elements[i].name + '\'.\n'
					boolMatch = false;
				}
			}
			if (!boolMatch) {
				bool = false;
			}
        }

        if (aForm.elements[i].wordcount) {
			if (!empty(aForm.elements[i])) {
				if (wordcount(aForm.elements[i].value) > aForm.elements[i].constraints) {
					strErrorMessage = strErrorMessage + 'The field \'' + aForm.elements[i].name + '\' contains more than ' + aForm.elements[i].constraints +' words.\n'
					bool = false;
				}
			}
        }

    }

    if (!bool) {
		alert('A number of amendments are required to the form before it can be processed.\n\n' + strErrorMessage);
    }
    
    return bool;
}

