function createHttpObject()
  {
  if( window.XMLHttpRequest )
    {
    oHttp = new XMLHttpRequest();
    }
  else if( window.ActiveXObject )
    {
    try {
        oHttp = new ActiveXObject("Msxml2.XMLHTTP");
        }
    catch( e )
        {
        try {
            oHttp = new ActiveXObject("Microsoft.XMLHTTP");
            }
        catch( e )
            {
            oHttp = false;
            }
        }
    }
  return oHttp;
  }


function AdControl( sState, sCity, sTopic, paid )
  {
  this.div = document.createElement( "div" );
  this.div.className = "adjar";
  this.div.style.visibility = "hidden";
  this.div.style.width = "150px";
  this.div.style.left = this.getLeft() + "px";
  this.div.style.top  = this.getTop() + "px";
  document.body.appendChild( this.div );

  this.state = sState;
  this.city  = sCity;
  this.topic = sTopic;
  this.paid  = paid;
  this.http  = createHttpObject();
  
  var oNode = document.getElementById( "navDiv" );
  var navHeight = oNode.offsetHeight;
  oNode = document.getElementById( "outer" );
  var outerHeight = oNode.offsetHeight;
  var avail_space = outerHeight - navHeight;
  if( avail_space > 150 )
    {
    this.getAd( avail_space );
    }
  }

AdControl.prototype.getLeft =
function( )
  {
  var oNode = document.getElementById( "navDiv" );
  var iLeft = 5;
  while( oNode.tagName != "BODY" )
    {
    iLeft += oNode.offsetLeft;
    oNode  = oNode.offsetParent;
    }
  return iLeft;
  }

AdControl.prototype.getTop =
function( )
  {
  var oNode = document.getElementById( "navDiv" );
  var iTop = oNode.offsetHeight + 3;
  while( oNode.tagName != "BODY" )
    {
    iTop += oNode.offsetTop;
    oNode  = oNode.offsetParent;
    }
  return iTop;
  }

AdControl.prototype.getAd = 
function( avail_space )
  {
  var oHttp = this.http;
  var oThis = this;
  if( oHttp.readyState != 0 ) {
    oHttp.abort();
    }

  var aData = new Array();
  var sData = '';
  sData  = encodeURIComponent( "state" );
  sData += "=";
  sData += encodeURIComponent( this.state );
  aData.push( sData );

  sData  = encodeURIComponent( "city" );
  sData += "=";
  sData += encodeURIComponent( this.city );
  aData.push( sData );

  sData  = encodeURIComponent( "topic" );
  sData += "=";
  sData += encodeURIComponent( this.topic );
  aData.push( sData );

  sData  = encodeURIComponent( "paid" );
  sData += "=";
  sData += encodeURIComponent( this.paid );
  aData.push( sData );

  sData  = encodeURIComponent( "avail_space" );
  sData += "=";
  sData += encodeURIComponent( avail_space );
  aData.push( sData );


  var sFormData = aData.join("&");

  oHttp.open( "post", "../cgi-bin/adjar.cgi", true );
  oHttp.setRequestHeader( "Content-Type", "application/x-www-form-urlencoded" );
  oHttp.onreadystatechange = function() {
     if( oHttp.readyState == 4 )
       {
       oThis.div.innerHTML = oHttp.responseText;
       oThis.div.style.visibility = "visible";
       }
     };
  oHttp.send( sFormData );
  };
