	function debug(msg, error) {
		//alert(msg + "\n" + error);
	}

	function getURLVar(urlVarName) {
		var query = window.location.search.substring(1);
		var vars = query.split("&");
		for (var i=0;i<vars.length;i++) {
			var pair = vars[i].split("=");
			if (pair[0] == urlVarName) {
				return pair[1];
			}
		}
	}

        var gameWindow;
        var randomTunnel="domainTunnel" + Math.floor(Math.random()*1000);
       
	if(window.opener) {
		var tnl = getURLVar("tnl");
		if(tnl) {
			randomTunnel = tnl;
		}
		debug("randomTunnel: " + randomTunnel);
	}

	function writeDomainTunnel(d) {
		var swfPath = "/modal/DsDomainTunnel.swf?version=2";
		var flashVars = "connectionName=" + randomTunnel + "&domain=" + document.domain + "&isServer=true";// + "&debug=true";
		d.write("<object height=\"1\" width=\"1\" classid=\"clsid:D27CDB6E-AE6D-11cf-96B8-444553540000\" id=\"domaintunnelserver\">\n");
		d.write("	<param value=\"transparent\" name=\"wmode\"/>\n");
		d.write("	<param value=\"" + flashVars + "\" name=\"flashVars\"/>\n");
		d.write("	<param value=\"" + swfPath + "\" name=\"movie\"/>\n");
		d.write("	<embed name=\"domaintunnelserver\" height=\"1\" width=\"1\" src=\"" + swfPath + "\"\n");
		d.write("		 flashVars=\"" + flashVars + "\" wmode=\"transparent\"/>\n");
		d.write("</object>");
	}

	//called by domainTunnel when loaded
	function domainTunnelLoaded(tunnelName) {
		try {
			debug("domainTunnelLoaded " + tunnelName + "\ndocument.domain: " + document.domain);
			randomTunnel = tunnelName;
			var openerVar = getURLVar("opener");
			var url = "https://" + server + "/DTCF/games/betware.html?host=" + encodeURIComponent(window.location.host + window.location.pathname) + "&tnl=" + randomTunnel + (openerVar ? "&opener=" + openerVar : "");
			document.getElementById('betware').innerHTML='<iframe id="betwareFrameId" name="betwareFrame" src="' + url + '" width="1" height="1"></iframe>';

		} catch(error) {
			debug("tnlLoaded", error);
		}
	}
	

	// Internet Explorer and Mozilla-based browsers refer to the Flash application object differently.
	// This function returns the appropriate reference, depending on the browser.
	function getTunnel()	{
		if (navigator.appName.indexOf ("Microsoft") != -1) {
			return window["domaintunnelserver"];
		} else {
			return document["domaintunnelserver"];
		}
	}

       function resize_iframe()
       {

             var height=window.innerWidth;//Firefox
             if (document.body.clientHeight)
             {
                    height=document.body.clientHeight;//IE
             }
             document.getElementById("content").style.height=parseInt(height-
             document.getElementById("content").offsetTop-8)+"px";
       }

       window.onresize=resize_iframe; 


       function openGameWindow(source, width, height, windowId)
       {
		var tunnel = getTunnel();
		if(tunnel != null) {
			tunnel.reconnect();
		}

		if(!windowId) {
			//parse gameId for unique windowName 
			windowId = source.substring(source.indexOf("DTCF/games/"));
			windowId = windowId.substring("DTCF/games/".length,windowId.lastIndexOf("/"));
		}


		if (source.indexOf('#') > 0) { source = source + "&?connectionName=" + randomTunnel; } else { source = source + "?connectionName=" + randomTunnel; }

		var main = window.location.host + window.location.pathname;
		source = source + "&main=" + encodeURIComponent(main);
		
		modalWindow.windowId = windowId;
 		modalWindow.width = width;
 		modalWindow.height = height;
		//modalWindow.content = "<iframe width='100%' height='100%' frameborder='0' scrolling='no' allowtransparency='true' src='" + source + "'></iframe>";
		modalWindow.source = source;
 		modalWindow.open();       
	}
       
       function openFreeGameWindow(source, width, height)
       {
           modalFreeWindow.windowId = "freeModal";
           modalFreeWindow.width = width;
           modalFreeWindow.height = height;
           modalFreeWindow.content = "<iframe width='" + width + "' height='" + height + "' frameborder='0' scrolling='no' allowtransparency='true' src='" + source + "'></iframe>";
           modalFreeWindow.openCentered();
       }

       function closeGame()
       {
           modalWindow.close();
           window.focus();
       }

       function closeFreeGame()
       {
           modalFreeWindow.close();
           window.focus();
       }

        function setContentFrameLocation(url)
        {
                if (url.substr(0, 4) != "http")
                        url = 'https://' + server + url;

                top.frames['contentArea']['content'].location.href = url;
				closeGame();
        }

        function setMainLocation(url)
        {
                if (url.substr(0, 4) != "http") 
                        url = 'https://' + server + url;

                top.location.href = url;
				closeGame();
        }

		function postToUrl(base, path, params) 
		{
			var form = base.createElement("form");
			form.setAttribute("method", "post");
			form.setAttribute("action", path);

			for(var key in params) 
			{
				var hiddenField = base.createElement("input");
				hiddenField.setAttribute("type", "hidden");
				hiddenField.setAttribute("name", key);
				hiddenField.setAttribute("value", params[key]);
				form.appendChild(hiddenField);
			}
			base.body.appendChild(form);
			form.submit();       
			base.body.removeChild(form);
		}

		function createSubscription(couponRequest)
		{                    
			postToUrl(top.frames['contentArea']['content'].document, 'https://'+server +'/DTCF/games/CLTO/createSubscription.do', {'xml':couponRequest});   
			closeGame();        
		}
