// Decalare an ElistCart Object
function ElistCart(cartName)
{
  if (cartName) {
    this.cartId = cartName;
  }
  else {
    this.cartId = "";
  }

  this.subscrHash = new Array;
  this.unsubHash = new Array;
  this.checkboxHash = new Array;
  this.radioSets = new Array();
  this.radioHash = new Array;
  this.textHash = new Array;
  this.hiddenHash = new Array;
  this.otherHash = new Array;

} // ElistCart constructor

ElistCart.prototype.toString = function()
{
  return "object ElistCart: " + this.cartId;
}

ElistCart.prototype.emptyCart = function()
{
  // "zero out" the arrays that contain the fields the cart
  // dynamically tracks.
  this.subscrHash = new Array();
  this.unsubHash = new Array();
  this.checkboxHash = new Array();
} // End function emptyCart()



// Initialize the cart.  There are certain fields we permanently
// "hold onto", other fields are dynamically added to or deleted
// from the cart.  This function will initialize the cart to have all
// permanent objects and none of the dynamic ones.
ElistCart.prototype.init = function(formMgr)
{
  var i;
  var fieldArray;
  var hashTbl;
  var iObj;
  var retVal = true;

  this.emptyCart();
  
  // "zero out" the arrays that contain the fields we permanently
  // hold references to.
  this.radioSets = new Array();
  this.radioHash = new Array();
  this.textHash = new Array();
  this.hiddenHash = new Array();
  this.otherHash = new Array();

  fieldArray = formMgr.textFields;
  hashTbl = this.textHash;
  for (i=0; i<fieldArray.length; i++) {
    iObj = fieldArray[i];
    hashTbl[iObj.name] = iObj;
  }

  fieldArray = formMgr.radioFields;
  for (i=0; i<fieldArray.length; i++) {
    iObj = fieldArray[i];
    if (! this.radioSets[iObj.name]) {
      this.radioSets[iObj.name] = new Array();
    }
    this.radioSets[iObj.name].push(iObj);
  }

  if (this.radioSets['subscriberType']) {
    this.radioHash['subscriberType'] = null;
  }

  fieldArray = formMgr.hiddenFields;
  hashTbl = this.hiddenHash;
  for (i=0; i<fieldArray.length; i++) {
    iObj = fieldArray[i];
    hashTbl[iObj.name] = iObj;
  }

  fieldArray = formMgr.otherFields;
  hashTbl = this.otherHash;
  for (i=0; i<fieldArray.length; i++) {
    iObj = fieldArray[i];
    hashTbl[iObj.name] = iObj;
  }

  return retVal;

} // End function init()

// Make the cart match the form
ElistCart.prototype.updateCartFromForm = function(formMgr)
{
  var i;
  var fieldArray;
  var hashTbl;
  var iObj;
  var retVal = true;

  this.emptyCart();

  fieldArray = formMgr.subscrFields;
  hashTbl = this.subscrHash;
  for (i=0; i<fieldArray.length; i++) {
    iObj = fieldArray[i];
    if (iObj.checked) {
      hashTbl[iObj.name] = iObj;
    }
  }

  fieldArray = formMgr.unsubFields;
  hashTbl = this.unsubHash;
  for (i=0; i<fieldArray.length; i++) {
    iObj = fieldArray[i];
    if (iObj.checked) {
      hashTbl[iObj.name] = iObj;
    }
  }

  fieldArray = formMgr.checkboxFields;
  hashTbl = this.checkboxHash;
  for (i=0; i<fieldArray.length; i++) {
    iObj = fieldArray[i];
    if (iObj.checked) {
      hashTbl[iObj.name] = iObj;
    }
  }

  hashTbl = this.radioHash;
  for (var key in this.radioSets) {
    var radArr = this.radioSets[key];
    var gotOne = false;
    for (i=0; i<radArr.length; i++) {
      iObj = radArr[i];
      if (iObj.checked) {
	hashTbl[key] = iObj;
	gotOne = true;
      }
    }
    if (! gotOne) {
      hashTbl[key] = null;
    }
  }

  fieldArray = formMgr.textFields;
  hashTbl = this.textHash;
  for (i=0; i<fieldArray.length; i++) {
    iObj = fieldArray[i];
    hashTbl[iObj.name] = iObj;
  }

  fieldArray = formMgr.hiddenFields;
  hashTbl = this.hiddenHash;
  for (i=0; i<fieldArray.length; i++) {
    iObj = fieldArray[i];
    hashTbl[iObj.name] = iObj;
  }

  fieldArray = formMgr.otherFields;
  hashTbl = this.otherHash;
  for (i=0; i<fieldArray.length; i++) {
    iObj = fieldArray[i];
    hashTbl[iObj.name] = iObj;
  }

  return retVal;

} // End function updateCartFromForm()


ElistCart.prototype.updateCart = function(cboxObj)
{
  var retVal = true;
  var hashTbl;
  var tmpHashTbl;
  var tmpObj;

  if (!cboxObj) {
    return false;
  }

  if (cboxObj.type == "checkbox") {
    // Note that subscribe and unsubscribe k
    if (cboxObj.value == "subscribe") {
      // Add or delete from the subscrHash
      hashTbl = this.subscrHash;
      this.updateHashTbl(hashTbl, cboxObj);

      // Since these checkboxes really behave like radio buttons,
      // do the right thing for the unsubHash.
      tmpHashTbl = this.unsubHash;
      if (tmpHashTbl[cboxObj.name]) {
	tmpObj = tmpHashTbl[cboxObj.name];
	this.updateHashTbl(tmpHashTbl, tmpObj);
      }
    }

    else if (cboxObj.value == "unsubscribe") {
      // Add or delete from the unsubHash
      hashTbl = this.unsubHash;
      this.updateHashTbl(hashTbl, cboxObj);

      // Since these checkboxes really behave like radio buttons,
      // do the right thing for the subscrHash.
      tmpHashTbl = this.subscrHash;
      if (tmpHashTbl[cboxObj.name]) {
	tmpObj = tmpHashTbl[cboxObj.name];
	this.updateHashTbl(tmpHashTbl, tmpObj);
      }
    }
    else {
      hashTbl = this.checkboxHash;
      this.updateHashTbl(hashTbl, cboxObj);
    }
  } // END if (checkbox)
  else if (cboxObj.type == "radio") {
    hashTbl = this.radioHash;
    this.updateHashTbl(hashTbl, cboxObj);
  }

  return retVal;
}

ElistCart.prototype.updateHashTbl = function(tbl, obj)
{
  if (obj.checked) {
    tbl[obj.name] = obj;
  }
  else {
    if (tbl[obj.name]) {
      delete tbl[obj.name];
    }
  }
}

ElistCart.prototype.showMe = function(textFieldId)
{
  showMeObj = document.getElementById(textFieldId);
  if (! showMeObj) {
    return;
  }

  var fieldObj;
  var hashTbl;
  var k;
  var count;
  var tmpStr = "===  SHOW CART  =================\n";

  tmpStr += "-Subs-\n";
  hashTbl = this.subscrHash;
  count=0;
  for (k in hashTbl) {
    fieldObj = hashTbl[k];
    tmpStr += "  " + count++ + ") " + k + "=" + fieldObj.value + "\n";
  }

  tmpStr += "-Unsubs-\n";
  hashTbl = this.unsubHash;
  count=0;
  for (k in hashTbl) {
    fieldObj = hashTbl[k];
    tmpStr += "  " + count++ + ") " + k + "=" + fieldObj.value + "\n";
  }

  tmpStr += "-Checkboxes-\n";
  hashTbl = this.checkboxHash;
  count=0;
  for (k in hashTbl) {
    fieldObj = hashTbl[k];
    tmpStr += "  " + count++ + ") " + k + "=" + fieldObj.value + "\n";
  }

  tmpStr += "-Radio-\n";
  count=0;
  for (k in this.radioSets) {
    tmpStr += "  set " + count++ + ") " + k + " [";
    var i;
    var radArray = this.radioSets[k];
    for (i=0; i<radArray.length; i++) {
      fieldObj = radArray[i];
      if (i > 0) {
	tmpStr += ", ";
      }
      tmpStr += fieldObj.value;
    }
    tmpStr += "]\n";
  }

  hashTbl = this.radioHash;
  count=0;
  for (k in hashTbl) {
    fieldObj = hashTbl[k];
    tmpStr += "  " + count++ + ") " + k + "=" + (fieldObj ? fieldObj.value : fieldObj) + "\n";
  }

  tmpStr += "-Text-\n";
  hashTbl = this.textHash;
  count=0;
  for (k in hashTbl) {
    fieldObj = hashTbl[k];
    tmpStr += "  " + count++ + ") " + k + "=\"" + fieldObj.value + "\"\n";
  }

  tmpStr += "-Hidden-\n";
  hashTbl = this.hiddenHash;
  count=0;
  for (k in hashTbl) {
    fieldObj = hashTbl[k];
    tmpStr += "  " + count++ + ") " + k + "=\"" + fieldObj.value + "\"\n";
  }

  tmpStr += "-Other-\n";
  hashTbl = this.otherHash;
  count=0;
  for (k in hashTbl) {
    fieldObj = hashTbl[k];
    tmpStr += "  " + count++ + ") " + k + "=\"" + fieldObj.value + "\"\n";
  }

  showMeObj.value += tmpStr;

} // End function showMe()

