
/*
* New switch tabs when you click
* April 2010 - yaz
*/
function mantisSwitchTabs(active_tab) {
	// loop thru content pieces, hide all
	$('moduletab_divs').childElements().each(function(element) {
		$(element).hide();
	});
	
	// show content piece
	$('content_'+active_tab).show();
	
	// loop thru tabs
	$('tabbed_nav').childElements().each(function(element) {
		$(element).down('a').removeClassName('current');
		
		// make active_tab tab selected
		var selected = 'tab_'+active_tab;
		if(selected == $(element).identify()) {
			$(element).down('a').addClassName('current');
		}
		// make tab show, in case it wasnt
		$(selected).show();
		
	});
	
	// default to not full screen
	mantisResetFullScreen();
	
	// hide rest
	$('errorbox', 'actionWindow', 'messagebox').invoke('hide');

}


/* 
* Makes the container box full screen
* For those modules that are huge (reports, new page creator, etc) 
*/
function mantisSetFullScreen() {
	if($('module_tabs').hasClassName('module_fullscreen') == false) {
		$('module_tabs').addClassName('module_fullscreen'); 
		$('module_content').addClassName('module_fullscreen'); 
	}
} 
function mantisResetFullScreen() {
	$('module_tabs').removeClassName('module_fullscreen');
	$('module_content').removeClassName('module_fullscreen'); 
} 

/*
* Clears the default text from an input field
* Usage on html input: onfocus="clear_input(this);"
* Sept 09 by Yaz
*/
function clear_input(theText) {
	if (theText.value == theText.defaultValue) {
		theText.value = ""
	}
}

/* 
* Initiate Tooltip 
* Feb 2010 - yaz
*/
function initTooltips() {
	var all_help_imgs = $$("img.mantis_help_icon").findAll(function(node){  return node.readAttribute('tooltip');  });
	
	all_help_imgs.each(function(node){
		new Tooltip(node, node.readAttribute('tooltip'));
	 	node.removeAttribute("tooltip");
	});
	
}



/**
* Modifies a <select> dropdown by adding a new item, removing it or updating it
* if option_value && option_name are undefined, it removes the option element
* if option_value && option_name are NOT undefined, it updates the option element
*   and if it does not find the needle it creates a new option
*
* @param id of element to alter
* @param string needle, generally the same as the option value
* @param string option value of the new/updated element
* @param string option name of the new/updated element
*
* Update for Prototype 1.6 May 2010
*/
function fncCommon_Alter_Select(elem_dropdown, needle, option_value, option_name){
	var dropdown = $(elem_dropdown).select('option');
	
	var rejection = dropdown.reject(function(option){
		return option.value != needle;
	}).each(function(option){
		if (typeof(option_value) != "undefined" && typeof(option_name) != "undefined"){
			option.value     = option_value;
			option.text 	= option_name;
		} else {
			$(option).remove();
		}
		
	});
		
	if(rejection.length == "0"){
		$(elem_dropdown).options[$(elem_dropdown).options.length] = new Option(option_name, option_value, false);
	}
}

/*
* Adds array of items to a <select> bix
* Useful for selecting multiple categories and moving them from one box to another
*/
function fncCommon_Select_Append_Selected(fromElement, toElement){
	var fromOptions = $(fromElement).select('option');
	var toOptions   = $(toElement).select('option');
	
	fromOptions = fromOptions.reject(function(fromOption){
		return !fromOption.selected || toOptions.any(function(toOption){return toOption.value == fromOption.value;});
	});
	
	fromOptions.each(function(fromOption){
		$(toElement).options[$(toElement).options.length] = new Option(fromOption.text, fromOption.value);
	});
}

/*
* Removes selected items from a <select> box
*/
function fncCommon_Select_Remove_Selected(sourceElement){
	var sourceOptions = $(sourceElement).select('option');
	sourceOptions.reject(function(sourceOption){
		return !sourceOption.selected;
	}).each(function(sourceOption){
		sourceOption.remove();
	});
}

/* 
* Updates <select> option where needle matches value
* 
*/
function fncCommon_Update_Select_Option_ByValue(sourceElement, optionNeedle, optionValue, optionName){
	var sourceOptions = $(sourceElement).select('option');
	sourceOptions.reject(function(sourceOption){
		return sourceOption.value != optionNeedle;
	}).each(function(sourceOption){
		sourceOption.value = optionValue;
		sourceOption.innerHTML = optionName;
	});
}

/*
* Updates <select> option where passed value matches option value
* WTF is this for!
*/
function fncCommon_Update_Select_Option(sourceElement, optionValue, optionName){
	var sourceOptions = $(sourceElement).select('option');
	var rejection = sourceOptions.reject(function(sourceOption){
		return sourceOption.value != optionValue;
	}).each(function(sourceOption){
		sourceOption.value     = optionValue;
		sourceOption.innerHTML = optionName;
	});
		
	if(rejection.length == "0"){
		$(sourceElement).options[$(sourceElement).options.length] = new Option(optionName, optionValue, false);
	}
}

/* Another random function without explanation */
function fncCommon_OnEnterPress(event, callback){
	if(event.keyCode == Event.KEY_RETURN) {
		try {
			eval(callback + '();');
		} catch(ex){
			throw(ex);
		}
	}
}
