/**
 * The FORMfields Library
 * The file contains a collection of JavaScript routines used by FORMgen.
 * Copyright 2005-2007 Brain Book Software LLC
 * For complete documentation, please visit http://www.formfields.com
 */

/**
 * Converts a form title to a form name by changing the case and removing characters.
 * @param string title the form title
 * @return string the form name
 */
function getFormName(title)
{
	var formName = "";
	var validChars = "abcdefghijklmnopqrstuvwxyz0123456789_";
	title = title.toLowerCase(); // Convert string to lowercase
	for (i = 0; i < title.length; i++) {
		var valid = false;
		for (j = 0; j < validChars.length; j++) {
			if (title.charAt(i) == validChars.charAt(j)) {
				valid = true;
				break;
			}
		}
		if (valid) {
			formName += title.charAt(i); // Add valid chars
		} else if (title.charAt(i) == ' ') {
			formName += "_"; // Substitute spaces with underscores
		}
	}
	return formName;
}

/**
 * Sets the form name based on the value of the form title when the form name is blank.
 * @param element formNameField the form name field
 * @param string titleFieldId the id value of the title field
 */
function setFormName(formNameField, titleFieldId)
{
	// Is the form name blank?
	if (formNameField.value === "") {
		titleField = document.getElementById(titleFieldId);
		formNameField.value = getFormName(titleField.value);
	}
}