// captcha protection for emails:
// usage:
/*
  <script type="text/javascript" src="/js/scriptaculous/prototype.js"></script>
  <script type="text/javascript" src="/js/scriptaculous/scriptaculous.js"></script>
  <script type="text/javascript" src="/js/cpatcha.js"></script>

  <style type="text/css" media="all">
  #captcha {
    position: absolute;
    background: rgb(245,245,245);
    width: 200px;
    text-align: left;
    border: 1px solid #d33539;
    padding: 5px;
    cursor: pointer;
  }

  #captcha input {
   border: 1px solid rgb(100,100,100);
   width: 120px;
   font-size: 1em;
   padding: 1px 3px 1px 3px;
  }

  #captcha p.center, #captcha form {
   text-align: center;
  }  
  </style>

  <!-- encode adress with ?type=encode&id=email@domain.com -->

  <!-- replace all mailto-links with sometning like that -->
  <a href="javascript:captcha('U2FsdGVkX1_PJ8wJIg_s6_XoMUl5B_0eyGgOp4_wQZU');">click2</a>

  <!-- put this div somewhere -->
  <div id="captcha" style="display: none;"></div>

*/

// config
var captcha_url      = '/cgi-bin/captcha.pl';
var captcha_div_id   = 'captcha';
var captcha_input_id = 'captcha_input';


// texts
var close_button = '<img style="float: right; cursor: pointer;" onclick="captcha(0);" src="/images/icn-close.png">';

function form_text(id,rnd) // show form
{
      return close_button
             //+ '<p>Zur Freischaltung der Adresse tippen Sie den Code ab und best&auml;tigen '
             //+ 'mit der Eingabetaste.</p>'
             + '<p>To unprotect my E-mail address, type the shown code and hit enter.</p>'
             + '<p class="center"><img style="cursor: pointer;" onclick="captcha(\'' + id + '\', \'' + Math.random() + '\');" src="'
             +    captcha_url + '?type=image&id=' + id + '&rnd=' + rnd + '"></p>'
             + '<form onsubmit=" return decodeAdress(\'' + id + '\',\'' + rnd + '\');">'
             + '   <input id="captcha_input" type="text" name="c"></form>';
}

function success_text(email) // show success (email)
{
      return close_button
             //+ '<p>Die E-Mail Adresse lautet: '
             + '<p>Mail E-Mail address is: '
             + '<a href="mailto:' + email + '">' + email + '</a></p>';
}

function error_text(id,rnd) // show error message and form again
{
      return close_button
             //+ '<p>Ihre Eingabe war falsch. Versuchen Sie es noch einmal.</p>'
             + '<p>Your input was wrong. Try again.</p>'
             + '<p class="center"><img style="cursor: pointer;" onclick="captcha(\'' + id + '\', \'' + Math.random() + '\');" src="'
             +     captcha_url + '?type=image&id=' + id + '&rnd=' + rnd + '"></p>'
             + '<form onsubmit=" return decodeAdress(\'' + id + '\',\'' + rnd + '\');">'
             + '   <input id="captcha_input" type="text" name="c"></form>';
}

// main function
function captcha(id,rnd)
{
      var captcha = document.getElementById(captcha_div_id);
      if (!captcha) alert("no captcha div defined");

      // hide captcha
      if (captcha.style.display = 'block')
      {
        captcha.innerHTML = '';
        captcha.style.display = 'none';
      }

      // create captcha
      if (id) {
        captcha.innerHTML = form_text(id,rnd);
        captcha.style.display = 'block';
        document.getElementById(captcha_input_id).focus();
      }

}

// handle decoding via ajax
function decodeAdress(id,rnd)
{     
      var captcha = document.getElementById(captcha_div_id);
      if (!captcha) alert("no captcha div defined");

      var handlerFunc = function(t) {
        if(t.responseText == 0) {
          captcha.innerHTML = error_text(id,rnd);
        } else {
          captcha.innerHTML = success_text(t.responseText);
        }
      }

      var text = document.getElementById(captcha_input_id).value;

      new Ajax.Request(captcha_url, {
        parameters: 'type=ajax&id=' + id + '&c=' + URLencode(text) + '&rnd=' + rnd,
        onSuccess: handlerFunc
      });

      return false;
}

// helper
function URLencode(sStr) {
  return escape(sStr).
    replace(/\+/g, '%2B').
    replace(/\"/g,'%22').
    replace(/\'/g, '%27').
    replace(/\//g,'%2F');
}

