$(document).ready(function() {
		   
	// Puts tint over site contents
	$('#subscribePopupBack').css({"opacity":"0.75"});

	// Default value for subscriber text field
	var $defaultInputText = "Type your email address here";

	// Sets value of subscriber field as Your Email Address and adjust color
	$('input#subscriberEmail').val($defaultInputText).css({'color':'#666666'});
	$('input#inPostSubscriberEmail').val($defaultInputText).css({'color':'#666666'});
	
	// Erases default value when user clicks in field then returns font color back to black
	$('input#subscriberEmail').click(
		function(){
			if(this.value == $defaultInputText)
			{
				this.value = '';
				$(this).css({color:"#000000"});
			}
		}
	);
	
	$('input#inPostSubscriberEmail').click(
		function(){
			if(this.value == $defaultInputText)
			{
				this.value = '';
				$(this).css({color:"#000000"});
			}
		}
	);
	
	
	// Puts default value back in field if user didn't enter anything
	$('input#subscriberEmail').blur(
		function(){
			if(this.value == ""){$('input#subscriberEmail').val($defaultInputText).css({'color':'#666666'});}
		}
	);
	
	$('input#inPostSubscriberEmail').blur(
		function(){
			if(this.value == ""){$('input#inPostSubscriberEmail').val($defaultInputText).css({'color':'#666666'});}
		}
	);
	
	// Do these actions if the user clicks the enter while in the subscriberEmail text box
	$('input#subscriberEmail').keypress(function(e) {
	    if(e.keyCode == 13)
		{						   
			$('#subscribePopupBack').css({"display":"block","visibility":"visible","opacity":"0.0"});
			$('#subscribePopupBack').animate({"opacity":"0.75"});
			$('#subscribePopup').fadeIn();
			$('#popupClose').fadeIn();
	
			// If the user did not enter anything in the input, do not carry value over,
			// else bring email over to the iframe popup
			if ($('input#subscriberEmail').val() == $defaultInputText) {
				var myFrame = $('#subscribeFormPopup');
				myFrame.contents().find('#sourceValue').val("sidebarpopup");}
			else {
				var $scriberCarryOver = $("input#subscriberEmail").val();
				var myFrame = $('#subscribeFormPopup');
				myFrame.contents().find('#subscriberPopin').val($scriberCarryOver);
				myFrame.contents().find('#sourceValue').val("sidebarpopup");
			}
			
			return false;
		}
	});
	
	$('input#inPostSubscriberEmail').keypress(function(e) {
	    if(e.keyCode == 13)
		{						   
			$('#subscribePopupBack').css({"display":"block","visibility":"visible","opacity":"0.0"});
			$('#subscribePopupBack').animate({"opacity":"0.75"});
			$('#subscribePopup').fadeIn();
			$('#popupClose').fadeIn();
	
			// If the user did not enter anything in the input, do not carry value over,
			// else bring email over to the iframe popup
			if ($('input#inPostSubscriberEmail').val() == $defaultInputText) {
				var myFrame = $('#subscribeFormPopup');
				myFrame.contents().find('#sourceValue').val("inpostpopup");}
			else {
				var $scriberCarryOver = $("input#inPostSubscriberEmail").val();
				var myFrame = $('#subscribeFormPopup');
				myFrame.contents().find('#subscriberPopin').val($scriberCarryOver);
				myFrame.contents().find('#sourceValue').val("inpostpopup");
			}
			
			return false;
		}
	});
	

	// Do these actions if the user clicks the subscribe button
	$("#subscribeSubmit").click(function() {
									   
		$('#subscribePopupBack').css({"display":"block","visibility":"visible","opacity":"0.0"});
		$('#subscribePopupBack').animate({"opacity":"0.75"});
		$('#subscribePopup').fadeIn();
		$('#popupClose').fadeIn();

		// If the user did not enter anything in the input, do not carry value over,
		// else bring email over to the iframe popup
		if ($('input#subscriberEmail').val() == $defaultInputText) {
			var myFrame = $('#subscribeFormPopup');
			myFrame.contents().find('#sourceValue').val("sidebarpopup");}
		else {
			var $scriberCarryOver = $("input#subscriberEmail").val();
			var myFrame = $('#subscribeFormPopup');
			myFrame.contents().find('#subscriberPopin').val($scriberCarryOver);
			myFrame.contents().find('#sourceValue').val("sidebarpopup");
		}
		
		return false;

	});
	
	$("#inPostSubscribeSubmit").click(function() {
				   
		$('#subscribePopupBack').css({"display":"block","visibility":"visible","opacity":"0.0"});
		$('#subscribePopupBack').animate({"opacity":"0.75"});
		$('#subscribePopup').fadeIn();
		$('#popupClose').fadeIn();

		// If the user did not enter anything in the input, do not carry value over,
		// else bring email over to the iframe popup
		if ($('input#inPostSubscriberEmail').val() == $defaultInputText) {
			var myFrame = $('#subscribeFormPopup');
			myFrame.contents().find('#sourceValue').val("inpostpopup");}
		else {
			var $scriberCarryOver = $("input#inPostSubscriberEmail").val();
			var myFrame = $('#subscribeFormPopup');
			myFrame.contents().find('#subscriberPopin').val($scriberCarryOver);
			myFrame.contents().find('#sourceValue').val("inpostpopup");
		}
		
		return false;

	});


	
	// Do these actions if the user clicks the subscribe button
	$("a.newsletterSignup").click(function() {
									   
		$('#subscribePopupBack').css({"display":"block","visibility":"visible","opacity":"0.0"});
		$('#subscribePopupBack').animate({"opacity":"0.75"});
		$('#subscribePopup').fadeIn();
		$('#popupClose').fadeIn();
		
	});


	// **********[ Field Validation ]**********
	$("#txtFirstName").blur(function() {
		validName();
	});
	$("#subscriberPopin").blur(function() {
		validemail();
	});
	$("#txtIndustry").blur(function() {
		validIndustry();
	});
    $("#txtZip").blur(function() {
		validZip();
	});
	// **********[ End Of Field Validation ]**********

	// Removes the tint over content if user clicks outside of subscribe box
	$('#subscribePopupBack').click(function() {
		$(this).hide();
		$('#subscribePopup').hide();
	});
	
	// Hides the popup if user clicks 'x' or 'close window' text
	$('a.close').click(function() {
		$('#subscribePopupBack').hide();
		$('#subscribePopup').hide();
	});
	
}); // End of Document Ready


// ****************************************[ Validation Functions ]****************************************


function validemail() {
	// if email field if blank throw error
	if (!$("#subscriberPopin").val()){
		$("#subscriberPopin").addClass("inputErrors");
		$("#subscriberPopin").val("Please enter an email address");
	}
	else {
		// if email field returns valid
		if (validateEmail($("#subscriberPopin").val())) {
			$("#subscriberPopin").removeClass("inputErrors");
		}
		else
		{
			$("#subscriberPopin").addClass("inputErrors");
			$("#subscriberPopin").val("Please enter a valid email address");
		}
	}
}

function validateEmail(email) {
	var val = true;
	if (!email) val = false;
	if (email.indexOf('@') < 0) val = false;
	if (email.indexOf('.') < 0) val = false;
	if (email.lastIndexOf('.') <= email.lastIndexOf('@') + 1) val = false;
	return val;
}

function validName() {
	// if name field if blank throw error
	if ($("#txtFirstName").val() < 2){
		$("#txtFirstName").addClass("inputErrors");
		$("#txtFirstName").val("Please enter your name");
	}
	else if ($("#txtFirstName").val() == "Please enter your name"){
		$("#txtFirstName").addClass("inputErrors");
		$("#txtFirstName").val("Please enter your name");
	}
	else
	{$("#txtFirstName").removeClass("inputErrors");}
}

function validIndustry() {
	// if industry field if blank throw error
	if ($("#txtIndustry").val() < 2){
		$("#txtIndustry").addClass("inputErrors");
		$("#txtIndustry").val("Please enter your industry");
	}
	else if ($("#txtIndustry").val() == "Please enter your industry"){
		$("#txtIndustry").addClass("inputErrors");
		$("#txtIndustry").val("Please enter your industry");
	}
	else
	{$("#txtIndustry").removeClass("inputErrors");}
}

function validZip() {
	// if zip code field if blank throw error
	if (!$("#txtZip").val()){
		$("#txtZip").addClass("inputErrors");
		$("#txtZip").val("Please enter your zip code");
	}
	else if ($("#txtZip").val() == "Please enter your zip code"){
		$("#txtZip").addClass("inputErrors");
		$("#txtZip").val("Please enter your zip code");
	}
	else
	{$("#txtZip").removeClass("inputErrors");}
}
