$(function() {
	// highlighter for the FAQ
	$("a.ques").click(
		function(){
			var which = $(this).attr("rel");
			$("dd").removeClass("highlight");$("dt").removeClass("highlight");
			$("dd[rel="+which+"]").addClass("highlight");
			$("dt[id="+which+"]").addClass("highlight");
		}
	);
	
	// hide the contact form
	$("a[rel='contact_form']").toggle(
      function () {
      	$myvar = $(this);
      	$("#contact_form").show("slide", { direction: "left" }, 1000, function(){
      		$myvar.addClass("highlight");
      		$myvar.html("Hide Contact Details");
      	});
      },
      function () {
      	$myvar = $(this);
      	$("#contact_form").hide("slide", { direction: "left" }, 1000, function(){
      	    $myvar.removeClass("highlight");
      		$myvar.html("Contact Turners");
      	});
      }
     );
	
    // select #flowplanes and make it scrollable. use circular and navigator plugins
	$("#flowpanes").scrollable({size: 1}).circular().mousewheel(400).navigator({

		// select #flowtabs to be used as navigator
		navi: "#flowtabs",

		// select A tags inside the navigator to work as items (not direct children)
		naviItem: 'a',

		// assign "current" class name for the active A tag inside navigator
		activeClass: 'current'

	});

	//form submit checker
	function isValidEmailAddress(emailAddress) {
		var pattern = new RegExp(/^(("[\w-\s]+")|([\w-]+(?:\.[\w-]+)*)|("[\w-\s]+")([\w-]+(?:\.[\w-]+)*))(@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$)|(@\[?((25[0-5]\.|2[0-4][0-9]\.|1[0-9]{2}\.|[0-9]{1,2}\.))((25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\.){2}(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\]?$)/i);
		return pattern.test(emailAddress);
	}
	$("#sendContact").click( function(){
		var steperrors = "";
		var useremail = $("#email").val();
		//first test that the name has a value
		if (!$("#name").val()){
    		$("#name").addClass("error"); $("label[for='name']").addClass("error");
    		$("p[rel='name']").html("Please enter your name!");
      		steperrors = "true";
    	}else{
    		$("#name").removeClass("error"); $("label[for='name']").removeClass("error");
    		$("p[rel='name']").html("&nbsp;");
    	}
		//check if the email address has something written it it
		if (!$("#email").val()){
    		$("#email").addClass("error"); $("label[for='email']").addClass("error");
    		$("p[rel='email']").html("Please enter your email address!");
      		steperrors = "true";
    	}else if(!isValidEmailAddress(useremail)){
			$("#email").addClass("error"); $("label[for='email']").addClass("error");
    		$("p[rel='email']").html("Please enter a valid email.");
      		steperrors = "true";
		}else{
    		$("#email").removeClass("error"); $("label[for='email']").removeClass("error");
    		$("p[rel='email']").html("&nbsp;");
    	}
    	//check a phone number has been written
    	if (!$("#telephone").val()){
    		$("#telephone").addClass("error"); $("label[for='telephone']").addClass("error");
    		$("p[rel='telephone']").html("Please enter your telephone!");
      		steperrors = "true";
    	}else{
    		$("#telephone").removeClass("error"); $("label[for='telephone']").removeClass("error");
    		$("p[rel='telephone']").html("&nbsp;");
    	}
    	//check that there is a message
    	if (!$("#message").val()){
    		$("#message").addClass("error"); $("label[for='message']").addClass("error");
    		$("p[rel='message']").html("You have not written a message!");
      		steperrors = "true";
    	}else{
    		$("#message").removeClass("error"); $("label[for='message']").removeClass("error");
    		$("p[rel='message']").html("&nbsp;");
    	}
    	//send focus to the first error if there is an error
    	if(steperrors!=""){
    		$("#contact input.error:first").focus().select();
    	}else{
    		// the actual sending function
    		//Get the data from all the fields
			var name = $('#name');
			var email = $('#email');
			var telephone = $('#telephone');
			var message = $('#message');
			//organize the data properly
			var data = 'name=' + name.val() + '&email=' + email.val() + '&telephone=' + telephone.val() + '&message='  + encodeURIComponent(message.val());
				
			//display 'sending email'
			$('#contact_form_send').html('<img src="myimages/ajax-loader.gif" alt="sending message" style="background:none; border:0; outline:0; position:relative; float:none;" /><br /><h6 style="color:#000030; font-weight:normal; letter-spacing:1px; margin-top: 10px;">{ SENDING MESSAGE }</h6>');
			
			//float out the contact form and bring in the new div
			$("#contact").hide("slide", { direction: "right" }, 600, function(){
				$("#contact_form_send").show("slide", { direction: "left" }, 800);
			});
			
			//start the ajax
			$.ajax({
				//this is the php file that processes the data and send mail
				url: "mailing.php",	
			
				//GET method is used
				type: "GET",

				//pass the data			
				data: data,		
			
				//Do not cache the page
				cache: false,
			
				//success
				success: function (html) {				
					//if mailing.php returned 1/true (send mail success)
					if (html==1) {
						$("#contact input:not(#sendContact)").val("");
    					$("#contact textarea").val("");			
						$('#contact_form_send').html('<p>Your message has been sent to TURNERS Birmingham.</p><p style="margin-bottom: 25px; border-bottom: 1px solid silver;">We will get back to you as soon as possible...</p><button id="newmessage">&laquo; Send a New Message</button>');
					$("#newmessage").click( function(){
						$("#contact_form_send").hide("slide", { direction: "left" }, 600, function(){
							$("#contact").show("slide", { direction: "right" }, 800);
						});
					});

					//if mailing.php returned 0/false (send mail failed)
					} else{
						$('#contact_form_send').html('<p style="color:red; margin-bottom: 25px; border-bottom: 1px solid silver;">Sorry. It was not possible to send your message at this time. Please try again later.</p><button id="tryagain">&laquo; Try Again?</button>');
						$("#tryagain").click( function(){
							$("#contact_form_send").hide("slide", { direction: "left" }, 600, function(){
								$("#contact").show("slide", { direction: "right" }, 800);
							});
						});
					}				
				}		
			});

    	}
	});
});
