/* calls 'complete' when xml and xsl are loaded. inserts result into 'node' */
function Checker(node, complete) {
	this.node = node;
	this.complete = complete;
}
Checker.prototype.add_xsl = function(xsl) { this.xsl = xsl; this.check(); }
Checker.prototype.add_xml = function(xml) { this.xml = xml; this.check(); }
Checker.prototype.check = function() {
	if (this.xml && this.xsl) {
		XSLTransform(this.xml, this.xsl, this.node);
		if (typeof(this.complete) != 'undefined') this.complete(this.xml);
	}
}

/* transform xml with xsl and insert into node */
function XSLTransform(xml, xsl, node, append) {
	if (typeof(append) == 'undefined') append = false;
	if (window.ActiveXObject) {
		var ex = xml.transformNode(xsl);
		if (append)
			node.insert(ex);
		else
			node.innerHTML=ex;
	} else if (document.implementation && document.implementation.createDocument) {
		xsltProcessor=new XSLTProcessor();
		xsltProcessor.importStylesheet(xsl);
		resultDocument = xsltProcessor.transformToFragment(xml,document);
		if (!append)
			while (node.hasChildNodes()) { node.removeChild(node.firstChild); }
		node.appendChild(resultDocument);
	}
}

function newXMLDoc(rootTagName, namespaceURL) {
    if (!rootTagName) rootTagName = "";
    if (!namespaceURL) namespaceURL = "";

    if (document.implementation && document.implementation.createDocument) {
        // This is the W3C standard way to do it
        return document.implementation.createDocument(namespaceURL, 
                       rootTagName, null);
    } else { // This is the IE way to do it
        var doc = new ActiveXObject("MSXML2.DOMDocument");

        // If there is a root tag, initialize the document
        if (rootTagName) {
            // Look for a namespace prefix
            var prefix = "";
            var tagname = rootTagName;
            var p = rootTagName.indexOf(':');
            if (p != -1) {
                prefix = rootTagName.substring(0, p);
                tagname = rootTagName.substring(p+1);
            }

            // If we have a namespace, we must have a namespace prefix
            // If we dont have a namespace, we discard any prefix
            if (namespaceURL) {
                if (!prefix) prefix = "a0"; // What Firefox uses
            }
            else prefix = "";

            // Create the root element (with optional namespace) as a
            // string of text
            var text = "<" + (prefix?(prefix+":"):"") + tagname +
                (namespaceURL
                 ?(" xmlns:" + prefix + '="' + namespaceURL +'"')
                 :"") +
                "/>";
            // And parse that text into the empty document
            doc.loadXML(text);
        }
        return doc;
    }
} 

function xmlToString(node) {
	if (typeof XMLSerializer != "undefined")
		return (new XMLSerializer()).serializeToString(node) ;
	else if (node.xml) return node.xml;
	else throw "XML.serialize is not supported or can't serialize " + node;
}

/* css buttons */
function makebutton(obj) {
        obj.addClassName('css_button');
        obj.onmousedown = function() { this.removeClassName('css_button').addClassName('css_button_pressed'); };
        obj.onmouseup = function() { this.removeClassName('css_button_pressed').addClassName('css_button'); };
        obj.onmouseout = function() { this.removeClassName('css_button_pressed').addClassName('css_button'); };
}

