//Gets the browser specific XmlHttpRequest Object 
function getXmlHttpRequestObject() {
	if (window.XMLHttpRequest) {
		return new XMLHttpRequest(); //Mozilla, Safari ...
	} else if (window.ActiveXObject) {
		return new ActiveXObject("Microsoft.XMLHTTP"); //IE
	} else {
		//Display our error message
		alert("Your browser doesn't support the XmlHttpRequest object.");
	}
}

//Our XmlHttpRequest object
var receiveReq = getXmlHttpRequestObject();
if (typeof( window[ 'phpSessID' ] ) == "undefined")
	var phpSessID = '';
var validCaptcha = false;
if (typeof( window[ 'EmailCaptchaPath' ] ) == "undefined")
	var EmailCaptchaPath = 'EmailCaptcha/';

//Initiate the AJAX request
function makeRequest(url, param, processor) {
//If our readystate is either not started or finished, initiate a new request
	if (receiveReq.readyState == 4 || receiveReq.readyState == 0) {
		if (phpSessID!=""){
			if(param != "") {
				param += '&PHPSID=' + phpSessID;
			} else {
				param += "PHPSID=" + phpSessID;
			}
		}
		//Set up the connection to captcha_test.html. True sets the request to asyncronous(default) 
		receiveReq.open("POST", url, false);
		
		receiveReq.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
		receiveReq.setRequestHeader("Content-length", param.length);
		receiveReq.setRequestHeader("Connection", "close");
		
		//Make the request
		receiveReq.send(param);
		processor();
	}
}

//Called every time our XmlHttpRequest objects state changes
function updatePage() {
	img = document.getElementById('imgCaptcha');
	img.src = EmailCaptchaPath + 'index.php?action=generate_captcha&PHPSID=' + phpSessID + '&regenirate=' + Math.random();
	sid = document.getElementById('PHPSID'); 
	sid.value = phpSessID;
}

function SetSessID() {
	if (receiveReq.readyState == 4) {
		phpSessID = receiveReq.responseText;
		updatePage();
	}
}

function SetCAPTCHAValidFlag() {
	if (receiveReq.readyState == 4) {
		if (receiveReq.responseText=='correct'){
			validCaptcha = true;
		} else {
			validCaptcha = false;
			updatePage();
		}
	}
}

function CheckCAPTCHA(theForm) {
	var url = EmailCaptchaPath + 'index.php?action=check_captcha';
	var postStr = theForm.txtCaptcha.name + "=" + encodeURIComponent( theForm.txtCaptcha.value );
	makeRequest(url, postStr, SetCAPTCHAValidFlag);
	return validCaptcha;
}


function AlertNotice() {
	if (receiveReq.readyState == 4) {
		if (receiveReq.responseText!=''){
			alert(receiveReq.responseText);
		}
	}
}
function GetNotice() {
	var url = EmailCaptchaPath + 'index.php?action=getnotice';
	makeRequest(url, '', AlertNotice);
}


document.write('<img id="imgCaptcha" src="" width="200" height="50" alt="Visual CAPTCHA" /><br />');
document.write('<label for="txtCaptcha" style="font: 10pt Arial; font-weight: bold">Enter the code shown: </label>');
document.write('<input id="txtCaptcha" type="text" name="txtCaptcha" value="" maxlength="6" size="6" autocomplete="off" /><br />');
document.write('<font style="FONT-WEIGHT: normal; FONT-SIZE: 11px; COLOR: #808080; LINE-HEIGHT: 15px; FONT-FAMILY: arial">This helps us prevent automated contacts.</font>');
document.write('<input id="PHPSID" type="hidden" name="PHPSID" value="" />');

makeRequest(EmailCaptchaPath + 'index.php?action=getsessionid' + (phpSessID==''?'':'&PHPSID=' + phpSessID), '', SetSessID);
GetNotice();