﻿var cnping =
{
    cnServerUrl: "http://cnpmintegration/services/service.svc/",
    cnAuthKeyParam: "app=pm2",
    cnAuthKeyWithCallbackParam: "app=pm2&callback=?",

    /******************************************
    * Sends a request to the CN Server
    * On success, sets the flag in the session to true or false
    *******************************************/
    pingCNServer: function() {

        if ($.browser.msie) {

            var url = cnping.cnServerUrl + "ping?" + cnping.cnAuthKeyWithCallbackParam;

            //Call the JSON API to get the data.
            $.getJSON(url, function(data) {
                var cnServerExists = false;
                if (data.Message != null) {
                    cnServerExists = true;
                }
                //Call the webservice to update the value in the session.
                West.Firm360.Web.WebServices.PreferencesWebService.SetIsCNEnabledFlag(cnServerExists,
                function() { /* on success do nothing */ },
                function(result) { /* log the error */West.Firm360.Web.WebServices.LoggingWebService.LogError("common.pingCNServer.onSetIsCNEnabledFlagFailure", result, null, null); });
            });
        }

    },

    getResults: function(isFullList, sectionContainerControlId, asyncTaskRequest) {

        if (isFullList) {
            getFullListHtml(sectionContainerControlId, asyncTaskRequest);
        }
        else {
            getSectionHtml(sectionContainerControlId, asyncTaskRequest);
        }
    },

    /******************************************
    * Sends a request to the CN Server. On return sets the value in the session and continues 
    * to execute code to render results. 
    *******************************************/
    pingCNServerAndGetResults: function(isFullList, sectionContainerControlId, asyncTaskRequest) {

        if ($.browser.msie) {

            var timeoutHandle = setTimeout(function() { cnping.getResults(isFullList, sectionContainerControlId, asyncTaskRequest) }, 3000);

            var url = cnping.cnServerUrl + "ping?" + cnping.cnAuthKeyWithCallbackParam;

            try {
                //Call the JSON API to get the data.
                $.getJSON(url, function(data) {
                    var cnServerExists = false;
                    if (data.Message != null && data.Message == "hello") { //check the message value. if API is disabled, message will be "disabled"
                        cnServerExists = true;
                        clearTimeout(timeoutHandle);
                    }
                    //Call the webservice to update the value in the session.
                    West.Firm360.Web.WebServices.PreferencesWebService.SetIsCNEnabledFlag(cnServerExists,
                function() { /* on success call the function to get results html*/
                    cnping.getResults(isFullList, sectionContainerControlId, asyncTaskRequest)
                },
                function(result) {
                    /* log the error */West.Firm360.Web.WebServices.LoggingWebService.LogError("common.pingCNServer.onSetIsCNEnabledFlagFailure", result, null, null);
                    //call the function to get results html even if there is an error with Set CN flag call.
                    cnping.getResults(isFullList, sectionContainerControlId, asyncTaskRequest)
                });
                });
            }
            catch (exception) {
                cnping.getResults(isFullList, sectionContainerControlId, asyncTaskRequest)
            }

        }
        else { //If browser is not IE, proceed to get the section as usual. no need to ping the CN server.
            cnping.getResults(isFullList, sectionContainerControlId, asyncTaskRequest)
        }

    }
}

