$(document).ready(function() {          
	// contact hover link	
	$(".contactHoverLink").click(function(event){   	
		$("#contactForm").show();
	 	$.scrollTo(0, 1000);
		event.preventDefault();
	});                            
	$(".close").click(function(event){   	
		$("#contactForm").hide();
		event.preventDefault();
	}); 
	$("#contactForm form").submit(function(e){
		return contactErrorCheck(e);
	});  
}); 

// contact form error checking
function contactErrorCheck(e) {        
	e.preventDefault(); 
	
	var errorMessage = "";        
	$fname = $("#input_fname").val()
	$lname = $("#input_lname").val()
	$email = $("#input_email").val()
	$phoneext = $("#input_phoneext").val()
	$phone = $("#input_phone").val()
	if(($fname == "" || $lname == "") && $email == "" && ($phone == "" || $phoneext == "")) {
		errorMessage = "Please fill out your name and an email address or phone number where we can contact you. ";
	} else {                 
		if($fname == "" || $lname == "") {
			errorMessage = "Please provide us with your name. ";
		} 
		if($email == "" && ($phone == "" || $phoneext == "")) { 
		   errorMessage = "Please provide an email or phone number where we can contact you. ";
		} else { 
			if($email != "") {
    			var emailBad = false;
				var emailFilter=/^.+@.+\..{2,6}$/;
		    	if (!(emailFilter.test($email))) { 
		    		emailBad = true;
		    	}

		    	var illegalChars= /[\s\!\*\(\)\<\>\,\;\:\\\/\"\[\]]/
		    	if ($email.match(illegalChars)) {
		    		emailBad = true;
		    	}
			    if(emailBad) {
					errorMessage += "There's an issue with your email address. "; 
				}                             
			}
			if(($phoneext != "" && $phoneext.length < 3) && ($phone != "" && $phone.length < 7)) {
			    errorMessage += "Your phone number is invalid, we need at least 10 digits. ";                              
			}                                                                  
		}
	}   
	
   // no errors, we're good.. else show error message
	if(errorMessage == "") {          
		// Send the data using post
	    $.post("/qry/ncctms_testdriveformhandler.taf?_function=submit", $("#contactForm form").serialize());
		$("#contactForm form").hide();
		$("#thankYouMessage").show();
		$.scrollTo(0, 500);		    		
	} else {
		// show error message
		$("form .errors").html("<div><h5>We're sorry, it appears there was a problem.</h5><p>" + errorMessage + "</p></div>");
		$("form .errors").show(); 
	}
}
