window.onload = function() {
  setInitialFigures();
  self.$("addButton").onclick = function() {
    submitSum();
  }
}

function setInitialFigures() {

  figuresRE = /#([-]*[0-9]*),([-]*[0-9]*),([-]*[0-9]*)/;
  figuresSpec = window.location.hash;
  if (!figuresRE.test(figuresSpec)) {
    return; // ignore url if invalid
  }
  $("figure1").value = figuresSpec.replace(figuresRE, "$1");
  $("figure2").value = figuresSpec.replace(figuresRE, "$2");
  $("figure3").value = figuresSpec.replace(figuresRE, "$3");

  submitSum();

}

function submitSum() {
  definedFigures = {
    figure1: $("figure1").value,
    figure2: $("figure2").value,
    figure3: $("figure3").value
  }
  hash =     "#" + definedFigures.figure1 + "," + definedFigures.figure2
           + "," +definedFigures.figure3;
  window.location.hash = hash;
  ajaxCaller.get("sum.phtml", definedFigures, onSumResponse, false, null);
}

function onSumResponse(text, headers, callingContext) {
  self.$("sum").innerHTML = text;
}
