/**
 * Copy the value of an input field's title attribute to its value attribute.
 * Clear the input field on focus if its value is the same as its title.
 * Repopulate the input field on blur if it is empty.
 * Hide the input field's associated label if it has one.
 */
var autoPopulate = {
	sInputClass:'populate', // Class name for input elements to autopopulate
	sHiddenClass:'structural', // Class name that gets assigned to hidden label elements
	bHideLabels:true, // If true, labels are hidden
	/**
	 * Main function
	 */
	init:function() {
		// Find all input elements with the given className
		var arrInputs = jQuery('input.' + autoPopulate.sInputClass);
		var iInputs = arrInputs.length;
		var oInput;
		for (var i=0; i<iInputs; i++) {
			var initText;
			oInput = arrInputs[i];
			// Make sure it's a text input
			if (oInput.type != 'text') { continue; }
			// Find the label
			var oLabel = jQuery('label[for='+oInput.id+']');
			// Hide the input's label
			if (autoPopulate.bHideLabels && oLabel[0]) { 
				try{
					//oLabel[0].addClass(autoPopulate.sHiddenClass);
					initText = oLabel.html();
				} catch(e) {
					if((oInput.value == '') && (oInput.title != '')) { initText = oInput.title; }
				}
			}
			// Set the value of the field to the content of the label
			if(oInput.value == '') oInput.value = initText;
			// Add event handlers for focus and blur
			jQuery(oInput).focus(function() {
				// If value and title are equal on focus, clear value
				if (this.value == initText) {
					this.value = '';
					this.select(); // Make input caret visible in IE
				}
			});
			jQuery(oInput).blur(function() {
				// If the field is empty on blur, assign title to value
				if (!this.value.length) { this.value = initText; }
			});
		}
	}
};

/**
 * Init on window load.
 * Replace this with a call to your own addEvent/DOMReady function if you use one.
 */
jQuery(document).ready(autoPopulate.init);