// For more information please visit http://www.godlikemouse.com

var GLM = {
    DOM:        Object,
    AJAX:       Object,
    Collection: Object
};

GLM.DOM.isInternetExplorer = (navigator.userAgent.indexOf("MSIE") >= 0);
GLM.DOM.isMozilla = (navigator.userAgent.indexOf("Gecko") >= 0);
GLM.DOM.isOpera = (navigator.userAgent.indexOf("Opera") >= 0);
GLM.Collection.Map = function(){
    var len = 0;
    var keys = new Array();
    var values = new Array();

    this.get = function(key){
        var val = null;
        for(var i=0; i<len; i++){
            if(keys[i] == key){
                val = values[i];
                break;
            }//end if
        }//end for

        return val;
    }//end get()

    this.put = function(key, value){
        keys[len] = key;
        values[len++] = value;
    }//end put()

    this.length = function(){
        return len;
    }//end length()

	this.contains = function(key){
	var con = false;
        for(var i=0; i<len; i++){
            if(keys[i] == key){
                con = true;
                break;
            }//end if
        }//end for

	return con;
    }//end contains()

    this.remove = function(key){
        var keyArr = new Array();
        var valArr = new Array();
        var l = 0;
        for(var i=0; i<len; i++){
            if(keys[i] != key){
                keyArr[l] = keys[i];
                valArr[l++] = values[i];
            }//end if
        }//end for

        keys = keyArr;
        values = valArr;
	len = l;
    }//end remove()        
    
}//end GLM.Collection.Map

GLM.AJAX = function(){

    var nameSpace = "http://tempuri.org/";
    var map = new GLM.Collection.Map();
    
    var ajaxObject = function(){
        try{return new XMLHttpRequest();}catch(ex){};
        try{return new ActiveXObject("Microsoft.XMLHTTP");}catch(ex){};
        try{return new SOAPCall();}catch(ex){};
    }//end ajaxObject()
    
    this.onError = function(error){
        alert(error);
    }//end onError()

    this.callPage = function(url, callbackFunction, method, args, async){        
        try{
            var ao = ajaxObject();
                
            ao.onreadystatechange = function(){
                if(ao.readyState==4 || ao.readyState=="complete"){
                    callbackFunction(ao.responseText);
                }
            };
                
            if(!method) method = "GET";
            if(!args) args = null;
            if(async == null) async = true;
                
            ao.open(method, url, async);
                
            if(method == "POST")
                ao.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
                
            ao.send(args);
        }
        catch(ex){
            this.onError(ex);
        }//end tc
    }//end callPage()
	

    this.callService = function(serviceUrl, soapMethod, callbackFunction /*, unlimited params */){
        
        var callServiceError = this.onError;
        
        var ao = ajaxObject();
        
        if(!ao.encode){
            if(serviceUrl.indexOf("http://") < 0)
                serviceUrl = "http://" + serviceUrl;
            serviceUrl += "?WSDL";
            
            var soapEnvelope = "<?xml version=\"1.0\" encoding=\"utf-8\"?>";
            soapEnvelope += "<soap:Envelope xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\">";
            soapEnvelope += "<soap:Body>";
            soapEnvelope += "<" + soapMethod + " xmlns=\"" + nameSpace + "\">";
            
            if(arguments.length > 3){
                for (var i = 3; i < arguments.length; i++)
                {
                    var params = arguments[i].split("=");
                    soapEnvelope += "<" + params[0] + ">";
                    soapEnvelope += params[1];
                    soapEnvelope += "</" + params[0] + ">";
                }//end for
            }//end if
			
            soapEnvelope += "</" + soapMethod + ">";
            soapEnvelope += "</soap:Body>";
            soapEnvelope += "</soap:Envelope>";
			
            
            
            ao.onreadystatechange = function(){
                
                if(ao.readyState == 4){
                    
                    if(GLM.DOM.IsOpera){
                        //opera
                        var response = ao.responseXML.getElementsByTagName(soapMethod + "Result")[0];
                        if(!response)
                            response = ao.responseXML.getElementsByTagName(soapMethod + "Response")[0];
                        if(!response){
                            callServiceError("WebService does not contain a Result/Response node");
                            return;
                        }//end if
                        
                        ao.callbackFunction(ao.responseXML.getElementsByTagName(soapMethod + "Result")[0].innerHTML);
                    }
                    else if(GLM.DOM.isInternetExplorer){
                        //IE
                        var responseXml = new ActiveXObject('Microsoft.XMLDOM');
                        responseXml.loadXML(ao.responseText);
                            
                        var responseNode = responseXml.selectSingleNode("//" + soapMethod + "Response");
                        if(!responseNode)
                            responseNode = responseXml.selectSingleNode("//" + soapMethod + "Result");
                        if(!responseNode)
                            callServiceError("Response/Result node not found.\n\nResponse:\n" + ao.responseText);
                            
                        var resultNode = responseNode.firstChild;
                        if (resultNode != null){
                            try{
                                callbackFunction(resultNode.text);
                            }
                            catch(ex){
                                callServiceError(ex);
                            }//end tc
                        }
                        else{
                            try{
                                callbackFunction();
                            }
                            catch(ex){
                                callServiceError(ex);
                            }//end tc
                        }//end if
                    }
                    else if(GLM.DOM.isMozilla){
                        //Mozilla
                        var xmlDocument = new DOMParser().parseFromString(ao.responseText, "text/xml");
                        
                        var xr = xmlDocument.evaluate("//" + soapMethod + "Result", xmlDocument.childNodes[xmlDocument.childNodes.length-1], null, XPathResult.ANY_TYPE, null);
                        var responseNode = xr.iterateNext();
                        
                        if(!responseNode)
                            callServiceError("Response/Result node not found.\n\nResponse:\n" + ao.responseText);
                        
                        var resultNode = responseNode.firstChild;
                        
                        if (resultNode != null){
                            try{
                                callbackFunction(resultNode.textContent);
                            }
                            catch(ex){
                                callServiceError(ex);
                            }//end tc
                        }
                        else{
                            try{
                                callbackFunction();
                            }
                            catch(ex){
                                callServiceError(ex);
                            }//end tc
                        }//end if
                    }//end if
                }//end if
            };
            
            ao.open("POST", serviceUrl, true);			
            ao.setRequestHeader("Content-Type", "text/xml");
            ao.setRequestHeader("SOAPAction", nameSpace + soapMethod);
            try{
                ao.send(soapEnvelope);
            }
            catch(ex){
                serviceCallError(ex);
            }//end tc
        }
        else{
            var soapParams = new Array();
            var headers = new Array();
            var soapVersion = 0;
            var object = nameSpace;
            
            if(serviceUrl.indexOf("http://") < 0)
                serviceUrl = document.location + serviceUrl;
            
            ao.transportURI = serviceUrl;
            ao.actionURI = nameSpace + soapMethod;
            
            for(var i=3; i<arguments.length; i++){
                var params = arguments[i].split("=");
                soapParams.push( new SOAPParameter(params[1],params[0]) );
            }//end for
            
            try{
                ao.encode(soapVersion, soapMethod, object, headers.length, headers, soapParams.length, soapParams);
            }
            catch(ex){
                serviceCallError(ex);
            }//end tc
		
            try{
                netscape.security.PrivilegeManager.enablePrivilege("UniversalBrowserRead");
            } 
            catch(ex){
                return false;
            }//end tc
            
            try{
                ao.asyncInvoke(
                    function(resp,call,status){

                    if(resp.fault)
                        return callServiceError(resp.fault);
                    if(!resp.body){
                        callServiceError("Service " + call.transportURI + " not found.");
                    }
                    else{
                        try{
                            callbackFunction(resp.body.firstChild.firstChild.firstChild.data);
                        }
                        catch(ex){
                            callServiceError(ex);
                        }//end tc
                    }//end if
                }
                );
            }
            catch(ex){
                serviceCallError(ex);
            }//end tc
                        
        }//end if
		
    }//end callService()
	
    this.setNameSpace = function(ns){
        nameSpace = ns;
    }//end setNameSpace()

	this.getNameSpace = function(){
        return ns;
    }//end getNameSpace()
	
}//end GLM.AJAX()
