var t = new Template('(#{0}#{1}#{2}) #{3}#{4}#{5}-#{6}#{7}#{8}#{9}');
$$('input[name*="phone"]').each(function(elm){
  var masked;
  elm.observe('keyup', function(evt){
    elm.writeAttribute('maxlength','14');
    masked = t.evaluate(this.getValue().gsub(/[^\d]+/,'').toArray());
    var lastDigit = 0;
    masked.toArray().each(function(n, idx){ if(n.match(/\d/)) lastDigit = idx + 1; });
    this.setValue(masked.substring(0, lastDigit));
  });
});