function getOffsetPage(ref) {
  var x = ref.offsetLeft, y = ref.offsetTop, node = ref.offsetParent;
  do {
    if (node.offsetLeft)
      x += node.offsetLeft;
    if (node.offsetTop)
      y += node.offsetTop;
  } while (node = node.offsetParent);

  return {
    x: x,
    y: y,
    left: x,
    top: y,
    toString: function() { return '[' + this.x + ',' + this.y + ']'; }
  };
}

var timeoutId, menu_id;

function holdMenu() {
  window.clearTimeout(timeoutId);
}

function showMenu(ref, id) {
  var pos = getOffsetPage(ref);
  var div = document.getElementById(id);
  div.style.left = pos.x + 72 + ref.clientWidth + 'px';
  div.style.top = pos.y + 'px';
  div.style.display = 'inline';
}

function shideMenu(id) {
  menu_id = id;
  timeoutId = window.setTimeout(
    function() {
      document.getElementById(id).style.display = 'none';
    }, 500
  );
}

function alterC2(c1) {
  if (!cats || c1.selectedIndex < 1)
    return;
  var f = c1.form, c2 = f.c2, index = 1;
  c2.options.length = 1;
  for (var i in cats['c' + c1.value]) {
    if (i.indexOf('cc') == 0)
      continue;
    var opt = new Option(cats['c' + c1.value][i], i.substring(1));
    c2.options[index++] = opt;
  }
}

function alterC3(c2) {
  var f = c2.form, c1 = f.c1, q = f.q, index = 1;
  if (!cats || c1.selectedIndex < 1 || c2.selectedIndex < 1)
    return;

  q.options.length = 1;
  for (var i in cats['c' + c1.value]['cc' + c2.value]) {
    var opt = new Option(cats['c' + c1.value]['cc' + c2.value][i], cats['c' + c1.value]['cc' + c2.value][i]);
    q.options[index++] = opt;
  }
}
