﻿var divID;
function SortOperation(anchorObject, renderDivID, url) {
    divID = renderDivID;
    var data = '{}';
    id = $.getParam("sortColumn", url);
    orderClass = $.getParam("sortOrder", url);
    ShowLoading($('[id$=' + divID + ']'));
    SendJsonCallToGetStatisticsContent(url, data, CheckGetStatisticsContentResponse, CheckGetStatisticsContentErrorResponse);
}

function CheckGetStatisticsContentErrorResponse(xmlReq) {
    var msg;
    if (xmlReq.responseText) {
        var err = xmlReq.responseText;
        if (err)
            msg = err;
        else
            msg = "Could not find the statistic service.";
    }
    $('[id$=divLoading]').hide();
    document.getElementById(divID).innerHTML = msg;
    return false;
}

function CheckGetStatisticsContentResponse(response) {
    var data = response;
    if (data == null || data.length == 0) {

        document.getElementById(divID).innerHTML = "Could not find the statistic service.";
        return false;
    }
    var id = "#" + divID;
    $('[id$=divLoading]').hide();
    document.getElementById(divID).innerHTML = $(data).find(id).html();
    return false;
}
function SendJsonCallToGetStatisticsContent(url, rawdata, onSuccess, onFalilure) {
    $.ajax(
		    {
		        type: 'POST',
		        url: url,
		        data: rawdata,
		        processData: true,
		        contentType: "application/json",
		        timeout: 10000,
		        dataType: "text/html",
		        success: onSuccess,
		        error: onFalilure
		    });
}


function RedirectToPage(url) {
    window.location = url;
}
function ShowLoading(obj) {
    var offset = $(obj).offset();
    var top = offset.top + ($(obj).height() / 2);
    var left = offset.left + ($(obj).width() / 2);
    $('[id$=divLoading]').css('top', top + "px");
    $('[id$=divLoading]').css('left', left + "px");
    $('[id$=divLoading]').show();
}

(function($) {
    $.getParam = function(key, url) {
        //get querystring(s) without the ?
        var urlParams = decodeURI(url); //MySideNOTE: do not use unescape() for URI, use decodeURI()

        //if no querystring, return null
        if (urlParams == false | urlParams == '') return null;

        //get key/value pairs
        var pairs = urlParams.split("&");

        var keyValue_Collection = {};
        for (var value in pairs) {
            //let's get the position of the first occurrence of "=", in case value has "=" in it
            var equalsignPosition = pairs[value].indexOf("=");

            if (equalsignPosition == -1) //in case there's only the key, e.g: http://7php.com/?niche
                keyValue_Collection[pairs[value]] = ''; //you could change the value to true as per your needs
            else
                keyValue_Collection[pairs[value].substring(0, equalsignPosition)] = pairs[value].substr(equalsignPosition + 1);
        }
        return keyValue_Collection[key];
    }
})
(jQuery);
