﻿var doCallback;

/// -----------------------------------------------------------------------
/// Provides the ability to make an Ajax call as a function of the
/// passed parameters.
///
///	<param name="webServiceUrl" type="String">
/// Set to the URL of the web service (not including the specific
/// method in the web service to call
///	</param>
///	<param name="webMethod" type="String">
/// The name of the method in the web service to call
///	</param>
///	<param name="data" type="String">
/// The data to be passed to the web service.  The data must be formatted
/// as JSON name/value pairs.  For Example:
///     {id: 23,
///      value: "A value"}
/// Set to null if not required
///	</param>
///	<param name="successMethodName" type="String">
/// The name of the callback method to call on success.
/// Set to null if not required
///	</param>
///	<param name="failureMethodName" type="String">
/// The name of the callback method to call on failure.
/// Set to null if not required
///	</param>
function MakeAjaxCall(webServiceUrl,
                      webMethod,
                      data,
                      successMethodName,
                      failureMethodName,
                      waitMessage)
{
  var url = webServiceUrl + "/" + webMethod;
  var successMethod;
  var failureMethod;

  if (successMethodName == null)
  {
    successMethod = AjaxSucceeded;
  }
  else
  {
    successMethod = successMethodName;
  }

  if (failureMethodName == null)
  {
    failureMethod = AjaxFailed;
  }
  else
  {
    failureMethod = failureMethodName;
  }

  $.ajax(
    { type: "POST",
      url: url,
      data: data,
      contentType: "application/json; charset=utf-8",
      dataType: "json",
      success: successMethod,
      error: failureMethod
    }
  );

  // set block UI mesasge if one has been provided
//  if (waitMessage != null)
//  {
//    $.blockUI.defaults.message = waitMessage;
//  }

  // set timer to call blockUI if the callback takes > 100 mSec
  // NOTE: Timer is cleared in success and failure callback methods
  //  $.blockUI.defaults.fadeIn = "fast";
  //  $.blockUI.defaults.fadeOut = "fast";
//  doCallback = setTimeout(function() { $.blockUI({ fadeIn: "fast", fadeOut: "fast", message: waitMessage }) },
//                          100);
}

/// -----------------------------------------------------------------------
/// Provides the default callback method to call upon success
function AjaxSucceeded()
{
  clearTimeout(doCallback);
  $.unblockUI();
}

/// -----------------------------------------------------------------------
/// Provides the default callback method to call upon failure
///	<param name="result" type="IXMLHTTPRequest">
///	</param>
function AjaxFailed(result)
{
  clearTimeout(doCallback);
  $.unblockUI();
  //  alert(result.status + ' ' + result.statusText);
  alert("Error occurred.  Please try again...");
}

