/*----------------------------------------------------------------
AJAX CLASS OBJECT 
File name : class-ajax.js
Last Update : Jerry Peter - 23 Agustus 2006

Base on book Mathew Errnisse: Build your own AJAX web application

Contoh pengunaan class:
<script language="javascript" src="class-ajax.js"></script>
<div id="content"></div>
<script language="javascript">
	var hand = function(str) {
		document.getElementById("content").innerHTML = str;
	}
	
	var oAJAX = new ajax();		
	oAJAX.doGet("http://www.servername.com/data.html",hand);
</script>
----------------------------------------------------------------*/
function ajax()
{
	this.req = null;
	this.url = null;
	this.method = 'GET';
	this.async = true;
	this.status = null;
	this.statusText = '';
	this.postData = null;
	this.readyState = null;
	this.responseText = null;
	this.responseXML = null;	
	this.handleResp = null;
	this.responseFormat = 'text', // 'text', 'xml', or 'object'
	
	this.init = function() {
		if (!this.req) {
			try {
				// Firefox, Safari, IE7, etc.
				this.req = new XMLHttpRequest();
				}
				catch (e) {
					try {
					// IE Versi lama.
					this.req = new ActiveXObject('MSXML2.XMLHTTP');
					}
					catch (e) {
						try {
						// IE Versi terbaru.
						this.req = new ActiveXObject('Microsoft.XMLHTTP');
						}
						catch (e) {
							// Could not create an XMLHttpRequest object.
							return false;
						}
					}
				}
		}
		return this.req;
	};		

	this.doGet = function(url, hand, format) {
		var self = this;
		self.url = url;
		self.handleResp = hand;
		self.responseFormat = format || 'text';
		self.doReq();
	};		


	this.doReq = function() {
		if (!this.init()) {
			alert('Could not create XMLHttpRequest object.');
			return;
		}		

		this.req.open(this.method, this.url, this.async);
		var self = this;						
		this.req.onreadystatechange = function() {
			var resp = null;			
			if (self.req.readyState == 4) {				
				switch (self.responseFormat) {
					case 'text':
						resp = self.req.responseText;						
						break;
					case 'xml':		
						resp = self.req.responseXML;
						break;
					case 'object':
						resp = req;
						break;
				}

				if (self.req.status >= 200 && self.req.status <= 299) {
					self.handleResp(resp);
				}else{
					self.handleErr(resp);
				}			

			} //END if (self.req.readyState == 4)			
		};	// END 	this.req.onreadystatechange = function()	
		
		this.req.send(this.postData);		
	};	// END this.doReq = function()
	
	
	this.handleErr = function() {
		var errorWin;
		try {
			errorWin = window.open('', 'errorWin');
			errorWin.document.body.innerHTML = this.responseText;
		}
		catch (e) {
			alert('ERROR, kemungkinan karena browser mengaktifkan pop-up blocker \n'
			+ 'Non aktifkan pop-up blocker jika ingin melihat error message yg terjadi '
			+ '\n'
			+ 'Status Code: ' + this.req.status + '\n'
			+ 'Status Description: ' + this.req.statusText);
		}
	};	
	
	this.abort = function() {
		if (this.req) {
			this.req.onreadystatechange = function() { };
			this.req.abort();
			this.req = null;
		}
	};	
}

