$(document).ready(function()
{
	$.ajax(
	{
		url: "getGroupFooter.php",
		cache: false,
		success: function(data)
		{
			$('#groupFooter').html(data);
		}
	});

	$(function()
	{
		if($.browser.msie)
		{
			var zIndexNumber = 1000;
			$('#middle-container *').each(function() {
				$(this).css('zIndex', zIndexNumber);
				zIndexNumber -= 10;
			});
			
			$('#masthead-container *').each(function() {
				$(this).css('zIndex', "");
			});
		}
	});


	$(".selectinput-trigger").click(function()
	{
		$(this).closest(".selectinput").find(".selectinput-options-top").show();
		return false;
	});
	
	$(".selectinput-options-top").hover(function()
	{
		
	}, function()
	{
		$(this).hide();
	});
	
	$(".selectinput-option").click(function()
	{
		var optionVal = $(this).closest("li").find(".option").val();
		$(this).closest(".selectinput").find(".selectinput-option").removeClass("current");
		$(this).addClass("current");
		$(this).closest(".selectinput").find(".selectinput-value").attr("value", optionVal);
		$(this).closest(".selectinput").find(".selectinput-trigger").html(optionVal.substring(0, 15));
		$(this).closest(".selectinput").find(".selectinput-options-top").hide();
		if(!$(this).hasClass("followLink"))
		{
			return false;
		}
	});
	
	$(".selectinput .selectinput-trigger").each(function()
	{
		var val = $(this).closest(".selectinput").find(".selectinput-value").val().substring(0, 15);
		if(val == "")
		{
			$(this).html("Select");
		}
		else
		{
			$(this).html(val);
		}
	});
	
	$(".checkboxes .radio").click(function()
	{
		
		$(this).closest(".checkboxes").find(".radio").removeClass("selected");
		$(this).addClass("selected");
		
		$(this).closest(".goals").find(".radio-input").attr("checked", "");
		$(this).closest(".checkboxinput").find(".radio-input").attr("checked", "checked");
		$(this).closest(".checkboxinput").find(".radio-input").click();
	});
	
	
	$("#subscribe-form").submit(function()
	{
		var validates = true;
		$("#newsletter_firstname").closest(".textinput").removeClass("error");
		$("#newsletter_surname").closest(".textinput").removeClass("error");
		
		
		var emailReg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;		
		
		if(!$("#newsletter_firstname").val().length)
		{
			$("#newsletter_firstname").closest(".textinput").addClass("error");
			validates = false;
		}
		
		if(!$("#newsletter_surname").val().length)
		{
			$("#newsletter_surname").closest(".textinput").addClass("error");
			validates = false;
		}
		
		if(!emailReg.test($("#newsletter_email").val()))
		{
			$("#newsletter_email").closest(".textinput").addClass("error");
			validates = false;
		}
		
		if(validates)
		{
 			$.ajax(
			{
				url: '/_inc/mailChimp/subscribe.php',
				type: 'POST',
				async: false,
				cache: false,
				timeout: 30000,
				data: { newsletter_email: $("#newsletter_email").val(), newsletter_firstname: $("#newsletter_firstname").val(), newsletter_surname: $("#newsletter_surname").val() },
				error: function(data)
				{
					$("#subscribe-error").html("Error: Please try again later").show();		
				},
				success: function(data)
				{
					var bits = data.split("~~");
					if(bits[0] == "OK")
					{
						$("#subscribe-form").fadeOut(1000);
						setTimeout(function()
						{
							$("#subscribe-error").html("Signup Successful, please check your email to confirm").fadeIn(1000);
						}, 1001);
					}
					else
					{
						$("#subscribe-error").html("Error: " + data).show();
					}
				}
			});
		}
		return false;
	});
});


  // Clear prompt from username field, if required.
  function clearUsernamePrompt() {
    var searchinput = document.getElementById("searchinput");
    if (searchinput.value == "Search for a PT") {
      searchinput.value = "";
	  searchinput.className="";
    }
  }

  // Set the prompt for the username field, if required.
  function setUsernamePrompt() {
    var searchinput = document.getElementById("searchinput");
    if (searchinput.value == "") {
      searchinput.value = "Search for a PT";
	  searchinput.className="inputbox";
    }
  }

