var privateTopSecretNeverUseMeEverRequest = null;
var timeOutReached = false;

function ajaxTestTimeout(callback) {
    window.clearTimeout();

    if (privateTopSecretNeverUseMeEverRequest && (privateTopSecretNeverUseMeEverRequest.readyState < 2)) {
        privateTopSecretNeverUseMeEverRequest.close;
        privateTopSecretNeverUseMeEverRequest = null;
        timeOutReached = true;
        eval(callback(null));
    }
}

/*
  RemoteObject allows for easy AJAX.  It can be instantiated directly or inherited.
*/
function RemoteObject(){
  var request = this.GetRequestObject();
  var browser = (function x() { })[-5] == 'x' ? 'FF3' : (function x() { })[-6] == 'x' ? 'FF2' : /a/[-1] == 'a' ? 'FF' : '\v' == 'v' ? 'IE' : /a/.__proto__ == '//' ? 'Saf' : /s/.test(/a/.toString) ? 'Chr' : /^function \(/.test([].sort) ? 'Op' : 'Unknown';

  /*
    SendRequest is what actually does the AJAX functionality.  It will return the XMLHttpRequestObject if running syncronously.
    If running asynchronously, it will return a boolean true if the request was made successfully, false otherwise.
  */
  this.SendRequest =  function(url, method, vars, async, callback, contenttype, timeout){
                        if(request){
                          if(!async){
                            async = false;
                          }

                          if(async && callback){
                            request.onreadystatechange = function(){
                              /*
                                  Request Object status integer:
                                    0 = uninitialized
                                    1 = loading
                                    2 = loaded
                                    3 = interactive
                                    4 = complete
                              */
                              if(request.readyState == 4){/* only process if complete */
                                callback(request);
                              } else if((request.readyState == 1) && (timeout) && (timeout > 0)){
                                privateTopSecretNeverUseMeEverRequest = request;
                                window.setTimeout('ajaxTestTimeout('+ callback +')', timeout);
                              }
                            };
                          }

                          if (method.toString().toLowerCase() == 'get') {
                            if (vars && (vars != '')) {
                              request.open(method, url + '?' + vars, async);
                            } else {
                              request.open(method, url, async);
                            }

                            request.send('');
                          } else {
                            request.open(method, url, async);

                            if (contenttype && (contenttype != '')) {
                                request.setRequestHeader("Content-type", contenttype);
                            }
                            else {
                                request.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
                            }

                            request.setRequestHeader("Content-length", vars.length);
                            request.setRequestHeader("Connection", "close");
                            request.send(vars);
                          }

                          if(!async){
                            return request;
                          } else {
                            return true;
                          }
                        } else {
                          return false;
                        }
                    };
  /*
    Indicates if the web browser in use is Internet Explorer.
  */
  this.IsInternetExplorer = function(){
                              return (browser.indexOf('IE') > -1);
                            };
  /*
    Indicates if the web browser in use is Firefox.
  */
  this.IsFirefox = function() {
                     return (browser.indexOf('FF') > -1);
                   };
  /*
    Indicates if the web browser in use is Chrome.
  */
  this.IsChrome = function() {
                     return (browser.indexOf('Chr') > -1);
                  };
  /*
    Indicates if the web browser in use is Safari.
  */
  this.IsSafari = function() {
                     return (browser.indexOf('Saf') > -1);
                  };
  /*
    Indicates if the web browser in use is Opera.
  */
  this.IsOpera = function() {
                   return (browser.indexOf('Op') > -1);
                 };
  /*
    Indicates if the web browser in use is unknown.
  */
  this.IsUnknown = function() {
                     return (browser.indexOf('Unknown') > -1);
                   };
}
/*
  Creates and returns a new HttpRequest Object for use with AJAX requests.
*/
RemoteObject.prototype.GetRequestObject = function(){
  var req = false;

  if(window.XMLHttpRequest && !(window.ActiveXObject)) {/* branch for native XMLHttpRequest object */
    try {
      req = new XMLHttpRequest();
    } catch(e) {
      req = false;
    }
  } else if(window.ActiveXObject) {/* branch for IE/Windows ActiveX version */
    try {
      req = new ActiveXObject('Msxml2.XMLHTTP');
    } catch(e) {
      try {
        req = new ActiveXObject('Microsoft.XMLHTTP');
      } catch(e) {
        req = false;
      }
    }
  }

  return req;
};
/*
  Helper function to retrieve a cookie's value.
*/
RemoteObject.prototype.GetCookie = function(cookieName){
  var cookieStartPosition;
  var cookieEndPosition;

  if (document.cookie.length > 0){
    cookieStartPosition = document.cookie.indexOf(cookieName + "=");

    if (cookieStartPosition != -1){
      cookieStartPosition = cookieStartPosition + cookieName.length + 1;
      cookieEndPosition = document.cookie.indexOf(";", cookieStartPosition);

      if (cookieEndPosition == -1){
         cookieEndPosition = document.cookie.length;
      }

      return unescape(document.cookie.substring(cookieStartPosition, cookieEndPosition));
    }
  }
  return "";
};
/*
  Helper function to set a cookie's value and expiration.
*/
RemoteObject.prototype.SetCookie = function(cookieName, cookieValue, cookieExpiresDays){
  var expireDate = new Date();

  expireDate.setDate(expireDate.getDate() + cookieExpiresDays);

  document.cookie = cookieName + "=" + escape(cookieValue) +
                    ((cookieExpiresDays == null) ? "" : ";expires=" + expireDate.toGMTString());
};