$(document).ready(function(){
  $("#registerLightForm").validate({
  		rules: {
  			name: {
  				remote: {
	  				url: "testnick",
	  				type: "post"
	  			}
  			},
  			email: {
  				remote: {
	  				url: "testemail",
	  				type: "post"
  				}
  			},
			passwd1: {
				notequal: "name"
			},
  			own_question: {
  				required: "#ownQuestionRow:visible"
  			},
  			b_state2: {
  				required: "#b_state2:visible"
  			}
  		},
  		messages: {
  			name: {
  				remote: jQuery.format("{0} is already in use")
  			},
  			email: {
  				remote: jQuery.format("{0} is already in use")
  			}
  		},
  		onkeyup: false,
		errorElement: "div",
		errorClass: "invalidComment",
		submitHandler: function(form) 
		{
			$("#registerLightForm").attr('action', $("#registerLightFormUrl").val());

			// Check recaptcha
			if (withRecaptcha())
				checkCaptcha($("#recaptcha_response_field").val()); // will submit form on success
			// Check standard captcha
			else if ($('#verif').length)
				checkCaptcha($('#verif').val()); // will submit form on success
			else
			{
				// Check zip code (on registerfull = Step 3)
				if ($("#b_zip").length && !checkZipMinLength('b_country', 'b_zip', 'b_zip_msg'))
					return false;
				form.submit();
			}
  	}
  });
  
  $("#lastname, #email, #name, #passwd1, #verif, #sec_answer").focus(showBottomHint);
  $("#name, #passwd1, #verif, #sec_answer").focus(showRightHint);
  $("#name, #passwd1, #verif, #sec_answer").blur(hideRightHint);
	
  $("#editProfileForm").validate({
  	onkeyup: false,
		errorElement: "div",
		errorClass: "invalidComment"
  });
  
  $("#editAddressForm").validate({
  	onkeyup: false,
	errorElement: "div",
	errorClass: "invalidComment",
	submitHandler: function(form) {
  		if (checkZipCodes())
  			form.submit();
	},
	rules: {
		b_state: {
			required: "#b_state:visible"
		},	  
		b_state2: {
			required: "#b_state2:visible"
		},
		s_address: {
			required: "#ship2diff:checked"
		},
		s_city: {
			required: "#ship2diff:checked"
		},
		s_country: {
			required: "#ship2diff:checked"
		},
		s_zip: {
			required: "#ship2diff:checked"
		},
		s_state: {
			required: "#s_state:visible"
		},			
		s_state2: {
			required: "#s_state2:visible"
		}
	}
  });
   
  $("#editSettingsForm").validate({
	ignore: ".ignore",
  	rules: {
	  email: {
	  	remote: {
	  		url: "testemail",
	  		type: "post"
		}
	  },
	  own_question: {
		required: "#ownQuestionRow:visible"
	  },
	  passwd1: {
		required: "#updatePassword:checked",
		notequal: "name"
	  }
  	},
	messages: {
		email: {
			remote: jQuery.format("{0} is already in use")
		}
	},
	onkeyup: false,
	errorElement: "div",
	errorClass: "invalidComment"
  });
  
  $("#upgradeForm").validate({
  	rules: {
  		passwd1: {
			notequal: "name"
		}
	},
	onkeyup: false,
	errorElement: "div",
	errorClass: "invalidComment"
  });   
});

function showRightHint() {
	var rightHintsText = new Array();
	rightHintsText["name"] = 'Note: Your username is used for your public community profile name and cannot be changed (not case sensitive). You may choose to use any nickname, pseudonym or avatar to be known by. This username will also be used for your profile URL. e.g. MyYogaHub.com/Username';
	rightHintsText["passwd1"] =	'Increase password security by including letters and numbers; use both upper and lower case characters. We suggest you do not use your Username.';
	rightHintsText["verif"] = 'Human requirement. By entering this code you help us prevent spam and fake registrations.';
	rightHintsText["sec_answer"] = 'Make sure your answer is memorable to you but hard for others to guess!';
	rightHintsText["interests"] = 'These will be displayed on your public profile and allow you to easily find other people in the community who share similar interests.';
	rightHintsText["description"] = 'This will be displayed on your community profile and can be updated at any time.';
	rightHintsText["sec_question"] = 'Choose an answer that is memorable, but not easy to guess. If you write your own question, do not choose a question that is common or obvious.';
	rightHintsText["timezoneoffset"] = 'Your time zone helps us provide you with content that is relevant to where you live.';

	var rightHint = $("#rightHint");
	var rightHintText = $("#rightHintText");
	rightHintText.html(rightHintsText[$(this).attr("id")]);

	// Prepare hint position
	var inputWidth = 340; 
	var offset = $(this).offset();
	var hintLeft = offset.left + inputWidth;
	rightHint.css({top:offset.top, left:hintLeft});
	rightHint.fadeIn("slow");	
}

function withRecaptcha()
{
	return $("#recaptcha_response_field").length != 0;
}

function refreshCaptcha()
{
	if (withRecaptcha())
		Recaptcha.reload();
	else
	{
		var rnd = Math.floor(Math.random()*101);
		$.get('refreshcaptcha/' + rnd, null,
			function(data){
				$(".verifImgCont").html(data);
			}
		);
	}
}

function checkCaptcha(verif)
{
	var captchaData = { "verif": verif };
	if (withRecaptcha())
	{
		captchaData["recaptcha_response_field"] = $("#recaptcha_response_field").val();
		captchaData["recaptcha_challenge_field"] = $("#recaptcha_challenge_field").val();
	}

	$.post(baseUrl + 'checkcaptcha', captchaData, function(result)
	{
		if (result == 'OK')
			document.forms.registerLightForm.submit();
		else
		{
			if (withRecaptcha())
				$('#invalidRecaptchaText').show();
			else
			{
				if ($("#invalidCaptchaText").length != 0)
					$("#invalidCaptchaText").show();
				else
					$('#verif').after('<div class="invalidComment" id="invalidCaptchaText">Retype the letters as shown above. If the characters are hard to read, click on the blue refresh icon.</div>');
			}

			refreshCaptcha();
		}
	});
}
