var ajaxObj = new Array();
var isIE = false;

/*
* @Class Ajax
* @version 2.0 
* @author vasupat chantakeaw
* @email thaiajax@gmail.com
*/

function ajax(){
	
	this.xmlhttp;
	this.formElement;
	this.requestFile;
	this.URLString;
	this.innerDivID;
	this.method 		= 'GET';
	this.responseType 	= 'Text'; /*Text , XML*/
	this.loadingType 	= 0;
	this.async 			= true;
	this.onCompletion 	= function() { };
	this.onProcess 		= function() { };
	this.loading 		= function(v){
		var v;
		if(this.loadingType>0){
			if(this.loadingType==1){
				if(!g("loading")) {this.createLoaddingDiv();}
			}
			if(v==1){
				if(this.loadingType==1){ g("loading").style.visibility = "visible";}
			}else{
				if(this.loadingType==1){ g("loading").style.visibility = "hidden";}
			}
		}
	}
	
	this.ConnXmlHttp = function(){
		try{
			xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
		}catch(e){
			try{
				isIE = true;
				xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
			}catch(e){
				xmlhttp = false;
			}
		}
	
		if(!xmlhttp && document.createElement){
			xmlhttp = new XMLHttpRequest();
		}
		
		return xmlhttp;
	}
	
	this.getRequestBody = function(myForm) {
		var aParams = new Array();  
			for (var i=0 ; i < myForm.elements.length; i++) {
				
				var formElement = myForm.elements[i];
				if(formElement.type=='checkbox' && !formElement.checked){ continue;} 
            	var sParam = encodeURIComponent(myForm.elements[i].name);
               	sParam += "=";
               	sParam += encodeURIComponent(myForm.elements[i].value);
               	aParams.push(sParam);
           	}     
           	return aParams.join("&");        
	}
	
	this.loadXMLDoc = function() {
	
		var self = this;
		this.xmlhttp = this.ConnXmlHttp();

		
		if (this.method == "GET") {
			this.xmlhttp.open(this.method, this.requestFile, this.async);
		}else{
			this.URLString = this.getRequestBody(this.formElement);
			this.xmlhttp.open(this.method,this.requestFile, this.async);
		}
		if (this.method == "POST"){
  			try {
				this.xmlhttp.setRequestHeader('Content-Type','application/x-www-form-urlencoded')  
			} catch (e) {}
		}
		this.xmlhttp.onreadystatechange = function(){
			if(self.xmlhttp.readyState==4){
				if(self.loadingType==1){ self.loading(0);}
				if (self.xmlhttp.status == 200) {
					if(self.responseType == "Text"){
						self.response = self.xmlhttp.responseText;
					}else{
						self.response = self.xmlhttp.responseXML;
					}	
					
					self.onCompletion();
				}
			}else{
				if(self.loadingType==1){ self.response = "Loading...";}
				if(self.loadingType==2){ 
					//self.response = "Loading...";
					self.response = '<img src="images/ajax-loader.gif" />';
					self.onCompletion();
				}
				if(self.loadingType==3){ 
					//self.response = "Loading...";
					self.response = '<img src="images/ajax-loader2.gif" />';
					self.onCompletion();
				}
			}
		}
		this.xmlhttp.send(this.URLString);
	}
	
	/*
	* Show Loading
	*/
	
	this.createLoaddingDiv = function(){
	
		var connLoading = document.createElement("DIV")
			connLoading.id = "loading";
			connLoading.style.position="absolute";
			connLoading.style.left="0px";
			connLoading.style.top="0px";
			connLoading.style.width="185px";
			connLoading.style.height="15px";
			connLoading.style.zIndex=99999;
			connLoading.style.backgroundColor="#FF0000";
			connLoading.style.visibility = "visible";
			connLoading.style.font = '11px Lucida Sans Unicode';
			connLoading.style.filter = 'alpha(opacity=60)';
			connLoading.innerHTML = "<strong><font color=\"#FFFFFF\">&nbsp;working on your request...</font></strong>";
		document.body.appendChild(connLoading);
		
	}
}



