
AsyncRequestManager = function() {
	this.request = null;
	this.url = null;
	this.retries = 0;
	this.maxTries = 10;
	this.lastResult = '';
	this.handleSuccess = function(o){
	    if(o.responseText != undefined){
	
	        if ( o.responseText.charAt(0) != '{' ) {
	            var check = /cssLogin\?ReadForm/gim;
	            if ( check.test( o.responseText ) ) {
	                window.location.reload();
	            } else {
	                if ( this.outer != null ) this.outer.lastResult = 'noncompliant response';
	            }
	        } else {
	            eval( 'var res = ' + o.responseText );
	            if ( res != null ) {
	                if ( res.EXCEPT != null ) {
	                    if ( this.outer != null ) this.outer.lastResult = 'EXCEPTION:\n' + 
	                        ( res.EXCEPT.method != null ? res.EXCEPT.method + '()' : '' )
	                        + '[' + ( res.EXCEPT.err != null ? res.EXCEPT.err : '' )								
	                        + '@' + ( res.EXCEPT.line != null ? res.EXCEPT.line : '' )
	                        + ']: '
	                        + ( res.EXCEPT.text != null ? res.EXCEPT.text : '' );
	                } else {               	                	
	                	if ( this.callout != null ) this.callout( res );	                    
	                }	
	            } else {
		            if ( this.outer != null ) this.outer.lastResult = 'No response object';
	            }	
	        }
			this.hideLoader();
	    }	
	};
	
	this.handleFailure = function(o){
			if( o.status == -1 ) {
				if ( this.outer != null ) this.outer.lastResult = 'timeout';
				if ( this.outer != null ) 
					if ( this.outer.retries++ < this.outer.maxTries ) this.outer.init( this, this.outer.url );
			} else {
			    if ( this.outer != null ) this.outer.lastResult = 'AJAX failure:\nHTTP status: ' + o.status + '\nStatus code message: ' + o.statusText;		
			}
			this.hideLoader();	
	};
	
	this.callback =	{
		success: this.handleSuccess,
		failure: this.handleFailure,
		timeout: 30000,
		callout: null,
		loadingDiv: null,
		contentDiv: null,	 
		hideLoader: function() {	
			if ( this.loadingDiv != null ) this.loadingDiv.style.display = 'none';	
			if ( this.contentDiv != null ) this.contentDiv.style.display = '';
		},
		outer: null		    
	};
	
	
	this.init = function( callout, url, loadingDiv, contentDiv ) {
		if ( callout != null )this.callback.callout = callout;	
		if ( loadingDiv != null ) this.callback.loadingDiv = document.getElementById( loadingDiv );
		if ( contentDiv != null ) this.callback.contentDiv = document.getElementById( contentDiv );
		this.callback.outer = this;
		if ( url != null ) this.url = url;
		this.request = YAHOO.util.Connect.asyncRequest('GET', this.url, this.callback );			
	};   
}
