Hodnoceni_Webu = function(url) {
    this['submit'] = function(data, callback) {
        return call('submit', [data], callback);
    }

    var url = typeof(url) === 'string' ? url : 'http://www.domestav.cz/cs/hodnoceni-webu?mode=rpc';
    var self = this;
    var nextId = 0;

    function call(method, params, callback)
    {
        var request = { id : nextId++, method : method, params : params };
            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.response;
        }

        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));
            request.http = http;
            return request;
        }

        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 req;
                  try {
                      req = sender.status == 200 ?
                          JSON.parse(sender.responseText) : {};
                          callback(req.result, req.response);
                  } catch(e) {} 


            }
        }

        function newHTTP()
        {
            return typeof(ActiveXObject) === 'function' ?
                new ActiveXObject('Microsoft.XMLHTTP') : /* IE 5 */
                new XMLHttpRequest(); /* Safari 1.2, Mozilla 1.0/Firefox, and Netscape 7 */
        }


}

Hodnoceni_Webu.methods = ["submit"];

