<!--

function init_home()
{
	$.getJSON("rpc/index.cfm?action=home",
		{}
	,
    	function(json)
		{
			var latest = json.home.latest;
			var html = "<table border=\"0\" cellpadding=\"0\" cellspacing=\"0\" id=\"latest_content_table\">";
			$.each(latest, function()
				{
					html += "<tr><td><a href=\"mailouts/"+this.file+"\" target=\"_blank\">"+this.subject+"</a></td></tr>";
				}
			);
			html += "</table>";
			
			var now_showing = json.home.latest[0];
			var now_showing_text = "<strong>"+now_showing.subject + "</strong><br/>"+now_showing.intro;
			var now_showing_image = "<img width=\"100\" src=\"/mailouts/images/"+now_showing.image_name+"\">";
			var blog = json.home.blog;
			var submitted_date = new Date(blog.submitted);
			
			$("#inner_content").html(html);
			$("#now_showing_text").html(now_showing_text);
			$("#now_showing_image").html(now_showing_image);
			$("#blog_header").addClass("blog_header");
			$("#blog_month").html(get_month(submitted_date.getMonth()+1));
			$("#blog_day").html(submitted_date.getDate());
			$("#blog_subject").html(blog.subject);
			$("#blog_submitted").html("Submitted on "+blog.submitted);
			$("#blog_author").html("by "+blog.author);
			$("#blog_body").html(blog.body);
			
			var comments = json.home.comments;
			var comment_info = json.home.comment_info;
			
			var comment_count = (comments.length == 0) ? 0 : comments[0].comment_count;
	
			var html = "<br />"
			html += "<span id=\"comment_header\">Comments ("+comment_count+")</span>";
			$("#blog_comments").html(html);
			
			html = "";
			html += "<br /><br />";
			$.each(comments, function()
				{
					html += "<table class=\"comment_table\">";
					html += "<tr><th>On "+this.posted+" at "+this.posted_time+", "+this.author+" posted:</th></tr>";
					html += "<tr><td>"+this.comment+"</td></tr>"
					html += "</table>"
					html += "<br />";
				}
			);
			
			$("#blog_comments").append(html);
			
			if(comment_info)
			{
				var comment_name = comment_info.comment_name;
				var comment_email = comment_info.comment_email;
				var comment_url = comment_info.comment_url;
			}
			else
			{
				var comment_name = "";
				var comment_email = "";
				var comment_url = "";
			}
			
			html = "<br /><span id=\"comment_header\">Post A Comment</span><br /><br />";
			html += "<table id=\"comment_form_table\">";
			html += "<tr><td>Name:</td>";
			html += "<td><input class=\"input_text\" type=\"text\" name=\"comment_name\" id=\"comment_name\" value=\""+comment_name+"\"></td></tr>";
			html += "<tr><td>E-mail (won't be displayed):</td>";
			html += "<td><input class=\"input_text\" type=\"text\" name=\"comment_email\" id=\"comment_email\" value=\""+comment_email+"\"></td></tr>";
			html += "<tr><td>URL:</td>";
			html += "<td><input class=\"input_text\" type=\"text\" name=\"comment_url\" id=\"comment_url\" value=\""+comment_url+"\"></td></tr>";
			html += "<tr><td>Remember personal info?:</td>";
			html += "<td><input type=\"checkbox\" name=\"comment_remember\" id=\"comment_remember\"></td></tr>";
			html += "<tr><td valign=\"top\">Comment:</td>";
			html += "<td><textarea class=\"input_textarea\" id=\"comment_text\"></textarea></td></tr>";
			html += "<tr><td valign=\"top\"></td>";
			html += "<td align=\"right\"><input type=\"submit\" class=\"submit_button\" value=\"Post Comment\" onclick=\"post_comment()\"></td></tr>";
			html += "</table>";
			html += "<input type=\"hidden\" id=\"article_id\" value=\""+blog.id+"\">";
			//html += "<a href=\"show_comment_form()\">Post A Comment</a>";
			$("#blog_comments").append(html);
		}
	);
}

function init_archive()
{
	$.ajax(
	{
		type: "GET"
	,	url: "rpc/index.cfm?action=archive"
	,	dataType: "json"
	,	success:
    	function(json)
		{
			var archive = json.archive;
			var html = "<table border=\"0\" cellpadding=\"0\" cellspacing=\"0\">";
			var current_year = "";
			
			$.each(archive, function()
				{
					if(current_year == "" || current_year != this.year)
					{
						if(current_year != "")
						{
							html += "<tr><td><br/></td></tr>";
						}
						html += "<tr><td><strong>"+this.year +"</strong></td></tr>"
					}
					html += "<tr><td><a href=\"javascript:show_month("+this.month+","+this.year+");\">"+get_month(this.month) +"</a></td></tr>";
					current_year = this.year;
				}
			);
			html += "</table>";
			
			$("#month_list").html(html);
		}
	,	error:
		function(error, error2, error3)
		{
			prompt("Output",error.responseText+error2+error3);
		}
	}
	);
}

function get_month(month)
{
	if(month == 1)
		return "January";
	else if (month == 2)
		return "February";
	else if (month == 3)
		return "March";
	else if (month == 4)
		return "April";
	else if (month == 5)
		return "May";
	else if (month == 6)
		return "June";
	else if (month == 7)
		return "July";
	else if (month == 8)
		return "August";
	else if (month == 9)
		return "September";
	else if (month == 10)
		return "October";
	else if (month == 11)
		return "November";
	else if (month == 12)
		return "December";
}

function show_month(month, year)
{
	$.ajax(
	{
		type: "GET"
	,	url: "rpc/index.cfm?action=archive_month"
	,	data:
		{
			month: month
		,	year: year
		}
	,	dataType: "json"
	,	success:
    	function(json)
		{
			var archive = json.archive_month;
			var html = "<table border=\"0\" cellpadding=\"0\" cellspacing=\"0\" id=\"archive_month_table\">";
			html += "<tr><th id=\"archive_month_header\"colspan=\"2\">"+get_month(archive[0].month)+" "+archive[0].date.substring(archive[0].date.length-4,archive[0].date.length)+"</th></tr>";
			$.each(archive, function()
				{
					var current_date = new Date(this.date);
					html += "<tr><td width=\"30\" id=\"archive_month_cell\"><strong>"+current_date.getDate()+get_suffix(current_date.getDate())+"</strong></td><td><a target=\"_blank\" href=\"/mailouts/"+this.newsletter+"\">"+this.subject+"</a>&nbsp;</td></tr>";
				}
			);
			html += "</table>";
			
			$("#day_list").html(html);
		}
	,	error:
		function(error, error2, error3)
		{
			prompt("Output",error.responseText+error2+error3);
		}
	}
	);
}

function submit_form()
{
	
	if(isValid())
	{
		$.post("rpc/index.cfm?action=subscribe", { 
				first_name: $("#first_name").val()
			, 	last_name: $("#last_name").val()
			//,	gender: $(".gender:checked").val()
			//,	age: $("#age").val() 
			,	email: $("#email").val() 
			,	comfirm_email: $("#confirm_email").val() 
			//,	cinema: $("#cinema").val() 
			//,	dvd: $("#dvd").val() 
			//,	wine: $("#wine").val() 
			//,	package: $("#package").val()}
			}
			,	function(data){
			    	
					var html = "";
					html += "	<table border=\"0\" width=\"600\">";
					html += "	<tr>";
					html += "		<td colspan=\"2\">Congratulations! You have successfully subscribed to tvBite. </td>";
					html += "	</tr>";
					html += "	<tr>";
					html += "		<td colspan=\"2\">&nbsp;</td>";
					html += "	</tr>";
					html += "	<tr>";
					html += "		<td colspan=\"2\"><strong>The following questions are optional. </strong></td>";
					html += "	</tr>";
					html += "	<tr>";
					html += "		<td colspan=\"2\">We will never sell the information you provide us with. The optional answers you ";
					html += "		give may help us stay in business. Thanks.<br /><br />";
					html += "		For more information, please read our <a href=\"privacy.html\" target=\"_blank\">Privacy Policy.</a><br /><br /></td>";
					html += "	</tr>";
					html += "	<tr>";
					html += "		<td>Gender:</td>";
					html += "		<td width=\"200\">";
					html += "		<table border=\"0\" cellpadding=\"0\" cellspacing=\"0\" width=\"100\">";
					html += "		<tr>";
					html += "			<td>male</td>";
					html += "			<td><input type=\"radio\" class=\"gender\" name=\"gender\" value=\"m\" checked=\"true\"></td>";
					html += "			<td>female</td>";
					html += "			<td><input type=\"radio\" class=\"gender\" name=\"gender\" value=\"f\"></td>";
					html += "		</tr>";
					html += "		</table>";
					html += "		</td>";
					html += "	</tr>";
					html += "	<tr>";
					html += "		<td>Age:</td>";
					html += "		<td><input class=\"input_text\" type=\"text\" name=\"age\" id=\"age\"></td>";
					html += "	</tr>";
					html += "	<tr id=\"age_error\" style=\"display:none\">";
					html += "		<td></td>";
					html += "		<td style=\"color:red\">Age must contain a valid value.</td>";
					html += "	</tr>";
					html += "	<tr>";
					html += "		<td colspan=\"2\">&nbsp;</td>";
					html += "	</tr>";
					html += "	<tr>";
					html += "		<td>How often do you go to the cinema?</td>";
					html += "		<td>";
					html += "		<select name=\"cinema\" id=\"cinema\">";
					html += "			<option value=\"0\">Select...";
					html += "			<option value=\"1\">More than once a week";
					html += "			<option value=\"2\">Once a week";
					html += "			<option value=\"3\">Once a month";
					html += "			<option value=\"4\">Less than once a month";
					html += "			<option value=\"5\">Less than once a year";
					html += "		</select>";
					html += "		</td>";
					html += "	</tr>";
					html += "	<tr>";
					html += "		<td>How often do you buy DVDs?</td>";
					html += "		<td>";
					html += "		<select name=\"dvd\" id=\"dvd\">";
					html += "			<option value=\"0\">Select...";
					html += "			<option value=\"1\">More than once a week";
					html += "			<option value=\"2\">Once a week";
					html += "			<option value=\"3\">Once a month";
					html += "			<option value=\"4\">Less than once a month";
					html += "			<option value=\"5\">Less than once a year";
					html += "		</select>";
					html += "		</td>";
					html += "	</tr>";
					html += "	<tr>";
					html += "		<td>How many bottles of wine do you buy per month?</td>";
					html += "		<td valign=\"top\">";
					html += "		<select name=\"wine\" id=\"wine\">";
					html += "			<option value=\"0\">Select...";
					html += "			<option value=\"1\">0";
					html += "			<option value=\"2\">1-10";
					html += "		<option value=\"3\">10-20";
					html += "			<option value=\"4\">More than 20";
					html += "		</select>";
					html += "		</td>";
					html += "	</tr>";
					html += "	<tr>";
					html += "		<td>What is your favourite TV programme?</td>";
					html += "		<td><input class=\"input_text\" type=\"text\" name=\"tv_programme\" id=\"tv_programme\"><input type=\"hidden\" id=\"id\" name=\"id\" value=\"\"/></td>";
					html += "	</tr>";
					html += "	<tr>";
					html += "		<td colspan=\"2\">&nbsp;</td>";
					html += "	</tr>";
					html += "	<tr>";
					html += "		<td colspan=\"2\" align=\"center\" height=\"53\"><div class=\"subscribe_button\"><a href=\"javascript:submit_question_form()\"><img src=\"images/pixel.gif\" width=\"105\" height=\"53\" border=\"0\"></a></div></td>";
					html += "	</tr>";
					html += "	</table>";
					
					$("#welcome_content").html(html);
			  }
			, "json");
	}
}

function submit_question_form()
{
	
	
	$.post("rpc/index.cfm?action=save_optional_info", 
	{ 
			gender: $(".gender:checked").val()
		,	age: $("#age").val() 
		,	cinema: $("#cinema").val() 
		,	dvd: $("#dvd").val() 
		,	wine: $("#wine").val() 
		,	tv_programme: $("#tv_programme").val()
		,	id: $("#id").val()
	},
			function(data){
		    	document.location.replace("index.html");
		  });
	
}

function isValid()
{
		var valid = true;
		$("#first_name_error").hide();
		$("#last_name_error").hide();
		
		$("#email_error").hide();
		$("#confirm_email_error").hide();
		$("#compare_email_error").hide();
		
		if($("#first_name").val() == "")
		{
			$("#first_name_error").show();
			valid = false;
		}
		if($("#last_name").val() == "")
		{
			$("#last_name_error").show();
			valid = false;
		}
		
		if($("#email").val() == "")
		{
			$("#email_error").show();
			valid = false;
		}
		if($("#confirm_email").val() == "")
		{
			$("#confirm_email_error").show();
			valid = false;
		}
		if($("#confirm_email").val() !== $("#email").val())
		{
			$("#compare_email_error").show();
			valid = false;
		}
	
	return valid;
}

function get_suffix(d) 
{
    d = String(d);
    return d.substr(-(Math.min(d.length, 2))) > 3 && d.substr(-(Math.min(d.length, 2))) < 21 ? "th" : ["th", "st", "nd", "rd", "th"][Math.min(Number(d)%10, 4)];
}

function post_comment()
{
	$.ajax(
	{
		type: "POST"
	,	url: "rpc/index.cfm?action=post_comment"
	,	data:
		{
			article_id: $("#article_id").val()
		,	comment_name: $("#comment_name").val()
		,	comment_email: $("#comment_email").val()
		,	comment_url: $("#comment_url").val()
		,	comment_remember: $("#comment_remember").attr("checked")
		,	comment_text: $("#comment_text").val()
		}
	,	success:
    	function()
		{
			init_comments();
		}
	,	error:
		function(error, error2, error3)
		{
			prompt("Output",error.responseText+error2+error3);
		}
	}
	);
}

function init_comments()
{
	$.ajax(
	{
		type: "POST"
	,	url: "rpc/index.cfm?action=get_comments"
	,	dataType: "json"
	,	data:
		{
			article_id: $("#article_id").val()
		}
	,	success:
    	function(json)
		{
			var comments = json.comments;
			var comment_count = (comments.length == 0) ? 0 : comments[0].comment_count;
			
			var comment_info = json.comment_info;
	
			var html = "<br />"
			html += "<span id=\"comment_header\">Comments ("+comment_count+")</span>";
			$("#blog_comments").html(html);
			
			html = "";
			html += "<br /><br />";
			$.each(comments, function()
				{
					html += "<table class=\"comment_table\">";
					html += "<tr><th>On "+this.posted+" at "+this.posted_time+", "+this.author+" posted:</th></tr>";
					html += "<tr><td>"+this.comment+"</td></tr>"
					html += "</table>"
					html += "<br />";
				}
			);
			$("#blog_comments").append(html);
			
			if(comment_info)
			{
				var comment_name = comment_info.comment_name;
				var comment_email = comment_info.comment_email;
				var comment_url = comment_info.comment_url;
			}
			else
			{
				var comment_name = "";
				var comment_email = "";
				var comment_url = "";
			}
			
			html = "<br /><span id=\"comment_header\">Post A Comment</span><br /><br />";
			html += "<table id=\"comment_form_table\">";
			html += "<tr><td>Name:</td>";
			html += "<td><input class=\"input_text\" type=\"text\" name=\"comment_name\" id=\"comment_name\" value=\""+comment_name+"\"></td></tr>";
			html += "<tr><td>E-mail (won't be displayed):</td>";
			html += "<td><input class=\"input_text\" type=\"text\" name=\"comment_email\" id=\"comment_email\" value=\""+comment_email+"\"></td></tr>";
			html += "<tr><td>URL:</td>";
			html += "<td><input class=\"input_text\" type=\"text\" name=\"comment_url\" id=\"comment_url\" value=\""+comment_url+"\"></td></tr>";
			html += "<tr><td>Remember personal info?:</td>";
			html += "<td><input type=\"checkbox\" name=\"comment_remember\" id=\"comment_remember\"></td></tr>";
			html += "<tr><td valign=\"top\">Comment:</td>";
			html += "<td><textarea class=\"input_textarea\" id=\"comment_text\"></textarea></td></tr>";
			html += "<tr><td valign=\"top\"></td>";
			html += "<td align=\"right\"><input type=\"submit\" class=\"submit_button\" value=\"Post Comment\" onclick=\"post_comment()\"></td></tr>";
			html += "</table>";
			
			html += "<input type=\"hidden\" id=\"article_id\" value=\""+comments[0].article_id+"\">";
			//html += "<a href=\"show_comment_form()\">Post A Comment</a>";
			$("#blog_comments").append(html);
		}
	,	error:
		function(error, error2, error3)
		{
			prompt("Output",error.responseText+error2+error3);
		}
	}
	);
	
	
}

function unsubscribe(id)
{
	$.post("do_unsubscribe.cfm", 
	{ 
		id: id
	},
		function(data)
		{
	    	$("#welcome_content").html("You have successfully unsubscribed.");
		}
	);
}

function recommend(id)
{
	if($("#friends_email").val() != "")
	{
		$.post("do_recommend.cfm", { 
				friends_email: $("#friends_email").val()
			,	from: $("#your_name").val()
			,	email: $("#email").val()
			}
			,	function(data){
			    	$("#welcome_content").html("Your recommendation has been sent!");
			  }
			, "json");
	}
	else
	{
		$("#confirm_email_error").show();
	}
}

function print_email()
{

	var mailto1 = "<a href=\"mailto:";
	var mailto2 = "\">";
	var mailto3 = "</a>";
	var domain = "@tvbite.com";
	
	var general = mailto1 + "queries" + domain + mailto2 + "queries" + domain + mailto3;
	$("#contact_general").html(general);
	
	var advertising = mailto1 + "advertising" + domain + mailto2 + "advertising" + domain + mailto3;
	$("#contact_advertising").html(advertising);
	
	var editor = mailto1 + "editor" + domain + mailto2 + "editor" + domain + mailto3;
	$("#contact_editorial").html(editor);
	
	var jobs = mailto1 + "jobs" + domain + mailto2 + "jobs" + domain + mailto3;
	$("#contact_jobs").html(jobs);
}


//-->