var captcha_answer = 0;
function generate_captcha() { // returns string to present to user; also sets captcha_answer to numerical result
  num1 = Math.floor(Math.random()*11);
  num2 = Math.floor(Math.random()*11);
  sum = num1 + num2;
  captcha_answer = sum;
  return "What is " + num1 + " + " + num2 + "?";
}
function check_captcha() {
  response = $('#captcha').attr('value');
  good = (parseInt(response) == captcha_answer);
  if (good) {
    $('#mailer #submit').removeAttr('disabled');
  } else {
    $('#mailer #submit').attr('disabled','disabled');
  }
}

$(document).ready(function() {
    $('#content a:not(.popupwindow)').filter(function() {
        var theHref = this;
        if (theHref.hostname && theHref.hostname !== location.hostname) {
            $(theHref).not(".noAutoIcon").addClass("offSite");
            $(theHref).not(".noAutoLink").attr('target','_blank').bind('click keypress', function(event) {
                var code=event.charCode || event.keyCode;
                if (!code || (code && code == 13)) {
                    if(pageTracker){
                        var fixedLink = this.href;
                        fixedLink = fixedLink.replace(/https?:\/\/(.*)/,"$1");
                        fixedLink = '/outgoing/' + fixedLink;
                        pageTracker._trackPageview(fixedLink);
                    };
                };
            });
        };
    }); // end jquery external links tracker for google analytics
    if (($('form[name="mailer[contact]"]').length == 1) || ($('form[name="mailer[contact2]"]').length == 1)) { // only on contact page, now!
      $('#mailer #submit').attr('disabled','disabled'); // if we're running, drop out the submit button but leave it there for later.
      $('#mailer textarea').attr('cols','80');    
      $('#mailer #captcha_tr').show(); // show html-hidden captcha row in table.
      $('#mailer #captcha_tr label').html(generate_captcha); // stuff label with captcha prompt
      //bind various change handlers to the text field:
      $('#mailer #captcha').keyup(check_captcha);
      $('#mailer #captcha').change(check_captcha);
      $('#mailer #captcha').click(check_captcha);
      $('#mailer #captcha').focus(check_captcha);
      $('#mailer #captcha').focusin(check_captcha);
      $('#mailer #captcha').focusout(check_captcha);
    }
});
