/*
 * mh.js - scripts for global site.
 *
 */

function test () { alert ("Hello, world"); }

/**
 *
 **/
function openHelpWindow (uri)
{
  var win =
  window.open (uri,
	       "help_window",
	       "height=600,width=500,scrollbars,menubar,toolbar,status,resizable");
  win.focus ();
  return false;
}

// -------------------------------------------------------------------------
// -------------------------------------------------------------------------
// -------------------------------------------------------------------------

/**
 * Fetch the scroll position of this webpage.
 **/
function getScrollTop ()
{
  // all except Explorer
  if (window.pageYOffset) {
    return window.pageYOffset;
  }
  // Explorer 6 Strict
  else if (document.documentElement && document.documentElement.scrollTop) {
    return document.documentElement.scrollTop;
  }
  // all other Explorers
  else if (document.body) {
    return document.body.scrollTop;
  }
  // no scrollTop available.
  else {
    return null;
  }
}

function setScrollTop (y) { window.scrollTo (0, y); } 

// -------------------------------------------------------------------------
// -------------------------------------------------------------------------
// -------------------------------------------------------------------------

/*
These JS functions were copied from QuirksMode:
   http://www.quirksmode.org/js/cookies.html
QuirksMode is an excellent reference site for Javascript.
*/

/**
 * Javascript create cookie
 **/
function createCookie(name,value,hours)
{
  if (hours)
    {
      var date = new Date();
      date.setTime(date.getTime()+(hours*60*60*1000));
      var expires = "; expires="+date.toGMTString();
    }
  else
    var expires = "";
  document.cookie = name+"="+value+expires+"; path=/";
}

/**
 * Javascript read cookie
 **/
function readCookie(name)
{
  var nameEQ = name + "=";
  var ca = document.cookie.split(';');
  for(var i=0;i < ca.length;i++)
    {
      var c = ca[i];
      while (c.charAt(0)==' ') c = c.substring(1,c.length);
      if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
    }
  return null;
}

/**
 * Javascript erase cookie
 **/
function eraseCookie(name)
{
  createCookie(name,"",-1);
}



// -------------------------------------------------------------------------
// ---- HTML Viewport ------------------------------------------------------
// -------------------------------------------------------------------------

// XXX add a method here that does HtmlClient or whatever to put HTML
// based on an URI.

// XXX rename us


/**
 *
 **/
function setViewportHtmlUri (id, uri)
{
  // alert ("got request for URI == "+uri);

  var xmlhttp = makeXmlHttp ();

  // --- Make an open call.
  xmlhttp.open ("GET", uri, true);

  // --- setup the callback.
  // XXX what about timeouts???
  xmlhttp.onreadystatechange = function () {

    // --- wait until it's done, and we get a reasonable status code...
    // --- and then, set the viewport HTML code.
    if (xmlhttp.readyState==4 && xmlhttp.status == 200)
      setViewportHtml (id, xmlhttp.responseText);
  };

  // --- send it!
  xmlhttp.send (null);
}


/**
 *
 **/
function setViewportHtmlFragment (id, fragmentId)
{ setViewportHtml (id, htmlFragmentById[fragmentId]); }

/**
 * Set the HTML in an HTML 'viewport': a region that has different
 * messages at different times.
 **/
function setViewportHtml (id, html)
{ setInnerHtml (getElement (id), html); }

var htmlFragmentById = new Object ();

function registerQuotedHtmlFragment (id, v)
{
  v = v.replace (/&gt;/g, ">");
  v = v.replace (/&lt;/g, "<");

  // --- MUST do this one last...
  v = v.replace (/&amp;/g, "&");

  registerHtmlFragment (id, v);
}
function registerHtmlFragment (id, v)
{
  htmlFragmentById[id] = v;
}

// -------------------------------------------------------------------------
// ---- Expando Regions ----------------------------------------------------
// -------------------------------------------------------------------------

/*
 Discussion:

 Expando regions are a mechanism in SWML that let you do simple
 expand/collapse behavior for pages.

*/

/**
 * Toggles an expando region: flips the states of the hidden/shown regions.
 **/
function toggleExpando (id, disp)
{
  if (disp == null) disp = "block";

  if (isExpandoShown (id)) hideExpando (id, disp);
  else                     showExpando (id, disp);
}

/**
 * Hides the expando region (shows the 'hide' view, hides the 'show' view)
 **/
function hideExpando (id, disp)
{
  if (disp == null) disp = "block";

  var hideElem = getElement (id+"_hide");
  var showElem = getElement (id+"_show");

  setVisibility (hideElem, disp);
  setVisibility (showElem, "none");
}

/**
 * Shows the expando region (shows the 'show' view, hides the 'hide' view)
 **/
function showExpando (id, disp)
{
  if (disp == null) disp = "block";

  var hideElem = getElement (id+"_hide");
  var showElem = getElement (id+"_show");

  setVisibility (hideElem, "none");
  setVisibility (showElem, disp);
}

/**
 * Returns true iff this expando is hidden (the 'hide' view is shown,
 * or the 'show' view is hidden)
 **/
function isExpandoHidden (id)
{
  var hideElem = getElement (id+"_hide");
  var showElem = getElement (id+"_show");

  if (showElem != null)
    return getVisibility (showElem) == "none";
  else if (hideElem != null)
    return getVisibility (hideElem) != "none";
}

/**
 * Returns true iff this expando is shown (the 'show' view is shown,
 * or the 'hide' view is hidden)
 **/
function isExpandoShown (id)
{
  var hideElem = getElement (id+"_hide");
  var showElem = getElement (id+"_show");

  if (showElem != null)
    return getVisibility (showElem) != "none";
  else if (hideElem != null)
    return getVisibility (hideElem) == "none";
}


// -------------------------------------------------------------------------
// ---- Utility Methods ----------------------------------------------------
// -------------------------------------------------------------------------

function submitActionForm (formName, actionField, actionValue)
{
  setValueById (actionField, actionValue);
  // alert (actionField+" = "+getValue (actionField));
  submitForm (formName);
}

/**
 *
 **/
function submitForm (formName)
{
  var form = document[formName];
  form.submit ();
}

/**
 *
 **/
function resetForm (formName)
{
  var form = getElement (formName);
  form.reset ();
}

/**
 *
 **/
function getFormFields (formName)
{
  var r = "";

  var first = true;
 
  var form = document[formName];
  for (var i in form.elements)
    {
      var element = form.elements[i];

      if (element != null)
	{
	  var name;
	  if (element.id != null) name = element.id;
	  else if (element.name != null) name = element.name;

	  // --- fetch only pleasant controls (that have a name and a value)
	  if (element.value != null && name != "" && name != null)
	    {
	      if (first) first = false; else r += "&";

	      r += name + "=" + element.value
	    }
	}
    }

  // --- finish building the standard string.
  return "?"+r;
}

function hasValue (objectId)
{
  var elem = getElement (objectId);

  var r = elem != null && (elem.value != null && elem.value != "");

	return r;
}

function getValue (objectId)
{
  var elem = getElement (objectId);
  return elem.value;
}

function setValueById (objectId, value)
{
  var elem = getElement (objectId);

  elem.value = value;
}

/**
 * Cross-browser function to get an object given its id.
 **/
function getElement (objectId)
{
  // --- W3C DOM
  if (document.getElementById && document.getElementById (objectId))
    return document.getElementById(objectId);

  // --- MSIE 4 DOM
  else if (document.all && document.all(objectId))
    return document.all(objectId);

  // --- otherwise, there's no general way of getting it...
  else
    return null;
}

function setVisibility (elem, visibility)
{
  if (elem != null) elem.style.display = visibility
}

function setVisibilityById (id, visibility)
{
  setVisibility (getElement (id), visibility);
}

function getVisibility (elem)
{
  if (elem == null) return null;
  return elem.style.display;
}

/**
 * Set the innerHTML on the element.
 **/
function setInnerHtml (elem, html)
{
  // XXX do more cross-browser stuff here (this works in 
  elem.innerHTML = html;
}


/**
 * Factory to create an xmlhttp object in a browser neutral way.
 *
 * Largely stolen from tutorial at:
 *    http://jibbering.com/2002/4/httprequest.html
 *
 * Seems wonky, but claims to support all browsers, and is sufficiently
 * weird that it seems best to leave it alone ...
 */
function makeXmlHttp ()
{
   var xmlhttp=false;

   /*@cc_on @*/
   /*@if (@_jscript_version >= 5)
   // JScript gives us Conditional compilation, we can cope with old IE
   // versions and security blocked creation of the objects.
    try {
     xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
    } catch (e) {
     try {
      xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
     } catch (E) {
      xmlhttp = false;
     }
    }
   @end @*/
   if (!xmlhttp && typeof XMLHttpRequest!='undefined')
     xmlhttp = new XMLHttpRequest();

   return xmlhttp
}

