//
//  THIS FILE IS SERVED FROM COMMONAPACHE SERVER.
//
window.name = "homewindow";
if(!gameWin)
	var gameWin= null;

function openGamePage(gameType) {
		openGamePage(gameType,'spbd')
}//openGamePage

function openGamePage(gameType,provider) 
	{
		//alert("game: "+gameType+", provider: "+provider);
		if (gameWin != null && !gameWin.closed)
		{
			gameWin.focus();
			gameWin.alert("The game window is already open");
		}	
		else 
		{
			openGamewindow(gameType,provider);		
		}
		try{cambio('menuSidegames','menuHelp');}catch(e){}
	}
//openGamePage

function openGamewindow(gameType,provider)
{
	var w = window.screen.availWidth;
	var h = window.screen.availHeight;
	if(h > 800) {
		h=750;
	}//for higher resolutions
	if(w > 800) {
		w = w-25;
	} else {
		w = w-10;
	}//for higher resolutions
	//var windowDimensionsString  =provider == 'obs'?",width=767,height=567" : ",width=750,height=550";
	//var windowDimensionsString  = ",width=750,height=550";
	var windowDimensionsString  =provider == 'obs'?",width=750,height=550" : ",width=800,height=600";
	var windowScreenLocation    = ",left="+(w-750)/2+",top="+(h-550)/2;
	var features = 	windowDimensionsString +
					windowScreenLocation + 
					",scrollbars=no" +
					",toolbar=no" +
					",menubar=no,resizable=yes,fullscreen=7";
	//modified on 05-nov-2004
		if(location.href.indexOf("index.html") != -1)
		{
			window.name	=  "slotgames";
			location.href="/commonsys/play/game_page.jsp?gameType="+gameType;
			return;
		}
	gameWin=window.open("/commonsys/play/game_page.jsp?gameType="+gameType+"&provider="+provider, "slotgames", features);
	//gameWin=window.open("../../play/game_page.jsp?gameType="+gameType, "slotgames", 'toolbar=0,scrollbars=0,location=0,statusbar=0,menubar=0,resizable=0,width=760,height=630,left = 260,top = 212');
}

function openWin(winName,stockroot_url)
{
	allowResize = "yes";
	// Start at top left
	LeftPosition = 100;
	TopPosition = 0;
	// Go almost full-screen for Flash Bingo
	myHeight = (screen.height) ? (screen.height-145) : 455;
	myWidth = myHeight * 1.3475;
	// Set the left and top window position to zero if logic above has produced a value less than zero
	if (LeftPosition < 0) {	LeftPosition = 0;}
	if (TopPosition < 0) {	TopPosition = 0;}
	// Configure other window properties
	winProps = "left="+LeftPosition+",top="+TopPosition+",width="+myWidth+",height="+myHeight+",location=0,scrollbars=0,menubar=0,toolbar=0,resizable=" + allowResize;			
	binWin = window.open("", winName, winProps);
	//var actionURL = stockroot_url+"/bingo/flashbingolauncher.jsp";
	try
	{	document.forms[winName].target = winName;
		//document.forms[winName].action = actionURL;
		document.forms[winName].submit();

	}
	catch(e)
	{
		alert(e);
	}
	binWin.focus();
}
function openWinGame(gameName,gameType,configId)
{
	var windowname =gameName;
	while (windowname.indexOf(' ')!=-1){windowname=windowname.replace(' ','');}
	if(gameName!="Keno")
	{
		try{allGameWindowOnLoad();}catch (e){}
	}
	 allowResize = "yes";
	 // Start at top left
	 LeftPosition = 100;
	 TopPosition = 0;
	 // Go almost full-screen for Flash Bingo
	 myHeight = (screen.height) ? (screen.height-145) : 455;
	 myWidth = myHeight * 1.3475;
	 // Set the left and top window position to zero if logic above has produced a value less than zero
	 if (LeftPosition < 0) { LeftPosition = 0;}
	 if (TopPosition < 0) { TopPosition = 0;}
	 // Configure other window properties
	 winProps = "left="+LeftPosition+",top="+TopPosition+",width="+myWidth+",height="+myHeight+",location=0,scrollbars=0,menubar=0,toolbar=0,resizable=" + allowResize;
	 binWin = window.open("/commonsys/launchGame.jsp?gameName="+gameName+"&gameType="+gameType+"&configId="+configId, windowname, winProps);
	 binWin.focus();
}
function openWinPullTab(winName,stockroot_url)
{
	allowResize = "yes";
	// Start at top left
	LeftPosition = 100;
	TopPosition = 0;
	// Go almost full-screen for Flash Bingo
	myHeight = (screen.height) ? (screen.height-145) : 455;
	myWidth = myHeight * 1.3475;
	// Set the left and top window position to zero if logic above has produced a value less than zero
	if (LeftPosition < 0) {	LeftPosition = 0;}
	if (TopPosition < 0) {	TopPosition = 0;}
	// Configure other window properties
	//alert(myHeight+"   "+myWidth);
	winProps = "left="+LeftPosition+",top="+TopPosition+",width=480,height=635,location=0,scrollbars=0,menubar=0,toolbar=0,resizable=" + allowResize;			
	binWin = window.open("", winName, winProps);
	//var actionURL = stockroot_url+"/bingo/flashbingolauncher.jsp";
	try
	{	document.forms[winName].target = winName;
		//document.forms[winName].action = actionURL;
		document.forms[winName].submit();

	}
	catch(e)
	{
		alert(e);
	}
	binWin.focus();
}


//adding the below functions for g2 bingo
function trim(stringToTrim) {
	if (stringToTrim == undefined) {
		return;
	}
	return stringToTrim.replace(/^\s+|\s+$/g,"");
}
function createRequestObject() {
    var httprReqobj;
    var browser = navigator.appName;
    if(browser == "Microsoft Internet Explorer"){
		try{
			httprReqobj = new ActiveXObject("Microsoft.XMLHTTP");        
		}catch(recreate){
			httprReqobj = new XMLHttpRequest();		
		}
    }else{
        httprReqobj = new XMLHttpRequest();
    }
    return httprReqobj;
}

function isBingoRoomAllowed(streamId)
{
	var qualifierReq = createRequestObject();
	qualifierReq.open('POST', "/commonsys/bingo_qualifier.jsp?streamId=" + streamId,false);
	qualifierReq.send(null);
	return trim(qualifierReq.responseText);
}

function openBingoGame(streamId)
{
	var message = isBingoRoomAllowed(streamId);
	if (message != "" && message != "SUCCESS")
	{
		alert(message);
		return;
	}

	try
	{
		 allowResize = "yes";
		 // Start at top left
		 LeftPosition = 100;
		 TopPosition = 0;
		 // Go almost full-screen for Flash Bingo
		 myHeight = screen.height;
		 myWidth = screen.width;
		 // Set the left and top window position to zero if logic above has produced a value less than zero
		 if (LeftPosition < 0) { LeftPosition = 0;}
		 if (TopPosition < 0) { TopPosition = 0;}
		 // Configure other window properties
		 var scroll	=	0;	
		 if (myHeight < 864 || myWidth < 1152 )
		 {
			 scroll	=	1;
		 }
		 winProps = "left="+LeftPosition+",top="+TopPosition+",width=1000,height=740,location=0,scrollbars="+scroll+",menubar=0,toolbar=0,resizable=" + allowResize;
		 var bossWin = window.open("/commonsys/bingo_launcher.jsp?streamId="+streamId,streamId, winProps);
		 bossWin.focus();		
	}
	catch (e)
	{
	}
}

function launchStream(streamName)
{
	try {
		openBingoGame(streamName);
		//eval(streamName+"()");
	}catch(e) {
		alert('Room is not available, try again later');
	}
}
var unfinishedGameWin;
function openUnfinishedGame(gameVariantName,deploymentUrl,gcName,gameSessionId)
{
	if (unfinishedGameWin != null && !unfinishedGameWin.closed)
	{
		unfinishedGameWin.focus();
		unfinishedGameWin.alert("The game window is already open");
		return;
	}	
	try
	{
		 allowResize = "yes";
		 // Start at top left
		 LeftPosition = 100;
		 TopPosition = 0;
		 // Go almost full-screen for Flash Bingo
		 myHeight = screen.height;
		 myWidth = screen.width;
		 // Set the left and top window position to zero if logic above has produced a value less than zero
		 if (LeftPosition < 0) { LeftPosition = 0;}
		 if (TopPosition < 0) { TopPosition = 0;}
		 // Configure other window properties
		 var scroll	=	0;	
		 if (myHeight < 630 || myWidth < 800 )
		 {
			 scroll	=	1;
		 }
		 winProps = "left="+LeftPosition+",top="+TopPosition+",width=800,height=630,location=0,scrollbars="+scroll+",menubar=0,toolbar=0,resizable=" + allowResize;
		 unfinishedGameWin = window.open("/commonsys/unfinished_game_launcher.jsp?gameVariantName="+gameVariantName+"&deploymentUrl="+deploymentUrl+"&gameName="+gcName+"&gameSessionId="+gameSessionId,"unfinishedgame", winProps);
		 unfinishedGameWin.focus();		
	}
	catch (e)
	{
		alert(e);
	}
}

/**
Added by PavanPinnu
It return the actual parent window of the sub window chain.
*/
function getMainParentWindow () {
	if (window.opener == null) {
		return window;
	}

	if (window.opener.name == 'homewindow') {
		return window.opener;
	}

	var win = window;
	do {
		win = win.opener;
	} while (win.name != 'homewindow');

	return win;
}


//end of g2 bingo
