﻿function Utilities() {

    this.centerPopup = function(popupID) {
        var windowWidth = $(window).width();
        var windowHeight = $(window).height();
        var popupWidth = $('#' + popupID).width();
        var popupHeight = $('#' + popupID).height();

        $('#' + popupID).css({
            "left": (windowWidth / 2) - (popupWidth / 2),
            "top": (windowHeight / 2) - (popupHeight / 2)
        });
    }

    this.showPopup = function(modalBackgroundID, popupID) {
        this.centerPopup(popupID);
        var displayVal = $('#' + popupID).css("display");
        if (displayVal == "none") {
            $('#' + modalBackgroundID).css({ "opacity": "0.5" });
            $('#' + modalBackgroundID).fadeIn('fast');
            $('#' + popupID).fadeIn('fast');
        }
    }

    this.hidePopup = function(modalBackgroundID, popupID) {
        var displayVal = $('#' + popupID).css("display");
        if (displayVal == "block") {
            $('#' + modalBackgroundID).fadeOut('fast');
            $('#' + popupID).fadeOut('fast');
        }
    }

    this.DisplayInfoPopup = function() {
        this.centerPopup('InfoPopup');
        var displayVal = $('#InfoPopup').css("display");
        if (displayVal == "none") {
            $('#InfoModalBackground').css({ "opacity": "0.5", "z-index": "100000" });
            $('#InfoModalBackground').fadeIn('fast');
            $('#InfoPopup').css({ "z-index": "100001" });
            $('#InfoPopup').fadeIn('fast');
        }
    }

    this.CloseInfoPopup = function(modalBackgroundID, popupID) {
        var displayVal = $('#InfoPopup').css("display");
        if (displayVal == "block") {
            $('#InfoModalBackground').fadeOut('fast');
            $('#InfoPopup').fadeOut('fast');
        }
    }

    this.ShowInfoPopup = function(Title, Content) {
        $('#InfoPopup .Header .Title').html(Title);
        $('#InfoPopup .Content .Text').html(Content);
        this.DisplayInfoPopup();
    }

    this.ProcessAjaxError = function(args) {
        this.ShowInfoPopup('Error', args.get_error().message.substring(53, args.get_error().message.length));
    }

    this.ProcessJqueryAjaxError = function(XMLHttpRequest, textStatus, errorThrown) {
        var err = eval("(" + XMLHttpRequest.responseText + ")");
        this.ShowInfoPopup('Error', err.Message);
    }

    this.Download = function(url, data, method) {
        //url and data options required
        if (url && data) {
            //data can be string of parameters or array/object
            data = typeof data == 'string' ? data : jQuery.param(data);
            //split params into form inputs
            var inputs = '';
            jQuery.each(data.split('&'), function() {
                var pair = this.split('=');
                inputs += '<input type="hidden" name="' + pair[0] + '" value="' + pair[1] + '" />';
            });
            //send request
            jQuery('<form action="' + url + '" method="' + (method || 'post') + '">' + inputs + '</form>').appendTo('body').submit().remove();
        };
    };
}

var utilities;
$(document).ready(function() {
    utilities = new Utilities();
});
