function DoEscape(s)
{
	var s2 = new String(s);
	return s2.replace(/#/g, "##");
}
function DoCallback(cmd, arg1, arg2, arg3)
{
	// we use # as an escape character
	// ## is a # in a value
	// #: indicates the end of a value
	var END = "#:";
	document.title=DoEscape(cmd)+END+DoEscape(arg1)+END+DoEscape(arg2)+END+DoEscape(arg3)+END;
}

function Ready()
{
	if (gDrmData.page == "response")
	{
		unlockCallback(false, gDrmData.reply);
	}
}

function Post(dst, func, a, b, c)
{
	var bOk = false;
	if (dst == "drm") 
	{
		if (func == "browse") 
		{
			bOk = true;
			var withoutGo = gDrmData.site_url;
			var i = withoutGo.indexOf("/go");
			if (i != -1) {
				withoutGo = withoutGo.substr(0, i);
			}
			if (a.indexOf("http") == 0) {
				DoCallback("page", a);
			} else {
				DoCallback("page", withoutGo + a);
			}
		}
		else if (func == "set_id") 
		{
			bOk = true;
			// no-op
		}
		else if (func == "unlock")
		{
			bOk = true;
			DoCallback("buy", a, b, c);
		}
	}
	else if (dst == "SELF")
	{
		if (func == "close") 
		{
			bOk = true;
			// no-op
		}
		else if (func == "init")
		{
			bOk = true;
			// no-op
		}		
	}
	else if (dst.indexOf("a/preplay") == 0)
	{
		if (func == "init")
		{
			// for now, skip ad and just play
			bOk = true;
			DoCallback("try");
		}
	}
	else if (dst == "manager") 
	{
		if (func == "quit") 
		{
			bOk = true;
			DoCallback("quit");
		} 
		else if (func == "windowsize" || func == "center" || func == "fullscreen") 
		{
			bOk = true;
			// no-op
		}		
	}
	else if (dst.indexOf("singleplayer/index") == 0)
	{
		if (func == "init") 
		{
			bOk = true;
			DoCallback("try");
		}
	}

	if (!bOk) {
		alert("error in Post: " + dst + ", " + func);
	}	
}

function PostWithCallback(callback, dst, func, a, b, c)
{
	var bOk = false;
	if (dst == "manager")
	{
		if (func == "session_data") 
		{
			bOk = true;	   
			var nums = gDrmData.usedSessions + " " + gDrmData.usedTotalSeconds + " " + gDrmData.usedSessionSeconds;
			callback(nums, gDrmData.game_data);
		}
	}
	else if (dst == "drm") 
	{
		if (func == "get_status")
		{
			bOk = true;
			// no-op
		}
	}	

	if (!bOk) {
		alert("error in PostWithCallback: " + dst + ", " + func);
	}	
}

