/*************************
    DEPRECATED FILE
**************************/

/********************************************************************************
	- MemHT Portal -
	
	Copyright (C) 2007-2008 by Miltenovik Manojlo
	http://www.memht.com
	
	This program is free software; you can redistribute it and/or modify
	it under the terms of the GNU General Public License as published by
	the Free Software Foundation; either version 2 of the License, or
	(at your opinion) any later version.
	
	This program is distributed in the hope that it will be useful,
	but WITHOUT ANY WARRANTY; without even the implied warranty of
	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
	GNU General Public License for more details.
	
	You should have received a copy of the GNU General Public License along
	with this program; if not, see <http://www.gnu.org/licenses/> (GPLv2)
	or write to the Free Software Foundation, Inc., 51 Franklin Street,
	Fifth Floor, Boston, MA02110-1301, USA.
		
********************************************************************************/

function AjaxObj() {
	this.obj = null;
	this.url = null;
	this.asynchronous = true;
	this.method = 'GET';
	this.format = 'text'; //text,xml,object
	this.responseElement = null;
	this.loader = false;
	this.loaderContent = 'Loading...';
	this.data = null;
	this.autoreload = false;
	this.autoreloadtime = 5; //sec
	
	//Initialize XMLHttpRequest Object
	this.initialize = function() {
		try {
			//Embedded
			this.obj = new XMLHttpRequest();
		} catch(e) {
			try {
				//IE 6
				this.obj = new ActiveXObject('Msxml2.XMLHTTP');
			} catch (e) {
				try {
					//IE 5.x
					this.obj = new ActiveXObject('Microsoft.XMLHTTP');
				} catch (e) {
					//No Ajax? No Party
					return;
				}
			}
		}
		return true;
	}
	
	//Retrieve data
	this.opGet = function(url,respelem) {
		var glue = (url.indexOf("?")>0) ? '&' : '?' ;
		this.url = url+glue+new Date().getTime(); //Anti-Cache for IE
		this.responseElement = respelem;
		if (this.loader && this.responseElement) { window.document.getElementById(this.responseElement).innerHTML = this.loaderContent; }
		this.opRequest();
	}
	
	//Send data
	this.opPost = function(url,respelem,data) {
		this.url = url;
		this.data = data;
		this.method = 'POST';
		this.responseElement = respelem;
		if (!data) { alert('No input data defined'); return; }
		if (this.loader && this.responseElement) { window.document.getElementById(this.responseElement).innerHTML = this.loaderContent; }
		this.opRequest();
	}
	
	//Request object
	this.opRequest = function() {
		var tobj = null;
		var self = null;
		var response = null;
		if (!this.initialize()) { alert('XMLHttpRequestObject cannot be created'); return; }
		tobj = this.obj;
		tobj.open(this.method,this.url,this.asynchronous);
		if (this.method == 'POST') { tobj.setRequestHeader('Content-Type','application/x-www-form-urlencoded');	}
		self = this;
		tobj.onreadystatechange = function() {
			self.readyState = tobj.readyState;
			
			//Ready
			if (tobj.readyState==4) {
				self.status = tobj.status;
				self.statusText = tobj.statusText;
				self.responseText = tobj.responseText;
				self.responseXML = tobj.responseXML;
				
				switch (self.format) {
					case 'text':
						response = self.responseText;
						break;
					case 'xml':
						response = self.responseXML;
						break;
					case 'obj':
						response = tobj;
						break;
				}
				
				if (self.status==200) {
					if (self.responseElement) {
						window.document.getElementById(self.responseElement).innerHTML = response;
					} else {
						alert('No response element has been defined');
						return;
					}
				}
			}
		}
		tobj.send(this.data);
	}
}