// JavaScript Document

//Global Variables
var registerNameTimer = 0;
var registerPassTimer = 0;
var registerEmailTimer = 0;

var contactNameTimer = 0;
var contactSubjectTimer = 0;
var contactEmailTimer = 0;
var contactMessageTimer = 0;

var editProfileEmailTimer = 0;
var editProfileAvatarTimer = 0;

var oldPassTimer = 0;
var newPassTimer = 0;
var confirmPassTimer = 0;

var wowLinkTimer = 0;
var steamLinkTimer = 0;
var xboxLinkTimer = 0;
var psnLinkTimer = 0;

var registerNameOk = 0;
var registerPassOk = 0;
var registerEmailOk = 0;

var contactNameOk = 0;
var contactEmailOk = 0;
var contactSubjectOk = 0;
var contactMessageOk = 0;

var newTopicSubjectOk = 0;
var newTopicBodyOk = 0;

var newPostBodyOk = 0;

var newCommentBodyOk = 0;

var editProfileEmailOk = 1;
var editProfileAvatarOk = 1;

var loginNameOk = 0;
var loginPassOk = 0;

//On Ready Functions
$(document).ready(function(){
	
	$(window).resize(function(){
		var documentHeight = $(document).height();
		var winH = $(window).height();  
		var winW = $(window).width();
		var messageBox = $('#messageBox');
		var loadingBox = $('#loadingBox');
		var imageBox = $('#imageBox');
		$('#modalBoxHolder').height(documentHeight);
		messageBox.css('top', (winH - messageBox.height()) / 2+$(window).scrollTop() + "px");
		messageBox.css('left', (winW - messageBox.width()) / 2+$(window).scrollLeft() + "px");
		loadingBox.css('top', (winH - loadingBox.height()) / 2+$(window).scrollTop() + "px");
		loadingBox.css('left', (winW - loadingBox.width()) / 2+$(window).scrollLeft() + "px");
		imageBox.css('top', (winH - imageBox.height()) / 2+$(window).scrollTop() + "px");
		imageBox.css('left', (winW - imageBox.width()) / 2+$(window).scrollLeft() + "px");
	});

//Functions on load
updateSession();

//Preload images
pic1= new Image(); 
pic1.src = '/images/icons/accept.png'; 

pic2= new Image(); 
pic2.src = '/images/icons/cancel.png';

pic3= new Image(); 
pic3.src = '/images/icons/error.png';

loadingPic = new Image();
loadingPic.src= 'images/icons/loading.gif';

//Interactive Element Effects

	$('#onlineUsers')
		.click(function(){
			var onlineUsers = $('#onlineUsers');
			var onlineList = $('#onlineList');
			if (onlineList.is(':hidden'))
			{
				$(this).css('backgroundColor','#222');
				$(this).css('padding','5px');
				$(this).css('border','1px solid #2a2a2a');
				$(this).css('borderBottom','1px solid #1a1a1a');
				onlineList.slideFadeShow(300);
			}
			else
			{
				onlineList.slideFadeHide(300, function(){
					onlineUsers.css('backgroundColor','');
					onlineUsers.css('padding','6px');
					onlineUsers.css('border','');
				});
			}
		});
	$('#ackAlert')
		.click(function(){
			var alertBar = $('#alertBox');
			var alertId = $(this).attr('name');
			$.ajax({
			type: 'POST',
			url: 'scripts/ackAlert.php',
			cache: false,
			async: true,
			dataType: 'json',
			data: 'alertId=' + alertId,
			success: function(response)
			{
				if (response[0].error == 0) {
					alertBar.fadeOut();
				}
			},
			error: function()
			{	
			}
		});	
	});
	
	$('#imageBox')
		.click(function() {
			var imageBox = $('#imageBox');
			if (imageBox.is(':visible'))
			{			
				imageBox.fadeOut('fast',function(){
					$('#modalBoxHolder').fadeOut('fast');
				});
			}
		});
	$('#modalBoxHolder')
		.click(function(){
			var imageBox = $('#imageBox');
			var loadingBox = $('#loadingBox');
			var messageBox = $('#messageBox');
			if (imageBox.is(':visible'))
			{			
				imageBox.fadeOut('fast',function(){
					$('#modalBoxHolder').fadeOut('fast');
				});
			}
			if (loadingBox.is(':visible'))
			{			
				loadingBox.fadeOut('fast',function(){
					$('#modalBoxHolder').fadeOut('fast');
				});
			}
			if (messageBox.is(':visible'))
			{			
				messageBox.fadeOut('fast',function(){
					$('#modalBoxHolder').fadeOut('fast');
				});
			}
		});
		
	$('.button')
	.mouseover(function(){
	if ($(this).attr('name') != 'open')
	{
		var buttonLabel = $(this).find('label');
		var buttonLink = $(this).find('.link');
		buttonLabel.css('color','#b3dbff');
		buttonLink.css('color','#b3dbff');
		$(this).css('background','#222');
		$(this).css('border','1px solid #1a1a1a');
		$(this).css('padding','4px');
	}
	})
	.mouseout(function(){
	if ($(this).attr('name') != 'open')
	{
		var buttonLabel = $(this).find('label');
		var buttonLink = $(this).find('.link');
		buttonLabel.css('color','#7693ad');
		buttonLink.css('color','#7693ad');
		$(this).css('background','');
		$(this).css('border','');
		$(this).css('padding','5px');
	}
	})
	
	$('.formButton')
	.mouseover(function(){
	if ($(this).attr('name') != 'open')
	{
		var buttonLabel = $(this).find('label');
		var buttonLink = $(this).find('.link');
		buttonLabel.css('color','#b3dbff');
		buttonLink.css('color','#b3dbff');
	}
	})
	.mouseout(function(){
	if ($(this).attr('name') != 'open')
	{
		var buttonLabel = $(this).find('label');
		var buttonLink = $(this).find('.link');
		buttonLabel.css('color','#7693ad');
		buttonLink.css('color','#7693ad');
	}
	})
	
	$('.topic')
	.mouseover(function(){
		var topicSubject = $(this).find('.subject');
		var topicLink = topicSubject.find('a');
		var topicPoster = $(this).find('.poster');
		$(this).css('background','#1c1c1c');
		topicLink.css('color','#b3dbff');
		topicLink.css('textShadow', '0 -1px 1px #000000');
	})
	
	.mouseout(function(){
		var topicSubject = $(this).find('.subject');
		var topicPoster = $(this).find('.poster');
		var topicLink = topicSubject.find('a');
		$(this).css('backgroundImage','url(images/backgrounds/postGradient.png)');
		topicLink.css('color','#7693ad');
		topicLink.css('textShadow', '');
	})
	
	$('.menu .item')
	.mouseover(function(){
		var itemLink = $(this).find('a');
		var itemDesc = itemLink.find('span');
		if ($(this).attr('id') == 'openItem')
		{
		}
		else
		{
		itemLink.css('color','#b3dbff');
		itemLink.css('textShadow', '');
		itemLink.css('background','#222222');;
		}
	})
	.mouseout(function(){
		var itemLink = $(this).find('a');
		var itemDesc = itemLink.find('span');
		if ($(this).attr('id') == 'openItem')
		{
		}
		else
		{
		var itemLink = $(this).find('a');
		var itemDesc = itemLink.find('span');
		itemLink.css('color','#7693AD');
		itemLink.css('textShadow', '0 -1px 1px #000000');
		itemLink.css('backgroundImage','url(images/backgrounds/postGradient.png)');
		}
	})

//Form Checking Effects
    $('#registerForm')
	.keydown(function(e){
		if(e.keyCode == 13)
		{
			registerHighlightMessage('register');
		}
	})
	.keyup(function(e){
		if(e.keyCode == 13)
		{
			clearHighlight('register');
		}
	})
    $('#registerSubmit')
	.mousedown(function(){
		registerHighlightMessage('register');
	})
	.mouseout(function(){
		clearHighlight('register');
	})
	.mouseup(function(){
		clearHighlight('register');
	})
	
	$('#loginForm')
	.keydown(function(e){
		if(e.keyCode == 13)
		{
			loginHighlightMessage();
		}
	})
	.keyup(function(e){
		if(e.keyCode == 13)
		{
		clearHighlight('login');
		}
	})
    $('#loginSubmit')
	.mousedown(function(){
		loginHighlightMessage();
	})
	.mouseout(function(){
		clearHighlight('login');
	})
	.mouseup(function(){
		clearHighlight('login');
	})
	
	$('#contactForm')
	.keydown(function(e){
		if(e.keyCode == 13)
		{
			contactHighlightMessage();
		}
	})
	.keyup(function(e){
		if(e.keyCode == 13)
		{
			clearHighlight('contact');
		}
	})
	
    $('#contactSubmit')
	.mousedown(function(){
		contactHighlightMessage();
	})
	.mouseout(function(){
		clearHighlight('contact');
	})
	.mouseup(function(){
		clearHighlight('contact');
	})
	
	$('#newTopicForm')
	.keydown(function(e){
		if(e.keyCode == 13)
		{
			newTopicHighlightMessage();
		}
	})
	.keyup(function(e){
		if(e.keyCode == 13)
		{
			clearHighlight('newTopic');
		}
	})
    $('#newTopicSubmit')
	.mousedown(function(){
		newTopicHighlightMessage();
	})
	.mouseout(function(){
		clearHighlight('newTopic');
	})
	.mouseup(function(){
		clearHighlight('newTopic');
	})
	
	$('#newPostForm')
	.keydown(function(e){
		if(e.keyCode == 13)
		{
			newPostHighlightMessage();
		}
	})
	.keyup(function(e){
		if(e.keyCode == 13)
		{
			clearHighlight('newPost');
		}
	})
    $('#newPostSubmit')
	.mousedown(function(){
		newPostHighlightMessage();
	})
	.mouseout(function(){
		clearHighlight('newPost');
	})
	.mouseup(function(){
		clearHighlight('newPost');
	})
	
	$('#newCommentForm')
	.keydown(function(e){
		if(e.keyCode == 13)
		{
			newCommentHighlightMessage();
		}
	})
	.keyup(function(e){
		if(e.keyCode == 13)
		{
			clearHighlight('newComment');
		}
	})
	
    $('#newCommentSubmit')
	.mousedown(function(){
		newCommentHighlightMessage();
	})
	.mouseout(function(){
		clearHighlight('newComment');
	})
	.mouseup(function(){
		clearHighlight('newComment');
	})
	
	$('#editProfileForm')
	.keydown(function(e){
		if(e.keyCode == 13)
		{
			editProfileHighlightMessage();
		}
	})
	.keyup(function(e){
		if(e.keyCode == 13)
		{
			clearHighlight('editProfile');
		}
	})
    $('#editProfileSubmit')
	.mousedown(function(){
		editProfileHighlightMessage();
	})
	.mouseout(function(){
		clearHighlight('editProfile');
	})
	.mouseup(function(){
		clearHighlight('editProfile');
	})
	
	$('#gameProfilesForm')
	.keydown(function(e){
		if(e.keyCode == 13)
		{
			gameProfilesHighlightMessage();
		}
	})
	.keyup(function(e){
		if(e.keyCode == 13)
		{
			clearHighlight('editProfile');
		}
	})
	
	$('#gameProfilesSubmit')
	.mousedown(function(){
		gameProfilesHighlightMessage();
	})
	.mouseout(function(){
		clearHighlight('editProfile');
	})
	.mouseup(function(){
		clearHighlight('editProfile');
	})
	
	$('#registerName').keyup(function(e)
	{
		if(e.keyCode == 13 | e.keyCode == 9) {
			clearHighlight('register');
			return false;
		}
		var nameTry = $(this).val();
		var statusMessage = $(this).parent().parent().find('.fieldMessage');
		var statusIcon = $(this).parent().parent().find('.fieldIcon');
		if (nameTry.length == 0) {
			statusMessage.html('');
			statusIcon.html('<img src="/images/icons/cancel.png"></img>');
			statusIcon.css('visibility', 'hidden');
			window.clearTimeout(registerNameTimer);
			registerNameOk = 0;
		} else if (nameTry.length < 3 ) {
			statusMessage.html('Too short!');
			statusIcon.html('<img src="/images/icons/cancel.png"></img>');
			statusIcon.css('visibility', 'visible');
			window.clearTimeout(registerNameTimer);
			registerNameOk = 0;
		} else {
			statusMessage.html('Checking availability...');
			statusIcon.css('visibility', 'hidden');
			if (registerNameTimer)window.clearTimeout(registerNameTimer)
			{
				registerNameTimer = window.setTimeout(function()
				{
					$.ajax({
						type: 'POST',
						url: 'scripts/nameCheck.php',
						cache: false,
						async: true,
						dataType: 'json',
						data: 'nameTry=' + nameTry,
						success: function(response)
						{
							if (response[0].error == 0) {
								statusMessage.html('Available!');
								statusIcon.html('<img src="/images/icons/accept.png"></img>');
								statusIcon.css('visibility', 'visible');
								registerNameOk = 1;
							} else {
								statusMessage.html('Unavailable!');
								statusIcon.html('<img src="/images/icons/cancel.png"></img>');
								statusIcon.css('visibility', 'visible');
								registerNameOk = 0;
							}
						},
						error: function()
						{	
							statusMessage.html('Unknown Error!');
							statusIcon.html('<img src="/images/icons/cancel.png"></img>');
							statusIcon.css('visibility', 'visible');
							registerNameOk = 0;
						}
					});
				}, 1500);
			}
		}
    });
	
	$('#registerPass').keyup(function(e) {
		if(e.keyCode == 13 | e.keyCode == 9)
		{
			clearHighlight('register');
			return false;
		}
		var passTry = $(this).val();
		var statusMessage = $(this).parent().parent().find('.fieldMessage');
		var statusIcon = $(this).parent().parent().find('.fieldIcon');
		if (passTry.length == 0)
		{
			statusMessage.html('');
			statusIcon.html('<img src="/images/icons/cancel.png"></img>');
			statusIcon.css('visibility', 'hidden');
			window.clearTimeout(registerPassTimer);
			registerPassOk = 0;
		}
		else if (passTry.length < 3 )
		{
			statusMessage.html('Too short!');
			statusIcon.html('<img src="/images/icons/cancel.png"></img>');
			statusIcon.css('visibility', 'visible');
			window.clearTimeout(registerPassTimer);
			registerPassOk = 0;
		}
		else
		{
			statusMessage.html('Checking password...');
			statusIcon.html('<img src="/images/icons/cancel.png"></img>');
			statusIcon.css('visibility', 'hidden');
			if (registerPassTimer)window.clearTimeout(registerPassTimer)
			{
				registerPassTimer = window.setTimeout(function()
				{
					statusMessage.html('Password is valid!');
					statusIcon.html('<img src="/images/icons/accept.png"></img>');
					statusIcon.css('visibility', 'visible');
					registerPassOk = 1;
				}, 1500);
			}
		}
    });
	
	$('#registerEmail').keyup(function(e)
	{
		if(e.keyCode == 13 | e.keyCode == 9) {
			clearHighlight('register');
			return false;
		}
		var emailTry = $(this).val();
		var statusMessage = $(this).parent().parent().find('.fieldMessage');
		var statusIcon = $(this).parent().parent().find('.fieldIcon');
		if (emailTry.length == 0) {
			statusMessage.html('');
			statusIcon.html('<img src="/images/icons/cancel.png"></img>');
			statusIcon.css('visibility', 'hidden');
			window.clearTimeout(registerEmailTimer);
			registerEmailOk = 0;
		} else {
			statusMessage.html('Checking email...');
			statusIcon.html('<img src="/images/icons/cancel.png"></img>');
			statusIcon.css('visibility', 'hidden');
			if (registerEmailTimer)window.clearTimeout(registerEmailTimer)
			{
				registerEmailTimer = window.setTimeout(function()
				{
					$.ajax({
						type: 'POST',
						url: 'scripts/emailCheck.php',
						cache: false,
						async: true,
						dataType: 'json',
						data: 'emailTry=' + emailTry,
						success: function(response)
						{
							if (response[0].error == 0) {
								statusMessage.html('Email is valid!');
								statusIcon.html('<img src="/images/icons/accept.png"></img>');
								statusIcon.css('visibility', 'visible');
								registerEmailOk = 1;
							} else {
								statusMessage.html('Email is invalid!');
								statusIcon.html('<img src="/images/icons/cancel.png"></img>');
								statusIcon.css('visibility', 'visible');
								registerEmailOk = 0;
							}
						},
						error: function()
						{
							statusMessage.html('Unknown error!');
							statusIcon.html('<img src="/images/icons/cancel.png"></img>');
							statusIcon.css('visibility', 'visible');
							registerEmailOk = 0;
						}
					});
				}, 1500);
			}
		}
    });
	
	$('#contactName').keyup(function(e) {
		if(e.keyCode == 13 | e.keyCode == 9)
		{
			clearHighlight('contact');
			return false;
		}
		var nameTry = $(this).val();
		var statusMessage = $(this).parent().parent().find('.fieldMessage');
		var statusIcon = $(this).parent().parent().find('.fieldIcon');
		if (nameTry.length == 0)
		{
			statusMessage.html('');
			statusIcon.html('<img src="/images/icons/cancel.png"></img>');
			statusIcon.css('visibility', 'hidden');
			window.clearTimeout(contactNameTimer);
			contactNameOk = 0;
		}
		else if (nameTry.length < 3 )
		{
			statusMessage.html('Too short!');
			statusIcon.html('<img src="/images/icons/cancel.png"></img>');
			statusIcon.css('visibility', 'visible');
			window.clearTimeout(contactNameTimer);
			contactNameOk = 0;
		}
		else
		{
			statusMessage.html('Checking name...');
			statusIcon.html('<img src="/images/icons/cancel.png"></img>');
			statusIcon.css('visibility', 'hidden');
			if (contactNameTimer)window.clearTimeout(contactNameTimer)
			{
				contactNameTimer = window.setTimeout(function()
				{
					statusMessage.html('Name is valid!');
					statusIcon.html('<img src="/images/icons/accept.png"></img>');
					statusIcon.css('visibility', 'visible');
					contactNameOk = 1;
				}, 1500);
			}
		}
    });
	
	$('#contactSubject').keyup(function(e) {
		if(e.keyCode == 13 | e.keyCode == 9)
		{
			clearHighlight('contact');
			return false;
		}
		var subjectTry = $(this).val();
		var statusMessage = $(this).parent().parent().find('.fieldMessage');
		var statusIcon = $(this).parent().parent().find('.fieldIcon');
		if (subjectTry.length == 0)
		{
			statusMessage.html('');
			statusIcon.html('<img src="/images/icons/cancel.png"></img>');
			statusIcon.css('visibility', 'hidden');
			window.clearTimeout(contactSubjectTimer);
			contactSubjectOk = 0;
		}
		else if (subjectTry.length < 3 )
		{
			statusMessage.html('Too short!');
			statusIcon.html('<img src="/images/icons/cancel.png"></img>');
			statusIcon.css('visibility', 'visible');
			window.clearTimeout(contactSubjectTimer);
			contactSubjectOk = 0;
		}
		else
		{
			statusMessage.html('Checking subject...');
			statusIcon.html('<img src="/images/icons/cancel.png"></img>');
			statusIcon.css('visibility', 'hidden');
			if (contactSubjectTimer)window.clearTimeout(contactSubjectTimer)
			{
				contactSubjectTimer = window.setTimeout(function()
				{
					statusMessage.html('Subject is valid!');
					statusIcon.html('<img src="/images/icons/accept.png"></img>');
					statusIcon.css('visibility', 'visible');
					contactSubjectOk = 1;
				}, 1500);
			}
		}
    });
	
	$('#contactEmail').keyup(function(e) {
		if(e.keyCode == 13 | e.keyCode == 9)
		{
			clearHighlight('contact');
			return false;
		}
		var emailTry = $(this).val();
		var statusMessage = $(this).parent().parent().find('.fieldMessage');
		var statusIcon = $(this).parent().parent().find('.fieldIcon');
		if (emailTry.length == 0)
		{
			statusMessage.html('');
			statusIcon.html('<img src="/images/icons/cancel.png"></img>');
			statusIcon.css('visibility', 'hidden');
			window.clearTimeout(contactEmailTimer);
			contactEmailOk = 0;
		}
		else
		{
			statusMessage.html('Checking email...');
			statusIcon.html('<img src="/images/icons/cancel.png"></img>');
			statusIcon.css('visibility', 'hidden');
			if (contactEmailTimer)window.clearTimeout(contactEmailTimer)
			{
				contactEmailTimer = window.setTimeout(function()
				{
					$.ajax({
						type: 'POST',
						url: 'scripts/emailCheck.php',
						cache: false,
						async: true,
						dataType: 'json',
						data: 'emailTry=' + emailTry,
						success: function(response)
						{
							if (response[0].result == 1)
							{
								statusMessage.html('Email is valid!');
								statusIcon.html('<img src="/images/icons/accept.png"></img>');
								statusIcon.css('visibility', 'visible');
								contactEmailOk = 1;
							}
							else
							{
								statusMessage.html('Email is invalid!');
								statusIcon.html('<img src="/images/icons/cancel.png"></img>');
								statusIcon.css('visibility', 'visible');
								contactEmailOk = 0;
							}
						},
						error: function()
						{	
						}
					});
				}, 1500);
			}
		}
    });
	
	$('#contactMessage').keyup(function(e) {
		if(e.keyCode == 13 | e.keyCode == 9)
		{
			clearHighlight('contact');
			return false;
		}
		var messageTry = $(this).val();
		var statusMessage = $(this).parent().parent().find('.fieldMessage');
		var statusIcon = $(this).parent().parent().find('.fieldIcon');
		if (messageTry.length == 0)
		{
			statusMessage.html('');
			statusIcon.html('<img src="/images/icons/cancel.png"></img>');
			statusIcon.css('visibility', 'hidden');
			window.clearTimeout(contactMessageTimer);
			contactMessageOk = 0;
		}
		else if (messageTry.length < 3 )
		{
			statusMessage.html('Too short!');
			statusIcon.html('<img src="/images/icons/cancel.png"></img>');
			statusIcon.css('visibility', 'visible');
			window.clearTimeout(contactMessageTimer);
			contactMessageOk = 0;
		}
		else
		{
			statusMessage.html('Checking message...');
			statusIcon.html('<img src="/images/icons/cancel.png"></img>');
			statusIcon.css('visibility', 'hidden');
			if (contactMessageTimer)window.clearTimeout(contactMessageTimer)
			{
				contactMessageTimer = window.setTimeout(function()
				{
					statusMessage.html('Message is valid!');
					statusIcon.html('<img src="/images/icons/accept.png"></img>');
					statusIcon.css('visibility', 'visible');
					contactMessageOk = 1;
				}, 1500);
			}
		}
    });
	
	$('#loginName').keyup(function(e) {
		if(e.keyCode == 13 | e.keyCode == 9)
		{
			clearHighlight('login');
			return false;
		}
		var nameTry = $(this).val();
		var statusMessage = $(this).parent().parent().find('.fieldMessage');
		if (nameTry.length == 0)
		{
			statusMessage.html('');
			loginNameOk = 0;
		}
		else
		{
			statusMessage.html('');
			loginNameOk = 1;
		}
	});
	
	$('#loginPass').keyup(function(e) {
		if(e.keyCode == 13 | e.keyCode == 9)
		{
			clearHighlight('login');
			return false;
		}
		var passTry = $(this).val();
		var statusMessage = $(this).parent().parent().find('.fieldMessage');
		if (passTry.length == 0)
		{
			statusMessage.html('');
			loginPassOk = 0;
		}
		else
		{
			statusMessage.html('');
			loginPassOk = 1;
		}
	});
	
	$('#newTopic_subject').keyup(function(e) {
		if(e.keyCode == 13 | e.keyCode == 9)
		{
			clearHighlight('newTopic');
			return false;
		}
		var subjectTry = $(this).val();
		var statusMessage = $('#' + 'newTopicSubjectMessage');
		if (subjectTry.length == 0)
		{
			statusMessage.html('');
			newTopicSubjectOk = 0;
		}
		else
		{
			statusMessage.html('');
			newTopicSubjectOk = 1;
		}
	});
	
	$('#newTopic_text').keyup(function(e) {
		if(e.keyCode == 13 | e.keyCode == 9)
		{
			clearHighlight('newTopic');
			return false;
		}
		var bodyTry = $(this).val();
		var statusMessage = $('#' + 'newTopicBodyMessage');
		if (bodyTry.length == 0)
		{
			statusMessage.html('');
			newTopicBodyOk = 0;
		}
		else
		{
			statusMessage.html('');
			newTopicBodyOk = 1;
		}
	});
	
	$('#newCommentText').keyup(function(e) {
		if(e.keyCode == 13 | e.keyCode == 9)
		{
			clearHighlight('newComment');
			return false;
		}
		var bodyTry = $(this).val();
		var statusMessage = $('#' + 'newCommentBodyMessage');
		if (bodyTry.length == 0)
		{
			statusMessage.html('');
			newCommentBodyOk = 0;
		}
		else
		{
			statusMessage.html('');
			newCommentBodyOk = 1;
		}
	});
	
		$('#newPostText').keyup(function(e) {
		if(e.keyCode == 13 | e.keyCode == 9)
		{
			clearHighlight('newPost');
			return false;
		}
		var bodyTry = $(this).val();
		var statusMessage = $('#' + 'newPostBodyMessage');
		if (bodyTry.length == 0)
		{
			statusMessage.html('');
			newPostBodyOk = 0;
		}
		else
		{
			statusMessage.html('');
			newPostBodyOk = 1;
		}
	});

	$('#editEmail').keyup(function(e) {
		editProfileEmailOk = 0;
		if(e.keyCode == 13 | e.keyCode == 9)
		{
			clearHighlight('editProfile');
			return false;
		}
		var emailTry = $(this).val();
		var statusMessage = $(this).parent().parent().find('.editFieldMessage');
		var statusIcon = $(this).parent().parent().find('.editFieldIcon');
		if (emailTry.length == 0)
		{
			statusMessage.html('');
			statusIcon.html('<img src="/images/icons/cancel.png"></img>');
			statusIcon.css('visibility', 'hidden');
			window.clearTimeout(editProfileEmailTimer);
			editProfileEmailOk = 0;
		}
		else
		{
			statusMessage.css('visibility', 'visible');
			statusMessage.html('Checking email...');
			statusIcon.html('<img src="/images/icons/cancel.png"></img>');
			statusIcon.css('visibility', 'hidden');
			if (editProfileEmailTimer)window.clearTimeout(editProfileEmailTimer)
			{
				editProfileEmailTimer = window.setTimeout(function()
				{
					$.ajax({
						type: 'POST',
						url: 'scripts/emailCheck.php',
						cache: false,
						async: true,
						dataType: 'json',
						data: 'emailTry=' + emailTry,
						success: function(response)
						{
							if (response[0].result == 1)
							{
								statusMessage.html('Email is valid!');
								statusIcon.html('<img src="/images/icons/accept.png"></img>');
								statusIcon.css('visibility', 'visible');
								editProfileEmailOk = 1;
							}
							else
							{
								statusMessage.html('Email is invalid!');
								statusIcon.html('<img src="/images/icons/cancel.png"></img>');
								statusIcon.css('visibility', 'visible');
								editProfileEmailOk = 0;
							}
						},
						error: function()
						{	
						}
					});
				}, 1500);
			}
		}
    });
	
	$('#editPass').keyup(function(e) {
		if(e.keyCode == 13 | e.keyCode == 9) {
			clearHighlight('editProfile');
			return false;
		}
		var passTry = $(this).val();
		var confirmPass = $('#confirmPass');
		var confirmPassMessage = confirmPass.parent().parent().find('.editFieldMessage');
		var confirmPassIcon = confirmPass.parent().parent().find('.editFieldIcon');
		var statusMessage = $(this).parent().parent().find('.editFieldMessage');
		var statusIcon = $(this).parent().parent().find('.editFieldIcon');
		if (passTry.length == 0) {
			statusMessage.html('');
			statusIcon.html('<img src="/images/icons/cancel.png"></img>');
			statusIcon.css('visibility', 'hidden');
			window.clearTimeout(editProfilePassTimer);
			confirmPass.attr('disabled', 'disabled')
			confirmPass.val('');
			confirmPassMessage.css('visibility', 'hidden');
			confirmPassIcon.css('visibility', 'hidden');
			editProfilePassOk = 0;
		} else if (passTry.length < 3 ) {	
			statusMessage.css('visibility', 'visible');
			statusMessage.html('Too short!');
			statusIcon.html('<img src="/images/icons/cancel.png"></img>');
			statusIcon.css('visibility', 'visible');
			window.clearTimeout(editProfilePassTimer);
			confirmPass.attr('disabled', 'disabled')
			confirmPass.val('');
			confirmPassMessage.css('visibility', 'hidden');
			confirmPassIcon.css('visibility', 'hidden');
			editProfilePassOk = 0;
		} else {
			statusMessage.css('visibility', 'visible');
			statusMessage.html('Checking password...');
			statusIcon.html('<img src="/images/icons/cancel.png"></img>');
			statusIcon.css('visibility', 'hidden');
			if (editProfilePassTimer)window.clearTimeout(editProfilePassTimer)
			{
				editProfilePassTimer = window.setTimeout(function()
				{
					$.ajax({
						type: 'POST',
						url: 'scripts/passCheck.php',
						cache: false,
						async: true,
						dataType: 'json',
						data: 'passTry=' + passTry,
						success: function(response)
						{
							if (response[0].error == 0) {
								statusMessage.html('Password is correct!');
								statusIcon.html('<img src="/images/icons/accept.png"></img>');
								statusIcon.css('visibility', 'visible');
								confirmPass.removeAttr('disabled');
								editProfilePassOk = 1;
							} else {
								statusMessage.html('Password is incorrect!');
								statusIcon.html('<img src="/images/icons/cancel.png"></img>');
								statusIcon.css('visibility', 'visible');
								confirmPass.attr('disabled', 'disabled')
								confirmPass.val('');
								confirmPassMessage.css('visibility', 'hidden');
								confirmPassIcon.css('visibility', 'hidden');
								editProfilePassOk = 0;
							}
						},
						error: function()
						{	
							statusMessage.html('Unknown error!');
							statusIcon.html('<img src="/images/icons/cancel.png"></img>');
							statusIcon.css('visibility', 'visible');
							confirmPass.attr('disabled', 'disabled')
							confirmPass.val('');
							confirmPassMessage.css('visibility', 'hidden');
							confirmPassIcon.css('visibility', 'hidden');
							editProfilePassOk = 0;
						}
					});
				}, 1500);
			}
		}
    });
	
	$('#confirmPass').keyup(function(e) {
		if(e.keyCode == 13 | e.keyCode == 9)
		{
			clearHighlight('register');
			return false;
		}
		var passTry = $(this).val();
		var statusMessage = $(this).parent().parent().find('.editFieldMessage');
		var statusIcon = $(this).parent().parent().find('.editFieldIcon');
		if (passTry.length == 0)
		{
			statusMessage.html('');
			statusIcon.html('<img src="/images/icons/cancel.png"></img>');
			statusIcon.css('visibility', 'hidden');
			window.clearTimeout(editProfileConfirmPassTimer);
			editProfileConfirmPassOk = 0;
		}
		else if (passTry.length < 3 )
		{	
			statusMessage.css('visibility', 'visible');
			statusMessage.html('Too short!');
			statusIcon.html('<img src="/images/icons/cancel.png"></img>');
			statusIcon.css('visibility', 'visible');
			window.clearTimeout(editProfileConfirmPassTimer);
			editProfileConfirmPassOk = 0;
		}
		else
		{
			statusMessage.css('visibility', 'visible');
			statusMessage.html('Checking password...');
			statusIcon.html('<img src="/images/icons/cancel.png"></img>');
			statusIcon.css('visibility', 'hidden');
			if (editProfileConfirmPassTimer)window.clearTimeout(editProfileConfirmPassTimer)
			{
				editProfileConfirmPassTimer = window.setTimeout(function()
				{
					statusMessage.html('Password is valid!');
					statusIcon.html('<img src="/images/icons/accept.png"></img>');
					statusIcon.css('visibility', 'visible');
					editProfileConfirmPassOk = 1;
				}, 1500);
			}
		}
    });
	
	$('#editAvatar').keyup(function(e)
	{
		editProfileAvatarOk = 0;
		if(e.keyCode == 13 | e.keyCode == 9) {
			clearHighlight('editProfile');
			return false;
		}
		var avatarTry = $(this).val();
		var statusMessage = $(this).parent().parent().find('.editFieldMessage');
		var statusIcon = $(this).parent().parent().find('.editFieldIcon');
		if (avatarTry.length == 0) {
			statusMessage.html('');
			statusIcon.html('<img src="/images/icons/cancel.png"></img>');
			statusIcon.css('visibility', 'hidden');
			window.clearTimeout(editProfileAvatarTimer);
			editProfileIconOk = 0;
		} else {
			statusMessage.css('visibility', 'visible');
			statusMessage.html('Checking avatar...');
			statusIcon.html('<img src="/images/icons/cancel.png"></img>');
			statusIcon.css('visibility', 'hidden');
			if (editProfileAvatarTimer)window.clearTimeout(editProfileAvatarTimer)
			{
				editProfileAvatarTimer = window.setTimeout(function()
				{
					$.ajax({
						type: 'POST',
						url: 'scripts/avatarCheck.php',
						cache: false,
						async: true,
						dataType: 'json',
						data: 'avatarTry=' + avatarTry,
						success: function(response)
						{
							if (response[0].result == 1) {
								statusMessage.html('Avatar is valid!');
								statusIcon.html('<img src="/images/icons/accept.png"></img>');
								statusIcon.css('visibility', 'visible');
								editProfileAvatarOk = 1;
							} else if (response[0].result == 2) {
								statusMessage.html('Ideal avatar is 40x40 square!');
								statusIcon.html('<img src="/images/icons/error.png"></img>');
								statusIcon.css('visibility', 'visible');
								editProfileAvatarOk = 1;
							} else if (response[0].result == 3) {
								statusMessage.html('Ideal avatar is 40x40 square!');
								statusIcon.html('<img src="/images/icons/error.png"></img>');
								statusIcon.css('visibility', 'visible');
								editProfileAvatarOk = 1;
							} else {
								statusMessage.html('Avatar is invalid!');
								statusIcon.html('<img src="/images/icons/cancel.png"></img>');
								statusIcon.css('visibility', 'visible');
								editProfileAvatarOk = 0;
							}
						},
						error: function()
						{	
							statusMessage.html('Unknown error!');
							statusIcon.html('<img src="/images/icons/cancel.png"></img>');
							statusIcon.css('visibility', 'visible');
							editProfileAvatarOk = 0;
						}
					});
				}, 1500);
			}
		}
    });

});

//Cookie Functions
function setCookie(name,value,expiredays)
    {
    var exdate=new Date();
    exdate.setDate(exdate.getDate()+expiredays);
    document.cookie=name+ "=" +escape(value)+
    ((expiredays==null) ? "" : ";expires="+exdate.toGMTString());
    }

function deleteCookie(cookieName)
    {
    document.cookie = cookieName +
    '=; expires=Thu, 01-Jan-70 00:00:01 GMT;';
    window.location = "index.php";
    }

//Form Error Checking Functions
function loginNameCheck()
{
	var fieldName = $('#loginName');
	var nameTry = fieldName.val();
	var statusMessage = fieldName.parent().parent().find('.fieldMessage');
	if (nameTry.length == 0)
	{
		statusMessage.html('');
		loginNameOk = 0;
	}
	else
	{
		statusMessage.html('');
		loginNameOk = 1;
	}
}

function loginPassCheck()
{
	var fieldName = $('#loginPass');
	var passTry = fieldName.val();
	var statusMessage = fieldName.parent().parent().find('.fieldMessage');
	if (passTry.length == 0)
	{
		statusMessage.html('');
		loginPassOk = 0;
	}
	else
	{
		statusMessage.html('');
		loginPassOk = 1;
	}
}
function clearHighlight(formName)
{
	$('.fieldMessage').css('color','#6d6d6d');
	$('.fieldMessage').css('color','#6d6d6d');
	$('.topicFormMessage').css('color','#6d6d6d');
}
function loginHighlightMessage()
{
	var loginCheckCount = loginNameOk + loginPassOk;
	if (loginCheckCount != 2)
	{
		var loginName = $('#' + 'loginName');
		var loginPass = $('#' + 'loginPass');
		var loginNameMessage = loginName.parent().parent().find('.fieldMessage');
		var loginPassMessage = loginPass.parent().parent().find('.fieldMessage');
		if (loginNameOk == 0)
		{
			loginNameMessage.css('color','#fff');
			if (loginName.val() == '')
			{
				loginNameMessage.html('Cannot be blank!');
			}
		}
		if (loginPassOk == 0)
		{
			loginPassMessage.css('color','#fff');
			if (loginPass.val() == '')
			{
				loginPassMessage.html('Cannot be blank!');
			}
		}
	}
}
function registerHighlightMessage()
{
	var registerCheckCount = registerNameOk + registerPassOk + registerEmailOk;
	if (registerCheckCount != 3)
	{
		var registerName = $('#' + 'registerName');
		var registerPass = $('#' + 'registerPass');
		var registerEmail = $('#' + 'registerEmail');
		var registerNameMessage = registerName.parent().parent().find('.fieldMessage');
		var registerPassMessage = registerPass.parent().parent().find('.fieldMessage');
		var registerEmailMessage = registerEmail.parent().parent().find('.fieldMessage');
		if (registerNameOk == 0)
		{
			registerNameMessage.css('color','#fff');
			if (registerName.val() == '')
			{
				registerNameMessage.html('Cannot be blank!');
			}
		}
		if (registerPassOk == 0)
		{
			registerPassMessage.css('color','#fff');
			if (registerPass.val() == '')
			{
				registerPassMessage.html('Cannot be blank!');
			}
		}
		if (registerEmailOk == 0)
		{
			registerEmailMessage.css('color','#fff');
			if (registerEmail.val() == '')
			{
				registerEmailMessage.html('Cannot be blank!');
			}
		}
	}
}

function contactHighlightMessage()
{
	var contactCheckCount = contactNameOk + contactSubjectOk + contactEmailOk + contactMessageOk;
	if (contactCheckCount != 4)
	{
		var contactName = $('#' + 'contactName');
		var contactSubject = $('#' + 'contactSubject');
		var contactEmail = $('#' + 'contactEmail');
		var contactMessage = $('#' + 'contactMessage');
		var contactNameMessage = contactName.parent().parent().find('.fieldMessage');
		var contactSubjectMessage = contactSubject.parent().parent().find('.fieldMessage');
		var contactEmailMessage = contactEmail.parent().parent().find('.fieldMessage');
		var contactMessageMessage = contactMessage.parent().parent().find('.fieldMessage');
		if (contactNameOk == 0)
		{
			contactNameMessage.css('color','#fff');
			if (contactName.val() == '')
			{
				contactNameMessage.html('Cannot be blank!');
			}
		}
		if (contactSubjectOk == 0)
		{
			contactSubjectMessage.css('color','#fff');
			if (contactSubject.val() == '')
			{
				contactSubjectMessage.html('Cannot be blank!');
			}
		}
		if (contactEmailOk == 0)
		{
			contactEmailMessage.css('color','#fff');
			if (contactEmail.val() == '')
			{
				contactEmailMessage.html('Cannot be blank!');
			}
		}
		if (contactMessageOk == 0)
		{
			contactMessageMessage.css('color','#fff');
			if (contactMessage.val() == '')
			{
				contactMessageMessage.html('Cannot be blank!');
			}
		}
	}
}

function newTopicHighlightMessage()
{
	var newTopicCheckCount = newTopicSubjectOk + newTopicBodyOk;
	if (newTopicCheckCount != 2)
	{
		var newTopicSubject = $('#' + 'newTopic_subject');
		var newTopicBody = $('#' + 'newTopic_text');
		var newTopicSubjectMessage = $('#' + 'newTopicSubjectMessage');
		var newTopicBodyMessage = $('#' + 'newTopicBodyMessage');
		if (newTopicSubjectOk == 0)
		{
			newTopicSubjectMessage.css('color','#fff');
			if (newTopicSubject.val() == '')
			{
				newTopicSubjectMessage.html('Cannot be blank!');
			}
		}
		if (newTopicBodyOk == 0)
		{
			newTopicBodyMessage.css('color','#fff');
			if (newTopicBody.val() == '')
			{
				newTopicBodyMessage.html('Cannot be blank!');
			}
		}
	}
}

function newPostHighlightMessage()
{
	var newPostCheckCount = newPostBodyOk;
	if (newPostCheckCount != 1)
	{
		var newPostBody = $('#' + 'newTopic_text');
		var newPostBodyMessage = $('#' + 'newPostBodyMessage');
		if (newPostBodyOk == 0)
		{
			newPostBodyMessage.css('color','#fff');
			if (newPostBodyMessage.val() == '')
			{
				newPostBodyMessage.html('Cannot be blank!');
			}
		}
	}
}

function newCommentHighlightMessage()
{
	if (newCommentBodyOk != 1)
	{
		var newCommentBodyMessage = $('#' + 'newCommentBodyMessage');
		if (newCommentBodyOk == 0)
		{
			newCommentBodyMessage.css('color','#fff');
			if (newCommentBodyMessage.val() == '')
			{
				newCommentBodyMessage.html('Cannot be blank!');
			}
		}
	}
}

function editProfileHighlightMessage()
{
	var editProfileCheckCount = (editProfileEmailOk + editProfileAvatarOk);
	if (editProfileCheckCount != 2)
	{
		var editEmail = $('#' + 'editEmail');
		var editAvatar = $('#' + 'editAvatar');
		var editEmailMessage = editEmail.parent().parent().find('.editFieldMessage');
		var editAvatarMessage = editAvatar.parent().parent().find('.editFieldMessage');
		if (editProfileEmailOk == 0)
		{
			editEmailMessage.css('color','#fff');
			if (editEmail.val() == '')
			{
				editEmailMessage.html('Cannot be blank!');
			}
		}
		if (editProfileAvatarOk == 0)
		{
			editAvatarMessage.css('color','#fff');
			if (editAvatar.val() == '')
			{
				editAvatarMessage.html('Cannot be blank!');
			}
		}
	}
}

//Form Actions
function logout()
{
    $.ajax({
		type: 'POST',
		url: 'scripts/userLogout.php',
		cache: false,
		async: true,
		dataType: 'json',
		success: function(response)
		{
			if (response[0].error == 0) {
				deleteCookie('userName');
				deleteCookie('userPass');
			} else {
			}
		},
		error: function()
		{
		}
	});
}
function register()
{
	var registerCheckCount = registerNameOk + registerPassOk + registerEmailOk;
	var registerBox = $('#registerBox');
	var oldMessageBox = $('#oldMessageBox');
	if (registerCheckCount == 3) {
		var registerName = $('#' + 'registerName').val();
		var registerPass = $('#' + 'registerPass').val();
		var registerEmail = $('#' + 'registerEmail').val();
		$.ajax({
			type: 'POST',
			url: 'scripts/register.php',
			cache: false,
			async: true,
			dataType: 'json',
			data: 'registerName=' + registerName + '&registerPass=' + registerPass + '&registerEmail=' + registerEmail,
			success: function(response)
			{
				if (response[0].error == 0) {
				} else {
				}
			},
			error: function()
			{	
			}
		});
		registerBox.fadeOut(function(){
			var oldMessageBoxSubmit = $('#' + 'oldMessageBoxSubmit');
			oldMessageBox.fadeIn();
			oldMessageBoxSubmit.focus();
		});
		return false;
	} else {
		return false;
	}
}

function login()
{
	loginNameCheck();
	loginPassCheck();
	var loginCheckCount = loginNameOk + loginPassOk;
	var loginBox = $('#loginBox');
	var oldMessageBox = $('#oldMessageBox');
	var errorBox = $('#errorBox');
	if (loginCheckCount == 2)
	{
		var loginName = $('#' + 'loginName').val();
		var loginPass = $('#' + 'loginPass').val();
		$.ajax({
			type: 'POST',
			url: 'scripts/login.php',
			cache: false,
			async: true,
			dataType: 'json',
			data: 'loginName=' + loginName + '&loginPass=' + loginPass,
			success: function(response)
			{
				if (response[0].error == 0) {
					setCookie('userName', response[0].name, '365');
			        setCookie('userPass', response[0].password, '365');
					
					loginBox.fadeOut(function()
					{
						oldMessageBox.fadeIn();
						window.setTimeout(function()
						{
							window.location = "index.php";
						},1500);
					});
				} else {
					if($.support.opacity) {
						loginBox.hide();
						var errorBoxSubmit = $('#errorBoxSubmit');
						errorMessage = errorBox.find('label');
						errorMessage.html('Incorrect username or password!');
						errorBox.fadeIn();
						errorBoxSubmit.focus();
					} else {
						loginBox.fadeOut(function(){
							var errorBoxSubmit = $('#errorBoxSubmit');
							errorMessage = errorBox.find('label');
							errorMessage.html('Incorrect username or password!');
							errorBox.fadeIn();
							errorBoxSubmit.focus();
						});
					}
					
				}
			},
			error: function()
			{
				loginBox.fadeOut(function(){
					var errorBoxSubmit = $('#errorBoxSubmit');
					errorMessage = errorBox.find('label');
					errorMessage.html(response[0].error);
					errorBox.fadeIn();
					errorBoxSubmit.focus();
				});
			}
		});
	return false;
	}
	else
	{
	return false;
	}
}

function contact()
{
	contactNameCheck();
	contactEmailCheck();
	var contactCheckCount = contactNameOk + contactSubjectOk + contactEmailOk + contactMessageOk;
	var contactBox = $('#contactBox');
	var oldMessageBox = $('#oldMessageBox');
	var loadingBox = $('#loadingBox');
	if (contactCheckCount == 4) {
		var contactName = $('#' + 'contactName').val();
		var contactSubject = $('#' + 'contactSubject').val();
		var contactEmail = $('#' + 'contactEmail').val();
		var contactMessage = $('#' + 'contactMessage').val();
		$.ajax({
			type: 'POST',
			url: 'scripts/contact.php',
			cache: false,
			async: true,
			dataType: 'json',
			data: 'contactName=' + contactName + '&contactSubject=' + contactSubject + '&contactEmail=' + contactEmail + '&contactMessage=' + contactMessage,
			success: function(response)
			{
				if (response[0].error == 0) {
				} else {
				}
			},
			error: function()
			{	
			}
		});
		contactBox.fadeOut(function(){
			window.setTimeout(function()
			{
				loadingBox.fadeIn(function(){
					loadingBox.fadeOut(function(){
						oldMessageBox.fadeIn();
					});
				});
			},1500);
		});
		return false;
	} else {
		return false;
	}
}
var recentlyReplied = 0;
function postReply(tid)
{
	if (recentlyReplied == 0) {
		if (newPostBodyOk == 1) {
			newPostBodyOk = 0;
			recentlyReplied = 1;
			var posts = $('#' + 'posts_' + tid);
			var newPost = $('#' + 'newPostForm');
			var replyText_field = $('#' + 'newPostText');
			var replyText_messy = $('#' + 'newPostText').val();
			var replyText = encodeURIComponent(replyText_messy);
			$.ajax({
				type: 'POST',
				url: 'scripts/postReply.php',
				cache: false,
				async: true,
				dataType: 'json',
				data: 'replyText=' + replyText + '&topicId=' + tid,
				success: function(response)
				{
					if (response[0].error == 0) {
						posts.append('<div id="post_'+response[0].newPostId+'" style="display: none;" class="post"><div class="left"><div class="userIcon"><img src="'+response[0].userIcon+'"></img></div></div><div class="info"><span class="left">'+response[0].userName+'</span><span class="right">1 second ago</span></div><div class="text">'+response[0].replyText+'</div><div class="controls"><span id="editPost_'+response[0].newPostId+'" onclick="editPost('+response[0].newPostId+')">Edit Post</span></div></div>');
						var recentPost = $('#' + 'post_' + response[0].newPostId);
						replyText_field.val('');
						$.scrollTo('#bottom', 500, {onAfter:function(){recentPost.fadeIn('slow', function(){recentlyReplied = 0});}});
					} else {
					}
				},
				error: function()
				{	
				}
			});
			return false;
		}
	}	
}
function editPost(pid)
{
	var post = $('#' + 'post_' + pid);
	var postTextarea = post.find('.text');
	var postHeight = postTextarea.height();
	var postLink = $('#' + 'editPost_' + pid);
	if (postLink.html() == 'Save Changes') {
		var postText_messy = $('#editText').val()
		var postText = encodeURIComponent(postText_messy);
		$.ajax({
			type: 'POST',
			url: 'scripts/editPost.php',
			cache: false,
			async: true,
			dataType: 'json',
			data: 'pid=' + pid + '&postText=' + postText,
			success: function(response)
			{
				if (response[0].error == 0) {
					postTextarea.html(''+response[0].postText+'');
					postLink.html('Edit Post');
				} else {
				}
			},
			error: function()
			{	
			}
		});
	} else {
		$.ajax({
			type: 'POST',
			url: 'scripts/getPostText.php',
			cache: false,
			async: true,
			dataType: 'json',
			data: 'pid=' + pid,
			success: function(response)
			{
				if (response[0].error == 0) {
					postTextarea.html('<textarea id="editText" class="editPost" style="height: '+postHeight+'px">'+response[0].postText+'</textarea>');
					postLink.html('Save Changes');
				} else {
				}
			},
			error: function()
			{	
			}
		});
	}
}
function savePostChanges(pid)
{
	alert('zomg');
}
var recentlyCommented = 0;
function postComment(bid)
{
	if (recentlyCommented == 0) {
		if (newCommentBodyOk == 1) {
			newCommentBodyOk = 0;
			recentlyCommented = 1;
			var comments = $('#' + 'comments_' + bid);
			var newComment = $('#' + 'newCommentForm');
			var commentText_field = $('#' + 'newCommentText');
			var commentText_messy = $('#' + 'newCommentText').val();
			var commentText = encodeURIComponent(commentText_messy);
			$.ajax({
				type: 'POST',
				url: 'scripts/postComment.php',
				cache: false,
				async: true,
				dataType: 'json',
				data: 'commentText=' + commentText + '&blogId=' + bid,
				success: function(response)
				{
					if (response[0].error == 0) {
						comments.append('<div id="comment_'+response[0].newCommentId+'" class="comment" style="display: none;"><div class="left"><div class="userIcon"><img src="'+response[0].userIcon+'"></img></div></div><div class="info"><span class="left"><a href="profile.php?user='+response[0].userName+'">'+response[0].userName+'</a></span><span class="right">'+response[0].timestamp+'</span></div><div class="text">'+response[0].commentText+'</div></div>');
						var recentComment = $('#' + 'comment_'+response[0].newCommentId);
						commentText_field.val('');
						recentComment.fadeIn('slow', function(){
							recentlyCommented = 0;
						});
					} else {
					}
				},
				error: function()
				{
				}
			});
			return false;
		}
	}	
}
var recentlyPosted = 0;
function postTopic(fid)
{
	if (recentlyPosted == 0) {
		var newTopicCheckCount = (newTopicSubjectOk + newTopicBodyOk);
		if (newTopicCheckCount == 2) {
			newTopicSubjectOk = 0;
			newTopicBodyOk = 0;
			recentlyPosted = 1;
			var newTopic = $('#' + 'newTopic');
			var topicsOutline = $('.topicsOutline');
			var newTopicButton = $('#' + 'newTopicButton');
			var newTopicSubjectField = $('#newTopic_subject');
			var newTopicTextField = $('#newTopic_text');
			var newTopicSubject = $('#newTopic_subject').val();
			var newTopicText_messy = $('#newTopic_text').val();
			var newTopicText = encodeURIComponent(newTopicText_messy);
			$.ajax({
				type: 'POST',
				url: 'scripts/postTopic.php',
				cache: false,
				async: true,
				dataType: 'json',
				data: 'newTopicSubject=' + newTopicSubject + '&fid=' + fid + '&newTopicText=' + newTopicText,
				success: function(response)
				{
					if (response[0].error == 0) {
						newTopicButton.css('background','');
						recentlyPosted = 0;
						window.location.reload();
						newTopic.attr('name','closed');			
					} else {
					}
				},	
				error: function()
				{	
				}
			});
			return false;
		}
	}
}
function markRead(itemId, itemType, userId)
{
	$.ajax({
		type: 'POST',
		url: 'scripts/markRead.php',
		cache: false,
		async: true,
		dataType: 'json',
		data: 'itemId=' + itemId + '&itemType=' + itemType + '&userId=' + userId,
		success: function(response)
		{
			if (response[0].error == 0) {
				window.location = "discuss.php";
			} else {
			}
		},
		error: function()
		{	
		}
	});
}
//Profile Page
function changeWindow(newWindow)
{
	var counter = 0;
	var childCount = $('.content').children().size();
	$('.content').children().fadeOut('fast', function(){
		counter++;
		if (counter == childCount)
		{
		$('.content').load('includes/'+newWindow+'.php', function(){
			$('.content').children().fadeIn('fast');
		});
		}
	});
}
//Element Effect Functions
function popUp(message)
{
	var modalBoxHolder = $('#' + 'modalBoxHolder');
	var messageBox = $('#' + 'messageBox');
	var loadingBox = $('#' + 'loadingBox');
	modalBoxHolder.stop(true, true);
	loadingBox.stop(true, true);
	var documentHeight = $(document).height();
	var winH = $(window).height();  
	var winW = $(window).width();
	loadingBox.hide();
	messageBox.html('');
	$.post('includes/messageBox.php', { message: message },
        function(data){
        messageBox.html(data);
    })
	modalBoxHolder.height(documentHeight);
	messageBox.css('top', (winH - messageBox.height()) / 2+$(window).scrollTop() + "px");
	messageBox.css('left', (winW - messageBox.width()) / 2+$(window).scrollLeft() + "px");
	modalBoxHolder.fadeIn('fast',function(){
		messageBox.fadeIn('fast');
	});
}
function toggleNewTopic()
{
	var newTopic = $('#' + 'newTopic');
	var newTopicButton = $('#' + 'newTopicButton');
	var newTopicSubjectField = $('#newTopic_subject');
	var newTopicTextField = $('#newTopic_text');
	if (newTopicButton.attr('name') == 'closed')
	{
		newTopic.parent().fadeIn();
		newTopicButton.css('background','#222');
		newTopicButton.css('padding','4px');
		newTopicButton.css('border','1px solid #111');
		newTopicButton.attr('name','open');
	}
	else
	{
		newTopicButton.css('background','');
		newTopicButton.css('padding','5px');
		newTopicButton.css('border','none');
		newTopic.parent().fadeOut(function(){
			newTopicSubjectField.val('');
			newTopicTextField.val('');
		});
		newTopicButton.attr('name','closed');
	}
}
function topicOver(tid)
{
	var topic = $('#' + tid);
	var topicSubject = topic.find('.subject');
	var topicLink = topicSubject.find('a');
	var topicPoster = topic.find('.poster');
	topic.css('background','#1c1c1c');
	topicLink.css('color','#ffffff');
	topicSubject.css('textShadow', '0 -1px 1px #000000');
}
function topicOut(tid)
{
	var topic = $('#' + tid);
	var topicSubject = topic.find('.subject');
	var topicPoster = topic.find('.poster');
	var topicLink = topicSubject.find('a');
	topic.css('background','#222');
	topicLink.css('color','#7693ad');
	topicSubject.css('textShadow', '');
}
function scrollTo(destination)
{
	$.scrollTo('#'+destination+'', 1000);
}
jQuery.fn.center = function () {
	this.css("position","absolute");
	this.css("top", ( $(window).height() - this.height() ) / 2+$(window).scrollTop() + "px");
	this.css("left", ( $(window).width() - this.width() ) / 2+$(window).scrollLeft() + "px");
	return this;
}
function showImage(imageUrl)
{
	var modalBoxHolder = $('#' + 'modalBoxHolder');
	var imageBox = $('#' + 'imageBox');
	var loadingBox = $('#' + 'loadingBox');
	var documentHeight = $(document).height();
	var winH = $(window).height();  
	var winW = $(window).width();
	var newHeight = winH - 150;

	modalBoxHolder.height(documentHeight);
	loadingBox.css('top',  (winH - loadingBox.height()) / 2+$(window).scrollTop() + "px");
	loadingBox.css('left', (winW - loadingBox.width()) / 2+$(window).scrollLeft() + "px");
	modalBoxHolder.fadeIn('fast',function(){
		loadingBox.fadeIn('fast',function(){
		$('#popImage').attr('src', imageUrl).load(function(){
				var currentImage = imageBox.find('img');
				currentImage.css('maxHeight',newHeight);
				imageBox.css('top', (winH - imageBox.height()) / 2+$(window).scrollTop() + "px");
				imageBox.css('left', (winW - imageBox.width()) / 2+$(window).scrollLeft() + "px");
				loadingBox.fadeOut('fast',function(){
					imageBox.fadeIn('fast');
				});
			})
		});
	});
}
function contactNameCheck()
	{
		var nameTry = $('#' + 'contactName').val();
		var statusMessage = $('#' + 'contactName').parent().parent().find('.fieldMessage');
		var statusIcon = $('#' + 'contactName').parent().parent().find('.fieldIcon');
		if (nameTry.length == 0)
		{
			contactNameOk = 0;
		}
		else if (nameTry.length < 3 )
		{
			contactNameOk = 0;
		}
		else
		{
			contactNameOk = 1;
		}
	}
	
function contactEmailCheck()
	{
		var emailTry = $('#' + 'contactEmail').val();
		var statusMessage = $('#' + 'contactEmail').parent().parent().find('.fieldMessage');
		var statusIcon = $('#' + 'contactEmail').parent().parent().find('.fieldIcon');
		if (emailTry.length == 0)
		{
			contactEmailOk = 0;
		}
		else
		{
			$.ajax({
				type: 'POST',
				url: 'scripts/emailCheck.php',
				cache: false,
				async: false,
				dataType: 'json',
				data: 'emailTry=' + emailTry,
				success: function(response)
				{
					if (response[0].error == 0) {
						contactEmailOk = 1;
					} else {
						contactEmailOk = 0;
					}
				},
				error: function()
				{	
				}
			});
		}
    }
//Action Redirector
function acceptMessage(origin)
{
	window.location = origin;
	return false;
}
//Session Counter
function updateSession()
{
	var onlineCount = $('#' + 'onlineCount');
	var onlineText = $('#' + 'onlineText');
	var onlineList = $('#' + 'onlineList');
	var onlineText = $('#' + 'onlineText');
	var onlineUsers = $('#' + 'onlineUsers');
	var activityLog = $('#' + 'activityLog');
	$.ajax({
		type: 'POST',
		url: 'scripts/updateSession.php',
		cache: false,
		async: true,
		dataType: 'json',
		success: function(response)
		{
			if (response[0].error == 0) {
				activityLog.prepend(''+response[0].actionUpdate+'');
				onlineList.html(''+response[0].onlineList+'')
				onlineCount.html(''+response[0].onlineCount+'');
				onlineText.html(''+response[0].onlineText+'');
			} else {
			}
		},
		error: function()
		{	
		}
	});
	setTimeout(function(){updateSession()}, 10000);
}
function editGameProfiles()
{
	showLoading();
	var wowLink_messy = $('#wowLink').val();
	var wowLink = encodeURIComponent(wowLink_messy);
	var steamLink = $('#steamLink').val();
	var xboxLink = $('#xboxLink').val();
	var psnLink = $('#psnLink').val();
	var warLink = $('#warLink').val();
	var lotroLink = $('#lotroLink').val();
	var aionLink = $('#aionLink').val();
	var eqLink = $('#eqLink').val();
	var loadingBox = $('#loadingBox');
	
	if (!checkArmory(wowLink)) {
		popUp('Armory Link is Invalid!');
	} else {
		$.ajax({
			type: 'POST',
			url: 'scripts/editGameProfiles.php',
			cache: false,
			async: true,
			dataType: 'json',
			data: 'wowLink=' + wowLink + '&steamLink=' + steamLink + '&xboxLink=' + xboxLink + '&psnLink=' + psnLink + '&warLink=' + warLink + '&lotroLink=' + lotroLink + '&aionLink=' + aionLink + '&eqLink=' + eqLink,
			success: function(response)
			{
				if (response[0].error == 0) {
					popUp('Game Profiles Updated!');
				} else {
				}
			},
			error: function()
			{	
			}
		});	
	}
	return false;
}
function changePassword()
{
	showLoading();
	var oldPass = $('#oldPass').val();
	var newPass = $('#newPass').val();
	var confirmPass = $('#confirmPass').val();
	var loadingBox = $('#' + 'loadingBox');
	var passwordChangeForm = $('#' + 'passwordChangeForm');
	
	if (oldPass.length == 0 | newPass.length == 0 | confirmPass.length == 0) {
			popUp('Please Fill Out All Fields!');
	} else if (newPass != confirmPass) {
		popUp('New and Confirm Password do not Match!');
	} else {
		$.ajax({
			type: 'POST',
			url: 'scripts/passCheck.php',
			cache: false,
			async: true,
			dataType: 'json',
			data: 'passTry=' + oldPass,
			success: function(response)
			{
				if (response[0].error == 0) {
					$.ajax({
						type: 'POST',
						url: 'scripts/passChange.php',
						cache: false,
						async: true,
						dataType: 'json',
						data: 'newPass=' + newPass,
						success: function(response)
						{
							if (response[0].error == 0) {
								setCookie('userName', response[0].userName, '365');
								setCookie('userPass', response[0].userPass, '365');
								popUp('Password Updated!');
							} else {
							}
						},
						error: function()
						{	
						}
					});
				}
				else
				{
					popUp('Current Password Incorrect!');
				}
			},
			error: function()
			{	
			}
		});
	}
	return false;
}
var newAvatar = "";
function editProfile()
{
	showLoading();
	var emailTry = $('#editEmail').val();
	var avatarTry = $('#editAvatar').val();
	var mottoTry = $('#editMotto').val();
	var loadingBox = $('#loadingBox');
	
	if (emailTry.length == 0) {
		popUp('Email cannot be empty!');
	}
	if (!checkEmail(emailTry)) {
		popUp('Email is Invalid!');
	} else if (!checkAvatar(avatarTry))	{
		popUp('Avatar is Invalid!');
	} else {
		$.ajax({
			type: 'POST',
			url: 'scripts/editProfile.php',
			cache: false,
			async: false,
			dataType: 'json',
			data: 'emailTry=' + emailTry + '&avatarTry=' + newAvatar + '&mottoTry=' + mottoTry,
			success: function(response)
			{
				if (response[0].error == 0) {
					popUp('Profile Updated!');
				} else {
				}
			},
			error: function()
			{	
			}
		});	
	}
	return false;
}
function checkEmail(emailTry)
{
	var errors = 0;
	$.ajax({
		type: 'POST',
		url: 'scripts/emailCheck.php',
		cache: false,
		async: false,
		dataType: 'json',
		data: 'emailTry=' + emailTry,
		success: function(response)
		{
			if (response[0].error == 0) {
			} else {
				errors++;
			}
		},
		error: function()
		{	
		}
	});	
	if (errors != 0)
	{
		return false;
	}
	else
	{
		return true;
	}
}
function checkAvatar(avatarTry)
{
	var errors = 0;
	$.ajax({
		type: 'POST',
		url: 'scripts/linkImage.php',
		cache: false,
		async: false,
		dataType: 'json',
		data: 'imagePath=' + avatarTry,
		success: function(response)
		{
			if (response[0].error == 0) {
				$.ajax({
					type: 'POST',
					url: 'scripts/createThumb.php',
					cache: false,
					async: false,
					dataType: 'json',
					data: 'filePath=' + response[0].localPath,
					success: function(response)
					{
						newAvatar = response[0].thumbPath;
						$('#currentAvatar').attr('src',newAvatar);
					},
					error: function()
					{	
					}
				});
			} else {
				errors++;
			}
		},
		error: function()
		{	
		}
	});
	if (errors != 0) {
		return false;
	} else {
		return true;
	}
}
//Profile Error Checking
function checkArmory(wowLink)
{
	var errors = 0;
	$.ajax({
		type: 'POST',
		url: 'scripts/armoryCheck.php',
		cache: false,
		async: false,
		dataType: 'json',
		data: 'wowLink=' + wowLink,
		success: function(response)
		{
			if (response[0].error == 0) {
			} else {
				errors++;
			}
		},
		error: function()
		{	
			errors++;
		}
	});	
	if (errors != 0) {
		return false;
	} else {
		return true;
	}
}

function showLoading()
{
	var modalBoxHolder = $('#' + 'modalBoxHolder');
	var loadingBox = $('#' + 'loadingBox');
	var documentHeight = $(document).height();
	var winH = $(window).height();  
	var winW = $(window).width();
	modalBoxHolder.height(documentHeight);
	loadingBox.css('top', (winH - loadingBox.height()) / 2+$(window).scrollTop() + "px");
	loadingBox.css('left', (winW - loadingBox.width()) / 2+$(window).scrollLeft() + "px");
	modalBoxHolder.fadeIn('fast',function(){
		loadingBox.fadeIn('fast',function(){
		});
	});
}
function closePopup()
{
		var imageBox = $('#imageBox');
		var loadingBox = $('#loadingBox');
		var messageBox = $('#messageBox');
		if (imageBox.is(':visible'))
		{			
			imageBox.fadeOut('fast',function(){
				$('#modalBoxHolder').fadeOut('fast');
			});
		}
		if (loadingBox.is(':visible'))
		{			
			loadingBox.fadeOut('fast',function(){
				$('#modalBoxHolder').fadeOut('fast');
			});
		}
		if (messageBox.is(':visible'))
		{			
			messageBox.fadeOut('fast',function(){
				$('#modalBoxHolder').fadeOut('fast');
			});
		}
}
jQuery.fn.slideFadeShow = function(speed, easing, callback) {
        return this.animate({opacity: 'show', height: 'show'}, speed,
easing, callback);
}; 
jQuery.fn.slideFadeHide = function(speed, easing, callback) {
        return this.animate({opacity: 'hide', height: 'hide'}, speed,
easing, callback);
}; 