function createRequestObject() {
    var ro;
    var browser = navigator.appName;
    if(browser == "Microsoft Internet Explorer"){
        ro = new ActiveXObject("Microsoft.XMLHTTP");
    }else{
        ro = new XMLHttpRequest();
    }
    return ro;
}
function getXMLFromText(response)
{
	if (window.ActiveXObject)
    {
            xmlDoc=new ActiveXObject("Microsoft.XMLDOM");
            xmlDoc.async="false";
            xmlDoc.loadXML(response);
			return xmlDoc; 
    }
       //for Firefox
     else
    {
           //create the xml docuemnt object in firefox
           var parser=new DOMParser();
           xmlDoc=parser.parseFromString(response,"text/xml");
			return xmlDoc; 
     }
}
var getStreamsXmlDoc;
function getStreamsValue(nodeName,i){
  	  if(i == null)
		i=0;
	  if(getStreamsXmlDoc.getElementsByTagName(nodeName)[i] != null)
	  		if(getStreamsXmlDoc.getElementsByTagName(nodeName)[i].firstChild != null)
				return getStreamsXmlDoc.getElementsByTagName(nodeName)[i].firstChild.nodeValue != "null"?getStreamsXmlDoc.getElementsByTagName(nodeName)[i].firstChild.nodeValue : "";
            else
				return '';
				
      else
			return ''
}
function getStreamsSize(nodeName){
	 if(getStreamsXmlDoc.getElementsByTagName(nodeName) != null) 
			return getStreamsXmlDoc.getElementsByTagName(nodeName).length;
     else
			return 0

}
var http_unfinished_games_req = createRequestObject();
function getUnfinishedGames()
{
	var d = new Date();
	try
	{
		document.getElementById("data").innerHTML="<font color='Brown'>Request in progress ...</font>";		
	}
	catch (e)
	{
	}
	http_unfinished_games_req.open("GET",context+"/account/getUnfinishedGamesAction.do?time="+d.getTime(),true);
    http_unfinished_games_req.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	http_unfinished_games_req.setRequestHeader("Content-length", 0);
    http_unfinished_games_req.setRequestHeader("Connection", "close");
    http_unfinished_games_req.send(null);
    http_unfinished_games_req.onreadystatechange = handleResponse;
}

function handleResponse()
{
	if(http_unfinished_games_req.readyState == 4)
	{			
		if(http_unfinished_games_req.status == 200)
		{
			try
			{
				var response		=	http_unfinished_games_req.responseText;
				if(response.indexOf("SessionId is invalid") != -1 )
				{
		 			if(location.href.indexOf("unfinished_games.thtml") != -1 )
					{
						alert("Your session has been expired , please login in to the site ");
						location.href = "../home/index.thtml?confirm=false";
					}
							
				}
				getStreamsXmlDoc	=	getXMLFromText(response);
				if(getStreamsSize('gameVariantName') > 0)
				{
					try
					{
						//this is to display link in the lobby page.
						document.getElementById("unfinishedgames").innerHTML="<div style='width:100%'><center>You have unfinished minigames to complete, please <a href='"+built_url+"/bingo/lobby/unfinished_games.thtml'>click here</a>  to finish these games.</center></div><br/>";
						//document.getElementById("accountPanel").style.height="130px" ;
					}
					catch (e)
					{
					}
					var strresponse	=	"<table><tr><td><h3>These are games that were closed before they completed spinning. In order to complete a game please:<br><br>1) Click the “Resume” button for each game.<br>2) Close the game once the spin has completed.<br>3) Launch the next game if applicable.<br>4) Any winnings will be credited to your account automatically.</h3> </td></tr></table><table cellspacing='0' border='2' class='printtbl' width='100%'><tr><td class='printtblhd' width='25%'>Game Name</td><td class='printtblhd' width='25%'>Stake</td><td class='printtblhd' width='25%'>Game Date</td><td class='printtblhd' width='25%'>Play</td></tr>";
					for(i=0;i<getStreamsSize('gameVariantName');i++)
					{
						var gameVariantName	=  getStreamsValue('gameVariantName',i);	
						var pendingBetAmount=  getStreamsValue('pendingBetAmount',i);
						var gameSessionId	=getStreamsValue('gameSessionId',i);
						var unfinishedGameDate	=	getStreamsValue('unfinishedGameDate',i);
						var deploymentUrl	=	getStreamsValue('deploymentUrl',i);
						var gcName	=	  getStreamsValue('gcName',i);

						strresponse	=	strresponse+"<tr class=printtblrow01>";
						strresponse	=	strresponse+"<td align=center width=25%>"+gameVariantName+"</td><td  align=center width=25%>"+pendingBetAmount+"</td>";
						strresponse	=	strresponse+"</td><td  align=center width=25%>"+unfinishedGameDate+"</td><td  align=center width=25%>&nbsp;&nbsp;&nbsp;<a href=\"javascript:openUnfinishedGame('"+gameVariantName+"','"+deploymentUrl+"','"+gcName+"','"+gameSessionId+"');\"><img src=../images/resume.gif border=0> </img></a></td></tr>";
					}
					strresponse	=	strresponse+"</table>";				
					try
					{
						document.getElementById("data").innerHTML=strresponse;						
					}
					catch (e)
					{
					}

				}
				else
				{
					try
					{
						document.getElementById("data").innerHTML="<font color='Brown'>No games found.</font>";					
					}
					catch (e)
					{
					}
				}
				
			}
			catch(err)
			  {
			  txt="There was an error on this page.\n\n";
			  txt+="Error description: " + err.description + "\n\n";
			  txt+="Click OK to continue.\n\n";
			  alert(txt);
			  }
					
		}
		else
		{	
			if(location.href.indexOf("unfinished_games.thtml") != -1 )
				alert("System Failure , Please try again later.");
		} 
	}
}
getUnfinishedGames();
