// Functions to perform CAPTCHA check and related AJAX code
// Assumes originating form is called myform
// This version for FiresticksandSong.com

// reload captcha image
function reload() {
document.captchaimage.src = "/forms/captcha2.php?" + (new Date()).getTime();
}
// Ajax captcha - see http://www.funkyajax.com/captcha4_page.html

var url = '/forms/captcheck.php?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('The entered code was not correct. Please try again');
      document.myform.code.value='';
      document.myform.code.focus();
      return false;
      }
    document.myform.subject.value = "Website Visitor Comments - Validated: " + document.myform.code.value;
	document.myform.submit();
	}
}

//The function checkcode() sets up the Ajax call:

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

function checkform ()
{

   if (document.myform.realname.value == "") {
		alert("Please make sure you provide your name!");
		document.myform.realname.focus();
    return false ;
  }
	if (document.myform.email.value == "") {
		alert ("Please make sure you provide your email address!")
		document.myform.email.focus();
	return false;
  }
   if (document.myform.message.value == "") {
    alert ("Please make sure you add a message!");
    document.myform.message.focus();
    return false ;
  }
  //return true ;

if(document.myform.code.value == '') {
  alert('Please enter the letters and numbers from the displayed image');
  document.myform.code.value='';
  document.myform.code.focus();
  return false;
  }

// Now the Ajax CAPTCHA validation
checkcode(document.myform.code.value);
return false;

}