//----------------------------------------------
// AJAX - Asyncronous Javascript And XML - Class
// (c) Red Enzian - Creative IT, 2006
// Script written by: Bernhard Rosenberger
//----------------------------------------------

//---
// Initialize the object
//---

function ajax()
{
	this.c 								= false;
	this.xmlerror 						= '';
	this.request_function 				= function() {};
	this.loading_div 					= null;
	this.ajax_usable 					= true;
	this.use_loading_div 				= false;
	this.loading_div_currently_showed 	= false;
	this.charset						= '';
}

//---
// Initialize
//---

ajax.prototype.initialize = function()
{
	if ( window.XMLHttpRequest )
	{
  		this.xmlhttprequesthandler = new XMLHttpRequest();
	} else if ( window.ActiveXObject ) {
  		try {
    		this.xmlhttprequesthandler = new ActiveXObject("Msxml2.XMLHTTP");
    	} catch (e) {
    		try {
    			this.xmlhttprequesthandler = new ActiveXObject("Microsoft.XMLHTTP");
    		} catch (e) {
    		}
    	}
	} else {
  		alert( 'Ihr Browser unterstützt AJAX nicht!' );
	}
}

//---
// AJAX Readystate not ready
//---

ajax.prototype.readystate_not_ready = function()
{
	return ( this.xmlhttprequesthandler.readyState && ( this.xmlhttprequesthandler.readyState < 4) );
}

//---
// AJAX Readystate ready and ok
//---

ajax.prototype.readystate_ready_and_ok = function()
{
	return ( this.xmlhttprequesthandler.readyState == 4 && this.xmlhttprequesthandler.status == 200 ) ? true : false;
}

//---
// Process a request
//---

ajax.prototype.process_request = function( uri, type, post )
{
	type = type == "POST" ? "POST" : "GET";

	// Check if we have an active AJAX Connection
	if ( !this.xmlhttprequesthandler )
	{
		this.initialize();
	}

	// Check the connection state
	if ( ! this.readystate_not_ready() )
	{
		// open a connection
		this.xmlhttprequesthandler.open(type, uri, true);

		if ( type == 'GET' )
		{
			this.xmlhttprequesthandler.send( null );
		} else {
			if ( typeof( this.xmlhttprequesthandler.setRequestHeader ) != "undefined" )
			{
				this.xmlhttprequesthandler.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded; charset=UTF-8');
			}

			this.xmlhttprequesthandler.send( post );
		}

		if ( this.xmlhttprequesthandler.readyState == 4 && this.xmlhttprequesthandler.status == 200 )
		{
			return true;
		}
	}

	return false;
}

//---
// Event Handler
//---

ajax.prototype.onreadystatechange = function( event )
{
	if ( !this.xmlhttprequesthandler )
	{
		this.initialize();
	}

	if ( typeof( event) == 'function' )
	{
		this.xmlhttprequesthandler.onreadystatechange = event;
	}
}

ajax.prototype.initialize();
