//
//  giving.js
//

function setupTotalCalculation () {
  Event.observe($("giving"), "submit", updateGivingTotal);
  $$("table.fields input.money").each(function (field) {
    Event.observe(field, "keyup", updateGivingTotal);
    Event.observe(field, "blur", updateGivingTotal);
  });
  Event.observe($("other_text"), "keyup", updateGivingItemName);
  Event.observe($("other_text"), "blur", updateGivingItemName);
}

function setupPlaceholderFields () {
  $$("input.placeholder").each(function (field) {
    field.value = field.getAttribute("value");
    Event.observe(field, "focus", focusPlaceholderField);
    Event.observe(field, "blur", blurPlaceholderField);
  });
}

function setupFieldFormatters () {
  $$("table.fields input.money").each(function (field) {
    Event.observe(field, "blur", function () {
      this.value = numberToCurrency(this.value.gsub(/[^0-9\.]+/, ""));
    });
  });
}

function updateGivingTotal () {
  var amount, total = 0.0;
  $$("table.fields input.money").each(function (field) {
    if ((amount = parseFloat(field.value.gsub(/[^0-9\.]+/, ""))) > 0.0)
      total = total + amount;
  });
  $("total").value = numberToCurrency(total);
  $("amount").value = numberToCurrency(total).gsub(/^\$/, "");
  updateGivingItemName();
}

function updateGivingItemName () {
  var amount, fields = new Array();
  var template = new Template("#{name} #{amount}");
  
  $$("table.fields input.money").each(function (field) {
    if ((amount = parseFloat(field.value.gsub(/[^0-9\.]+/, ""))) > 0.0)
      fields.push(template.evaluate({
        name: fieldNameForField(field),
        amount: numberToCurrency(amount)
      }));
  });
  $("item_name").value = fields.join(" / ");
}

function fieldNameForField (field) {
  if (field.name == "other") {
    var name = $("other_text").value;
    if (name == "")
      return "Other";
    else return name;
  }
  var label = $(field.name + "_label");
  return label.innerHTML.unescapeHTML();
}

function focusPlaceholderField () {
  if (this.value == this.getAttribute("value")) {
    this.value = "";
    this.removeClassName("placeholder");
  }
}

function blurPlaceholderField () {
  if (this.value == "") {
    this.value = this.getAttribute("value");
    this.addClassName("placeholder");
  }
}

function numberToCurrency (number) {
  var parts = numberWithPrecision(number, 2).split(".");
  if (parts.length < 2) parts.push("00");
  return "$" + numberWithDelimiter(
    parts.first(), ",") + "." + parts.last();
}

function numberWithPrecision (number, precision) {
  var rounded = Math.round(number * 
    Math.pow(10, precision)) / Math.pow(10, precision);
  var parts = rounded.toString().split(".");
  
  var padding = "0".times(precision);
  if (parts.length < 2) parts.push(padding);
  
  var padded = (parts.last() + padding).substr(0, precision);
  return [parts.first(), padded].join(".");
}

function numberWithDelimiter (number, delimiter) {
  return number.toString().gsub(
    /(\d)(?=(\d\d\d)+(?!\d))/, "#{1}" + delimiter);
}

Event.observe(window, "load", setupTotalCalculation);

Event.observe(window, "load", setupPlaceholderFields);
Event.observe(window, "load", setupFieldFormatters);

