﻿
function BuildEmptyDivIfNeeded(id)
{
    if ( !(  $("#"+id).length)) 
    {
        $("body").prepend('<div id="'+id+'" style="display: none;"></div>');
    }
}

function ShowLoadingDialog(id) {

     BuildEmptyDivIfNeeded(id);
     var loadingDialogSelector = $("#" + id);

     $(loadingDialogSelector).html('<img src="/Content/Images/ajax-loader.gif" />');
     $(loadingDialogSelector).dialog({
         height: 200,
         width: 400,
         autoOpen: false,
         modal: true,
         closeOnEscape: false,
         resizable: false,
         title: 'Loading...',
         overlay: { background: "#5c5c5c", opacity: .8 }
     });
     $(loadingDialogSelector).dialog("open");
     return false;
 }



 function ShowAjaxPopup(ajaxUrl, title, height, width) {
     var dialogId = "ajaxDialog";

     BuildEmptyDivIfNeeded(dialogId);
     var loadingId = "ajaxLoadingDialog";

     ShowLoadingDialog(loadingId);
     var dialogSelector = $("#" + dialogId);
     var loadingSelector = $("#" + loadingId);

     $.ajax({
         url: ajaxUrl,
         cache: false,
         success: function (result) {
             $(loadingSelector).dialog("close");
             $(dialogSelector).dialog("destroy");

             if (result.ForceReloadCurrentPage === true) {
                 location.reload(true);
                 return;
             }

             $(dialogSelector).html(result);
             $(dialogSelector).dialog({
                 modal: true,
                 height: height,
                 width: width,
                 autoOpen: false,
                 //dialogClass: 'LightboxDetailsLink quick_view',
                 title: title,
                 resizeable: false,
                 overlay: { background: "#5c5c5c", opacity: .8 }
             });
             $(dialogSelector).dialog("open");
         }
         //, error: handled by global ajaxError
     });
     return false;
 }



 function ShowAjaxPopupQV(ajaxUrl, title, height, width) {
     var dialogId = "ajaxDialog";

     BuildEmptyDivIfNeeded(dialogId);
     var loadingId = "ajaxLoadingDialog";

     ShowLoadingDialog(loadingId);
     var dialogSelector = $("#" + dialogId);
     var loadingSelector = $("#" + loadingId);

     $.ajax({
         url: ajaxUrl,
         cache: false,
         success: function (result) {
             $(loadingSelector).dialog("close");
             $(dialogSelector).dialog("destroy");
             $(dialogSelector).html(result);
             $(dialogSelector).dialog({
                 modal: true,
                 height: height,
                 width: width,
                 autoOpen: false,
                 dialogClass: 'LightboxDetailsLink quick_view',
                 title: title,
                 resizeable: false,
                 overlay: { background: "#5c5c5c", opacity: .8 }
             });
             $(dialogSelector).dialog("open");
         }
         //, error: handled by global ajaxError
     });
     return false;
 }
