/**
 * This function is the main link function.  It is responsible
 * for getting the user from one place to another.  There should be
 * no hard links from one part of the system to another, 
 * instead execute the command and let the module deal with
 * it.
 * \param module The module to work in
 * \param command The command to execute
 * \param parameter An array (or a single string value) containing the parameters to pass to the module.
 */
function execute_command(module, command, parameter)
{
	document.nrs_form.module.value = module;
	document.nrs_form.command.value = command;
	if(typeof parameter == "object")
	{
		document.nrs_form.parameter.value = parameter.join("','");
	}
	else
	{
		document.nrs_form.parameter.value = parameter;
	}
	document.nrs_form.submit();
}

/**
 * This function is an important jquery functions.  First off, it defines all the original bindings
 * that will be applied to the page (such as highlight search, etc), so we need to define it 
 * globally.  However, since each module might need to add to this function, we do this by checking
 * to see if the function "jquery_module_hook" exists, and calling it if it does.  This way, each of the modules 
 * can explicitly add content to this function.
 */
jQuery(document).ready
(
	function()
	{
		//first, execute the module's jquery
		if(typeof jquery_module_hook == "function")
			jquery_module_hook();
		//all text hsould highlight yellow on focus
		jQuery("input[type=text]").focus(
			function()
			{
				jQuery(this).addClass("highlight_input");
			}
		).blur(
			function()
			{
				jQuery(this).removeClass("highlight_input");
			}
		);
		//all text with the class "date_input" should be calendars.
		jQuery('input.date_input').datepicker(
			{ 
				changeYear: true, 
				changeMonth: true, 
				yearRange: '-30:+10'
			});
		//al select boxes should be striped.
		jQuery('select[size] option:odd').addClass('odd');
		
		jQuery('input[type=text][onEnter]').keypress(
			function(e)
			{
				if(e.which == 13)
				{
					eval(jQuery(this).attr('onEnter'));
					return false;
				}
			}
		);
	}
);

/**
 * This function will always be called when an ajax call is made.
 */
function onXajaxCall()
{
	if(typeof xajax_on_call == "function")
		xajax_on_call();
}

/**
 * This function will always be called when an ajax call is returned.
 */
function onXajaxReturn()
{
	//all text hsould highlight yellow on focus
	jQuery("input[type=text]").focus(
		function()
		{
			jQuery(this).addClass("highlight_input");
		}
	).blur(
		function()
		{
			jQuery(this).removeClass("highlight_input");
		}
	);
	//all text with the class "date_input" should be calendars.
	jQuery('input.date_input').datepicker(
		{ 
			changeYear: true, 
			changeMonth: true, 
			yearRange: '1900:2020'
		});
	//al select boxes should be striped.
	jQuery('select[size] option').removeClass('odd');
	jQuery('select[size] option:odd').addClass('odd');
	jQuery('input[type=text][onEnter]').unbind("keypress");
	jQuery('input[type=text][onEnter]').keypress(
			function(e)
			{
				if(e.which == 13)
				{
					eval(jQuery(this).attr('onEnter'));
					return false;
				}
			}
		);
}


/**
 * This function is used to populate a select box with the array options passed.
 * The options are an array of object, where each object has two properties, 
 * value, which is the option value, and label, which is the option label.
 * There is an optional, 3rd paramter that can be passed to this function.  if the 
 * 3rd parameter is true, then it will create the select group using optgroups.
 * The options paramter now must be in the form of:
 *	options = [
 *				{
 *					label: <optgroup label>,
 *					options: [{label: <option label>, value: <option value>},...]
 *				}
 *				...]
 *
 * Also, for any case, if there are extra parameters, either in the optgroup OR the
 * options object, those will be added as extra properties.
 */
function replace_select_options(select_id, options)
{
	var options_str = "";
	if(arguments.length == 3 && arguments[2])
	{
		for(var i = 0; i < options.length; i++)
		{
			options_str += "<optgroup label=\"" + options[i].label + "\"";
			for(var properties in options[i])
			{
				if(properties == "label")
					continue;
				if(properties == "options")
					continue;
				options_str += " " + properties + "=\"" + options[i][properties] + "\"";
			}
			options_str += ">";
			for(var j = 0; j < options[i].options.length; j++)
			{
				options_str += "<option label=\"" + options[i].options[j].label + "\" value=\"" + 
								options[i].options[j].value + "\"";
				for(var properties in options[i].options[j])
				{
					if(properties == "label")
						continue;
					if(properties == "value")
						continue;
					options_str += " " + properties + "=\"" + options[i].options[j][properties] + "\"";
				}
				options_str += ">" + options[i].options[j].label + "</option>";
			}
			options_str += "</optgroup>";
		}
	}
	else
	{
		for(var i = 0; i < options.length; i++)
		{
			options_str += "<option label=\"" + options[i].label + "\" value=\"" + options[i].value + "\"";
			for(var properties in options[i])
			{
				if(properties == "label")
					continue;
				if(properties == "value")
					continue;
				options_str += " " + properties + "=\"" + options[i][properties] + "\"";
			}
			options_str += ">" + options[i].label + "</option>";
		}
	}
	
	jQuery("#" + select_id).html(options_str);
}


/**
 * This command will allow you to prompt the user before the javascript command is executed.
 */
function prompt_js(msg, command)
{
	if(confirm(msg))
	{
		eval(command);
	}
}

/**
 * This function will create the div needed by the online elp system to display the information in.
 * The newly created div will be assigned an id of "contextual_web_help_div", so please do not use that div
 * name on the web pages.
 */
function create_help_div()
{
	if(!e){var e=window.event};
	// assign attributes to pop-up DIV element and append it to
	var div = document.getElementById('contextual_web_help_div');
	if(!div)
	{
		var div = document.createElement('div');
		div.setAttribute('id', 'contextual_web_help_div');
		div.className = 'help_pop_up';
		document.getElementsByTagName('body')[0].appendChild(div);  
	}
}

/**
 * The following functions relate to the online conmtextual help system.
 */
function destroy_help_div(comment_id)
{
	var div = document.getElementById('contextual_web_help_div')
	if(!div){return};
	div.parentNode.removeChild(div);
}

/**
 * THis function creates the search div
 */
function create_search_div()
{
	if(!e){var e=window.event};
	// assign attributes to pop-up DIV element and append it to
	var div = document.getElementById('site_search_div');
	if(!div)
	{
		var div = document.createElement('div');
		div.setAttribute('id', 'site_search_div');
		div.className = 'site_search_div';
		document.getElementsByTagName('body')[0].appendChild(div);  
	}
}

/**
 * The following functions relate to the online conmtextual help system.
 */
function destroy_search_div()
{
	var div = document.getElementById('site_search_div')
	if(!div){return};
	div.parentNode.removeChild(div);
}

/**
 * Simple function that quesries the user to enter a command name before adding the command ql
 */
function add_command_ql()
{
	var cmd = prompt("Enter the name for this QuickLink (if blank, will use default name)", "");
	if(cmd != null)
		xajax_add_command_ql(cmd);
}

/**
 * THis function creates the search div
 */
function create_website_ql_div()
{
	if(!e){var e=window.event};
	// assign attributes to pop-up DIV element and append it to
	var div = document.getElementById('web_ql_div');
	if(!div)
	{
		var div = document.createElement('div');
		div.setAttribute('id', 'web_ql_div');
		div.className = 'web_ql_div';
		document.getElementsByTagName('body')[0].appendChild(div);  
	}
}

/**
 * The following functions relate to the online conmtextual help system.
 */
function destroy_web_ql_div()
{
	var div = document.getElementById('web_ql_div')
	if(!div){return};
	div.parentNode.removeChild(div);
}

/**
 * THis function creates the search div
 */
function create_request_form_div()
{
	if(!e){var e=window.event};
	// assign attributes to pop-up DIV element and append it to
	var div = document.getElementById('request_form_div');
	if(!div)
	{
		var div = document.createElement('div');
		div.setAttribute('id', 'request_form_div');
		div.className = 'request_form';
		document.getElementsByTagName('body')[0].appendChild(div);
	}
}

/**
 * The following functions relate to the online conmtextual help system.
 */
function destroy_request_form_div()
{
	var div = document.getElementById('request_form_div')
	if(!div){return};
	div.parentNode.removeChild(div);
}

/**
 * This function will retireve al the request info from the request pop up form.
 */
function get_ql_request()
{
	var request = new Object();
	request.category_id = jQuery('#ql_request_category_id').val();
	request.calltype = jQuery('#ql_request_calltype').val();
	request.requested_by = jQuery('#ql_request_requested_by').val();
	request.problem = jQuery('#ql_request_problem').val();
	request.room = jQuery('#ql_request_room').val();
	return request;
}

