/*
        utils.js - a set of javascript utilities
*/

/*
  function to set visiblity of a DOM element
*/
function setVisibility(elem, visibility) {
  nElem = xgetElementById(elem);
  if ((nElem != null) && (!nElem.opened)) {
    nElem.style.visibility = visibility;
  }
}

/*
  function to safely retrieve a DOM element whether
  it is passed in as a DOM element already, or as the
  id of a DOM element.
*/
function xgetElementById(elem) {
  var nElem = elem;
  if (typeof elem == "string") {
    nElem = document.getElementById(elem);
  }
  return nElem;
}

/*
  function to display a new popup window with a size
*/
function showDocument(uri, name, w, h) {
  var sb = "scrollbars=no";
  var mb = ",menubar=no";
  var rsz = ",resizable=yes";
  var dep = ",dependent=yes";
  var raised = ",alwaysRaised=yes";
  var status = ",status=no";
  var title = ",titlebar=no";
  var options = sb + mb + rsz + dep + raised + status + title;

  if (w != null) {
    options = options + ",width=" + w;
  }
  if (h != null) {
    options = options + ",height=" + h;
  }

  var uri = encodeURI(uri);
  var nWindow = window.open(uri, name, options);
  return nWindow;
}


/*
  function to display a new scrolling popup window with a size
*/
function showScrollingDocument(uri, name, w, h) {
  var sb = "scrollbars=yes";
  var mb = ",menubar=no";
  var rsz = ",resizable=yes";
  var dep = ",dependent=yes";
  var raised = ",alwaysRaised=yes";
  var status = ",status=yes";
  var title = ",titlebar=no";
  var options = sb + mb + rsz + dep + raised + status + title;

  if (w != null) {
    options = options + ",width=" + w;
  }
  if (h != null) {
    options = options + ",height=" + h;
  }

  var uri = encodeURI(uri);
  var nWindow = window.open(uri, name, options);
  return false;
}

function createCookie(name, value, days) {
   if (days) {
      var date = new Date();
      date.setTime(date.getTime() + (days*24*60*60*1000));
      var expires = "; expires=" + date.toGMTString();
   }
   else var expires = "";
   document.cookie = name+"="+value+expires+"; path=/";
}

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;
}

function eraseCookie(name) {
   createCookie(name,"",-1);
}
