

//Javascript
$(function() {
	$('.contactus input')		//Apply enter button submit functionality to inputs
		.live('keydown',function(event) {
			if(event.keyCode==13)
				submitContact();
		});
	$('#name').focus();
	recaptchaCreate();
});

function focusField() {			//Focus on first incomplete required field
	if($('#name').val() == '')
		$('#name').focus();
	else if($('#email_error').html() != '')
		$('#email').focus();
	else
		Recaptcha.focus_response_field();
}

function recaptchaCreate() {
	Recaptcha.create("6Le9wAYAAAAAAKFp6PHNQMZkfKEp64QBt6BG8Z9J", //wahm1.internetbrands.com
//	Recaptcha.create("6LesJgcAAAAAAK2o_0Y4IThwtpzNu3DH52ye8k5i", // AMVM 
//	Recaptcha.create("6Le9wAYAAAAAAKFp6PHNQMZkfKEp64QBt6BG8Z9J",
//	Recaptcha.create("6LeSxAYAAAAAAPCgVeLU5vluHKpNUihs0micuNVG",
		"recaptcha_div", {
		theme: "white",
		callback: focusField
	});
}

function submitContact() {		//Ajaxian post data
	var error=false;
	if($('#name').val() == '') {
		$('#name').css('background-color','#FF9999');
		$('#name_error').html('Please enter your name.');
		error=true;
	}
	else {
		$('#name').css('background-color','#FFFFFF');
		$('#name_error').html('');
	}

	if(!$('#email').val().match(/^[-a-z0-9_]+[-a-z0-9_.]*[@]{1}[-a-z0-9_]+[-a-z0-9_.]*[.]{1}[a-z]{2,5}$/i)) {
		$('#email').css('background-color','#FF9999');
		$('#email_error').html('Please enter a valid email address.');
		error=true;
	}
	else {
		$('#email').css('background-color','#FFFFFF');
		$('#email_error').html('');
	}

	if($('#comment').val() == '') {
		$('#comment').css('background-color','#FF9999');
		$('#comment_error').html('Please enter a comment.');
		error=true;
	}
	else {
		$('#comment').css('background-color','#FFFFFF');
		$('#comment_error').html('');
	}

	if($('#recaptcha_response_field').val() == '') {
		$('#recaptcha_response_field').css('background-color','#FF9999');
		$('#recaptcha_error').html('Please solve the spam check.');
		error=true;
	}
	else {
		$('#recaptcha_response_field').css('background-color','#FFFFFF');
		$('#recaptcha_error').html('');
	}

	if(!error) {
		$.post(window.location.href.match(/^http[s]?:[/]{2}[a-z0-9.:-]+/i)+'/includes/php/recaptcha_handler.php', {
			name: $("#name").val(),
			email: $("#email").val(),
			website: $("#website").val(),
			comment: $("#comment").val(),
			challenge: Recaptcha.get_challenge(),
			response: Recaptcha.get_response()},
			function(data) {handleReturnData(data);},
			"json");
		$(".contactus [class$=error]").html('');
	}
}

function redirectHP() {
	window.location=window.location.href.match(/^http[s]?:[/]{2}[a-z0-9.:-]+/i);
}

function handleReturnData(data) {	//Handle JSON AJAX return data
	if(data.complete == true) {		//If complete, redirect
		setTimeout('redirectHP()',6000);
		$(".contactus").html("<h3>Thank You for Contacting WAHM.</h3><br /><p>You will be redirected shortly to the Home Page.  If your browser does not automatically redirect you please <a href=\"http://www.wahm.com\">click here<a/>.</p>");
	}
	else {
		$.each(data, function(key, val) {	//Show error messages
			if(key != 'complete') {
				$('#'+key+'_error').html(val);
				
				if(key == 'recaptcha') {
					Recaptcha.destroy();
					if(val != 'Correct!')
						recaptchaCreate();
				}
				else {						//Red out missing fields
					if(val != '')
						$('#'+key).css('background-color','#FF9999');
					else
						$('#'+key).css('background-color','white');
				}
			}
		});
	}
}
