/**
 * Disable the form button on submit.
 * @param theForm The form for which the submit and reset buttons are 
 * to be disabled.
 */
function disableForm(theform) {

	if (document.all || document.getElementById) {
		for (i = 0; i < theform.length; i++) {
		var tempobj = theform.elements[i];
		if (tempobj.type.toLowerCase() == "submit" || tempobj.type.toLowerCase() == "reset")
			tempobj.disabled = true;
		}
	}
	return true;
}

/**
 * Asks the user, if he / she really wants to delete an item.
 */
function deleteConfirm(msg) {
	var deleteOk=confirm(msg); 
	if(!deleteOk) {
		return false;
	}
	return true;
}

/**
 * Toggles the visibility of a block, so that user has
 * a more compact screen.
 * @param the id's that are going to be collapsed.
 */
function toggleBlock(togglingElem, disableText, enableText) {

	for (var i = 3, length = arguments.length; i < length; i++) {
		var id = arguments[i];
		var block = document.getElementById(id);
		if(block) {
			if ( block.style.display == 'none' ) {
				block.style.display = '';
				togglingElem.innerHTML = enableText;
			}
			else {
				block.style.display = 'none';
				togglingElem.innerHTML = disableText;
			}
		}
	}
	return false;
}

/**
 * Shortens a string with the structure (.+)\s*(\(.+?\)).
 * @param text The string that is to be shortened.
 * @param text The length of the shortened string.
 */
function shorten(text, length) {
	if(!text) {
		return "";
	}
	var expr = /(.+)\s*(\(.+?\))/;
	var matches = expr.exec(text);
	if(!matches) {
		return text;
	}
	var mainText = RegExp.$1;
	var appendix = RegExp.$2;
	if(mainText.length > length) {
		mainText = mainText.substring(0, length) + "...";
	}
	return mainText + appendix;
}

/**
 * Displays the form list in the new form screen.
 * @param checkbox The checkbox that is used for activating the form combo list
 * on the user screen. 
 */
function displayFormList(checkbox) {

	var listFormCombo = document.getElementById('form_row');
	if(listFormCombo) {
		if(checkbox.checked) {
			listFormCombo.style.display= '';
		}
		else {
			listFormCombo.style.display= 'none';
		}
	}
}

/**
 * Activates the default process definition, which is in English.
 */
function activateDefaultProcess() {

	var combo = document.getElementById("id_form:processDef");
	if(combo) {
		for(var i = 0; i < combo.options.length; i++) {
			var option = combo.options[i]; 
			if(option.text.indexOf("default") == 0) {
				combo.selectedIndex = i;
				return;
			}
		}
	}
}

/**
 * Prefills the process definition form, in case a certain entry is chosen.
 */
function prefillProcessDefForm(combo) {
	var selectedValue = combo.options[combo.selectedIndex].value;
	if(selectedValue == "emptyProcess") {
		var adminFirstNameField = document.getElementById("id_form:input_event_admin_first");
		if(adminFirstNameField) {
			adminFirstNameField.value = "------";
		}
		var adminSurnameField = document.getElementById("id_form:input_event_admin_last");
		if(adminSurnameField) {
			adminSurnameField.value = "------";
		}
		var adminEmailField = document.getElementById("id_form:input_event_admin_email");
		if(adminEmailField) {
			adminEmailField.value = "none@none.net";
		}
	}
}


/**
 * Deletes the content of certain text fields, if they have a certain value.
 * @param textFieldId The text field identifier.
 * @param value The value that is deleted.
 */
function deleteField(id, delValue) {
	var element = document.getElementById(id);
	if(element) {
		var value = element.value;
		if(delValue == value) {
			element.value = "";
		}
	}
}

//////////////////// Combo Box Functions //////////////////////////

/**
 * Container for a value and name pair.
 * @param value The internal value.
 * @param name The name that is displayed in the combo.
 */
function ComboData(value, name) {
	this.value = value;
	this.name = name;
}

/**
 * Object used to store the original options, so that they can be restored.
 * @param originalOptions The original options of a combo box.
 */
function Combo(originalOptions) {
	this.options = [];
	if(originalOptions) {
		for(var i = 0, length = originalOptions.length; i < length; i++) {
			this.options[this.options.length] = new ComboData(originalOptions[i].value, originalOptions[i].text);
		}
	}
	this.fillCombo = fillCombo;
}

/**
 * Member function used to fill a combo box.
 * @param combo The combo box that is going to be filled.
 */
var fillCombo = function(combo) {
	combo.innerHTML = "";
	for(var i = 0, length = this.options.length; i < length; i++) {
		combo.options[combo.options.length] = new Option(this.options[i].name, this.options[i].value);
	}
}

/**
 * May be used to set the original state of a combo box.
 */
var origCombo;

/**
 * Sets the original combo box.
 */ 
function setOrigCombo(combo) {
	origCombo = new Combo(combo.options);
}

/**
 * Sorts a combo box in either ascending or descending order.
 * @param combo The combo box that is to be sorted.
 * @param asc If <code>true</code> then the combo should be sorted in ascending order,
 * else in descending.
 */
function sort(combo, asc) {
	var options = combo.options;
	var output = "";
	var comboDataArray = new Array();
	for(var i = 0, length = options.length; i < length; i++) {
		comboDataArray[comboDataArray.length] = new ComboData(options[i].value, options[i].text);
	}
	if(asc) {
		comboDataArray.sort(sortByName)
	}
	else {
		comboDataArray.reverse(sortByName)
	}
	combo.innerHTML = "";
	for(var i = 0, length = comboDataArray.length; i < length; i++) {
		combo.options[combo.options.length] = new Option(comboDataArray[i].name, comboDataArray[i].value);
	}
}

/**
 * Sorts a combo data object by name and in a case insensitive way.
 * @param a The first combo data object.
 * @param b The second combo data object.
 */
var sortByName = function(a, b) {
	var x = a.name.toLowerCase();
	var y = b.name.toLowerCase();
	return ((x < y) ? -1 : ((x > y) ? 1 : 0));
}

//////////////////// End Combo Box Functions //////////////////////


