//<![CDATA[
var isIE = null;
var TEXTNODETYPE = 3;
var isMozilla;
var xmlhttp;
// for Mozilla, etc.
if (window.XMLHttpRequest) {
    isMozilla = true;
}
// for IE
else if (window.ActiveXObject) {
    isMozilla = false;
}

// borrowed from prototype.js
var Try = {
  these: function() {
    var rV;
    for (var i = 0; i < arguments.length; i++) {
      var lb = arguments[i];
      try {
        rV=lb();
        break;
      } catch (e) {}
    }
    return rV;
  }
}

function getDOMText(nodes) {
    var toReturn = "";
    for (var i = 0; i < nodes.length; i++) {
        if (nodes[i].nodeType == TEXTNODETYPE) {
            toReturn = toReturn + nodes[i].nodeValue;
        }
    }
    return toReturn;
}

function clearAll(elem) {
    clearAllStarting(elem, 0);
}

function clearAllButFirstElement(elem) {
    clearAllStarting(elem, 1);
}

function clearAllAfterFirstElementType(elem, elemType) {
    if (!elem || elem.childNodes.length == 0) {
        return;
    }
    var curChild = elem.childNodes[0];
    while (curChild != null && curChild.nodeName.toUpperCase() != elemType.toUpperCase()) {
        curChild = curChild.nextSibling;
    }
    if (curChild != null) {
        clearAllStartingNode(elem, curChild.nextSibling);
    }
}

function clearAllStarting(elem, starting) {
    if (!elem || (elem.childNodes.length < starting)) {
        return;
    }
    // Calling clearAllStartingNode(elem, elem.childNodes[starting]) doesn''t
    // work - the startingNode gets set to void for some reason.
    var curChild = elem.childNodes[starting];
    var nextChild = null;
    while (curChild != null) {
        nextChild = curChild.nextSibling;
        elem.removeChild(curChild);
        curChild = nextChild;
    }
}

function clearAllStartingNode(elem, startingNode) {
    var curChild = startingNode;
    var nextChild = null;
    while (curChild != null) {
        nextChild = curChild.nextSibling;
        elem.removeChild(curChild);
        curChild = nextChild;
    }
}

// I didn''t have to do this before, but I guess I have to for
// the Yahoo XmlHttpRequest.  Bleh.
function getDocElem(request) {
    var toReturn = request.responseXML;
    var toReturn2 = null;
    try {
        toReturn2 = toReturn.documentElement;
    } catch (e) {}
    if (toReturn2 != null) {
        toReturn = toReturn2;
    }
    return toReturn;
}

function getXPathFromXML(xmlElem) {
    /*if (xmlElem == null) {
        alert('xmlElem NULL!!');
    }*/
    var xpath = null;
    Try.these(function() {xpath = new ActiveXObject("MSXML2.DOMDocument.5.0");},
              function() {xpath = new ActiveXObject("MSXML2.DOMDocument.4.0");},
              function() {xpath = new ActiveXObject("MSXML2.DOMDocument.3.0");},
              function() {xpath = new ActiveXObject("MSXML2.DOMDocument");},
              function() {xpath = new ActiveXObject("Microsoft.XMLDOM");},
              function() {xpath = null;});
    if (xpath != null) {
        isIE = true;
        // xmlElem.xml is IE-only, but only IE needs it, so we''re OK.
        xpath.loadXML(xmlElem.xml);
        // FFV - this is hacky
        if (xpath.readyState && xpath.readyState != 4) {
            while (xpath.readyState != 4) {
            }
        }
        return xpath;
    } else {
        isIE = false;
        if (!xmlElem.selectNodes) {
            // Taken from http://www.wrox.com/WileyCDA/Section/id-291861.html
            Element.prototype.selectNodes = function(sXPath) {
                var oEvaluator = new XPathEvaluator();
                var oResult = oEvaluator.evaluate(sXPath, this, null, 
                    XPathResult.ORDERED_NODE_ITERATOR_TYPE, null);    
        
                var aNodes = new Array;
        
                if (oResult != null) {
                    var oElement = oResult.iterateNext();
                    while(oElement) {
                        aNodes.push(oElement);
                        oElement = oResult.iterateNext();
                    }
                }
                return aNodes;
            };
        }
        return xmlElem;
    }
}

function myEncodeURI(source) {
    // Do the javascript thing, but encode a space as a +
    var nextSpace = source.indexOf(' ');
    while (nextSpace != -1) {
        source = source.substring(0, nextSpace) + "+" + source.substring(nextSpace+1);
        nextSpace = source.indexOf(' ');
    }
    return encodeURI(source);
}

function encodeMyHtml(rawText) {
     encodedHtml = escape(rawText);
     encodedHtml = encodedHtml.replace(/\+/g,"%2B");
     encodedHtml = encodedHtml.replace(/\//g,"%2F");
     encodedHtml = encodedHtml.replace(/\?/g,"%3F");
     encodedHtml = encodedHtml.replace(/=/g,"%3D");
     encodedHtml = encodedHtml.replace(/&/g,"%26");
     encodedHtml = encodedHtml.replace(/@/g,"%40");
     //alert('encodedHtml is ' + encodedHtml);
     return encodedHtml;
}


// FFV - this is a hack to get around problems with getting nested XPath
// expressions in mozilla and IE.
function getNestedXPathExpression(root, target) {
    if (isIE) {
        return '/' + root + '/' + target;
    } else {
        return target;
    }
}

function getNestedXPathText(xpath, root, target) {
    return getDOMText(xpath.selectNodes(getNestedXPathExpression(root, target))[0].childNodes);
}

function getDisplayStyle(isVisible) {
    if (!isVisible) {
        return "none";
    } else {
        return "";
    }
}

function setVisibility(elem, isVisible) {
    elem.style.display = getDisplayStyle(isVisible);
}

function setEnabled(elem, enabled) {
    if (enabled) {
        elem.disabled = false;
    } else {
        elem.disabled = true;
    }
}

function toggleClassVisibility(className) {
    var stylesheet = document.styleSheets[0];
    var rules;
    if (stylesheet.cssRules) {
        rules = stylesheet.cssRules;
    } else {
        rules = stylesheet.rules;
    }
    for (var i = 0; i < rules.length; i++) {
        if (rules[i].selectorText == "." + className) {
            var curStyle = rules[i].style;
            if (curStyle.display == "none") {
                curStyle.display = "";
            } else {
                curStyle.display = "none";
            }
        }
    }
}

function doRequest(url, async, callback, method, postData) {
    if (isMozilla) {
        xmlhttp = new XMLHttpRequest();
        // http://www.onlamp.com/pub/a/onlamp/2005/05/19/xmlhttprequest.html
        // Some versions of mozilla lock up without this, apparently.
        if (xmlhttp.overrideMimeType) {
            xmlhttp.overrideMimeType('text/xml');
        }
    } else {
        try {
            xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
        } catch(e) {
            xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
        }
    }
    if (async) {
        xmlhttp.onreadystatechange = callback;
    }
    method = method.toUpperCase();
    xmlhttp.open(method, url, async);
    if (method == "GET") {
        if (isMozilla) {
            xmlhttp.send(null);
        } else {
            xmlhttp.send();
        }
    } else {
        // We're doing POST.
        xmlhttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded; charset=UTF-8');
        xmlhttp.send(postData);
    }
    if (!async) {
        callback();
    }
}


//]]>
