var HasClicked=false;
var ItemClicked;
var ZoneID=0;
var TimerID;
var BlinkID;
var cName;
var cNewName;
var Mode=0;

function GetRadioButtonValue(O,X)
{
	for (i=0;i<X;i++)
	{
		if (O[i].checked)
		{
			return i;
		}
	}
}

function OnTimer()
{
	clearTimeout(timerID);
	if (document.all(BlinkID).className==cName)
		document.all(BlinkID).className=cNewName;
	else
		document.all(BlinkID).className=cName;
	timerID = setTimeout("OnTimer()", 200);
}

var pX;
var pY;
function clickType(O, OType,Width)
{
	if (Mode>=2)
		return;
	pX=event.x;
	pY=event.y;
	if (Mode==1 && O.zone != "")
		return;
	Type=GetRadioButtonValue(document.all(OType),2);
	if (!HasClicked)
	{
		HasClicked=true;
		ItemClicked=O.id;
		if (Type==0)
		{
			BlinkID=ItemClicked;
			cNewName="seat_blink";
		}
		else
		{
			BlinkID="Z"+ItemClicked;
			cNewName="zone_blink";
		}
		cName=document.all(BlinkID).className;
		timerID = setTimeout("OnTimer()", 200);
	}
	else
	{
		clearTimeout(timerID);
		document.all(BlinkID).className=cName;
		HasClicked=false;
		start=parseInt(ItemClicked);
		end=parseInt(O.id);
		if (Type==0)
		{
			className="seat";		
			siegeName="*";
			if (document.all(ItemClicked).className=="seat")
			{
				className="seat_none";
				siegeName="";
			}
		}
		else
		{
			className="zone_0";		
			ZoneID++;
			if (document.all("Z" + ItemClicked).className=="zone")
			{
				className="zone_none";
			}
		}
			
		if (parseInt(ItemClicked)>parseInt(O.id))
		{	
			end=parseInt(ItemClicked);
			start=parseInt(O.id);
		}
		for (i=start;i<=end;i++)
		{
			if (!(i%Width<start%Width || i%Width>end%Width))
			{
				if (Type==0)
				{
					document.all(String(i)).className=className;
					document.all(String(i)).siege=siegeName;
				}
				else
				{
					document.all(String(i)).zonenr = ZoneID;
					if (i%Width==start%Width && Math.floor(i/Width)==Math.floor(start/Width))
						document.all("Z"+String(i)).className=className + "_tl";
					else if (i%Width==end%Width && Math.floor(i/Width)==Math.floor(start/Width))
						document.all("Z"+String(i)).className=className + "_tr";
					else if (i%Width==start%Width && Math.floor(i/Width)==Math.floor(end/Width))
						document.all("Z"+String(i)).className=className + "_bl";						
					else if (i%Width==end%Width && Math.floor(i/Width)==Math.floor(end/Width))
						document.all("Z"+String(i)).className=className + "_br";												
					else if (i%Width==start%Width)
						document.all("Z"+String(i)).className=className + "_l";
					else if (i%Width==end%Width)
						document.all("Z"+String(i)).className=className + "_r";
					else if (Math.floor(i/Width)==Math.floor(start/Width))
						document.all("Z"+String(i)).className=className + "_t";
					else if (Math.floor(i/Width)==Math.floor(end/Width))
						document.all("Z"+String(i)).className=className + "_b";					
					else
						document.all("Z"+String(i)).className=className;
				}
			}
		}
		if (Type==1)
		{
			document.all(String(start)).innerHTML="<A href='javascript:GetZoneDiv("+ZoneID+","+String(start)+")'>" + ZoneID + "</A>"
			document.all(String(start)).zone = ZoneID;
			document.all(String(start)).section = "*";
			document.all(String(start)).block = "*";
			document.all(String(start)).entry = "*";
		}
	}	
}

function GetZoneDiv(ZoneID, cID)
{
	if (Mode!=1)
		return;
	document.all("ZoneEditNr").innerHTML = ZoneID;
	document.all("ZoneEditCell").value = cID;
	if (document.all(String(cID)).section != null)
	{
		document.all("ZoneEditSection").value=document.all(String(cID)).section;
		document.all("ZoneEditBlock").value=document.all(String(cID)).block;
		document.all("ZoneEditEntry").value=document.all(String(cID)).entry;
	}
	else
	{
		document.all("ZoneEditSection").value="";
		document.all("ZoneEditBlock").value="";
		document.all("ZoneEditEntry").value="";
	}
	document.all("ZoneEdit").style.display = "inline";
	document.all("ZoneEdit").style.left = pX;
	document.all("ZoneEdit").style.top = pY;	
}

function SaveZone()
{
	document.all(String(document.all("ZoneEditCell").value)).section  = document.all("ZoneEditSection").value;
	document.all(String(document.all("ZoneEditCell").value)).block = document.all("ZoneEditBlock").value;
	document.all(String(document.all("ZoneEditCell").value)).entry = document.all("ZoneEditEntry").value;
	document.all("ZoneEdit").style.display = "none";
}

function SaveMode(maxV)
{
	if (Mode==2)
	{
		for (i=0;i<=maxV;i++)
		{
			if (document.all(String(i)).siege=="*")
			{
				if (typeof(document.all('I'+String(i)).value) == "undefined")
					document.all(String(i)).siegenr="";
				else
					document.all(String(i)).siegenr=document.all('I'+String(i)).value;	
			}
		}	
	}
	if (Mode==3)
	{
		for (i=0;i<=maxV;i++)
		{
			if (document.all(String(i)).siege=="*")
			{
				if (typeof(document.all('IR'+String(i)).value) == "undefined")
					document.all(String(i)).rownr="";
				else
					document.all(String(i)).rownr=document.all('IR'+String(i)).value;	
			}
		}	
	}
}

function SwitchToSeat(maxV)
{
	SaveMode(maxV);
	Mode=0;
	for (i=0;i<=maxV;i++)
	{
		if (document.all(String(i)).siege=="*")
		{
			document.all(String(i)).className="seat";
			if (document.all(String(i)).zone !="")
				document.all(String(i)).innerHTML="<A href='javascript:GetZoneDiv("+document.all(String(i)).zone+","+String(i)+")'>" + document.all(String(i)).zone + "</A>"
			else				
				document.all(String(i)).innerHTML="&nbsp;";
		}
	}		
}

function SwitchToZone(maxV)
{
	SwitchToSeat(maxV);
	Mode=1;
}

function SwitchToSeatEdit(maxV)
{
	SaveMode(maxV);
	Mode=2;
	for (i=0;i<=maxV;i++)
	{
		if (document.all(String(i)).siege=="*")
		{
			document.all(String(i)).className="seat_edit";
			if (document.all(String(i)).siegenr)
				S=document.all(String(i)).siegenr;
			else
				S="";
			document.all(String(i)).innerHTML="<INPUT id='I" + String(i) + "' class='inputsiege' value='" + S + "'/>";
		}
	}
}

function SwitchToRowEdit(maxV)
{
	SaveMode(maxV);
	Mode=3;
	for (i=0;i<=maxV;i++)
	{
		if (document.all(String(i)).siege=="*")
		{
			document.all(String(i)).className="seat_row_edit";
			if (document.all(String(i)).rownr)
				S=document.all(String(i)).rownr;
			else
				S="";
			document.all(String(i)).innerHTML="<INPUT id='IR" + String(i) + "' class='inputrow' value='" + S + "'/>";
		}
	}
}


function PrepareData(maxV)
{
	SaveMode(maxV);
	R="";
	for (i=0;i<=maxV;i++)
	{
		L = String(i) + ",";
		if (document.all(String(i)).siege=="*")
		{
			if (document.all(String(i)).siegenr)
				L=L+document.all(String(i)).siegenr+",";
			else
				L=L+"*,";
			if (document.all(String(i)).rownr)
				L=L+document.all(String(i)).rownr+",";
			else
				L=L+"*,";
				
		}
		else
			L=L+",,";
		if (typeof(document.all(String(i)).zonenr) == "undefined")
			L=L + ",";
		else
			L=L+document.all(String(i)).zonenr + ",";
			
		if (typeof(document.all(String(i)).section) == "undefined")
			L=L + ",";
		else
			L=L+document.all(String(i)).section + ",";
		
		if (typeof(document.all(String(i)).block) == "undefined")
			L=L + ",";
		else
			L=L+document.all(String(i)).block + ",";
		
		if (typeof(document.all(String(i)).entry) == "undefined")
			L=L + "|";
		else
			L=L+document.all(String(i)).entry + "|";
		R=R+L;
	}
	document.all("DATA").value=R;
}


function PrepareSeatData(maxV)
{
	R="";
	for (i=0;i<=maxV;i++)
	{
		L="";
		if (document.all(String(i)).siege=="*")
		{
			L = String(i) + ",";		
			if (document.all(String(i)).siegenr)
				L=L+document.all(String(i)).siegenr+",";
			else
				L=L+"*,";
			L=L+document.all(String(i)).tobuy+"|";	
		}
		R=R+L;
	}
	document.all("DATA").value=R;
}

function clickSeat(N)
{
	if (document.all(String(N)).tobuy==0)
	{
		document.all(String(N)).tobuy=1;
		document.all(String(N)).className="seatsel";
	}
	else if (document.all(String(N)).tobuy==1)
	{
		document.all(String(N)).tobuy=0;
		document.all(String(N)).className="seattobuy";
	}
}
