var attempts = 0;
var highAttempts = 0;
var highScoreList;
var resourceurl;
var gameCode;
var gameType;
var xmlhttp =  new XMLHttpRequest();
var xmlhttp2 =  new XMLHttpRequest();


function setResources(rurl, gc, gt) {
	resourceurl = rurl;
	gameCode = gc;
	gameType = gt;
}
function setGameType(gt) {
	gameType = gt;
}
function submitScore(score, scoreID, generic) {
	xmlhttp.open('POST', resourceurl + '/highscore.php', true);
	xmlhttp.onreadystatechange = handleSubmit;
	xmlhttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
	
	var scoreVal = setScore(score);
	var refid = setScore(scoreID);
	var pcode = 'action=submit&gamecode=' + scoreID + '&refid=' + refid + '&score=' + scoreVal + '&generic=' + generic + '&gametype=' + gameType;
	xmlhttp.send(pcode);		
}
function handleSubmit() {
    if (xmlhttp.readyState == 4) {
		if (xmlhttp.responseXML && xmlhttp.responseXML.getElementsByTagName('highscore')[0] && xmlhttp.responseXML.getElementsByTagName('highscore')[0].firstChild.data == '1') {
			setHighScore();
			/*var highscore = document.getElementById("highscore");
			var box = document.getElementById("box");
			highscore.style.top = findPosY(box) + 50;
			highscore.style.left = findPosX(box) + 250 - (highscore.width / 2);
			highscore.style.display = "block";
			highscore.style.zIndex = '3'; */
		} else {
	    	//alert("submitted, but not high score!");
		} 
    }
}

function getScore() {
	xmlhttp.open('POST', resourceurl + '/highscore.php', true);
	xmlhttp.onreadystatechange = handleChange;
	xmlhttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded'); 
	xmlhttp.send('action=getdata&gamecode=' + gameCode);
}
function handleChange() {
    if (xmlhttp.readyState == 4) {
		if (!xmlhttp.responseXML || !xmlhttp.responseXML.getElementsByTagName('xmldata')[0]|| !xmlhttp.responseXML.getElementsByTagName('xmldata')[0].firstChild) { 
			attempts++;
			if (attempts < 5) {
				window.setTimeout('getScore()', 500);
			} else {
				alert("Oops! We're having troubles with the server, and your high scores will not be recorded.  Try again in a few minutes if you're dying to get on that high score list...");
			} 
			return;
		}
		var tmpdata = xmlhttp.responseXML.getElementsByTagName('xmldata')[0].firstChild.data;
		eval(tmpdata);
		attempts = 0;
    }
}
function getHighScores() {
	xmlhttp2.open('POST', resourceurl + '/highscore.php', true);
	xmlhttp2.onreadystatechange = handleHighScores;
	xmlhttp2.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded'); 
	xmlhttp2.send('action=highscores&gametype=' + gameType);
}
function handleHighScores() {
    if (xmlhttp2.readyState == 4) {
		if (!xmlhttp2.responseXML || !xmlhttp2.responseXML.getElementsByTagName('xmldata')[0] || !xmlhttp2.responseXML.getElementsByTagName('xmldata')[0].firstChild || xmlhttp2.responseXML.getElementsByTagName('xmldata')[0].firstChild.data != gameType) { 
			highAttempts++;
			if (highAttempts < 5) {
				window.setTimeout('getHighScores()', 500);
			} else {
				alert("Oops! Can't seem to get the high scores for this game... try again soon!");
			} 
			return;
		}
		var scoreList = xmlhttp2.responseXML.getElementsByTagName('userscore');
		setScoreList();
		for (var i = 0; i < scoreList.length && i < 10; i++) {
			var scoreItem = scoreList[i];
			var scoreVl = scoreItem.getElementsByTagName('score')[0].firstChild.data;
			var userid = scoreItem.getElementsByTagName('userid')[0].firstChild.data;
			var username = scoreItem.getElementsByTagName('username')[0].firstChild.data;
			setScoreItem(i, userid, username, scoreVl);
		}
		highAttempts = 0;
		displayHighScores(highScoreList);
    }
}
function setScoreList() {
	highScoreList = new Array(10);
}
function setScoreItem(idx, userid, username, score) {
	var scoreItem = new Array(userid, username, score);
	highScoreList[idx] = scoreItem;
}