/***************************************************************************
 *	 This Software based on ajax tutorial found in Sams.Ajax.for.Web.Application.Developers book
 *   This program is free software; you can redistribute it and/or modify
 *   it under the terms of the GNU General Public License as published by
 *   the Free Software Foundation; either version 2 of the License, or
 *   (at your option) any later version.
 *
 ***************************************************************************/
Ajax = {};
Ajax.ini=function(){
	Ajax.update=false;
	Ajax.headers=new Array();	try {
	// Firefox, Opera 8.0+, Safari
		Ajax.request=new XMLHttpRequest();
	} catch(e) {
		try {
			Ajax.request=new ActiveXObject("Msxml2.XMLHTTP");
		} catch(e) {
			try {
				Ajax.request=new ActiveXObject("Microsoft.XMLHTTP");
			} catch(e) {
				alert("Your browser does not support AJAX!");
				return false;
			}
		}
	}
}
Ajax.ini();
Ajax.makeHeaders = function(key, val){
	Ajax.headers[key]=val;
}
Ajax.makeRequest = function(method, url, callbackMethod, parameters){
	if(!Ajax.update){
		Ajax.update=true;
		Ajax.request.open(method, url, true);
		for(var par in Ajax.headers){
			if(typeof Ajax.headers[par] !='function'){
				Ajax.request.setRequestHeader(par,Ajax.headers[par]);			
			}
		}
		Ajax.request.onreadystatechange = callbackMethod;
		if(parameters!=null){
			Ajax.request.send(encodeURI(parameters));
		}
		else{
			Ajax.request.send(null);
		}
	}
}
Ajax.checkReadyState = function(_id,loading){
    ok=false;
	switch(Ajax.request.readyState){
	   case 1:
           document.getElementById(_id).innerHTML = loading;
			break;
       case 2:
           document.getElementById(_id).innerHTML = loading;
           break;
       case 3:
           document.getElementById(_id).innerHTML = loading;
           break;
       case 4:
		   Ajax.update = false;
           document.getElementById(_id).innerHTML = '';
           ok=true;
		   break;
       default:
           document.getElementById(_id).innerHTML = "An unexpected error has occurred.";
   }
   return ok;
}
Ajax.removeChildren=function(Node){
	if(Node==null){
		return;
	}
	else{
		while(Node.hasChildNodes()){
			Node.removeChild(Node.firstChild);
		}
	}	
}
Ajax.getResponse = function(){
    if(this.request.getResponseHeader('Content-Type').indexOf('xml') != -1){
        return Ajax.request.responseXML.documentElement;
    }
    else{
        return Ajax.request.responseText;
    }
}

