function valid_email(email) 
{
   var reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
   //var address = document.forms[form_id].elements[email].value;
   if(reg.test(email) == false) {
      //alert('Invalid Email Address');
      return false;
   }
   return true;
}

var j = jQuery.noConflict();
j(document).ready(function()
{				
	j(".read-full a").click(function()
	{
		var index = j(".read-full a").index(this);
		var full_review = j(this).parent().prev();
		
		if(j(full_review).height() == review_height)
		{
			j(full_review).animate({height: review_heights[index]}, 1000);
			j(this).text("Hide Full Review");
		}
		else
		{
			
			j(full_review).animate({height: review_height}, 1000);
			j(this).text("Read Full Review");
		}
		return false;
		
	});
	
	j(".read-full-playthrough a").toggle(function()
	{
		var index = j(".read-full-playthrough a").index(this);
		var full_review = j(this).parent().prev();
		
		j(full_review).slideToggle(1000);
		j(this).text("Hide Playthrough Requirements");
		return false;
		
	},
	function()
	{
		var index = j(".read-full-playthrough a").index(this);
		var full_review = j(this).parent().prev();
		
		j(full_review).slideToggle(1000);
		j(this).text("Show Playthrough Requirements");
		return false;
		
	});
	
	var textboxes = [];
	
	j(".textbox").each(function(i){ textboxes[i] = j(this).val(); });

	j(".textbox").focus(function()
	{
		var index = j(".textbox").index(this);
		if(j(this).val() == textboxes[index])
			j(this).val("");	
	});
	
	j(".textbox").blur(function()
	{
		var index = parseInt(j(".textbox").index(this));
		if(j(this).val() == "")
			j(this).val(textboxes[index]);	
	});
	
	
	
	j(".expandable-toggler a").click(function()
	{
		if(j(".expandable").height() == expandable_min_height)
		{
			j(".expandable").animate({height:expandable_height}, 1000);
			j(".expandable-toggler a").text("Hide text");
		}
		else
		{
			j(".expandable").animate({height:expandable_min_height}, 1000);
			j(".expandable-toggler a").text("Read more");
		}
		return false;
	});
	
	j(".back-link").click(function()
	{
		history.back();
		return false;
	});
	
	j("#newsletter-form").submit(function()
	{
		var error = false;
		var params = "";
		
		j("#"+j(this).attr("id")+" input").each(function()
		{
			if(j(this).hasClass("email"))
			{
				if(!valid_email(j(this).val()))
				{
					alert("Invalid email address");
					error = true;
				}
				else
				{
					params += "&"+j(this).attr("name")+"="+j(this).val();
				}
			}
			else
			{
				params += "&"+j(this).attr("name")+"="+j(this).val();
			}
			
		});																				   
			
		if(error) return false;
		
		j.ajax({
			type: "post",
			url: '/wp-content/themes/reviewking/rpc.php',
			data: "action=newsletter-signup"+params,
			dataType: "json",
			success:function(data)
			{
				j("#newsletter-form input.email").val("Email address here");
				alert(data.status_detail);
				
			}
		});
		return false;
	});
	
	j("#competition-form").submit(function()
	{
		var error = false;
		var params = "";
		
		j("#"+j(this).attr("id")+" input").each(function()
		{
			if(j(this).hasClass("email"))
			{
				if(!valid_email(j(this).val()))
				{
					alert("Invalid email address");
					error = true;
				}
				else
				{
					params += "&"+j(this).attr("name")+"="+j(this).val();
				}
			}
			else
			{
				params += "&"+j(this).attr("name")+"="+j(this).val();
			}
			
		});																				   
			
		if(error) return false;
		
		j.ajax({
			type: "post",
			url: '/wp-content/themes/reviewking/rpc.php',
			data: "action=competition-signup"+params,
			dataType: "json",
			success:function(data)
			{
				j("#competition-form input.email").val("Email address here");
				alert(data.status_detail);
			}
		});
		return false;
	});
	
	
});
