/**
 * general.js
 *
 * This function contains all functions that are generic enough to be used throughout
 * the script. 
 */ 

if (typeof g == "undefined")
  g = {};


/**
 * Bootstrap function. All onload handlers should be added through this function, rather than
 * explicitly set through window.onload (or through the body onload attribute!)
 */
g.addOnloadEvent = function(func)
{
	if (window.onload != "function")
		window.onload = func;
	else
	{
		var old_onload = window.onload;
		window.onload = function()
		{
		  old_onload();
		  func();
		}
	}
}

/**
 * Opens a URL in a popup window.
 */
g.open_window = function(url)
{
  if (!url)
    alert("Sorry, there doesn't appear to be a URL specified.");
  else
    window.open(url, "verify_url", "width=900,height=600,menu=no,toolbar=no,resizable=yes");
}


/**
 * The default error handler for the rsv validation library. 
 */
function g_rsvErrors(f, errorInfo)
{
  var errorHTML = rsv.errorTextIntro + "<br /><br />";
  for (var i=0; i<errorInfo.length; i++)
  {
    errorHTML += rsv.errorHTMLItemBullet + errorInfo[i][1] + "<br />";

    if (rsv.styleOffendingFields)
      rsv.styleField(errorInfo[i][0], i==0);
  }

  if (errorInfo.length > 0)
  {
    g.displayMessage(rsv.errorTargetElementId, 0, errorHTML);
	  return false;
	}

	return true;
}


/**
 * Generic function for displaying a message in the UI, as returned from an Ajax response handler. This 
 * does all the fancy-pants stuff like blind-downing the message, and changing the colour on the message
 * with the Fade Anything Technique (fat.js) to draw attention to it.
 *
 * Assumption: the element with the target_id contains a single DIV tag. This is done so that the styles
 * may be applied to the inner DIV, allowing the outer div to be smoothly blind-upped and -downed.  
 *
 * @param string target_id the HTML target element 
 * @param boolean success whether this is an error or a notification
 * @param string message the message to display
 */
g.displayMessage = function(target_id, success, message)
{
  var messageClass = (success == 1) ? "notify" : "error";

  // remove all old class names and add the new one
  var class_names = $(target_id).classNames();
  for (var i=0; i<class_names.length; i++)
    $(target_id).removeClassName(class_names[i]);

  $(target_id).addClassName(messageClass);
  $(target_id).innerHTML = "<div style=\"padding:8px\">" 
    + "<a href=\"#\" onclick=\"return g.hideMessage('" + target_id + "')\" style=\"float:right\">X</a>"
    + message + "</div>";
  $(target_id).style.display = "block";

	// add the nice fade effect for the notification message
	Fat.fade_element(target_id, 60, 1500, '#FFFC05', '#ffffdd');
}


/**
 * Hides a message on the screen by fading it out and blinding up at the same time.
 */
g.hideMessage = function(target_id)
{
  Effect.BlindUp($(target_id));
  Effect.Fade($(target_id));
  
  return false;
}


/**
 * Simple helper function that's called on all server-side requests; it checks that the
 * person is currently logged in. If they're not, it sends them to the logout page. 
 */
g.checkLoggedIn = function(is_logged_in)
{
  if (!is_logged_in)
    top.location = g.logoutURL + "?session_timeout=1";
}

Array.prototype.contains = function(value)
{
  var found = false;
   
  for (var i=0; i<this.length; i++)
  {
    if (this[i] == value)
      found = true;
  }
   
  return found;
}

String.prototype.ucFirstLetter = function() { return this.substr(0,1).toUpperCase() + this.substr(1); }
String.prototype.lcFirstLetter = function() { return this.substr(0,1).toLowerCase() + this.substr(1); }


/**
 * Called *within* a page to change tabs.
 *
 * @param integer tab the tab number
 * @param string page the unique page anchor (used for maintaining history)
 */
g.changeTab = function(tab, page)
{
  if (g.currentTab == tab)
    return false;

  $("tab" + g.currentTab + "_content").hide();
  $("tab" + tab + "_content").show();
  $("data_tab" + g.currentTab).style.backgroundImage = "url(" + g.rootURL + "/images/tab_unselected.jpg)";
  $("data_tab" + tab).style.backgroundImage = "url(" + g.rootURL + "/images/tab_selected.jpg)";
	
  g.currentTab = tab;

  return false;  
}
