var XmlXslt = function(targetElementId) {
	this._elementId = targetElementId;
	this.xml;
	this.xsl;
	this.ex;
	this.done = false;

	this.loadXMLDoc = function(dname) {
		if (window.XMLHttpRequest) {
			xhttp = new XMLHttpRequest();
		} else {
			xhttp = new ActiveXObject("Microsoft.XMLHTTP");
		}
		xhttp.open("GET", dname, false);
		xhttp.send("");
		return xhttp.responseXML;
	};

	this.transform = function() {
		var resultDocument;
		if (this.xml != null && this.xsl != null) {
			if (window.ActiveXObject) {
				this.ex = this.xml.transformNode(this.xsl);
			} else if (document.implementation
					&& document.implementation.createDocument) {
				try {
					xsltProcessor = new XSLTProcessor();

					xsltProcessor = new XSLTProcessor();
					xsltProcessor.importStylesheet(this.xsl);
					var xmlDom = xsltProcessor.transformToDocument(this.xml);

					if(xmlDom){
					var serializer = new XMLSerializer();
					this.ex = serializer
							.serializeToString(xmlDom.documentElement);
					} else {
						alert("XSLTprocessor error");
					}
				} catch (e) {
					alert("XSLT error: " + e.message + "(" + e + ")");
					return false;
				}
			}
		} else if (this.xml == null && this.xsl != null) {
			alert("missing xml!");
		} else if (this.xml != null && this.xsl == null) {
			alert("missing xsl!");
		} else if (this.xml == null && this.xsl == null) {
			alert("missing all!");
		}
	};

	this.resultToElem = function() {

		if (this.ex == null) {
			this.transform();
		}
		if (this.ex != null && !this.done) {

			var targetElement = _getById(this._elementId);

			if (targetElement) {
				if (window.ActiveXObject) {
					targetElement.innerHTML = this.ex;
				} else if (document.implementation
						&& document.implementation.createDocument) {
					_clearEl(targetElement);
					// targetElement.appendChild(this.ex);

					targetElement.innerHTML = this.ex;
				}
				done = true;
			} else {
				alert("element " + this._elementId + " does not exists!");
			}

		} else {
			alert("not ready!");
		}
	};

	this.setXml = function(_xml) {
		this.xml = _xml;
		this.done = false;
		if (this.xsl && this.xml) {
			this.transform();
			this.resultToElem(this.elementId);
		}

	};

	this.setXsl = function(_xsl) {
		this.xsl = _xsl;
		if (this.xsl && this.xml) {
			this.transform();
			this.resultToElem(this.elementId);
		}
	};

	this.loadXsl = function(xslUri) {
		this.setXsl(this.loadXMLDoc(xslUri));
	};

	this.loadXml = function(xmlUri) {
		this.setXml(this.loadXMLDoc(xmlUri));
	};

	this.errorCallBackXsl = function() {
		alert("XSL load failed");
	};

	this.errorCallBackXml = function() {
		alert("XML load failed");
	};
};

