// This JavaScript was automatically generated by
// FluorineFx.Json.Rpc.DefaultJsonRpcProxyGenerator, FluorineFx, Version=1.0.0.15, Culture=neutral, PublicKeyToken=1875756ba09931ab
// on Monday, September 13, 2010 at 5:21:56 PM (India Standard Time)

// Default Proxy

function ServiceLibrary2(autocomplete) {
    this["SearchCityCountryByKeyword"] = function(prefixText, callback) {
        return call("SearchCityCountryByKeyword", [prefixText], callback);
    }

    this["SearchCitiesByCountry"] = function(prefixText, contextKey, callback) {
        return call("SearchCitiesByCountry", [prefixText, contextKey], callback);
    }

    this["GetDataForMemberTripsMap"] = function(UserID, callback) {
        return call("GetDataForMemberTripsMap", [UserID], callback);
    }

    this["GetDestination"] = function(CityID, callback) {
        return call("GetDestination", [CityID], callback);
    }

    var url = typeof (url) === 'string' ? url : 'http://www.baraaza.com/jsongateway.aspx?proxy&destination=fluorine&source=Baraaza.Business.FlashService.ServiceLibrary2';
    var self = this;
    var nextId = 0;
    var credentials;
    var http = null;

    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 = 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));
        if (autocomplete) {
            autocomplete.ClearAutoCompleteList();
            autocomplete.UpdateProgressBar(true);
        }
        return request.id;
    }

    this["AbortRequest"] = function() {
        if (http != null) {
            http.abort();
            if (autocomplete) {
                autocomplete.UpdateProgressBar(false);
            }
        }
    }

    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);
            if (autocomplete) {
                autocomplete.UpdateProgressBar(false);
            }
        }
        if (sender.readyState == 2 || sender.readyState == 3) {
            if (autocomplete) {
                autocomplete.ClearAutoCompleteList();
                autocomplete.UpdateProgressBar(true);
            }
        }
    }

    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 */
    }
}

ServiceLibrary2.rpcMethods = ["SearchCityCountryByKeyword", "SearchCitiesByCountry", "GetDataForMemberTripsMap", "GetDestination"];

