function SetDivText(divElement, value) {
	if (document.all) {
		divElement.innerText = value;
	}
	else {
		divElement.childNodes[0].nodeValue = value;
	}
}

function GetDivText(divElement) {
	if (document.all) {
		return divElement.innerText;
	}
	else {
		return divElement.childNodes[0].nodeValue;
	}
	return "";
}

function initialisePortal(displayGameID, imageName)
{	
	self.name = 'Portal';
	if (displayGameID != "")
	{
		
		link_onclick(displayGameID);
		changeImage(imageName, displayGameID)
	}
}
	
function link_onclick(gameDisplayId)
{   
	var gameHideObj = new getObj("gameHideId");
	var gameDispObj = new getObj(gameDisplayId);
	var controlID = GetDivText(gameDispObj.obj);

	if (GetDivText(gameHideObj.obj) != 0)
	{		
		showObj(GetDivText(gameHideObj.obj), false);
	}
	showObj(controlID + "_" + gameDisplayId, true);
	
	SetDivText(gameHideObj.obj, controlID + "_" + gameDisplayId);
} 

function showObj(objName, visible)
{
	var x = new getObj(objName);

	if (visible)
	{	
		x.style.display = "block";
	}
	else
	{
		x.style.display = "none";
	}
} 

function changeImage(imageName, gameID)
{
	var imgObj = new getObj("Image" + gameID);
	var controlID = GetDivText(imgObj.obj);
	var srcObj = new getObj("src");
	var storeControlObj = new getObj("storeControlID");
	var newImgObj = new getObj(controlID + "_gameImage");

	if (GetDivText(storeControlObj.obj) != "")
	{
		var x = new getObj(GetDivText(storeControlObj.obj) + "_gameImage");	
		x.obj.src = GetDivText(srcObj.obj);
	}
	SetDivText(srcObj.obj, newImgObj.obj.src);	
	newImgObj.obj.src = "Images/" + imageName;	
	SetDivText(storeControlObj.obj, controlID);
}



function deposit_onclick()
{
	alert("Deposit!")
}

/********************************************************
		Game Window Functions & Variables
********************************************************/

// Theses are references to the game child window and the time out function
var gameWin, timeOutForGame;

function showGame(gameId, hostURL, mode, height, width, sid,  aPath)
{
	var x = new getObjWithoutStyle("CurrentOpenGameId");
	if (x.obj.value == "-1") {
		x.obj.value = "" + gameId + "";
		gameWin = openGameWindow(gameId, hostURL, mode, height, width, sid,  aPath);		
	}
	else {
		alert("Please close your open game window or wait for your balance to be refreshed.\nBefore attempting to open a new game.")
	}
	
	//timeOutForGame = window.setTimeout("checkGameWindow(gameWin, " + gameId + ")", 1000);
	return false;
}

function showGameNew(gameId, hostURL, mode, height, width, sid, aPath) {
    var x = new getObjWithoutStyle(CurrentOpenGameId);
    var clevel = document.getElementById('IdLevel').value;
    
    if (mode == "") {
        mode = document.getElementById('clgin').value;
    }
    sid = document.getElementById('ens').value;
    
    if (x.obj.value == "-1") {
        if (mode == "R" && sid != "") {
            x.obj.value = "" + gameId + "";
        }

        if (sid != "") {
            if (mode == "R" && clevel != 85 && clevel != 84) {
                gameWin = openGameWindow(gameId, hostURL, mode, height, width, sid, aPath);
            }
            else { 
                   alert('[#Sorry! You are not allowed to play for real.|61559#]');
            }
             
         }
         else 
        {
            if (mode == "F") {
                gameWin = openGameWindow(gameId, hostURL, mode, height, width, sid, aPath);
            }
            else {
                alert("Please login to access to play in real mode");
            }
        }
    }
    else {
        alert("Please close your open game window or wait for your balance to be refreshed.\nBefore attempting to open a new game.")
    }

    //timeOutForGame = window.setTimeout("checkGameWindow(gameWin, " + gameId + ")", 1000);
    return false;
}


function openGameWindow(gameId, hostURL, mode, height, width, sid, aPath)
{
	var myLeft = Math.round((screen.availWidth - width)/2);
	var myTop = Math.round((screen.availHeight - height)/2);
	var features = 'scrollbars=no, resizable=0';
	features+= ',left=' + myLeft + ',top=' + myTop + ',width=' + width + ',height=' + height;
	
	//"Hosts/GameFrame.aspx?gameUrl=" + hostURL + "&gameId=" + gameId + "&mode=" + mode;
	//newUrl = "Hosts/" + hostURL + "?gameId=" + gameId + "&mode=" + mode;
	aPath = "http://" + window.location.host + "/GLOBET_GAMES_HOST_NET";
	newUrl = aPath + "/Hosts/" + "GameFrame.aspx?gameUrl=" + aPath + "/Hosts/" + hostURL + "&gameId=" + gameId + "&mode=" + mode + "&csid=" + sid;
	//alert(features);
	URL= aPath + "/Hosts/" + hostURL;
	URL+="?gameId=";
	URL+=gameId;
	URL+="&mode=";
	URL+=mode;
	
	return window.open(newUrl,"GameHost",features); 
}
 
function checkGameWindow(gameWin, gameId)
{	
	if (gameWin.closed)
	{
		gameWin.close();
		var newURL;
		if(window.location.href.indexOf("Closed") > 0)
		{
			var p = window.location.href.indexOf("Closed");
			var orgUrl = window.location.href.substring(0,p);
			
			
			if(orgUrl.substring(-1) == '?')
				newURL = orgUrl + "&Closed=" + gameId;
			else
				newURL = orgUrl + "Closed=" + gameId;
		}
		else
		{
			//newURL = window.location.href + '?Closed=' + gameId;
			
			if(window.location.href.search.length > 1)
			{
				
				newURL = window.location.href + '&Closed=' + gameId;
			}
			else
			{
				
				newURL = window.location.href + '&Closed=' + gameId;
			}
		}
		
		window.clearTimeout(timeOutForGame);
		window.location.href = newURL;					
	}
   else
   {
		timeOutForGame = window.setTimeout("checkGameWindow(gameWin, " + gameId + ")", 1000);
   }
}

//	 Misc Functions 
function getObj(name)
{
	if (document.getElementById)
	{
		this.obj = document.getElementById(name);
		this.style = document.getElementById(name).style;
	}
	else if (document.all)
	{
		this.obj = document.all[name];
		this.style = document.all[name].style;
	}
	else if (document.layers)
	{
	   	this.obj = document.layers[name];
	   	this.style = document.layers[name];
	}
}

function getObjWithoutStyle(name)
{
	if (document.getElementById)
	{
		this.obj = document.getElementById(name);
	}
	else if (document.all)
	{
		this.obj = document.all[name];
	}
	else if (document.layers)
	{
	   	this.obj = document.layers[name];
	}
}

function ResetCurrentOpenWindowId() {
    if (resetCurrentOpenWindowId) {
        var x = new getObjWithoutStyle('CurrentOpenGameId');
		x.obj.value = '-1';
	}
}
function ResetCurrentOpenWindowIdNew() {
    if (resetCurrentOpenWindowId) {
        var x = new getObjWithoutStyle(CurrentOpenGameId);
        x.obj.value = '-1';
    }
}
function MyFunction(p1) {
   // alert("You have clicked on image : " + p1);
    
    if (p1 != null && p1 != "undefined") {
        $.post("../HttpHandlers/HelpPage.ashx", { gid: p1 }, function(data) {
        $('#aboutgamecontent').empty().append(data);
        });
    }
    //  return false;
}