/**
 *********************************************************************************************************************************
 * Ajax
 *********************************************************************************************************************************
 * VERSION:       0.1 Beta
 * COMPATIBILITY: Firefox 2.0, MSIE 6, MSIE 7, Safari 3.0
 ---------------------------------------------------------------------------------------------------------------------------------
 * DESCRIPTION
 * Ready files from server and put in specific html container
 ---------------------------------------------------------------------------------------------------------------------------------
 * DEPENDENCES
 * Run in HTTP Server
 ---------------------------------------------------------------------------------------------------------------------------------
 * AUTHOR:        Maurício J. Fávaro (mfavaro@cit.com.br)
 * DATE:          2008/03/04
 * LASTS UPDATES
 * 2008/03/04: Maurício J. Fávaro (mfavaro@cit.com.br)
 *********************************************************************************************************************************
 */
function Ajax()
{
	// Attributes
	this.http = null;
	this.response = null;
	this.target = null;
	this.cacheActive = false;
	this.url = null;
	
	// Public Methods
	this.setTarget = Ajax_setTarget;
	this.load = Ajax_load;
	this.write = Ajax_write;
	this.onStart = Ajax_onStart;
	this.onLoad = Ajax_onLoad;
	this.onError = Ajax_onError;
	
	
	// Private Methods
	this.getStatus = Ajax_getStatus;
	this.getXMLHttpRequest = Ajax_getXMLHttpRequest;	
	
	// Constructor
	this.getXMLHttpRequest();
}
function Ajax_getXMLHttpRequest()
{
	if (window.XMLHttpRequest)
	{
		this.http = new XMLHttpRequest();
	}
	else if (window.ActiveXObject)
	{
		this.http = new ActiveXObject("Microsoft.XMLHTTP");
	}
}
function Ajax_setTarget(target)
{
	this.target = target;
}
function Ajax_load(url)
{
	this.url = url;
	if(this.cacheActive)
	{
		this.http.open("GET", urlNoCache, true);
	}
	else
	{
		this.http.open("GET", url+"?"+Math.ceil(Math.random()*100000), true);
	}
	this.http.onreadystatechange = this.onLoad;
	this.http.send(null);
	this.onStart();
}
function Ajax_getStatus()
{
	if (this.http.readyState == 4 && this.http.status == 200)
	{
		this.response = this.http.responseText;
		//alert(this.http.statusText);
		return true;
	}
	else if (this.http.readyState == 4)
	{
		//alert(this.http.statusText);
		this.onError();
		return false;
	}
	else
	{
		return false;
	}
}
function Ajax_write(value)
{
	if(value != undefined)
	{
		document.getElementById(this.target).innerHTML = value;
	}
	else
	{
		document.getElementById(this.target).innerHTML = this.response;
	}
}
/*
 * Overload Methods
 * - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
 */
function Ajax_onStart()
{
	ajax.write("<img src='images/loading.gif' class='loading' />");
}

function Ajax_onError()
{
	ajax.write("<span class='page-not-found'>Page not found.</span>");
}
function Ajax_onLoad()
{
	// instanceName.getStatus()
	if(ajax.getStatus())
	{
		// instanceName.write();
		ajax.write();
	}
}

/*
 * Example 01
 * - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
var ajax = new Ajax();
ajax.onLoad = Ajax_onLoad;
ajax.setTarget("div01");
ajax.load("pagina1.html");
 */
/*
 * Example 02
 * - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
function Ajax_onLoad()
{
	// instanceName.getStatus()
	if(ajax.getStatus())
	{
		loading.style.visibility = "hidden";
		// instanceName.write();
		ajax.write();
	}
}
function Ajax_onStart()
{
	loading.style.visibility = "visible";
}
var ajax = new Ajax();
ajax.onLoad = Ajax_onLoad;
ajax.onStart = Ajax_onStart;
ajax.setTarget("div01");
<p><a href="javascript:ajax.load('pagina1.html');">p&aacute;gina 01</a></p>
<p><a href="javascript:ajax.load('pagina2.html');">p&aacute;gina 02</a> </p>
 */
