/**
 * Global request object
 */
var request     = null;
var requestUrl  = "/ajax/";
var isWorking   = false;

/**
 * Function initializes a request object which has already been constructed.
 * Parameters:
 *  requestType    - HTTP request type: GET or POST.
 *  url            - The URL of the handler script.
 *  asynchronous   - Boolean: use asynchronous connection or not.
 *  responseHandle - The name of the function that will handle the response.
 */
function initRequest( requestType, url, asynchronous, responseHandle ) {
  try {
      // Set the callback function
      request.onreadystatechange = responseHandle;

      // Try to open connection
      request.open( requestType, url, asynchronous );

      if( requestType.toLowerCase() == "post" ) {
        request.setRequestHeader( "Content-Type", "application/x-www-form-urlencoded; charset=utf-8" );
        request.send( arguments[4] );
      } else {
        request.send( null );
      }
      document.getElementById('ajaxLoader').style.visibility = 'visible';
  } catch (errv) {
    isWorking = false;
    document.getElementById('ajaxLoader').style.visibility = 'hidden';
    alert(  "The application cannot contact the server at the moment.\n"+
            "Please try again in a few seconds.\n" );
  }
}

/**
 * Function constructs a Request object. Handles both Mozilla-based
 * browsers and Microsoft-browsers.
 * Parameters:
 *  requestType    - HTTP request type: GET or POST.
 *  url            - The URL of the handler script.
 *  asynch         - Boolean: use asynchronous connection or not.
 *  responseHandle - The name of the function that will handle the response.
 *  Note:
 *  Any fifth parameters represented as arguments[4] are the data a
 *  POST request is designed to send.
 */
function httpRequest( requestType, url, asynch, responseHandle ) {
  try
  {
    // Firefox, Opera 8.0+, Safari
    request=new XMLHttpRequest();
  }
  catch (e)
  {
    // Internet Explorer
    try
    {
      request=new ActiveXObject("Msxml2.XMLHTTP");
    }
    catch (e)
    {
      try
      {
        request=new ActiveXObject("Microsoft.XMLHTTP");
      }
      catch (e)
      {
        alert("Your browser does not support AJAX!");
        return false;
      }
    }
  }

  // Test for a null request if neither ActiveXObject was initialized
  if ( request ) {
    if( requestType.toLowerCase() != "post" ) {
      initRequest( requestType, url, asynch, responseHandle );
    } else {
      //the POSTed data
      var args = arguments[4];
      if( args != null && args.length > 0 ){
        initRequest( requestType, url, asynch, responseHandle, args );
      }
    }
  }
}

function findPositionX(obj) {
  var left = 0;

  if(obj.offsetParent) {
    while(1) {
      left += obj.offsetLeft;

      if(!obj.offsetParent)
        break;

      obj = obj.offsetParent;
    }
  }
  else if(obj.x) {
    left += obj.x;
  }

  return left;
}

function findPosY(obj) {
    var top = 0;

    if(obj.offsetParent) {
      while(1) {
        top += obj.offsetTop;

        if(!obj.offsetParent)
          break;

        obj = obj.offsetParent;
      }
    }
    else if(obj.y) {
      top += obj.y;
    }

    return top;
}
