$(document).ready(function() {

  var form = $('#prod_detail_form');
  var prod_rn = $(form).find("input:hidden[@name='prod_rn']").val();

  if (form && prod_rn) {
    // This markup will not be present if base price = 0, so add it.
    $(form).find("#price_" + prod_rn).html("<span class=\"price_label\"></span> " + js_options['currency'] + "<span class=\"base_price\"></span>");

    update_price(form, prod_rn);

    $(form).find("select.product_option").each(function() {
      var element = $(this);
      update_option(form, prod_rn, element);
      element.change(function() {
        update_option(form, prod_rn, element);
      });
    });
  }
});

function update_option(form, prod_rn, element) {
  var name = element.attr('name');
  var selected = element.val();

  price = update_price(form, prod_rn);

  // Then update the changed select
  if (js_options['products'][prod_rn]['options'][name]) {
    var options = '';

    price_without = price - js_options['products'][prod_rn]['options'][name][selected]['price_modifier'];

    $.each(js_options['products'][prod_rn]['options'][name], function(key, value) {
      var option_title = value.name == null ? "Choose One" : value.name;
      option_price = price_without + value.price_modifier;
      option_modifier = option_price;// - price;
      var modifier_string = (option_modifier != 0) ? ' (' + js_options['currency'] + add_commas(DA2(Math.abs(option_modifier),2)) + ')' : '';
      options += '<option value="' + key + '">' + option_title + modifier_string + '</option>';
    });

    element.html(options);
    setTimeout(function() {element.val(selected)}, 10);
  }
}

function update_price(form, prod_rn) {
  var price = js_options['products'][prod_rn]['base_price'];
  // First update the displayed price based on all options
  $(form).find("select.product_option").each(function() {
    var e2 = $(this);
    var n2 = e2.attr('name');
    var s2 = $(this).val();
    price += js_options['products'][prod_rn]['options'][n2][s2]['price_modifier'];
  });

  var has_options = $(form).find("select.product_option").length > 0;

  if (has_options) {
    price_text = "Selected Price: ";
  } else {
    price_text = "Price: ";
  }

  // Set the displayed price
  if (price > 0) {
    $(form).find("#price_" + prod_rn).show();
    $(form).find("#price_" + prod_rn + " .price_label").html("<b>" + price_text + "</b>");
    $(form).find("#price_" + prod_rn + " .base_price").text(add_commas(DA2(price, 2)));
  } else {
    $(form).find("#price_" + prod_rn).hide();
  }

  return price;
}

Number.prototype.toFixxed = function (f) {
    f = parseInt(f / 1 || 0);
    if (f < 0 || f > 20) {
        alert("The number of fractional digits is out of range");
    }
    if (isNaN(this)) {
        return "NaN";
    }
    var s = this < 0 ? "-" : "", x = Math.abs(this);
    if (x > Math.pow(10, 21)) {
        return s + x.toString();
    }
    var m = Math.round(x * Math.pow(10, f)).toString();
    if (!f) {
        return s + m;
    }
    while (m.length <= f) {
        m = "0" + m;
    }
    return s + m.substring(0, m.length - f) + "." + m.substring(m.length - f);
}

function DA2(X, N) {
    return (new Number(X)).toFixxed(N);
}

function add_commas(nStr)
{
	nStr += '';
	x = nStr.split('.');
	x1 = x[0];
	x2 = x.length > 1 ? '.' + x[1] : '';
	var rgx = /(\d+)(\d{3})/;
	while (rgx.test(x1)) {
		x1 = x1.replace(rgx, '$1' + ',' + '$2');
	}
	return x1 + x2;
}
