/* ---------------------------------------------------- */
/* JQuery												*/
/* ---------------------------------------------------- */
$jQuery(document).ready(function() 
{
	/*############################################################################
	Rotating images
	############################################################################ */
	/* make the non javascript image area hidden when javascript is enabled */
	/*$jQuery("#imagedisplay").css("visibility","hidden");
	/* make the image area visible - and then fade it in */
	$jQuery("#image_rotate_header").css("visibility","visible");
	$jQuery("#image_rotate_header").fadeIn(1500);
	
	/* Fade through the images */
	$jQuery('#image_rotate_header').innerfade({ 
		speed: 3000, 
		timeout: 7000, 
		type: 'sequence', 
		containerheight: '176px'
	});
	
	/* make the non javascript image area hidden when javascript is enabled */
	/*$jQuery("#imagedisplay").css("visibility","hidden");
	/* make the image area visible - and then fade it in */
	$jQuery("#image_rotate").css("visibility","visible");
	$jQuery("#image_rotate").fadeIn(1500);
	
	/* Fade through the images */
	$jQuery('#image_rotate').innerfade({ 
		speed: 3000, 
		timeout: 7000, 
		type: 'sequence', 
		containerheight: '136px'
	});

	/* make the non javascript image area hidden when javascript is enabled */
	/*$jQuery("#imagedisplay").css("visibility","hidden");
	/* make the image area visible - and then fade it in */
	$jQuery("#latest_project_images_rotate").css("visibility","visible");
	$jQuery("#latest_project_images_rotate").fadeIn(1500);
	
	/* Fade through the images */
	$jQuery('#latest_project_images_rotate').innerfade({ 
		speed: 3000, 
		timeout: 7000, 
		type: 'sequence', 
		containerheight: '136px'
	});	
	

	/*############################################################################
	Hide any error display messages
	############################################################################ */
	$jQuery('div#error').hide();
	
	/*############################################################################
	Set up any variables required
	############################################################################ */
	var errorDelay = 2000; // the delay before error messages begin to fade out
	var error = 'no';
	
	/*############################################################################
	Initialise the Pretty Forms system
	############################################################################ */
	prettyForms();
	
	
	/*############################################################################
	Quick Contact form submission
	############################################################################ */
	$jQuery("#submit").click(function() {
		// validate and process form here 
		var contactname = $jQuery("input#contactname").val();
		var contactmethod = $jQuery("input#contactmethod").val();
		if ($jQuery("#quote").attr('checked')) {var quote = 'yes';} else {var quote = 'no';}
		if ($jQuery("#chat").attr('checked')) {var chat = 'yes';} else {var chat = 'no';}
		if ($jQuery("#info").attr('checked')) {var info = 'yes';} else {var info = 'no';}
		if ($jQuery("#visit").attr('checked')) {var visit = 'yes';} else {var visit = 'no';}
		
		// Check they've left a contact name
		if (contactname == "") {
			$jQuery("div#error").html("Please enter your name");
			var error = 'yes';
		}
		// Check they've left contact details
		else if (contactmethod == "") {
			$jQuery("div#error").html("Please enter contact details");
			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('#submit').attr('disabled','disabled');
			$jQuery("div#quick_contact_header").hide();
			$jQuery("div#error").fadeIn(800);
			// After a specified time, fade message away
			window.setTimeout(function() {
				$jQuery("div#error").fadeOut(800, function() {
					$jQuery("div#quick_contact_header").fadeIn(800);
					// reenable the submit button
					$jQuery('#submit').attr('disabled','');
				});		
			}, errorDelay);
			return false;
		}
		
		// ensure the error message div is hidden if successful
		$jQuery("div#error").hide();
		
		var dataString = 'contactname='+ contactname + '&contactmethod=' + contactmethod + '&quote=' + quote + '&chat=' + chat + '&info=' + info + '&visit=' + visit;  
			
		alert(dataString);
		return false;
		
		$jQuery.ajax({  
			type: "POST",  
			url: "includes/process_quick_contact.php",  
			data: dataString,  
			success: function() {  
				$jQuery('#form').html("<div id='message'></div>");  
				$jQuery('#message').html("<h1>Thank you for contacting us.</h1>")
				.append("<p>One of our team will be in touch soon.</p>")
				.hide()  
				.fadeIn(1500);  
			}  
		});  
		return false; 
	});
	
	/*############################################################################
	Main Contact form submission
	############################################################################ */
	$jQuery("#submit_contact").click(function() {
		// validate and process form here 
		var contactname = $jQuery("input#contactname").val();
		var company = $jQuery("input#company").val();
		var number = $jQuery("input#number").val();
		var email = $jQuery("input#email").val();		
		var questions = $jQuery("textarea#questions").val();			
			
		// Check they've left a contact name
		if (contactname == "") {
			var errormessage = "Please enter your name";
			var error = 'yes';
		}
		// Check they've left company details
		else if (company == "") {
			var errormessage = "Please enter your company";
			var error = 'yes';
		}
		// Check they've left a contact number
		else if (number == "") {
			var errormessage = "Please enter your number";
			var error = 'yes';
		}
		// Check they've left an email address
		else if (email == "") {
			var errormessage = "Please enter your email";
			var error = 'yes';
		}		
		// Check they've left a query
		else if (questions == "") {
			var errormessage = "Please enter your query";
			var error = 'yes';
		}			
		
		// If they've provided an email address, check it's in a valid format
		if (email != "") {
			if (!/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test(email)) {
				var errormessage = "That email is not valid";
				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('#submit').attr('disabled','disabled');
			$jQuery("div#error_content").hide();
			$jQuery("div#error_content").html(errormessage);
			$jQuery("div#error_content").fadeIn(800);
			// After a specified time, fade message away
			window.setTimeout(function() {
				$jQuery("div#error_content").fadeOut(800, function() {
					// re-enable the submit button
					$jQuery('#submit').attr('disabled','');
				});		
			}, errorDelay);
			return false;
		}
		
		// ensure any error messages are hidden if successful
		$jQuery("div#error_content").html("<!-- -->");
		
		var dataString = 'contactname='+ contactname + '&company=' + company + '&number=' + number + '&email=' + email + '&questions=' + questions; 
		
		$jQuery.ajax({  
			type: "POST",  
			url: "includes/process_contact.php",  
			data: dataString,  
			success: function() {  
				$jQuery('#form').html("<div id='message'></div>");  
				$jQuery('#message').html("<h1>Thank you for contacting us.</h1>")
				.append("<p>One of our team will be in touch soon.</p>")
				.hide()  
				.fadeIn(1500);  
			}  
		});  
		return false; 
	});	
	
});