var url = 'chkcaptcha.php';
var captchaOK = 2;  // 2 - not yet checked, 1 - correct, 0 - failed

function getCookie(c_name)
{
if (document.cookie.length>0)
  {
  c_start=document.cookie.indexOf(c_name);
  if (c_start!=-1)
    { 
    c_start=c_start + c_name.length+1; 
    c_end=document.cookie.indexOf(";",c_start);
	
    if (c_end==-1) c_end=document.cookie.length;
    return unescape(document.cookie.substring(c_start,c_end));
    } 
  }
return "";
}

function getHTTPObject() {
    try {
          req = new XMLHttpRequest();
    } catch (err1) {
        try {
            req = new ActiveXObject("Msxml2.XMLHTTP.5.0");
        } catch (err2) {
            try {
                req = new ActiveXObject("Microsoft.XMLHTTP");
            } catch (err3) {
                req = false;
            }
        }
    }
    return req;
}
        
var http = getHTTPObject(); // We create the HTTP Object        
function ResetSecurityCode(){
//	document.getElementById('security_code_chk').value="0";
}

function checkSecurityCode2() {
   if (http.readyState == 4) {
    captchaOK = http.responseText;
	//alert(http.responseText);
    if(http.responseText != "OK") {
		alert("กรุณากรอกตัวอักษรให้ถูกต้อง");
		return false;
      }  else {
		if (chk_cookie !=""){
			alert("คุณเพิ่งโพสไป กรุณารอสักครู่");
			return false;
		} else {
			document.getElementById("check_span").style.display = "none"; 
			document.getElementById("post_span").style.display = "none"; 
			return false;

		}
      }
   }
}



function checkSecurityCode() {

	var secCode = document.getElementById("security_code").value;
	chk_cookie = getCookie('POST_COOKIE');
	if (secCode.length != 6){
		alert("กรุณากรอกตัวอักษรให้ถูกต้อง");
		return false;
	}
    http.open("GET", url + "?code=" + escape(secCode), true);
	http.onreadystatechange = checkSecurityCode2;
	http.send();
	return false;
}
