/*  AJAX Callback
 *  2006 Tim Igoe (tim@jellymedia.com)
 *
/*--------------------------------------------------------------------------*/

function ajax() 
{
  this.req = null;
  this.url = 'rpc.php';
  this.handler = null;
  this.method = 'GET';
  this.async = true;
  this.Req = null;
  this.responseFormat = 'text';
  this.postData = null;
  this.response = null;
  this.field = null;
  
   // Initilise the XMLHTTPRequest Object
  this.init = function()
  {
    if (!this.Req)
	{
      if (window.XMLHttpRequest) 
      { // Mozilla, Safari, ...
        this.req = new XMLHttpRequest();
		return true;
      }
      else if (window.ActiveXObject) 
      { // IE
       this.req = new ActiveXObject("Microsoft.XMLHTTP");
	   return true;
      }  
	  return false;
	}
	return true; // Already initilized
  };
  
  // Send the message
  this.sendReq = function()
  {
    if (!this.init())
	{
	  alert('Javascript Error - please make sure you are using an upto date browser.');
	  return;
	}

	this.req.open(this.method, this.url + '?file=' + this.handler + '&query=' + this.postData, this.async);
	
	var self = this;
	
    // Process the return from the communication
	this.req.onreadystatechange = function ()
	{  
      if (self.req.readyState == 4)
      { 
        if (self.req.status == 200)
        {  
		  if (self.type == 'html')
		  {
			div = document.getElementById(self.field);
			div.innerHTML = self.req.responseText.substr(0, self.req.responseText.length - 2);
			hideLoading();
		  }
		  else if (self.type == 'javascript')
		  {
            self.response = self.req.responseText.substr(0, self.req.responseText.length - 2);
            eval(self.response);
			showLoading();
		  }
          else if (self.type == 'string')
		  {
            self.response = self.req.responseText.split("||");
            self.process(self.response);
		  }
          else if (self.type == 'array')
		  {
            self.response = self.req.responseText.split("\n");
		    self.processTable(self.response);
		  }
        }
		else if (this.req.status == 404)
		{
		  alert('Error - File Not Found');
		}
      }	 
	  
	  return this.response;
	}	

	this.req.send(null);
  };
  
  this.abort = function()
  {
    if (this.req)
	{
	  this.req.onreadystatechange = function() { };
	  this.req.abort();
	  this.req = null;
	}
  }; 
 
  this.process = function(data)
  {
	this.response = data;
    var response = '';
    for (var i = 0; i < this.response.length; i++)
    {
	  if (this.response[i] != '')
	  {
		this.response[i] = this.response[i].replace("\n", "");
  	    response += '<div class="unroll" onMouseOver="this.className=\'roll\'" onmouseout="this.className=\'unroll\'" onclick="fillForm(\'' + this.field + '\', \'' + this.response[i] + '\')">' + this.response[i] + '</div>';
	  }
    }
    if (response != '')
    {
  	  div = document.getElementById(this.field);
	  div.innerHTML = response;
      showDiv(this.field);
      response = null;
    }
    else
    { // No data - error
	  hideDiv(this.field)
    }
  
    hideLoading();
}
  
  this.processTable = function(data)
  {
	this.response = data;
    var response = '<table width="100%">';

    var show = true;

    if (this.response.length > 0)
	{
      for (var i = 0; i < this.response.length; i++)
      { 
	    linedata = this.response[i].split("||");
		if (i == 0)
		{ // First row - contains row information
		  // Field 1 = Title Row status
          // Field 2 = Rows
		  // Field 3 = Start
		  // Field 4 = End
		  if (linedata[0].match('hidden'))
		  {
            show = false;
		  }
		  else if (linedata[1] != 0)
		  { // 0 in first element of this row means we don't
		    // process the row
		  }
		}
		else 
        {
		  if (i == 1)
			response += "<thead>";
          else if (i == 2)
			response += "<tbody>";
			
		  if (show || i > 1)
            response += "<tr>";
			
          for (var j = 0; j < linedata.length; j++)
          {	
            if (i == 1)
		    { // First (real) Row - header
		      if (show)
			  {
		        response += '<th>' + linedata[j] + '</th>';
			  }
		    }
		    else
		    { // Other row - normal
		      response += '<td>' + linedata[j] + '</td>';
		    }
		  }
		  
		  if (show || i > 1)
            response += "</tr>";
		}
		if (i == 1)
		  response += "</thead>";
      }
      response += "</tbody>";
    }
	else
	{
	  response += "</tr>";
	  response += "<td align=\"center\">No Data, please refine your query.</td>";
	  response += "</tr>";
	}
	
    response += "</table>";	
  	div = document.getElementById(this.field);
	div.innerHTML = response;
    showDiv(this.field);
    response = null;
	  
    hideLoading();
  }
}



function showDiv(divName)
{
  if (document.layers)
    document.layers[divName].display = "block";
  else if (document.getElementById(divName))
    document.getElementById(divName).style.display = "block";
}

function hideDiv(divName)
{
  if (document.layers)
    document.layers[divName].display = "none";
  else if (document.getElementById(divName))
    document.getElementById(divName).style.display = "none";
}

function fillForm(field, value)
{
  // Get the field
  fieldEl = document.getElementById(field.substr(5));
  if (fieldEl)
    fieldEl.value = value;

  // Hide the AJAX Selection
  hideDiv(field);
}

function ProcessAjax(value, field, handler, type)
{
  if (type == null)
    type = 'string';
	
  showLoading();

  if (value == '')
  {
    hideDiv(field);
	hideLoading();
  }
  else
  {
    theAjax = new ajax();
	
	theAjax.type = type;
    theAjax.postData = value;
    theAjax.handler = handler;
    theAjax.field = field;
	
    theAjax.response = theAjax.sendReq();
 
    theAjax = null;
  }
}

function showLoading()
{
  hideDiv('loading_box_off');
  showDiv('loading_box');
  // loading = true;
}

function hideLoading()
{
  hideDiv('loading_box');
  showDiv('loading_box_off');
  // loading = false;
}
