/**---------------------------------------------------------------------------------------------------
 *
 * File:		advert.js
 * Date:		25.07.2007
 * Description:	Dynamic Adverts
 *
 * Usage:
 *
 *--------------------------------------------------------------------------------------------------*/

var	AJAX	= 1,
	DHTML	= 2;
	

/**---------------------------------------------------------------------------------------------------
 *
 * Network wrapper
 *
 */
function send( name, type, recv, obj, url, data )
{
	net = new Network( name, type, recv, obj );
	
	net.send( url, data );
}


/**---------------------------------------------------------------------------------------------------
 *
 *
 */
function Network( name, type, recv, obj )
{
	this.name		= name || "unnamed";
	this.obj		= obj;
	this.xmlHttpReq	= window.XMLHttpRequest ? new XMLHttpRequest() : new ActiveXObject( "Microsoft.XMLHTTP" );
	
	
	/**
	 * Sets the type of network connection to use: AJAX or DHTML
	 *
	 * @param integer
	 */
	this.setType = function( type )
	{
		this.type = type || AJAX;
		
		if ( this.type == DHTML )
			this.send = this.sendDHTML;
			
		else
			this.send = this.sendAJAX;
	}
	
	/**
	 * Sends a string of data to a URL
	 *
	 * @param string url	URL to send data to
	 * @param string data	data to send
	 */
	this.sendAJAX = function( url, data )
	{
		var	self = this;
		
		try
		{
			self.xmlHttpReq.open( "POST", url, true );
			self.xmlHttpReq.setRequestHeader(	"Content-Type",
												"application/x-www-form-urlencoded" );
			self.xmlHttpReq.onreadystatechange = function()
			{
				if ( self.xmlHttpReq.readyState == 4 )
					self.recv( self.xmlHttpReq.responseText, self.obj );
			}
	
			this.xmlHttpReq.send( data );
		}
		catch( e )
		{
			alert( "Cannot access remote domain." );
		}
	}
	
	/**
	 * Requests data from a URL
	 *
	 * @param string	id		ID of the div
	 * @param string	name	name of the object
	 * @param string	url		URL of the script to call
	 */
	this.sendDHTML = function( url, data )
	{
		var	obj = document.createElement( "IFRAME" );
		
		obj.id					= this.name + "_dhtmlframe";
		obj.src					= url + "&targetframe=" + this.name + "&framename=" + obj.id;
		obj.style.visibility	= "hidden";
		
		if ( document.getElementById( obj.id ) )
			document.getElementById( this.name ).removeChild( document.getElementById( obj.id ) );
		
		document.getElementById( this.name ).appendChild( obj );
	}
	
	/**
	 * Function stub called when data has been received from the server
	 * Over-ride this method with your own method to handle incoming data
	 *
	 * @param string data	Incoming data
	 */
	this.send = function() {}
	this.recv = recv;
	
	
	// sets the type of the connection
	this.setType( type );
}


/*--------------------------------------------------------------------------------------------------*/
