/*
 *  The js file is used to perform operation related to asynchroniouse ajax calls.
 *
 */

/*
 * Generic function which makes a ajax call.
 * @param urlPath: the server path location where we will make ajax call.
 * @param callbackMethod : name of the callback method which should be called once results are fetched.
 */
function setAjaxCallDetails(urlPath, callbackMethod) {

  var xmlHttpRequest;
  try {
    // Firefox, Opera 8.0+, Safari
    xmlHttpRequest = new XMLHttpRequest();
  } catch (e) {
    // Internet Explorer
    try {
      xmlHttpRequest = new ActiveXObject("Msxml2.XMLHTTP");
    } catch (e) {
      try {
        xmlHttpRequest = new ActiveXObject("Microsoft.XMLHTTP");
      } catch (e) {
        alert("Your browser does not support AJAX!");
        return false;
      }
    }
  }

  ajaxCallComplete = false;
  // This is the callback method which gets called once the data is received.
  xmlHttpRequest.onreadystatechange = function() {
    if (xmlHttpRequest.readyState !== 4) {
      // not ready yet
      return;
    }
    if (xmlHttpRequest.status !== 200 && xmlHttpRequest.status !== 0) {
      // ready, but not OK
      alert("There was a problem with the request.(Code: "
          + xmlHttpRequest.status + ")");
      return;
    }

    var response = xmlHttpRequest.responseText;

    // this is the callback method which gets called based on the parameter
    // passed.
    callbackMethod(response);
  }
  // makes a server call.
  xmlHttpRequest.open("GET", urlPath, true);
  // override the cache validation - this is required for repetitive ajax calls on IE 7
  xmlHttpRequest.setRequestHeader("If-Modified-Since", "Thu, 1 Jan 1970 00:00:00 GMT");
  xmlHttpRequest.setRequestHeader("Cache-Control", "no-cache");
  //send request
  xmlHttpRequest.send(null);

}