﻿Sys.Application.add_load(AppLoad);

function AppLoad()
{
    Sys.WebForms.PageRequestManager.getInstance().add_initializeRequest(CheckStatus);  
  
  Sys.WebForms.PageRequestManager.getInstance().add_endRequest(EndRequest);
  //Sys.WebForms.PageRequestManager.getInstance().add_beginRequest(BeginRequest);
}
 

var lastPostBackElement;
var updateError = "false";
function BeginRequest(sender, args) {
  // Clear the error if it's visible from a previous request.
  if ($get('AjaxError').style.display == "")
  {
    //CloseError();
  }
}
 
function EndRequest(sender, args) {
  // Check to see if there's an error on this request.
  if (args.get_error() != undefined)
  {
    var prm = Sys.WebForms.PageRequestManager.getInstance();
    
    // If there is, show the custom error.
    ShowWarningDiv('AjaxError', args.get_error());
    
    //disable javascript timer so errors won't continue
    Sys.WebForms.PageRequestManager.getInstance().abortPostBack();
    
    updateError = "true";
    // Let the framework know that the error is handled, 
    //  so it doesn't throw the JavaScript alert.
    args.set_errorHandled(true);
  }
}
 

function CheckStatus(sender, args)
{
  var prm = Sys.WebForms.PageRequestManager.getInstance();
  
  if ($get('AjaxError').style.display == "")
  {
    prm.abortPostBack();
    args.set_cancel(true);
  }
  
  if (prm.get_isInAsyncPostBack() & updateError) 
  {
     args.set_cancel(true);
     ShowWarningDiv('AjaxError', 'Still working on previous request.');
  }
  
  else if (!prm.get_isInAsyncPostBack() & updateError) 
  {
     ShowWarningDiv('AjaxError', 'Processing....');
  }
  
  
  lastPostBackElement = args.get_postBackElement().id
  
}

function ShowWarningDiv(div, msg)
{
    $get(div).style.display = "";
    $get(div).innerHTML = "There was a data update error. Please refresh your page.";
}


function CloseError() {
  // Hide the error div.
  $get('AjaxError').style.visibility = "hidden";
}
if(typeof(Sys) !== "undefined") Sys.Application.notifyScriptLoaded();

