var http = getHTTPObject();
//var captchaURL = "captcha.pl";
var captchaURL = "http://www.smtptrap.com/cgi-bin/captcha.pl";
var ajaxPic = new Image(16,16);
ajaxPic.src="images/ajaxloader.gif";
var captchaimg = new Image(200, 50);
var button = '<input type="image" src="images/btn-ama.jpg" value="Display AMA Keys" onClick="captcha_check();">';

function display_button() {
   document.getElementById('reload').innerHTML = '<input type="image" src="images/btn-refresh.jpg" onClick="reload_captcha();" value="Refresh image">';
}

function reload_captcha() {
   document.getElementById('reload').innerHTML = '<img src="'+ ajaxPic.src +'" width=16, height=16>';
   // we use img.src for Firefox only, to avoid to have a 'broken' image
   // while loading the new captcha :
   if ( navigator.userAgent.match(/Firefox/i) ) {
      document.captchaimg.src = captchaURL + '?do=1&rand=' + Math.random();
   // chrome & co. don't refresh the image with the above method,
   // must use another way to do it :
   } else {
      document.getElementById('captcha').innerHTML = '<img src="'+ captchaURL + '?do=1&rand=' + Math.random() +'" width=200, height=50>';
   }
   // we must cheat : add a 1/2 second pause so that user can see
   // the AJAX loader and understand the captcha is being reloaded :
   setTimeout(display_button, 500)
   // clear user input as well :
   document.amaform.amacaptcha.value = '';
   document.amaform.amacaptcha.focus();
}

function captcha_check () {
   // ensure we have 6 letters/numbers ( case is insensitive ):
   if (! document.amaform.amacaptcha.value.match(/^[0-9A-Za-z]{6}$/) ) {
      alert("Please enter the 6 letters/numbers displayed above");
      document.amaform.amacaptcha.focus();
      return false;
   }
   // AJAX loader pic :
   document.getElementById('formbutton').innerHTML = '<img src="'+ ajaxPic.src +'" width=16, height=16>';
   // post to Perl script :
   http.open("POST", captchaURL, true);
   http.onreadystatechange = captcha_res;
   http.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
   http.send( 'do=2&item=' + document.amaform.amacaptcha.value );
}
function captcha_res() {
   if (http.readyState == 4){
      res = http.responseText;
      var tmp = new Array();
      tmp = res.split('|');
      // check for error :
      if ( tmp[0] == 'ERR' ) {
         alert( 'Error : ' + tmp[1] );
         document.getElementById('formbutton').innerHTML = button;
         // we reload the captcha as well :
         reload_captcha();
         return false;
      } else if ( tmp[0] == 'OK' ) {
         // hide captcha table :
         document.getElementById('tcaptcha').style.display = 'none';
         // output key :
         document.getElementById('keyoutput').innerHTML = tmp[1];
         // show key table :
         document.getElementById('tkey').style.display = '';
      } else {
         // something went wrong, don't know what it is (timeout etc) :
         alert("An error occured, please try again.");
         document.getElementById('formbutton').innerHTML = button;
         reload_captcha();
      }
   }
}

function getHTTPObject(){
   var http;
   if(window.XMLHttpRequest){
      http = new XMLHttpRequest();
   } else if(window.ActiveXObject){
      http = new ActiveXObject("Microsoft.XMLHTTP");
   }
   return http;
}



