	/* Global variables  */	
	var gproduct=1;
	var gReport=1;  //--------------------------- Why are these necessary? IIABDFI!
	
	//Javascript versions of CF session values.
	var sessionProId;
	var sessionRptId;
	var sessionRgnId;
	var sessionPTId;
	var sessionPrcId;
    var sessionUntId;
	
	//Currently selected values.
	var selectedProId;
	var selectedRptId;
	var selectedPTId;
	var selectedPrcId;
	var selectedUntId;
	
	/* Initialise DWR library  */
	function init(){
		DWRUtil.useLoadingMessage();
		DWREngine._errorHandler =  errorHandler;
	}
	
    function updateDropdown(type, selectedVal, sessionVal){
		//Fired from Product Family...
		if(type=='product'){
			//Fired from Family...
			sessionProId = sessionVal;
			getProductInfo(selectedVal);			
		}else if(type=='analysis'){ 
			//Fired from Product...
			sessionRptId = sessionVal;
			DWREngine._execute(_cfscriptLocation, null, 'checkIsSticky', type, selectedVal, analysisUpdate);
		}else if(type=='geography'){
		    //Fired from Analysis...
			sessionRgnId = sessionVal;
			DWREngine._execute(_cfscriptLocation, null, 'checkIsSticky', type, selectedVal, DWRUtil.getValue('myproduct'), regionUpdate);
		}else if(type=='planttype'){
		    //Fired from Region...
			sessionPTId = sessionVal;
			DWREngine._execute(_cfscriptLocation, null, 'checkIsSticky', type, selectedVal, DWRUtil.getValue('myproduct'), planttypeUpdate);
		}else if(type=='process'){
		    //Fired from Plant type...
			sessionPrcId = sessionVal;
			DWREngine._execute(_cfscriptLocation, null, 'checkIsSticky', type, selectedVal, DWRUtil.getValue('myproduct'),DWRUtil.getValue('CPMPlantTypeID'),  processUpdate);
		}else if(type=='unit'){
		    //Fired from ???...
			sessionUntId = sessionVal;
			//alert('selectedVal: '+selectedVal);
			DWREngine._execute(_cfscriptLocation, null, 'checkIsSticky', type, selectedVal, DWRUtil.getValue('myproduct'),  DWRUtil.getValue('myReport'),  unitUpdate);
		}else{
			alert('Unknown dropdown ['+type+'] requested an update');
		}
	}
	
	
	//-------------------------------- Updates ----------------------------------------------------//
	function analysisUpdate(returnVal){
	    var useSession = trim(returnVal[0]["VALUE"]);
		if (useSession != 'false'){
			selectedRptId = parseInt(useSession);	
		}else{
			selectedRptId = false;
		}
		getAnalysisInfo(DWRUtil.getValue('myproduct'));
		document.getElementById('myReport').onchange(); 
		
	}
	
	function regionUpdate(returnVal){
	    var useSession = trim(returnVal[0]["VALUE"]);
		if (useSession != 'false'){
			selectedRgnId = parseInt(useSession);	
		}else{
			selectedRgnId = false;
		}
		getRegionInfo(DWRUtil.getValue('myproduct'), DWRUtil.getValue('myReport'));
	}
	
	function planttypeUpdate(returnVal){
	    var useSession = trim(returnVal[0]["VALUE"]);
		if (useSession != 'false'){
			selectedPTId = parseInt(useSession);	
		}else{
			selectedPTId = false;
		}
		getPlantTypeInfo(DWRUtil.getValue('myproduct'), DWRUtil.getValue('myRegion'));
		//Force the update to fire...
		document.getElementById('CPMPlantTypeID').onchange(); 
	}
	
	function processUpdate(returnVal){ 
	    var useSession = trim(returnVal[0]["VALUE"]);
		if (useSession != 'false'){
			selectedPrcId = parseInt(useSession);	
		}else{
			selectedPrcId = false;
		}
		
		getProcessInfo(DWRUtil.getValue('myproduct'), DWRUtil.getValue('myReport'), DWRUtil.getValue('myRegion'));
	}
	
	function unitUpdate(returnVal){ 
	    var useSession = trim(returnVal[0]["VALUE"]);
		if (useSession != 'false'){
			selectedUntId = parseInt(useSession);	
		}else{
			selectedUntId = false;
		}
		
		getUnitInfo(DWRUtil.getValue('myproduct'), DWRUtil.getValue('myReport'));
	}
	
	/* Calls the Coldfusion function required to build the relevant drop down */
	function getDropdownContents(element, firstVal, secondVal, refgProd){		
	  var cfFunct;
	  var cfCallback;
	
	  if(element=='myproduct'){
		cfFunct = 'makelookup';
		cfCallback = setBrandResult;
	  }else if (element=='myReport'){
	    cfFunct = 'getReportType';
		cfCallback = setAnalysisResult;
	  }else if (element=='myRegion'){
	    cfFunct = 'setRegion';
		cfCallback = setRegionResult;
	  }else if (element=='myScenario'){
	    cfFunct = 'setScenario';
		cfCallback = setScenarioResult;
	  }else if (element=='CPMPlantTypeID'){
	    cfFunct = 'setPlantType';
		cfCallback = setPlantTypeResult;
	  }else if (element=='myProcess'){
	    cfFunct = 'setProcess';
		cfCallback = setProcessResult;
	  }else if (element=='units_id'){
	    cfFunct = 'setUnits';
		cfCallback = setUnitResult;
	  }else if (element=='PriceProdID'){
	    cfFunct = 'setPriceProd';
		cfCallback = setPriceProdResult;
	  }
	  
	  // Execute the required AJAX function...
	  if(element=='myProcess'){
		  //Should really be using  if(thirdVal==false), but this will do for now :)
		  cpmIDVal = DWRUtil.getValue('CPMPlantTypeID');
		  DWREngine._execute(_cfscriptLocation, null, cfFunct,firstVal, gproduct, secondVal, cpmIDVal, cfCallback);
		  return true;
	  }else{
	   if (secondVal==false){
		if (cfFunct && cfCallback){
			if(refgProd){
				DWREngine._execute(_cfscriptLocation, null, cfFunct,firstVal, gproduct, cfCallback);
				return true;
			}else{
				DWREngine._execute(_cfscriptLocation, null, cfFunct,firstVal,cfCallback);
				return true;
			}
		}else{
			alert('Unrecognised drop-down ['+element+'] was passed to getDropdownContents');
		}
	  }else{
		if (cfFunct && cfCallback){
			if(refgProd){
				DWREngine._execute(_cfscriptLocation, null, cfFunct,firstVal, gproduct, secondVal, cfCallback);
				return true;
			}else{
				DWREngine._execute(_cfscriptLocation, null, cfFunct,firstVal, secondVal,cfCallback);
				return true;
			}
		}else{
			alert('Unrecognised drop-down ['+element+'] was passed to getDropdownContents (regionVal was also undefined)');
		}
	  }
	 }
	}
	
	function getProductInfo(prodFamID){
	    gProdFam = prodFamID;
	    // Build myproduct dropdown...
		if(getDropdownContents('myproduct', DWRUtil.getValue("ProdClassid"), false,false)){
		  return true;
		}else{
		  alert('Timeout occurred on: myproduct');
		  return false;
		}
	}
	
	function getAnalysisInfo(prodID){
		// Build myReport dropdown...
		if(getDropdownContents('myReport', prodID, false, false)){
		  return true;
		}else{
		  alert('Timeout occurred on: myReport');
		  return false;
		}
	}
	
	function getRegionInfo(prodID, analysisID){
		gproduct=prodID;
		//Build myRegion dropdown...
		if(getDropdownContents('myRegion',analysisID,false,true)){	
			//Build myScenario drop down...
			getDropdownContents('myScenario',analysisID, false,true);
			return true;
		}else{
		  alert('Timeout occurred on: myRegion');
		  return false;
		}
	}
	
	function getPlantTypeInfo(prodID, regionID){
		gproduct=prodID;
		//Build myPlantType dropdown...
		if(getDropdownContents('CPMPlantTypeID',regionID,prodID,false)){	
			return true;
		}else{
		  alert('Timeout occurred on: CPMPlantTypeID');
		  return false;
		}
	}

	function getProcessInfo(prodID, analysisID, regionID){
		gproduct=prodID;
		//Build myProcess dropdown...
		if(getDropdownContents('myProcess',analysisID,regionID, true)){	
			return true;
		}else{
		  alert('Timeout occurred on: myProcess');
		  return false;
		}
	}
	
	
	function getUnitInfo(prodID, analysisID){
		gproduct=prodID;
		//Build myProcess dropdown...
		if(getDropdownContents('units_id', prodID, analysisID, false)){	
			return true;
		}else{
		  alert('Timeout occurred on: units_id');
		  return false;
		}
	}
	
	//------------Call back functions for building dropdowns ----------------------------------//
//	function setBrandResult(myproductArray){
//		DWRUtil.removeAllOptions("myproduct");
//		DWRUtil.addOptions("myproduct", myproductArray, "KEY", "VALUE", false);
		//Post ajax commands should be placed here...
		//Update the units drop down,,, only after the Ajax has fired.
//		getDropdownContents('units_id', DWRUtil.getValue('myproduct'), false, false);
//		document.getElementById('myproduct').onchange();
//	}
	
	function setBrandResult(myproductArray){
		DWRUtil.removeAllOptions("myproduct");
		DWRUtil.addOptions("myproduct", myproductArray, "KEY", "VALUE", false);
		//Post ajax commands should be placed here...
		//Update the units drop down,,, only after the Ajax has fired.
		getDropdownContents('units_id', DWRUtil.getValue('myproduct'),DWRUtil.getValue('myReport'), false);
		if(DWRUtil.getValue('myReport') == 11){
			getDropdownContents('PriceProdID', DWRUtil.getValue('myproduct'), false, false);
		}
		document.getElementById('myproduct').onchange();
	}
	
    function setAnalysisResult(myReportArray){
		DWRUtil.removeAllOptions("myReport");
		DWRUtil.addOptions("myReport", myReportArray,"KEY","VALUE", selectedRptId);
		//Post ajax commands should be placed here...
		if(DWRUtil.getValue('myReport') == 11){
			getDropdownContents('PriceProdID', DWRUtil.getValue('myproduct'), false, false);
		}
	}
	
	function setRegionResult(myRegionArray){ 
		DWRUtil.removeAllOptions("myRegion");
		DWRUtil.addOptions("myRegion",myRegionArray,"KEY","VALUE", selectedRgnId);
		//Post ajax commands should be placed here...
		//getDropdownContents('units_id', DWRUtil.getValue('myproduct'), DWRUtil.getValue('myReport'), false);
		if(document.getElementById('CPMPlantTypeID')){
			getPlantTypeInfo(DWRUtil.getValue('myproduct'), DWRUtil.getValue('myRegion'));
		}
	}
		
	function setScenarioResult(myScenarioArray) {
		DWRUtil.removeAllOptions("myScenario");
		DWRUtil.addOptions("myScenario",myScenarioArray,"KEY","VALUE", false);
		//Post ajax commands should be placed here...
	}
	
	function setPlantTypeResult(myPlantArray){
		DWRUtil.removeAllOptions("CPMPlantTypeID");
		DWRUtil.addOptions("CPMPlantTypeID",myPlantArray,"KEY","VALUE", selectedPTId);
		//Post ajax commands should be placed here...
	}
	
	function setProcessResult(myProcessArray){
		DWRUtil.removeAllOptions("myProcess");
		DWRUtil.addOptions("myProcess",myProcessArray,"KEY","VALUE", selectedPrcId);
		//Post ajax commands should be placed here...
	}
	
	function setUnitResult(myUnitArray){
		DWRUtil.removeAllOptions("units_id");
		DWRUtil.addOptions("units_id",myUnitArray,"KEY","VALUE", selectedUntId);
		//Post ajax commands should be placed here...
	}

	function setPriceProdResult(myPriceProdArray){
		if(document.getElementById("PriceProdID")){
		DWRUtil.removeAllOptions("PriceProdID");
		DWRUtil.addOptions("PriceProdID",myPriceProdArray,"KEY","VALUE", false);
	}
	//Post ajax commands should be placed here...
}

