// Javascript member functions...
// these are basic ajax functions that are used for member purposes.
var currentgameslug = "";

function nukeGame(gameslug) {
	
	var answer = confirm("Nuking a game cost "+NUKEPOINTS+" Points!  Are you sure?")
	
	if (answer) {
		var xmlhttp =  new XMLHttpRequest();
		xmlhttp.open('POST', 'subs/nukegame.php', true);
		xmlhttp.onreadystatechange = function() {
    		if (xmlhttp.readyState == 4) {
    			// Your callback code goes here
				//alert(xmlhttp.status);
				//alert(xmlhttp.responseText);
				//document.getElementById("game_rating").innerHTML = xmlhttp.responseText;
				alert(xmlhttp.responseText);
	   		}
		}
		xmlhttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
    	xmlhttp.send('game=' + gameslug);			
	}
}

function promoteGame(gameslug) {
	var answer = confirm("Promoting a game cost "+PROMOTEPOINTS+" Points!  Are you sure?")
	
	if (answer) {
		
		var xmlhttp =  new XMLHttpRequest();
		xmlhttp.open('POST', 'subs/promotegame.php', true);
		xmlhttp.onreadystatechange = function() {
    		if (xmlhttp.readyState == 4) {
    			// Your callback code goes here
				//alert(xmlhttp.status);
				//alert(xmlhttp.responseText);
				//document.getElementById("game_rating").innerHTML = xmlhttp.responseText;
				alert(xmlhttp.responseText);
	   		}
		}
		xmlhttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
    	xmlhttp.send('game=' + gameslug);			
	}
}

function voteAgain(gameslug) {
	currentgameslug = gameslug;
	var answer = confirm("Voting again cost "+VOTEAGAINPOINTS+" Points!  Are you sure?")
	
	if (answer) {
		var xmlhttp =  new XMLHttpRequest();
		xmlhttp.open('POST', 'subs/voteagain.php', true);
		xmlhttp.onreadystatechange = function() {
    		if (xmlhttp.readyState == 4) {
    			// Your callback code goes here
				//alert(xmlhttp.status);
				//alert(xmlhttp.responseText);
				//document.getElementById("game_rating").innerHTML = xmlhttp.responseText;
				alert(xmlhttp.responseText);
				if (xmlhttp.responseText == "You can now rate this game again!") {
					getRatingHTML(currentgameslug);
				}
	   		}
		}
		xmlhttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
    	xmlhttp.send('game=' + gameslug);				
	}
}

function getRatingHTML(gameslug) {
		var xmlhttp =  new XMLHttpRequest();
		xmlhttp.open('POST', 'subs/getratinghtml.php', true);
		xmlhttp.onreadystatechange = function() {
    		if (xmlhttp.readyState == 4) {
    			// Your callback code goes here
				//alert(xmlhttp.status);
				//alert(xmlhttp.responseText);
				//document.getElementById("game_rating").innerHTML = xmlhttp.responseText;
				//alert(xmlhttp.responseText);
				document.getElementById("game_rating").innerHTML = xmlhttp.responseText;
	   		}
		}
		xmlhttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
    	xmlhttp.send('game=' + gameslug);				
}

function faveGame(gameslug) {
	var xmlhttp =  new XMLHttpRequest();
	xmlhttp.open('POST', 'subs/favegame.php', true);
	xmlhttp.onreadystatechange = function() {
    	if (xmlhttp.readyState == 4) {
    		// Your callback code goes here
			//alert(xmlhttp.status);
			//alert(xmlhttp.responseText);
			//document.getElementById("game_rating").innerHTML = xmlhttp.responseText;
			alert(xmlhttp.responseText);
   		}
	}
	xmlhttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
    xmlhttp.send('game=' + gameslug);				
}

function addFriend(username) {
	var xmlhttp =  new XMLHttpRequest();
	xmlhttp.open('POST', 'subs/addfriend.php', true);
	xmlhttp.onreadystatechange = function() {
    	if (xmlhttp.readyState == 4) {
    		// Your callback code goes here
			//alert(xmlhttp.status);
			//alert(xmlhttp.responseText);
			//document.getElementById("game_rating").innerHTML = xmlhttp.responseText;
			alert(xmlhttp.responseText);
   		}
	}
	xmlhttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
    xmlhttp.send('user=' + username);				
}

function sendEmail() {
	//alert("sending email");
	var xmlhttp =  new XMLHttpRequest();
	xmlhttp.open('POST', 'subs/sendemail.php', true);
	xmlhttp.onreadystatechange = function() {
    	if (xmlhttp.readyState == 4) {
    		// Your callback code goes here
			//alert(xmlhttp.status);
			//alert(xmlhttp.responseText);
			document.getElementById("EmailHolder").innerHTML = xmlhttp.responseText;
			//alert(xmlhttp.responseText);
			
   		}
	}
	xmlhttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
    xmlhttp.send('emailto=' + document.getElementById('emailto').value + '&youremail=' + document.getElementById('youremail').value + '&yourname=' + document.getElementById('yourname').value + '&subject=' + document.getElementById('subject').value + '&message=' + document.getElementById('message').value + '&gameslug=' + document.getElementById('gameslug').value + '&gametitle=' + document.getElementById('gametitle').value );
	


}

function sendAnotherEmail(gameTitle, urlslug) {
	var xmlhttp =  new XMLHttpRequest();
	xmlhttp.open('POST', 'chunks/email-form.php', true);
	xmlhttp.onreadystatechange = function() {
    	if (xmlhttp.readyState == 4) {
    		// Your callback code goes here
			//alert(xmlhttp.status);
			//alert(xmlhttp.responseText);
			document.getElementById("EmailHolder").innerHTML = xmlhttp.responseText;
			//alert(xmlhttp.responseText);
			
   		}
	}
	xmlhttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
    xmlhttp.send('gametitle='+gameTitle+'&urlslug='+urlslug);
}





function sendEmail_ref() {
	//alert("sending email");
	var xmlhttp =  new XMLHttpRequest();
	xmlhttp.open('POST', 'subs/sendemail_ref.php', true);
	xmlhttp.onreadystatechange = function() {
    	if (xmlhttp.readyState == 4) {
    		// Your callback code goes here
			//alert(xmlhttp.status);
			//alert(xmlhttp.responseText);
			document.getElementById("EmailHolder_ref").innerHTML = xmlhttp.responseText;
			//alert(xmlhttp.responseText);
			
   		}
	}
	xmlhttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
    xmlhttp.send('emailto=' + document.getElementById('emailto').value + '&youremail=' + document.getElementById('youremail').value + '&yourname=' + document.getElementById('yourname').value + '&subject=' + document.getElementById('subject').value + '&message=' + document.getElementById('message').value );
	
}

function sendAnotherEmail_ref() {
	var xmlhttp =  new XMLHttpRequest();
	xmlhttp.open('POST', 'chunks/email-form_ref.php', true);
	xmlhttp.onreadystatechange = function() {
    	if (xmlhttp.readyState == 4) {
    		// Your callback code goes here
			//alert(xmlhttp.status);
			//alert(xmlhttp.responseText);
			document.getElementById("EmailHolder_ref").innerHTML = xmlhttp.responseText;
			//alert(xmlhttp.responseText);
			
   		}
	}
	xmlhttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
    xmlhttp.send('sendemail=1');
}



var lastCommentTime = 0;

function addComment(urlslug) {
	
	newcomment = document.getElementById("yourcomment").value;
	if (newcomment=="") {
		alert("You cant post an empty comment!");
		return;
	}
	if (newcomment.length<4) {
		alert("Minimum comment length is 4 characters!");
		return;
	}
	var d = new Date();
	var commentTime = d.getTime();
	if (lastCommentTime!=0 && commentTime-lastCommentTime<5*60000) {
		alert("Anti-spam - you must leave at least 5 minutes between comments!");
		return;
	}
	
	var answer = confirm("You will not be able to edit you comment afterwards\nPlease confirm you want to post:")
	
	
	
	if (answer) {
		lastCommentTime = commentTime;
		//alert(newcomment);
	
		var xmlhttp =  new XMLHttpRequest();
		xmlhttp.open('POST', 'subs/comment-manager.php', true);
		xmlhttp.onreadystatechange = function() {
    		if (xmlhttp.readyState == 4) {
    			// Your callback code goes here
				//alert(xmlhttp.status);
				//alert(xmlhttp.responseText);
				if (xmlhttp.responseText == "Wait 5 Minutes") {
					
					alert("Anti-spam - you must leave at least 5 minutes between comments!");
					lastCommentTime = 0;
					
				} else {
					document.getElementById("commentsList").innerHTML = xmlhttp.responseText;
					//alert(xmlhttp.responseText);
			
					alert("Thank you for your comment!");
				}
	   		}
		}
		xmlhttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
    	xmlhttp.send('action=addcomment&slug='+urlslug+'&comment='+newcomment);
	}
}

function deleteComment(urlslug, id) {
	
	//newcomment = document.getElementById("yourcomment").value;
	//if (newcomment=="") {
	//	alert("You cant post an empty comment!");
	//	return;
	//}
	
	var answer = confirm("Are you sure you want to delete this comment?")
	
	
	
	if (answer) {
	
		//alert(newcomment);
	
		var xmlhttp =  new XMLHttpRequest();
		xmlhttp.open('POST', 'subs/comment-manager.php', true);
		xmlhttp.onreadystatechange = function() {
    		if (xmlhttp.readyState == 4) {
    			// Your callback code goes here
				//alert(xmlhttp.status);
				//alert(xmlhttp.responseText);
				document.getElementById("commentsList").innerHTML = xmlhttp.responseText;
				//alert(xmlhttp.responseText);
			
				//alert("Thank you for your comment!");
	   		}
		}
		xmlhttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
    	xmlhttp.send('action=deletecomment&slug='+urlslug+'&id='+id);
	}
}




function addProfileComment(userprofile) {
	
	newcomment = document.getElementById("yourcomment").value;
	if (newcomment=="") {
		alert("You cant post an empty comment!");
		return;
	}
	
	var answer = confirm("You will not be able to edit you comment afterwards\nPlease confirm you want to post:")
	
	
	
	if (answer) {
	
		//alert(newcomment);
	
		var xmlhttp =  new XMLHttpRequest();
		xmlhttp.open('POST', 'subs/comment-manager.php', true);
		xmlhttp.onreadystatechange = function() {
    		if (xmlhttp.readyState == 4) {
    			// Your callback code goes here
				//alert(xmlhttp.status);
				//alert(xmlhttp.responseText);
				document.getElementById("commentsList").innerHTML = xmlhttp.responseText;
				//alert(xmlhttp.responseText);
			
				alert("Thank you for your comment!");
	   		}
		}
		xmlhttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
    	xmlhttp.send('action=addcomment&profile='+userprofile+'&comment='+newcomment);
	}
}

function deleteProfileComment(profile, id, ownComment) {
	
	//newcomment = document.getElementById("yourcomment").value;
	//if (newcomment=="") {
	//	alert("You cant post an empty comment!");
	//	return;
	//}
	
	var answer = confirm("Are you sure you want to delete this comment?")
	
	
	
	if (answer) {
	
		//alert(newcomment);
	
		var xmlhttp =  new XMLHttpRequest();
		xmlhttp.open('POST', 'subs/comment-manager.php', true);
		xmlhttp.onreadystatechange = function() {
    		if (xmlhttp.readyState == 4) {
    			// Your callback code goes here
				//alert(xmlhttp.status);
				//alert(xmlhttp.responseText);
				document.getElementById("commentsList").innerHTML = xmlhttp.responseText;
				//alert(xmlhttp.responseText);
			
				//alert("Thank you for your comment!");
	   		}
		}
		xmlhttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
    	xmlhttp.send('action=deletecomment&profile='+profile+'&id='+id+'&own='+ownComment);
	}
}
