// JavaScript Document

function showTopGames(tab) {
	var topGames_thisWeek = document.getElementById('topGames_thisWeek');
	var topGames_thisMonth = document.getElementById('topGames_thisMonth');
	var topGames_allTime = document.getElementById('topGames_allTime');
	var thisWeek = document.getElementById('thisWeek');
	var thisMonth = document.getElementById('thisMonth');
	var allTime = document.getElementById('allTime');
	
	if (tab == "thisWeek") {
		allTime.onclick = function () { showTopGames('allTime'); };
		allTime.innerHTML = '<a href="javascript:showTopGames(\'allTime\');">All Time</a>';
		allTime.style.backgroundImage = "url(images/top-games-off-bg.gif)";
		allTime.style.cursor = "pointer";
		topGames_allTime.style.width = "0px";
		topGames_allTime.style.height = "0px";
		topGames_allTime.style.display = "none";
		
		thisMonth.onclick = function () { showTopGames('thisMonth'); };
		thisMonth.innerHTML = '<a href="javascript:showTopGames(\'thisMonth\');">This Month</a>';
		thisMonth.style.backgroundImage = "url(images/top-games-off-bg.gif)";
		thisMonth.style.cursor = "pointer";
		topGames_thisMonth.style.width = "0px";
		topGames_thisMonth.style.height = "0px";
		topGames_thisMonth.style.display = "none";
		
		thisWeek.onclick = "";
		thisWeek.innerHTML = 'This Week';
		thisWeek.style.backgroundImage = "url(images/top-games-on-bg.gif)";
		thisWeek.style.cursor = "arrow";
		topGames_thisWeek.style.width = "537px";
		topGames_thisWeek.style.height = "auto";
		topGames_thisWeek.style.display = "block";
	} else if (tab == "thisMonth") {
		allTime.onclick = function () { showTopGames('allTime'); };
		allTime.innerHTML = '<a href="javascript:showTopGames(\'allTime\');">All Time</a>';
		allTime.style.backgroundImage = "url(images/top-games-off-bg.gif)";
		allTime.style.cursor = "pointer";
		topGames_allTime.style.width = "0px";
		topGames_allTime.style.height = "0px";
		topGames_allTime.style.display = "none";
		
		thisWeek.onclick = function () { showTopGames('thisWeek'); };
		thisWeek.innerHTML = '<a href="javascript:showTopGames(\'thisWeek\');">This Week</a>';
		thisWeek.style.backgroundImage = "url(images/top-games-off-bg.gif)";
		thisWeek.style.cursor = "pointer";
		topGames_thisWeek.style.width = "0px";
		topGames_thisWeek.style.height = "0px";
		topGames_thisWeek.style.display = "none";
		
		thisMonth.onclick = "";
		thisMonth.innerHTML = 'This Month';
		thisMonth.style.backgroundImage = "url(images/top-games-on-bg.gif)";
		thisMonth.style.cursor = "arrow";
		topGames_thisMonth.style.width = "537px";
		topGames_thisMonth.style.height = "auto";
		topGames_thisMonth.style.display = "block";
	} else if (tab == "allTime") {
		thisMonth.onclick = function () { showTopGames('thisMonth'); };
		thisMonth.innerHTML = '<a href="javascript:showTopGames(\'thisMonth\');">This Month</a>';
		thisMonth.style.backgroundImage = "url(images/top-games-off-bg.gif)";
		thisMonth.style.cursor = "pointer";
		topGames_thisMonth.style.width = "0px";
		topGames_thisMonth.style.height = "0px";
		topGames_thisMonth.style.display = "none";
		
		thisWeek.onclick = function () { showTopGames('thisWeek'); };
		thisWeek.innerHTML = '<a href="javascript:showTopGames(\'thisWeek\');">This Week</a>';
		thisWeek.style.backgroundImage = "url(images/top-games-off-bg.gif)";
		thisWeek.style.cursor = "pointer";
		topGames_thisWeek.style.width = "0px";
		topGames_thisWeek.style.height = "0px";
		topGames_thisWeek.style.display = "none";
		
		allTime.onclick = "";
		allTime.innerHTML = 'All Time';
		allTime.style.backgroundImage = "url(images/top-games-on-bg.gif)";
		allTime.style.cursor = "arrow";
		topGames_allTime.style.width = "537px";
		topGames_allTime.style.height = "auto";
		topGames_allTime.style.display = "block";
	}
}

function showHideFavorites(tab) {
	var justPlayed = document.getElementById('justPlayed');
	var favoriteGame = document.getElementById('favoriteGame');
	var jPlayed = document.getElementById('jPlayed');
	var fGame = document.getElementById('fGame');
	
	if (tab == "jPlayed") {
		fGame.onclick = function () { showHideFavorites('fGame'); };
		fGame.innerHTML = '<a href="javascript:showHideFavorites(\'fGame\');">My Favorite</a>';
		fGame.style.backgroundImage = "url(images/favorites-off-bg.gif)";
		fGame.style.cursor = "pointer";
		favoriteGame.style.width = "0px";
		favoriteGame.style.height = "0px";
		favoriteGame.style.display = "none";
		
		jPlayed.onclick = "";
		jPlayed.innerHTML = 'Just Played';
		jPlayed.style.backgroundImage = "url(images/favorites-on-bg.gif)";
		jPlayed.style.cursor = "arrow";
		justPlayed.style.width = "292px";
		justPlayed.style.height = "auto";
		justPlayed.style.display = "block";
	} else if (tab == "fGame") {
		fGame.onclick = "";
		fGame.innerHTML = 'My Favorite';
		fGame.style.backgroundImage = "url(images/favorites-on-bg.gif)";
		fGame.style.cursor = "arrow";
		favoriteGame.style.width = "292px";
		favoriteGame.style.height = "auto";
		favoriteGame.style.display = "block";
		
		jPlayed.onclick = function () { showHideFavorites('jPlayed'); };
		jPlayed.innerHTML = '<a href="javascript:showHideFavorites(\'jPlayed\');">Just Played</a>';
		jPlayed.style.backgroundImage = "url(images/favorites-off-bg.gif)";
		jPlayed.style.cursor = "pointer";
		justPlayed.style.width = "0px";
		justPlayed.style.height = "0px";
		justPlayed.style.display = "none";
	}
}

function updateTopGames(filter,mode) {	
	if (mode == 1) {
		mode = "thisWeek";
		mode2 = "thisweek";
	} else if (mode == 2) {
		mode = "thisMonth";
		mode2 = "thismonth";
	} else {
		mode = "allTime";
		mode2 = "alltime";
	}
	
	ajax = ajaxFunction();
	ajax.onreadystatechange = function () { updateTopGames_stateChanged(filter,mode); };
	var url = base_url+ "includes/top-games.php";
	url = url+ "?f=" +escape(encodeURI(filter));
	url = url+ "&m=" +escape(encodeURI(mode2));
	url = url+ "&ajax=1";
	ajax.open("GET",url,true);
	ajax.send(null);
}

function ajaxFunction() {
	var ajax;
	try {
		// Firefox, Opera 8.0+, Safari
		ajax = new XMLHttpRequest();
	}
	catch (e) {
		// Internet Explorer
		try {
			ajax = new ActiveXObject("Msxml2.XMLHTTP");
  	 	}
  		catch (e) {
    		try {
      			ajax = new ActiveXObject("Microsoft.XMLHTTP");
      		} catch (e) {
      			alert("Your browser does not support AJAX!");
      			return false;
      		}
    	}
	}
	return ajax;
}

function updateTopGames_stateChanged(filter,mode) { 
	var topGameHTML = document.getElementById('topGames_' +mode);
	
	if (ajax.readyState == 4) {
		topGameHTML.innerHTML = ajax.responseText;
		
		if (mode == "thisWeek") {
			updateTopGames(filter,2);
		} else if (mode == "thisMonth") {
			updateTopGames(filter,3);
		}
	}
}

function submitNewReview() {
	document.getElementById('submitReview_btn').style.display = "none";
	
	var errorMSG = "";
	if (document.review.reviewtitle.value == "") {
		errorMSG = errorMSG+ "A short comment was not entered\n";
	}
	if (document.review.newreview.value == "") {
		errorMSG = errorMSG+ "No text was entered in the review post box\n";
	}
	if (document.review.reviewrating.value == 0) {
		errorMSG = errorMSG+ "Please rate the game 1-10";
	}
	
	if (errorMSG == "") {
		ajax = ajaxFunction();
		var contentType = "application/x-www-form-urlencoded; charset=UTF-8";
		ajax.onreadystatechange = function () { submitNewReview_stateChanged(); };
		var url = base_url+ "includes/ajax-comments.php";
		var newreview = document.review.newreview.value;
		var reviewtitle = document.review.reviewtitle.value;
		var reviewrating = document.review.reviewrating.value;
		var id = document.review.id.value;
		if (document.review.verifycode) {
			var verifyverifycode = document.review.verifycode.value;
		} else {
			var verifyverifycode = 0;
		}
		var query = "newreview=" + escape(encodeURI(newreview));
		query = query+ "&reviewtitle=" + escape(encodeURI(reviewtitle));
		query = query+ "&reviewrating=" + escape(encodeURI(reviewrating));
		query = query+ "&id=" + escape(id);
		query = query+ "&verifycode=" + escape(encodeURI(verifyverifycode));
		ajax.open("POST", url, true);
		ajax.setRequestHeader("Content-Type", contentType);
		ajax.send(query);
	} else {
		alert(errorMSG);
		document.getElementById('submitReview_btn').style.display = "block";
	}
}

function submitNewReview_stateChanged() { 
	var allReviewsBoxHTML = document.getElementById('allReviewsBox');
	if (document.getElementById('vcode')) {
		var captchaHTML = document.getElementById('vcode');
	}
	var codeError = '<div class="gameDarkReviewBox" style="color: red; padding: 5px; font-weight: bold; text-align: center;"><div style="color: red; padding: 5px; font-weight: bold; text-align: center;">The security code you entered is incorrect! If it still doesn\'t work try refreshing the page.</div></div>';
	
	if (ajax.readyState == 4) {
		if (ajax.responseText != codeError) {
			document.review.newreview.value = "";
			document.review.reviewtitle.value = "";
			if (document.review.verifycode) {
				document.review.verifycode.value = "";
				captchaHTML.innerHTML = "";
				captchaHTML.innerHTML = '<img src="' +base_url+ 'includes/imageverification.php" alt="" />';
			}
		}
		
		allReviewsBoxHTML.innerHTML = ajax.responseText;
		document.getElementById('submitReview_btn').style.display = "block";
	}
}

function submitGameVote(gameid,vote,userid) {
	ajax = ajaxFunction();
	ajax.onreadystatechange = function () { submitGameVote_stateChanged(vote,userid); };
	var url = base_url+ "includes/ajax-voting.php";
	url = url+ "?gameid=" +gameid;
	url = url+ "&userid=" +userid;
	url = url+ "&vote=" +vote;
	ajax.open("GET",url,true);
	ajax.send(null);
}

function submitGameVote_stateChanged(vote,userid) { 
	var gamePlaysBoxHTML = document.getElementById('gamePlaysInnerBox');
	var passFailBoxHTML = document.getElementById('passFailBox');
	
	if (ajax.readyState == 4) {
		gamePlaysBoxHTML.innerHTML = ajax.responseText;
		if (userid != 0) {
			passFailBoxHTML.innerHTML = '';
			if (vote == 1) {
				passFailBoxHTML.innerHTML = '<img src="' +base_url+ 'images/passed-game.gif" width="244" height="46" alt="-You Passed This Game-" />';
			} else {
				passFailBoxHTML.innerHTML = '<img src="' +base_url+ 'images/failed-game.gif" width="244" height="46" alt="-You Failed This Game-" />';
			}
		}
	}
}

function pageSelect(url) {
	location.href = url;
}
