// JavaScript Document
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';
	this.mimeType = null;
	//this.enctype =null;
	
	this.init = function(){
    if(!this.req)
	{
	try{
		//Try to create object for Firefox, Safarai, IEF7, etc.
		this.req = new XMLHttpRequest();
	   }// end try  
	catch(e){
		try{
			//Try to create object for later versions of IE.
			this.req = new ActiveXObject('MSXML2.XMLHTTP');
		   }  // end try
		   catch(e){
			   try{
				   //Try to create object for early version of IE.
				   this.req = new ActiveXObject('Microsoft.XMLHTTP');
			      } /// end try
			  catch(e){
				  		//Could not create an XMLHttpRequest object.
				  		return false;
			          }  // end catch
		           }  // end catch
			}  // end catch
	} // end if
	return this.req;
} // end this.init

	this.doReq = function(){
		if(!this.init()){
			alert('Could not create XMLHttpRequest object.');
			return;
		}  // end if
		this.req.open(this.method, this.url, this.async);
		
		if(this.method == "POST")
		{
			this.req.setRequestHeader('Content-Type','application/x-www-form-urlencoded; charset=UTF-8');
			
		} // end if
		
		if(this.mimeType)
		{
			try{
				req.overrideMimeType(this.mimeType);
				
			}  // end try
			catch(e){
				//couldn't override Mime type
			}  // end catch
			
		} // end if
		
		var self = this; //Fix loss-of-scope in inner function
		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.resonseXML;
						break;
					case 'object': 
						resp = req;
						break;
				}  // end switch
				if(self.req.status >=200 && self.req.status <= 299)
				{					
					self.handleResp(resp);
					//alert(resp);
				}  // end if
				else
				{
					self.handleErr(resp);
				} // end else
				
			}  // end if
		} // end function
		this.req.send(this.postData);
	};  // end this.doReq

	this.setMimeType = function(mimType){
		this.mimeType = mimType;
	}  // end this.setMimeType
	
	this.handleErr = function(){
		var errorWin;
		try{
			errorWin = window.open('','errorWin');
			errorWin.document.body.innerHTML = this.responseText;
		}  // end try
		catch(e){
			alert('An error occured, but the error message cannnot be display. This is probably because of your browser\'s'
				  + 'pop-up blocker. \n' + 'Please allow pop-ups from this website if you want to see the full error message.\n\n'
				  + 'Status Code: ' + this.req.status + '\n' +  'Status Description: ' + this.req.statusText);
		}  // end catch
		
	}  // end function
	
	this.abort = function(){
		if(this.req){
			this.req.onreadystatechange = function(){};
			this.req.abort();
			this.req = null;
		}  // end if
	} // end function
	
	this.doGet = function(url, hand, format){
		this.url = url;
		this.handleResp = hand;
		this.responseFormat = format || 'text';
		this.doReq();
	} // end function
	
	this.doPost = function(url, postData, hand, format){
		this.url = url;
		this.handleResp = hand;
		this.responseFormat = format || 'text';
		this.method = 'POST';
		this.postData = postData;
		//this.enctype = 'multipart/form-data';
		this.doReq();
	}; // end method
} // end Ajax

