/** stored control values */
var gaField = new Array();

/**
 * Clear the error message for the field identified by id 
 * 
 * @param id of the field 
 * @return nothing
 */
function clearErrorMessageFor(id) {	
	var spans = $$("span[errorFor=" + id + "]").each(function(item) {
		item.getParent().setStyle('opacity', 0);
	});
}

/**
 * Clear all fields with a value equals to the label value.
 * It allows to not take in consideration the label value
 * when the page is submited. It is because the label
 * is in the textbox control.
 *  
 * @return nothing
 */
function clearAllEmptyFields() {
	$(document).getElements("input[id][label]").each(function(item) {
		if(item.get("value")==item.get("label"))
			item.set("value","");
	});
}

/**
 * When a Textbox is clicked if there is a value 
 * equals to its label, then empty the textbox. 
 * Else we keep the value
 *   
 * @param id of the textbox which has been clicked.
 * 
 * @return nothing
 */
function clearFieldForInput(id) {
	var inp =  $(document).getElement("input[id=" + id + "]" );
	if( (inp != null) && (inp.value.length>0) && ( inp.get("label")==inp.value) ) {
		var v = $(inp).get('value');
		
		gaField[id]=v;
		$(inp).set("value", "");
	}

	if(inp!=null) {
		$(inp).removeEvents('blur');
		$(inp).addEvent('blur', function() {
			restoreValue(id);
		});
	}
}

/**
 * When a textbox loose its focus, if the value 
 * is empty : restore the label, otherwise keeps 
 * the user value. it uses an array to store all 
 * values
 *  
 * @param id textbox id when the value should be restored
 * @return
 */
function restoreValue(id) {
	var currentValue = $(document).getElement("input[id=" + id + "]").get("value");

	if(currentValue.length==0) {
		var v2set = gaField[id];
		
		if(v2set==null) {
			v2set= $(document).getElement("input[id=" + id + "]").get("label");
		}
		
		$(document).getElement("input[id=" + id + "]").set("value",v2set);
	}
}
	