$(document).ready(function(){

	$("#contactLink a").click(function(){
		if ($("#contactForm").is(":hidden")){
			$("#contactForm").slideDown("slow");
		}else{
			$("#contactForm").slideUp("slow");
		}
		return false;
	});
	
	var quickqueryOptions = {
		target:        '#messageSent',   // target element(s) to be updated with server response 
        beforeSubmit:  quickqueryValidate,  // pre-submit callback 
        success:       closeForm  // post-submit callback
    };

	// bind to the form's submit event 
	$('#quickquoteform').submit(function() { 
		// inside event callbacks 'this' is the DOM element so we first 
	    // wrap it in a jQuery object and then invoke ajaxSubmit 
	    $(this).ajaxSubmit(quickqueryOptions); 

	    // !!! Important !!! 
	    // always return false to prevent standard browser submit and page navigation 
	    return false; 
	});
	
	var contactOptions = { 
        target:        '#contact-feedback',   // target element(s) to be updated with server response 
        beforeSubmit:  validate  // pre-submit callback 
    };
	
	$('#contactform').submit(function() { 
		// inside event callbacks 'this' is the DOM element so we first 
	    // wrap it in a jQuery object and then invoke ajaxSubmit 
	    $(this).ajaxSubmit(contactOptions); 

	    // !!! Important !!! 
	    // always return false to prevent standard browser submit and page navigation 
	    return false; 
	});
	
           
});

function quickqueryValidate(){
	var emailReg = /^([\w-\.]+@([\w-]+\.)+[\w-]{2,4})?$/;
	var emailToVal = $("#email-quick").val();
	$('.error').remove();
	if(emailToVal == '') {
		$("#email-quick").before('<span class="error">Please enter an email address.</span>');
		return false;
	} else if(!emailReg.test(emailToVal)) {	
		$("#email-quick").before('<span class="error">Enter a valid email address</span>');
		return false;
	}
}

function validate(){
	var emailReg = /^([\w-\.]+@([\w-]+\.)+[\w-]{2,4})?$/;
	var emailToVal = $("#email-field").val();
	$('.error').remove();
	if(emailToVal == '') {
		$("#email-field").before('<span class="error">Please enter an email address.</span>');
		return false;
	} else if(!emailReg.test(emailToVal)) {	
		$("#email-field").before('<span class="error">Enter a valid email address</span>');
		return false;
	}
}


function closeForm(){
	$("#messageSent").show("slow");
    setTimeout('$("#feedback-quickquery").hide(); $("#contactForm").slideUp("slow");', 2000);
}
