/**
*	ajax_v2.js
*	author : Andrei McMillan
*	last revised: 1-5-2007
**/
/*
	Example:
	var url = "test.php";
	var query = null;
	var my_method = "POST";// or "GET"
	var div = "my_div_id1" //where the result will be displayed
	//if this is set, make sure that the function_name exist.
	var f_name = null; 
	
	function lookup(){
		query = "word="+document.getElementById("movie").value;
		var ajax = new AJAX(url,query,my_method,div,f_name);
	}
*/
////////////////////////////////////////////////////////////////////////////////

var http_object = null;
var output_div_id = null;
var addition_task = 0;
var function_name = null;
var qstr = null;
var m = 'GET';
var IE = document.attachEvent?true:false;
var tempVal = null;

//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

/*
* this is the main call to ajax
*/
function AJAX(url,query,m1,div,func_name){
   this.div = set_output_div(div);
	this.m = set_method(m1);
	this.query = set_query(query==null?"":query);
	if(typeof func_name != "undefined"){
      this.func = (func_name==null)?"":set_function_name(func_name);
   }//end if
   this.result = send_req(url);
}//end AJAX


/*
* 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(m1){
	if(!m1.match(/^GET$|^POST$/ig)){m1="GET"}
	this.m = m1;
}//end set_method

/*
* Sets the query string for either POST or GET method
*/
function set_query(qstr){
	this.qstr = qstr;
}//end set_query

/*
* Sends the request
*/
function send_req(url){
	//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(m, get_url(url), true);
	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 sndReq

/*
* query string for POST or GET METHOD
*/
function get_query(){
	return (m=='GET')?null:qstr;
}//end getquerystring function

/*
* return the correct format of the url based on POST or GET method
*/
function get_url(url){
	return (m=='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
