$(document).ready(
	function()
	{
		/****************************************
		 * Form posting of the Free Thought Feed
		 ****************************************/
		function feed_answer(form)
		{
			$.getJSON("/ajax/load_signup_form.php",{ feed_answer : $('input#free_thought_feed_feed_answer').val(), feed_question_id : $('input[name=feed_question_id]').val()}, function(data) {
						var div = $('<div id="mdialog"></div>');
						$('body').append(div);
						div.dialog({ modal: 'true', height: '275px', width: '500px', resizable: false, draggable: false, close: function() {$(this).remove();} });
						div.append(data.html_form);

						// Validation for the FTF signup form
						$("#feed_signup").validate(
						{
							errorElement: "div",
							submitHandler: feed_signup,
							rules:
							{
								name:
								{
									required: true
								},
								email_address:
								{
									required: true,
									email: true
								},
								i_am:
								{
									required: true
								},
								terms_condition:
								{
									required: true
								},
								required_standards:
								{
									required: true
								}
							},
							messages:
							{
								name:
								{
									required: 'Please enter your first name.'
								},
								email_address:
								{
									required: 'Please enter your email address.',
									email: 'Please enter a valid email address.'
								},
								i_am:
								{
									required: 'Please make a selection.'
								},
								terms_condition:
								{
									required: 'You must agree to the terms and conditions.'
								},
								required_standards:
								{
									required: 'You must confirm your answer meets the required standards.'
								}
							},
							errorPlacement: feed_signup_error_placement
						});
			});
		}

		var unique_feed_signup_id = 0;
		function feed_signup(form)
		{
			var form_data = {
								feed_question_id : $("input[@name=\'feed_question_id\']").val(),
								feed_answer : $("input[@name=\'feed_answer\']").val(),
								name : $("input[@name=\'name\']").val(),
								email_address : $("input[@name=\'email_address\']").val(),
								i_am: $("select[@name=\'i_am\']").val()
							};
			if( $("input[@name=\'terms_condition\']").length )
			{
				form_data['terms_condition'] = $("input[@name=\'terms_condition\']").attr("checked");
			}

			var unique_id = $("input[@name=\'unique_id\']").val();

			// Check for already posted data
			if( unique_feed_signup_id != unique_id  )
			{
				unique_feed_signup_id = unique_id;
				$.post("/ajax/store_feed_post.php",
						form_data,
						function(data)
						{

							// Set lifetime to 15 min
							var cookie_life_time = 1 / 24 / 4;

							$.cookie('previous_name', $("input[@name=\'name\']").val() , {expires: cookie_life_time} );
							$.cookie('previous_email_address', $("input[@name=\'email_address\']").val() , {expires: cookie_life_time} );
							$.cookie('previous_i_am', $("select[@name=\'i_am\']").val() , {expires: cookie_life_time} );
							$.cookie('previous_terms_condition', $("input[@name=\'terms_condition\']").attr("checked") , {expires: cookie_life_time} );

							if( data == "true" )
							{
								$("#mdialog").dialog("close");
								$("#mdialog").remove();
								load_question_and_answers();
							}
							else
							{
								$("#mdialog").html("Can't process your request!!" + data);
							}
						}
					);
			}
			else
			{
				$("#mdialog").dialog("close");
				$("#mdialog").remove();
			}

			$("#free_thought_feed_feed_answer").val("");

			// Omniture tracking
			var s=s_gi(s_account);
			s.pageName=s.pageName + ':submit';
			s.t();

		}

		function feed_signup_error_placement(error, element)
		{
			// The terms and condition error placement is different
			if( element.attr("name") == "terms_condition" )
			{
				error.insertAfter(element.next());
			}
			else
			{
				// The default error placement
				error.insertAfter(element);
			}
		}


		/***************************
		 * Validation for Quiz signup form
		 ***************************/
		$("#free_thought_feed").validate(
		{
			errorElement: "div",
			submitHandler: feed_answer,
			rules:
			{
				feed_answer:
				{
					required: true,
					maxlength: 140,
					allow_html: 'no'
				}
			},
			messages:
			{
				feed_answer:
				{
					required: 'Please enter your answer.',
					maxlength: 'Please limit your answer to 140 characters.'
				}
			}
		});


		/********************************
		 * Add validation to check if HTML code is allowed to enter
		 ********************************/

		jQuery.validator.addMethod("allow_html", function(value, element, param)
			{
				if( param == 'no' )
				{
					if( value.match(/([\<])([^\>]{1,})*([\>])/i)==null )
					{
						return true;
					}
	 				else
	 				{
	 					return false;
	 				}
				}

				return true;
			}
			, "HTML tags/code are not allowed."
		);



		/*-----------------------------*
		 * Tell a Friend form
		 *-----------------------------*/
		$("a#tell_a_friend").click(
			function()
			{
				var div = $('<div id="tell_friend_popup"></div>');
				$('body').append(div);
				div.dialog({ modal: 'true', height: '340px', width: '500px', resizable: false, draggable: false, 	close: function(event, ui){ $("#tell_friend_popup").remove(); }});
				$(div).load("/tell_a_friend", function()
					{
						$("#form_friend").validate(
						{
							errorElement: "div",
							submitHandler: function()
							{
								$.post(	"/ajax/tell_friend",
									{
										your_name: $("input#form_friend_your_name").val(),
										your_email: $("input#form_friend_your_email").val(),
										friend_name: $("input#form_friend_friend_name").val(),
										friend_email: $("input#form_friend_friend_email").val(),
										message: $("textarea[name=message]").val()
									},
									function(data)
									{
										$("div#tell_friend_popup").html(data);
									}
								);
							}
						});
					}
				);
				return false;
			}
		);
	}
);

function add_bookmark( title, url )
{
	if(window.sidebar) // Firefox
	{
		window.sidebar.addPanel(title, url,'');
	}
	else if(window.opera)//Opera
	{
		var a = document.createElement("A");
		a.rel = "sidebar";
		a.target = "_search";
		a.title = title;
		a.href = url;
		a.click();
	}
	else if(document.all) //IE
	{
		window.external.AddFavorite(url, title);
	}
}

function fbs_click()
{
	u=location.href;
	t=document.title;
	window.open('http://www.facebook.com/sharer.php?u='+encodeURIComponent(u)+'&t='+encodeURIComponent(t),'sharer','toolbar=0,status=0,width=626,height=436');
	return false;
}