var g = {};
g.current_tab = 1; // may be overwritten by the page


/**
 * Changes a tab with JS, hiding & showing the appropriate page elements.
 */
g.change_tab = function(tab)
{
  if (g.current_tab == tab)
    return;

  $('tab' + g.current_tab + "_content").style.display = "none";
  $('tab' + tab + "_content").style.display = "block";

  $("data_tab" + g.current_tab).style.backgroundImage = "url(" + g_root_url + "/images/tab_unselected.jpg)";
  $("data_tab" + tab).style.backgroundImage = "url(" + g_root_url + "/images/tab_selected.jpg)";

  g.current_tab = tab;

  return false;
}


g.check_same_address = function(checked)
{
  $("billing_street_address").disabled = checked;
  $("billing_city").disabled = checked;
  $("billing_prov_state").disabled = checked;
  $("billing_zip").disabled = checked;
  $("billing_country").disabled = checked;		
}


g.toggle_search = function()
{
  jQuery("#small_search").slideToggle("slow");
	return false;
}


/**
 * Moves selected option(s) from one select box to another. This generally is used for multi-select
 * boxes to transfer options from one to the other.
 *
 * @param object sel_from the source select box element
 * @param object sel_to the target select box element
 */
g.move_options = function(sel_from, sel_to)
{
  var sel_length = sel_from.length;
  var sel_texts  = [];
  var sel_vals   = [];
  var sel_count = 0;

  var i;

  // find the selected Options in reverse order and delete them from the 'from' Select
  for (i=sel_length-1; i>=0; i--)
  {
    if (sel_from.options[i].selected)
    {
      // if there's no value, that means the lawyer is away. Don't move them.
      if (sel_from.options[i].value == "")
        continue;

      sel_texts[sel_count] = sel_from.options[i].text;
      sel_vals[sel_count]  = sel_from.options[i].value;
      g.delete_option(sel_from, i);
      sel_count++;
    }
  }

  // add the selected text/values in reverse order. This will add the Options to the 'to' Select
  // in the same order as they were in the 'from' Select
  for (i=sel_count-1; i>=0; i--)
    g.add_option(sel_to, sel_texts[i], sel_vals[i]);
}


/**
 * Adds a new option to a select dropdown box.
 *
 * @param object selectbox the select box object
 * @param string text_val the display text of the select box
 * @param string value the value of the select box
 */
g.add_option = function(selectbox, text_val, value)
{
  var new_option = new Option(text_val, value);
  var sel_length = selectbox.length;
  selectbox.options[sel_length] = new_option;
}


/**
 * Deletes an option from a select box.
 *
 * @param object selectbox the select box element
 * @param integer ind the index of the item to remove
 */
g.delete_option = function(selectbox, ind)
{
  var sel_length = selectbox.length;
  if (sel_length > 0)
    selectbox.options[ind] = null;
}


/**
 * Helper function used to select all options in a multi-select dropdown. This is used on form submit
 * to ensure the contents are passed along to the server.
 */
g.select_all = function(el)
{
  for (var i=0; i<el.length; i++)
    el[i].selected = true;

  return true;
}

/**
 * Called on page load for all pages. It initializes the top banner tooltip and sets up the
 * search. 
 */
window.addEvent('domready', function() {
  var Tips2 = new Tips($$('.titles'), { 
  		initialize:function(){
    			this.fx = new Fx.Style(this.toolTip, 'opacity', {duration: 500, wait: false}).set(0);
  		},
  		onShow: function(toolTip) { this.fx.start(1);	},
  		onHide: function(toolTip) { this.fx.start(0); }
  	});

  jQuery("#small_search").hide();		
});

