// Allows you to build vehicle applications using a selection list
// for year, make, model and engine. Once all compenents are selected
// the ID of the vehicle is returned. Multiple years, makes, models
// and engines can be selected at one time and in that case multiple
// vehicle IDs will be returned.

//-------------------------------------------------------------------
// ApplicationMaker()
// Instantiate a new application maker object
//-------------------------------------------------------------------
function ApplicationMaker(url, vmake, vmodel, vyear, vengine, vapplication, vresult)
  {
  this.url         = url;

  this.vmake        = getObject(vmake);
  this.vmodel       = getObject(vmodel);
  this.vyear        = getObject(vyear);
  this.vengine      = getObject(vengine);
  this.vapplication = getObject(vapplication);
  this.vresult      = getObject(vresult);

  this.vmakeVal    = new Array();
  this.vmodelVal   = new Array();
  this.vyearVal    = new Array();
  this.vengineVal  = new Array();
  }

//-------------------------------------------------------------------
// OnXML()
//-------------------------------------------------------------------
ApplicationMaker.prototype.OnXML = function(response)
  {
  var result = response.getElementsByTagName("result")[0];
  
  var data = result.childNodes;
  var type = result.getAttribute("type");
  var opt  = null;
  var obj  = null;

  if (type == "year")
    {
    opt = this.vyear;
    this.ClearOptions(this.vmake);
    this.ClearOptions(this.vmodel);
    this.ClearOptions(this.vyear);
    this.ClearOptions(this.vengine);
    this.vresult.value      = "";
    this.vapplication.value = "";
    }
  else if (type == "make")
    {
    opt = this.vmake;
    this.ClearOptions(this.vmake);
    this.ClearOptions(this.vmodel);
    this.ClearOptions(this.vengine);
    this.vresult.value      = "";
    this.vapplication.value = "";
    }
  else if (type == "model")
    {
    opt = this.vmodel;
    this.ClearOptions(this.vmodel);
    this.ClearOptions(this.vengine);
    this.vresult.value      = "";
    this.vapplication.value = "";
    }
  else if (type == "engine")
    {
    opt = this.vengine;
    this.ClearOptions(this.vengine);
    this.vresult.value      = "";
    this.vapplication.value = "";
    }
  else if (type == "applications")
    {
    obj = this.vapplication;
    this.vresult.value      = "";
    this.vapplication.value = "";
    }

/*  else
    {
    this.ClearOptions(this.vmake);
    this.ClearOptions(this.vmodel);
    this.ClearOptions(this.vengine);
    }
*/
  if (!opt && !obj)
    return;

  var objval = new Array();
  var num    = 0;

  // parse XML data and populate the select box
  for (var i=0, idx=1; i < data.length; i++)
    {
    if (data.item(i).nodeType == 1)  // A Tag node
      {
      if (opt)
        {
        var doc    = opt.ownerDocument ? opt.ownerDocument : opt.document;
        var option = doc.createElement("OPTION");

        option.value = data.item(i).getAttribute("id");
        option.text  = data.item(i).firstChild.data;

        opt.options.add(option, idx++);

        // opt.options[idx++] = new Option(data.item(i).firstChild.data, data.item(i).getAttribute("id"));
        }
      if (obj)
        {
        objval.push(data.item(i).getAttribute("id"));
        this.vresult.value += data.item(i).firstChild.data + '\n';
        num++;
        }
      }
    }

  this.vresult.value = "(" + num + " Resulting Applications)\n" + this.vresult.value;

  if (obj)
    obj.value = objval.join(',');

  if (opt)
    opt.options.selectedIndex = 0;
  }

//-------------------------------------------------------------------
// OnMakeChange()
//-------------------------------------------------------------------
ApplicationMaker.prototype.OnYearChange = function()
  {
  this.vyearVal = this.GetSelectValue(this.vyear);

  this.vmakeVal   = new Array();
  this.vmodelVal  = new Array();
  this.vengineVal = new Array();

  this.vapplication.value   = "";

  this.Update();
  }

//-------------------------------------------------------------------
// OnModelChange()
//-------------------------------------------------------------------
ApplicationMaker.prototype.OnMakeChange = function()
  {
  this.vyearVal = this.GetSelectValue(this.vyear);
  this.vmakeVal = this.GetSelectValue(this.vmake);

  this.vmodelVal     = new Array();
  this.vengineVal    = new Array();
  this.vapplication.value   = "";

  this.Update();
  }

//-------------------------------------------------------------------
// OnYearChange()
//-------------------------------------------------------------------
ApplicationMaker.prototype.OnModelChange = function()
  {
  this.vyearVal     = this.GetSelectValue(this.vyear);
  this.vmakeVal     = this.GetSelectValue(this.vmake);
  this.vmodelVal    = this.GetSelectValue(this.vmodel);

  this.vengineVal     = new Array();
  this.vapplication.value   = "";

  this.Update();
  }

//-------------------------------------------------------------------
// OnEngineChange()
//-------------------------------------------------------------------
ApplicationMaker.prototype.OnEngineChange = function()
  {
  this.vyearVal   = this.GetSelectValue(this.vyear);
  this.vmakeVal   = this.GetSelectValue(this.vmake);
  this.vmodelVal  = this.GetSelectValue(this.vmodel);
  this.vengineVal = this.GetSelectValue(this.vengine);

  this.vapplication.value   = "";

  this.Update();
  }

//-------------------------------------------------------------------
// GetSelectValue()
// Returns the value from an HTML select list object. Supports
// select lists with the "multiple" flag set
//-------------------------------------------------------------------
ApplicationMaker.prototype.GetSelectValue = function(obj)
  {
  var value = new Array();

  for (var i=0; i<obj.length; i++)
    {
    if (obj.options[i].selected)
      value.push(obj.options[i].value);
    }

  return value;
  }

//-------------------------------------------------------------------
// Update()
//-------------------------------------------------------------------
ApplicationMaker.prototype.Update = function()
  {
  var data = new Array();
  var i;
  
  for (i=0; i<this.vyearVal.length; i++)
    data.push("yr[]="+encodeURIComponent(this.vyearVal[i]));

  for (i=0; i<this.vmakeVal.length; i++)
    data.push("mk[]="+encodeURIComponent(this.vmakeVal[i]));

  for (i=0; i<this.vmodelVal.length; i++)
    data.push("md[]="+encodeURIComponent(this.vmodelVal[i]));

  for (i=0; i<this.vengineVal.length; i++)
    data.push("eg[]="+encodeURIComponent(this.vengineVal[i]));
  
  data = data.join('&');

  ajaxSendRequest(this, this.url, data, "post", true, null);
  }

//-------------------------------------------------------------------
// ClearOptions()
// Remove all options from a selection list form item except for
// the first item
//-------------------------------------------------------------------
ApplicationMaker.prototype.ClearOptions = function(obj)
  {
  for (var i = (obj.options.length-1); i > 0; i--)
    obj.options[i] = null;

  obj.options.selectedIndex = 0;
  }
