// Centered popup window
// Usage: <a href="JavaScript:openCenteredWindow(popupUrl, popupName, width, height, popupFeatures)">link</a>
function openCenteredWindow(popupUrl, popupName, popupWidth, popupHeight, popupFeatures)
{
  if (popupWidth > screen.availWidth-60)
    popupWidth = screen.availWidth-60;
  if (popupHeight > screen.availHeight-60)
    popupHeight = screen.availHeight-60;
  if (screen.availWidth) {
    var winl = (screen.availWidth-popupWidth)/2;
    var wint = (screen.availHeight-popupHeight)/2;
  }
  else {
    winl = 0;
    wint = 0;
  }
  if (winl < 0)
    winl = 0;
  if (wint < 0)
    wint = 0;
  var settings = 'height=' + popupHeight + ',';
  settings += 'width=' + popupWidth + ',';
  settings += 'top=' + wint + ',';
  settings += 'left=' + winl + ',';
  settings += popupFeatures;
  win = window.open(popupUrl, popupName, settings);
  win.window.focus();

  return false;
}

// Check all checkboxes in group
function checkBoxes(cGroup, cChecked)
{
  if (cGroup.length) {
    // we have an array
    for (var c = 0; c < cGroup.length; c++)
      cGroup[c].checked = cChecked;
  }
  else {
    // we have a single input
    cGroup.checked = cChecked;
  }
}

function uncheckCheckboxValue(cGroup, cValue, cChecked)
{
  if (cGroup.length) {
    // we have an array
    for (var c = 0; c < cGroup.length; c++) {
      if (cGroup[c].value == cValue) {
        cGroup[c].checked = cChecked;
        break;
      }
    }
  }
  else {
    // we have a single input
    if (cGroup.value == cValue)
      cGroup.checked = cChecked;
  }
}


// Get cookie identified by name
function getCookie (name)
{
  var arg = name + "=";
  var alen = arg.length;
  var clen = document.cookie.length;
  var i = 0;
  while (i < clen) {
    var j = i + alen;
    if (document.cookie.substring(i, j) == arg)
      return getCookieVal (j);
    i = document.cookie.indexOf(" ", i) + 1;
    if (i == 0) break;
  }
  return null;
}

function getCookieVal(offset)
{
  var endstr = document.cookie.indexOf (";", offset);
  if (endstr == -1)
    endstr = document.cookie.length;
  return unescape(document.cookie.substring(offset, endstr));
}

// Set cookie identified by name with value
function setCookie (name, value)
{
  var argv = setCookie.arguments;
  var argc = setCookie.arguments.length;
  var expires = (argc > 2) ? argv[2] : null;
  var path = (argc > 3) ? argv[3] : null;
  var domain = (argc > 4) ? argv[4] : null;
  var secure = (argc > 5) ? argv[5] : false;
  document.cookie = name + "=" + escape (value) +
                    ((expires == null) ? "" : ("; expires=" + expires.toGMTString())) +
                    ((path == null) ? "" : ("; path=" + path)) +
                    ((domain == null) ? "" : ("; domain=" + domain)) +
                    ((secure == true) ? "; secure" : "");
}

// Delete cookie identified by name
function deleteCookie (name)
{
  var exp = new Date();
  Exp.setTime (exp.getTime() - 1);
  var cval = getCookie (name);
  document.cookie = name + "=" + cval + "; expires=" + exp.toGMTString();
}

