// Timestamp of cart that page was last updated with
var RequestTimeout;
var timeout = 5000;
var async = true;

/*
 * Returns an new XMLHttpRequest object, or false if the browser
 * doesn't support it
 */
function createRequest () {
  // Try different things to see which browser is being used.
  try {
    // Non Microsoft browsers (Firefox, Safari etc.)
    return new XMLHttpRequest ();
  } catch (trymicrosoft) {
    try {
      // IE6 and above.
      return new ActiveXObject ("Msxml2.XMLHTTP");
    } catch (othermicrosoft) {
      try {
        // Older versions of IE i.e. pre-IE6.
        return new ActiveXObject ("Microsoft.XMLHTTP");
      } catch (failed) {
        // No support for XHR
        alert ("Error creating the XMLHttpRequest object! " + failed.message);
      } // End of catch (failed)
    } // End of catch (othermicrosoft)
  } // End of catch (trymicrosoft)
  return null;
} // End of function createRequest

/*
 * Returns a function that waits for the specified XMLHttpRequest
 * to complete, then passes it XML response to the given handler function.
 * req - The XMLHttpRequest whose state is changing
 * responseXmlHandler - Function to pass the XML response to
 */
function getReadyStateHandler (request, responseXmlHandler) {
  // Return an anonymous function that listens to the XMLHttpRequest instance
  return function () {
    try {
      if (request) {
        //document.getElementById("information").value = stat (request.readyState);
        //document.getElementById('TxL64O16E81').innerHTML = stat (request.readyState);

        // If the request's status is "complete"
        if (request.readyState == 4) {
          clearTimeout (RequestTimeout);

          //document.form1.statusnum.value = request.status;
          //document.form1.status.value = request.statusText;

          // Check that we received a successful response from the server
          if (request.status == 200) {
            try {

              //var response = request.responseText;

              // Pass the XML payload of the response to the handler function.
              responseXmlHandler (request.responseXML);
            // try
            } catch (e) {
              alert ('Exception: '+ e.description);
            } // End of try

            // Waitinganimation verstecken
            //document.getElementById("waiting").style.visibility = "hidden";

          // if (request.status == 200)
          } else {
            // An HTTP problem has occurred
            alert ("Error "+ request.status +": "+ request.statusText);
          }  // End of if (request.status == 200)

        // if (request.readyState == 4)
        } else {

          // Waiting ...
          //document.getElementById("waiting").style.visibility = "visible";

          //document.getElementById(ID).src = "PICS/waiting.gif";
          //document.getElementById(ID).width = "100";
          //document.getElementById(ID).height = "100";

        } // End of if (request.readyState == 4)
      } // End of if (request)
    // try
    } catch (e) {
      alert ('Exception: '+ e.description);
    } // End of try
  } // return function ()
} // End of function getReadyStateHandler

/*
 * Adds the specified item to the shopping cart, via Ajax call
 * postData - product code of the item to add
 */
function sendRequest (url, postData, responseHandler) {
  var request = createRequest();
  if (request) {
    request.open ("POST", url, async);
    request.setRequestHeader ("If-Modified-Since", "Sat, 1 Jan 2000 00:00:00 GMT");
    request.setRequestHeader ("Content-Type", "application/x-www-form-urlencoded");
    request.onreadystatechange = getReadyStateHandler (request, responseHandler);
    request.send (postData);
    RequestTimeout = setTimeout ("request.abort();", timeout);
  } else {
    alert("Браузер не поддерживает AJAX");
  }
} // End of function sendRequest

/*
function stat (n)
{
  switch (n) {
    case 0:
      return "не инициализирован";
    break;

    case 1:
      return "Loading ...";
    break;

    case 2:
      return "Loaded";
    break;

    case 3:
      return "Processing ...";
    break;

    case 4:
      return "Ready";
    break;

    default:
      return "неизвестное состояние";
  }
}
*/

