$(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
	 	      
		        })
		        
		        
		        
		        
//DEAL DESK FORM
	    
	     //checkout js
	     		    	// Place ID's of all required fields here.
	    	
	    	$(".desksubmit").click(function(){
	    	
	    	var required = [
	    
	    	"email",
	    	"f_name",	
	    	"l_name",	
	  
	    	
	    	];
	    	
	    	
	    	
	    	// If using an ID other than #email or #error then replace it here
	    	var email = $("#email");
	    	var errornotice = $("#error");
	    	// The text to show up within a field when it is incorrect
	    	var emptyerror = "Please fill out this field.";
	    	var emailerror = "Please enter a valid e-mail.";
	    	
	    	for (i=0;i<required.length;i++) {
	    			var input = $('#'+required[i]);
	    			if ((input.val() == "") || (input.val() == emptyerror)) {
	    			
	    				input.addClass("needsfilled");
	    				input.val(emptyerror);
	    				errornotice.fadeIn(750);
	    				
	    				
	    			} else {
	    			
	    				input.removeClass("needsfilled");
	    				
	    			}
	    		}
	    		// Validate the e-mail.
	    		if (!/^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/.test(email.val())) {
	    			email.addClass("needsfilled");
	    			email.val(emailerror);
	    		}
	    	
	    		
	    		//alert("about to checkout");
	    		
	    
	    	// Clears any fields in the form when the user clicks on them
	    	$(":input").focus(function(){		
	    	   if ($(this).hasClass("needsfilled") ) {
	    			$(this).val("");
	    			$(this).removeClass("needsfilled");
	    	   }
	    	});
	    	
	    	
	    	//if any inputs on the page have the class 'needsfilled' the form will not submit
	    	if ($(":input").hasClass("needsfilled")) {
	    		//alert("needsfilled");
	    		return false;
	    	} else {
	    		errornotice.hide();
	    		//alert("doesnt neet filled");
	    		
	    	}
	    		//if all is OK, posts data
	    		var data = $('#dealdesk').serialize();
	    		$('#loader-image').show();
	    		$.ajax({
	    				type: "POST",
	    				url: "/formapi/dealdesk",
	    				data: data,
	    				success: function(){
	    				
	    						alert("Thank you for your submission!");
	    						$('form :input', this).val("");
	    						$(".checkbox", this).attr('checked', false);
	    						//$('#loader-image').hide();
	    						$('#checkout_form').hide(function(){
	    						$('#checkout_success').show();
	    						
	    						
	    						}); 
	    		
	    						}
	    				
	    			});
	    			
	    	
	    	
	    	
	    		});
	
	//END OF LINE					   
});
		
