//******************************************************
// Released under the OSI MIT license http://www.opensource.org/licenses/mit-license.php
//Copyright (c) 2005 Mike Surel
//Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
//documentation files (the "Software"), to deal in the Software without restriction, including without limitation 
//the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and 
//to permit persons to whom the Software is furnished to do so, subject to the following conditions:
//The above copyright notice and this permission notice shall be included in all copies or substantial portions of
//the Software.
//THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO 
//THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 
//AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF 
//CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
// IN THE SOFTWARE.
// Sends the request to the page specified in the object.
// We use a closure for the event handler. If the variable
// isn't global it loses scope so the use of a closure seemed
// like a more encapsulated and efficient way to do this.
//******************************************************
function jpAjax_getPlaylist() 
{
    // Class variables Page and Request need to be set in order to do the
    // request.
    this._httpRequestObject.open('POST', this.Page + this.Request);
    // The following two variables are used to the closure
    // can see the objects
    var myHTTPObject = this._httpRequestObject;
    var myTarget = this.TargetElement;
    // Event handler for the page being loaded. Closure
    // keeps it local
    this._httpRequestObject.onreadystatechange = function()
        {
            if (myHTTPObject.readyState == 4)
            {
				document.getElementById("playlist").innerHTML = myHTTPObject.responseText;
                document.getElementById('playlist').style.display = "block";
				dragsort.makeListSortable(document.getElementById("playlistItems1"),verticalOnly, saveOrder);
            }
        };
    // Send the request to the web server
    this._httpRequestObject.send(null);
}
function jpAjax_addToPlaylist(gameID) 
{
    // Class variables Page and Request need to be set in order to do the
    // request.
    this._httpRequestObject.open('post', this.Page + this.Request);
    // The following two variables are used to the closure
    // can see the objects
    var myHTTPObject = this._httpRequestObject;
    var myTarget = this.TargetElement;
    // Event handler for the page being loaded. Closure
    // keeps it local
    this._httpRequestObject.onreadystatechange = function()
        {
            if (myHTTPObject.readyState == 4)
            {
            	showPlaylist(gameID);
            	playlistSaveButton(gameID);
            }
        };
    // Send the request to the web server
    this._httpRequestObject.send(null);
}
function jpAjax_addToPlaylistGame() 
{
    // Class variables Page and Request need to be set in order to do the
    // request.
    this._httpRequestObject.open('post', this.Page + this.Request);
    // The following two variables are used to the closure
    // can see the objects
    var myHTTPObject = this._httpRequestObject;
    var myTarget = this.TargetElement;
    // Event handler for the page being loaded. Closure
    // keeps it local
    this._httpRequestObject.onreadystatechange = function()
        {
            if (myHTTPObject.readyState == 4)
            {
				showGamePlaylist();
				playlistSaveButton(gameID);
				document.getElementById("addPlaylistButton").style.display = "none";			
            }
        };
    // Send the request to the web server
    this._httpRequestObject.send(null);
}
function jpAjax_updatePlaylist() 
{
    // Class variables Page and Request need to be set in order to do the
    // request.
    this._httpRequestObject.open('post', this.Page + this.Request);
    // The following two variables are used to the closure
    // can see the objects
    var myHTTPObject = this._httpRequestObject;
    var myTarget = this.TargetElement;
    // Event handler for the page being loaded. Closure
    // keeps it local
    this._httpRequestObject.onreadystatechange = function()
        {
            if (myHTTPObject.readyState == 4)
            {
            }
        };
    // Send the request to the web server
    this._httpRequestObject.send(null);
}
function jpAjax_playlistSaveButton() 
{
    // Class variables Page and Request need to be set in order to do the
    // request.
    this._httpRequestObject.open('post', this.Page + this.Request);
    // The following two variables are used to the closure
    // can see the objects
    var myHTTPObject = this._httpRequestObject;
    var myTarget = this.TargetElement;
    // Event handler for the page being loaded. Closure
    // keeps it local
    this._httpRequestObject.onreadystatechange = function()
        {
            if (myHTTPObject.readyState == 4)
            {
				if(parseInt(myHTTPObject.responseText) < 2)
				{
					if(document.getElementById(myTarget))
						document.getElementById(myTarget).style.display = "none";
				}
				else if(parseInt(myHTTPObject.responseText) >1)
				{
					if(document.getElementById(myTarget))
						document.getElementById(myTarget).style.display = "block";					
				}
            }
        };
    // Send the request to the web server
    this._httpRequestObject.send(null);
}
function jpAjax_deletePlaylistItem(gameID,levelID) 
{
    // Class variables Page and Request need to be set in order to do the
    // request.
    this._httpRequestObject.open('post', this.Page + this.Request);
    // The following two variables are used to the closure
    // can see the objects
    var myHTTPObject = this._httpRequestObject;
    var myTarget = this.TargetElement;
    // Event handler for the page being loaded. Closure
    // keeps it local
    this._httpRequestObject.onreadystatechange = function()
        {
            if (myHTTPObject.readyState == 4)
            {
				playlistSaveButton(gameID);
            }
        };
    // Send the request to the web server
    this._httpRequestObject.send(null);
}
function jpAjax_addRating() 
{
    // Class variables Page and Request need to be set in order to do the
    // request.
    this._httpRequestObject.open('get', this.Page + this.Request);
    // The following two variables are used to the closure
    // can see the objects
    var myHTTPObject = this._httpRequestObject;
    var myTarget = this.TargetElement;
    // Event handler for the page being loaded. Closure
    // keeps it local
    this._httpRequestObject.onreadystatechange = function()
        {
            if (myHTTPObject.readyState == 4)
            {
            	var myObject = eval('(' + myHTTPObject.responseText + ')');
				document.getElementById('r_votes').innerHTML = myObject[0].votes;
				document.getElementById('starUser0').innerHTML = parseInt(myObject[0].rating) + "%"; 
				document.getElementById('starCur0').style.width = Math.round(myObject[0].rating*84/100)+'px';
				document.getElementById('starCur0').title = Math.round(myObject[0].rating*84/100);
            }
        };
    // Send the request to the web server
    this._httpRequestObject.send(null);
}
function jpAjax_addDifficulty() 
{
    // Class variables Page and Request need to be set in order to do the
    // request.
    this._httpRequestObject.open('get', this.Page + this.Request);
    // The following two variables are used to the closure
    // can see the objects
    var myHTTPObject = this._httpRequestObject;
    var myTarget = this.TargetElement;
    // Event handler for the page being loaded. Closure
    // keeps it local
    this._httpRequestObject.onreadystatechange = function()
        {
            if (myHTTPObject.readyState == 4)
            {
            	var myObject = eval('(' + myHTTPObject.responseText + ')');
				document.getElementById('d_votes').innerHTML = myObject[0].votes;
				document.getElementById('starUser1').innerHTML = parseInt(myObject[0].difficulty) + "%"; 
				document.getElementById('starCur1').style.width = Math.round(myObject[0].difficulty*84/100)+'px';
				document.getElementById('starCur1').title = Math.round(myObject[0].difficulty*84/100);            	
            }
        };
    // Send the request to the web server
    this._httpRequestObject.send(null);
}
function jpAjax_setLevelBoxes() 
{
    // Class variables Page and Request need to be set in order to do the
    // request.
    this._httpRequestObject.open('POST', this.Page + this.Request);
    // The following two variables are used to the closure
    // can see the objects
    var myHTTPObject = this._httpRequestObject;
    var myTarget = this.TargetElement;
    // Event handler for the page being loaded. Closure
    // keeps it local
    this._httpRequestObject.onreadystatechange = function()
        {
            if (myHTTPObject.readyState == 4)
            {
				document.getElementById(myTarget).innerHTML = myHTTPObject.responseText;
            }
        };
    // Send the request to the web server
    this._httpRequestObject.send(null);
}
function jpAjax_showGamePlaylist() 
{
    // Class variables Page and Request need to be set in order to do the
    // request.
    this._httpRequestObject.open('POST', this.Page + this.Request);
    // The following two variables are used to the closure
    // can see the objects
    var myHTTPObject = this._httpRequestObject;
    var myTarget = this.TargetElement;
    // Event handler for the page being loaded. Closure
    // keeps it local
    this._httpRequestObject.onreadystatechange = function()
        {
            if (myHTTPObject.readyState == 4)
            {
				document.getElementById(myTarget).innerHTML = myHTTPObject.responseText;
				dragsort.makeListSortable(document.getElementById("playlistItems1"),verticalOnly, saveOrder);
            }
        };
    // Send the request to the web server
    this._httpRequestObject.send(null);
}
function jpAjax_isLevelAddedToPlaylist() 
{
    // Class variables Page and Request need to be set in order to do the
    // request.
    this._httpRequestObject.open('post', this.Page + this.Request);
    // The following two variables are used to the closure
    // can see the objects
    var myHTTPObject = this._httpRequestObject;
    var myTarget = this.TargetElement;
    // Event handler for the page being loaded. Closure
    // keeps it local
    this._httpRequestObject.onreadystatechange = function()
        {
            if (myHTTPObject.readyState == 4)
            {
				if(parseInt(myHTTPObject.responseText) == 0)
				{
					if(document.getElementById(myTarget))
						document.getElementById(myTarget).style.display = "block";
				}
            }
        };
    // Send the request to the web server
    this._httpRequestObject.send(null);
}
function jpAjax_getLevels() 
{
    // Class variables Page and Request need to be set in order to do the
    // request.
    this._httpRequestObject.open('post', this.Page + this.Request);
    // The following two variables are used to the closure
    // can see the objects
    var myHTTPObject = this._httpRequestObject;
    var myTarget = this.TargetElement;
    // Event handler for the page being loaded. Closure
    // keeps it local
    this._httpRequestObject.onreadystatechange = function()
        {
            if (myHTTPObject.readyState == 4)
            {
				if(myHTTPObject.responseText.length > 0)
				{ 
					var myObject = eval('(' + myHTTPObject.responseText + ')');
					showLevels(myObject);
				}
				else
					clearLevels();
            }
        };
    // Send the request to the web server
    this._httpRequestObject.send(null);
}
function jpAjax_update() 
{
    // Class variables Page and Request need to be set in order to do the
    // request.
    this._httpRequestObject.open('post', this.Page + this.Request);
    // The following two variables are used to the closure
    // can see the objects
    var myHTTPObject = this._httpRequestObject;
    var myTarget = this.TargetElement;
    // Event handler for the page being loaded. Closure
    // keeps it local
    this._httpRequestObject.onreadystatechange = function()
        {
            if (myHTTPObject.readyState == 4)
            {
            }
        };
    // Send the request to the web server
    this._httpRequestObject.send(null);
}
function jpAjax_getLevelAuthor() 
{
    // Class variables Page and Request need to be set in order to do the
    // request.
    this._httpRequestObject.open('post', this.Page + this.Request);
    // The following two variables are used to the closure
    // can see the objects
    var myHTTPObject = this._httpRequestObject;
    var myTarget = this.TargetElement;
    // Event handler for the page being loaded. Closure
    // keeps it local
    this._httpRequestObject.onreadystatechange = function()
        {
            if (myHTTPObject.readyState == 4)
            {
				setLevelCreator(eval('(' + myHTTPObject.responseText + ')'));
            }
        };
    // Send the request to the web server
    this._httpRequestObject.send(null);
}
function jpAjax_getHighscores() 
{ 
    // Class variables Page and Request need to be set in order to do the
    // request.
    this._httpRequestObject.open('post', this.Page + this.Request);
    // The following two variables are used to the closure
    // can see the objects
    var myHTTPObject = this._httpRequestObject;
    var myTarget = this.TargetElement;
    // Event handler for the page being loaded. Closure
    // keeps it local
    this._httpRequestObject.onreadystatechange = function()
        {
            if (myHTTPObject.readyState == 4)
            {
            	//alert('test' + myHTTPObject.responseText);
				if(myHTTPObject.responseText.length == 0)
					returnObject = null;
				else
					returnObject = myHTTPObject.responseText;
				highscoreObject = eval('(' + returnObject + ')');
				//point = 0;
				setHighscores();
            }
        };
    // Send the request to the web server
    this._httpRequestObject.send(null);
}
function jpAjax_getOwnHighscore() 
{
    // Class variables Page and Request need to be set in order to do the
    // request.
    this._httpRequestObject.open('post', this.Page + this.Request);
    // The following two variables are used to the closure
    // can see the objects
    var myHTTPObject = this._httpRequestObject;
    var myTarget = this.TargetElement;
    // Event handler for the page being loaded. Closure
    // keeps it local
    this._httpRequestObject.onreadystatechange = function()
        {
            if (myHTTPObject.readyState == 4)
            {
            	if(myHTTPObject.responseText != null && myHTTPObject.responseText != "")
					setOwnHighscore(eval('(' + myHTTPObject.responseText + ')'));
				else
					setOwnHighscore(new Object());
            }
        };
    // Send the request to the web server
    this._httpRequestObject.send(null);
}
function jpAjax_getOnlineHighScores() 
{
    // Class variables Page and Request need to be set in order to do the
    // request.
    this._httpRequestObject.open('post', this.Page + this.Request);
    // The following two variables are used to the closure
    // can see the objects
    var myHTTPObject = this._httpRequestObject;
    var myTarget = this.TargetElement;
    // Event handler for the page being loaded. Closure
    // keeps it local
    this._httpRequestObject.onreadystatechange = function()
        {
            if (myHTTPObject.readyState == 4)
            {
				setOnlineHighscore(eval('(' + myHTTPObject.responseText + ')'));
            }
        };
    // Send the request to the web server
    this._httpRequestObject.send(null);
}
function jpAjax_getMobileHighScores() 
{
    // Class variables Page and Request need to be set in order to do the
    // request.
    this._httpRequestObject.open('post', this.Page + this.Request);
    // The following two variables are used to the closure
    // can see the objects
    var myHTTPObject = this._httpRequestObject;
    var myTarget = this.TargetElement;
    // Event handler for the page being loaded. Closure
    // keeps it local
    this._httpRequestObject.onreadystatechange = function()
        {
            if (myHTTPObject.readyState == 4)
            {
				setMobileHighscore(eval('(' + myHTTPObject.responseText + ')'));
            }
        };
    // Send the request to the web server
    this._httpRequestObject.send(null);
}
function jpAjax_getILikeLevelCount() {
	// Class variables Page and Request need to be set in order to do the
  // request.
  this._httpRequestObject.open('post', this.Page + this.Request);
  // The following two variables are used to the closure
  // can see the objects
  var myHTTPObject = this._httpRequestObject;
  var myTarget = this.TargetElement;
  // Event handler for the page being loaded. Closure
  // keeps it local
  this._httpRequestObject.onreadystatechange = function()
      {
          if (myHTTPObject.readyState == 4)
          {
          	alert(myHTTPObject.responseText);
			if(myHTTPObject.responseText.length == 0)
				returnObject = null;
			else
				returnObject = myHTTPObject.responseText;
			  	ILikeLevelUserStatusObject = eval('(' + returnObject + ')');
			  	setILikeLevelCount();
			//point = 0;
				//iLikeLevelSection();
          }
      };
  // Send the request to the web server
  this._httpRequestObject.send(null);
}
function jpAjax_getILikeGameCount(func) {
	// Class variables Page and Request need to be set in order to do the
  // request.
  this._httpRequestObject.open('post', this.Page + this.Request);
  // The following two variables are used to the closure
  // can see the objects
  var myHTTPObject = this._httpRequestObject;
  var myTarget = this.TargetElement;
  // Event handler for the page being loaded. Closure
  // keeps it local
  this._httpRequestObject.onreadystatechange = function()
      {
          if (myHTTPObject.readyState == 4)
          {
          	alert(myHTTPObject.responseText);
			if(myHTTPObject.responseText.length == 0)
				returnObject = null;
			else
				returnObject = myHTTPObject.responseText;
			  	ILikeLevelUserStatusObject = eval('(' + returnObject + ')');
			  	func.call();
			//point = 0;
				//iLikeLevelSection();
          }
      };
  // Send the request to the web server
  this._httpRequestObject.send(null);
}
function jpAjax_genericCall(func) {
  this._httpRequestObject.open("POST", this.Page);
  var myHTTPObject = this._httpRequestObject;
  var myTarget = this.TargetElement;
  this._httpRequestObject.onreadystatechange = function()
      {
          if (myHTTPObject.readyState == 4)
          {
			if(myHTTPObject.responseText.length == 0)
				returnObject = null;
			else if(func != null)
			  	func.call(null, eval('(' +  myHTTPObject.responseText + ')'));
          }
      };
	this._httpRequestObject.setRequestHeader('Content-Type',
'application/x-www-form-urlencoded');
  this._httpRequestObject.send(this.Request);
}
function jpAjax_getILikeLevelUserStatus() {
	// Class variables Page and Request need to be set in order to do the
  // request.
  this._httpRequestObject.open('post', this.Page + this.Request);
  // The following two variables are used to the closure
  // can see the objects
  var myHTTPObject = this._httpRequestObject;
  var myTarget = this.TargetElement;
  // Event handler for the page being loaded. Closure
  // keeps it local
  this._httpRequestObject.onreadystatechange = function()
      {
          if (myHTTPObject.readyState == 4)
          {
          	alert(myHTTPObject.responseText);
			if(myHTTPObject.responseText.length == 0)
				returnObject = null;
			else
				returnObject = myHTTPObject.responseText;
			  	ILikeLevelUserStatusObject = eval('(' + returnObject + ')');
			  	setILikeLevelStatus();
			//point = 0;
				//iLikeLevelSection();
          }
      };
  // Send the request to the web server
  this._httpRequestObject.send(null);
}
//******************************************************
// Our class for doing AJAX type requests. The constructure
// requires that you specify an element and a page on the
// server to send the request to. The request itself
// is optional.
// Public properties that can be set are:
// TargetElement    -   The HTML element in the page to update
// Page             -   Web page to call for server side processing
// Request          -   Additional information to send to the Page
//                      Often URL parameters. Handling these are the
//                      responsibility of the server
//******************************************************
function JpAjax(targetElement, page, request)
{
    // Make this null if the required arguments are not
    // specified.
    if (arguments.length < 2)
    {
        alert('You need to specify an HTML Element and a page');
        return;
    }
    // Initialize our properties
    this.TargetElement = targetElement;
    this.Page = page;
    if (request == null)
    {
        this.Request = "";
    }
    else
    {
        this.Request = request;
    }
    // Handle IE and non-IE browsers
    if(navigator.appName == "Microsoft Internet Explorer")
    {
        this._httpRequestObject = new ActiveXObject("Microsoft.XMLHTTP");
    }
    else
    {
        this._httpRequestObject = new XMLHttpRequest();
    }
}
// Expose the SendRequest function for client code to call.
JpAjax.prototype.getPlaylist = jpAjax_getPlaylist
JpAjax.prototype.addToPlaylist = jpAjax_addToPlaylist
JpAjax.prototype.updatePlaylist = jpAjax_updatePlaylist
JpAjax.prototype.deletePlaylistItem = jpAjax_deletePlaylistItem
JpAjax.prototype.addRating = jpAjax_addRating
JpAjax.prototype.addDifficulty = jpAjax_addDifficulty
JpAjax.prototype.setLevelBoxes = jpAjax_setLevelBoxes
JpAjax.prototype.addToPlaylistGame = jpAjax_addToPlaylistGame
JpAjax.prototype.showGamePlaylist = jpAjax_showGamePlaylist
JpAjax.prototype.playlistSaveButton = jpAjax_playlistSaveButton
JpAjax.prototype.isLevelAddedToPlaylist = jpAjax_isLevelAddedToPlaylist
JpAjax.prototype.getLevels = jpAjax_getLevels
JpAjax.prototype.update = jpAjax_update
JpAjax.prototype.getLevelAuthor = jpAjax_getLevelAuthor
JpAjax.prototype.getHighscores = jpAjax_getHighscores
JpAjax.prototype.getOwnHighscore = jpAjax_getOwnHighscore
JpAjax.prototype.getOnlineHighScores = jpAjax_getOnlineHighScores
JpAjax.prototype.getMobileHighScores = jpAjax_getMobileHighScores
JpAjax.prototype.getILikeLevelUserStatus = jpAjax_getILikeLevelUserStatus
JpAjax.prototype.getILikeLevelCount = jpAjax_getILikeLevelCount
JpAjax.prototype.getILikeGameCount = jpAjax_getILikeGameCount
JpAjax.prototype.genericCall = jpAjax_genericCall