/**
 * Google Analytics JavaScript class.
 */
GoogleAnalytics = {

	/**
	 * Enables form field tracking for the form with the given ID.
	 *
	 * Note: can be called before document onload.
	 *
	 * @param	string	formId
	 * @param	string	formLabel
	 */
	trackFormFields: function(formId, formLabel)
	{
		/* Instead of direct tracking onchange/onclick, we will track the form
		 * fields on window unload, so we won't get troubled data when a form
		 * field is altered a couple of times before submitting the form
		 */
		connect(currentWindow(), "onunload", function() { GoogleAnalytics.doTrackFormFields(formId, formLabel); });
	}, // function trackFormFields

	/**
	 * Does the actual tracking for form field (called on window unload).
	 *
	 * This will call _trackEvent() for each non-empty form field.
	 *
	 * @param	string	formId
	 * @param	string	formLabel
	 */
	doTrackFormFields: function(formId, formLabel)
	{
		/* get the form */
		var form = $(formId);
		if (!form)
		{
			return;
		}

		/* if no form label (human readable name) is given, use the form ID */
		if (formLabel.length == 0)
		{
			formLabel = formId;
		}

		/* Track a single event for this form, so we know how many visitors have
		 * visited the form (even if they haven't filled in a single field).
		 */
		pageTracker._trackEvent("Forms", formLabel, "(form viewed)");

		/* track all form fields */
		for (var i = 0; i < form.elements.length; i++)
		{
			/* name is required */
			if (!form.elements[i].name)
			{
				continue;
			}
			/* skip buttons and file inputs */
			if (form.elements[i].type && (form.elements[i].type == "submit" || form.elements[i].type == "button" || form.elements[i].type == "file"))
			{
				continue;
			}
			/* only checked radiobuttons and checkboxes which are checked should be tracked */
			if (form.elements[i].type && (form.elements[i].type == "radio" || form.elements[i].type == "checkbox") && form.elements[i].checked === false)
			{
				continue;
			}
			/* disabled fields should not be tracked */
			if (form.elements[i].disabled && form.elements[i].disabled === true)
			{
				continue;
			}
			/* do not track empty fields */
			if (form.elements[i].value == "")
			{
				continue;
			}
			/* track this field */
			pageTracker._trackEvent("Forms", formLabel, form.elements[i].name);
		}
	} // function doTrackFormFields

}; // class GoogleAnalytics