/* 


TSS 11.27.2005

*/



/*************************************
OBJECT
*************************************/

var biz = Object();

biz.object=function(id)
{

	this.id=id;
	this.r=null;
	this.callBack=null;
	this.bizml=null;

}
biz.object.prototype.fill = function(url,
                                        callBack,
                                        method,
                                        params,
                                        soapReq,
                                        webMethod,
                                        contentType)
{

    this.r = new http.requestor(this,
	                            url,
	                            this.finishFill,
	                            this.defaultObjErr,
	                            method,
	                            params,
	                            soapReq,
	                            webMethod,
	                            contentType);
	
	this.callBack=callBack;
	
	this.r.loadXML();

}

biz.object.prototype.finishFill=function()
{

	this.bizml = this.r.response;
	
	this.parseXML(this.bizml);
	
	this.callBack.call(this);
	

}

biz.object.prototype.getXML = function(url,
                                        callBack,
                                        method,
                                        params,
                                        soapReq,
                                        webmethod,
                                        contentType)
{

    this.r = new http.requestor(this,
	                            url,
	                            this.finishXML,
	                            this.defaultObjErr,
	                            method,
	                            params,
	                            soapReq,
	                            webmethod,
	                            contentType);
	
	this.callBack=callBack;
	
	this.r.loadXML();

}

biz.object.prototype.finishXML=function()
{

	this.bizml = this.r.response;
	
	this.callBack.call(this);
	

}

biz.object.prototype.parseXML = function(oNode, parent) {


    if (parent == null)
        parent = this;

    if (oNode.nodeType == 1 && oNode.nodeName.indexOf("soap:") == -1 && (this.r.webmethod == null || (oNode.nodeName != this.r.webmethod + "Response" && oNode.nodeName != this.r.webmethod + "Result"))) {

        if ((oNode.childNodes.length > 0 && (oNode.childNodes[0].nodeType == 1) || oNode.parentNode.nodeType == 9)) //Object
        {

            if (parent[oNode.nodeName] == null) {

                parent[oNode.nodeName] = new Object();

                for (var i = 0; i < oNode.attributes.length; i++)
                    parent[oNode.nodeName][oNode.attributes[i].nodeName] = oNode.attributes[i].nodeValue;

                if (oNode.childNodes.length == 0)
                    parent[oNode.nodeName] = oNode.text;
                else {

                    for (var i = 0; i < oNode.childNodes.length; i++)
                        this.parseXML(oNode.childNodes[i], parent[oNode.nodeName]);

                }

            }
            else //collection?
            {

                if (parent[oNode.nodeName].length == null) {

                    var tempObj = parent[oNode.nodeName];

                    parent[oNode.nodeName] = null;

                    parent[oNode.nodeName] = [];

                    parent[oNode.nodeName][0] = tempObj;

                    tempObj = null;
                }

                var tempObj = new Object();

                var next = parent[oNode.nodeName].length;

                parent[oNode.nodeName][next] = tempObj;

                tempObj = null;

                for (var i = 0; i < oNode.attributes.length; i++)
                    parent[oNode.nodeName][next][oNode.attributes[i].nodeName] = oNode.attributes[i].nodeValue;

                if (oNode.childNodes.length == 0)
                    parent[oNode.nodeName][next] = oNode.text;
                else {

                    for (var i = 0; i < oNode.childNodes.length; i++)
                        this.parseXML(oNode.childNodes[i], parent[oNode.nodeName][next]);

                }

            }

        }
        else {

            if (parent[oNode.nodeName]) {
                if ((typeof parent[oNode.nodeName]) == "object") {
                    parent[oNode.nodeName].push(oNode.text);
                }
                else {
                    var tempVal = parent[oNode.nodeName];

                    parent[oNode.nodeName] = null;

                    parent[oNode.nodeName] = [];

                    parent[oNode.nodeName].push(tempVal);
                    parent[oNode.nodeName].push(oNode.text);

                    tempVal = null;

                }
            }
            else
                parent[oNode.nodeName] = oNode.text;

        }
    }
    else {

        for (var n = 0; n < oNode.childNodes.length; n++)
            this.parseXML(oNode.childNodes[n], parent);

    }

}

biz.object.prototype.save=function()
{

}

biz.object.prototype.remove=function()
{

}

biz.object.prototype.isDirty
{

}
biz.object.prototype.defaultObjErr = function() {

    this.Error = new Object();

    this.Error.Code = "1";
    this.Error.Message = "ajax error fetching data!"
	  + "<br />readyState: " + this.r.req.readyState
	  + "<br />status: " + this.r.req.status
	  + "<br />headers: " + this.r.req.getAllResponseHeaders()
      + "<br />message: " + this.r.req.responseText;

    this.callBack.call(this);

}
/*************************************
END OBJECT
*************************************/

/*************************************
REQUESTOR
*************************************/

var http=new Object();

var httpBundles=new Array();

http.xmlns = "http://androidhof.com/";

http.READY_STATE_UNINITIALIZED=0;
http.READY_STATE_LOADING=1;
http.READY_STATE_LOADED=2;
http.READY_STATE_INTERACTIVE=3;
http.READY_STATE_COMPLETE=4;

http.requestor = function(caller,
                            url,
                            onload,
                            onerror,
                            method,
                            params,
                            soapReq,
                            webmethod, 
                            contentType) {

    this.id = caller.id;
    this.caller = caller;
    this.url = url;
    this.req = null;
    this.onload = onload;
    this.onerror = onerror;
    this.method = (method) ? method.toUpperCase() : "GET";
    
    if(!contentType)
    {
    
        if(soapReq==true)
            this.contentType="text/xml; charset=utf-8";
        else if(method.toUpperCase()=="POST")
            this.contentType="application/x-www-form-urlencoded";      
    
    }
    else
        this.contentType=contentType;
        
    this.params = (soapReq==true && method.toUpperCase()=="POST") ? soapIt(params,webmethod,http.xmlns) : params;
    this.soapReq = (soapReq) ? soapReq : false;
    this.webmethod=webmethod;

    this.response = null;

}

http.requestor.prototype.loadXML = function() {

    if (window.XMLHttpRequest) //Mozilla/Safari/IE 7 +
    {
        this.req = new XMLHttpRequest();
    }
    else if (window.ActiveXObject)  //IE
    {
        this.req = new ActiveXObject("Microsoft.XMLHTTP");
    }

    if (this.req) {

        try {
            var loader = this;
            this.req.onreadystatechange = function() {
                loader.onReadyState.call(loader);
            }

            if (this.method == "GET")
                this.url += "/" + this.webmethod + ((this.params) ? "?" + this.params : "");


            this.req.open(this.method, this.url, true);

            if (this.method == "POST") {
            
                this.req.setRequestHeader("Content-length", this.params.length);
                
                if (this.soapReq == true) {

                    this.req.setRequestHeader("SOAPAction",http.xmlns + this.webmethod);

                }
                else {
                    
                    this.req.setRequestHeader("Connection", "close");

                }
                
            }

            if (this.contentType) {
                this.req.setRequestHeader("Content-Type", this.contentType);
            }

            this.req.send(this.params);

        }
        catch (err) {
            this.onerror.call(this.caller);
        }
    }
}
	
http.requestor.prototype.onReadyState=function()
{	
	var req=this.req;
	var ready=req.readyState;
		
	if(ready==http.READY_STATE_COMPLETE)
	{		
		var httpStatus=req.status;
		
		if(httpStatus==200 || httpStatus==0)
		{
			this.response=req.responseXML;
			this.onload.call(this.caller);
		}
		else
		{
			this.onerror.call(this.caller);
		}
	}
}

http.requestor.prototype.defaultReqErr = function() {

    this.Error = new Object();

    this.Error.Code = "1";
    this.Error.Message = "ajax error fetching data!"
	  + "\n\nreadyState: " + this.req.readyState
	  + "\nstatus: " + this.req.status
	  + "\nheaders: " + this.req.getAllResponseHeaders;

    this.onload.call(this.caller);

}

function soapIt(params, objId, ns)
{

    var sReturn="<?xml version=\"1.0\" encoding=\"utf-8\"?>";
    
    sReturn +="<soap:Envelope xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" ";
    sReturn += "xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" ";
    sReturn += "xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\">";

    sReturn += "<soap:Body>";

    sReturn += "<" + objId + " xmlns=\"" + ns+ "\">";
    
    var aParams=new Array();
    
    if (params) {

        if (params.length > 0) {

            if (params.indexOf("&") > -1) {

                aParams = params.split("&");

            }
            else
                aParams.push(params);

        }

    }
    
    var sName = "";
    var sVal = "";
    
    for (var i = 0; i < aParams.length; i++) {

        sName = aParams[i].split("=")[0];
        sVal = aParams[i].split("=")[1];

        sReturn += "<" + sName + ">" + sVal + "</" + sName + ">";

    }

    sReturn += "</" + objId + ">";
    
    sReturn += "</soap:Body>";
    sReturn += "</soap:Envelope>";

    return sReturn;


}
/*************************************
END REQUESTOR
*************************************/



