//ajax.js
//last revised: 5-18-2007
/*
	To use this file, set the following functions
		set_output_div(output_div_id);
		//set_function_name(function_name); // <--optional
		//set_method('POST'); // <-- optional, the default is 'GET'
		set_query('query=data_goes_here');
		send_req(url);
*/

var http_object = null;
var output_div_id = null;
var addition_task = 0;
var function_name = null;
var qstr = null;
var method = 'GET';
var async = true;
var IE = document.attachEvent?true:false;

//note : for IE7, http_object needs to created first
if (window.ActiveXObject){
	http_object = new ActiveXObject("Microsoft.XMLHTTP");
}else{
	http_object = new XMLHttpRequest(); 
}//end else

/*
* Sets the id of the div tag that the result will be written to
*/
function set_output_div(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
		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

/*
* Sets the function that will process the result
* If this is not set, the result will be written to the @output_div_id
*/
function set_function_name(function_name){
	addition_task = 1;
	this.function_name = function_name;
}//end set_function_name

/*
* set the method POST or GET
*/
function set_method(method){
	if(!method.match(/^GET$|^POST$/ig)){method="GET"}
	this.method = method;
}//end set_method

/*
* Sets the query string for either POST or GET method
*/
function set_query(qstr){
	this.qstr = qstr;
}//end set_query

function set_async(async){
	this.async = async;
}//end set_async

function get_async(){
	return this.async;
}//end get_async

/*
* Sends the request
*/
function send_req(url){
	if(check_vals()){
		//NOTE: For some reason, firefox need the create the object everytime
		//you make a request
		if(!IE){
			http_object = new XMLHttpRequest();	
		}//end if
		http_object.open(method, get_url(url), get_async());
		http_object.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
		http_object.setRequestHeader("Content-length", qstr.length);
		http_object.setRequestHeader("Connection", "close");
		http_object.onreadystatechange = handle_response;
		http_object.send(get_query());
	}//end if
}//end sndReq

/*
* query string for POST or GET METHOD
*/
function get_query(){
	return (method=='GET')?null:qstr;
}//end getquerystring function

/*
* return the correct format of the url based on POST or GET method
*/
function get_url(url){
	return (method=='GET')?(url+"?"+qstr):url;
}//end get_url

/*
* Handles the response
*/
function handle_response() { 
	if(http_object.readyState == 4){ 
		var response = http_object.responseText;
		if(addition_task){
			eval(function_name+"('"+escape(response)+"')");
			addition_task = 0;
		}else{
			document.getElementById(output_div_id).innerHTML = response;
		}//end else
	}//end if
}//end handle_response

/*
* checks to make sure the required values are set before sending the request
*/
function check_vals(){
	if(output_div_id==null){
		//alert("please set 'output_div_id' by setting 'set_output_div(output_div_id)'");
		return false;
	}if(qstr==null){
		//alert("please set 'qstr' by setting 'set_query(qstr)'");
		return false;
	}else{
		return true;
	}//end else
}//end check_vals