// Javascript utility functions
function load(url) {
 top.location.href = url
}
function debuglog(message) {
}
// debuglogx() is the log() function from
// http://ajaxcookbook.org/javascript-debug-log/
// by Bret Taylor
// licensed http://creativecommons.org/licenses/by/2.5/
function debuglogx(message) {
 if (!debuglog.window_ || debuglog.window_.closed) {
  var win = window.open("", null, "width=400,height=200," +
            "scrollbars=yes,resizable=yes,status=no," +
            "location=no,menubar=no,toolbar=no");
  if (!win) return;
  var doc = win.document;
  doc.write("<html><head><title>Debug Log</title></head>" +
        "<body></body></html>");
  doc.close();
  debuglog.window_ = win;
 }
 var logLine = debuglog.window_.document.createElement("div");
 logLine.appendChild(debuglog.window_.document.createTextNode(message));
 debuglog.window_.document.body.appendChild(logLine);
}
function parseCookies() {
 var cdict = {}
 debuglog("cookie="+document.cookie)
 var clist = document.cookie.split(';')
 for (var i = 0; i < clist.length; i++) {
  c = clist[i]
  j = 0
  while (c.charAt(j) == ' ') j++
  k = j
  while (k < c.length && c.charAt(k) != '=') k++
  cname = c.substring(j, k)
  cval = (k < c.length) ? c.substring(k + 1, c.length) : ""
  cdict[cname] = cval
  debuglog("cookie "+cname+" = "+cval)
 }
 return cdict
}
function checkCookie(cookiename, actiondescription) {
 // per https://bugzilla.mozilla.org/show_bug.cgi?id=230350
 // navigator.cookieEnabled is probably useless.  If it is false, then
 // we believe it.  If true, we try to set a cookie to make sure.
 debuglog("cookies enabled = "+typeof(navigator.cookieEnabled) + " " +navigator.cookieEnabled)
 var okcookies = typeof(navigator.cookieEnabled) != 'undefined' && navigator.cookieEnabled
 if (okcookies) {
  var d = new Date()
  d.setTime(d.getTime()+10000)
  var testname = "testCookie"
  document.cookie = testname+"=y;expires="+d.toGMTString()+";path=/"
  var c = parseCookies()
  if (testname in c && c[testname] == "y") {
   debuglog("test cookie set "+testname)
  } else {
   okcookies = false
  }
  if (! (cookiename in c)) {
   okcookies = false
  } else {
   debuglog("cookie "+cookiename+": "+c[cookiename])
  }
 }
 if (!okcookies) {
  /* FIXME (DQM): Message should perhaps indicate warning rather than error.
   * E.g. "Please enable cookies in your browser to enable automatic redirection
   * to download pages"
   */
  alert("Please enable cookies in your browser, required for "+actiondescription)
 }
}
function initializeSelect(sel, initval, defidx) {
 // sets 'selected' property of option in a select whose value matches
 // initval and sets 'selectedIndex' property of select to that option index,
 // otherwise sets to defidx (assumed to be an empty entry)
 opts = sel.options
 debuglog("initval "+initval)
 debuglog("defidx "+defidx)
 debuglog("sel.selectedIndex "+sel.selectedIndex)
 sel.selectedIndex = defidx
 for (var i = 0; i < opts.length; i++) {
  debuglog("i "+i)
  debuglog("opts[i].value __"+opts[i].value+"__")
  debuglog("opts[i].selected __"+opts[i].selected+"__")
  if (opts[i].value == initval) {
   opts[i].selected = true
   sel.selectedIndex = i
  } else {
   opts[i].selected = false
  } //if
 } //for
}

function initializeRadio(rad, initval) {
 // sets the 'checked' property of the radiobutton element matching initval
 // to true and all other elements to false.
 for (var i = 0; i < rad.length; i++) {
  if (rad[i].type == "radio") {
   if (rad[i].value == initval) {
    rad[i].checked = true
   } else {
    rad[i].checked = false
   } //if
  } //if
 } //for
}

