function callBack(url, divid)
{
   xmlHttp=GetXmlHttpObject()
   

   if (xmlHttp==null)
   {
      alert ("Browser does not support HTTP Request")
      return
   } 
    
   /*url=url+"?sid="+Math.random()*/


    //xmlHttp.onreadystatechange=stateChanged
    xmlHttp.onreadystatechange=function(){
    	if (xmlHttp.readyState==4)
    	   {
    	   
    	      if(xmlHttp.status == 200)
    	      { 
    	         document.getElementById(divid).innerHTML=xmlHttp.responseText
    	      } 
    		  else
    		  {
    		  	alert("Probleem met het request!\r\nStatus:\r\n"+xmlHttp.status);
    		  }
    		  
    	   }
    };
   	xmlHttp.open("GET",url,true)
    xmlHttp.send(null)
}

function callBackPOST(url, divid, params){
	xmlHttp=GetXmlHttpObject()
	   
	   if (xmlHttp==null)
	   {
	      alert ("Browser does not support HTTP Request")
	      return
	   } 

	    xmlHttp.onreadystatechange=function(){
	    	if (xmlHttp.readyState==4)
	    	   {
	    	      if(xmlHttp.status == 200)
	    	      { 
	    	    	 //alert(xmlHttp.responseText);
	    	         document.getElementById(divid).innerHTML=xmlHttp.responseText
	    	      } 
	    		  else
	    		  {
	    		  	alert("Probleem met het request!\r\nStatus:\r\n"+xmlHttp.status);
	    		  }
	    		  
	    	   }
	    };
	   	xmlHttp.open("POST",url,true);
	   	//Send the proper header information along with the request
	   	xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	   	xmlHttp.setRequestHeader("Content-length", params.length);
	   	xmlHttp.setRequestHeader("Connection", "close");
	    xmlHttp.send(params)
}

/*
function stateChanged() 
{ 
   if (xmlHttp.readyState==4)
   {
   
      if(xmlHttp.status == 200)
      { 
         document.getElementById("debug").innerHTML="result:"+xmlHttp.responseText
         document.getElementById("debug").innerHTML+="<br /><hr />"+xmlHttp
      } 
	  else
	  {
	  	alert("Probleem met het request!");alert(xmlHttp.status);
	  }
	  
   }
}   */

function GetXmlHttpObject()
{ 
    var objXMLHttp=null
    if (window.XMLHttpRequest)
    {
       objXMLHttp=new XMLHttpRequest()
    }
    else if (window.ActiveXObject)
    {
       objXMLHttp=new ActiveXObject("Microsoft.XMLHTTP")
    }
    return objXMLHttp
} 

function rate(gameID, grade){
	//alert('een '+grade+' voor spel met id: '+gameID+'!');
	callBack("http://www.ludomo.com/lib/process_ratings.php?commit=1&g_id="+gameID+"&grade="+grade, 'rating'+gameID);
	//document.getElementById('rating'+gameID).innerHTML = html;
	//document.getElementById('debug').innerHTML += "<br /><b>result:</b><br />"+html;
	
    //document.getElementById("debug").innerHTML+="<br />Request naar server gestuurd om een "+grade+" te geven aan het spel met ID="+gameID;
}

function fillstars(gameID, grade){
	grade = parseInt(grade);
	var div = document.getElementById('rating'+gameID);
	
	for(i=1; i<=grade; i++){
		document.getElementById('g'+gameID+'star'+i).src="Images/Star1.png";
	}
	for(i=1+grade; i<=5; i++){
		document.getElementById('g'+gameID+'star'+i).src="Images/Star0.png";
	}
}

function getFormFields(formid){
	var oForm = document.getElementById(formid);
	if(oForm != null){
		var paramsStr = "";
		for (var i=0;i<oForm.length;i++){
			if(paramsStr.length > 1)
				paramsStr += "&";
			
			paramsStr += oForm.elements[i].id+"="+oForm.elements[i].value;
		}
		return paramsStr;
	}else
		return "";
}

function submitForm(formid, url, targetid){
	//alert(url);
	callBackPOST(url,targetid, getFormFields(formid));
	window.location.hash=targetid;
}

function editMessage(type, msgid, gameid){
	//alert('Editing message of type='+type+' with id='+msgid);
	var divID = type+msgid;
	var formID = "f"+divID;
	var msgText = document.getElementById(divID+'txt').innerHTML;
	
	if(type == "comment"){
		document.getElementById(divID).innerHTML = "<form id='"+formID+"' action='javascript:submitForm(\""+formID+"\",\"http://www.ludomo.com/lib/comments.php?g_id="+gameid+"\",\""+divID+"\")' method='post'>" +
				"<input type='hidden' value='"+msgid+"' id='msgid' /><textarea id='bericht' rows='8' cols='70'>"+msgText+"</textarea>" +
				"<input type='submit' value='Edit Message' id='bEdit' /></form>";
	}
}

function removeMessage(type, id, gameid){
	if(type == "comment"){
		if(confirm('Are you sure you want to remove this message?'))
			callBackPOST("http://www.ludomo.com/lib/comments.php?g_id="+gameid,type+id, "removeMSG=1");
	}
}

function showComments(gameid, start, language){
	callBack("http://www.ludomo.com/lib/comments.php?g_id="+gameid+"&language="+language+"&start="+start,"comments");
}
