/**
 * form utilities
 * @author pete goodman
 *
 */
var formUtil = {

	/**
	 * functions to clear a textbox of text
	 * @param id string the id of the text field in question
	 */
	autoFill : function (id){
		
		// retrieve the field, and access it's default text
		var field = document.getElementById(id);
		var defaulttext = field.value;
		
		field.style.color = "#999"

		// if default text is present when selected, hide it 
		field.onfocus = function() {
			if (this.value == defaulttext) {
				this.style.color = "#000";
				this.value = "";
			}
		}
		
		// if no text is entered on exit, replace default text
		field.onblur = function() {
			if (this.value == "") {
				this.style.color = "#999";
				this.value = defaulttext;
			}
		}
	},
	
	autoFill2 : function (id,idvalue,iddefault){
		
		// retrieve the field, and access it's default text
		var field = document.getElementById(id);
		if (idvalue == iddefault || idvalue == "") {
			field.value = iddefault
			field.style.color = "#999"
			
			// if default text is present when selected, hide it 
			field.onfocus = function() {
				if (this.value == iddefault) {
					this.style.color = "#000";
					this.value = "";
				}
			}
			
			// if no text is entered on exit, replace default text
			field.onblur = function() {
				if (this.value == "") {
					this.style.color = "#999";
					this.value = iddefault;
				}
			}
		}
		else {
			field.value = idvalue
			field.style.color = "#000"
		}
	},
	autoFillList : function (id,iddefault,idvalues) {
		var field = document.getElementById(id);
		idvaluesarr = idvalues.split("|");
		var newopt;
		var x;
		for (x=0;x<=idvaluesarr.length-1;x=x+2) {
			newopt=document.createElement('option');
			newopt.text=idvaluesarr[x+1];
			newopt.value=idvaluesarr[x];
			if (idvaluesarr[x] == iddefault) {
				newopt.selected = true
			}
			try {
			    field.add(newopt,null); // standards compliant
			}
			catch(ex) {
			    field.add(newopt); // IE only
			}
		}
	},

		
	/**
	 * functions to select or deselect all checkboxes
	 * @param parentcheckbox string the id of the parent check box that toggles the rest
	 * @param childboxcontainer string the id of the html container for all child checkboxes
	 */
	toggleCheckboxes : function (parentcheckbox, childboxcontainer) {
		
		// detect when the parent checkbox value is changed
		var parentCheckbox = document.getElementById(parentcheckbox);
		parentCheckbox.onclick = function() { 
			switchfields(this.checked);
		}

		//get child checkboxes 
		var container = document.getElementById(childboxcontainer);
		var childCheckboxes = container.getElementsByTagName('input');

		 // traverse and check 
		function switchfields(checkOnOff) { 
			 for (var i=0; i < childCheckboxes.length; i++) {
			 	childCheckboxes[i].checked = checkOnOff; 
			 }
		}
	}
}