var UEL;
var Recaptcha;
if (!UEL) { UEL = new Object(); }
//..................................................................................:constructor
class_SpryReCaptcha = function () {
	this.MsgRequired = $('#ReCaptcha_Message');
	this.MsgIncorrect = $('#ReCaptcha_Error');
	this.MsgInvalidForm = $('#ReCaptcha_Form');
}
//..................................................................................:validateOrder
class_SpryReCaptcha.prototype.create = function (_opt) {
	if (Recaptcha != null) {
		Recaptcha.create("6Lf27MISAAAAAHlosYIQA1YZSWmbrGBNY07ql5KG", _opt.Region, {theme: "clean"});
		$('form:has(#'+_opt.Region+')')
			.bind('reset',UEL.SpryReCaptcha.actReset);
			//this.Form = .bind('submit',UEL.SpryReCaptcha.validateForm).get();
		$('#submit').attr('disabled', false);
		$('#reset').attr('disabled', false);
	}
}
class_SpryReCaptcha.prototype.Form = null;
class_SpryReCaptcha.prototype.MsgRequired = null;
class_SpryReCaptcha.prototype.MsgIncorrect = null;
class_SpryReCaptcha.prototype.MsgInvalidForm = null;
//..................................................................................:validateOrder
class_SpryReCaptcha.prototype.actReset = function () {
	UEL.SpryReCaptcha.MsgInvalidForm.css('display','none');
	UEL.SpryReCaptcha.MsgIncorrect.css('display','none');
	UEL.SpryReCaptcha.MsgRequired.css('display','none');
}
//..................................................................................:validateOrder
class_SpryReCaptcha.prototype.validateForm = function (_Form) {
	if (_Form != null) {
		try {
			//validate the form
			//validate menu first, otherwise suppressed by invalid order
			var isVerified = false;
			var isEmpty = Recaptcha.get_response() == "";
			var isValid = Spry.Widget.Form.validate(_Form);
		
			UEL.SpryReCaptcha.MsgRequired.css('display',(isEmpty)? 'inline': 'none');
		
			if (!isEmpty && isValid) {
				try {
					// Retrieve document path, e.g. http://www.abc.uel.ac.uk/level/sublevel/page
					var DocumentPath = new String(document.URL);
					DocumentPath = DocumentPath.toLowerCase();
					// Find protocol and remove protocol from the path, e.g. www.abc.uel.ac.uk/level/sublevel/page
					var Protocol = 'http';
					var i = DocumentPath.indexOf('://');
					if ( i>=0 ) {
						Protocol = DocumentPath.substr(0, i);
					}
					
					var qry = "key=6Lf27MISAAAAAHlosYIQA1YZSWmbrGBNY07ql5KG";
					qry += "&challenge=" + Recaptcha.get_challenge();
					qry += "&response=" + Recaptcha.get_response();
					var response = Spry.Utils.loadURL("GET", Protocol + "://www.uel.ac.uk/cgi-bin/recaptcha.asp?"+qry, false);
					var answer = response.xhRequest.responseText.split('\n');
					isVerified = answer[0] == 'true';
				} catch (e) {
					/*var response = $.get("http://www.uel.ac.uk/cgi-bin/recaptcha.asp", {
							key:		"6LeMCwUAAAAAAODekvxlvuvhlksf0O_HAVdSP6OS",
							challenge:	Recaptcha.get_challenge(),
							response:	Recaptcha.get_response()
							});
					var answer = response.responseText.split('\n');
					isVerified = answer[0] == 'true';*/
				}
				if (!isVerified) {Recaptcha.reload();}
			} 
			
			//Incorrect antispam
			UEL.SpryReCaptcha.MsgIncorrect.css('display',(isValid && !isEmpty && !isVerified)? 'inline': 'none');
			//The form is not complete or valid
			UEL.SpryReCaptcha.MsgInvalidForm.css('display',(!isValid || isEmpty)? 'inline': 'none');
			
			if (isValid && isVerified) {
				//only if form is complete and correct it will be sent via email
				//this also prevents form submission if JavaScript is disabled
				_Form.action = "/cgi-bin/form2mail.asp";
				_Form.method = "POST";
				return true;
			}
		} catch(e) {
		}
	}
	return false;
}
UEL.SpryReCaptcha = new class_SpryReCaptcha();
