function toggleBox(element) { //Toggle boxes
	new Effect.toggle(element,'slide');
	return false;
}

function rateThis(id, rating) { 
  parameters = 'action=rate&item=' + id + '&rating=' + rating + '&ajax=1'; 
  new Ajax.Updater('ratingblock', '/includes/ajax.php', {method: 'get', parameters: parameters});
  return false;
}

function favThis(id) { 
  parameters = 'action=addfav&item=' + id + '&ajax=1';  
  new Ajax.Updater('favblock', '/includes/ajax.php', {method: 'get', parameters: parameters});
  return false;  
}

function addFriend(id) { 
  parameters = 'action=addfriend&item=' + id + '&ajax=1';  
  new Ajax.Updater('addfriendblock', '/includes/ajax.php', {method: 'get', parameters: parameters});
  return false;  
}

function countTime(id) { 
  setTimeout("countGame(" + id + ")",8000);
}

function countGame(id){
  new Ajax.Request('/includes/ajax.php?action=countGame&item=' + id + '&ajax=1', {asynchronous:true});
}

function removeFavGame(id){
	  parameters = 'action=deletefav&favid=' + id + '&ajax=1';  
	  new Effect.Fade ('gamerow' + id);
	  new Ajax.Updater('gamerow' + id, '/includes/ajax.php', {method: 'get', parameters: parameters}); 
	  return false;
}

function removeFriend(id){
	  parameters = 'action=deletefriend&friendid=' + id + '&ajax=1';  
	  new Effect.Fade ('friendrow' + id);
	  new Ajax.Updater('friendrow' + id, '/includes/ajax.php', {method: 'get', parameters: parameters}); 
	  return false;
}

function addComment() { //Process the signups
	if (!$('commentform') ) return false;
	$('commentform').onsubmit = function(){ 
		if ($('comment').value == '') return false;
		new Ajax.Updater('newcomment', '/includes/ajax.php?action=addcomment&ajax=1',{
			onLoading:function(request){},onComplete:function(request){
				new Insertion.Before('newcomment', $('newcomment').innerHTML);
				new Form.reset('commentform');
				new Effect.Highlight('newlyinserted');$('newlyinserted').id ='';
			},
			evalScripts:true,method: 'post',parameters:Form.serialize(this),asynchronous:true
			});
			return false; 
	}
}

function sendGame() {
	if (!$('friendform') ) return false;
	$('friendform').onsubmit = function(){ 
		new Ajax.Updater('friendstatus', '/includes/ajax.php?action=sendtofriend&ajax=1',{
			onLoading:function(request){},onComplete:function(request){
				new Form.reset('friendform');
				new Insertion.Before('friendstatus', $('friendstatus').innerHTML);
			},
			evalScripts:true,method: 'post',parameters:Form.serialize(this),asynchronous:true
			});
			return false; 
	}
}

function checkName(){ //Check availability of username on sign up
	new Form.Element.Observer('userUsername', 1, function(element, value) {
	new Ajax.Updater('username-results', '/includes/ajax.php?action=checkuser', {asynchronous:true, evalScripts:true,method: 'post', parameters:'userUsername=' + value})})
}

function checkEmail(){ //Check availability of email on sign up
	new Form.Element.Observer('userEmail', 1, function(element, value) {
	new Ajax.Updater('email-results', '/includes/ajax.php?action=checkemail', {asynchronous:true, evalScripts:true,method: 'post', parameters:"userEmail=" + value})})
}

function checkMath(){ //Check math problem
	new Form.Element.Observer('math', 0, function(element, value) {
	new Ajax.Updater('math-results', '/includes/ajax.php?action=checkmath', {asynchronous:true, evalScripts:true,method: 'post', parameters:"math=" + value})})
}

function checkPass(){ //Check password on sign up
	new Form.Element.Observer('userPassword', 1, function(element, value) {
		new Ajax.Updater('password-results', '/includes/ajax.php?action=checkpassword', {
			asynchronous:true, evalScripts:true,method: 'post',parameters:'userPassword=' + value
		}
		)
	}
	)
}

//Load the various onSubmit events
Event.observe(window, "load", addComment);
Event.observe(window, "load", sendGame);
