prefix='/6nimmt/img/'; 
ext='.gif';  
var req = false;
var bLastCard = false;
var bIsBusy = false;
var srcLastCardPlayed = 0;
var activeTimeout = 0;
var iLastCard = 0;

if(window.XMLHttpRequest)
{
	try
	{
		req = new XMLHttpRequest();
	} catch(e) { req = false; }
}
else if (window.ActiveXObject)
{
	try
	{
		req = new ActiveXObject("Msxml2.XMLHTTP");
	}
	catch(e)
	{
		try
		{
			req = new ActiveXObject("Microsoft.XMLHTTP");
		}
		catch(e)
		{
			req = false;
		}
	}
}


function parsegame()
{
   if (req.readyState == 4)
   {
        if (req.status == 200)
		{
			document.getElementById("hand" + iLastCard).disabled = "disabled";
			document.getElementById("hand" + iLastCard).src = prefix + 0 + ext;
			if (activeTimeout != 0)
			{
				window.clearTimeout(activeTimeout);
				activeTimeout = 0;
			}		
			var commands = req.responseText.split("\n");
            for(count = 0; count < commands.length; count++)
			{
				var zeile = commands[count];
				var num = zeile.charAt(1);
				switch(zeile.charAt(0))
				{
					case 'C':
						DisplayComment(num, zeile.slice(3));
					break;
					case 'D':
						SetDetailURL(zeile.slice(2));
					break;
					case 'H':
						DisplayCard(num, zeile.slice(3));
					break;
					case 'S':
						DisplayScore(num, zeile.slice(3));
					break;
					case 'R':
						DisplayDesk(zeile.slice(1,4), zeile.slice(5));
					break;
					case 'U':
						SetSubmitURL(zeile.slice(2));
					break;
					case 'X':
						if (zeile.charAt(2) == '1')
						{
							bLastCard = true;
						}
						else
						{
							bLastCard = false;
						}
					break;
				}
			}
			
        } 
		bIsBusy = false;
    }	
}

function DisplayCard(player, card)
{
	try
	{
		var eleScore = "horn" + player;
		document.images[eleScore].src = prefix + card + ext;
		document.getElementById(eleScore).alt = "" + card + "";
		document.getElementById(eleScore).title = "" + card + "";
	}
	catch(e){}
}

function DisplayDesk(row, card)
{
	try
	{
		var ele = "desk" + row;
		document.images[ele].src = prefix + card + ext;
		if (card == 0)
			card = "";
		document.getElementById(ele).alt = "" + card + "";
		document.getElementById(ele).title = "" + card + "";
	}
	catch(e){}

}

function DisplayComment(num, comment)
{
	if (num == 1)
		document.getElementById("c1").firstChild.nodeValue  = comment;
	else if (num == 2)
		document.getElementById("c2").firstChild.nodeValue = comment;
	else if (num == 3)
		document.getElementById("c3").firstChild.nodeValue  = comment;
	else if (num == 4)
		document.getElementById("c4").firstChild.nodeValue  = comment;
}

function play(card)
{
	if (bIsBusy)
		return false;
		
	iLastCard = card;
		
	var currentsubmiturl = document.getElementById("currentsubmiturl").value + "&w=" + card;
	
	if (bLastCard == true)
	{
		location.href = currentsubmiturl;
		return false;
	}

	if (req)
	{
	
		currentsubmiturl =  "play.asp" + currentsubmiturl;
		req.open("GET", currentsubmiturl, true);
		req.onreadystatechange = parsegame;
		req.send("");
		bIsBusy = true;
		srcLastCardPlayed = document.getElementById("hand" + card).src;
		activeTimeout = window.setTimeout("networktimeout()", 5000);
		return false;
		
	}
	
	return true;
}

function networktimeout()
{	
	if (bIsBusy)
	{
		req.abort();
		bIsBusy = false;
		window.alert("The server does not respond. Please ensure, that your computer is connected to the Internet and repeat your turn.");
	}
	else
	{
		srcLastCardPlayed = "";
	}

}


function SetSubmitURL(submiturl)
{
	document.getElementById("currentsubmiturl").value = submiturl;
}

function SetDetailURL(detailurl)
{
	document.getElementById("details").href = detailurl;
	document.getElementById("details").firstChild.nodeValue = "[in detail]";
}

function DisplayScore(player, score)
{
	try
	{
		var eleScore = "score" + player;
		var eleFont = "font" + player;
		var text = score + ' Cattle Head';
		if (score != 1)
			text += 's';
		document.getElementById(eleScore).firstChild.nodeValue = text;
		if (score > 0)
			document.getElementById(eleFont).color = "red";
		else
			document.getElementById(eleFont).color = "black";
	}
	catch(e){}
}

