/**
 * @author W951AGS
 */

$(document).ready(function() {
	if ($('#bus_check').is(':checked')) {$('#business').show()}
	else $('#business').hide();
 	$("#bus_check").click(function(){
		if ($('#bus_check').is(':checked')) {
			$('#business').slideDown('fast');
		}
		else $('#business').slideUp('fast');
  });
	
	//Configure mask for phone number field
	$('.phone').mask("(999) 999-9999",{placeholder:" "});
	
	// validate the comment form when it is submitted
	$("#emailform").validate();
	$("#emailform").submit(function(){
		if ($("#emailform").valid()) {
			ajaxSubmit();
			return false;
		}
	});
});

jQuery.validator.addMethod("phone", function(value, element) { 
  return this.optional(element) || /^\(\d{3}\) ?\d{3}( |-)?\d{4}|^\d{3}( |-)?\d{3}( |-)?\d{4}/.test(value); 
}, "Please use one of the following formats:<br/>(111)222-1234, 111-222-1234, 1112221234");


function ajaxSubmit(){
	var formData = $("#emailform").serialize();
	$.ajax({
		type: 'POST',
		url: '/contact_us.jsp',
		data: formData,
		success: function(){
			$("#contactform").slideUp(0, function(){
				result = '<p>Thank you for contacting us, '+$('#name').val()+'. Your message has been sent. A CapStar Bank '
				+ 'representative will contact you regarding your question(s).</p>';
				$('#thankyou').html(result).fadeIn('slow');
			});
		},
		error: function(){
			$("#contactform").slideUp(0, function(){
				result = "<p class='error'>We're sorry, "+$('#name').val()+". It looks like there was a communication "
				+ "problem with the server. Please contact us by one of the other methods below and let us know.</p>";
				$('#thankyou').html(result).fadeIn('slow');
			});

		}
	});
	return false;
};
