// This JavaScript was automatically generated by
// FluorineFx.Json.Rpc.JsonRpcProxyGenerator, FluorineFx, Version=1.0.0.12, Culture=neutral, PublicKeyToken=1875756ba09931ab
// on terça-feira, 7 de setembro de 2010 at 20:20:47 (E. South America Standard Time)

// Proxy version 1.0

function ServiceHistoria(url)
{
    this["listar"] = function(callback)
    {
        return call("listar", [ ], callback);
    }
    
    this["remover"] = function(id, callback)
    {
        return call("remover", [ id ], callback);
    }
    
    this["paginarPorLoja"] = function(id, callback)
    {
        return call("paginarPorLoja", [ id ], callback);
    }
    
    this["obterPorLoja"] = function(Lojaid, callback)
    {
        return call("obterPorLoja", [ Lojaid ], callback);
    }
    
    this["cadastrar"] = function(area, callback)
    {
        return call("cadastrar", [ area ], callback);
    }
    
    this["obter"] = function(id, callback)
    {
        return call("obter", [ id ], callback);
    }
    
    this["paginar"] = function(pagina, callback)
    {
        return call("paginar", [ pagina ], callback);
    }
    
    var url = typeof(url) === 'string' ? url : 'http://www.casabraz.com.br/jsongateway.aspx?proxy&destination=fluorine&source=ServiceLibrary.ServiceHistoria';
    var self = this;
    var nextId = 0;
    var credentials;

    this['setCredentials'] = function(userid, password)
    {
        if( userid == null && password == null )
            this.credentials = null;
        else
            this.credentials = Base64.encode(userid + ':' + password);
    }

    function call(method, params, callback)
    {
        var request = { id : nextId++, method : method, params : params, credentials:self.credentials };
        return callback == null ? 
            callSync(method, request) : callAsync(method, request, callback);
    }

    function callSync(method, request)
    {
        var http = newHTTP();
        http.open('POST', url, false, self.httpUserName, self.httpPassword);
        setupHeaders(http, method);
        http.send(JSON.stringify(request));
        if (http.status != 200)
            throw { message : http.status + ' ' + http.statusText, toString : function() { return message; } };
        var response = JSON.parse(http.responseText);
        if (response.error != null) throw response.error;
        return response.result;
    }

    function callAsync(method, request, callback)
    {
        var http = newHTTP();
        http.open('POST', url, true, self.httpUserName, self.httpPassword);
        setupHeaders(http, method);
        http.onreadystatechange = function() { http_onreadystatechange(http, callback); }
        http.send(JSON.stringify(request));
        return request.id;
    }

    function setupHeaders(http, method)
    {
        http.setRequestHeader('Content-Type', 'text/plain; charset=utf-8');
        http.setRequestHeader('X-JSON-RPC', method);
    }

    function http_onreadystatechange(sender, callback)
    {
        if (sender.readyState == /* complete */ 4)
        {
            var response = sender.status == 200 ? 
                JSON.parse(sender.responseText) : {};
            
            response.xmlHTTP = sender;
                
            callback(response);
        }
    }

    function newHTTP()
    {
        if (typeof(window) != 'undefined' && window.XMLHttpRequest)
            return new XMLHttpRequest(); /* IE7, Safari 1.2, Mozilla 1.0/Firefox, and Netscape 7 */
        else
            return new ActiveXObject('Microsoft.XMLHTTP'); /* WSH and IE 5 to IE 6 */
    }
}

ServiceHistoria.rpcMethods = ["listar","remover","paginarPorLoja","obterPorLoja","cadastrar","obter","paginar"];
