function changeCommentOrder()
{
	//Determine the order selected
	select_box = $('comment_ordering').sorttype;
	comment_order = select_box.options[select_box.selectedIndex].value;

	//Get the date one year in the future	
	var today = new Date();
	today.setDate(today.getDate() + 365);

	//Create the cookie, and set it to expire one year from now
	document.cookie = 'comment_order=' + comment_order + '; expires=' + today.toUTCString() + '; domain=.digyourowngrave.com; path=/';

	//Reload the page	
	location.reload(true);
}

function noFirstComments(num_comments)
{
	var comment_string = $('comment').value.toLowerCase(); // make the search case insensitive
	
	if (num_comments == 0)
	{
		if (comment_string.match("first") || comment_string.match("1st"))
		{
			return displayFirstCommentDialog();
		}
	}
	else
	{
		if (comment_string.match("first comment") || comment_string.match("first post") || comment_string.match("1st comment") || comment_string.match("1st post"))
		{
			return displayFirstCommentDialog();
		}
	}	
	
	return true;
}

function displayFirstCommentDialog()
{
	return confirm('FIRST COMMENT posts are not allowed on DigYourOwnGrave. They will be deleted and repeat offenders will be banned. Are you sure you wish to submit this comment?');
}

function checkComment(num_comments)
{
	var numErrors = 0;

	var comment_string = $('comment').value;

	// Check for no capitalization
	var regex = new RegExp("[A-Z]");
	var noCaps = !regex.exec(comment_string);
	if(noCaps)
	{
		numErrors++;
	}

	// Check for no punctuation
	var regex = new RegExp('[.,!?:;]');
	var noPunctuation = !regex.exec(comment_string);
	if(noPunctuation)
	{
		numErrors++;
	}
	
	// Check for too few words
	var numWords = countWords(comment_string);
	if(numWords<4)
	{
		numErrors++;
	}

	if(numErrors>1)
	{
		alert('Please edit your comment so that it contains adequate content, as well as proper spelling, capitalization, and punctuation.');
		
		//The form will not be submitted
		return false;
	}
	
	return noFirstComments(num_comments);
}

function countWords(comment_string)
{
	var y = comment_string;
	var numWords = 0;
	a = y.replace(/\s/g,' ');
	a = a.split(' ');
	for(z=0; z<a.length; z++)
	{
		if (a[z].length > 0) numWords++;
	}
	return numWords;
}

function launchLinkInParentWindow(url, shouldClosePopupWindow)
{
	self.opener.location = url;
	
	if(shouldClosePopupWindow)
		self.close();
}

function showHighScoreReviewForm()
{
	if ($('highscore_review_form').style.display == 'none')
		Effect.BlindDown('highscore_review_form', {queue: {position: 'end', scope: 'highscore_review_form'}} );
}

function showHighScoreInstructions()
{
	if ($('highscore_instructions').style.display == 'none')
		Effect.BlindDown('highscore_instructions', {queue: {position: 'end', scope: 'highscore_instructions'}} );
}

function showScreenshotInstructions()
{
	if ($('screenshot_instructions').style.display == 'none')
		Effect.BlindDown('screenshot_instructions', {queue: {position: 'end', scope: 'screenshot_instructions'}} );
}

function add_to_favorites(post_id, page_url)
{
	var parameters = "post_id=" + post_id + "&page_button_pressed_on=" + page_url;
	button_id = 'favorites_button_' + post_id;
	new Ajax.Updater(button_id, '/lg_favorites/add_post_to_favorites.php', {method: 'post', parameters: parameters, evalScripts: true, onLoading: function() { fadeCallback(button_id, 'addtofavs'); }, onComplete: function() { appearCallback(button_id, 'addtofavs'); } });
}

function remove_from_favorites(post_id, page_url)
{
	var parameters = "id=" + post_id + "&page_button_pressed_on=" + page_url;
	button_id = 'favorites_button_' + post_id;
	new Ajax.Updater(button_id, '/lg_favorites/remove_post_from_favorites.php', { method: 'post', parameters: parameters, evalScripts: true, onLoading: function() { fadeCallback(button_id, 'addtofavs'); }, onComplete: function() { appearCallback(button_id, 'addtofavs'); } });
}

function remove_from_favorites_page(favorite_id, user_id)
{
	var parameters = "id=" + favorite_id + '&user_id=' + user_id + '&favs_page=1';
	new Ajax.Updater('ajax_temp', '/lg_favorites/remove_post_from_favorites.php', { method: 'post', parameters: parameters, evalScripts: true, onLoading: function() { Effect.Fade('favorite_' + favorite_id, { duration: 0.5, from: 1.0, to: 0.1, queue: {position:'end', scope: 'removefromfavspage'} }); }, onComplete: function() { Effect.BlindUp('favorite_' + favorite_id, { queue: {position:'end', scope: 'removefromfavspage'} }); } });
}

function fadeCallback(element_id, scope)
{
	Effect.Fade(element_id, { duration: 0.2, from: 1.0, to: 0.3, queue: {position:'end', scope: scope} });
}

function appearCallback(element_id, scope)
{
	Effect.Appear(element_id, { duration: 1.5, from: 0.3, to: 1.0, queue: {position:'end', scope: scope} } );
}

function updateGamesNumPlayersWidget()
{
	var parameters = "script=1";
	new Ajax.PeriodicalUpdater('games_num_players', '/multiplayer/display_games_num_players.php', { method: 'get', parameters: parameters, frequency: 15, decay: 1.05 });
}

// Last five arguments can be null
function addGameAnalytics(game_highscores_id, version, event_code, game_mode, level, var1, var2, var3)
{
	if (game_mode == null)
	{
		game_mode = '';
	}
	
	if (level == null)
	{
		level = '';
	}
	
	if (var1 == null)
	{
		var1 = '';
	}
	
	if (var2 == null)
	{
		var2 = '';
	}
	
	if (var3 == null)
	{
		var3 = '';
	}

	var parameters = "game_highscores_id=" + game_highscores_id + "&version=" + version + "&event_code=" + event_code + "&game_mode=" + game_mode + "&level=" + level + "&var1=" + var1 + "&var2=" + var2 + "&var3=" + var3;
	
	new Ajax.Request('/game_analytics/add_data.php', { method: 'post', parameters: parameters });
}

//Cookie functions
function createCookie(name,value,days)
{
	if (days)
	{
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	}
	else var expires = "";
	document.cookie = name+"="+value+expires+"; path=/";
}

function readCookie(name)
{
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++)
	{
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return null;
}

function eraseCookie(name)
{
	createCookie(name,"",-1);
}