$(function(){
	$("#subForm input:submit").click(function() {	
			
		// First, disable the form from submitting
		$('form#subForm').submit(function() { return false; });
		
		// Grab form action
		formAction = $("form#subForm").attr("action");
		
		// Hacking together id for email field
		// Replace the xxxxx below:
		// If your form action were http://mysiteaddress.createsend.com/t/r/s/abcde/, then you'd enter "abcde" below
		emailId = "jicjt";
		emailId = emailId.replace("/", "");
		emailId = emailId + "-" + emailId;
		
		// Validate email address with regex
		if (!checkEmail(emailId)) 
		{
			alert("Please enter a valid email address");
			return false;
		}
		
		// Serialize form values to be submitted with POST
		var str = $("form#subForm").serialize();
		
		// Add form action to end of serialized data
		final = str + "&action=" + formAction;
		
		// Submit the form via ajax
		$.ajax({
			url: "../mailinglist-proxy.php",
			type: "POST",
			data: final,
			success: function(html){
			      $("#theForm").fadeOut(300, function () { // If successfully submitted hides the form
			        $("#confirmation").fadeIn();  // Shows "Thanks for subscribing" div
			      });
			}
		});
	});
	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);
	}
	/* NOT CURRENTLY USED
	//newsletter email field behaviors
	$('input#newsletterEmail').addClass("inactive");
	$('input#newsletterEmail').val("email address");
	$('input#newsletterEmail').focus(function(){
		if($(this).val()=="email address"){
			$(this).removeClass("inactive");
			$(this).val("");
		}
	});
	$('input#newsletterEmail').blur(function(){
		if($(this).val()==""){
			$(this).addClass("inactive");
			$(this).val("email address");
		}
	});
	//end newsletter email field behaviors
	var i=0; //keep track of how many form fields are empty
	$(".btn_join").click(function() { 
	 i=0; 
		var email = $('input#newsletterEmail').val();
		var email_error="an email is required";
  		if (email == "" | email == email_error | email=="email address") {
			i++;
			$('input#newsletterEmail').removeClass("inactive");
			$('input#newsletterEmail').addClass("error");
			$('input#newsletterEmail').val(email_error);	
			$('input#newsletterEmail').focus(function(){
			$(this).removeClass('error');
				if($(this).val()==email_error){
					$(this).val("");
				}
			});
      }
	  if(i!=0){
			return false;	
		}
	  var dataString = $('#newsletter form').serialize();
	  $.ajax({
		type: "POST",
		url: "process_mailingList.php",
		data: dataString,
		success: function() {
		  $('#newsletter').html("<div id='message'></div>");
		  $('#message').html("<h1>Thanks!</h1>")
		  .append("<p>You will receive your first copy of the 525creative newsletter shortly.</p>")
		  .hide()
		  .fadeIn(800)
		}
	  });
	  return false;
   });
   */
	//contact form field behaviors
	$('#contact input#name, #contact input#email').addClass("inactive");
	$('#contact input#name, #contact input#email').val("Required");
	$('#contact input#name, #contact input#email').focus(function(){
		if($(this).is(".inactive")){
			$(this).removeClass("inactive");
			$(this).val("");
		}
	});
	$('#contact input#name, #contact input#email').blur(function(){																 
		if($(this).val()==""){
			$(this).addClass("inactive");
			$(this).val("Required");
		}
	});
	$('#contact input#everything').click(function(){
		if($('#contact input#everything').attr('checked')==true){
    		$('#contact .projectType input:checkbox').attr('checked', true);
		} else{
			$('#contact .projectType input:checkbox').attr('checked', false);
		}
	});
	$('span.disclaimer').remove(); //this gets rid of the disclaimer about required fields if Javascript is enabled
	$('#contact input:checkbox').click(function(){
		if($(this).attr('checked')==false){
			$('#contact input#everything').attr('checked', false);
		}
    });
	//end contact form field behaviors
	var i=0; //keep track of how many form fields are empty
	$("#contact .btn_submit").click(function() { 
	 i=0;
     // validate and process form here
	  var name = $("input#name").val();
	  var name_error="Don't forget your name";
	  if (name == "" | name == name_error | name=="Required") {
			i++;
			$('input#name').removeClass("inactive");
			$('input#name').addClass("error");	
			$('input#name').val(name_error);
			$('input#name').after('<img style="margin:10px 0 0 6px;" src="../images/bullet_redArrow_left.gif" />');	
			$('input#name').focus(function(){
				$(this).removeClass("error");
				if($(this).val()==name_error){					
					$(this).val("");
				}
		});
      }
		var email = $('input#email').val();
		var email_error="We need your email";
  		if (email == "" | email == email_error | email=="Required") {
			i++;
			$('input#email').removeClass("inactive");
			$('input#email').addClass("error");
			$('input#email').val(email_error);	
			$('input#email').after('<img style="margin:10px 0 0 6px;" src="../images/bullet_redArrow_left.gif" />');	
			$('input#email').focus(function(){
			$(this).removeClass('error');
				if($(this).val()==email_error){
					$(this).val("");
				}
			});
      }
	  if(i!=0){
		  $('html, body').animate({scrollTop:0}, 'slow');
		  return false;	
		}
	  var dataString = $('#contact form').serialize();
	  //alert (dataString);return false;
	  $.ajax({
		type: "POST",
		url: "../_includes/process_contact.php",
		data: dataString,
		success: function() {
		  $('html, body').animate({scrollTop:0}, 'slow');
		  $('#contact_form').html("<div id='message'></div>");
		  $('#message').html("<h2>Thanks, "+name+"!</h2>")
		  .append("<p>Someone will be in touch with you shortly to discuss your project.</p>")
		  .hide()
		  .fadeIn(800)
		}
	  });
	  return false;
   });
});