/** Begin Ajax Object ***/

function SafAjaxObj ( targ_d ) {
	this._target_div	= document.getElementById( targ_d );
	this._waitingMsg	= "";
	this._init();
	this._loads	= 0;
	this._handlerFunction = null;
	this._isDone	= false;
}

SafAjaxObj.prototype.SetHandler = function(h) { this._handlerFunction = h; }

SafAjaxObj.prototype.isDone	= function() { return this._isDone; }
        
SafAjaxObj.prototype.setWaitingMsg      = function(m) { this._waitingMsg = m; }
                                                                                                                             
SafAjaxObj.prototype._objXmlHttp	= null;

SafAjaxObj.prototype._init	= function() {
	this._getXmlHttpObject();
}
       
SafAjaxObj.prototype.loadUrl	= function(url) {
//add a &rand=Math.random(); here
                
	if( this._waitingMsg != "" )
		this._target_div.innerHTML       = this._waitingMsg;
                                                                                                                             
	if( this._objXmlHttp == null )
	{
		this._target_div.innerHTML       = "-- no obj --";
		return;
	}
               
	var _this	= this; 
	this._objXmlHttp.onreadystatechange      = function() { _this._stateChanged();}
	try {  this._objXmlHttp.open("GET",url,true);
	} catch (e) { alert('caught an error: ' + e ); }
	
	this._objXmlHttp.send(null);
	this._loads++;
}

SafAjaxObj.prototype._getXmlHttpObject	= function() {
	if(window.XMLHttpRequest)
		this._objXmlHttp         = new XMLHttpRequest();
	else if( window.ActiveXObject )
		this._objXmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
}

SafAjaxObj.prototype._stateChanged               = function() {

	if( this._httpIsReady() )    
		this._processHttpRequest()
}
                                                                                                                             
        
SafAjaxObj.prototype._targetType = "div";
SafAjaxObj.prototype.SetTargetType               = function(Type) {
	this._targetType = Type;
}
        
SafAjaxObj.prototype._processHttpRequest         = function() {

	var response	= this._objXmlHttp.responseText;
	
	if( this._handlerFunction != null )
		this._handlerFunction( response );
	else if( this._targetType == "textarea" )
		this._target_div.innerText = response;
	else this._target_div.innerHTML	= response; //simple output to target div

	this._isDone	= true;
}
        
SafAjaxObj.prototype._httpIsReady 		= function() {
	if(this._objXmlHttp.readyState == 4 
	|| this._objXmlHttp.readyState == "complete" )
	{
		return true;
	} else { return false; }
}

