var current_page = "intro";

function init(layout)
{

	$("#container1").width($(window).width());
	$("#container2").width($(window).width());
	$("#container3").width($(window).width());
	$("#container4").width($(window).width());
	
	var clientWidth = $(window).width();
	var right = (clientWidth - 1000) /2;
	$("#menu").css("right",right+"px");
	$("#logo_button").css("left",right+20+"px");
	
	if(!layout)
		init_archive();
/*

	$("#page1").css("width", "1000px")

	var clientWidth = $(window).width();
	var right = (clientWidth - 1000) /2;
	$("#menu").css("right",right+"px");
	$("#subscribe").css("left",clientWidth+"px");
	$("#archive").css("left",2*clientWidth+"px");
	$("#contact").css("left",3*clientWidth+"px");*/
}

function navigate(page)
{
	var clientWidth = $(window).width();
	if(page == "subscribe")
		$("#slider").animate({"left":-clientWidth+"px"}, "slow");
	else if(page == "archive")
		$("#slider").animate({"left":-2*clientWidth+"px"}, "slow");
	else if(page == "contact")
		$("#slider").animate({"left":-3*clientWidth+"px"}, "slow");
	else if(page == "home")
		$("#slider").animate({"left":"0px"}, "slow");
	
}


function submit_form(yahoo)
{
	if(yahoo==1)
		var from_yahoo = 1;
	else
		var from_yahoo = 0;
	
	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() 
			,	yahoo: from_yahoo
			//,	cinema: $("#cinema").val() 
			//,	dvd: $("#dvd").val() 
			//,	wine: $("#wine").val() 
			//,	package: $("#package").val()}
			}
			,	function(data){
			    	
					var html = "";
					html += "	<table border=\"0\" width=\"600\" id=\"questions\">";
					html += "	<tr>";
					html += "		<td colspan=\"2\">Congratulations! You have successfully subscribed to tvBite. </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\" style=\"color:#ffffff\">Privacy Policy.</a><br /><br /></td>";
					html += "	</tr>";
					html += "	<tr>";
					html += "		<td style=\"width:200px\">Gender:</td>";
					html += "		<td>";
					html += "		<table border=\"0\" cellpadding=\"0\" cellspacing=\"0\">";
					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 style=\"width:300px\">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 style=\"width:300px\"></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 style=\"width:300px\">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 style=\"width:300px\">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 style=\"width:300px\">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 style=\"width:300px\">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;<input type=\"hidden\" id=\"user_id\" value=\""+data.id+"\"></td>";
					html += "	</tr>";
					html += "	<tr>";
					html += "		<td colspan=\"2\" align=\"center\" height=\"53\"><a href=\"javascript:submit_question_form()\"><img src=\"images/button_form_subscribe.png\" border=\"0\"></a></td>";
					html += "	</tr>";
					html += "	</table>";
					
					/*html = "<table border=\"0\" width=\"600\">";
					html += "<tr>";
					html += "	<td><img src=\"images/button_subscribe.png\"></td>";
					html += "</tr>";
					html += "<tr>";
					html += "	<td style=\"padding-left:5px\"><strong>Thank you for subscribing to tvBite.</strong></td>";
					html += "</tr>";
					html += "</table>";*/
					
					$("#subscribe").html(html);
					//$("#welcome_content").html(html);
			  }
			, "json");
	}
}


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 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\" width=\"600px\"><tr><th id=\"archive_month_header\"colspan=\"2\" align=\"left\" style=\"padding-left:0px;margin-left:0px\"><img src=\"images/button_archive.png\"></th>";
			html += "<th align=\"right\"><select onchange=\"if(this.value.indexOf('|') != 0) show_month(this.value.substring(0,this.value.indexOf('|')),this.value.substring(this.value.indexOf('|')+1,this.value.length))\">";
			var current_year = "";
			var current_month = "";
			var max_year = 0;
			var max_month = 0;
			
			
			$.each(archive, function(i)
				{
					if((new Number(max_year) <= new Number(this.year) && new Number(max_month) <= new Number(this.month)) || new Number(max_year) == 0)
					{
						max_year = this.year;
						max_month = this.month;
					}
					
					if(current_year == "" || current_year != this.year)
					{
						html += "<option style=\"background-color:#dddddd\" value=\""+this.year+"\">"+ this.year +"</option>";
					}
					html += "<option value=\""+this.month+"|"+this.year+"\">"+get_month(this.month) +"</option>";
					current_year = this.year;
				}
			);
			html += "</select></th></tr></table>";
			$("#month_list").html(html);
			show_month(max_month, max_year);
		}
	,	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\">";
			$.each(archive, function()
				{
					if(this.newsletter_id != 0)
					{
						var current_date = new Date(this.date);
						html += "<tr><td valign=\"top\" width=\"30\" id=\"archive_month_cell\" style=\"font-size:10pt\"><strong>"+current_date.getDate()+get_suffix(current_date.getDate())+"</strong></td><td style=\"font-size:10pt;\"><a style=\"color:#ffffff\" target=\"_blank\" href=\"/mailouts/"+this.newsletter+"\">"+URLDecode(this.subject)+"</a>&nbsp;</td></tr>";
					}
				}
			);
			html += "</table>";
			
			$("#day_list").html(html);
		}
	,	error:
		function(error, error2, error3)
		{
			prompt("Output",error.responseText+error2+error3);
		}
	}
	);
}

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 URLDecode(psEncodeString) 
{
  // Create a regular expression to search all +s in the string
  var lsRegExp = /\+/g;
  // Return the decoded string
  return unescape(String(psEncodeString).replace(lsRegExp, " ")); 
}

function print_email()
{

	var mailto1 = "<a style=\"color:#ffffff\" 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);
}


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: $("#user_id").val()
	},
			function(data){
		    	document.location.replace("index.html");
		  });
	
}
