// insertAdjacentHTML(), insertAdjacentText() and insertAdjacentElement()
// for Netscape 6/Mozilla by Thor Larholm thor@jscript.dk

// 30-10-2007: split into three different top-level 'if's to works with Safari 3 -IA

var ua = navigator.userAgent.toLowerCase();
if (ua.indexOf("safari") > -1) {
  window.HTMLElement = window.HTMLElement || Object; // dirty hack
}

if (typeof HTMLElement != "undefined" && !HTMLElement.prototype.insertAdjacentElement) {

  HTMLElement.prototype.insertAdjacentElement = function(where, parsedNode) {
    switch (where) {
      case 'beforeBegin':
        this.parentNode.insertBefore(parsedNode, this)
        break;

      case 'afterBegin':
        this.insertBefore(parsedNode, this.firstChild);
        break;

      case 'beforeEnd':
        this.appendChild(parsedNode);
        break;

      case 'afterEnd':
        if (this.nextSibling) 
          this.parentNode.insertBefore(parsedNode, this.nextSibling);
        else
          this.parentNode.appendChild(parsedNode);
        break;
    } // end of switch
  }
}

if (typeof HTMLElement != "undefined" && !HTMLElement.prototype.insertAdjacentHTML) {
  HTMLElement.prototype.insertAdjacentHTML = function(where, htmlStr) {
    var r = this.ownerDocument.createRange();
    r.setStartBefore(this);
    var parsedHTML = r.createContextualFragment(htmlStr);
    this.insertAdjacentElement(where, parsedHTML);
  }
}

if (typeof HTMLElement != "undefined" && !HTMLElement.prototype.insertAdjacentText) {
  HTMLElement.prototype.insertAdjacentText = function(where, txtStr) {
    var parsedText = document.createTextNode(txtStr);
    this.insertAdjacentElement(where, parsedText);
  }
}
  
// loadXML() for Mozilla by Erik Arvidsson
// http://webfx.eae.net/dhtml/xmlextras/xmlextras.html

if (typeof XMLDocument != "undefined" && !XMLDocument.prototype.loadXML) {
  XMLDocument.prototype.loadXML = function(txtStr) {

    // parse the string to a new doc
    var doc = (new DOMParser()).parseFromString(txtStr, "text/xml");

    // remove all initial children
    while (this.hasChildNodes())
      this.removeChild(this.lastChild);

    // insert and import nodes
    for (var i = 0; i < doc.childNodes.length; i++) {
      this.appendChild(this.importNode(doc.childNodes[i], true));
    }
  }
}

// selectSingleNode() for Mozilla
// based on wrapper function found on http://kb.mozillazine.org/XPath

if (typeof XMLDocument != "undefined" && !XMLDocument.prototype.selectSingleNode) {
  XMLDocument.prototype.selectSingleNode = function(aExpr) {
    var xpe = new XPathEvaluator();
    var nsResolver = xpe.createNSResolver(this.ownerDocument == null ?
      this.documentElement : this.ownerDocument.documentElement);
    var result = xpe.evaluate(aExpr, this, nsResolver, 0, null);
    return result.iterateNext();
  }
}

// selectNodes() for Mozilla
// based on wrapper function found on http://kb.mozillazine.org/XPath

if (typeof XMLDocument != "undefined" && !XMLDocument.prototype.selectNodes) {
  XMLDocument.prototype.selectNodes = function(aExpr) {
    var xpe = new XPathEvaluator();
    var nsResolver = xpe.createNSResolver(this.ownerDocument == null ?
      this.documentElement : this.ownerDocument.documentElement);
    var result = xpe.evaluate(aExpr, this, nsResolver, 0, null);
    var found = [];
    while (res = result.iterateNext())
      found.push(res);
    return found;
  }
}

// setSelectionRange() for MSIE
// based on example found on http://www.webreference.com/programming/javascript/ncz/
/*
if ((typeof HTMLInputElement != "undefined") && HTMLInputElement.prototype.createTextRange
     && !HTMLInputElement.prototype.setSelectionRange) {
  HTMLInputElement.prototype.setSelectionRange = function (selStart, selLength) {
    var range = this.createTextRange(); 
    range.moveStart("character", selStart); 
    range.moveEnd("character", selLength - this.value.length); 
    range.select();
  }
}
*/