
function updateCharCount(counter, field, maxchars) {
   counter = $(counter);
   field = $(field);
   maxchars = parseInt(maxchars);
   var len = field.value.length;
   if (len >= maxchars) {
    alert("You've exceeded the maximum amount of characters you're allowed to type into the comment area");
   }
 counter.update(len + ((len == 1) ? " character" : " characters"));
}


 function submitUserComment(url) {

    new (Ajax.Request)(url, {
      method: "post",
      parameters: {
        ajaxrequest: true,
        maxchars: $F("maxchars"),
        scripturl: $F("scripturl"),
        siteid: $F("siteid"),
        address: $F("address"),
        comment_email: $F("comment_email"),
        comment_name: $F("comment_name"),
        comment_text: $F("comment_text"),
        comment_captcha: $F('comment_captcha')
      },
      onSuccess: function (transport) {
        var json = new Hash(transport.responseJSON);

        if (json.get("status").toLowerCase() == "successful") {
            alert("Your comment was submitted and will appear online shortly");
            $("usercomment_form").reset();
            $("comment_char_counter").update("&nbsp;");
            $("usercomment_form").focusFirstElement();
        } else if (json.get("message") != "") {
            alert(json.get("message"));

            if (json.get("element") != "undefined") {
                $(json.get("element")).focus();
            } else {
                $("usercomment_form").focusFirstElement();
            }
        }
      },
      onFailure: function () {
            alert("An error occured while submitting the comment. Please notify the website administrator if it occurs again.");
      }
    });
 }

 function redrawSecurityImage() {
    $('img_securitycode').src = '/ajax/create_img.php?request=' + Math.floor(Math.random()*1001);
 }

 function onRatingsWidgetClick(event) {
   var parameterString = this.href.replace(/.*\?(.*)/, "$1");
   var parameterTokens = parameterString.split("&");
   var parameterList = new Array;
   for (j = 0; j < parameterTokens.length; j++) {
   var parameterName = parameterTokens[j].replace(/(.*)=.*/, "$1");
   var parameterValue = parameterTokens[j].replace(/.*=(.*)/, "$1");
   parameterList[parameterName] = parameterValue;
   }
   var theratingID = parameterList.q, theVote = parameterList.j, theuserIP = parameterList.t, theunits = parameterList.c;
   $("unit_ul" + theratingID).update("<div class=\"loading\"></div>");
   new (Ajax.Request)("/rpc_ratingwidget.php", {method: "post", parameters: {j: theVote, q: theratingID, t: theuserIP, c: theunits}, onSuccess: function (transport) {var resp = transport.responseText;if (resp.indexOf("|") != -1) {resp = resp.split("|");$(resp[0]).update(resp[1]);}}});
   Event.stop(event);
 }

Element.addMethods({
    insertAqui: function (element, val) {
      element = $(element);

      if (Prototype.Browser.IE) {
        element.focus();
        sel = document.selection.createRange();
        sel.text = val;
      } else if (Prototype.Browser.Gecko) {
        var startPos = element.selectionStart;
        var endPos = element.selectionEnd;
        element.value = element.value.substring(0, startPos) + val + element.value.substring(endPos, element.value.length);
      } else {
        element.value += val;
      }

      element.focus();
    }
});

