var loadFunctions = new Array();
var resizeFunctions = new Array();

// Adds a new function to be called during the body's onLoad event.
function addLoadFunction(func)
{
	loadFunctions[loadFunctions.length] = func;
}

// Adds a new function to be called during the window's onResize event.
function addResizeFunction(func)
{
	resizeFunctions[resizeFunctions.length] = func;
}

// set the events to execute all registered functions
window.onload = new Function("executeFunctionArray(loadFunctions)");
window.onresize = new Function("executeFunctionArray(resizeFunctions)");

function executeFunctionArray(functions)
{
	// run every function in the array
	for (var i = 0; i < functions.length; i++)
	{
		functions[i]();
	}
}

function make_copy_button(url, button_text)
{
	if (is_ie)
	{
		document.write("<form action='javascript:copy_url(\"" + url + "\")'>");
		document.write("<input type='submit' value='" + button_text + "'>");
		document.write("</form>");
	}
}

function copy_url(url)
{
 	clipboardData.setData('TEXT', url); 
}

// goes to the given url if the user answers "OK" to the confirmation dialog.
// returns true for OK, false for Cancel.
function confirm_link_activation(question, url)
{
	if (confirm(question))
	{
		window.location = url;
		return true;
	}
	else
	{
		return false;
	}
}

function open_popup(url) 
{ 
	window.open(url, '', 'width=500, height=400, scrollbars, resizable'); 
	// returns false since this javascript is following the link,
	// and we want to make sure the browser doesn't then follow the href
	return false;
}


// opens a new window (with no browser features except scrollbars)
// with the given name and brings it to the foreground.
// This keeps us from using the 'target' attribute on the link,
// because non-JS browsers will open up a new window, and we can't control that new window
// (like close it, or bring it to the foreground if it's already opened).
// So instead we make non-JS browsers open it in the same window, but JS browsers
// open it in a new window.
// 'element' can be either a form or a link, since both have the 'target' property.
// The additional url is optional, and is only needed to use one URL for
// non-JS browsers, and another for JS browsers.
function open_new_window(name, element, different_url) 
{ 
	w = window.open('', name, 'scrollbars, resizable, menubar=yes'); 
	w.focus();
	element.target = name;

	if (different_url) {
		if (element.href) {
			element.href=different_url;
		}  else if (element.action) {
			element.action=different_url;
		}
	}
	
	return true;
}


// Returns the Y offset of the browser's window.
function getScrollY() 
{
  var offset = 0;
  if (typeof( window.pageYOffset ) == 'number') 
  {
    //Netscape compliant
    offset = window.pageYOffset;
  } 
  else 
  {
    if (document.body && document.body.scrollTop) 
    {
      //DOM compliant
      offset = document.body.scrollTop;
    } 
    else 
    {
      if (document.documentElement && document.documentElement.scrollTop) 
      {
      	//IE6 standards compliant mode
        offset = document.documentElement.scrollTop;
      }
    }
  }
  
  return offset;
}

function getOffsetFromBottom()
{
	return getScrollY()-getBodyHeight();
}

// opens the given URL so that certain elements in the browser do not move 
// relative to the window.
// Adds "pos" to the URL.
// If top is true, the url is relative to the top of the browser; if
// false, from the bottom.  
function openAtSameOffset(top, url) 
{
  if (top)
  {
    window.location = url + "&pos=" + String(getScrollY());
  }
  else
  {
    window.location = url + "&pos=" + String(getOffsetFromBottom());
  }
}

// Does the same thing as openAtSameOffset, but for FORM submits 
// instead of links. Note: you MUST have an input named "pos" in the
// form!
function submitAtSameOffset(top, form)
{
	newPos = 0;
	if (top)
	{
		newPos = String(getScrollY());
	}
	else
	{
		newPos = String(getOffsetFromBottom());
	}
	
	form.elements["pos"].value = newPos;
	form.submit()
}

// moves the page to the value given in pos, relative to the
// top of the page if it is positive, or the bottom
// if it is negative.
function reposition(pos)
{
	if (pos < 0)
	{
		window.scrollTo(0, getBodyHeight() + pos);
	}
	else
	{	
		window.scrollTo(0, pos);
	}	
}

function getBodyHeight()
{
	if (document.documentElement && document.documentElement.clientHeight)
	{
		// IE 6
		height = document.documentElement.scrollHeight;
	}
	else if (document.body && document.body.scrollHeight)
	{
		// IE 5 and others
		height = document.body.scrollHeight;
	}
	else if (document.body && document.body.offsetHeight)
	{
		// Netscape 6/Mozilla
		height = document.body.offsetHeight;
	}
	else
	{
		// old NS (4)
		height = document.height;
	}
		
	return height;
}

function getBodyWidth() 
{
  var width = 0;
  if (typeof(window.innerWidth) == 'number')
   {
    // Non-IE
    width = window.innerWidth;
   } 
   else 
   {
    if (document.documentElement &&
        (document.documentElement.clientWidth || document.documentElement.clientHeight)) 
    {
      //IE 6+ in 'standards compliant mode'
      width = document.documentElement.clientWidth;
     } 
     else 
     {
       if (document.body && ( document.body.clientWidth || document.body.clientHeight)) 
       {
         //IE 4 compatible
         width = document.body.clientWidth;
      }
    }
  }
  
  return width;
}

function getBodyHeightTest()
{
	if (document.documentElement && document.documentElement.clientHeight)
	{
		// IE 6
		height = document.documentElement.scrollHeight;
		version = "IE6";
	}
	else if (document.body && document.body.scrollHeight)
	{
		height = document.body.scrollHeight;
		version = "IE+5";
	}
	else if (document.body && document.body.offsetHeight)
	{
		// Netscape
		height = document.body.offsetHeight;
		version = "NS";
	}
	else
	{
		height = document.height;
		version = "unknown";
	}
		
	return [ height, version, getScrollY() ];
}
