//<script>
// ----------------------------------------------------------------------
// object......: BuildACar
// parameters..: none
// purpose.....: to calculate and store user selections
// ----------------------------------------------------------------------
   
function BuildACar() {
  this.feature = new Array;    
  this.strInteriorColorDivId = "";
  this.aExteriorColor = new Array;    
}

function Feature() {
  this.strName = "";
  this.strRetail = "";
  this.strVB = "";
  this.strPort = "";
  this.bSelected = false;
}

function ExteriorColor() {
  this.color = new Color;
  this.aInteriorColor = new Array;
}

function Color() {
  this.strName = "";
  this.strDescription = "";
  this.strSrc = "";
  this.strValue = "";
}

BuildACar.prototype.addExteriorColor = function (strName, strDescription, strSrc, strValue) {
  intIndex = this.aExteriorColor.length;
  this.aExteriorColor[intIndex]                = new ExteriorColor;
  this.aExteriorColor[intIndex].strName        = strName;
  this.aExteriorColor[intIndex].strDescription = strDescription;
  this.aExteriorColor[intIndex].strSrc         = strSrc;
  this.aExteriorColor[intIndex].strValue       = strValue;
}

BuildACar.prototype.addInteriorColor = function (strExteriorColorName, strInteriorColorName, strDescription, strSrc, strValue) {
  var intExtIndex    = this.getArrayIndexFromName(strExteriorColorName, this.aExteriorColor);
  var aInteriorColor = this.aExteriorColor[intExtIndex].aInteriorColor;
  var intIntIndex    = aInteriorColor.length;
  aInteriorColor[intIntIndex]                = new Color;
  aInteriorColor[intIntIndex].strName        = strInteriorColorName;
  aInteriorColor[intIntIndex].strDescription = strDescription;
  aInteriorColor[intIntIndex].strSrc         = strSrc;
  aInteriorColor[intIntIndex].strValue       = strValue;
}

BuildACar.prototype.showInteriorColor = function (strExteriorColorName){
  var strOutput = ""
  var x  = this.getArrayIndexFromName(strExteriorColorName, this.aExteriorColor);
  //  strOutput += x + " " + this.aExteriorColor[x].strName + " " + this.aExteriorColor[x].strDescription + " " + this.aExteriorColor[x].strSrc + " " + this.aExteriorColor[x].strValue + "\n";
  var aInteriorColor = this.aExteriorColor[x].aInteriorColor;
  strOutput += this.InteriorColorTableStart();
  for (var x=0; x<aInteriorColor.length; x++){
    // build inner html here     
    //  strOutput += x + " " + aInteriorColor[x].strName + " " + aInteriorColor[x].strDescription + " " + aInteriorColor[x].strSrc + " " + aInteriorColor[x].strValue + "\n";
    strOutput += this.InteriorColorAdd(aInteriorColor[x].strName, aInteriorColor[x].strValue, aInteriorColor[x].strDescription)  
   }
   strOutput += this.InteriorColorTableEnd();  
   document.getElementById("test").innerHTML = strOutput;
}

BuildACar.prototype.getArrayIndexFromName = function (strName, aList) {
  intReturn = -1;
  for (var x=0; x<aList.length; x++) {
    if (aList[x].strName == strName) intReturn = x;
  }
  return intReturn;
}

   
BuildACar.prototype.addItem = function (strName, strClass, strRetail, strVB, strPort, bSelected) {
  intIndex = this.feature.length;
  this.feature[intIndex]= new Feature;
  this.feature[intIndex].strName    = strName;
  this.feature[intIndex].strClass   = strClass;
  this.feature[intIndex].strRetail  = strRetail;
  this.feature[intIndex].strVB      = strVB;
  this.feature[intIndex].strPort    = strPort;
  this.feature[intIndex].bSelected  = bSelected;
}

BuildACar.prototype.setSelect = function (strItemName, bSelected) {
  intIndex = this.getIndexFromName(strItemName);
  if (intIndex > -1) this.feature[intIndex].bSelected = bSelected;
  document.getElementById("spnOptionRetail").childNodes.item(0).nodeValue = this.getOptionTotal("RETAIL");
  //document.getElementById("spnOptionVB").childNodes.item(0).nodeValue = this.getOptionTotal("VB");
  document.getElementById("spnOptionPort").childNodes.item(0).nodeValue = this.getOptionTotal("PORT");

  document.getElementById("spnTotalRetail").childNodes.item(0).nodeValue = this.getGrandTotal("RETAIL");
  //document.getElementById("spnTotalVB").childNodes.item(0).nodeValue = this.getGrandTotal("VB");
  document.getElementById("spnTotalPort").childNodes.item(0).nodeValue = this.getGrandTotal("PORT");
}

BuildACar.prototype.reCalculate = function () {
  if (document.getElementById("spnOptionRetail")) {
    var agt = navigator.userAgent.toLowerCase();
//  document.getElementById("spnOptionTotal").innerHTML = document.getElementById("spnOptionTotal").innerHTML;
    document.getElementById("spnOptionRetail").childNodes.item(0).nodeValue = this.getOptionTotal("RETAIL");
    //document.getElementById("spnOptionVB").childNodes.item(0).nodeValue = this.getOptionTotal("VB");
    document.getElementById("spnOptionPort").childNodes.item(0).nodeValue = this.getOptionTotal("PORT");
    
    document.getElementById("spnTotalRetail").childNodes.item(0).nodeValue = this.getGrandTotal("RETAIL");
    //document.getElementById("spnTotalVB").childNodes.item(0).nodeValue = this.getGrandTotal("VB");
    document.getElementById("spnTotalPort").childNodes.item(0).nodeValue = this.getGrandTotal("PORT");
  }
}
   
BuildACar.prototype.select = function (strItemName) {
  intIndex = this.getIndexFromName(strItemName);
  if (intIndex > -1) this.feature[intIndex].bSelected = true;
}

BuildACar.prototype.getIndexFromName = function (strName) {
  intReturn = -1;
  for (var x=0;x<this.feature.length;x++) {
    if (this.feature[x].strName == strName) intReturn = x;
  }
  return intReturn;
}

BuildACar.prototype.deSelect = function (strItemName) {
  intIndex = this.getIndexFromName(strItemName);
  if (intIndex > -1) this.feature[intIndex].bSelected = false;
}

BuildACar.prototype.list = function () {
  strMsg = ""
  for (var x=0;x<this.feature.length;x++) {
    strMsg += x;
    strMsg += "|" + this.feature[x].strName;
    strMsg += "|" + this.feature[x].strRetail;
    strMsg += "|" + this.feature[x].strVB;
    strMsg += "|" + this.feature[x].strPort;
    strMsg += "|" + this.feature[x].bSelected + "\n";
  }
  alert(strMsg)     
}

BuildACar.prototype.listSelected = function () {
  strMsg = ""
  for (var x=0;x<this.feature.length;x++) {
    if (this.feature[x].bSelected) {
      strMsg += x;
      strMsg += "|" + this.feature[x].strName;
      strMsg += "|" + this.feature[x].strRetail;
      strMsg += "|" + this.feature[x].strVB;
      strMsg += "|" + this.feature[x].strPort;
      strMsg += "|" + this.feature[x].bSelected + "\n";
    }
  }
  alert(strMsg)     
}

BuildACar.prototype.getOptionTotal = function (strColumn) {
  //alert("strColumn = "+strColumn);
  intReturn = 0;
  //alert(this.feature.length);
  for (var x=0;x<this.feature.length;x++) {
    if (this.feature[x].bSelected && this.feature[x].strClass=="OPTION") {
      switch (strColumn) {
        case "RETAIL":
          intReturn += parseInt(this.feature[x].strRetail,10);
          break;
      //  case "VB":
      //    intReturn += parseInt(this.feature[x].strVB,10);
      //    break;
        case "PORT":
          //alert(this.feature[x].strSell);
          intReturn += parseInt(this.feature[x].strPort,10);
          break;
      }
    }
  }
  switch (strColumn) {
    case "RETAIL":
  /** If they are on the quote page, then fill the option total on sidebar with the real option total **/
      if(document.getElementById("theOptionRetail")) {
        theOptionRetail = document.getElementById("theOptionRetail").value;
        return theOptionRetail;
      }
      break;
    //case "VB":
  /** If they are on the quote page, then fill the option total on sidebar with the real option total **/
    //  if(document.getElementById("theOptionVB")) {
    //    theOptionVB = document.getElementById("theOptionVB").value;
    //    return theOptionVB;
    //  }
    //  break;
    case "PORT":
  /** If they are on the quote page, then fill the option total on sidebar with the real option total **/
      if(document.getElementById("theOptionPort")) {
        theOptionPort = document.getElementById("theOptionPort").value;
        return theOptionPort;
      }
      break;
  }
  return CommaFormatted(intReturn);
}

BuildACar.prototype.getGrandTotal = function (strColumn) {
  intReturn = 0;
  for (var x=0;x<this.feature.length;x++) {
    if (this.feature[x].bSelected) {
      switch (strColumn) {
        case "RETAIL":
          intReturn += parseInt(this.feature[x].strRetail,10);
    /** If they are on the quote page, then fill the total on sidebar with the real total **/
          if(document.getElementById("theTotalRetail")) {
            theTotalRetail = document.getElementById("theTotalRetail").value;
            return theTotalRetail;
          }
          break;
        //case "VB":
        //  intReturn += parseInt(this.feature[x].strVB,10);
  /** If they are on the quote page, then fill the total on sidebar with the real total **/
        //  if(document.getElementById("theTotalVB")) {
        //    theTotalVB = document.getElementById("theTotalVB").value;
        //    return theTotalVB;
        //  }
        //  break;
        case "PORT":
          intReturn += parseInt(this.feature[x].strPort,10);
  /** If they are on the quote page, then fill the total on sidebar with the real total **/
          if(document.getElementById("theTotalPort")) {
            theTotalPort = document.getElementById("theTotalPort").value;
            return theTotalPort;
          }
          break;
      }
    }
  }
  return CommaFormatted(intReturn);
}
   
function CommaFormatted(number) {
  number = '' + number;
  if (number.length > 3) {
    var mod = number.length % 3;
    var output = (mod > 0 ? (number.substring(0,mod)) : '');
    for (i=0 ; i < Math.floor(number.length / 3); i++) {
      if ((mod == 0) && (i == 0)) {
        output += number.substring(mod+ 3 * i, mod + 3 * i + 3);
        alert(output);
      } else {
        output+= ',' + number.substring(mod + 3 * i, mod + 3 * i + 3);
      }
      return (output);
    }
  } else { return number; }
}

BuildACar.prototype.InteriorColorTableStart = function () {
  var strOutput = "";
  // wrapper table for interior colors
  strOutput += "<table width='100%' border= '1'>";
  return strOutput;
}

BuildACar.prototype.InteriorColorTableEnd = function () {
  var strOutput = "";
  strOutput += "</table>"
  return strOutput;
}

BuildACar.prototype.InteriorColorAdd = function (strId, strValue, strDescription) {
  var strOutput = "";
  // outer row for an interior color
  strOutput += "<tr class=''>";
  // color swatch column
  strOutput += "<td colspan='1'>";

  // output the color swatch as a table
  strOutput += "<table class='colorSwatch' cellspacing='0' cellpadding='0' border='0'>"
  strOutput += "<tbody>"
  // spacer row
  strOutput += "<tr>"
  strOutput += "<td><img height='1' src='../images/Blank.gif' width='1'></td>"
  strOutput += "<td bgcolor='#000000'><img height='1' src='../images/Blank.gif' width='1'></td>"
  strOutput += "<td><img height='1' src='../images/Blank.gif' width='1'></td>"
  strOutput += "</tr>"
  // interior color swatch row
  strOutput += "<tr>"
  strOutput += "<td bgcolor='#000000'><img height='1' src='../images/Blank.gif' width='1'></td>"
  strOutput += "<td bgcolor='" + strValue + "'><a href='javascript:configClick('208')'><img height='22' alt='Click to select this color.' src='../images/Blank.gif' width='44' border='0'></a></td>"
  // strOutput += "<td bgcolor='" + strValue + "'><img height='22' alt='Click to select this color.' src='../images/Blank.gif' width='44' border='0'></td>"
  strOutput += "<td bgcolor='#000000'><img height='1' src='../images/Blank.gif' width='1'></td>"
  strOutput += "</tr>"
  // spacer row   
  strOutput += "<tr>"
  strOutput += "<td><img height='1' src='../images/Blank.gif' width='1'></td>"
  strOutput += "<td bgcolor='#000000'><img height='1' src='../images/Blank.gif' width='1'></td>"
  strOutput += "<td><img height='1' src='../images/Blank.gif' width='1'></td>"
  strOutput += "</tr>"
  strOutput += "</tbody>"
  strOutput += "</table>"
  strOutput += "</td>"
  // radio button column
  // strOutput += "<td colspan='1' valign='middle'><input type='radio' name='fldInteriorColor' value='#222222' onclick='fnSetInteriorColor('#222222',\"" + strDescription + "\");'></td>"
  strOutput += "<td colspan='1' valign='middle'><input type='radio' name='fldInteriorColor' value='#222222' onclick='fnSetInteriorColor(\"#222222\",\"" + strDescription + "\");'></td>"
  // description column
  strOutput += "<td colspan='1' valign='top'>"
  // description table
  strOutput += "<table border='0' width='100%'>"
  strOutput += "<tbody>"
  strOutput += "<tr class='bodycopy'>"
  strOutput += "<td></td>"
  strOutput += "</tr>"
  strOutput += "<tr class='bodycopy'>"
  strOutput += "<td>" + strDescription + "</td>"
  strOutput += "</tr>"
  strOutput += "</tbody>"
  strOutput += "</table>"
  strOutput += "</td>"
  strOutput += "</tr>"

  return strOutput;
}  
