$(document).ready(
	function()
	{
		
		function checkEmail(email)
		{	
			var pattern = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
			var emailVal = $("#" + email).val();
			return pattern.test(emailVal);
		}
		
	$(":submit").click(function() {	
			
			// First, disable the form from submitting
			$('#newsletterform').submit(function() { return false; });
			
			// Grab form action
			formAction = $("#newsletterform").attr("action");
			
			// Hack together id for email field
			emailId = formAction.replace("http://ctsfishing.createsend.com/t/r/s/", "");
			emailId = emailId.replace("/", "");
			emailId = emailId + "-" + emailId;
			
			// Validate email address with regex
			if (!checkEmail(emailId)) 
			{
				alert("Please enter a valid email address");
				return;
			}
			
			// Serialize form values to be submitted with POST
			var str = $("form").serialize();
			
			// Add form action to end of serialized data
			final = str + "&action=" + formAction;
			
			// Submit the form via ajax
			$.ajax({
				url: "proxy.php",
				type: "POST",
				data: final,
				success: function(html){
					$("#nletterform").hide(); // If successfully submitted hides the form
					$("#confirmation").fadeIn("slow");  // Shows "Thanks for subscribing" div
					// Fire off Google Analytics fake pageview
					var pageTracker = _gat._getTracker("UA-8747216-1");
					pageTracker._trackPageview("/newsletter_signup");
					var pageTracker2 = _gat._getTracker("UA-8775028-1");
					pageTracker2._trackPageview("/newsletter_signup");
				}
			});
		});
	
		$('#ykuiuh-ykuiuh').focus(function()
			{	
				var femail = $('#ykuiuh-ykuiuh').val();
				if (femail == "Email Address"){
					$('#ykuiuh-ykuiuh').val("");
				}
				return false;
			}
		);
		
		$('#ykuiuh-ykuiuh').blur(function()
			{	
				var femail = $('#ykuiuh-ykuiuh').val();
				if (femail == ""){
					$('#ykuiuh-ykuiuh').val("Email Address");
				}
				return false;
			}
		);
		
		$('.datatable tr').mouseover(function()
			{
				$(this).addClass('row-hilite');
				return false;
			}
		);
		
		$('.datatable tr').mouseout(function()
			{
				$(this).removeClass('row-hilite');
				return false;
			}
		);
		
		//front page form
		
		$('#follow-name').focus(function()
			{	
				var fname = $('#follow-name').val();
				if (fname == "name"){
					$('#follow-name').val("");
				}
				return false;
			}
		);
		
		$('#follow-name').blur(function()
			{	
				var fname = $('#follow-name').val();
				if (fname == ""){
					$('#follow-name').val("name");
				}
				return false;
			}
		);
		
		$('#follow-email').focus(function()
			{	
				var femail = $('#follow-email').val();
				if (femail == "email"){
					$('#follow-email').val("");
				}
				return false;
			}
		);
		
		$('#follow-email').blur(function()
			{	
				var femail = $('#follow-email').val();
				if (femail == ""){
					$('#follow-email').val("email");
				}
				return false;
			}
		);
		
		$('#follow-us').submit(function()
			{	
				var myerror;
				myerror = "please fill in both fields ";
				var myplus;
				myplus = "nice one my son!";
				var fname = $('#follow-name').val();
				var femail = $('#follow-email').val();
				$('#follow-response').removeClass('form-error');
				$('#follow-response').removeClass('form-plus');
				if (fname == "" || fname =="name" || femail == "" || femail =="email"){
					$('#follow-response').empty();
					$('#follow-response').addClass('form-error');
					$('#follow-response').append(myerror);
					$('#follow-response').fadeIn();
				}else{
					var dataString = 'name='+ fname + '&email=' + femail;
					$.ajax({  
					  type: "POST",  
					  url: "follow.php",  
					  data: dataString,  
					  success: function() {  
							$('#follow-response').empty();
							$('#follow-response').addClass('form-plus');
							$('#follow-response').append(myplus);
							$('#follow-response').fadeIn();
							$('#follow-name').val("name");
							$('#follow-email').val("email");
					    }
					});  
				}
				return false;
			}
		);
		
		$('#contact-us').submit(function()
			{	
				var myerror;
				myerror = "please fill in the name, email and message fields";
				var myplus;
				myplus = "thank you for your email, we will be in touch soon!";
				var cname = $('#contact-name').val();
				var cemail = $('#contact-email').val();
				var ccompany = $('#contact-company').val();
				var cnumber = $('#contact-number').val();
				var cmessage = $('#contact-message').val();
				$('#contact-response').removeClass('form-error');
				$('#contact-response').removeClass('form-plus');
				if (cname == "" || cemail == "" || cmessage == ""){
					$('#contact-response').hide();
					$('#contact-response').empty();
					$('#contact-response').addClass('form-error');
					$('#contact-response').append(myerror);
					$('#contact-response').fadeIn();
				}else{
					var dataString = 'name='+ cname + '&email=' + cemail + '&company=' + ccompany + '&number=' + cnumber + '&message=' + cmessage;
					$.ajax({  
					  type: "POST",  
					  url: "contact.php",  
					  data: dataString,  
					  success: function() {  
					 		 $('#contact-response').hide();
							$('#contact-response').empty();
							$('#contact-response').addClass('form-plus');
							$('#contact-response').append(myplus);
							$('#contact-response').fadeIn();
							$('#contact-name').val("");
							$('#contact-email').val("");
							$('#contact-company').val("");
							$('#contact-number').val("");
							$('#contact-message').val("");
					    }
					});  
				}
				return false;
			}
		);
	
	}
);