﻿function w_getClientId(clientId) {
  var elem = null;
  var ci = clientId;

  while (ci && (elem == null)) {
    try { eval("elem = document.getElementById('" + ci + "_content')"); }
    catch (x) { elem = null; }
    if (!elem) {
      if (ci.indexOf('_') < 0) ci = '';
      else ci = getClientId(ci);
    }
  }
  return ci;
}

function w_getValue(clientId, id) {
  var val = null;
  var ci = clientId;

  while (ci && (val == null)) {
    try { eval("val = " + ci + "_" + id); }
    catch (x) { val = null; }
    if (!val) {
      if (ci.indexOf('_') < 0) ci = '';
      else ci = getClientId(ci);
    }
  }
  return val;
}

function w_setValue(clientId, id, val) {
  var ci = clientId;
  var success = true;

  while (ci && (val == null)) {
    try {
      eval(ci + "_" + id + " = val");
      success = true;
    }
    catch (x) { success = false; }
    if (!success) {
      if (ci.indexOf('_') < 0) ci = '';
      else ci = getClientId(ci);
    }
  }
}

function w_getElement(clientId, id) {
  var elem = null;
  var ci = clientId;

  while (ci && (elem == null)) {
    try { eval("elem = document.getElementById('" + ci + "_" + id + "')"); }
    catch (x) { elem = null; }
    if (!elem) {
      if (ci.indexOf('_') < 0) ci = '';
      else ci = getClientId(ci);
    }
  }
  return elem;
}

function getWidgetBaseId(id, length) {
  var baseId = "";

  if (id) {
    var spl = id.split('_');
    for (var i = 0; i < length; i++) {
      if (i > 0) baseId += '_';
      baseId += spl[i];
    }
  }
  return baseId;
}

function toggleView(page, hideIndex, showIndex) {
  var doHide = false;

  if (showIndex.length) {
    for (var i = 0; i < showIndex.length; i++) {
      var lines = getChildNode(page, showIndex[i], true);
      if (lines) { lines.style.display = displayed(lines); doHide = true; }
    }
  }
  else {
    var lines = getChildNode(page, showIndex, true);
    if (lines) { lines.style.display = displayed(lines); doHide = true; }
  }

  if (doHide) {
    if (hideIndex.length) {
      for (var i = 0; i < hideIndex.length; i++) {
        var lines = getChildNode(page, hideIndex[i], true);
        if (lines) lines.style.display = "none";
      }
    }
    else {
      var lines = getChildNode(page, hideIndex, true);
      if (lines) lines.style.display = "none";
    }
  }
}

function displayed(elem) {
  if (elem) {
    var tn = elem.tagName.toLowerCase();

    return document.all ? "block" : (tn == "td" ? "table-cell" : (tn == "tr" ? "table-row" : (tn == "table" ? "table" : "block")));
  }
  return "";
}

