/* ---------------------------------------------------- */
/* JQuery												*/
/* ---------------------------------------------------- */
$jQuery(document).ready(function() 
{

	/*############################################################################
	Set up any variables required
	############################################################################ */
	var errorDelay = 4000; // the delay before error messages begin to fade out	
	
	/*############################################################################
	Quick Contact form submission checking and Ajax call
	############################################################################ */
	$jQuery("#quick_contact_submit").click(function() {
		
		/* validate and process form here */ 
		var contactname = $jQuery("#contactname").val();
		var contactphone = $jQuery("#contactphone").val();
		var contactemail = $jQuery("#contactemail").val();
		var contactquestion = $jQuery("#contactquestion").val();
		
		/* Check they've left a contact name */
		if (contactname == "") {
			$jQuery("div#quick_contact_error").html("Please enter your name");
			var error = 'yes';
		}
		/* Check they've left contact details */
		else if (contactemail == "" && contactphone == "" ) {
			$jQuery("div#quick_contact_error").html("Please enter an email or phone number");
			var error = 'yes';
		}
		/* check email is in a valid format */
		else if (contactemail != "" ) {
			if (!/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test(contactemail)) {
				$jQuery("div#quick_contact_error").html("Email address is invalid");
				var error = 'yes';
			}
		}
		else if (contactquestion == "") {
			$jQuery("div#quick_contact_error").html("Please enter your question");
			var error = 'yes';
		}
		
		/* if there's an error, display the appropriate error message */
		if (error == 'yes') {
			/* disable the submit button so form can't be submitted again whilst error being displayed */ 
			$jQuery('#quick_contact_submit').attr('disabled','disabled');
			$jQuery("div#quick_contact_error").hide();
			$jQuery("div#quick_contact_error").fadeIn(800);
			/* After a specified time, fade message away */
			window.setTimeout(function() {
				$jQuery("div#quick_contact_error").fadeOut(800, function() {
					/* clear the error message content */
					$jQuery("div#quick_contact_error").html("&nbsp;");
					$jQuery("div#quick_contact_error").fadeIn(800);
					/* reenable the submit button */
					$jQuery('#quick_contact_submit').attr('disabled','');
				});		
			}, errorDelay);
			return false;
		}
		
		//alert("here");return false;
		
		/* ensure the error message div is not displaying any message */
		$jQuery("div#quick_contact_error").html("&nbsp;");
		
		var dataString = 'contactname=' + contactname + '&contactphone=' + contactphone + '&contactemail=' + contactemail + '&contactquestion=' + contactquestion;  
			
		$jQuery.ajax({  
			type: "POST",  
			url: "include/process_quick_contact.php",  
			data: dataString,  
			success: function() {  
				$jQuery('#quick_contact_form').html("<div id='message'></div>");  
				$jQuery('#message').html("<p>Thank you for contacting us.</p>")
				.append("<p>We will contact you shortly.</p><p>The Thamesdown Recycling team</p>")
				.hide()  
				.fadeIn(1000);  
			}  
		});  
		return false; 
	});
		
	 
});

