var url = '/captcheck.htm?code=';
var captchaOK = 2;  // 2 - not yet checked, 1 - correct, 0 - failed

    function getHTTPObject()
    {
	    try {
	    req = new XMLHttpRequest();
	    }
	    catch (err1)
	    {
	      try {
	    	  req = new ActiveXObject("Msxml12.XMLHTTP");
	      } catch (err2)
	      {
		      try {
		        req = new ActiveXObject("Microsoft.XMLHTTP");
		        } catch (err3)
		        {
		req = false;
		        }
		      }
		}
        return req;
	}

var http = getHTTPObject(); // We create the HTTP Object

function handleHttpResponse() {
if (http.readyState == 4) {
    captchaOK = http.responseText;
    if(captchaOK != 1) {
      alert('Wprowadzony tekst z obrazka jest niepoprawny. Spróbuj ponownie');
      reload();
      document.frmCaptcha.captcha.value='';
      document.frmCaptcha.captcha.focus();
      return false;
    }
    
    if(document.frmCaptcha.source.value == '')
    {
      document.frmCaptcha.source.value = document.frmCaptcha.source_name.value + " ";
      if(document.frmCaptcha.source_url.value != 'http://')
      {
        document.frmCaptcha.source.value = document.frmCaptcha.source.value + document.frmCaptcha.source_url.value;
      }
    }
    
      document.frmCaptcha.submit();
      return true;
   }
}

function checkcode(thecode) {
	http.open("GET", url + escape(thecode), true);
	http.onreadystatechange = handleHttpResponse;
	http.send(null);
}

function checkform() {
// First the normal form validation
	if(document.frmCaptcha.captcha.value=='') {
	  alert('Wpisz tekst, który zostal umieszczony na obrazku');
	  document.frmCaptcha.captcha.value='';
	  document.frmCaptcha.captcha.focus();
	  return false;
    }
	  // Now the Ajax CAPTCHA validation

	  checkcode(document.frmCaptcha.captcha.value);
	  return false;
}

function reload () {
    var f = document.getElementById('captcha');
//    f.src = '/captcha.png';
    f.contentWindow.location.reload(true);
}
