 /**
        Allows for simplified AJAX calls internally and externally

        @param {string} controllerPath The URL to which the request should be sent.
            If calling internally, use relative path for a controller action. The controller needs to be configured to pass back JSON,
            and should accept a single parameter called 'input'. If calling externally, specify the full URL, ensure it can accept a parameter called 'input',
            and make sure the URL being called is also returning JSON.
        @param {string} input (string / dictionary): Value to be passed in the request.
            Can be single string, or JSON-ready dictionary of key/value pairs.
        @param {boolean} asynchronous Specifies whether the call should be made asynchronously.
            If a responseMethod is specified to be handled in the AJAX response, async is safe. If a responseMethod is not specified
            (meaning, you are expecting a value to be returned from AJAXHelper), this can cause problems, as the code following AJAXHelper()
            may have values that have not yet been defined.
        @param {string} successMethod Specifies the name of a previously-defined (not anonymous) JS function to handle a successful AJAX response.
            This method should be defined somewhere the current 'window' object has access (a currently-loaded document). If successMethod
            is defined, AJAXHelper() will act as a void method, as the 'success' AJAX parameter will call the specified function to
            handle the response. If successMethod is not being used, pass in null or an empty string, and expect a value to be returned from AJAXHelper()
            to be handled in the caller of AJAXHelper().
        @param {string} errorMethod Specifies the name of a previously-defined (not anonymous) JS function to handle a failed AJAX response.
            This method should be defined somewhere the current 'window' object has access (a currently-loaded document). If errorMethod
            is defined, AJAXHelper() will act as a void method, as the 'error' AJAX parameter will call the specified function to
            handle the response. If errorMethod is not being used, pass in null or an empty string, and expect a value to be returned from AJAXHelper()
            to be handled in the caller of AJAXHelper().
        @param {boolean} hideLoader Specifies whether or not the loader gif should be displayed while the call is taking place.
        @param {boolean} keepLoader Specifies whether or not the loader gif should be removed once the call has taken place.

        Possible enhancements:
            - Create parameter to specify request headers
*/
 
function AJAXHelper(controllerPath, input, doAsynchronousCall, successMethod, errorMethod, hideLoader, keepLoader) {
    return CallAjax("GET",
        controllerPath,
        { "input": input },
        doAsynchronousCall,
        successMethod,
        errorMethod,
        hideLoader,
        keepLoader);
}


function CallAjax(httpMethod, requestUri, payload, asynchronous, successCallBackMethod, errorCallBackMethod, hideLoader, keepLoader) {
    var ajaxResponse = {};
    //console.log(requestUri + ":" + successCallBackMethod);

    if (payload.type !== null || payload.name !== "") {
        $.ajax({
            url: requestUri,
            async: asynchronous,
            data: payload,
            dataType: "json",
            method: httpMethod,
            beforeSend: function (xhr) {
                //console.log('beforesend');
                //fixes problem with loader not appearing sometimes
                if (typeof hideLoader === 'undefined' || hideLoader === null || hideLoader === false) {
                   // console.log("showing loader");
                    setTimeout(function () {
                        if (!$(".loader-gif").is(":visible")) { $(".loader-gif").show();}
                    }, 250);
                }       
            },
            complete: function (xhr) {
               // console.log('complete');
                //fixes problem with loader not *disappearing* sometimes!
                if (typeof keepLoader === 'undefined' || keepLoader === null || keepLoader === false) {
                  //  console.log("hiding loader");
                    setTimeout(function () {
                        $(".loader-gif").hide();
                    }, 500);
                }
            },
            success: function (response) {
                if (successCallBackMethod !== undefined && successCallBackMethod !== null && successCallBackMethod !== "") {
                    //console.log(requestUri);
                    //console.log("calling " + successCallBackMethod);
                    //console.log(asynchronous);
                    window[successCallBackMethod](response, asynchronous);
                    ajaxResponse = response;
                   // ajaxResponse = {};
                } else {
                    ajaxResponse = response;
                }
            },
            error: function (response) { 
                if (errorCallBackMethod !== undefined && errorCallBackMethod !== null && errorCallBackMethod !== "") {
                    window[errorCallBackMethod](response, asynchronous);
                    ajaxResponse = response;
                    //ajaxResponse = {};
                } else {
                    ajaxResponse = response;
                }
            }
        });
    }
    return ajaxResponse;
}

 /*
    AutoComplete(): Allows for and handles AJAX calls for autocomplete-specific functionality.
        Handles responses of both array and object types.

        inputId (string): Id for the client-side input using the autocomplete functionality.
            Should be single string.
        datalistId (string): Id for the datalist to which the response items will be appended.
            Should be single string. Specified if the input in question is not a dropdown / select item (a text input, for instance).
            Make sure that the datalist is bound to the input in question. If not needed (input is a select item), pass in empty string.
        controllerMethod (string): The URL to which the request should be sent.
            Use relative path for a controller action. The controller needs to be configured to pass back JSON, and should accept a single parameter called 'input'.

        Possible enhancements:
            - Not sure this is needed any longer, since we've started using the Infragistics igCombo Box functionality. Main reason for the switch was that
                (as of when this code was written), Safari does not support the use of datalists, severly limiting the use of this function. The igCombo Box also
                allowed for more consistent cross-browser behavior.
            -DF: 4/25/2019  Using this for adminpartial run-as autocomplete because I didn't want to load all the igcombo dependencies on the site index
*/
 function AutoComplete(inputId, datalistId, controllerPath) {
     AutoComplete(inputId, datalistId, controllerPath, null, null);
 }

 function AutoComplete(inputId, datalistId, controllerPath,elementIdToShowBefore, elementIdToShowAfter) {
     if ($(inputId).val().length >= 3) {
         var elementToShowBefore = $(elementIdToShowBefore);
         var elementToShowAfter = $(elementIdToShowAfter);
        
         if (elementToShowBefore !== null && elementToShowBefore !== undefined &&
             elementToShowAfter !== null && elementToShowAfter !== undefined) {
             elementToShowBefore.show();
             elementToShowAfter.hide();
         }

        $.ajax({
            url: controllerPath,
            async: true,
            data: {
                input: $(inputId).val()
            },
            dataType: "json",
            method: "GET",
            success: function (response) {
                if (elementToShowBefore !== null && elementToShowBefore !== undefined &&
                    elementToShowAfter !== null && elementToShowAfter !== undefined) {
                    elementToShowBefore.hide();
                    elementToShowAfter.show();
                }
                var len = 10;
                if (Array.isArray(response.results)) {
                    if (response.results.length < 10) {
                        len = response.results.length;
                    }
                    if (datalistId !== "") {
                        $(datalistId + " option").remove();
                        for (var i = 0; i < len; i++) {
                            $(datalistId).append("<option value=\"" + response.results[i] + "\">" + response.results[i] + "</option>");
                        }
                        $(datalistId).css("visibility", "visible");
                        var input = document.querySelector(datalistId);
                        input.value = input.value;
                    }
                    else {
                        $(inputId + " option").remove();
                        for (var i = 0; i < len; i++) {
                            $(inputId).append("<option value=\"" + response.results[i] + "\">" + response.results[i] + "</option>");
                        }
                        $(inputId).css("visibility", "visible");
                        var input = document.querySelector(inputId);
                        input.value = input.value;
                    }
                }
                else {
                    if (response.results.length < 10) {
                        len = response.results.length;
                    }
                    if (datalistId !== "") {
                        $(datalistId + " option").remove();
                        Object.keys(response.results).forEach(function (key) {
                            $(datalistId).append("<option value=\"" + key + "\">" + response.results[key] + "</option>");
                        });
                        $(datalistId).css("visibility", "visible");
                    }
                    else {
                        $(inputId + " option").remove();
                        Object.keys(response.results).forEach(function (key) {
                            $(inputId).append("<option value=\"" + key + "\">" + response.results[key] + "</option>");
                        });
                        $(inputId).css("visibility", "visible");
                    }
                }
            }
        });                    
    }
}

function IsDateInPast(element) {
    var date = Date.parse(element);
    var now = new Date();
    now.setHours(0, 0, 0, 0);
    if (date < now) {
        return true;
    }
    else {
        return false;
    }
}



