/** public static class Captcha : Interface for singleton _Captcha instance.
  * Override the default instance class (_Captcha) by using setCaptchaClass().
  */
Captcha = {
	FIELD_NAME : "captchaText",
	IMAGE_NAME : "captchaImg",
	REQUEST_ID_NAME : "captchaRequestId",
	captchaClassName : "",
	instance : null,
	/** public String getCaptchaClass : get the name of the _Captcha instance class.
	  * @return _Captcha instance classname
	  */
	getCaptchaClass : function() { return (window[Captcha.captchaClassName]) ? Captcha.captchaClassName : "_Captcha"; },
	/** public _Captcha getInstance : get the singleton _Captcha instance.
	  * @return _Captcha instance
	  */
	getInstance : function() {
		if (!Captcha.instance) Captcha.instance = new window[Captcha.getCaptchaClass()]();
		return Captcha.instance;
	},
	/** public void setCaptchaClass : set the name of the _Captcha instance class.
	  * @param String captchaClass Name of _Captcha subclass to use
	  */
	setCaptchaClass : function(captchaClass) { Captcha.captchaClassName = captchaClass; }
}

/** public class _Captcha : Instance class encapsulating functionality surrounding 
  * a Captcha request.
  */
function _Captcha() {
	this.request = null;
}
/** public void completeRequest : Executed when verifyCaptcha request completes.
  * Validates the response from the backend, and handles any verification failures.
  * If no failures, forwards to the thankyou page.
  */
_Captcha.prototype.completeRequest = function() {
	if (this.request.readyState != 4) return;
	try {
		this.validateResponse();
		linkToPage("thankYouEmailAFriend.do?pageName="+document.getElementById("captchaText").form.pageName.value);
	} catch (err) {
		handleException(err);
	}
}
_Captcha.prototype.generateImage = function() {
	var requestId = Math.floor(Math.random() * 10000000);
	document.getElementById(Captcha.REQUEST_ID_NAME).value = requestId;
	document.images[Captcha.IMAGE_NAME].src = "generateCaptchaImage.do?" + document.getElementById(Captcha.REQUEST_ID_NAME).name + "=" + requestId;
}
/** public void sendRequest : Sends a request to the verifyCaptcha action. Builds a
  * list of parameters from either the passed list, or the keyword field's form.
  */
_Captcha.prototype.sendRequest = function(parameterList) {
	if (!parameterList) var parameterList = document.getElementById("captchaText").form.elements;
	var parameters = "";
	for (var i=0; i<parameterList.length; i++) {
		var field = parameterList[i];
		if (!field.type || !field.name) continue;
		switch (field.type) {
			case "button": case "submit": case "reset": case "image":
				break;
			case "radio": case "checkbox":
				if (field.checked) parameters += field.name + "=" + field.value + "&";
				break;
			default:
				parameters += field.name + "=" + field.value + "&";
		}
	}
	this.request = (window.XMLHttpRequest) ? new XMLHttpRequest() : new ActiveXObject("Microsoft.XMLHTTP");
	this.request.open("POST", "verifyCaptcha.do", true);
	this.request.setRequestHeader("content-type", "application/x-www-form-urlencoded");
	this.request.onreadystatechange = function() { Captcha.getInstance().completeRequest(); };
	this.request.send(parameters);
}
/** public void validateResponse : Examines response text returned by verifyCaptcha request;
  * Throws appropriate exceptions if any failures occur.
  */
_Captcha.prototype.validateResponse = function() {
	var response = this.request.responseText.trim();
	if (response.match("failure")!=null) throw new CaptchaError([document.getElementById(Captcha.FIELD_NAME)], ERRORMESSAGE_CAPTCHA_VERIFICATION);
	else if (response.match("success")==null) throw new CaptchaError([], ERRORMESSAGE_EMAIL_FORM);
}

/** public class CaptchaError : Customized FormValidationError subclass for handling captcha errors.
  * Executes captcha-specific error handling treatments in addition to the standard error treatments.
  * @param HTMLField field Keyword field
  */
function CaptchaError(fieldList, message) {
	this.superClass(fieldList, message);
}
CaptchaError.prototype = new FormValidationError;
CaptchaError.prototype.superClass = FormValidationError;
/** public void handleException : Regenerates captcha image, resets keyword field,
  * then executes standard error treatment.
  */
CaptchaError.prototype.handleException = function() {
	Captcha.getInstance().generateImage();
	document.getElementById(Captcha.FIELD_NAME).value = "";
	this.executeStandardActions();
}

