+
+/*
+********************************************************************************
+** AJAX HELPER CLASS ***********************************************************
+********************************************************************************
+*/
+var pzHttpRequest = function ( url, errorHandler ) {
+ this.maxUrlLength = 2048;
+ this.request = null;
+ this.url = url;
+ this.errorHandler = errorHandler || null;
+ this.async = true;
+ this.requestHeaders = {};
+
+ if ( window.XMLHttpRequest ) {
+ this.request = new XMLHttpRequest();
+ } else if ( window.ActiveXObject ) {
+ try {
+ this.request = new ActiveXObject( 'Msxml2.XMLHTTP' );
+ } catch (err) {
+ this.request = new ActiveXObject( 'Microsoft.XMLHTTP' );
+ }
+ }
+};
+
+pzHttpRequest.prototype =
+{
+ safeGet: function ( params, callback )
+ {
+ var encodedParams = this.encodeParams(params);
+ var url = this._urlAppendParams(encodedParams);
+ if (url.length >= this.maxUrlLength) {
+ this.requestHeaders["Content-Type"]
+ = "application/x-www-form-urlencoded";
+ this._send( 'POST', this.url, encodedParams, callback );
+ } else {
+ this._send( 'GET', url, '', callback );
+ }
+ },
+
+ get: function ( params, callback )
+ {
+ this._send( 'GET', this._urlAppendParams(this.encodeParams(params)),
+ '', callback );
+ },
+
+ post: function ( params, data, callback )
+ {
+ this._send( 'POST', this._urlAppendParams(this.encodeParams(params)),
+ data, callback );
+ },
+
+ load: function ()
+ {
+ this.async = false;
+ this.request.open( 'GET', this.url, this.async );
+ this.request.send('');
+ if ( this.request.status == 200 )
+ return this.request.responseXML;
+ },
+
+ encodeParams: function (params)
+ {
+ var sep = "";
+ var encoded = "";
+ for (var key in params) {
+ if (params[key] != null) {
+ encoded += sep + key + '=' + encodeURIComponent(params[key]);
+ sep = '&';
+ }
+ }
+ return encoded;
+ },
+
+ _send: function ( type, url, data, callback)
+ {
+ var context = this;
+ this.callback = callback;
+ this.async = true;
+ this.request.open( type, url, this.async );
+ for (var key in this.requestHeaders)
+ this.request.setRequestHeader(key, this.requestHeaders[key]);
+ this.request.onreadystatechange = function () {
+ context._handleResponse(url); /// url used ONLY for error reporting
+ }
+ this.request.send(data);
+ },
+
+ _urlAppendParams: function (encodedParams)
+ {
+ if (encodedParams)
+ return this.url + "?" + encodedParams;
+ else
+ return this.url;
+ },
+
+ _handleResponse: function (savedUrlForErrorReporting)
+ {
+ if ( this.request.readyState == 4 ) {
+ // pick up appplication errors first
+ var errNode = null;
+ if (this.request.responseXML &&
+ (errNode = this.request.responseXML.documentElement)
+ && errNode.nodeName == 'error') {
+ var errMsg = errNode.getAttribute("msg");
+ var errCode = errNode.getAttribute("code");
+ var errAddInfo = '';
+ if (errNode.childNodes.length)
+ errAddInfo = ': ' + errNode.childNodes[0].nodeValue;
+
+ var err = new Error(errMsg + errAddInfo);
+ err.code = errCode;
+
+ if (this.errorHandler) {
+ this.errorHandler(err);
+ }
+ else {
+ throw err;
+ }
+ } else if (this.request.status == 200 &&
+ this.request.responseXML == null) {
+ if (this.request.responseText != null) {
+ //assume JSON
+ var json = eval("(" + this.request.responseText + ")");
+ this.callback(json, "json");
+ } else {
+ var err = new Error("XML response is empty but no error " +
+ "for " + savedUrlForErrorReporting);
+ err.code = -1;
+ if (this.errorHandler) {
+ this.errorHandler(err);
+ } else {
+ throw err;
+ }
+ }
+ } else if (this.request.status == 200) {
+ this.callback(this.request.responseXML);
+ } else {
+ var err = new Error("HTTP response not OK: "
+ + this.request.status + " - "
+ + this.request.statusText );
+ err.code = '00' + this.request.status;
+ if (this.errorHandler) {
+ this.errorHandler(err);
+ }
+ else {
+ throw err;
+ }
+ }
+ }
+ }
+};
+
+/*
+********************************************************************************
+** XML HELPER FUNCTIONS ********************************************************
+********************************************************************************
+*/
+
+// DOMDocument
+
+if ( window.ActiveXObject) {
+ var DOMDoc = document;
+} else {
+ var DOMDoc = Document.prototype;
+}
+
+DOMDoc.newXmlDoc = function ( root )
+{
+ var doc;
+
+ if (document.implementation && document.implementation.createDocument) {
+ doc = document.implementation.createDocument('', root, null);
+ } else if ( window.ActiveXObject ) {
+ doc = new ActiveXObject("MSXML2.DOMDocument");
+ doc.loadXML('<' + root + '/>');
+ } else {
+ throw new Error ('No XML support in this browser');
+ }
+
+ return doc;
+}
+
+
+DOMDoc.parseXmlFromString = function ( xmlString )
+{
+ var doc;
+
+ if ( window.DOMParser ) {
+ var parser = new DOMParser();
+ doc = parser.parseFromString( xmlString, "text/xml");
+ } else if ( window.ActiveXObject ) {
+ doc = new ActiveXObject("MSXML2.DOMDocument");
+ doc.loadXML( xmlString );
+ } else {
+ throw new Error ("No XML parsing support in this browser.");
+ }
+
+ return doc;
+}
+
+DOMDoc.transformToDoc = function (xmlDoc, xslDoc)
+{
+ if ( window.XSLTProcessor ) {
+ var proc = new XSLTProcessor();
+ proc.importStylesheet( xslDoc );
+ return proc.transformToDocument(xmlDoc);
+ } else if ( window.ActiveXObject ) {
+ return document.parseXmlFromString(xmlDoc.transformNode(xslDoc));
+ } else {
+ alert( 'Unable to perform XSLT transformation in this browser' );
+ }
+}
+
+// DOMElement
+
+Element_removeFromDoc = function (DOM_Element)
+{
+ DOM_Element.parentNode.removeChild(DOM_Element);
+}
+
+Element_emptyChildren = function (DOM_Element)
+{
+ while( DOM_Element.firstChild ) {
+ DOM_Element.removeChild( DOM_Element.firstChild )
+ }
+}
+
+Element_appendTransformResult = function ( DOM_Element, xmlDoc, xslDoc )
+{
+ if ( window.XSLTProcessor ) {
+ var proc = new XSLTProcessor();
+ proc.importStylesheet( xslDoc );
+ var docFrag = false;
+ docFrag = proc.transformToFragment( xmlDoc, DOM_Element.ownerDocument );
+ DOM_Element.appendChild(docFrag);
+ } else if ( window.ActiveXObject ) {
+ DOM_Element.innerHTML = xmlDoc.transformNode( xslDoc );
+ } else {
+ alert( 'Unable to perform XSLT transformation in this browser' );
+ }
+}
+
+Element_appendTextNode = function (DOM_Element, tagName, textContent )
+{
+ var node = DOM_Element.ownerDocument.createElement(tagName);
+ var text = DOM_Element.ownerDocument.createTextNode(textContent);
+
+ DOM_Element.appendChild(node);
+ node.appendChild(text);
+
+ return node;
+}
+
+Element_setTextContent = function ( DOM_Element, textContent )
+{
+ if (typeof DOM_Element.textContent !== "undefined") {
+ DOM_Element.textContent = textContent;
+ } else if (typeof DOM_Element.innerText !== "undefined" ) {
+ DOM_Element.innerText = textContent;
+ } else {
+ throw new Error("Cannot set text content of the node, no such method.");
+ }
+}
+
+Element_getTextContent = function (DOM_Element)
+{
+ if ( typeof DOM_Element.textContent != 'undefined' ) {
+ return DOM_Element.textContent;
+ } else if (typeof DOM_Element.text != 'undefined') {
+ return DOM_Element.text;
+ } else {
+ throw new Error("Cannot get text content of the node, no such method.");
+ }
+}
+
+Element_parseChildNodes = function (node)
+{
+ var parsed = {};
+ var hasChildElems = false;
+
+ if (node.hasChildNodes()) {
+ var children = node.childNodes;
+ for (var i = 0; i < children.length; i++) {
+ var child = children[i];
+ if (child.nodeType == Node.ELEMENT_NODE) {
+ hasChildElems = true;
+ var nodeName = child.nodeName;
+ if (!(nodeName in parsed))
+ parsed[nodeName] = [];
+ parsed[nodeName].push(Element_parseChildNodes(child));
+ }
+ }
+ }
+
+ var attrs = node.attributes;
+ for (var i = 0; i < attrs.length; i++) {
+ var attrName = '@' + attrs[i].nodeName;
+ var attrValue = attrs[i].nodeValue;
+ parsed[attrName] = attrValue;
+ }
+
+ // if no nested elements, get text content
+ if (node.hasChildNodes() && !hasChildElems) {
+ if (node.attributes.length)
+ parsed['#text'] = node.firstChild.nodeValue;
+ else
+ parsed = node.firstChild.nodeValue;
+ }
+
+ return parsed;
+}
+
+/* do not remove trailing bracket */