﻿// create a object called RenderParameter that accepts id, colorid, and type as parameters
function RenderParameter(id, colorID, type, price, parentID)
{
  // assign id to RenderParameter property ID
  this.ID = id;
  // assign colorID to RenderParameter property colorID
  this.colorID = colorID;
  // assign type to RenderParameter property type
  this.type = type;
  // assign price to RenderParameter property price
  this.Price = price;
  
  this.parentID = parentID
}

// create variables
var _modelID;
var _compositionID;
var _viewID = 0;
var _hideHardware = false;
var _renderType = 0;
var _msrp;
var _optionArray = new Array();

// adds a option to the array of options and display the image
function SetOption(optionID, colorID, typeID, price, parentID, singleSelectionOnly)
{
  //alert("begin with: " + GetOptions(1));
  var add = "true";
  //alert(optionID + " " + colorID + " " + typeID + " " + price + " " + parentID + " " + singleSelectionOnly);
  if(singleSelectionOnly == 'false')
  {
    //alert("add 1");
    PreRenderOptions(optionID, colorID, typeID, price, parentID);
    //Display();
    add = false;
  }
  else
  {
    for(var i = 0; i < _optionArray.length; i++)
    {
      if(_optionArray[i].parentID == parentID)
      {
        //alert("add 2");
        //alert("add 2 before:" + GetOptions(1) + " categoryID:" + parentID + " item to be replace:" + _optionArray[i].ID);
        var option = new RenderParameter(optionID, colorID, typeID, price, parentID);
        _optionArray[i] = option;
        //alert("featureID:" + _optionArray[i].ID + " color:" + _optionArray[i].colorID + " parentID:" + _optionArray[i].parentID);
        //alert("add 2 after:" + GetOptions(1));
        //Display();
        add = "false";
        break;
      }
    }
  }
  if(add == "true")
  {
    PreRenderOptions(optionID, colorID, typeID, price, parentID);
  }
  Display();
  DisplayOptionTotal();
  //alert("End with: " + GetOptions(1));
}

function AddFeature(featureID, categoryID, colorID, price, singleSelectionOnly)
{
  //alert("Here");
  SetOption(featureID, colorID, 2, price, categoryID, singleSelectionOnly);
}

// assigns the _viewID and displays the image
function ChangeView(viewID)
{
  _viewID = viewID;
  if(_viewID == 4)
  {
    _hideHardware = true;
  }
  else
  {
    _hideHardware = false;
  }
  Display();
}

function ClearColorAreas()
{
  var newOptionArray = new Array();
  try {
  //_optionArray = newOptionArray;
  } catch (err) { alert(err.discription + " ClearColorAreas"); }
  //alert("clear color areas");
}

// assign the image src and link href
function Display()
{
  var queryStringBuilder = '';
  try {
  //alert(GetOptions(1));
  queryStringBuilder = _modelID.toString() + "&compositionID=" + _compositionID.toString() + "&view=" + _viewID + "&colorAreas=" + GetOptions(1) + "&features=" + GetOptions(2);// + "&HideHardware=" + _hideHardware.toString();
  queryStringBuilder = queryStringBuilder + "&renderType=" + _renderType;
  } catch (err) { alert("Dipslay - QueryStringBuilder:" + err.discription); }
  //alert(_renderType);
  
  //if(document.getElementById("ctl00_ContentPlaceHolder1_IspecImage1_uiBoatImage") != null)
  if(document.getElementById("uiBoatImage") != null)
  {
    //var img = document.getElementById("ctl00_ContentPlaceHolder1_IspecImage1_uiBoatImage");
    // TODO: top line of code is old code.
    // bottom line is new code to convert back to old code must use old code
    // in IspecImage.aspx as well
    var img = document.getElementById("uiBoatImage");
    //alert(queryStringBuilder);
    img.src = "ShowIspec.aspx?modelID=" + queryStringBuilder;
    //alert(img.src);
  }
  
  if(document.getElementById("ctl00_SidBarContent_uiSummaryImage") != null)
  {
    var img = document.getElementById("ctl00_SidBarContent_uiSummaryImage");
    img.src = "ShowIspec.aspx?modelID=" + queryStringBuilder + "&width=188&height=128";
    //alert(queryStringBuilder);
  }
  //alert("here");
}

function CompositionChange(compID)
{
  _compositionID = compID;
}

// pre loads the model information when the page loads.
function PreLoadModelComposition(modelID, compositionID, view, hideHardware, renderType)
{
  try {
  //alert(modelID + " " + compositionID + " " + view + " " + hideHardware + " rtype:" + renderType);
  if(_modelID != modelID)
  {
  _modelID = modelID;
  _compositionID = compositionID;
  _renderType = renderType;
  if(view == 0)
  {
    _viewID = 2;
  }
  else
  {
    _viewID = view;
  }
  }
  Display();
  } catch (err) { alert("ERROR"); }
}

 //adds the option to the list
function PreRenderOptions(optionID, colorID, typeID, price, parentID)
{
  //alert(price);
  // add flag
  var add = true;
  // create variable option as RenderParameter and pass it the needed fields
  var option = new RenderParameter(optionID, colorID, typeID, price, parentID);

  // loop the array
  for(var i=0; i <= _optionArray.length - 1; i++)
  {
    // check to see if option exsist in the array
    if(_optionArray[i].ID == optionID)
    {
      // add option to the list at location i
      //alert(optionID + ':' + colorID + ':' + price);
      _optionArray[i] = option;
      // set add flag to false
      add = false;
      // break out of for loop
      break;
    }
  }
  // if add flag is true then option is a new option and needs to be added to the array
  if(add)
  {
    //alert(optionID + ':' + colorID + ':' + price);
    // add new option to the array.
    _optionArray[_optionArray.length] = option;
  }
  
  //alert("While Adding: " + GetOptions(1));
}

// get options given a type (1 = color areas, 2 = features)
function GetOptions(type)
{
  // return option string
  var optionString = "";
  // loop _optionArray
  for(var i=0; i <= _optionArray.length - 1; i++)
  {
    // if _hideHardware is true then hide hardware (right now just towers) - not used.
    if(_hideHardware)
    {
      // if the option at location [i] is of the given type then append [id],[colorID]| to optionString
      if(_optionArray[i].type == type && _optionArray[i].ID != 3306 && _optionArray[i].ID != 3307 && _optionArray[i].ID != 3308 && _optionArray[i].ID != 19269)
      {
        var colorID = 0;
        if(_optionArray[i].colorID != 0)
          colorID = _optionArray[i].colorID;
        optionString = optionString + _optionArray[i].ID + "," + colorID + "|";
      }
    }
    // _hideHardware is false don't hide hardware
    else
    {
      // if the option at location [i] is of the given type then append [id],[colorID]| to optionString
      if(_optionArray[i].type == type)
      {
        var colorID = 0;
        if(_optionArray[i].colorID != 0)
          colorID = _optionArray[i].colorID;
        optionString = optionString + _optionArray[i].ID + "," + colorID + "|";
      }
    }
  }
  // remove the last char from optionString and return the string
  return optionString.substring(0, optionString.length - 1);
}

function LoadBoatCost(modelCost, compositionCost, msrp)
{
  //alert(modelCost + " " + compositionCost);
  _boatCost = parseFloat(modelCost) + parseFloat(compositionCost);
  _msrp = msrp;
  DisplayOptionTotal();
}

function DisplayOptionTotal()
{
  var boatCostDisplay = xGetElementById("uiBoatCost");
  var optionTotalDisplay = xGetElementById("uiOptionsTotalPrice");
  var totalDisplay = xGetElementById("uiTotal");
  
  var optionTotal = 0.00;
  var total = 0.00;
  
  //alert(_msrp);
  
  for(var oCounter = 0; oCounter < _optionArray.length; oCounter++)
  {
    if(_optionArray[oCounter].ID == 37687 && _msrp != 1)
    {
      optionTotal = optionTotal + Math.round((parseFloat(_optionArray[oCounter].Price) / parseFloat('.8')));
      //alert(Math.round((parseFloat(_optionArray[oCounter].Price) / parseFloat('.8'))));
    }
    else
    {
      optionTotal = optionTotal + Math.round((parseFloat(_optionArray[oCounter].Price) / _msrp));
    }
    //optionTotal = optionTotal + parseFloat(_optionArray[oCounter].Price);
    //if(_optionArray[oCounter].Price != 0)
      //alert(_optionArray[oCounter].ID + " " + _optionArray[oCounter].Price);
    //alert(_optionTotal);
  }
  //alert(optionTotal);
  
//  if(_modelID != 471 && _modelID != 472)
//    total = optionTotal + Math.round((parseFloat(_boatCost) / _msrp));
//  else
    total = optionTotal + parseFloat(_boatCost);
   
  
  if(optionTotalDisplay != null)
  {
    //alert(optionTotalDisplay.innerHTML);
    optionTotalDisplay.innerHTML = '$' + CurrencyFormatted(optionTotal.toString());
  }
  if(totalDisplay != null)
  {
    //alert(totalDisplay.id);
    totalDisplay.innerHTML = '$' + CurrencyFormatted(total.toString());
  }
  if(boatCostDisplay != null)
  {
    if(_msrp != 1)
    {
      if(_modelID != 471 && _modelID != 472)
        boatCostDisplay.innerHTML = '$' + CurrencyFormatted((Math.round(_boatCost)).toString());
      else
        boatCostDisplay.innerHTML = '$' + CurrencyFormatted(_boatCost.toString());
      
    }
    else
    {
      boatCostDisplay.innerHTML = '$' + CurrencyFormatted((Math.round(_boatCost)).toString());
    }
  }
}

function CurrencyFormatted(amount)
{
  var delimiter = ","; // replace comma if desired
  var a = amount.split('.',2)
  var d = a[1];
  var i = parseInt(a[0]);
  if(isNaN(i)) { return ''; }
  var minus = '';
  if(i < 0) { minus = '-'; }
  i = Math.abs(i);
  var n = new String(i);
  var a = [];
  while(n.length > 3)
  {
	  var nn = n.substr(n.length - 3);
	  a.unshift(nn);
	  n = n.substr(0, n.length - 3);
  }
  if(n.length > 0) { a.unshift(n); }
  n = a.join(delimiter);
  if(isNaN(d)) { amount = n + ".00"; }
  else { amount = n + '.' + d; }
  amount = minus + amount;
  return amount;

}