/* copyright 2009-2010 leisure time inc */

dieOldBrowserAlready = false;
base_href = "/catalog/";

function dieOldBrowser() {
  if (dieOldBrowserAlready) return;
  dieOldBrowserAlready = true;
  var ok = confirm("This web browser doesn't support HTML DOM 1, introduced in 1998. Do you want to learn about newer web browsers?");
  if (ok) window.location = "http://www.end6.org/";
}

//ELement VISibility lives!
function setElVis(idName, state) {
  if (!document.getElementById) return dieOldBrowser();
  var el = document.getElementById(idName);
  if (!el) return alert("Page contains no " + idName);
  var st = el.style;
  if (!st) return dieOldBrowser();
  el.style.visibility = state ? 'visible' : 'hidden';
}

// Ends the processing (propagation, default action) of an event.
// Example: return killEvent(e);
function killEvent(e) {
  if (e.stopPropagation) e.stopPropagation();
  if (e.preventDefault) e.preventDefault();
  e.cancelBubble = true;
  return false;
}

function status(txt) {
  var el = document.getElementById('status');
  if (!el) return;
  while (el.firstChild!==null) {
    el.removeChild(el.firstChild); // remove all existing content
  }
  el.appendChild(document.createTextNode(txt));
}
function makeEl(tagName, attrs, text) {
  var el = document.createElement(tagName);
  if (attrs) {
    for(var name in attrs) {
      el.setAttribute(name, attrs[name]);
      // ie 7 also needs class poked into the className
      if (name == 'class') el.setAttribute('className', attrs[name]);
    }
  }
  if (text) el.appendChild(document.createTextNode(text));
  return el;
}

function ihref(url, text, onclick) {
  var el = makeEl('a', {'href': base_href + url}, text);
  if (onclick) el.onclick = onclick;
  return el;
}

function hasClass(el, cn) {
  var clses = el.className;
  if (!clses) return false;
  clses = clses.split(" ");
  for (var j in clses) {
    if (clses[j] == cn) return true;
  }
  return false;
}

function firstkid(el, kidTag, kidClass) {
  var kids = el.childNodes;
  kidTag = kidTag.toLowerCase();
  if (kids) for (var j = 0; j < kids.length; ++j) {
    var kid = kids[j];
    if (kid && kid.nodeName.toLowerCase() == kidTag
        && (!kidClass || hasClass(kid, kidClass))) {
      return kid;
    }
  }
  return null;
}

function allkids(el, kidTag, kidClass) {
  var kids = el.childNodes;
  var found = [];
  kidTag = kidTag.toLowerCase();
  if (kids) for (var j = 0; j < kids.length; ++j) {
    var kid = kids[j];
    if (kid && kid.nodeName.toLowerCase() == kidTag
        && (!kidClass || hasClass(kid, kidClass))) {
      found.push(kid);
    }
  }
  return found;
}

// alternative to el.innerText using only W3C DOM
function innertxt(el) {
  var txt = "";
  var kids = el.childNodes;
  for(var i = 0; i < kids.length; i++) {
    var kid = kids[i];
    var nt = kid.nodeType;
    if (nt == 1) {
       txt += innertxt(kid);
    } else if(nt == 3) {
       txt += kid.nodeValue;
    }
  }
  return txt;
}


