/********************************************************************************************************
Projeto                     =   API Ajax - ShopTour
Arquivo                     =   AJAX.js
Descricao                   =   Objeto de gerenciamento de funcoes AJAX.
Dependencias                =   nenhum
Criador                     =   LabST (Laboratorio Shop Tour)
Data de criacao             =   05/11/2007
Data de altima alteracao    =   06/11/2007
********************************************************************************************************/
function AJAX() {
	/*
        Funcao          :   AjaxRequest;
	    Descricao       :   Tenta criar e retorna, se possivel, um objeto XMLHttpRequest;
        Entrada         :   nada;
        Saida           :   XMLHttpRequest =  caso consiga criar o objeto;
                            false = caso nao consiga criar o objeto;
	*/
    this.AjaxRequest = function()
    {
        var returnAjax = false;
        if( window.XMLHttpRequest )
        {
            returnAjax = new XMLHttpRequest();
        } 
        else 
        {
            if ( window.ActiveXObject )
            {
                try
                {
                    returnAjax = new ActiveXObject("Msxml2.XMLHTTP");
                } 
                catch (e) 
                {
                    try 
                    {
                        returnAjax = new ActiveXObject("Microsoft.XMLHTTP");
                    } 
                    catch (e) 
                    {
                        alert("AJAX: AjaxRequest();");
                    }
                } 
            }
        }
        return returnAjax;
        
    }
    
    
    this.AjaxXml = function()
    {
        var returnXml = false;
        try
        {
            if (window.ActiveXObject) 
            { 
                returnXml = new ActiveXObject("Microsoft.XMLDOM");
            } 
            else 
            {
                try
                {
                    if (document.implementation && document.implementation.createDocument) 
                    { 
                        returnXml = document.implementation.createDocument("","",null);
                    } 
                }
                catch(e)
                {
                    alert("AJAX: AjaxXml();");
                }
            }
        }
        catch(e)
        {
            alert("AJAX: AjaxXml();");
        }
        
        return returnXml;
    }
    
    
    /*
        Funcao          :   RequestGET;
	    Descricao       :   Realizar uma requisicao via method GET atraves de um objeto XMLHTTPRequest;
        Entrada         :   xmlHttpRequest = obj XMLHTTPRequest;
                            URL = url onde será solicitada a informacao;
                            MethodOnReadyStateChange = metodo de tratamento da informacao solicitada;
        Saida           :   false = caso ocorra erros de processamento;
    */
    this.RequestGET = function( xmlHttpRequest, URL, MethodOnReadyStateChange )
    {
        try
        {
            if ( typeof(xmlHttpRequest) == "object" )
            {
                var timestamp = new Date();
                if ( URL.indexOf("?") > 0 )
                {
                    URL +=  "&ncache=" + timestamp.getTime()
                }
                else
                {
                    URL +=  "?ncache=" + timestamp.getTime()
                }
                
                xmlHttpRequest.open("GET", URL, true);
                eval("xmlHttpRequest.onreadystatechange = " + MethodOnReadyStateChange + ";");
                xmlHttpRequest.send(null);
            }
        }
        catch(e)
        {
            //alert("AJAX: RequestGET(xmlHttpRequest, URL, MethodOnReadyStateChange);");
            return false;
        }
    }
    
    /*
        Funcao          :   RequestSOAP;
	    Descricao       :   Realizar uma requisicao via method POST atraves de um objeto XMLHTTPRequest e o objet SOAP( Simple Object Access Protocol );
        Entrada         :   xmlSOAPRequest = obj document.implementation.createDocument("","",null);
                            xmlHttpRequest = obj XMLHTTPRequest;
                            URLWS = URL do WebService (http://www.xxx.com.br/webService.asmx)
                            URLMethod = URL do método do WebService para ser executado (http://www.xxx.com.br/GetWebService)
                            arrSOAP = array com parametros(nome e valores) a serem passados ao WebService
                            MethodOnReadyStateChange = metodo de tratamento da informacao solicitada;
        Saida           :   false = caso ocorra erros de processamento;
    */
    this.RequestSOAP = function( xmlSOAPRequest, xmlHttpRequest, URLWS, URLMethod, arrSOAP, MethodOnReadyStateChange )
    {
        try
        {
            if (( typeof(xmlHttpRequest) == "object" )&&( typeof(xmlSOAPRequest) == "object" ))
            {            
                if ( Browser.isIE ) 
                {
                    //Internet Explorer
                    xmlSOAPRequest.loadXML( this.GetXMLSOAP(arrSOAP) );
                }
                else
                {
                    //Firefox | Opera | Netscape
                    domParser = new DOMParser();
                    xmlSOAPRequest = domParser.parseFromString(this.GetXMLSOAP(arrSOAP), "text/xml");
                }
          
            	if(MethodOnReadyStateChange != null && MethodOnReadyStateChange != "null") eval("xmlHttpRequest.onreadystatechange = " + MethodOnReadyStateChange + ";");
            	xmlHttpRequest.open("POST", URLWS, true);
            	xmlHttpRequest.setRequestHeader ("SOAPAction", URLMethod);
            	xmlHttpRequest.setRequestHeader ("Content-Type", "text/xml");
                xmlHttpRequest.setRequestHeader ("Content-Length", this.GetXMLSOAP(arrSOAP).length);
            	xmlHttpRequest.send(xmlSOAPRequest);
            }
        }
        catch(e)
        {
            //alert("AJAX: RequestSOAP(xmlSOAPRequest, xmlHttpRequest, URL, arrSOAP, MethodOnReadyStateChange); " + e.message);
            return false;
        }
    }
    
    /*
        Funcao          :   GetXMLSOAP;
	    Descricao       :   Criar e retornar um XML(string) seguindo os padroes informados para o consumo de um WebService via SOAP;
        Entrada         :   listParameters = array com todas as informações necessárias para gerar o XML;
                                -> O padrão do XML é o seguinte:
                                    [0][0] = "SOAP";
                                    [0][1] = Modelo a ser gerado;
                                    [1][0] = Nome do WebService a ser consumido
                                    [1][1] = Endereço que se encontra o WebService
                                    [n][0] = Nome do atributo a ser gerado;
                                    [n][1] = Valor do atributo;
        Saida           :   xmlSOAP = XML(string) gerado a partir dos parâmetros informados;
                            false = caso ocorra erros de processamento;
    */
    this.GetXMLSOAP = function ( listParameters )
    {
        try
        {
            var i = 0;
            var xmlSOAP = "";
            if ( listParameters[0][0] == "SOAP" )
            {
                switch( listParameters[0][1] )
                {
                    case "1.1" :
                        //Modelo SOAP 1.1
                        xmlSOAP += "<?xml version='1.0' encoding='utf-8'?>";
                        xmlSOAP += "<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/'>";
                        xmlSOAP += "<soap:Body>";
                        xmlSOAP += "<" + listParameters[1][0] + " xmlns='" + listParameters[1][1] + "'>";
                        
                        for ( i=2; i<listParameters.length; i++ ) 
                        {
                            xmlSOAP += "<" + listParameters[i][0] + ">" + listParameters[i][1] + "</" + listParameters[i][0] + ">";
                        }
                            
                        xmlSOAP += "</" + listParameters[1][0] + ">";
                        xmlSOAP += "</soap:Body>";
                        xmlSOAP += "</soap:Envelope>";
                    
                        break;
                    case "1.2" :
                        //Modelo SOAP 1.2
                        xmlSOAP += "<?xml version=\"1.0\" encoding=\"utf-8\"?>";
                        xmlSOAP += "<soap12:Envelope xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:soap12=\"http://www.w3.org/2003/05/soap-envelope\">";
                        xmlSOAP += "<soap12:Body>";
                        xmlSOAP += "<" + listParameters[1][0] + " xmlns='" + listParameters[1][1] + "'>";
                        
                        for ( i=2; i<listParameters.length; i++ ) 
                        {
                            xmlSOAP += "<" + listParameters[i][0] + ">" + listParameters[i][1] + "</" + listParameters[i][0] + ">";
                        }
                            
                        xmlSOAP += "</" + listParameters[1][0] + ">";                        
                        xmlSOAP += "</soap12:Body>";
                        xmlSOAP += "</soap12:Envelope>";
                    
                        break;
                    default :
                        return false;
                }
            }            
            
            return xmlSOAP;
        }
        catch(e)
        {
            //alert("AJAX: GetXMLSOAP(listParameters);");
            return false;
        }
    }
}