﻿/* Spinner.js */

function pageIsValid(validationGroup)
{
    var valid = true;
    //Page_IsValid will be true at this point since the client-side validation functions will not be called until DoPostBack
    //We need to force validation
    if (typeof (Page_ValidationActive) != 'undefined')
    {
        if (Page_ValidationActive)
        {
            if (typeof (Page_ClientValidate) == 'function')
            {
                if (validationGroup)
                {
                    valid = Page_ClientValidate(validationGroup);
                }
                else
                {
                    valid = Page_ClientValidate();
                }
            }
        }

    }
    return valid;
}
function showSpinner(divid, validationGroup)
{
    if (pageIsValid(validationGroup))
    {
        jQuery('#' + divid).ajaxLoader();
    }
}
function hideSpinner(divid)
{
    jQuery('#' + divid).ajaxLoaderRemove();
}
function showSpinnerLarge(divid, validationGroup)
{
    if (pageIsValid(validationGroup))
    {
        jQuery('#' + divid).ajaxLoaderLarge();
    }
}
function hideSpinnerLarge(divid)
{
    jQuery('#' + divid).ajaxLoaderLargeRemove();
}
//Method for displaying the normal ajax error overlay
function showSpinnerError(divid, errorMessage)
{
    logAjaxError(errorMessage);
    jQuery('#' + divid).ajaxLoaderError();
    hideSpinner(divid);
}
//Method for removing the normal ajax error overlay
function hideSpinnerError(divid)
{
    jQuery('#' + divid).ajaxLoaderErrorRemove();
}
//Method for displaying the large ajax error overlay
function showSpinnerLargeError(divid, errorMessage)
{
    logAjaxError(errorMessage);
    jQuery('#' + divid).ajaxLoaderErrorLarge();
    hideSpinnerLarge(divid);
}
//Method for removing the large ajax error overlay
function hideSpinnerLargeError(divid)
{
    jQuery('#' + divid).ajaxLoaderErrorLargeRemove();
}

function logAjaxError(errorMessage)
{
    if (errorMessage == null || errorMessage == '')
    {
        errorMessage = 'Ajax error occurred. Error message was not specified.';
    }
    Amway.Core.Web.UI.Common.AmwayAjaxErrorControl.LogAjaxError(errorMessage, function(response) { });
}

function blockPage(validationGroup) {

  //check for pages not using the master => not declearing SpinnerMessageValue from the Page.Control
  var spinnermessage = "Please wait ....";
  try {
      spinnermessage = SpinnerMessageValue;
  }
  catch (e) {
      //Do Nothing
      //alert("spinnermessage failed");
  }
    
    if (pageIsValid(validationGroup))
    {
        $.blockUI({
        message: '<center><h4><img src="/Shop/Images/indicator_big_white.gif" valign="middle"/>&nbsp;<b>' + spinnermessage + '</b></h4></center>',
            css: {
                border: 'none',
                padding: '15px',
                backgroundColor: '#000',
                '-webkit-border-radius': '10px',
                '-moz-border-radius': '10px',
                opacity: .7,
                color: '#fff'
            }
        });
    }
}

function blockPageNoValidation() {
    //check for pages not using the master => not declearing SpinnerMessageValue from the Page.Control
    var spinnermessage = "Please wait ....";
    try {
        spinnermessage = SpinnerMessageValue;
    }
    catch (e) {
        //Do Nothing
        //alert("spinnermessage failed");
    }
   
    $.blockUI({
    message: '<center><h4><img src="/Shop/Images/indicator_big_white.gif" valign="middle"/>&nbsp;<b>' + spinnermessage + '</b></h4></center>',
        css: {
            border: 'none',
            padding: '15px',
            backgroundColor: '#000',
            '-webkit-border-radius': '10px',
            '-moz-border-radius': '10px',
            opacity: .7,
            color: '#fff'
        }
    });
}
function unblockPage()
{
    $.unblockUI();
}

var UpdatePanelSpinner = function(updatePanelId)
{
    var _updatePanelId = updatePanelId;
    var _prm = Sys.WebForms.PageRequestManager.getInstance();

    _prm.add_beginRequest(function(sender, e)
    {
        if ($('#' + _updatePanelId).find('#' + e.get_postBackElement().id).length > 0)
        {
            showSpinnerLarge(_updatePanelId, 'vg');
        }
    });

    _prm.add_endRequest(function(sender, e)
    {
        //no checks possible here
        hideSpinnerLarge(_updatePanelId);
    });
}