import mx.remoting.Service;
import mx.rpc.RelayResponder;
import mx.rpc.FaultEvent;
import mx.rpc.ResultEvent;
import mx.remoting.PendingCall;
import mx.remoting.RecordSet;
import mx.remoting.DataGlue;
import mx.remoting.debug.NetDebug;

NetDebug.initialize();

var UIService:Service = new Service(
  // gatewayUrl
  "http://www.flash-remoting.com/flashservices/gateway",
  // log the connection
  new Log(),
  // Remote service UI.cfc located in com/oreilly/frdg directory
  "com.oreilly.frdg.UI");

// call the service remote method
var pc:PendingCall = UIService.getAll();

// tell the service what methods handle result and fault conditions
pc.responder = new RelayResponder(this, "getAll_Result", "getAll_Fault" );

function getAll_Result(myResult:ResultEvent):Void {
  ComboBoxFill(employees_cb, myResult.result.EMPLOYEES,"--Choose an employee--");
  ComboBoxFill(suppliers_cb, myResult.result.SUPPLIERS,"--Choose a supplier--");
  ComboBoxFill(regions_cb, myResult.result.REGIONS,"--Choose a region--");
  ComboBoxFill(categories_cb, myResult.result.CATEGORIES,"--Choose a category --");
}

function getAll_Fault(theFault:FaultEvent):Void {
  // if there is an error, call an error handling routine
  errorHandler(theFault.fault.faultstring);
}

// General error handler for entire movie
function errorHandler(theError:String):Void{
  // for developmental purposes, simply trace the error
  trace(theError);
};

 

// Major error handler, usually a connection is bad, so the movie will fail
_global.System.onStatus = function(error):Void {
  errorHandler("There was a connection failure");
}

// ComboBoxFill
// arguments: combobox name, recordset name, optional zeroElement
// This function assumes that data is coming in with
// ID column in [0] position of the recordset and description column
// in the [1] position
// cbName is the fully-qualified name of the ComboBox -- no quotes
// zeroElement is an optional argument that contains a zero element
// of a descriptive label, like "--Categories--"

function ComboBoxFill(cbName, rs, zeroElement):Void{
  var fields = rs.getColumnNames();
  // if there is a descriptive text to put in the Combo box
  // put it in the 0 position of the RecordSet
  if(zeroElement != null) {
    var temp = {};
    temp[fields[0]] = 0;
    temp[fields[1]] = zeroElement;
    rs.addItemAt(0, temp);
  }
  var idField = '#' + fields[0] + '#';
  var descField = '#' + fields[1] + '#';
  DataGlue.bindFormatStrings(cbName, rs, descField, idField);
}

// Call the remote service to populate the combo boxes
UIService.getAll();