var invalidFieldNames = new Array();

function validateForm(formName,fieldNames)
{
	// Store form reference
	formObject = document.forms[formName];

	// Reset invalid field counter
	var invalidFields = 0;
	invalidFieldNames.length = 0;

	// Hide alerts if not the first time validating
	hideAlerts(formObject);

	// Go through the form elements passed
	for (var intCounter = 0; intCounter < fieldNames.length; intCounter++)
	{
		// Store form element reference
		formElement = formObject.elements[fieldNames[intCounter]];

		// Check fields and increment counter if empty
		if(isEmpty(formElement))
		{
			invalidFields++;
			// Highlight form element and focus
			showAlert(formObject, formElement);
			formElement.focus();
		}
	}

	// If empty fields were found
	if (invalidFields > 0)
      {
		// Show alert
		alert("There was a problem with the form.\nPlease check the following fields:\n\n" + invalidFieldNames.reverse().join("\n"));
		return false;
	}
	else
	{
	// Return true if all form elements are complete
      formObject.submit();
	return true;
	}
}

// Function to check if a form element is empty
function isEmpty(formElement)
{
	// Check if element is empty
	if(formElement.value.length == 0)
	{
		// Is empty
		return true;
	}
	else
	{
		// Not empty
		return false;
	}
}

function showAlert(formObject,formElement)
{
		// Get list of all labels within form
            var formLabels = formObject.getElementsByTagName("label");

		// Loop through list of labels
		var currentLabel = 0;
		var matchingLabel = 0;

		// Loop for number of labels or until match is found
		while((currentLabel < formLabels.length) && (matchingLabel == 0))
		{
			// Look for label matching the element name
			if (formLabels[currentLabel].htmlFor == formElement.name)
			{
				// alert("Found " + elementObject.name);
				matchingLabel = formLabels[currentLabel];
			}
			currentLabel++;
		}

		// Check if matching label was found
		if(matchingLabel != 0)
		{
			// Set up error image in DOM
			var errorImage = document.createElement("img");
	            errorImage.setAttribute("src", "images/icons/warning.gif");
			errorImage.setAttribute("id", "errorImage" + (currentLabel - 1));
			errorImage.setAttribute("align", "middle");
			errorImage.setAttribute("width", "14px");
			errorImage.setAttribute("height", "13px");
			errorImage.setAttribute("class", "imgIcon");
			errorImage.setAttribute("alt", "Please check this form field.");

			// Set label to red colour
			matchingLabel.style.color = "#FF0000";

			// Add field to list of incomplete fields
			var labelText;
			if (!matchingLabel.innerText)
			{
                        labelText = matchingLabel.textContent;
			}
			else
			{
				labelText = matchingLabel.innerText;
			}
			labelText = labelText.replace(":","");
			labelText = labelText.replace(/^\s+|\s+$/g, "");
			invalidFieldNames[invalidFieldNames.length] = "* " + labelText;

			// Add error image to document
			matchingLabel.appendChild(errorImage);
		}
}

function hideAlerts(formObject)
{
	// Get list of all labels within form
      var formLabels = formObject.getElementsByTagName("label");

	// Loop through list of labels
	var currentLabel = 0;

	// Loop for number of labels or until match is found
	while(currentLabel < formLabels.length)
	{
		// Reset label styles
		formLabels[currentLabel].style.color = "";

		// Remove error image
		if(oldErrorImage = document.getElementById("errorImage" + currentLabel))
		{
			oldErrorImage.parentNode.removeChild(oldErrorImage);
		}
		currentLabel++;
	}
}

