/*
    ajax.js

*/

var READY_STATE_COMPLETE = 4;
var req     = null;
var timerId = null;

function sendRequest( url, ready_state_change_function, http_method, params )
{
  if( ! http_method )
  {
    http_method = "GET";
  }
  if( ! params )
  {
    params = "";
  }

  if( window.XMLHttpRequest )
  {
    req = new XMLHttpRequest();
  }
  else if( window.ActiveXObject )
  {
    req = new ActiveXObject( "Microsoft.XMLHTTP" );
  }

  if( req )
  {
    try
    {
      req.open( http_method, url, true );
      req.setRequestHeader( "Content-Type", "application/x-www-form-urlencoded" );
      req.onreadystatechange=ready_state_change_function;
      req.send( params );
    }
    catch( e )
    {
      alert( 'Error: ' + e + '</br>' );
    }
  }
}


function sendRequestMultipart( url, onload_function, http_method, params )
{
  alert( "params: '" + params + "'" );
  if( ! http_method )
  {
    http_method = "GET";
  }
  if( ! params )
  {
    params = "";
  }

  if( window.XMLHttpRequest )
  {
    req = new XMLHttpRequest();
  }
  else if( window.ActiveXObject )
  {
    req = new ActiveXObject( "Microsoft.XMLHTTP" );
  }

  if( req )
  {
    try
    {
      req.multipart = true;

      req.open( http_method, url, true );
      req.setRequestHeader( "Content-Type", "application/x-www-form-urlencoded" );
      req.onload=onload_function;
      req.send( params );
    }
    catch( e )
    {
      alert( 'Error: ' + e + '</br>' );
    }
  }
}


//
//
//
var net = new Object();

net.READY_STATE_UNINITIALIZED = 0;
net.READY_STATE_LOADING       = 1;
net.READY_STATE_LOADED        = 2;
net.READY_STATE_INTERACTIVE   = 3;
net.READY_STATE_COMPLETE      = 4;

net.ContentLoader =
function( url, onload, params, args )
{
  this.params      = null;
  this.args        = args;
  this.http_method = "GET";
  this.req         = null;

  this.url         = url;
  this.onload      = onload;

  if( params )
  {
    this.params      = params;
    this.http_method = "POST";
  }

  this.loadXMLDoc( url );
}
  
net.ContentLoader.prototype =
{
  loadXMLDoc:function( url )
  {
    if( window.XMLHttpRequest )
    {
      this.req = new XMLHttpRequest();
    }
    else if( window.ActiveXObject )
    {
      this.req = new ActiveXObject( "Microsoft.XMLHTTP" );
    }

    if( this.req )
    {
      try
      {
        var loader = this;
        this.req.open( this.http_method, url, true );
        this.req.setRequestHeader( "Content-Type",
                                   "application/x-www-form-urlencoded" );
        this.req.onreadystatechange=
          function(){ loader.onReadyState.call( loader ); }
        this.req.send( loader.params );
      }
      catch( e )
      {
        alert( 'Error: ' + e + '</br>' );
      }
    }
  },
  
  onReadyState:function()
  {
    var ready = this.req.readyState;

    if( ready == net.READY_STATE_COMPLETE )
    {
      this.onload.call( this );
    }
    else
    {
      //alert( "loading...[" + ready + "]<br>" );
    }
  }

} // net.ContentLoader.prototype


function loadScript( source_file, type )
{
  var script = null;

  if( type == "js" )
  {
    script = document.createElement( "script" );
    script.setAttribute( "type", "text/JavaScript" );
    script.setAttribute( "src", source_file );
  }
  else if( type == "css" )
  {
    script = document.createElement( "link" );
    script.setAttribute( "rel", "stylesheet" );
    script.setAttribute( "type", "text/css" );
    script.setAttribute( "href", source_file );
  }

  if( typeof script != "undefined" )
  {
    var head = document.getElementsByTagName( "head" );
    
    if( head )
    {
      head[0].appendChild( script );
    }
  }
}


function displayContent()
{
  var div_content = document.getElementById( "div_content" );

  if( this.args )
  {
    var args_list = this.args.split( "," );
    var msg = '';

    for( var i=0; i<args_list.length; i++ )
    {
      msg += args_list[i] + "\n";

      var key_val = args_list[i].split( "=" );

      if( (key_val[0] == "js") || (key_val[0] == "css") )
      {
        loadScript( key_val[1], key_val[0] );
      }
    }
  }

  div_content.innerHTML = this.req.responseText;

  setCurrentURL( this.url, null, "/" );

}


function loadContent( url, args )
{
  var content_loader = new net.ContentLoader( url, displayContent, null, args );
}


