var RecaptchaOptions = {theme : 'clean'};

$(document).ready(function(){
						   
  $("#submitbtn").click(function() {ValidateRecaptcha();});							   

  $("#name, #email").keypress(function() {
		if ($("#name").hasClass("error") != true) {	
			$('#namewraptexttip').removeClass('hideTextTip');
			$('#namewraptexttip').addClass('showTextTip');
		}
		else {	
			$('#namewraptexttip').removeClass('showTextTip');
			$('#namewraptexttip').addClass('hideTextTip');
		}	
		
		if ($("#email").hasClass("error") != true) {	
			$('#emailwraptexttip').removeClass('hideTextTip');
			$('#emailwraptexttip').addClass('showTextTip');
	
		}	
		else {	
			$('#emailwraptexttip').removeClass('showTextTip');
			$('#emailwraptexttip').addClass('hideTextTip');
		}	
  });
  
  $("#question").keypress(function() {  
		if ($("#question").hasClass("error")) {	
			$("#contact").validate().element("#question");
			$('#questionwraptexttip').removeClass('showTextTip');
			$('#questionwraptexttip').addClass('hideTextTip');
		}
		else {
			$('#questionwraptexttip').removeClass('hideTextTip');
			$('#questionwraptexttip').addClass('showTextTip');
		}
  });
  
$("div#errorMsg").hide();
$("#contact").validate({	
	 	
	onkeyup: function(element) {
    if ($(element).attr('name') != 'recaptcha_response_field') {
        $.validator.defaults.onkeyup.apply(this,arguments);
    }
},	
		rules: {
			 // simple rule, converted to {required:true}
			 name: "required",
			 // compound rule
			 email: {
			   required: true,
			   email: true
			 },
			 recaptcha_response_field: {
			  required: true, 
			  remote: {
				type: "GET",
				dataType:"script",
				url: "http://scripts.sfusion.com/includes/recaptcha/ajax.recaptcha.php",
				data: "recaptcha_challenge_field=" + $("input#recaptcha_challenge_field").val() + "&recaptcha_response_field=" + $("input#recaptcha_response_field").val()
			  }
			},			 
			question: {
			  required: function(element) {
				return $("#question").val().length < 25;
			  },
			  minlength: 25	
			}
		   },
		messages: {
			 name: {
			 required: "<img src=\"http://cache.sfusion.com/images/for-forms/exclamation.png\" alt=\"Error\"/> Please specify your name"},
		     email: {
			   required: "<img src=\"http://cache.sfusion.com/images/for-forms/exclamation.png\" alt=\"Error\"/> To verify your contact request, we require a valid email",
			   email: "<img src=\"http://cache.sfusion.com/images/for-forms/exclamation.png\" alt=\"Error\"/> Your email address must be in the format of name@domain.com"
		   },
			 recaptcha_response_field: {
			 	required: "<img src=\"http://cache.sfusion.com/images/for-forms/exclamation.png\" alt=\"Error\"/> Please enter the two words above",
				remote: "<img src=\"http://cache.sfusion.com/images/for-forms/exclamation.png\" alt=\"Error\"/> Your captcha is incorrect. Please try again"},		   
		   question: {
			 	required: "<img src=\"http://cache.sfusion.com/images/for-forms/exclamation.png\" alt=\"Error\"/> Please enter a question or comment",
			 	minlength: "<img src=\"http://cache.sfusion.com/images/for-forms/exclamation.png\" alt=\"Error\"/> Please enter more than 25 characters"
			 },
		  },
		    
		errorPlacement: function(error, element) {
			 error.insertAfter(element.parent("div").next("div"));
			 var myvariable = element.parent("div").attr('id');
			 $('#' + myvariable + 'texttip').addClass('hideTextTip');
		   }
	});
  });
  


function maxlength(input, limit, info)
{
	var length = input.value.length;
	if(length > limit)
	{
			$('#' + info).html('Sorry, only '+limit+' characters are allowed.');
			input.value = input.value.substr(0,limit);
	}
	else $('#' + info).html('You have '+ (limit - length) +' characters left.');
}; 

function ValidateRecaptcha(){

challengeField = $("input#recaptcha_challenge_field").val();
responseField = $("input#recaptcha_response_field").val();
	
$.getJSON('http://scripts.sfusion.com/includes/recaptcha/json.recaptcha.php?recaptcha_challenge_field='+challengeField+'&recaptcha_response_field='+responseField+'&callback=?', 
	function(data){
		var result = data.result;
		var invalidvars = $("#contact").validate().numberOfInvalids();
		if($("#captcha").html() != "<p class=\"captchasuccess\">Nice work typing in those words. We won't make you do it again.</p>"){
			if((result == "true") && (invalidvars == 0)){
				$("#recaptcha_response_field").rules("remove", "required remote");
				$("#captchaStatus").html("");
				$("#contact").submit();
			} else if(result == "true"){
				$("#recaptcha_response_field").rules("remove", "required remote");
					var invalidvars = $("#contact").validate().numberOfInvalids();
					if ($("#contact").validate().numberOfInvalids() == 0) {
						$("#contact").submit();
					}
				$("#captchaStatus").html("");
				$("#captcha").html("<p class=\"captchasuccess\">Nice work typing in those words. We won't make you do it again.</p>");	
			} else {
				$("#captchaStatus").html("<p class=\"captchaerror\"><img src=\"http://cache.sfusion.com/images/for-forms/exclamation.png\" alt=\"Error\"/> Your captcha is incorrect. Please try again</p>");
				Recaptcha.destroy();
				Recaptcha.create("6LdSYQkAAAAAAJaDPeDjUIXWiotOEI3eqe1RHmYr", "captcha", {theme: "clean"});	
			}	
		}
	});
}