jQuery(function($){
	$contact = $('#contact_form');
	$radios = $('input[type="radio"]');
	$byPhone = $('#by_phone');
	$byEmail = $('#by_email');
	$phone = $('#phone_option');
	$email = $('#email_option');
	$boxDiv = $byEmail.parent().parent();
	$result = $('#form_results');

	$radios.click(function(){
		if($byPhone.is(':checked')){
			$email.hide();
			$phone.show();
			$boxDiv.addClass('valid').removeClass('invalid');
		}
		if($byEmail.is(':checked')){
			$email.show();
			$phone.hide();
			$boxDiv.addClass('valid').removeClass('invalid');
		}
	});

	$contact.submit(function(e){
		e.preventDefault();
		var valid = 0;
		$result.html('');
		if($('#contact_name').val() == ''){
			$result.html('<p class="error">Please enter your name</p>');
			valid = 1;
		} 
		if ($('#contact_msg').val() == ''){
			$result.append('<p class="error">Please enter a message</p>');
			valid = 1;
		}
		if(!$boxDiv.hasClass('valid')){
			$result.append('<p class="error">Please select a contact method</p>');
		}
		if($byEmail.is(':checked') && $('#contact_email').val() == ''){
			$result.append('<p class="error">Please enter a valid email address</p>');
		} else if($byPhone.is(':checked') && $('#contact_phone').val() == ''){
			$result.append('<p class="error">Please enter a valid phone number</p>');
		} else if (valid == 0){
			$.post('inc/contact.php',$contact.serialize(),function(r){
				$result.html(r);
			});
		}
	});
});
