
//wait for the DOM to be loaded
jQuery( document ).ready( function() {
	
	//Check if this is the contact us page.
	if( $( 'body' ).hasClass( 'contactUs' ) )
	{
		//Insert Recaptcha.
		//Recaptcha.create("6LfBRgcAAAAAANXMSS1vxAzYL_uNsPXAet6aQ7Fl", "recaptchaLoad", { theme: 'white' });
	};//end if
	
	//VALIDATOR
	jQuery.validator.setDefaults({
		submitHandler: function( pmForm ){ if( validateCaptcha() ){ pmForm.submit(); }; }
	});
	
	// validate the leasing form when it is submitted
	jQuery( "#contactForm" ).validate( {
		rules: {
			name: "required",
			email: { required: true, email: true },
			//emailConfirm: {	required: true, email: true, equalTo: "#email" },
			message: "required",
			recaptcha_response_field: "required"
			//recaptcha_response_field: { required: true, remote: "script/recaptcha.php" }

		}
		,
		messages: {
			name: "Please enter your name.",
			email: "Please enter a valid email address.",
			//emailConfirm: "Please confirm your email address.",
			message: "Please enter your message.",
			recaptcha_response_field: "Please enter Recaptcha validation text."
			//recaptcha_response_field: "needed"
		}
	} );
	
} );

function testRecaptcha()
{
	//alert("submitted!");
	//$( 'div#kitchenHero' ).load( 'script/getHomeHeroImages.php', {}, function(){ $('#kitchenHero').cycle( { fx: 'fade', timeout: 10000 } ); } );
	
	/*$.post("test.php", { func: "getNameAndTime" },
  function(data){
    alert(data.name); // John
    console.log(data.time); //  2pm
  }, "json");*/
	//alert( "hello" );
	//$.post( 'script/recaptcha.php', {}, function( pmData, textStatus ){ alert( pmData ); alert( textStatus ); } )
	validateCaptcha();
	
};//end function testRecaptcha

function validateCaptcha()
{
    challengeField = $("input#recaptcha_challenge_field").val();
    responseField = $("input#recaptcha_response_field").val();
    //alert(challengeField);
    //alert(responseField);
    //return false;
	
	
    var html = $.ajax({
    type: "POST",
    url: "script/recaptcha.php",
    data: "recaptcha_challenge_field=" + challengeField + "&recaptcha_response_field=" + responseField,
    async: false
    }).responseText;
    
    if(html == "correct")
    {
        $( "#captchaStatus" ).html("Success. Submitting form.");
		
		//$( '#contactForm' ).submit();
        //return false;
        // Uncomment the following line in your application
        return true;
    }
    else
    {
		//alert(html);
        $( "#captchaStatus" ).html("Your captcha is incorrect. Please try again");
        Recaptcha.reload();
        return false;
    }
	
}