/**---------------------------------------------------------------------------------------------------
 *
 * File:		network.js
 * Author:		Peter Quinn
 * Date:		25.07.2007
 * Description:	AJAX Network Class
 *
 * Usage:		send( "http://www.blabla.com/script.php", "", recv );
 *				net = new Network();
 *				net.send( "http://www.blabla.com/script.php", "name=bla&age=20&desc=sdfasdfasdf" );
 *				net.recv( data );
 *
 *--------------------------------------------------------------------------------------------------*/



/**---------------------------------------------------------------------------------------------------
 *
 * Network class wrapper
 *
 * @param string url	URL of the script to call
 * @param string data	Data to send to the script
 * @param function recv	Function to call when data is received
 */
function send( url, data, recv )
{
	net = new Network();
	
	net.recv = recv;
	net.send( url, data );
}


/**
 * Network Class
 *
 */
function Network()
{
	this.request = window.XMLHttpRequest ? new XMLHttpRequest() : new ActiveXObject( "Microsoft.XMLHTTP" );
	
	
	/**
	 * Sends a string of data to a URL
	 *
	 * @param string url	URL to send data to
	 * @param string data	data to send
	 */
	this.send = function( url, data )
	{
		var	self = this;
		
		try
		{
			self.request.open( "POST", url, true );
			self.request.setRequestHeader(	"Content-Type",
											"application/x-www-form-urlencoded" );
			self.request.onreadystatechange = function()
			{
				if ( self.request.readyState == 4 )
					self.recv( self.request.responseText );
			}
	
			this.request.send( data );
		}
		catch( e )
		{
			alert( "Cannot access remote domain." );
		}
	}
	
	/**
	 * 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.recv = function( data )
	{
		alert( data );
	}
}


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