$(function() {
	
// @formapi
// Simple Contact Form Submission
// Author: Brian Foust
// Form vars: f_nameVal, l_nameVal, phoneVal, emailToVal, commentsVal
	
		$("#cform").submit(function(){	
								   				   
		$(".error").hide(); // Hide errors
		var hasError = false; //Start with no errors
		var emailReg = /^([\w-\.]+@([\w-]+\.)+[\w-]{2,4})?$/; //email needs to have email structure xx@xx.com
		
		
		var f_nameVal = $("#f_name").val();
		if(f_nameVal == '') {
			$("#f_name").after('<span class="error">You forgot to enter your first name.</span>');
			hasError = true;
		}
		
		var l_nameVal = $("#l_name").val();
		if(l_nameVal == '') {
			$("#l_name").after('<span class="error">You forgot to enter your last name.</span>');
			hasError = true;
		}
		
		var phoneVal = $("#phone").val();
		if(phoneVal == '') {
			$("#phone").after('<span class="error">You forgot to enter your phone number.</span>');
			hasError = true;
		}
		
		var emailToVal = $("#emailTo").val();
		if(emailToVal == '') {
			$("#emailTo").after('<span class="error">You forgot to enter the email address to send to.</span>');
			hasError = true;
		} else if(!emailReg.test(emailToVal)) {	
			$("#emailTo").after('<span class="error">Enter a valid email address to send to.</span>');
			hasError = true;
		}
	
		
		var commentsVal = $("#comments").val();
		if(commentsVal == '') {
			$("#message").after('<span class="error">You forgot to enter your comments.</span>');
			hasError = true;
		}
		
		var submitdata = $("#cform").serialize(); //round the data up into one variable
		
		if(hasError == false) 
		{
			
			$("#submit").append('<img src="img/loading.gif" alt="Loading" id="loading" />'); //load a simple "loading" image
			
			$.post("sendemail.php", submitdata, 
   					
   					function(data){		   
							
						$("#cform").slideUp();											
						$("#messagebox").html('<h1>Success</h1><p>Your email was sent. Thank You!</p>').css('border', '1px dashed #aaa', 'padding', '15px');

   					}
				 );
				 	
		}
		
		return false; d
	});	
	
// @formapi
// Simple Application Form Submission
// Author: Brian Foust

	$("#appform").submit(function(){	
								   				   
		$(".error").hide(); // Hide errors
		var hasError = false; //Start with no errors
		var emailReg = /^([\w-\.]+@([\w-]+\.)+[\w-]{2,4})?$/; //email needs to have email structure xx@xx.com
		
		
		var f_nameVal = $("#f_name").val();
		if(f_nameVal == '') {
			$("#f_name").after('<span class="error">You forgot to enter your first name.</span>');
			hasError = true;
		}
		
		var l_nameVal = $("#l_name").val();
		if(l_nameVal == '') {
			$("#l_name").after('<span class="error">You forgot to enter your last name.</span>');
			hasError = true;
		}
		
		var phoneVal = $("#phone").val();
		if(phoneVal == '') {
			$("#phone").after('<span class="error">You forgot to enter your phone number.</span>');
			hasError = true;
		}
		
		var emailToVal = $("#emailTo").val();
		if(emailToVal == '') {
			$("#emailTo").after('<span class="error">You forgot to enter the email address to send to.</span>');
			hasError = true;
		} else if(!emailReg.test(emailToVal)) {	
			$("#emailTo").after('<span class="error">Enter a valid email address to send to.</span>');
			hasError = true;
		}
	
		
		var commentsVal = $("#comments").val();
		if(commentsVal == '') {
			$("#message").after('<span class="error">You forgot to enter your comments.</span>');
			hasError = true;
		}
		
		var submitdata = $("#cform").serialize(); //round the data up into one variable
		alert(submitdata);
		
		if(hasError == false) 
		{
			
			$("#submit").append('<img src="img/loading.gif" alt="Loading" id="loading" />'); //load a simple "loading" image
			
			$.post("sendemail.php", submitdata, 
   					
   					function(data){		   
							
						$("#appform").slideUp();											
						$("#messagebox").html('<h1>Success</h1><p>Your email was sent.</p>').css('border', '1px dashed #aaa');

   					}
				 );
				 	
		}
		
		return false; 
	});	
	
	
	$(".selectofficer").change(function () {
			 var p = $(this);
	          var str = "";
	          $("option:selected", p).each(function () {
	                str += $(this).val();
	              });
	         // alert("You are being redirected to a Loan Officer's Page.");
	          window.location = "/lo/" + str; // redirect
	 	      
		        })
	    
	
	//END OF LINE					   
});
		
