function ajaxFunction()
{
var xmlHttp;
try
  {
  // Firefox, Opera 8.0+, Safari
  xmlHttp=new XMLHttpRequest();
  }
catch (e)
  {
  // Internet Explorer
  try
    {
    xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
    }
  catch (e)
    {
    try
      {
      xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
      }
    catch (e)
      {
      alert("Your browser does not support AJAX!");
      return false;
      }
    }
  }
  xmlHttp.onreadystatechange=function()
    {
    if(xmlHttp.readyState==4)
      {
    	document.getElementById("votediv").innerHTML=xmlHttp.responseText;
      }
    }
  
    score=0;
	radioArray = document.ratethisgame.vote;
	for (i=0; i < radioArray.length; i++){
		if (radioArray[i].checked){
			score=radioArray[i].value;
        break
		}
	}  
  
  gameid=document.ratethisgame.gameid.value;
	
  xmlHttp.open("GET","vote_server.php?score="+score+"&gameid="+gameid,true);
  xmlHttp.send(null);
}



function tellAfriend()
{
var xmlHttp;
try
  {
  // Firefox, Opera 8.0+, Safari
  xmlHttp=new XMLHttpRequest();
  }
catch (e)
  {
  // Internet Explorer
  try
    {
    xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
    }
  catch (e)
    {
    try
      {
      xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
      }
    catch (e)
      {
      alert("Your browser does not support AJAX!");
      return false;
      }
    }
  }
  xmlHttp.onreadystatechange=function()
    {
    if(xmlHttp.readyState==4)
      {
    	document.getElementById("tellafriendid").innerHTML=xmlHttp.responseText;
      }
    }

  gameid=document.tellafriendform.gameid.value;
  yourname=document.tellafriendform.yourname.value;
  friendsemail=document.tellafriendform.friendsemail.value;
  
  if ((yourname=="") || (friendsemail=="") ||(friendsemail.indexOf("@")==-1)||(friendsemail.indexOf(".")==-1)){
	  alert("Please fill in both fields with valid values.");
	  return;
  }  
	
  xmlHttp.open("GET","tellfriend_server.php?yourname="+yourname+"&friendsemail="+friendsemail+"&gameid="+gameid,true);
  xmlHttp.send(null);
}


function addFavorite()
{
var xmlHttp;
try
  {
  // Firefox, Opera 8.0+, Safari
  xmlHttp=new XMLHttpRequest();
  }
catch (e)
  {
  // Internet Explorer
  try
    {
    xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
    }
  catch (e)
    {
    try
      {
      xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
      }
    catch (e)
      {
      alert("Your browser does not support AJAX!");
      return false;
      }
    }
  }
  xmlHttp.onreadystatechange=function()
    {
    if(xmlHttp.readyState==4)
      {
    	document.getElementById("addfavediv").innerHTML=xmlHttp.responseText;
      }
    }

  fav_game_id=document.fav_game.gameid.value;

	
  xmlHttp.open("GET","add_favorite_server.php?fav_game_id="+fav_game_id,true);
  xmlHttp.send(null);

}


function share_fb()
{


  fav_game_id=document.share_game.gameid.value;
  fav_game_name=document.share_game.gamename.value;
  fav_game_pic=document.share_game.gamepic.value;
  


 var attachment = 
{'media':
  [{'type': 'image',
    'src':  'http://www.bestguywins.com/'+fav_game_pic, 
    'href': 'http://www.bestguywins.com/playgame.php?id='+fav_game_id+'&name='+fav_game_name }],
  
 'name': fav_game_name + '!' ,
 'href':'http://www.bestguywins.com/playgame.php?id='+fav_game_id+'&name='+fav_game_name,
 'description':'A cool flash game on BestGuyWins.com! Click to play ' + fav_game_name +' online!'

 }

FB.Connect.streamPublish('', attachment, null, null, null, 
	function (post_id, exception, data){
		var user_message = "";
		if(data != null && data.user_message != null){
			user_message = data.user_message;
		}
		if(post_id != null && post_id != "null"){
			log(14,fav_game_name + "(" + fav_game_id +") was shared on facebook. and said: " + user_message );
		}
	}
);


}

function log(event_id, msg)
{
var xmlHttp;
try
  {
  // Firefox, Opera 8.0+, Safari
  xmlHttp=new XMLHttpRequest();
  }
catch (e)
  {
  // Internet Explorer
  try
    {
    xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
    }
  catch (e)
    {
    try
      {
      xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
      }
    catch (e)
      {
      alert("Your browser does not support AJAX!");
      return false;
      }
    }
  }
  
  xmlHttp.open("GET","log.php?event_id="+event_id+"&msg="+msg,true);
  xmlHttp.send(null);
  
}

function removeFavorite(game_id)
{
var xmlHttp;
try
  {
  // Firefox, Opera 8.0+, Safari
  xmlHttp=new XMLHttpRequest();
  }
catch (e)
  {
  // Internet Explorer
  try
    {
    xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
    }
  catch (e)
    {
    try
      {
      xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
      }
    catch (e)
      {
      alert("Your browser does not support AJAX!");
      return false;
      }
    }
  }
  xmlHttp.onreadystatechange=function()
    {
    if(xmlHttp.readyState==4)
      {
    	document.getElementById("removefavediv"+game_id).innerHTML=xmlHttp.responseText;
      	document.getElementById("favegame_"+game_id).style.display = 'none';
      }
    }



	
  xmlHttp.open("GET","remove_favorite_server.php?fav_game_id="+game_id,true);
  xmlHttp.send(null);

}

function addAndUpdateComments(){
		
	game_name_element=document.getElementById("game_name");
	if (game_name_element != null ){
		game_name=game_name_element.value;
	}else{
		game_name="";
	}
	
	game_id_element=document.getElementById("game_id");
	if (game_id_element != null ){
		game_id=game_id_element.value;
	}else{
		game_id="";
	}
	
	user_id_element=document.getElementById("user_id");
	if (user_id_element != null ){
		user_id=user_id_element.value;
	}else{
		user_id="";
	}
	
	user_name_element=document.getElementById("user_name");
	if (user_name_element != null ){
		user_name=user_name_element.value;
	}else{
		user_name="";
	}
	
	comment_element=document.getElementById("comment");
	if (comment_element != null ){
		comment=comment_element.value;
	}else{
		comment="";
	}
	
var xmlHttp;
try
  {
  // Firefox, Opera 8.0+, Safari
  xmlHttp=new XMLHttpRequest();
  }
catch (e)
  {
  // Internet Explorer
  try
    {
    xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
    }
  catch (e)
    {
    try
      {
      xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
      }
    catch (e)
      {
      alert("Your browser does not support AJAX!");
      return false;
      }
    }
  }
  xmlHttp.onreadystatechange=function()
	{
	if(xmlHttp.readyState==4)
  {
	  
	  getComments();
      document.getElementById("commentgame").style.display='none';
      document.getElementById("response_text").style.display='block';
      document.getElementById("response_text").innerHTML=xmlHttp.responseText;

  }
}

if (comment == "") 
	return;
	 
if (user_name == "") 
	return;
	
	
if (comment == "Write a comment...") 
	return;
	
if (user_name == "Your name...") 
	return;
	
	
  xmlHttp.open("GET","add_comment_server.php?game_id="+game_id+"&game_name="+game_name+"&comment="+comment+"&user_id="+user_id+"&user_name="+user_name,true);
  xmlHttp.send(null);
}

function addComment()
{
	
	game_id_element=document.getElementById("game_id");
	if (game_id_element != null ){
		game_id=game_id_element.value;
	}else{
		game_id="";
	}
	
	user_id_element=document.getElementById("user_id");
	if (user_id_element != null ){
		user_id=user_id_element.value;
	}else{
		user_id="";
	}
	
	user_name_element=document.getElementById("user_name");
	if (user_name_element != null ){
		user_name=user_name_element.value;
	}else{
		user_name="";
	}
	
	comment_element=document.getElementById("comment");
	if (comment_element != null ){
		comment=comment_element.value;
	}else{
		comment="";
	}
	
var xmlHttp;
try
  {
  // Firefox, Opera 8.0+, Safari
  xmlHttp=new XMLHttpRequest();
  }
catch (e)
  {
  // Internet Explorer
  try
    {
    xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
    }
  catch (e)
    {
    try
      {
      xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
      }
    catch (e)
      {
      alert("Your browser does not support AJAX!");
      return false;
      }
    }
  }
  
  xmlHttp.open("GET","add_comment_server.php?game_id="+game_id+"&comment="+comment+"&user_id="+user_id+"&user_name="+user_name,true);
  xmlHttp.send(null);
  
}


function getComments()
{
	game_id_element=document.getElementById("game_id");
	if (game_id_element != null ){
		game_id=game_id_element.value;
	}else{
		game_id="";
	}
	
	user_id_element=document.getElementById("user_id");
	if (user_id_element != null ){
		user_id=user_id_element.value;
	}else{
		user_id="";
	}
	
	user_name_element=document.getElementById("user_name");
	if (user_name_element != null ){
		user_name=user_name_element.value;
	}else{
		user_name="";
	}
	
	
	
var xmlHttp;
try
  {
  // Firefox, Opera 8.0+, Safari
  xmlHttp=new XMLHttpRequest();
  }
catch (e)
  {
  // Internet Explorer
  try
    {
    xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
    }
  catch (e)
    {
    try
      {
      xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
      }
    catch (e)
      {
      alert("Your browser does not support AJAX!");
      return false;
      }
    }
  }
xmlHttp.onreadystatechange=function()
{
if(xmlHttp.readyState==4)
  {
	document.getElementById("comments").innerHTML=xmlHttp.responseText;
  
  }
}
  xmlHttp.open("GET","get_comments_server.php?game_id="+game_id+"&user_id="+user_id+"&user_name="+user_name,true);
  xmlHttp.send(null);
  
}

function commentClicked(){
	var comment = document.getElementById("comment").value;
	if (comment == "Write a comment..."){
		document.getElementById("comment").value="";
	}
}

function nameClicked(){
	var username = document.getElementById("user_name").value;
	if (username == "Your name..."){
		document.getElementById("user_name").value="";
	}
}


function commentBlurred(){
	var comment = document.getElementById("comment").value;
	if (comment == ""){
		document.getElementById("comment").value="Write a comment...";
	}
}

function nameBlurred(){
	var username = document.getElementById("user_name").value;
	if (username == ""){
		document.getElementById("user_name").value="Your name...";
	}
}

function showAllComments(){
	
	var i = 0;
	
	var comment_elem = document.getElementById("comment_"+i);
	
	while (comment_elem != null){
		comment_elem.style.display = 'block';
		i++;
		comment_elem = document.getElementById("comment_"+i);
	}
	document.getElementById("show_all_comments").style.display = 'none';
} 

function translate(text, divId){
	google.language.translate(text, "en", "he", function(result) {
  	if (!result.error) {
    	var container = document.getElementById(divId);
    		container.innerHTML = result.translation;
  	}
	});
}



