var Options = new Object()
var System = new System()

function System () {
  var optionName // name of option to be updated
  var optionId // numeric id of option to be updated

  this.changeOption = function (optionName, optionId) {
    this.updateOptionName = optionName
    this.updateOptionId = optionId

    this.updatePrice()
    this.updateLease()
    this.updateStickies() 
    this.updateSummary()
    this[optionName] = optionId
  }

  this.updatePrice = function () {
    var priceElement = document.getElementById("price")

    // find the last option object
    if (this[this.updateOptionName]) {
      previousOptionId = this[this.updateOptionName]
    } else {
      previousOptionId = 0
    }

    // calculate new price
    previousOptionPrice = Options[this.updateOptionName][previousOptionId][2]
    currentOptionPrice = Options[this.updateOptionName][this.updateOptionId][2]
    this.currentPrice = this.currentPrice - previousOptionPrice + currentOptionPrice

    // update price
    priceElement.innerHTML = "$"+number_format(this.currentPrice, 0, '.', ',')
  }

  this.updateLease = function () {
    var leaseElement = document.getElementById("lease")
    var leasePrice = document.getElementById("leasePrice")

    // figure out what lease rate to use
    for (var i in leaseRates) {
      if (leaseRates[i][0] <= this.currentPrice && leaseRates[i][1] >= this.currentPrice) {
        leaseElement.style.display = "block"
        this.currentLease = this.currentPrice / leaseRates[i][2];
        break;
      } else {
        leaseElement.style.display = "none"
        this.currentLease = 0
      }
    }

    // update lease
    leasePrice.innerHTML = "$"+number_format(this.currentLease, 0, '.', ',')
  }

  this.updateStickies = function () {
    for (var i in Options[this.updateOptionName]) {
      var stickyElement = document.getElementById(this.updateOptionName+"-"+i+"-sticky")
      var newCost = Options[this.updateOptionName][i][2] - Options[this.updateOptionName][this.updateOptionId][2]

      if (i == this.updateOptionId) {
        stickyElement.innerHTML = ""
      } else if (newCost > 0) {
        stickyElement.innerHTML = "[Add $"+number_format(newCost, 0, '.', ',')+"]"    
      } else if (newCost < 0) {
        stickyElement.innerHTML = "[Subtract $"+number_format(Math.abs(newCost), 0, '.', ',')+"]" 
      }
    }
  }
  
  this.updateSummary = function () {
    var summaryColumnElement = document.getElementById(this.updateOptionName+"-summary-column")
    var summaryRowElement = document.getElementById(this.updateOptionName+"-summary-row")

    if (summaryColumnElement) {
      summaryColumnElement.innerHTML = Options[this.updateOptionName][this.updateOptionId][1]
  
      if (Options[this.updateOptionName][this.updateOptionId][3] == 0 && this.updateOptionId == 0) {
        summaryRowElement.style.display = "none"
      } else {
        try {
          summaryRowElement.style.display = "table-row"
        } catch(e) {
          summaryRowElement.style.display = "block"
        }
      }
    }
  }
}

function number_format (a, b, c, d) {
	// number_format(number, decimals, comma, formatSeparator)
	a = Math.round(a * Math.pow(10, b)) / Math.pow(10, b);
	e = a + '';
	f = e.split('.');
	if(!f[0]) f[0] = '0';
	if(!f[1]) f[1] = '';
	if(f[1].length < b){
		g = f[1];
		for(i = f[1].length + 1; i <= b; i++) {
			g += '0';
		}
		f[1] = g;
	}
	if(d != '' && f[0].length > 3) {
		h = f[0];
		f[0] = '';
		for(j = 3; j < h.length; j += 3) {
			i = h.slice(h.length - j, h.length - j + 3);
			f[0] = d + i +  f[0] + '';
		}
		j = h.substr(0, (h.length % 3 == 0) ? 3 : (h.length % 3));
		f[0] = j + f[0];
	}
	c = (b <= 0) ? '': c;
	return f[0] + c + f[1];
}
