// JavaScript Document
$(document).ready(function() {
            setCurrentPageTitle("Apply Now");
            $(".error").html("<div class='innerError'></div>");
            $(".innerError").hide();
            jQuery("#submitStatus").hide(); 
            jQuery(".phoneNumberField").keypress(function (e) { 
              //if the letter is not digit or "-" display error and don't type anything
              if( e.which!=8 && e.which!=0 && e.which!=45 && e.which!=13 && (e.which<48 || e.which>57))
              {
                //display error message    
                jQuery(".innerError", $(this).prev()).html("Digits only!").fadeIn(800).fadeTo(1000,1,function(){
    		            jQuery(this).fadeOut(200);
  			    });
                return false;
              }
            }); 
        });
        
           
        function btnSubmitClick() {
            var name 	= jQuery("input.txtName").val();
	        var email 	= jQuery("input.txtEmail").val();
	        var phone	= jQuery("input.txtPhone").val();
	        var msg 	= jQuery("textarea.txtLoanRequest").val();
	        var hear 	= jQuery("select.ddlHear").val();
        	
	        if (name == "") {
	              
             jQuery("#r1 .innerError").html("Please Enter Your Name").fadeIn(800).fadeTo(3000,1,function(){
    		        jQuery(this).fadeOut(200);
  			        });
	         var error_name = true;
	         var error = true;      
            }	
	        if (email == "") {      
	         jQuery("#r2 .innerError").html("Please Enter Your Email Address").fadeIn(800).fadeTo(3000,1,function(){
    		        jQuery(this).fadeOut(200);
  			        });
	         var error_email = true;
	         var error = true;      
            }
	        else if(!isEmail(email)){
		        jQuery("input.txtEmail").focus();
		        jQuery("#r2 .innerError").html("Please Enter a Valid Email Address").fadeIn(800).fadeTo(3000,1,function(){
    		        jQuery(this).fadeOut(200);
  			        });
	 	        var error = true;
	        }		
	        if (phone == "") {      
             jQuery("#r3 .innerError").html("Please Enter Your Phone Number").fadeIn(800).fadeTo(3000,1,function(){
    		        jQuery(this).fadeOut(200);
  			        });
	         var error_phone = true;
	         var error = true;      
            }				
	        if (msg == "") {      
             jQuery("#r4 .innerError").html("Please Enter A Message").fadeIn(800).fadeTo(3000,1,function(){
    		        jQuery(this).fadeOut(200);
  			        });
	         var error_msg = true;
	         var error = true;      
            }	
	        if (hear.substr(0,1)=="-") {      
             jQuery("#r5 .innerError").html("Please Tell Us How You Heard of Us").fadeIn(800).fadeTo(3000,1,function(){
    		        jQuery(this).fadeOut(200);
  			        });
	         var error_hear = true;
	         var error = true;      
            }	
        	
	        if (error){		
		        if(error_name){
			        jQuery("input.txtName").focus();			
		        }
		        else if(error_email){
			        jQuery("input.txtEmail").focus();
		        }
		        else if(error_phone){
			        jQuery("input.txtPhone").focus();			
		        }		
		        else if(error_msg){
			        jQuery("textarea.txtLoanRequest").focus();			
		        }
		        else if(error_hear){
			        jQuery("select.ddlHear").focus();		
		        }
		        return false;
	        }
		 		 jQuery("#applyNowContainer").slideUp(800);
                    jQuery("#h2_ApplyNow,#h3_ApplyNow").slideUp(800);		
		            jQuery("#submitStatus").html("<h2>Sending request now...</h2>").fadeTo(500,1,function(){
    	                jQuery(this).slideDown(400);
  		            });
		 
	var txtName 	= jQuery('input.txtName').attr('value');
	var txtEmail 	= jQuery('input.txtEmail').attr('value');
	var txtPhone 	= jQuery('input.txtPhone').attr('value');
	var txtLoanRequest 	= jQuery('textarea.txtLoanRequest').attr('value');
	var ddlHear 	= jQuery('select.ddlHear').attr('value');
	
	
		jQuery.ajax({
			type: "POST",
			url: "scripts/send_email_quote.php",
			data: 	"txtName=" + txtName+ "&txtEmail=" + txtEmail + "&txtPhone=" + txtPhone + "&txtLoanRequest=" + txtLoanRequest + "&ddlHear=" + ddlHear,
			success: function(){
				 jQuery("#applyNowContainer").slideUp(800);
                    jQuery("#h2_ApplyNow,#h3_ApplyNow").slideUp(800);		
		            jQuery("#submitStatus").html("<h2>Thank You</h2> <p>Your message has been sent.</p>").fadeTo(500,1,function(){
    	                jQuery(this).slideDown(400);
  		            });
			}
		});
	            return false;
	        
        }