var mac = /Konqueror|Safari|KHTML/.test(navigator.userAgent);
var ajax_img = "/images/icons/ajax.gif";

// Lookup an HTML element.
// (Same as $("blah") in prototype.js, without overhead.)
function e(id) {
  if (document.getElementById)
    return(document.getElementById(id));
  else if (document.all)
    return(document.all[id]);
  return(null);
}

// Increment a string by numerical counter at end.
function inc_string(x) {
  var x2 = x.replace(/_[0-9]*$/, "");
  if (x2.length == x.length) {
    return(x2 + "_2");
  } else {
    x = x.replace(/.*_/, "");
    return(x2 + "_" + (parseInt(x)+1));
  }
}

// Get position relative to top left corner of document.
function position(e) {
  var x = 0;
  var y = 0;
  do {
    x += e.offsetLeft || 0;
    y += e.offsetTop  || 0;
    if (mac && e.offsetParent == document.body &&
      e.style["position"] == "absolute")
      break;
  } while (e = e.offsetParent);
  return [x, y];
}

// Overwrite HTML inside a div.
function write_html(div, html) {
  div.innerHTML = html
  div.style.display = "block";
}

// Handy helper: writes a nice little message in a div.
function flash_notice(div, text, center) {
  var str = "<i>" + text + "</i>";
  str += " <img src='"+ajax_img+"' style='border:0px; margin:0px'/>";
  if (center) str = "<center>" + str + "</center>";
  div.innerHTML = "<br/>" + str + "<br/>";
  div.style.display = "block";
}

// Handy helper: writes error message along with "cancel" button.
function flash_error(div, text, center) {
  var str = text + "<br/><br/>";
  str += "<input type='button' value='Continue' " +
    "onclick='clear_error(\"" + div.id + "\")'/>";
  if (center) str = "<center>" + str + "</center>";
  div.innerHTML = "<br/>" + str + "<br/>"
  div.style.display = "block";
}

// Clears the above error message.
function clear_error(id) {
  var div = e(id);
  div.innerHTML = "";
  div.style.display = "none";
}

// Make a div visible.
function show_div(div) {
  div.style.display = "block";
}

// Make a div hidden.
function hide_div(div) {
  div.style.display = "none";
}

// Move a div to a specific location.
function move_div(div, x, y) {
  div.style.left = x + "px";
  div.style.top  = y + "px";
}

// Try to center a div in the window.
function center_div(div) {
  var w = div.offsetWidth;
  var h = div.offsetHeight;
  var sx = document.documentElement.scrollLeft || document.body.scrollLeft;
  var sy = document.documentElement.scrollTop  || document.body.scrollTop;
  var win = window_size();
  var sw = win[0];
  var sh = win[1];
  var x = Math.round((sw - w) / 2);
  var y = Math.round((sh - h) / 2);
  if (x < 0) x = 0;
  if (y < 0) y = 0;
  div.style.left = (x + sx) + "px";
  div.style.top  = (y + sy) + "px";
}

// Get inner size of browser window.  This is a bit tricky.
// Apparently it can fail on Safari: if the document height
// is between the window height and window height plus scroll
// bar width, if will incorrectly choose the document height.
function window_size() {
  var sw, sh;
  var dw = document.width;
  var dh = document.height;
  var sw1 = document.body.clientWidth;
  var sw2 = document.documentElement.clientWidth;
  var sh1 = document.body.clientHeight;
  var sh2 = document.documentElement.clientHeight;
  var sh3 = window.innerHeight;
  sw = sw1 != 0 && sw1 != dw ? sw1 : sw2 != 0 ? sw2 : dw;
  sh = sh1 != 0 && sh1 != dh ? sh1 : sh2 != 0 ? sh2 : dh;
  if (sh3 != 0 && sh3 < sh) sh = sh3;
  return [sw, sh];
}

// Make sure the top/left of a div is visible.
function ensure_visible(div) {
  var pos = position(div);
  var win = window_size();
  var x  = pos[0]-10;
  var y  = pos[1]-10;
  var w  = div.offsetWidth;
  var h  = div.offsetHeight;
  var sx = document.documentElement.scrollLeft || document.body.scrollLeft;
  var sy = document.documentElement.scrollTop  || document.body.scrollTop;
  var sw = win[0];
  var sh = win[1];
  if (sx+sw < x+w) sx = x+w-sw;
  if (sy+sh < y+h) sy = y+h-sh;
  if (sx > x) sx = x;
  if (sy > y) sy = y;
  window.scrollTo(sx, sy);
}

// Try to center the div vertically.
function warp_center(div) {
  var pos = position(div);
  var win = window_size();
  var x  = pos[0];
  var y  = pos[1];
  var w  = div.offsetWidth;
  var h  = div.offsetHeight;
  var sx = document.documentElement.scrollLeft || document.body.scrollLeft;
  var sy = document.documentElement.scrollTop  || document.body.scrollTop;
  var sw = win[0];
  var sh = win[1];
  // Make sure left side is visible.
  if (sx+sw < x+w) sx = x+w-sw;
  if (sx > x) sx = x;
  // Center vertically.
  sy = y - (sh - h) / 2;
  if (sy < 0) sy = 0;
  window.scrollTo(sx, sy);
}
