
/*
	Title: 				Javascript AJAX Class
	Author: 			Godofredo Timajo
	Location: 			Philippines
	Created: 			March 28, 2007
	Description: 		Class for using Asynchronuos Javascript And XML for a webpage.
	
	Updates:			April 24, 2007 	=> Added xml output parsing.
						May 1, 2007		=> Added Loading feature.
	
*/

function AJAX(){
	var obj = new Object();
	
	obj.xmlhttp = null;
	obj.actTrue = "";
	obj.actFalse = "";
	obj.destID = "";
	obj.xmlParser = null;
	
	obj.withLoadingImage = false;
	obj.loadingImage = "";
	obj.defaultImage = "";
	obj.imageID = "";
	
	
	this.destID = obj.destID;
	
	obj.ini = function (type){
		if((type=='print') && (!isset(obj.destID))){
			alert("AJAX::ini\nNo Destination elementID specified.");
			return false;
		}
		else if((type=='xml') && (!isset(obj.xmlParser))){
			alert("AJAX::ini\nNo XML Parser specified.");
			return false;
		}
		else if(type==''){
			if (!isset(obj.actTrue)){
				alert("AJAX::ini\nNo True Action specified.");
				return false;
			}
			if (!isset(obj.actFalse)){
				alert("AJAX::ini\nNo False Action specified.");
				return false;
			}
		}	
		return true;
	}
	
	obj.start = function (type){
		if(obj.ini(type)){
			if(obj.xmlhttp != null){
				obj.xmlhttp.abort();
				obj.xmlhttp = null;
			}
			try{
				obj.xmlhttp = new XMLHttpRequest();
			} catch (err) {
				try {
					obj.xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
				} catch (err) {
					try {
						obj.xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
					} catch (err) {
						alert ("Your browser does not support XML-HTTP Request");
					}
				}
			}
		}
	}
	
	obj.process = function (url, method, data){
		if( (url != undefined) || (url != "") ){
			if((method=="") || (method==undefined)) method = "GET";
			if((data=="") || (data==undefined)) data = null;
			
			obj.xmlhttp.open(method, url, true);
			if(method=="POST")
	 			obj.xmlhttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
			obj.xmlhttp.send(data);
		} else {
			alert("Error: AJAX::process\n\tNo URL specified for processing.");
			return;
		}
	}
	
	obj.doXML = function(){
		switch(obj.xmlhttp.readyState){
			case 1: //set-up done
					if(obj.withLoadingImage)
						doc(obj.imageID).src = obj.loadingImage;
					break;
			case 2: //sending
			case 3: //processing
					break;
			case 4: //completed
					obj.xmlParser(obj.xmlhttp.responseXML);
					obj.xmlhttp = null;
					if(obj.withLoadingImage)
						doc(obj.imageID).src = obj.defaultImage;
					break;
		}
	}
	
	
	obj.printOut = function (){
		switch(obj.xmlhttp.readyState){
			case 1: //set-up done
					if(obj.withLoadingImage)
						doc(obj.imageID).src = obj.loadingImage;
					break;
			case 2: //sending
			case 3: //processing
					break;
			case 4: //completed
					//alert(obj.xmlhttp.responseText);
					document.getElementById(obj.destID).innerHTML = obj.xmlhttp.responseText;
					obj.xmlhttp = null;
					if(obj.withLoadingImage)
						doc(obj.imageID).src = obj.defaultImage;
					break;
		}
	}
	
	obj.doAction = function (){
		switch(obj.xmlhttp.readyState){
			case 1: //set-up done
					if(obj.withLoadingImage)
						doc(obj.imageID).src = obj.loadingImage;
					break;
			case 2: //sending
			case 3: //processing
					break;
			case 4: //completed
					if(isTrue(obj.xmlhttp.responseText))
						eval(obj.actTrue);
					else if(isFalse(obj.xmlhttp.responseText))
						eval(obj.actFalse);
					else
						alert("Error on AJAX::doAction function:\n" + obj.xmlhttp.responseText);
					obj.xmlhttp = null;
					if(obj.withLoadingImage)
						doc(obj.imageID).src = obj.defaultImage;
					break;
					
		}
	}
	
	this.setLoadingImage = function (imageID, defaultImage, processImage){
		if(isset(imageID) && isset(defaultImage) && isset(processImage)){
			obj.imageID = imageID;
			obj.defaultImage = defaultImage;
			obj.loadingImage = processImage;
			obj.withLoadingImage = true;
		}
	}
	
	this.setDestination = function (elementID){
		if(isset(elementID))
			obj.destID = elementID;
	}
	
	this.setXMLParser = function (func){
		if(isset(func))
			obj.xmlParser = func;
	}
	
	this.setActions = function (actionTrue, actionFalse){
		if(isset(actionTrue) && isset(actionFalse)){
			obj.actTrue = actionTrue;
			obj.actFalse = actionFalse;
		}
	}
	
	this.sendByGET = function (url, parameters, type){
		var params = "";
		if(isArray(parameters)){
			for(i in parameters){
				params += (params != "")? "&" : "";
				params += parameters[i];
			}
		} else
			params = parameters;

		var typeOBJ = new Object();
		typeOBJ.type = type;
		obj.start(typeOBJ.type);
		obj.xmlhttp.onreadystatechange = function () {
			if(typeOBJ.type == "print")
				obj.printOut();
			else if(typeOBJ.type == "xml")
				obj.doXML();
			else
				obj.doAction();
		}
		var uri = (url.indexOf('?')==-1)?url+'?'+params:(params=='')?url:url+'&'+params;
		obj.process(uri, 'GET');
	}
	
	this.sendByPOST = function (url, parameters, type){
		var params = "";
		if(isArray(parameters)){
			for(i in parameters){
				params += "&" + parameters[i];
			}
		} else if(parameters!=''){
			params = parameters;
		}
		var typeOBJ = new Object();
		typeOBJ.type = type;
		obj.start(type);
		obj.xmlhttp.onreadystatechange = function () {
			if(typeOBJ.type == "print")
				obj.printOut();
			else if(typeOBJ.type == "xml")
				obj.doXML();
			else
				obj.doAction();
		}
		obj.process(url, 'POST', params);
	}
	
	
}
