// Ajax & Dom Javascript Library

/*****************************************************************************

This library contains the following functions:

	Ajax.object
	Dom.domToText
	Dom.textToDom
	Dom.transform

*****************************************************************************/

Ajax = new function() { }
Dom = new function() {}

Ajax.object = function(sUrl, bNoCache) {
    var that = this;
    this.updating = false;
    this.abort = function() {
        if (that.updating) {
            that.updating = false;
            that.AJAX.abort();
            that.AJAX = null;
        }
    }
    this.update = function(passData, postMethod) {
        if (that.updating) { return false; }
        that.AJAX = null;
        if (window.XMLHttpRequest) {
            that.AJAX = new XMLHttpRequest();
        } else {
            that.AJAX = new ActiveXObject("Microsoft.XMLHTTP");
        }
        if (that.AJAX == null) {

            return false;
        } else {
            that.AJAX.onreadystatechange = function() {
                if (that.AJAX.readyState == 4) {
                    that.updating = false;
                    that.callback(that.AJAX.responseText, that.AJAX.status, that.AJAX.responseXML);
                    that.AJAX = null;
                }
            }
            that.updating = new Date();
            if (/post/i.test(postMethod)) {
                if (noCache) {
                    var uri = urlCall + '?timestamp=' + that.updating.getTime();
                }
                else {
                    var uri = urlCall;
                }
                that.AJAX.open("POST", uri, true);
                that.AJAX.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
                that.AJAX.send(passData);
            } else {
				if (noCache) {
                    var uri = urlCall + '?timestamp=' + that.updating.getTime();

                }
                else {
                    var uri = urlCall;
                }
				//var uri = urlCall + '?timestamp=' + that.updating.getTime();
                var uri = urlCall;
                that.AJAX.open("GET", uri, true);
                that.AJAX.send(null);
            }
            return true;
        }
    }
    var urlCall = sUrl;
	var noCache = bNoCache;
    this.callback = function() { };
}

/*****************************************************************************************

  Object Name: Dom.domToText
  Object Type: JavaScript Function
  Object Parameters:
    dXml                    XMLDOM object to convert

  Object Description:
    Converts an XMLDOM object to an XML String.

  Revision History:
    April 27, 2006          ---

*****************************************************************************************/

Dom.domToText = function(dXml) {
	var sXml = null;
	try {
		if (sXml = dXml.xml) {

			return sXml;
		} else {
			var hXmlSerializer = new XMLSerializer;
			sXml = hXmlSerializer.serializeToString(dXml);
			return sXml;
		}
	} catch (e) {
		alert(' Error in domToText: '+e.message);
		return false;
	}
}


/*****************************************************************************************

  Object Name: Dom.textToDom
  Object Type: JavaScript Function
  Object Parameters:
	sXml                    XML String to convert

  Object Description:
	Converts an XML String to an XMLDOM object.

  Revision History:
	April 27, 2006          ---

*****************************************************************************************/

Dom.textToDom = function(sXml) {
	try {
		var hDomParser = new DOMParser();
		var dXml = hDomParser.parseFromString(sXml, "text/xml");
	} catch (e) {
		try {
			var dXml = new ActiveXObject("Microsoft.XMLDOM");
			dXml.async = false;
			dXml.loadXML(sXml);
		} catch (e) {
			dXml = false;

			alert(' Error in textToDom: '+e.message);
		}
	}
	return dXml;
}


/*****************************************************************************************

  Object Name: Dom.transform
  Object Type: JavaScript Function
  Object Parameters:
	dXml                    XMLDOM source to transform
	dXsl                    XMLDOM XSL for tranformation
	fDom                    Boolean that determines return type.
							  true  : return XMLDOM
							  false : return XML String

  Object Description:
	Transforms an XML with an XSL and returns either an XMLDOM or XML String.

  Revision History:
	April 27, 2006          ---

*****************************************************************************************/

Dom.transform = function(dXml, dXsl, fDom) {
	try {
		var hXsltProcessor = new XSLTProcessor();
		hXsltProcessor.importStylesheet(dXsl);
		var dRes = hXsltProcessor.transformToFragment(dXml, document);
		if (fDom) {

			var hDomParser = new DOMParser();
			var dRet = hDomParser.parseFromString('<temp/>', "text/xml");
			dRet.replaceChild(dRes.childNodes[0], dRet.childNodes[0]);
			return dRet;
		} else {
			return Dom.domToText(dRes);
		}
	} catch (e) {
		try {
			var sRes = dXml.transformNode(dXsl);
		} catch (e) {
			alert(' Error in transform: '+e.message);            
			return false;
		}
		if (fDom) {
			return Dom.textToDom(sRes);
		} else {
			return sRes;
		}
	}
}
