function round_to_2(num){
  try { 
    return Math.round(num*100)/100; 
  } catch (ignored){}
  return 0.00;
}

document.observe("dom:loaded", function() {
  $$("textarea.limited").each(function(ta) {checkCharacterLimit(ta)});
  
  $$(".new_compost").invoke('observe', 'submit', function(e) {
    btn = this.down(".button").down("button");
    if(this.down("textarea.limited").value == "") {
      e.stopPropagation();
      return false;
    } else {
      btn.disabled = true;
      btn.setStyle("opacity: 0.31;")
      return true;
    }
  });
});

function checkCharacterLimit(target) {
  resetCharacterLimit(target);
  
  target.observe('keyup', function() {
    resetCharacterLimit(target);
    
    chars = $F(target).length
    if(chars > limit) {
      button = target.next().down("button");
      button.down("img").hide();
      button.down("p").show();
      
      counter.addClassName("over");
    } else {
      button = target.next().down("button");
      button.down("img").show();
      button.down("p").hide();
      
      counter.removeClassName("over");
    }

    counter.update(limit - chars);
  });
}

function resetCharacterLimit(target) {
  limit = target.readAttribute('title');
  counter = target.next().down(".counter");
  counter.update(limit);
}
