
function ApplicationLoadHandler(sender, args)
{	
	// add event handler during initializeRequest event
	if (!Sys.WebForms.PageRequestManager.getInstance().get_isInAsyncPostBack())
	{
		Sys.WebForms.PageRequestManager.getInstance().add_initializeRequest(BeginRequest);
		Sys.WebForms.PageRequestManager.getInstance().add_endRequest(EndRequest);
	}
}

// Perform checks on event submission
function BeginRequest(sender, args)
{
  //disable all button
  DisableAllButton();
  //change cursor into wait
  document.body.style.cursor = 'wait';
  
  if (typeof(CustomOnRequest) == "function")
    CustomOnRequest(true);
}

// perform error handling after receiving response
function EndRequest(sender, args) 
{    
    //enable all button
    EnableAllButton();
  
    //change cursor into default
    document.body.style.cursor = 'default';
    
    if (typeof(CustomOnRequest) == "function")
        CustomOnRequest(false);
    
    if (typeof(runSifr) == "function")
    {
        runSifr();//rerun sifr everything end request
    }
    
    // if error occurs, likely to be connection or server related error
    /*
    if (args.get_error() != undefined)
    {   
        args.set_errorHandled(true);     
        alert('There is a problem communicating with the web server. Please refresh the browser and try again later. \n' + args.get_error().description );
        
        return;
    }
    */
}

function DisableAllButton()
{
      // Get an array of all the input elements on the page.
      var inputs = document.getElementsByTagName('input');
     
      // Loop through the elements and check each one's type.
      //  if it's a submit type element, disable it.
      for (element in inputs)
      {   try
          {
             if (inputs[element].type == 'submit')
             {
                inputs[element].disabled = true;
                
             }
             inputs[element].style.cursor = 'wait';
          }catch(e){}
      }
}

function EnableAllButton()
{
      // Get an array of all the input elements on the page.
      var inputs = document.getElementsByTagName('input');
     
      // Loop through the elements and check each one's type.
      //  if it's a submit type element, disable it.
      for (element in inputs)
      {
          try
          {
            if (inputs[element].type == 'submit')
            { 
                inputs[element].disabled = false;
                
            }
            inputs[element].style.cursor = 'default';
          }catch(e){}
          
      }
}


if (typeof(Sys) !== "undefined") 
{
    Sys.Application.add_load(ApplicationLoadHandler);
    Sys.Application.notifyScriptLoaded();
}

	
        // Fires when page is saved
		function showProgressIndicator(id , validateFirst,validationGroup)
		{
		    var valResult = true;
		    
		    if (validateFirst) 
		    {   //validate page
		        if (!validationGroup)
		        {
		            validationGroup = "";
		        }
                valResult = Page_ClientValidate(validationGroup);
            }
            
            if (valResult)
            {
                //display progress bar
			    var progress = document.getElementById(id);
			    var ipts = progress.parentNode.getElementsByTagName("input");
			    for (var i=0; i<ipts.length; i++)
			    {
				    if (ipts[i].type == "submit" || ipts[i].type == "button")
				    {
					    ipts[i].readOnly = true;
					    ipts[i].className += " ReadOnly";
				    }
			    }
			    progress.style.display = "inline";
			    return true;
			}
		}
		
		function hideProgressIndicator(id)
		{
                //display progress bar
			    var progress = document.getElementById(id);
			    var ipts = progress.parentNode.getElementsByTagName("input");
			    for (var i=0; i<ipts.length; i++)
			    {
				    if (ipts[i].type == "submit" || ipts[i].type == "button")
				    {
					    ipts[i].readOnly = false;
					    ipts[i].className -= " ReadOnly";
				    }
			    }
			    progress.style.display = "none";
			    return true;
		}
		
		
    //function custom on ajax request
    //if you want to display general divwait.. put the divWait in the page
    function CustomOnRequest(begin)
    {
        if (begin)
        {//begin request
            ToggleDivShowHide("divWait",true);
        }
        else
        {
            ToggleDivShowHide("divWait",false);
        }
    }
		
	//toggle div show hide
    function ToggleDivShowHide(id,isShow)
    {
        var divWait = document.getElementById(id);
        if (divWait)
        {
            if (isShow)
            {
                divWait.style.display = "block";
            }
            else
            {
                divWait.style.display="none";
            }
            
        }
    }
