/**
 * connection.js
 *
 * connection.js contains functions to handle all communication between the javascript application
 * and the php application on the server.
 *
 * LICENSE: Some license information
 *
 * @copyright  2006 Dakota Network 
 * @license    http://www.dakotanetwork.com.com/license/DNC/1_0.txt   DNC Licence 1.0
 * @version    0.01
 * @package    DNC_FW
 * @link       http://www.dakotanetwork.com/package/DNC_FW/
 * @since      File available since Release 0.01
*/

/**
 * dncHandleFailure
 * 
 * if the server returns an error code this function will hadle the result
 */
var dncHandleFailure = function(o){
	// hid the loading panel
	// dncPanelWait.hide();
	// display the error message
	alert("error: "+o.statusText);
}
var run_ss = 0;
 /**
 * dncHandleSuccess
 * 
 * All successfull transactions are ran through this function.  
 */
var dncHandleSuccess = function(o){
	
	// hid the loading panel
	// dncPanelWait.hide();
	
	// convert the text into a json object
	// see includes/json/json.js
	//objJson = json.parse(o.responseText);
	objJson = eval('(' + o.responseText + ')');
	//alert ("hi");
	if (!objJson){
		dncSimpleDialog("Error!", o.responseText);
	}
	// if the variable "result" is set to true or false we will display a dialog
	if (objJson.result=="false"){
		
		// display a warning dialog
		dncSimpleDialog("Warning!", objJson.result_message);
				
	} else if (objJson.result=="true"){
		
		// display a success dialog
		dncSimpleDialog("Success!", objJson.result_message);
		
		// run the function initaly passed to the makeRequest function
		o.argument.successFunction(objJson);
		
	} else {
		
		
		
		
		// run the function initaly passed to the makeRequest function
		o.argument.successFunction(objJson);
		
		
		var javascript_arr = objJson.javascript;
		var stylesheet_arr = objJson.stylesheet;
		var div_arr = objJson.div;
		if (javascript_arr){
			for (i=0; i < javascript_arr.length; i++) {
				loadJavascript(javascript_arr[i]);
			}
		}
		
		if (stylesheet_arr){
			for (i=0; i < stylesheet_arr.length; i++) {
				loadStylesheet(stylesheet_arr[i]);
			}
		}
		
		if (div_arr){
			for (i=0; i < div_arr.length; i++) {
				
				loadHeadDiv(div_arr[i]);
			}
		}
	
	}
	if (run_ss==1) {
		setTimeout("supersleight.run()", 3000);
		//supersleight.run();
	}
}

// setup a global dncPanelWait variable
var dncPanelWait = null;

 /**
 * initDncPanelWait
 * 
 * sets up the laoding panel  
 */
function initDncPanelWait(){
	
	// create the panel
	dncPanelWait =  new YAHOO.widget.Panel("wait", { 	width:"240px",  
																fixedcenter:true, 
																underlay:"shadow", 
																close:false, 
																visible:false,  
																draggable:false, 
																modal:true
																// effect:{effect:YAHOO.widget.ContainerEffect.FADE, duration:0.5
														} );
																
	dncPanelWait.setHeader("Working, please wait...");
	dncPanelWait.setBody("<img src=\"/DNC-FW/templates/default/images/loading.gif\"/>");
	dncPanelWait.render(document.body);
	dncPanelWait.show();
}

 /**
 * makeRequest
 * 
 * all "ajax" requests are sent through this function  
 */
function makeRequest(callback_var, php_file, php_function, get_args){
	
	
	// this creates the callback variable adding the user defined successFunction that
	// will be ran by dncHandleSuccess
	var dnc_callback =	{
						success:dncHandleSuccess,
						failure:dncHandleFailure,
						argument: {successFunction: callback_var}
						};
	// create the url using the arguments
	var sUrl = php_file+"?function="+php_function;
	
	// add any get variables
	for(var field in get_args){
		
		sUrl = sUrl+"&"+field+"="+get_args[field];
	}
	
	// acutaly run the request, this will now be handed to dncHandleSuccess (or dncHandleFailure)
	var request = YAHOO.util.Connect.asyncRequest('GET', sUrl, dnc_callback);
	return false;
}

function dncNull(json) {
	// nothin
}