/**
	Class Server
	
	Handles a AJAX comunication with the server. Interacts with the 
PHP Application class, using the "resource" protocol
*/

function Server(url) {
	// methods
	this.show = this.load = serverLoad;
	this.hide = serverHide;
	this.unhide = serverUnhide;
	this.process = serverProcess;
	
	this.onProcess = null;

	// public members
	this.loadInto = null;
	this.url = url;
	this.className = 'Server';
	this.params = '';
	this.onLoad = null;
	this.onBeforeLoad = null;
	
	this.data = null;
	
	this.post = null;
	
	this.method = 'GET';
	
	// private members
	this.__loadInto = null;
	this.__draw = serverDraw;
}

function serverLoad(extraParams, into, syncron) {
	if ( extraParams == null ) extraParams = '';
	this.__loadInto = (into != null) ? into : this.loadInto;
	
	/*
	if (! this.__loadInto ) {
		return false;
	}
	*/
	
	var async = syncron ? false : true;
	
	var url = 
			this.url
			+ this.params
			+ extraParams;
			
	//alert(url);
	
	// create a copy of this
	var _this = this;
	
	// setting the xmlDoc
	var xmlDoc;
	if ( typeof window.ActiveXObject != 'undefined' ) {
		xmlDoc = new ActiveXObject("Microsoft.XMLHTTP");
		xmlDoc.onreadystatechange = function(){_this.process()};
	}
	else {
		xmlDoc = new XMLHttpRequest();
		xmlDoc.onload = function(){_this.process()};
	}
	
	this.xmlDoc = xmlDoc;
	
	if ( this.onBeforeLoad ) {
		if ( this.onBeforeLoad() == false ) {
			return false;
		}
	}
	
	this.xmlDoc.open(this.method, url, async);
	
	if ( this.method == 'POST' ) {
		this.xmlDoc.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
		this.xmlDoc.setRequestHeader("Content-length", this.post.length);
		this.xmlDoc.setRequestHeader("Connection", "close");
	}
	
	this.xmlDoc.send(this.post);
	
	//alert(url + ' ' + this.method + ': ' + this.post);
}

function serverHide() {
	if (! this.loadInto ) {
		return false;
	}
	
	this.loadInto.style.display = 'none';
}

function serverUnhide() {
	if (! this.loadInto ) {
		return false;
	}
	
	this.loadInto.style.display = '';
}

function serverProcess() {
	if ( this.xmlDoc.readyState != 4 ) return;
	
	if ( this.onLoad ) {
		this.onLoad();
	}
	else {
		this.__draw();
		
	}
}

function serverDraw() {
	if (! this.__loadInto ) {
		return false;
	}
	
	this.__loadInto.style.display = '';
	this.__loadInto.innerHTML = this.xmlDoc.responseText;
}
