/*
Aggregate functions for the Yui connection manager ajax calls
author: Robert Whiting Novell, Inc. 2006
*/




// CORE 
// ====================================================================

// Core activity object
var activeRequestCount = 0;


// regression testing support function
// returns true if ajax processes are still active
function waitForAjax(wait) {
	if( activeRequestCount == 0 ) {
		var waitTime = 300;
		if( wait )
			waitTime = wait;
		// lets wait a few milliseconds and double check to see if we are chaining calls
		setTimeout("waitForAjax2()",waitTime);
	}
	else {
		return true();
	}
}
// returns true if ajax processes are still active
function waitForAjax2() {
	if( activeRequestCount == 0 )
		return false;
	else
		return true;
}


// Generic action function
function yuiSendRequest( url, target, payload, commandSequence) {
	//alert("Inside yuiSendRequest(): url: "+url+"  target: "+target+"   payload: "+payload+"    commandSequence: "+commandSequence);

	var callback =
	{
		success:handleSuccess,
		failure:handleFailure,
		argument:{target:target, cmdSeq:commandSequence },
		timeout:20000
	};
	
	// make the request to the server
//	var request = YAHOO.util.Connect.asyncRequest('POST', url, callback, payload);
    if( payload.length>0 && payload.charAt(0)!='?' ){
        if(payload.charAt(0)!='&' ){
            payload = '?' + payload;
        }else{
            payload = '?' + payload.substr(1);
        }
    }
	var request = YAHOO.util.Connect.asyncRequest('GET', url + payload, callback, null);
		
	//statusLoading();
	statusLoading();
	activeRequestCount++;
}

var handleSuccess = function(o){
	//alert("Inside of handleSuccess");
	
	// decrement the active request count
	if( activeRequestCount > 0 ) 
		activeRequestCount--;

	if( activeRequestCount == 0 )
		statusComplete();			
	
	if(o.responseText !== undefined){
		// check to see if this is coming from an ajax request. If not, then it is most likely an ichain call to login
		if( o.getAllResponseHeaders.toLowerCase().indexOf('novell_ajax') >= 0  )
		{
			//alert("ajax call!: "+o.getAllResponseHeaders);
			
			// fill in the div
			var status = o.statusText;
			var divId = o.argument.target;
			var divObj = document.getElementById(divId);
			divObj.innerHTML = o.responseText;

			// run the commandsequence if requested			
			if( o.argument.cmdSeq )
			{
				var commandSequenceNode = document.getElementById(o.argument.cmdSeq);
				if(commandSequenceNode != null) {
					eval(commandSequenceNode.innerHTML);
				}
			}
		} else {
			// error, reload the page
			//alert("success - error: "+o.status);
			statusRequestError();
			setTextById(divId, '');
			document.location.reload(true);
		}
	}
}

var handleFailure = function(o){
	//alert("Inside of handleFailure: "+o.status);

	// decrement the active request count
	if( activeRequestCount > 0 ) 
		activeRequestCount--;

	if( activeRequestCount == 0 )
		statusComplete();			
	
	statusRequestError();
	if(o.responseText !== undefined){
		var divId = o.argument.target;
		var divObj = document.getElementById(divId);
		divObj.innerHTML = o.statusText;
	}
	
	if( o.status == "404" ) {
		showMessage('The previous action called an invalid URL! Press OK to close and continue! ('+o.status+')','Response Error!')
	}
	else {
		showConfirm('The previous action did not receive a response from the server! '+
			'Do you wish to refresh the application? ('+o.status+')',onTimeoutReloadApp);
	}
};

function onTimeoutReloadApp(response) {
	if( response == true ) 
		document.location.reload(true);	
}


// UTIL -  backward compatibility
// ===========================================================

function sndRequest(source,target,payload,commandSequence) {
	yuiSendRequest(source,target,payload,commandSequence);
}
function sendRequest(method, source, target, displayonerror, onerror, oncomplete, payload, toggleStatus, commandSequence) {
	yuiSendRequest(source,target,payload,commandSequence);
}





