/*
 * JavaScript functions for manipulating forms.
 * Author: Sindri Traustason
 *         Guillaume Mallard
 * 02.08.2004
 */

/*
 * Sets the value of the input field with id=fieldId to the given value.
 * Pre: document must contain a object with id fieldId that has a value field.
 */
function setFieldValue(fieldId, value){
	var field = document.getElementById(fieldId);
	field.value = value;
}

/*
 * Sets the action of the form with id=formId to the given value.
 * Pre: document must contain a form with id formId.
 */
function setFormAction(formId, action){
	var form = document.getElementById(formId);
	form.action = action;
}

/*
 * Sets the default forms action to the given value
 * Pre: document must contain a form with id 'pageform'.
 */
function setAction(action){
	setFormAction('pageform', action);
}

/*
 * Returns true if the toolbar button number i is displayed.
 */
function isButtonDisplayed(i){
	var toolbarDiv = document.getElementById("mainToolbar");
	return (toolbarDiv.getElementsByTagName('a')[i].style.display!="none");
}

/*
 * Pre: none.
 * Post: toolbar button number i is displayed.
 */
function displayButton(i){
	var toolbarDiv = document.getElementById("mainToolbar");
	toolbarDiv.getElementsByTagName('a')[i].style.display="inline";
}

/*
 * Pre: none.
 * Post: toolbar button number i is not displayed.
 */
function hideButton(i){
	var toolbarDiv = document.getElementById("mainToolbar");
	toolbarDiv.getElementsByTagName('a')[i].style.display="none";
}

/*
 * Pre: none.
 * Post: toolbar button number i has the oposite display state from before.
 */
function toggleButton(i){
	if(isButtonDisplayed(i)){
		hideButton(i);
	} else {
		displayButton(i);
	}
}



/*
 * trim a string just like the java function.
 * @param  s - the string to trim
 * @return the string trimmed
 */
function Trim(s) {

	// Remove leading spaces and carriage returns
	while ((s.substring(0,1) == ' ') || (s.substring(0,1) == '\n') || (s.substring(0,1) == '\r')) {
		s = s.substring(1,s.length);
	}

	// Remove trailing spaces and carriage returns
	while ((s.substring(s.length-1,s.length) == ' ') || (s.substring(s.length-1,s.length) == '\n') || (s.substring(s.length-1,s.length) == '\r')) {
		s = s.substring(0,s.length-1);
	}
	return s;
 }


/*
 * Select all the options of a listbox
 * @param listboxId, the id of the listbox
 */
 function selectAllListboxOptions( listboxId ) {

	var listbox = document.getElementById( listboxId );
	if ( listbox.options.length > 0 ) {
		for ( var i = 0 ; i < listbox.options.length ; i ++ ) {
			listbox.options[i].selected = true;
		}
	}
 }

//
// Validate number input is positive.
//
function validatePositiveNumber(elt) {
	if (isNaN(elt.value) || parseInt(elt.value) < 0) {
		alert('Please specify a non-negative integer');
		document.focus(elt);
	}
}

//
// Show hide dive based on checkbox value
//
function toggleDivByCheckbox(checkbox, divId){
	toggleDiv(checkbox.checked, divId);
}

//
// Show hide dive based on boolean value
//
function toggleDiv(show, divId){
	var theDiv = document.getElementById(divId);
	if(theDiv != undefined){
		if(show){
			theDiv.style.display = 'block';
		} else {
			theDiv.style.display = 'none';
		}
	}
}
