
ie = (document.all)?true:false
mac = (navigator.platform.indexOf("Mac") != -1)? true : false;

function hideHighlight(id)
{
	var div = document.getElementById(id+'marker')
	if(div == null)
		return;

	div.style.display='none';
}

function updatePos()
{
	var loada = new Array();
	var form = document.forms["mainform"]
	var selec  = new String(form.selected.value)
	loada = selec.split('\;')

	for(var i = 0; i < loada.length; i++)
	{
		var div = document.getElementById(loada[i]+'marker')
		if(div == null)
			continue;

		var coords = div.coords

		var top = 10000
		var left = 0

		if(coords)
		{
			coordsa = coords.split(',');

			for(var j = 0; j < coordsa.length; j++)
			{
				if(j % 2 == 0)
					left += parseInt(coordsa[j])
					//left = parseInt(coordsa[i])<left?parseInt(coordsa[i]):left
				else
					top = parseInt(coordsa[j])<top?parseInt(coordsa[j]):top
			}

			left = left/2 - 25
		}

		var o = document.getElementById('gameimg')
		for(; o; o = o.offsetParent)
		{
			top += o.offsetTop;
			left += o.offsetLeft;
		}
		
		div.style.left = left
		div.style.top = top
	}

}

function showHighlight(id, coords)
{
	var div = document.getElementById(id+'marker')
	if(div != null)
	{ 
		div.style.display='inline'
		return false;
	}
	var div = document.createElement('DIV')
	var img = document.createElement('IMG')
	img.src='sel.gif'
	img.width = 50
	img.height = 50

	//div.style.backgroundColor='black'
	div.style.position='absolute'
	div.style.display='inline'

	div.id = id+'marker'
	div.coords = coords

	div.onclick = function(){ selectMan(id); checkForm() }

	div.appendChild(img)
	document.body.appendChild(div)
	updatePos()
}

function checkForm()
{
/*
	form = document.forms["mainform"]
	var old = form.style.display
	if(form.count.value == 10)
		form.style.display='block';
	else
		form.style.display='none';

	if(old != form.style.display)
		updatePos()
*/
	form = document.forms["mainform"]
	var gameimg = document.getElementById('gameimg')
	var select = document.getElementById('selectimg')
	var text = document.getElementById('text')
	if(form.count.value == 10)
	{
		select.style.display='block';
		text.style.display='none';
		gameimg.useMap = ''
	}
	else
	{
		select.style.display='none';
		text.style.display='block';
		gameimg.useMap = '#clickarea'
	}
}

function selectMan(id)
{
	var form = document.forms["mainform"]
	var val = form.selected.value

	startgame()

	if(document.getElementById(id))
		var coords = document.getElementById(id).getAttribute('coords')

	if(val.indexOf(id+';') != -1)
	{
		var reg = new RegExp(id+';')

		val = val.replace(reg, '')
		form.selected.value=val
		form.count.value = form.count.value - 1;
		hideHighlight(id)
	}
	else
	{
		if(form.count.value == 10)
		{
			var unsel = val.substring(0, val.indexOf(';'))
			selectMan(unsel)
			val=val.substring(val.indexOf(';') + 1)
			form.count.value = 9
		}


		form.selected.value=val+id+';'
		form.count.value = parseInt(form.count.value)+parseInt(1);
		showHighlight(id, coords)
	}

	if(document.getElementById('text'))
	{
		var text = document.getElementById('text')

		text.innerHTML = ''
		if(parseInt(form.count.value) > 0)
			text.innerHTML += form.count.value+' selected'
		if(parseInt(form.count.value) < 10)
			text.innerHTML += ' ' + (10 - form.count.value) + ' left'
	}

	return false;
}

function submitcheck()
{
	if(document.forms["mainform"].count.value != 10)
	{
		alert('You should select 10 hookers!\nNot '
					+document.forms["mainform"].count.value)
		return false;
	}
	
	return true;
}

function preselect(loading, erasemap)
{
	var loada = new Array();
	loada = loading.split('\;')
	form = document.forms["mainform"]
	
	form.count.value = 0
	form.selected.value = ''
	for( var i = 0; i < loada.length; i++)
	{
		if(loada[i] == '') { continue; }

		selectMan(loada[i])
	}

	//if(document.getElementById('gameimg') && erasemap)
	//	document.getElementById('gameimg').useMap = ''

	checkForm()

	return true;
}

function startgame()
{
	var divrules = document.getElementById('rules')
//	var divtitle = document.getElementById('title')

	//if(document.getElementById('gameimg'))
	//	document.getElementById('gameimg').useMap = '#clickarea'

	if(divrules) //&& divtitle)
	{
		divrules.style.display = 'block'
		//divtitle.style.display = 'none'
		return false;
	}


	return true;
}

var img=new Image();
img.src='sel.gif';

