//*****************************************************************************
// CAPTCHA Client side code...

var postRequest;
var IsResultObtained;
var TheResult;

function RefreshImage(valImageId) 
{
	var objImage = document.images[valImageId];
	if (objImage == undefined) 
	{
		return;
	}
	var now = new Date();
	objImage.src = objImage.src.split('?')[0] + '?x=' + now.toUTCString();
}
function setStatusText(text)
{
	// Display the status message.
	var el = document.getElementById("fldStatus");
	if (el.firstChild == null)
		el.appendChild(document.createTextNode(""));
	el.firstChild.nodeValue = text;
}
function WaitForReply()
{
	if (IsResultObtained)
	{
		// Action it...
		if (TheResult) 
		{
			//alert("// OK, redirect...");
			document.forms[0].submit();
		}
		else
		{
			// Not OK, already dealt with...
		}
	}
	else
	{
		setStatusText("waiting");
		setTimeout(WaitForReply, 1000);
	}	
}
function VerifyCaptcha()
{
	IsResultObtained = false;
	TheResult = false;
	setStatusText("Please wait...");
	var MyCaptcha  = document.forms[0].elements["captchacode"].value;
	if (MyCaptcha.length == 0)
	{
		setStatusText("Please enter the characters shown in the image above");
		return false;
	}

	// Encode the data to be POSTed.
	var ECaptcha = encodeURI(MyCaptcha);

	// Perform an asynchronous request to get a list of zip codes for that city and state.
	postRequest = new HttpRequest();
	postRequest.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
	postRequest.failureCallback = requestFailed;
	postRequest.url = VerifyCaptchaURL
	postRequest.successCallback = CaptchaCallback;
	postRequest.post("captchacode=" + ECaptcha);
	
	setStatusText("waiting");
	setTimeout(WaitForReply, 1000);
	return false;
}
function CaptchaCallback(httpRequest)
{
	IsResultObtained = true;
	// Assume no matches were found.
	var statusText = 'You have entered the wrong code';

	// Set focus back on the page field.
	document.forms[0].elements["captchacode"].focus();

	// Get the XML document returned from the request and fill in the fields.
	try
	{
		var xmlDoc = httpRequest.responseXML;
		var StatusValue;
		if(window.ActiveXObject)
        {
        	StatusValue = xmlDoc.selectSingleNode("//Captcha/Status").text;
        }
        else
        {
           var xpe = new XPathEvaluator();
           var nsResolver = xpe.createNSResolver( xmlDoc.ownerDocument == null ? xmlDoc.documentElement : xmlDoc.ownerDocument.documentElement);
           var results = xpe.evaluate("//Captcha/Status", xmlDoc, nsResolver, XPathResult.FIRST_ORDERED_NODE_TYPE, null);
           StatusValue = results.singleNodeValue.textContent; 
        }
   		if (StatusValue.length > 0)
		{
			if (StatusValue == 'OK')
			{
				TheResult = true;
				setStatusText(StatusValue + ', redirecting...');
				return;
			}
		}
	}
	catch (ex)
	{
		alert('EX: ' + ex.message)
	}
	// Display the status message.
	setStatusText(statusText);
	TheResult = false;
}
function requestFailed(httpRequest)
{
	setStatusText("Lookup failed, HTTP " + httpRequest.status + " " + httpRequest.statusText + ".", 1);
	TheResult = false;
	IsResultObtained = true;
}


