#include "NetServices.as"
#include "NetDebug.as"
#include "DataGlue.as"

// Remoting initialization
if (initialized == null) {
  initialized = true;
  NetServices.setDefaultGatewayUrl("http://localhost/flashservices/gateway");
  my_conn = NetServices.createGatewayConnection();
  // Create the service objects
  UIService = my_conn.getService("com.oreilly.frdg.UI", this);
}

function getAll_Result(result) {
  ComboBoxFill(employees_cb, result.Employees,"--Choose an employee--");
  ComboBoxFill(suppliers_cb, result.Suppliers,"--Choose a supplier--");
  ComboBoxFill(regions_cb, result.Regions,"--Choose a region--");
  ComboBoxFill(categories_cb, result.Categories,"--Choose a category --");
}

function getAll_Status(status) {
  errorHandler(status.description);
}

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

// General error handler for entire movie
function errorHandler(message){
  trace(message);
};

// 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){
  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 = {};
    rs.addItemAt(0, temp);
    rs.setField(0,fields[0], 0);
    rs.setField(0,fields[1],zeroElement);
  }
  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();