/* 
	Library: Gainsborough - web client framework
	Component: AJAX object
	
	Hextra Digital 
	http://www.hextra-digital.com
*/

/*
	ajaxobject basic implementation - used in ajax requests
	can be expanded by using ajaxobject.prototype
*/

function ajaxobject( url, callback, notify )
{
	this.url          = url;
	this.callback     = callback; 
	this.notifyID	  = notify;
	this.notify       = ge( notify );

	this.progress 	  = null;
	this.progressID	  = '';
	this.http_request = null;	
	this.use_post	  = false;
	this.parameters	  = '';
}

	
ajaxobject.prototype.create = function()
{
}
	
ajaxobject.prototype.destroy = function()
{
	delete this.http_request;
	//delete this;
}
	
ajaxobject.prototype.setProgress = function( _id )
{
	this.progressID = _id;
	this.progress = ge( this.progressID );
}
	
ajaxobject.prototype.report = function( _text )
{
	if( this.progress )	re( this.progress, _text );		
}
	
ajaxobject.prototype.makeRequest = function()
{	
	if( !this.url ) return false;
	
	this.report( '<strong><img src="circle_ball.gif" />Conectando...</strong>' );

	this.http_request = null;

	if( window.XMLHttpRequest ) // Mozilla, Safari,...
	{
		this.http_request = new XMLHttpRequest();
		if( this.http_request )
		{
			if( this.http_request.overrideMimeType ) 
			{
				this.http_request.overrideMimeType( 'text/xml' );
			}
		}
	} 
	else if( window.ActiveXObject ) // MSIE 
	{
		try 
		{
			this.http_request = new ActiveXObject( 'Msxml2.XMLHTTP' );
		} 
		catch( e ) 
		{
			try 
			{
				this.http_request = new ActiveXObject( 'Microsoft.XMLHTTP' );
			} 
			catch( e ) {}
		}
	}

	if( !this.http_request ) 
	{
		if( this.progress ) this.report( '<strong>No se ha podido crear una instancia XMLHTTP</strong>' );
		else alert( 'No se ha podido crear una instancia XMLHTTP' );
		return false;
	}

	if( this.callback ) 
	{
		var thisObj = this;
		this.http_request.onreadystatechange = function() { thisObj.genericCallback(); }
	}
	
	if( this.use_post ) // asynchronous HTTP POST
	{
		this.http_request.open( 'POST', this.url, true ); 
		this.http_request.setRequestHeader( 'Content-type', 'application/x-www-form-urlencoded' );
		this.http_request.setRequestHeader( 'Content-length', this.parameters.length );
		this.http_request.setRequestHeader( 'Connection', 'close' );
		this.http_request.send( this.parameters );
	}
	else // asynchronous HTTP GET
	{
		this.http_request.open( 'GET', this.url, true );
		this.http_request.send( null );
	}
}

ajaxobject.prototype.genericCallback = function()
{
	if( this.http_request )
	{
		if( this.http_request.readyState == 4 ) // once is completed
		{
			try
			{
				if( this.http_request.status )
				{
					if( this.http_request.status == 200 || this.http_request.status == 0 ) // OK
					{
						if( this.callback ) 
						{
							try
							{
								this.callback( this );
								this.report( 'Última petición: correcto.' );
							}
							catch( e )
							{
								this.report( '<strong>Excepción en el <i>callback</i>: ' + e.description  + '</strong>' );
							}
						}
					}
					else // 404, 500, etc.
					{
						this.report( '<strong>Estado ' + this.http_request.status + '. No se ha podido completar la operación.<br/><a href="#" onclick="' + this.makeRequest() + '" >Pulse aquí para volver a intentarlo</a> o <a href="/" >aquí para volver al inicio</a>.</strong>' ); 
					}
				}
				else this.report( '<strong>No hay estado.</strong>' );
			}
			catch( e )
			{
				this.report( '<strong>Excepción: ' + e.description + '.</strong>' );			
			}
			this.destroy();
		}
	}
	else{
		//this.report( '<strong>Excepción: ' + e.description + '.</strong>' );			
	}
}