function IsValidEmail(email){
	var filter = /^([\w-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([\w-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$/;
	return filter.test(email);
}
function autoFill(id, v){
	$(id).css({ color: "#b2adad" }).attr({ value: v }).focus(function(){
		if($(this).val()==v){
			$(this).val("").css({ color: "#333" });
		}
	}).blur(function(){
		if($(this).val()==""){
			$(this).css({ color: "#b2adad" }).val(v);
		}
	});	
}

function commentReply (location,commentid){
	window.location.hash=location;
	$('#leaveCommentTitle').text('Leave a Reply');
	$('input[name=comment_reply_id]').val(commentid);
	$('#replyto').html('Reply to comment (<a href="javascript:void(0);" onclick="javascript:commentReplyRemove();">remove</a>)');
}
function commentReplyRemove (){
	window.location.hash=location;
	$('#leaveCommentTitle').text('Leave a Comment');
	$('input[name=comment_reply_id]').val('');
	$('#replyto').html('');
}


$(document).ready(function(){

	$('#reportBroken').click(function() {
	 	$('#reportBoxClose').hide();
	 	$('#reportBoxOpen').show();
	});	
	$('#brokenSubmit').click(function() {
		var email = $('#reportEmail').val();
		var detail = $('#reportDetail').val();
		var file = $('#reportFile').val();
		
	 	$.get("http://www.pppmag.com/ajax.php", { reportFile: file, email: email, detail: detail },
  			function(data){
  			if(data==1){
  				$('#reportBoxOpen').html('<span style="font-weight:bold;color:red;">Your message has been sent!</span>');
  			}else{
  				$('#reportBoxOpen').html('<span style="font-weight:bold;color:red;">There was an error, please use our contact form.</span>');
  			}
		});
		
	 	return false;
	});

	$('#download_media_kit').click(function(){
		var file = $('#print_file:checked').length;
		var edit_calendar = $('#edit_calendar:checked').length;
		var name = $('#name').val();
		var email = $('#email').val();
		if(file==0&&edit_calendar==0){
			alert("Please request a download.");
			return false;
		}
		if(name==""){
			alert("Please input a valid name.");
			return false;
		}
		if(!IsValidEmail(email)){
			alert("Please input a valid email address.");
			return false;
		}
	});

	$('#nothanks,#catfish-close').click(function() {
		var adValue = $(this).attr("name");
		$.get("http://www.pppmag.com/ajax.php", { nothanks: adValue },
   		function(data){
   			//alert(data);
   		});
	});
						   
	$(".button_read_article").mouseover(function () {
		$(this).attr("src", 'http://www.pppmag.com/images/read_article_over.png');
	});
	$(".button_read_article").mouseout(function () {
		$(this).attr("src", 'http://www.pppmag.com/images/read_article.png');
    });
	$(".button_view_digital_edition").mouseover(function () {
		$(this).attr("src", 'http://www.pppmag.com/images/view_digital_edition_over.png');
	});
	$(".button_view_digital_edition").mouseout(function () {
		$(this).attr("src", 'http://www.pppmag.com/images/view_digital_edition.png');
    });
	
	$('#forgot_password_link').click(function() {						   
		$('#forgot_password').html("<form method='post'>Email address: <input type='text' name='forgot'> <input type='submit' value='Request'></form>");
		$('#forgot').html('');							   
	});

	$('#gotoComments').click(function() {
		$.scrollTo('.article_section_footer', 400);
		return false;
	});
	
	autoFill($("#enter_first"), "first name");
	autoFill($("#enter_last"), "last name");
	autoFill($("#enter_postalcode"), "postal code");
	autoFill($("#enter_email"), "email address");
	
	$(".vote").click(function () {
		 $("[name=web_poll]:checked").each(function () {
				var voteid = $(this).val();									 
				$.get("http://www.pppmag.com/poll/request.php", { vote: voteid },
   					function(data){
     					$("#response").html(data);
   					});									 								 
              });
		return false;
	});
	
	$(".search-filter").click(function () {
		var name = $(this).attr("name");
		if(name=="all"){
			var allChecked = $("#search-filter-all:checked").val();
			if(allChecked=="on"){
				$(".search-filter").attr('checked',true);
			}else{
				$(".search-filter").attr('checked',false);
			}
		}
	});
	
	$("[id^=vote]").click(function () {
		var commentid = $(this).attr("name");
		var updown = $(this).attr("id");
		var upd = updown.split("-");
		$.get("http://www.pppmag.com/ajax.php", { vote: commentid, hud: upd[1] },
   			function(data){
     		$("span#comment_options_vote_"+commentid).html('Voted');
   		});	
	});
	
	$("#newsletter_signup").click(function () {
		var name = $("#enter_name").val();
		var first = $("#enter_first").val();
		var last = $("#enter_last").val();
		var postalcode = $("#enter_postalcode").val();
		var email = $("#enter_email").val();
		if(!IsValidEmail(email)){
			alert("Please input a valid email address.");
		}else{
		$('#newsletter_signup_status').html('<br /><img src="http://www.pppmag.com/images/spinner.gif">');
			$.get("http://www.pppmag.com/newsletters.php", { name: name, first: first, last: last, postalcode: postalcode, email: email },
   			function(data){
     			$("#newsletter_form").html(data);
   			});					 								 
        }
		return false;
		
	});
	
	$("#search-advanced").click(function() {
		$("#advanced-search").show();
		return false;
										 
	});
	
	// controls character input/counter
	$('textarea#contact_author,textarea#contact_text,textarea#emailafriend_text').keyup(function() {
		var charLength = $(this).val().length;
		var limit = 1000;
		// Displays count
		$('div#CharCount').html(charLength + ' of '+limit+' characters used');
		// Alerts when 250 characters is reached
			if($(this).val().length > limit)
			$('div#CharCount').html('<strong><font color="red">You may only have up to '+limit+' characters.</font></strong>');
	});
				   
});
