﻿var Compare = {
  checkboxes: null,
  numberChecked: 0,
  check: function() {
    this.numberChecked++;
    if (this.numberChecked >= 5) {
      this.disableCheckboxes();
    }
  },
  uncheck : function() {
    this.numberChecked--;
    if (this.numberChecked <= 4) {
      this.enableCheckboxes();
    }
  },
  disableCheckboxes: function () {
    this.checkboxes.each(function () {
      if (!this.checked) {
        this.disabled = true;
      }
    });
  },
  enableCheckboxes: function () {
    this.checkboxes.each(function () {
      if (!this.checked) {
        this.disabled = false;
      }
    });
  },
  compareProducts: function() {
    var productIds = "";
    var first = true;
    this.checkboxes.each(function () {
      if (this.checked) {
        if (!first) {
          productIds += ",";
        }
        productIds += this.value;
        first = false;
      }
    });
    window.location = Globals.ApplicationPath + "/product-compare/products/" + productIds;
  }
};

$(document).ready(function() {
  Compare.checkboxes = $(":checkbox");
  Compare.checkboxes.change(function() {
    if (this.checked) {
      Compare.check();
    }
    else {
      Compare.uncheck();
    }
  });
  // Initialize the numberChecked value for people that click the back button...
  $(":checkbox:checked").each(function() {
    Compare.numberChecked++;
  });
  if (Compare.numberChecked >= 5)
  {
      this.disableCheckboxes();
  }
});