﻿// JScript File
Type.registerNamespace('Habitat.WebParts.GeoLocSearch.JavaScript');

// Constructor
Habitat.WebParts.GeoLocSearch.JavaScript.Proxy = function() {
    this._timeout = 0;
    this._userContext = null;
    this._succeeded = null;
    this._failed = null;
    this._urlService = '/_layouts/Habitat.WebParts.GeoLocSearchWebPart/Data/GeoLocSearchHandler.ashx';
}

Habitat.WebParts.GeoLocSearch.JavaScript.Proxy.prototype = {
    get_xhr: function() {
        var xhr = null;
        try {
            xhr = new XMLHttpRequest();
        } catch (e) {
            try { xhr = new ActiveXObject("Msxml2.XMLHTTP"); }
            catch (e2) {
                try { xhr = new ActiveXObject("Microsoft.XMLHTTP"); }
                catch (e) { }
            }
        }
        return xhr;
    },
    Call: function(comboBoxDataType, pName, pValue, delegate, i, j) {
        var url = this._urlService + "?RequestDataType=RequestDataTypeCombo&ComboBoxDataType=" + comboBoxDataType + "&" + pName + "=" + pValue + "&time=" + new Date().getTime();
        //alert(url + '\n' + i + '\n' + j);
        window.status += url + '|';

        // Create the WebRequest object to call the handler
        var webRequest = new Sys.Net.WebRequest();
        webRequest.set_url(url);
        webRequest.set_httpVerb("GET");
        webRequest.set_userContext({ I: i, J: j, Delegate: delegate });
        webRequest.add_completed(Function.createDelegate(this, this.OnWebRequestCompleted));
        webRequest.invoke();
    },
    CallItems: function(delegate, idAgency, idCity, idLodgingNature, idHousingType,
                        idLodgingType, idWorkingStatus, isSelling, i, j) {
        // Initialize the url
        var url = this._urlService;
        url += "?RequestDataType=RequestDataTypeItems";
        url += "&IdAgency=" + idAgency;
        url += "&IdCity=" + idCity;
        url += "&IdLodgingNature=" + idLodgingNature;
        url += "&IdHousingType=" + idHousingType;
        url += "&IdLodgingType=" + idLodgingType;
        url += "&IdWorkingStatus=" + idWorkingStatus;
        url += "&IsSelling=" + isSelling;
        url += "&time=" + new Date().getTime();

        // Set the window status to inform the user that a request will be send
        window.status += url + '|';

        // Create the WebRequest object to call the handler
        var webRequest = new Sys.Net.WebRequest();
        webRequest.set_url(url);
        webRequest.set_httpVerb("GET");
        webRequest.set_userContext({ I: i, J: j, Delegate: delegate });
        webRequest.add_completed(Function.createDelegate(this, this.OnWebRequestCompleted));
        webRequest.invoke();
    },
    OnWebRequestCompleted: function(executor, eventArgs) {
        if (executor.get_responseAvailable()) {
            var resultData = executor.get_responseData();
            var context = executor.get_webRequest().get_userContext();
            var result = null;
            eval('result=' + resultData + '');

            context.Delegate(result, context.I, context.J);
        }
        else {
            if (executor.get_timedOut())
                alert("Timed Out");
            else
                if (executor.get_aborted())
                alert("Aborted");
        }
    }

}

Habitat.WebParts.GeoLocSearch.JavaScript.Proxy.registerClass('Habitat.WebParts.GeoLocSearch.JavaScript.Proxy', Sys.Component);

// Since this script is not loaded by System.Web.Handlers.ScriptResourceHandler
// invoke Sys.Application.notifyScriptLoaded to notify ScriptManager 
// that this is the end of the script.
if (typeof (Sys) !== 'undefined') Sys.Application.notifyScriptLoaded();
