// This JavaScript was automatically generated by
// Jayrock.JsonRpc.Web.JsonRpcProxyGenerator, Jayrock, Version=0.9.12915.0, Culture=neutral, PublicKeyToken=null
// on Saturday, May 25, 2013 at 10:33:30 PM (Pacific Daylight Time)

function DemoService(url)
{
    var self = this;
    var m = ["echo","echoObject","echoArgs","echoAsStrings","echoGuid","add","getStringArray","now","newGuid","cookies","serverVariables","getAuthor","getCouple","swapNames","getDataSet","getDataTable","getRowArray","getRowCollection","getDataView","getFirstDataRow","getFirstDataRowView","getDropDown","getDataGrid","total","sleep","throwError","format","counter","encode","decode","stypeof","echoTime","wadd","system.listMethods","system.version","system.about"];
    var idems = [true,true,true,true,true,true,true,true,true,true,true,true,true,true,true,true,true,true,true,true,true,true,true,true,true,true,true,true,true,true,true,true,true,true,true,true];
    
    // Echoes back the text sent as input.
    
    this[m[0]] = function /* echo */ (text, callback)
    {
        if (self.kwargs) return rpc(new Call(0, { text: text }, callback));
        return rpc(new Call(0, [ text ], callback));
    }
    
    // Echoes back the object sent as input.
    
    this[m[1]] = function /* echoObject */ (o, callback)
    {
        if (self.kwargs) return rpc(new Call(1, { o: o }, callback));
        return rpc(new Call(1, [ o ], callback));
    }
    
    // Echoes back the arguments sent as input. This method demonstrates variable number of arguments.
    
    this[m[2]] = function /* echoArgs */ (args, callback)
    {
        if (self.kwargs) return rpc(new Call(2, { args: args }, callback));
        return rpc(new Call(2, [ args ], callback));
    }
    
    // Echoes back the arguments as an array of strings. This method demonstrates working with variable number of arguments.
    
    this[m[3]] = function /* echoAsStrings */ (args, callback)
    {
        if (self.kwargs) return rpc(new Call(3, { args: args }, callback));
        return rpc(new Call(3, [ args ], callback));
    }
    
    // Echoes back the given GUID. This method demonstrates working with an argument typed as System.Guid.
    
    this[m[4]] = function /* echoGuid */ (id, callback)
    {
        if (self.kwargs) return rpc(new Call(4, { id: id }, callback));
        return rpc(new Call(4, [ id ], callback));
    }
    
    // Return the sum of two integers.
    
    this[m[5]] = function /* add */ (a, b, callback)
    {
        if (self.kwargs) return rpc(new Call(5, { a: a, b: b }, callback));
        return rpc(new Call(5, [ a, b ], callback));
    }
    
    // Returns an array of city names. Demonstrates returning a strongly-typed array.
    
    this[m[6]] = function /* getStringArray */ (callback)
    {
        if (self.kwargs) return rpc(new Call(6, { }, callback));
        return rpc(new Call(6, [ ], callback));
    }
    
    // Returns the local time on the server. Demonstrates how DateTime is returned simply as a string using the ISO 8601 format.
    
    this[m[7]] = function /* now */ (callback)
    {
        if (self.kwargs) return rpc(new Call(7, { }, callback));
        return rpc(new Call(7, [ ], callback));
    }
    
    // Generates and returns a GUID as a string.
    
    this[m[8]] = function /* newGuid */ (callback)
    {
        if (self.kwargs) return rpc(new Call(8, { }, callback));
        return rpc(new Call(8, [ ], callback));
    }
    
    // Returns the cookie names seen by the server.
    
    this[m[9]] = function /* cookies */ (callback)
    {
        if (self.kwargs) return rpc(new Call(9, { }, callback));
        return rpc(new Call(9, [ ], callback));
    }
    
    // Returns the server variables collection at the server. Demonstrates returning NameValueCollection.
    
    this[m[10]] = function /* serverVariables */ (callback)
    {
        if (self.kwargs) return rpc(new Call(10, { }, callback));
        return rpc(new Call(10, [ ], callback));
    }
    
    // Returns information about the author. Demonstrates how a Hashtable from the server is automatically converted into an object on the client-side.
    
    this[m[11]] = function /* getAuthor */ (callback)
    {
        if (self.kwargs) return rpc(new Call(11, { }, callback));
        return rpc(new Call(11, [ ], callback));
    }
    
    // Returns a server-typed object representing a couple. Demonstrates to returning server-typed objects.
    
    this[m[12]] = function /* getCouple */ (callback)
    {
        if (self.kwargs) return rpc(new Call(12, { }, callback));
        return rpc(new Call(12, [ ], callback));
    }
    
    // Swaps first and last name of person. Demonstrates receiving and returning a server-typed object.
    
    this[m[13]] = function /* swapNames */ (p, callback)
    {
        if (self.kwargs) return rpc(new Call(13, { p: p }, callback));
        return rpc(new Call(13, [ p ], callback));
    }
    
    // Returns the Northwind employees as a DataSet.
    
    this[m[14]] = function /* getDataSet */ (callback)
    {
        if (self.kwargs) return rpc(new Call(14, { }, callback));
        return rpc(new Call(14, [ ], callback));
    }
    
    // Returns the Northwind employees as a DataTable.
    
    this[m[15]] = function /* getDataTable */ (callback)
    {
        if (self.kwargs) return rpc(new Call(15, { }, callback));
        return rpc(new Call(15, [ ], callback));
    }
    
    // Returns the Northwind employees as an array of DataRow objects.
    
    this[m[16]] = function /* getRowArray */ (callback)
    {
        if (self.kwargs) return rpc(new Call(16, { }, callback));
        return rpc(new Call(16, [ ], callback));
    }
    
    // Returns the Northwind employees as a DataRowCollection.
    
    this[m[17]] = function /* getRowCollection */ (callback)
    {
        if (self.kwargs) return rpc(new Call(17, { }, callback));
        return rpc(new Call(17, [ ], callback));
    }
    
    // Returns the Northwind employees as a DataView object.
    
    this[m[18]] = function /* getDataView */ (callback)
    {
        if (self.kwargs) return rpc(new Call(18, { }, callback));
        return rpc(new Call(18, [ ], callback));
    }
    
    // Returns the first Northwind employee as a DataRow object.
    
    this[m[19]] = function /* getFirstDataRow */ (callback)
    {
        if (self.kwargs) return rpc(new Call(19, { }, callback));
        return rpc(new Call(19, [ ], callback));
    }
    
    // Returns the first Northwind employee as a DataRowView object.
    
    this[m[20]] = function /* getFirstDataRowView */ (callback)
    {
        if (self.kwargs) return rpc(new Call(20, { }, callback));
        return rpc(new Call(20, [ ], callback));
    }
    
    // Returns a data-bound DropDownList to the client as HTML.
    
    this[m[21]] = function /* getDropDown */ (callback)
    {
        if (self.kwargs) return rpc(new Call(21, { }, callback));
        return rpc(new Call(21, [ ], callback));
    }
    
    // Returns a data-bound DataGrid to the client as HTML.
    
    this[m[22]] = function /* getDataGrid */ (callback)
    {
        if (self.kwargs) return rpc(new Call(22, { }, callback));
        return rpc(new Call(22, [ ], callback));
    }
    
    // Returns the total of all integers sent in an array.
    
    this[m[23]] = function /* total */ (values, callback)
    {
        if (self.kwargs) return rpc(new Call(23, { values: values }, callback));
        return rpc(new Call(23, [ values ], callback));
    }
    
    // Blocks the request for the specified number of milliseconds (maximum 7 seconds).
    
    this[m[24]] = function /* sleep */ (milliseconds, callback)
    {
        if (self.kwargs) return rpc(new Call(24, { milliseconds: milliseconds }, callback));
        return rpc(new Call(24, [ milliseconds ], callback));
    }
    
    // Throws an error if you try to call this method.
    
    this[m[25]] = function /* throwError */ (callback)
    {
        if (self.kwargs) return rpc(new Call(25, { }, callback));
        return rpc(new Call(25, [ ], callback));
    }
    
    // Formats placeholders in a format specification with supplied replacements. This method demonstrates fixed and variable arguments.
    
    this[m[26]] = function /* format */ (format, args, callback)
    {
        if (self.kwargs) return rpc(new Call(26, { format: format, args: args }, callback));
        return rpc(new Call(26, [ format, args ], callback));
    }
    
    // Increments a counter and returns its new value. Demonstrates use of session state.
    
    this[m[27]] = function /* counter */ (callback)
    {
        if (self.kwargs) return rpc(new Call(27, { }, callback));
        return rpc(new Call(27, [ ], callback));
    }
    
    // Returns the bytes of a string in a given encoding that are transmitted as a Base64 string.
    
    this[m[28]] = function /* encode */ (s, encoding, callback)
    {
        if (self.kwargs) return rpc(new Call(28, { s: s, encoding: encoding }, callback));
        return rpc(new Call(28, [ s, encoding ], callback));
    }
    
    // Returns the string from encoded bytes (transmitted as a Base64 string).
    
    this[m[29]] = function /* decode */ (bytes, encoding, callback)
    {
        if (self.kwargs) return rpc(new Call(29, { bytes: bytes, encoding: encoding }, callback));
        return rpc(new Call(29, [ bytes, encoding ], callback));
    }
    
    // Returns the CLR type that a given value converted to on the server.
    
    this[m[30]] = function /* stypeof */ (o, callback)
    {
        if (self.kwargs) return rpc(new Call(30, { o: o }, callback));
        return rpc(new Call(30, [ o ], callback));
    }
    
    // Echoes back the date/time sent as parameter.
    
    this[m[31]] = function /* echoTime */ (time, callback)
    {
        if (self.kwargs) return rpc(new Call(31, { time: time }, callback));
        return rpc(new Call(31, [ time ], callback));
    }
    
    // Adds two float arguments and returns their result. This method demostrates use of warped parameters.
    
    this[m[32]] = function /* wadd */ (x, y, callback)
    {
        if (self.kwargs) return rpc(new Call(32, { x: x, y: y }, callback));
        return rpc(new Call(32, [ x, y ], callback));
    }
    
    // Returns an array of method names implemented by this service.
    
    this[m[33]] = function /* system.listMethods */ (callback)
    {
        if (self.kwargs) return rpc(new Call(33, { }, callback));
        return rpc(new Call(33, [ ], callback));
    }
    
    // Returns the version server implementation using the major, minor, build and revision format.
    
    this[m[34]] = function /* system.version */ (callback)
    {
        if (self.kwargs) return rpc(new Call(34, { }, callback));
        return rpc(new Call(34, [ ], callback));
    }
    
    // Returns a summary about the server implementation for display purposes.
    
    this[m[35]] = function /* system.about */ (callback)
    {
        if (self.kwargs) return rpc(new Call(35, { }, callback));
        return rpc(new Call(35, [ ], callback));
    }
    
    var url = typeof(url) === 'string' ? url : 'http://www.raboof.com/projects/jayrock/demo.ashx';
    var nextId = 0;

    function Call(method, params, callback)
    {
        this.url = url;
        this.callback = callback;
        this.proxy = self;
        this.idempotent = idems[method];
        this.request = 
        { 
            id     : ++nextId, 
            method : m[method], 
            params : params 
        };
    }

    function rpc(call)
    {
        return self.channel != null && typeof(self.channel.rpc) === 'function' ?
            self.channel.rpc(call) : call;
    }

    this.kwargs = false;
    this.channel = new JayrockChannel();

    function JayrockChannel()
    {
        this.rpc = function(call)
        {
            var async = typeof(call.callback) === 'function';
            var xhr = newXHR();
            xhr.open('POST', call.url, async, this.httpUserName, this.httpPassword);
            xhr.setRequestHeader('Content-Type', this.contentType || 'application/json; charset=utf-8');
            xhr.setRequestHeader('X-JSON-RPC', call.request.method);
            if (async) xhr.onreadystatechange = function() { xhr_onreadystatechange(xhr, call.callback); }
            xhr.send(JSON.stringify(call.request));
            call.handler = xhr;
            if (async) return call;
            if (xhr.status != 200) throw new Error(xhr.status + ' ' + xhr.statusText);
            var response = JSON.parse(xhr.responseText);
            if (response.error != null) throw response.error;
            return response.result;
        }

        function xhr_onreadystatechange(sender, callback)
        {
            if (sender.readyState == /* complete */ 4)
            {
                try { 
                    sender.onreadystatechange = null; // Avoid IE7 leak (bug #12964)
                } 
                catch (e) { 
                    /* IE 6/Mobile throws for onreadystatechange = null */ 
                }

                var response = sender.status == 200 ? 
                    JSON.parse(sender.responseText) : {};
                
                callback(response, sender);
            }
        }

        function newXHR()
        {
            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 */
        }
    }
}

DemoService.rpcMethods = ["echo","echoObject","echoArgs","echoAsStrings","echoGuid","add","getStringArray","now","newGuid","cookies","serverVariables","getAuthor","getCouple","swapNames","getDataSet","getDataTable","getRowArray","getRowCollection","getDataView","getFirstDataRow","getFirstDataRowView","getDropDown","getDataGrid","total","sleep","throwError","format","counter","encode","decode","stypeof","echoTime","wadd","system.listMethods","system.version","system.about"];
