/**
 *	Created by Daniel Jakobsson <fake_healer@hotmail.com> 2005-07-13
 *	
 *	
 *	Represents the server.
 *	
 *	Deliver packets of data from the server without needing to reload the page.
 *	The data can be of any javascript type. Of course the clientside function
 *	must know what it receives.
 *	
 *	Usage:
 *	
 *	DO NOT CREATE ANY NEW INSTANCES. Use Server.instance. It does not make sence
 *	to have more than one server anyway. Or rather; if more than one is needed,
 *	use the Server.instance.setRequestUrl(String url) before each call.
 *	The Server.instance is called from timers.
 *	
 *	
 *	The Server needs an address (page) to send its request.
 *	Ex;
 *	Server.instance.setRequestUrl('jsServerPoll.asp');
 *	
 *	The serverside page must:
 *	1. Be of content type: text/javascript.
 *	2. Disable any caching (to keep the data fresh).
 *	3. Be able to receive the following parameters:
 *		- module
 *		- filter
 *		- page
 *		- packetId
 *	4. On clientside call: Server.instance.packetLoaded(packetId, serverData);
 *	
 *	
 *	
 *	To request data from the server/run a serverside procedure:
 *	Server.instance.getDataPacket([module[, onload[, args[, filter[, page]]]]])
 *	
 *	module: A module to handle the request on the server. This is only necessary if
 *		several modules exists (recommended). In other words a way to organize
 *		the modules on the serverside.
 *	onload: A clientside function/method to call when the packet has been loaded. 
 *		This function handles the requested data. Necessary if any data is returned.
 *	args: An array of arguments to the clientside onload function/method (previous 
 *		parameter).
 *	filter: Filter for the serverside module.
 *	page: Current page, if the requested data requires pagination.
 *	
 *	
 *	When the packet is loaded the onload function is called (if defined).
 *	onloadFunction(data, args);
 *	data: The data from the server.
 *	args: The arguments array that was passed as third parameter in the getDataPacket(..).
 *	
 *	
 */
function Server() {

	this.packets={};
	this.packetCounter=0;
	this.requestUrl = '';

	/**
	 *	
	 *	
	 */
	this.getNextPacketId = function () {
		return "id"+(this.packetCounter++);
	}


	/**
	 *	
	 *	
	 */
	this.setRequestUrl = function (url) {
		this.requestUrl=url;
	}


	/**
	 *	
	 *	
	 */
	this.getDataPacket = function (module, onload, args, filter, page) {

		if (this.requestUrl == '') {
			alert('Set the request url: Server.instance.setRequestUrl("serverpage.asp");');
		}

		var packetId=this.getNextPacketId();
		this.packets[packetId]=new ServerPacket(packetId);
		this.packets[packetId].onload=onload;
		this.packets[packetId].args=args;
		var url = this.requestUrl+"?module="+module+"&filter="+filter+"&page="+page+"&packetId="+packetId;
		
		this.packets[packetId].createScript(url);
		return url;
	}

	/**
	 *	Called by the loaded serverside page.
	 *	
	 */
	this.packetLoaded = function (packetId,data) {

		this.packets[packetId].onload(data,this.packets[packetId].args);
		this.packets[packetId].timer=setTimeout('Server.instance.packets.'+packetId+'.destroyScript();', 50);
	}


}

Server.instance=new Server();




/**
 *	
 *	Represents each packet.
 */
function ServerPacket(id) {
	this.id=id;
	this.script=null;
	this.url="";
	this.timer=0;
	this.patience=15 * 1000;
	this.patienceTimer=0;
	this.creationTimeStamp=new Date();
	this.newTryTimer=0;
	this.enableDEBUG = false;

	this.args=[];
	this.onload=function (data) {
		alert("The onload function is not set for packet: "+id);
	};



	this.createScript=function(url){

		if (typeof url != "undefined") {

			this.url=url;
		} else if (typeof this.url != "undefined") {
			url = this.url;
		}
		
		if (this.enableDEBUG){
			document.getElementById("divDebug").innerHTML="ServerPacket:"+this.id+":createScript<br>";
		}

		this.script=document.createElement("SCRIPT");
		document.getElementsByTagName("HEAD")[0].appendChild(this.script);
		this.creationTimeStamp=new Date();
		this.patienceTimer=setTimeout('Server.instance.packets.'+this.id+'.endPatience();', this.patience);
		this.script.src=url;
	}

	this.destroyScript=function(){
		this.killTimer();
		document.getElementsByTagName("HEAD")[0].removeChild(this.script);
	}


	this.endPatience=function () {

		//alert("ServerPacket:"+this.id+":endPatience\nthis.url: "+this.url);
		if (this.enableDEBUG){
			document.getElementById("divDebug").innerHTML="ServerPacket:"+this.id+":endPatience<br>";
		}
		

		this.destroyScript();
		this.newTryTimer=setTimeout('Server.instance.packets.'+this.id+'.createScript();', 100);
	}


	this.killTimer=function(){
		if (this.timer) {
			clearTimeout(this.timer);
			this.timer=0;
		}
		if (this.patienceTimer) {
			clearTimeout(this.patienceTimer);
			this.patienceTimer=0;
		}
	}


}
