/**
 *	ajax_v2.js
 *	author : Andrei McMillan
 *	last revised: 07-15-2007
 */
/*
	To use this file:
	var url = "file.php";
	var query = "name=john" or null;
	var method = "POST" or "GET"; //optional
	var div = "my_div_id"; //optional
	var function_name = "process_ajax" or null; //optional
	var asyn = true or false; //optional
	
	new AJAX(url,query,method,div,function_name,asyn);
*/

function AJAX(_url,_query,_method,_div,_function,_asyn){
	
	if (window.ActiveXObject){
		this.http_object = new ActiveXObject("Microsoft.XMLHTTP");
	}else{
		this.http_object = new XMLHttpRequest();
	}//end else
	
	if(_query == null){
		_query = "";
	}//end if	
	if(!_method.match(/^GET$|^POST$/ig)|| typeof _method == 'undefined'){
		_method='GET';
	}//end if
	if(typeof _div == 'undefined' || _div == null){
		_div = 'ajax_div_not_set';
	}//end if
	if(typeof _function == 'undefined'){
		_function = null;
	}//end if
	if(typeof _asyn == 'undefined' || (_asyn!=true || _asyn!=false ) ){
		_asyn = true;
	}//end if
	
	this.addition_task = false;
	this.IE = document.all?true:false;
	this.set_output_div(_div);
	this.set_method(_method);
	this.set_query(_query);
	this.set_asyn(_asyn);
	this.set_function_name(_function);
   //this._result = 
   this.send_req(_url);//make the request
}//end AJAX   
	
/*** FUNCTIONS ***/

/*
 * Sets the id of the div tag that the result will be written to
 */
AJAX.prototype.set_output_div = function(output_div_id){
	if(document.getElementById( output_div_id )){
		this.output_div_id = output_div_id;
	}else{
		//if the div doesn't exist, create it
		var new_div = document.createElement('div');
		new_div.setAttribute("id", output_div_id);
		document.getElementsByTagName("body").item(0).appendChild(new_div);
		this.output_div_id = output_div_id;
	}//end else
}//end set_output

/*
 * set the method POST or GET
 */
AJAX.prototype.set_method = function(_method){
	this._method = _method;
}//end set_method

/*
 * Sets the query string for either POST or GET method
 */
AJAX.prototype.set_query = function(_query){
	this._query = _query;
}//end set_query

/*
 * Sets the function that will process the result
 * If this is not set, the result will be written to the @output_div_id
 */
AJAX.prototype.set_function_name = function(_function){
	if(_function==null){
		this._addition_task = false;
		this._function = _function;
	}else{	
		this._addition_task = true;
		this._function = _function;
	}//end else
}//end set_function_name

/**
 * Sets the asynchronous mode
 */
AJAX.prototype.set_asyn = function(_asyn){
	 this._asyn = _asyn;
}//end set_asyn

/*
 * Sends the request
 */
AJAX.prototype.send_req = function(_url){
	if(window.ActiveXObject){
		this.http_object = new ActiveXObject("Microsoft.XMLHTTP");
	}else{
		this.http_object = new XMLHttpRequest();
	}//end else
	try{
		var loader = this;
		this.http_object.onreadystatechange = function(){
			loader.handle_response.call(loader);
		};
		//this._asyn = false;
		this.http_object.open(this._method, this.get_url(_url), this._asyn);
		this.http_object.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
		this.http_object.setRequestHeader("Content-length", this._query.length);
		this.http_object.setRequestHeader("Connection", "close");
		this.http_object.send(this.get_query());
		
	}catch(err){
		alert('error: '+err);
	}
	
}//end send_req

/*
 * query string for POST or GET METHOD
 */
AJAX.prototype.get_query = function(){
	return (this._method=='GET')?null:this._query;
}//end get_query function

/*
 * return the correct format of the url based on POST or GET method
 */
AJAX.prototype.get_url = function(_url){
	return (this._method=='GET')?(_url+"?"+this._query):_url;
}//end get_url

/*
 * Handles the response
 */
AJAX.prototype.handle_response = function(){
	if(this.http_object.readyState == 4){ 
		var response = this.http_object.responseText;
		if(this._addition_task){
			eval(this._function+"('"+escape(response.replace(/\'/g,"\\'"))+"')");
			this._addition_task = false;
		}else{
			if(this.output_div_id != null){
				document.getElementById(this.output_div_id).innerHTML = response;
			}//end if
		}//end else
	}//end if
	else{
		//waiting
	}//end else
}//end handle_response