/*--------------------------------------------------------------------------*
 *
 *  YET ANOTHER AJAX LOADER
 *  =======================
 *
 *  Version: 1.2
 *  ------------
 *  Date:    March 6th 2008
 *  - added get/post selection
 *  - added ajax_createxhr function
 *  - added IE7 support
 *
 *  Version: 1.1
 *  ------------
 *  Date:    February 17th 2008
 *  - added js eval support
 *  - removed 1 jquery dependency
 *  - added an accesor to change the get/post action mode
 *
 *  (c) 2006 - 2008 Emile Alabassi
 *  Author: Emile Alabassi
 *
 *  Copyright (C) 2007,2008 Emile Alabassi <emile.alabassi@neufcegetel.fr>
 *
 *--------------------------------------------------------------------------*/
var g_user      = false;
var g_passwd    = false;
var g_infodivid = 'infobox';
var g_ajaxload_str = false;
var g_action_type  = 'POST'; /* default case */

var g_cb_callback = false;
var g_cb_args = false;
var g_cb_registered = 0;

/*--------------------------------------------------------------------------*/
/*----------------------------- ACCESSORS ----------------------------------*/
/*--------------------------------------------------------------------------*/

function ajax_register_callback(callback, args)
{
  g_cb_callback   = callback;
  g_cb_args       = args;
  g_cb_registered = 1;
}

/*------------------------------ the img div settings ----------------------*/
function ajax_set_infodivid(divid)
{
  g_infodivid = divid;
}

function ajax_set_actiontype(action_type)
{
  g_action_type = action_type;
}

/*----------------------------- authentification  -------------------------*/
function ajax_set_auth(user, passwd)
{
  ajax_set_user(user);
  ajax_set_passwd(passwd);
}

function ajax_set_user(user)
{
  g_user = user;
}

function ajax_set_passwd(passwd)
{
  g_passwd = passwd;
}

/*----------------------------- the post mode (get / post) -----------------*/
function ajax_set_post_mode()
{
  g_action_type = 'POST';
}

function ajax_set_get_mode()
{
  g_action_type = 'GET';
}

/*--------------------- the string being displayed while waitng ------------*/
function ajax_set_loadstr(str)
{
  g_ajaxload_str = str;
}

/*----------------------- show / hide divs functions -----------------------*/
function ajax_show(id)
{
  var element;
  element = document.getElementById(id);

  if (element)
    {
      element.style.display = 'block';
    }
}

function ajax_hide(id)
{
  var element;
  element = document.getElementById(id);

  if (element)
    {
      element.style.display = 'none';
    }
}

function ajax_showhide(id)
{
  var element;
  element = document.getElementById(id);

  if (element)
    {
      if(element.style.display == 'block')
	{
	  element.style.display = 'none';
	} 
      else
	{
	  element.style.display = 'block';
	}
    }
}

function ajax_eval_div(divid)
{
  var i;
  var x;
  
  x = divid.getElementsByTagName("script"); 
  
  for(i = 0; i < x.length; i++)
    {
      eval(x[i].text);
    }
}

function ajax_writediv(divid, str)
{
  var element;
  element = document.getElementById(divid);
  if (element)
    {
      element.innerHTML = str;
    }
}

/*--------------------------------------------------------------------------*/
/*----------------------- THE MAIN FUNCTIONS -------------------------------*/
/*--------------------------------------------------------------------------*/

/* call a remote page, send its result to divid, and eval js_code as param */
function ajax_callrpc_js(divid, url, js_code)
{
  ajax_callrpc(divid, url);
  eval(js_code);
}

function ajax_createxhr()
{
  var xhr = false;
  
  if(window.XMLHttpRequest) // FIREFOX
    {
      xhr = new XMLHttpRequest();
    }
  else if(window.ActiveXObject) // IE
    {
      xhr = new ActiveXObject("Microsoft.XMLHTTP");
      if (!xhr)
	{
	  xhr = ActiveXObject('Msxml2.XMLHTTP');
	  if (!xhr)
	    {
	      xhr = ActiveXObject('Msxml3.XMLHTTP');
	    }
	}
    }
  return xhr;
}

function ajax_createxhr_tmp() 
{
  var request = false;
  try 
    {
      request = new ActiveXObject('Msxml2.XMLHTTP');
    }
  catch (err2) 
    {
      try 
	{
	  request = new ActiveXObject('Microsoft.XMLHTTP');
	}
    catch (err3) 
      {
	try 
	  {
	    request = new XMLHttpRequest();
	  }
	catch (err1) 
	  {
	    request = false;
	  }
      }
    }
  return request;
}

/* call a remote page and send its result to divid */
function ajax_callrpc(divid, url)
{
  var ret;
  var xhr;
  var asynch;
  var data;
  var script;

  var callback;
  var callback_args;
  
  var temp = new Array();  
  
  if ( g_cb_registered == 1)
    {
      callback      = g_cb_callback;
      callback_args = g_cb_args;
      
      // erase globals
      
      g_cb_registered = 0;
      g_cb_callback   = false;
      g_cb_args       = false;
    }

  var temp = new Array();
  temp = url.split('?');
  script = temp[0];
  data   = temp[1];
  
  /* perform an asynchrone call ! */
  asynch = true;

  xhr = ajax_createxhr();
  if (xhr == false)
    {
      ajax_writediv(divid, "<span style=\"color:red; font-weight:bold;\">" + "Error, xmlhttp is not available on your browser" +  "</span>");
      ajax_show(divid);
      return false;
    }
  
  /* clear the div in case it has already been populated */
  ajax_writediv(divid, '');
  
  ajax_show(g_infodivid);
  data += "&ajax=true";

  if (g_action_type == 'GET')
    {
      xhr.open(g_action_type, script + '?' + data, asynch, g_user, g_passwd);
      xhr.setRequestHeader("User-Agent",   "diag-ajax");
    }
  else
    {
      xhr.open(g_action_type, script, asynch, g_user, g_passwd);
      xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
      xhr.setRequestHeader("User-Agent",   "diag-ajax");
      xhr.send(data);
    }

  xhr.onreadystatechange = function ()
    {
      /*
	readyState possible values
	0 - UNINITIALIZED
	1 - LOADING
	2 - LOADED
	3 - INTERACTIVE
	4 - COMPLETE
      */
      switch (xhr.readyState)
     {
       case 3:
       break;
       
       case 0:
       case 1:
       case 2:
       {
	 if (g_ajaxload_str != false)
	   {
	     alert(g_ajaxload_str);
	   }	 
       }
       break;
       
       case 4:
       {

	 switch (xhr.status)
	   {
	   case 200:
	     {
	       res = xhr.responseText;
	       ajax_writediv(divid, res);
	       ajax_hide(g_infodivid);
		   
		   if (callback)
		   {
		     var _callback_str = callback + "('" + callback_args + "');";
		     // evaluate it (there must be a better way to do it in a c-like fashion)
		     eval (_callback_str);
		   }		   
 	     }
	     break;
	     
	   case 404:
	     {
	       res = "<span style=\"color:red; font-weight:bold;\">Server error (" + xhr.status + " " + xhr.statusText + ")</span>";
	       ajax_writediv(divid, res);	
	       ajax_hide(g_infodivid);
	     }
	     break;
	   }
       }
       break;       
     }
    }

  if (g_action_type == 'GET')
    {
      xhr.send(null);
    }

  ajax_show(divid);

  /* check and eval js code if any */
  ajax_eval_div(divid);
}

/* special case for remote thickbox pages */
function ajax_calltb(title, url, width, height)
{
  var qsize;
  
  qsize  = "&width="+width;
  qsize += "&height="+height;

  tb_show(title, url + qsize, false);
}
/*--------------------------------------------------------------------------*/
