// -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= //
// Package : makemodel                 Version : 2.3
// Date    : 09/17/2003                Author  : Shaun Thomas
// Req     : Javascript 1.0              Type  : function
//
// Descripion:
// -=-=-=-=-=-
// When searching, it is sometimes nice to be able to restrict search terms
// down before executing.  In this case, the user may select make, model, or
// clas.  This set of functions will restrict the list of makes to a certain
// clas, and the list of models to a certain make.  It also keeps track of
// counts so those may be updated, and each category of make will have
// updated stats when a new clas is selected.
// -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= //

aModel = new Array();
aMake  = new Array();
aMakeType = new Array();
aModelType = new Array();

// -------------------------------------------------------------------------- //
// Function Definitions
// -------------------------------------------------------------------------- //

/**
 * Inserts a make into our global arrays.
 *
 * Since there are many attributes we're keeping track of, it didn't make any
 * sense to generate all of the javascript on a per-make basis.  Given
 * proper parameters, this function should construct everything for us.
 *
 * @param nID       Numeric ID of the make.
 * @param sMake     String name of the make.
 * @param nTotal    Total number of vehicles under this make.
 */
function insert_make(nID, sMake, nTotal)
{
  aMake[nID] = new Array();
  aMake[nID]['name'] = sMake;
  aMake[nID]['count'] = nTotal;
  aMake[nID]['models'] = new Array();

} // End function insert_make.


/**
 * Inserts a model into our global arrays.
 *
 * Like insert_make, insert model saves us a lot of work in generating
 * javascript.  Since the vehicle classification is associated with the
 * model, we have to reverse engineer that information into the make array
 * for when we update everything on a select, as well.
 *
 * @param nID        Numeric ID of the model.
 * @param nMakeID    Numeric ID of the make.
 * @param sModel     String name of the model.
 * @param sType      String category designation of the model.
 * @param nTotal     Total number of vehicles under this model.
 */
function insert_model(nID, nMakeID, sModel, sType, nCount)
{
  if (!aMake[nMakeID])
    return;

  // There is make information here, since we have to know which types are
  // valid for a specific make.  We'll also update the count here to make
  // sure the count is specific to each make type.

  if (!aMake[nMakeID][sType])
    aMake[nMakeID][sType] = nCount;
  else
    aMake[nMakeID][sType] += nCount;

  if (!aMakeType[sType])
    aMakeType[sType] = new Array();

  if (!aModelType[sType])
    aModelType[sType] = new Array();

  // This is straight-forward.  Just set all of the attributes we need to
  // keep track of later.

  aMakeType[sType][nMakeID] = 1;
  aModelType[sType][nID] = 1;
  aMake[nMakeID]['models'][nID] = 1;

  // Now create the model array for this modelid.  Only do this if it
  // didn't already exist.  Increment the count in case the same model
  // with a different type gets set later.

  if (!aModel[nID])
  {
    aModel[nID] = new Array();
    aModel[nID]['name'] = sModel;
    aModel[nID]['make'] = nMakeID;
    aModel[nID]['count'] = 0;
  }

  aModel[nID]['count'] += nCount;


} // End function insert_model.


/**
 * Loop through our arrays to update the search form-items.
 *
 * Here is the guts of the system.  When a user selects a clas or make, we
 * have to cut the list of models down to fit only that selection.  The list
 * of makes should be restricted too, in case of a clas selection.
 *
 * @param sType      String of the selected clas.
 * @param nMakeID    Numeric ID of the selected make.
 */
function change_type(sType, nMakeID)
{
  aMakeList = new Array();
  aMakeList[nMakeID] = 1;

  // If the Make ID is set to 0, that makes someone just changed the clas.
  // Knowing this, we should blow up the current contents of the make form
  // item, and rebuild it only with makes with at least one model in this clas.
  
  if (nMakeID == 0)
  {
    aMakeList = new Array();
 
    with (document.newauto.make)
    {
      // Wipe out all of the current form items, we won't be needing them.

      for (i = options.length - 1; i > 0; i--)
        options[i] = null;

      aTemp = (sType) ? aMakeType[sType] : aMake;

      // For each known make, check to see if the type has a count > 0.  If
      // so, create a new form option, and set the count to the count for
      // this clas.  If no clas is selected, just use the default total.

      for (nTempID in aTemp)
      {
        nCount = (sType) ? aMake[nTempID][sType] : aMake[nTempID]['count'];
        sName  = aMake[nTempID]['name'] + ' (' + nCount + ')';
        options[options.length] = new Option(sName, nTempID);
        aMakeList[nTempID] = 1;
      }
    }

  } // End make change block.

  // Now, we work on the models themselves.  Same idea as above, but with
  // a different array.

  with (document.newauto.model)
  {
    for (i = options.length - 1; i > 0; i--)
      options[i] = null;

    aTemp = (sType) ? aModelType[sType] : aModel;
    aTemp = (nMakeID) ? aMake[nMakeID]['models'] : aTemp;

    // This time we'll be skipping models that don't have this clas, or make.
    // And there's no need to change the count.

    for (nModelID in aTemp)
    {
      if (sType && !aModelType[sType][nModelID]) continue;
      if (nMakeID && !aMakeList[aModel[nModelID]['make']]) continue;

      sName = aModel[nModelID]['name'] + ' (' + aModel[nModelID]['count'] + ')';
      options[options.length] = new Option(sName, nModelID);
    }

  } // End model change block.

} // End function change_type.

// -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= //
