//----- Daisy chain functions to execute on load.
function addOnLoad( f ) {
  var oldf = window.onload;

  if (typeof oldf == "function") {
    window.onload = function() {
		      if ( oldf ) {
			oldf();
		      }
		      f();
		    }
  } else {
    window.onload = f;
  }
}

//----- Detect window height
function get_window_height() {
  /* Old code
  if (window.innerHeight) {
    return = window.innerHeight;
  } else if(document.documentElement && document.documentElement.clientHeight) {
    return = document.documentElement.clientHeight;
  }
   */
  // Mozilla, Safari, ...
  if (self.innerHeight)
    return self.innerHeight;
  // IE 6
  if (document.documentElement && document.documentElement.clientHeight)
    return document.documentElement.clientHeight;
  // IE 5
  if (document.body)
    return document.body.clientHeight
  return 0;
}

//----- Resize the main window
function resize_main() {
  var ywin = get_window_height();

  var mainwin = document.getElementById("main");
  mainwin.style.height = (ywin - 98 - 30 - 8) + "px";
}

//----- Resize the main window
function resize_mainwide() {
  var ywin = get_window_height();

  var mainwin = document.getElementById("mainwide");
  mainwin.style.height = (ywin - 98 - 30 - 8) + "px";
}

function print_referrer() {
  if (document.referrer != "") {
    document.getElementById("referrer").innerHTML = "Referrer = " + document.referrer;
  }
}

function preview_pic( img, ar ) {
  //alert( "Hello" );
  if (img != "") {
    var ptag = document.getElementById("preview_pic");
    ptag.innerHTML = "<img src='" + img + "' width='190'>";
    ptag.style.border ="6px solid white";

    //ptag.style.zIndex = "100";
    //alert( ptag.innerHTML );
  }
  return true;
}

function clear_preview() {
  var ptag = document.getElementById("preview_pic");
  ptag.innerHTML = "";
  ptag.style.border ="";
  //ptag.style.zIndex = "-100";
  return true;
}

function read_cookie( c ) {
  var nameEq  = c + "=";
  var cookies = document.cookie;
  var p       = cookies.indexOf(nameEq);
  if (p == -1) {
    return null;
  } else {
    var start = p + nameEq.length;
    var end   = cookies.indexOf(";", start);
    if (end == -1)
      end = cookies.length;
    var value = cookies.substring(start,end);
    return value;
  }
}

// Restore the visibility state to state stored in cookie.
function init_visible() {
  var value = read_cookie( "viscat" );
  if (value == null) {
    //alert( "Cookie is null." );
    var nbits = sid2subcat.length;
    value = "";
    for ( var i = 0; i < nbits; i++ )
      value = value + "0";
    document.cookie = "viscat=" + value;
    //alert( "Writing cookie: " + value );
  } else {
    //alert( "Cookie is " + value );
    var allDivTags = document.getElementsByTagName("div");

    for ( i = 0; i < allDivTags.length; i++ )
      if ( allDivTags[i].className.indexOf("sid") == 0) {
	var el     = allDivTags[i];
	var subcat = parseInt(el.className.substr(3));

	var isVisible = value.substr(subcat,1) == "1";
	el.style.visibility = isVisible ? "visible" : "hidden";
	el.style.display = isVisible ? "inline" : "none";
      }
  }
}

// Set a substring within a string
function setChars( s, at, c ) {
  return s.substr(0,at) +
    c.substr(0,s.length-at) +
    s.substr(at+c.length);
}

// Toggle visibility of items from a subcategory.
function toggle_visible (theClass) {
  var allDivTags = document.getElementsByTagName("div");
  var subcat = parseInt(theClass.substr(3));

  var value = read_cookie( "viscat" );
  if (value == null) {
    alert( "Null cookie viscat unexpected." );
  }

  //alert( "Toggling div " + theClass );
  for ( i = 0; i < allDivTags.length; i++ )
    if ( allDivTags[i].className == theClass) {
      var el = allDivTags[i];
      var isVisible = (el.style.visibility == "hidden") ? true : false;
      value = setChars(value,subcat,(isVisible? "1" : "0") );
      el.style.visibility = isVisible ? "visible" : "hidden";
      el.style.display = isVisible ? "inline" : "none";
    }
  document.cookie = "viscat=" + value;
  value = read_cookie( "viscat" );
  //alert( "Confirmed that cookie has been set to " + value );
}

// Send an AJAX request to the server.  Call the specified function
// when the results come back.
function ajaxReq(func, script /*, key, val, ... */) {
  var s = script + "?";
  for (var i = 2; i+1 < arguments.length; i += 2) {
    if (i > 2) {
      s += "&";
    }
    s += arguments[i] + "=" + encodeURIComponent(arguments[i+1]);
  }
  var http;
  if (window.XMLHttpRequest) {
    http = new XMLHttpRequest();
  } else if (window.ActiveXObject) {
    http = new ActiveXObject("Microsoft.XMLHTTP");
  }
  if (http) {
    http.open("GET", s, true);
    http.onreadystatechange = function() {
      if (http.readyState == 4) {
        func(eval("(" + http.responseText + ")"));
        http.onreadystatechange = null
        http = null
      }
    }
    http.send(null);
  }
}
