/**
 * The FormUtil package.
 */
var FormUtil = {

	// configuration for the maxlength stuff
	_maxLenConfig: 			new Array(),
	_countUpdateInterval:		1,
	_lineBreakCharacterCount:	2,
	
	/**
	 * Sets the maximum length allowed for the given input field.  Event
	 * listeners are attached to the keyup event to ensure that the maximum
	 * character length is never exceeded.
	 * @param inputId the input to set the maxlength on
	 * @param displayId id of an element to display length information
	 * @param displayFormat the display format for displayId (use {count} and {remaining} and {maxLength})
	 * @param maxLength the maximum length of the field
	 * @param errorFunction function called when the maxLength has been reached
	 */
	setMaxLength: function(inputId, displayId, displayFormat, maxLength, errorFunction, okFunction) {
		
		// setup event listener
		YAHOO.util.Event.onContentReady(inputId, function() {
			
			// get elements
			var inputElement 	= YAHOO.util.Dom.get(inputId);

			var displayElement	= false;
			if(YAHOO.util.Dom.get(displayId)) {
				displayElement = YAHOO.util.Dom.get(displayId);
			}
			
			if (YAHOO.util.Dom.hasClass(inputId, "login_required")){
					inputElement.readOnly = true;
			} else {
				inputElement.readOnly = false;
			}
			
			// setup keyup listener
			YAHOO.util.Event.addListener(inputId, 'keyup', function() {
			
				if (inputElement.value.length>=maxLength) {
					inputElement.value = inputElement.value.substring(0, maxLength);
					if (errorFunction!=undefined) {
						errorFunction();
					}
				} else {
					if (okFunction!=undefined) {
						okFunction();
					}
				}
				
			});
			
			// setup display update timer
			setInterval(function() {
				
				// get the current length of the value
				/*
				var val = inputElement.value;
				var count = val.length;
				for (var idx=val.indexOf("\n"); idx!=-1; idx=val.indexOf("\n", idx+1)) {
					count += FormUtil._lineBreakCharacterCount - 1;
				}
				*/
				var count = inputElement.value.length;
				
				// see if we have a default text for this
				if ((FormUtil._defaultTexts[inputElement.id]!=undefined)
					&& (inputElement.value==FormUtil._defaultTexts[inputElement.id])) {
					count = 0;
				}
				
				// create new text for display
				var newText = displayFormat
					.replace("{count}", count)
					.replace("{maxLength}", maxLength)
					.replace("{remaining}", (maxLength-count >= 0 ? maxLength-count : 0));
				
				// limit the number of DOM manipulations if possible
				if(displayElement) {
					if (newText!=displayElement.innerHTML) {
						displayElement.innerHTML = newText;
					}
				}
				
			}, FormUtil._countUpdateInterval);
		});
	},
	
	// where we store the default text for
	// form elements by id
	_defaultTexts: new Array(),
	
	// foreground colors of text boxes
	_defaultTextColor: 	"#999",
	_typingTextColor:	"#000",
	
	/**
	 * Sets the default text for the given input id.
	 * @param inputId the input's id
	 * @param defaultText the default text for the input
	 */
	setDefaultText:	function(inputId, defaultText) {
		
		// save the default text
		FormUtil._defaultTexts[inputId] = defaultText;
		
		// setup event listener
		YAHOO.util.Event.onContentReady(inputId, function() {
			
			// get the element and form
			var inputElement = YAHOO.util.Dom.get(inputId);
			var inputForm = YAHOO.util.Dom.get(inputId).form;
			
			// set default text
			if (inputElement.value=="") {
				YAHOO.util.Dom.setStyle(inputElement, "color", FormUtil._defaultTextColor);
				inputElement.value = FormUtil._defaultTexts[inputId];
			}
			
			// add blur listener
			YAHOO.util.Event.addListener(inputId, 'blur', function() {
				if (inputElement.value=="") {
					inputElement.value = FormUtil._defaultTexts[inputId];
					YAHOO.util.Dom.setStyle(inputElement, "color", FormUtil._defaultTextColor);
				}
			});
			
			// add focus listener
			YAHOO.util.Event.addListener(inputId, 'focus', function() {
				if (inputElement.value==FormUtil._defaultTexts[inputId]) {
					inputElement.value = "";
					YAHOO.util.Dom.setStyle(inputElement, "color", FormUtil._typingTextColor);
				}
			});
			
			// add submit listener to form
			YAHOO.util.Event.addListener(inputForm, 'submit', function() {
				if (inputElement.value==FormUtil._defaultTexts[inputId]) {
					inputElement.value = "";
				}
			});
			
		});
	}
};
