var last_menu = null;
var last_menu_parent = null;
var hide_timeout = null;
var last_tab = new Array( );
var ajax_functions = new Array( );
var ajax_default_hash = null;
var last_history = null;

var stamp = new Date( ).getTime( );

function httpgetstring( formid )
{
  if( typeof( tinyMCE ) != "undefined" && tinyMCE.triggerSave )
    tinyMCE.triggerSave( ); // Copy HTML input contents to their form fields

  form = document.forms[formid];

  getstring = "";
  firstval = true;
  for( i = 0; i < form.elements.length; i++ )
  {      
    if( form.elements[i].name )
    {
      if( form.elements[i].type != "checkbox" || form.elements[i].checked )
      {
        if( firstval )
          firstval = false;
        else
          getstring += "&";

        getstring += form.elements[i].name + "=" + encodeURIComponent( form.elements[i].value );
      }
    }
  }

  return getstring;
}

function ajax_hash_exec( hash )
{
  var params = hash.split(",");
  var func = params[0];

  var code = func + "(";

  last_history = hash;

  if( ajax_functions[func] )
  {
    for( var i = 1; i < params.length; i++ )
    {
      params[i] = params[i];

      if( i > 1 )
        code += ",";

      code += params[i];    
    }

    code += ")";
    eval(code);
  }
}

function ajax_allowed_functions( )
{
  for( var i = 0; i < arguments.length; i++ )
    ajax_functions[arguments[i]] = true;
}

function ajax_load( )
{
  var code = "";
  
  code += arguments[0];
  for( var i = 1; i < arguments.length; i++ )
  {
    code += ",";

    if( typeof( arguments[i] ) == "string" )
      code += "\"" + addslashes( arguments[i] ) + "\"";
    else
      code += arguments[i];
  }

  ajax_hash_exec( code );
  window.location.hash = code;
}

function ajax_check_history_changed( )
{
  var new_history = window.location.hash.substring(1); 
 
  if( new_history != last_history )
    ajax_hash_exec( new_history );

  setTimeout( "ajax_check_history_changed()", 200 );
}


function ajax_history_change( )
{
  ajax_hash_exec( new_location );
}     

function ajax_history_init( )
{
  if( window.location.hash )
    ajax_hash_exec( window.location.hash.substring(1) );

  setTimeout( "ajax_check_history_changed()", 200 );
}

function httpload( url, handler, msg )
{
  if( url.indexOf( "?" ) == -1 )
    url += "?dynamicload=" + stamp++;
  else
    url += "&dynamicload=" + stamp++;

  if( msg )
  {
    document.getElementById("ajax_status_container").style.visibility = "visible";
    document.getElementById("ajax_status_text").innerHTML = msg;  
  }

  // Branch for native XMLHttpRequest object
  if( window.XMLHttpRequest )
    var req = new XMLHttpRequest( );   
  // Branch for IE/Windows ActiveX version
  else if( window.ActiveXObject )
    var req = new ActiveXObject( "Microsoft.XMLHTTP" );

  if( req )
  {
    var usepost = (url.length > 1024); // Use POST method if query string will be too long
    var loc;

    //usepost = true; // Always use POST method for now...

    if( usepost )     
    {
      loc = url.indexOf( "?" );

      if( loc != -1 )
      {
        urlportion = url.substring( 0, loc );
        dataportion = url.substring( loc + 1 );
      }
      else
        urlportion = url;

      req.open( "POST", urlportion, true );
    }
    else
      req.open( "GET", url, true );
    
    req.onreadystatechange = function() {
      if( req.readyState == 4 )
      {
        if( handler )
        {
          document.getElementById("ajax_status_container").style.visibility = "hidden";    

          if( typeof( handler ) == "function" )
            handler( req );
          else
            document.getElementById(handler).innerHTML = req.responseText;
        }
      }
    }

    req.setRequestHeader( "Content-Type", "application/x-www-form-urlencoded; charset=UTF-8" ); 

    if( usepost )
      req.send( dataportion );
    else
      req.send( null );

    return false;
  }
  else
    return true;
}

function xml_to_array( xml )
{
  var i;
  var obj;
  
  obj = new Object( );

  for( i = 0; i < xml.childNodes.length; i++ )
  {
    if( xml.childNodes[i].nodeType == 1 )
    {
      if( xml.childNodes[i].childNodes )
      {
        if( !obj[xml.childNodes[i].nodeName] )
          obj[xml.childNodes[i].nodeName] = new Array( );

        obj[xml.childNodes[i].nodeName].push( xml_to_array( xml.childNodes[i] ) );
      }
    }
    else if( xml.childNodes[i].nodeType == 3 )
    {
      obj["value"] = xml.childNodes[i].nodeValue;
    }
  }

  return obj;
}

function xml_to_menu( xml, callback, id, rootname, rooturl, do_items )
{
  var menu_data = "";

  if( xml.menu[0].submenu )
    menu_data = parse_menu_xml( xml.menu[0].submenu, id, do_items, callback );

  menu_data = "new Hash( 1, new Hash( 'contents', '"+rootname.replace(/'|"/g,"\\'")+"', '"+rooturl+"', '' " + menu_data + "))";

  eval( "domMenu_data.set('"+id+"', " + menu_data + ");" );
}

function addslashes( str )
{
  if( !str )
    return str;

  str = str.replace( /\\/g, "\\\\" );
  str = str.replace(/\'|\"/g,"\\\"");
  return str;
}

function parse_menu_xml( xml, id, do_items, callback )
{
  var index = 1;
  var menu_data = "";
  var submenu;

  for( var i = 0; i < xml.length; i++ )
  {   
    if( do_items )
    {       
      var is_item = !xml[i].submenu;
      var is_leaf = !(xml[i].submenu && xml[i].submenu[0].submenu);

      action = is_item ? callback( id, xml[i] ) : "";   

      if( is_item )
      {
        menu_data += ",";
        menu_data += index+", new Hash( 'contents', '"+addslashes(xml[i].name[0].value)+"','uri','"+addslashes(action)+"'";       
        menu_data += ")";

        index++;
      }
      else if( !is_leaf )
      {
        sub_menu_data = parse_menu_xml( xml[i].submenu[0].submenu, id, do_items, callback );
        if( sub_menu_data != "" )
        {
          menu_data += ",";
          menu_data += index+", new Hash( 'contents', '"+addslashes(xml[i].name[0].value)+"','uri','"+addslashes(action)+"'";       
          menu_data += sub_menu_data;
          menu_data += ")";      
  
          index++;
        }
      }
    }
    else
    {
      if( xml[i].submenu ) 
      {
        action = callback( id, xml[i] );   

        menu_data += ",";
    
        menu_data += index+", new Hash( 'contents', '"+addslashes(xml[i].name[0].value)+"','uri','"+addslashes(action)+"'";  
        
        if( xml[i].submenu[0].submenu ) 
          menu_data += parse_menu_xml( xml[i].submenu[0].submenu, id, do_items, callback );

        menu_data += ")";      
      
        index++;
      }
    }
  }

  return menu_data;
}

function setup_dommenu( target, extra )
{
  settings = new Hash(
  'subMenuWidthCorrection', -100,
  'verticalSubMenuOffsetX', 20,
  'verticalSubMenuOffsetY', 0,
  'horizontalSubMenuOffsetY', 5,
  'horizontalSubMenuOffsetX', -50,
  'openMouseoverMenuDelay', -1,
  'openMousedownMenuDelay', 0,
  'closeMouseoutMenuDelay', 200,
  'expandMenuArrowUrl', 'images/li.gif' );

  if( extra )
    settings.merge( extra );

  domMenu_settings.set(target, settings );  
}

function dommenu_set_name( target, name )
{
  document.getElementById(target+"-0").getElementsByTagName("span")[0].innerHTML = name;
}

function get_query_var( name )
{
  var query = window.location.search.substring( 1 );
  var vars = query.split( "&" );

  for( var i = 0; i < vars.length; i++ )
  {
    var pair = vars[i].split( "=" );
    
    if( pair[0] == name ) 
      return pair[1];
  } 

  return "";
}

function create_tabs( group, tabs )
{
  var i;
  var id;
  var classname;

  for( i = 0; i < tabs.length; i++ )
  {
    tabs[i][1] = null; // Ignore images for now...

    if( tabs[i][1] )
    {
      classname = "tabimg";
      img = "<img src=\"" + tabs[i][1] + "\" style=\"vertical-align: middle; margin-right: 3px\" />";
    }
    else
    {
      classname = "tab";
      img = "";  
    }

    do_onclick = "display_tab('" + group + "'," + tabs[i][0] + ");";

    if( tabs[i][3] )
      do_onclick += tabs[i][3];

    document.write( "<div class=\"" + classname + "\" id=\"" + group + tabs[i][0] + "_tab\" onclick=\"" + do_onclick + "\">" + 
                    "<a class=\"tablink\" href=\"javascript:void(0)\">" + img + tabs[i][2] + "</a></div>" );   
  }

  document.write( "<div style=\"clear: left; line-height: 0px; height: 1px; font-size: 0px;\">&nbsp;</div>" );
  document.write( "<div class=\"tabline\">&nbsp;</div>" );
}

function use_tab( group, id )
{
  var target;
  var query_val;
  
  query_val = get_query_var( group + "_tab" );
  if( query_val != "" )
    id = query_val;

  target = group + id; 

  display_tab( group, id );

  change_vis( group, true );

  last_tab[group] = target;
}

function display_tab( group, id )
{
  var obj, parent, tabbox;
  var target = group + id;

  // Tab content not shown if not present
  if( !document.getElementById( target ) )
    return;

  tabbox = document.getElementById( group + "_container" );

  if( last_tab[group] )
  {
    parent = document.getElementById( last_tab[group] + "_tab" );

    change_vis( last_tab[group], false );

    if( parent.className == "tab_on" )
      parent.className = "tab";

    if( last_tab[group] == (group + id) )
    {
      if( tabbox.className == "tabbox_on" )
      {
        tabbox.className = "tabbox";
        last_tab[group] = null;

        return;
      }
    }
  }
 
  parent = document.getElementById( target + "_tab" );
  tabbox.className = "tabbox_on";

  change_vis( target, true );

  if( parent.className == "tab" )
    parent.className = "tab_on";
  else if( parent.className == "tabimg" )
    parent.className = "tabimg_on";

  last_tab[group] = target;
}

function display_menu( target )
{
  if( target )
  {
    if( last_menu )
      hide_menu( );

    if( hide_timeout )
      clearTimeout( hide_timeout );

    var obj = document.getElementById( target );
    var parent = document.getElementById( target + "_parent" );

    parentX = getPageOffsetLeft( parent );
    parentY = getPageOffsetTop( parent );

    // Make sure it fits into the current view area
    if( (parentX + obj.offsetWidth + 10) > (getViewportWidth() + getViewportScrollX()) )
      obj.style.left = (parentX - obj.offsetWidth + 10) + "px";
    else
      obj.style.left = parentX + "px";

    if( (parentY + parent.offsetHeight + obj.offsetHeight + 10) > (getViewportHeight() + getViewportScrollY()) )
      obj.style.top = (parentY - obj.offsetHeight) + "px";
    else
      obj.style.top = (parentY + parent.offsetHeight) + "px";
    
    // Show it
    obj.style.visibility = "visible";

    if( parent.className == "menu" )
      parent.className = "menu_on";

    last_menu = obj;
    last_menu_parent = parent;
  }
  else
    hide_timeout = setTimeout( "hide_menu()", 300 );     
}

function hide_menu( )
{
  if( last_menu_parent.className == "menu_on" )
    last_menu_parent.className = "menu";

  last_menu.style.visibility = "hidden";
  last_menu = null;
  clearTimeout( hide_timeout );
}

function getPageOffsetLeft( el )
{
  var x;

  x = el.offsetLeft;
  if( el.offsetParent )
    x += getPageOffsetLeft( el.offsetParent );

  return x;
}

function getPageOffsetTop( el )
{
  var y;

  y = el.offsetTop;
  if( el.offsetParent )
    y += getPageOffsetTop( el.offsetParent );

  return y;
}

function change_vis( obj )
{
  var o = document.getElementById( obj );

  if( o )
  {
    if( o.style.display == "none" )
      o.style.display = "";
    else
      o.style.display = "none";
  }
}  

function set_vis( obj, vis )
{
  var o = document.getElementById( obj );

  if( vis )
    o.style.display = "";
  else
    o.style.display = "none";
}



// Thanks to http://13thparallel.org/archive/viewport/
getViewportWidth = function() {
  var width = 0;
  if( document.documentElement && document.documentElement.clientWidth ) {
    width = document.documentElement.clientWidth;
  }
  else if( document.body && document.body.clientWidth ) {
    width = document.body.clientWidth;
  }
  else if( window.innerWidth ) {
    width = window.innerWidth - 18;
  }
  return width;
};

getViewportHeight = function() {
  var height = 0;
  if( document.documentElement && document.documentElement.clientHeight ) {
    height = document.documentElement.clientHeight;
  }
  else if( document.body && document.body.clientHeight ) {
    height = document.body.clientHeight;
  }
  else if( window.innerHeight ) {
    height = window.innerHeight - 18;
  }
  return height;
};

getViewportScrollX = function() {
  var scrollX = 0;
  if( document.documentElement && document.documentElement.scrollLeft ) {
    scrollX = document.documentElement.scrollLeft;
  }
  else if( document.body && document.body.scrollLeft ) {
    scrollX = document.body.scrollLeft;
  }
  else if( window.pageXOffset ) {
    scrollX = window.pageXOffset;
  }
  else if( window.scrollX ) {
    scrollX = window.scrollX;
  }
  return scrollX;
};

getViewportScrollY = function() {
  var scrollY = 0;
  if( document.documentElement && document.documentElement.scrollTop ) {
    scrollY = document.documentElement.scrollTop;
  }
  else if( document.body && document.body.scrollTop ) {
    scrollY = document.body.scrollTop;
  }
  else if( window.pageYOffset ) {
    scrollY = window.pageYOffset;
  }
  else if( window.scrollY ) {
    scrollY = window.scrollY;
  }
  return scrollY;
};

start_menu = function() {
if (document.all&&document.getElementById) {
navRoot = document.getElementById("menu");
for (i=0; i<navRoot.childNodes.length; i++) {
node = navRoot.childNodes[i];
if (node.nodeName=="LI") {
node.onmouseover=function() {
this.className+=" over";
  }
  node.onmouseout=function() {
  this.className=this.className.replace(" over", "");
   }
   }
  }
 }
}