
	var add_text;
	var add_name;
	var add_category;
	var disablepost = false;
	var newPostID = false;
	var anon;
	
	
	function addYo()
	{
		if ( tab == "live" )
		{
			animatedcollapse.toggle('addform');
		}
		else { window.location='index.php?t=live&showadd=true'; }
	}
	
	
	function showAddLoading()
	{
		$("#addloading").css("display","block");
		$("#add_submit").attr("disabled","disabled");
	}
	
	function hideAddLoading()
	{
		$("#addloading").css("display","none");
		$("#add_submit").attr("disabled","");
	}
	
	
	function goPostAnon()
	{
		$("#add_name").attr( 'value' , defaulttext['add_name'] );
		setInputClass('add_name');
		
		$("#add_anon").attr('value','1');
		
		$("#add_registeredname").css('display','none');
		$("#add_customname").css('display','');
	}
	

	function goAdd()
	{
		showAddLoading();
		preventSpam('add','cleanNewPost()');
	}	
	
	
	
	
	function html_entity_decode(str) 
	{
  		var ta=document.createElement("textarea");
  		ta.innerHTML=str.replace(/</g,"&lt;").replace(/>/g,"&gt;");
  		return ta.value;
	}
	
	
	
	function cleanNewPost()
	{
		
		add_category = $("#add_category").attr('value');
		add_text = $("#add_text").val();
		add_name = $("#add_name").val();
		
		//Process post text
		$.ajax({
		type: "POST",
		url: "processtext.php",
		data: { what: "add_text" , text: urlencode(add_text) },
			success: function(resp)
			{
				//Post_text response
				resp = $.trim(resp);

				//add it to the preview
				prevtext = resp.replace("amirite?", "<strong>amirite?</strong>");
				prevtext = prevtext.replace("am I right?", "<strong>am I right?</strong>")
				
				$("#addpreview_text").html( prevtext );
				
				//post it back to the form
				$("#add_text").attr('value', resp );
				
				//and set it as a variable
				add_text = resp;
				
						//Process post name
						$.ajax({
						type: "POST",
						url: "processtext.php",
						data: { what: "add_name" , text: urlencode(add_name) },
							success: function(resp){
								//Post_name response
					
									name = $.trim(resp);
								
									//add it to the preview
										$("#addpreview_name").text(name);
										
										//get the current date
										$.get("currentdate.php", 
											function(date)
											{
												$("#addpreview_date").text(date);
											}
										);
										
										$("#addpreview_category").text(add_category);
									
									//post it back to the form
									$("#add_name").attr('value',name);
									
									//set it as a variable
									add_name = name;
									
									//ad set it as a cookie
									$.cookie('name', name, { expires: 9001 , domain: '.amirite.net' });
									
								loadAddPopup();

							}
						});
			}
		});
		
	}
		
	
	

	function loadAddPopup() 
	{
		//Check for blank post
		if ( !add_text || add_text == "Write your message" ) 
		{ 
			$("#add_preview_content").css("display","none");
			$("#add_preview_error").css("display","block");
			centerPopup("#addPopup");
			loadPopup("#addPopup");
			hideAddLoading();
			return false;
		}
		else {
			$("#add_preview_error").css("display","none");
			$("#add_preview_content").css("display","block");
		}
		
		hideAddLoading();
		centerPopup("#addPopup");
		loadPopup("#addPopup");
		
	}
	
	
	
	function goToCaptcha()
	{
		//Captcha has been disabled. Go straight to adding it to the databse
		$.cookie('passedCaptcha', true, { expires: 9001 });
		
		if ( $.cookie('passedCaptcha') ) { addToDB(); }
		else { animatedcollapse.show('add_captcha'); }
	}
	
		
	function submitCaptcha()
	{
		checkCaptcha( "addToDB()" );
	}
	

	
	function checkCaptcha( actionOnCorrect )
	{
		
		$.ajax({
		type: "POST",
		url: "processcaptcha.php",
		data: "challenge="+ $("#recaptcha_challenge_field").attr('value') +"&response=" + $("#recaptcha_response_field").attr('value'),
		success: function(resp){  
		
			if (resp == "no") 
			{
				$("#captcha_error").css('display','block');
			}			
			else 
			{ 
				$.cookie('passedCaptcha', true, { expires: 9001 });
				eval(actionOnCorrect);
			}
		}		
	    });		
	}
	
	
	
	
	
	function addToDB()
	{

		if ( disablepost != true ) { 
		
			disablepost = true;
			
			userID = $("#add_userID").attr('value');
			anon = $("#add_anon").attr('value');
			
			$.ajax({
			type: "POST",
			url: "addpost.php",
			data: "text=" + urlencode(add_text) + "&name=" +  urlencode(add_name) + "&category=" + add_category + "&userID=" + userID  + "&anon=" + anon,
			success: function(newID){
				
				newID = $.trim(newID);
				newPostID = newID;
				
				url = 'www.amirite.net/post'+newID;
				html = '<a href="http://' + url + '" id="newpostlink">' + url + '</a>';
				
				pageTracker._trackPageview('madenewpost.php');
				
				$("#add_result").html(html);
				
				/*$("#newpostlink").click(function () {
						$("#addform").css("display","none");
						return true;
				});*/
				
				vote(newID,'up',true);
				
				//add_text = false; 
				//add_name = false;
				animatedcollapse.show('add_done');
				
				$("#closeAddLink").click(function () {
												   
						$("#add_text").attr('value',defaulttext["add_text"]);
						$("#add_text").addClass("defaultinput");						   
			   
						getNewPost(newID);
						closePopup();
						animatedcollapse.hide('addform');
						$("#closeAddLink").unbind("click");
						setTimeout('disablepost = false;',1000);
						
						if ( facebook_uid != false && $("#add_anon").attr('value') != '1' ) { setTimeout('facebook_postpost('+newID+');',1000); }
						
						return false;
				});
				
			}
			});
		}
	}	
	
	
	
	
	function facebook_postpost(postID)
	{
		FB.ensureInit(function () {
								
			FB.Connect.streamPublish(
				html_entity_decode( add_text ) , 
				{'name':'Do you agree with '+facebook_firstname+'? Vote at www.amirite.net','href':'http://www.amirite.net/post'+postID}, 
				[
					{'text':'Yeah You Are!', 'href':'http://www.amirite.net/vote.php?ID='+postID+'&vote=up&noscript=true'}
				] , null , null , facebook_savepostID , facebook_autopublish
			);
			
		}); 
	}
	
	function facebook_savepostID(ID,exception)
	{
		if ( ID )
		{
			$.post("fb_savepostid.php", { postID: newPostID, fbID: ID } );
		}
	}		
			
	
	
	function getNewPost(postID)
	{
		$.get("showsinglepost.php", { ID: postID },
  			function(resp){
				
				$("#posts").prepend(resp);
				
				animatedcollapse.addDiv('post'+postID+'share', 'fade=1,speed=300');	
				animatedcollapse.init();
				
				setTimeout( "$('#post"+postID+"').fadeIn(1000);" , 1500);
				
				lastID = postID;
				$.get( "getpostapproveddate.php", { postID: postID }, function(resp){ lastapproveddate = resp; } );
  		});
	}
	
	
	
	function preventSpam(type,actionOnAllow)
	{		
		$.get("spamcheck.php", { type: type },
  			function(resp){
				
				resp = $.trim(resp);
				
				if (resp == "okay") { 
				
					if ( type == "comment") {
						$("#comment_captcha_content").css("display","block");
						$("#comment_submit_spam").css("display","none");
					}
					else if ( type =="add") {
						$("#add_preview_content").css("display","block");
						$("#add_preview_spam").css("display","none");
					}
					else if ( type =="report") {
						$("#report_step1_content").css("display","block");
						$("#report_step1_spam").css("display","none");
					}
				

					eval(actionOnAllow);
				}
				else {
					
					$("#spamtimeout"+type).text(resp);
					
					if ( type == "comment") {
						$("#comment_captcha_content").css("display","none");
						$("#comment_submit_spam").css("display","block");
						centerPopup("#commentPopup");
						loadPopup("#commentPopup");
						hideCommentLoading();
					}
					else if ( type == "add") {
						$("#add_preview_content").css("display","none");
						$("#add_preview_spam").css("display","block");
						centerPopup("#addPopup");
						loadPopup("#addPopup");
						hideAddLoading();
					}
					else if ( type == "report") {
						$("#report_step1_content").css("display","none");
						$("#report_step1_spam").css("display","block");
						centerPopup("#reportPopup");
						loadPopup("#reportPopup");
					}
					
				}
  		});
	}

