/*
 * NOTE: this validation is used for both 
 * 		- Basic form
 * 		- My Account - My Details form 
 */
$(document).ready(function(){

	$("#frmBasic,#frmMyDetails").validate({
		errorElement: "em",
		errorContainer: "#msgError",
		onsubmit: true,
		onfocusout: false,
		onkeyup: false,
		onclick: false,
		rules: {
			// sRefNum is required if its exists on the form
			sRefNum: {
				required:		$("#sRefNum").length > 0
			},
			// sSubRef is required if its exists on the form
			sSubRef: {
				required:		$("#sSubRef").length > 0
			},
			// sReaderNumber is required if its exists on the form
			sReaderNumber: {
				required:		$("#sReaderNumber").length > 0
			},
			// sCustomerNumber is required if its exists on the form
			sCustomerNumber: {
				required:		$("#sCustomerNumber").length > 0
			},
			sTitle:				"required",
			sFirstName:			"required",
			sLastName:			"required",
			sEmail: {
				required: 		true,
				email: 			true
			},
			sPasswordCurrent: {
				required:		function(element) {
									return $("#sPassword").val() != '';
								}
			},
			sPassword: {
				required:		function(element) {
									// if sPasswordCurrent does not exist - sPassword is required
									if ($("#sPasswordCurrent").length <= 0) {
										return true;
									}
									
									// if sPasswordCurrent does exist - sPassword is only required if sPasswordCurrent is filled in
									return $("#sPasswordCurrent").val() != '';
								},
				minlength:		8,
				alphanumeric: 	true
			},
			sPasswordConfirm: {
				equalTo:		"#sPassword"
			}/*,
			recaptcha_response_field:{
				recaptchaCheck:	$('#recaptcha_challenge_field')
			}*/
		},
		// Customisable error messages for forms
		messages: {
			sRefNum:			"Please provide a value",
			sSubRef:			"Please provide a subscription reference",
			sReaderNumber:		"Please provide a reader number",
			sCustomerNumber:	"Please provide a customer number",
			sTitle:				"Please select a title",
			sFirstName:			"Please provide a first name",
			sLastName:			"Please provide a last name",
			sEmail: 			"Please provide a valid email address",
			sPasswordCurrent: 	"Please provide a your current password",
			sPassword: {
				required: 		"Please provide a valid password",
				minlength:		"Please provide a valid password",// the password strength indicator already states it's too short
				alphanumeric:	"Only letters and numbers accepted"
			},
			sPasswordConfirm:{
				equalTo:		"Passwords do not match"
			}/*,
			recaptcha_response_field:{
				recaptchaCheck: 	jQuery.format("Invalid captcha. Please try again")
			}*/
		}
	});
	
	$("#sPassword").passStrength();

});

//this is the validate method which checks the Captcha
//this does an Ajax request which takes the two fields, performs the check, 
//then returns a 1 if it passed the check and a 0 if it fails
//also if it does fail the captcha refreshes with a new pair of words 

jQuery.validator.addMethod('recaptchaCheck', function(value) {
	var sURL 							= $('#sAddOwnEventURL').val();
	var thedata = "";

	$.ajax({
		async: false,
		url: sURL+'/_templates/forms/act_AJAXCaptureCheck.cfm',
		dataType: 'json',
		data: {
				recaptcha_challenge_field: 	$('#recaptcha_challenge_field').val(),
				recaptcha_response_field: 	$('#recaptcha_response_field').val() 
				},
		success: function(data) {
			thedata = data;
		}
	});

	if(thedata.success == 1){
		// reload captcha, so that if other fields fail, we have a new captcha to check on!
		Recaptcha.reload(); 
		return true;
	}else if(thedata.success == 0){
		Recaptcha.reload();
		return false;
	}
}, '' );
