var gDay = new Array('So','Mo','Di','Mi','Do','Fr','Sa')

var gLast

function ShowFrame(aTable, aShowID, aHall, aDay, aShow, aTime)
{

	if (gLast != aDay + " " + aShowID)
	{
		gLast = aDay + " " + aShowID
	
		var theA = document.getElementById(gLast)
	
		theA.style.backgroundColor = "#A30808"
	
	
	
	
		var theTr = document.createElement('tr')
	
		var theTd = document.createElement('td')
		theTd.setAttribute('colspan', "8")
		
		theTr.appendChild(theTd)
		
		var theTable = document.getElementById(aTable)
		
		var theTBody = theTable.lastChild
		
	//	var theRows = theTBody.getElementsByTagName('tr')
	//	theRows[theRows.length - 2].style.visibility = "collapse"
	//	theRows[theRows.length - 1].style.visibility = "collapse"
		
		theTBody.appendChild(theTr)
	
	//	alert(theTd.clientWidth)
	
	
	
		var theFrame = document.createElement('iframe')
		theFrame.id = "reserv"
		theFrame.height = "660px"
	
	//	theFrame.width = "840px"
		theFrame.width = theTd.clientWidth + "px"
	
		theFrame.frameborder = "1px"
		theFrame.scrolling = "no"
//		theFrame.src = "http://localhost:8080/Reservation/" + aHall + "/" + aDay + "/" + aShow + ".html?mode=2"
		theFrame.src = "http://www.advance-ticket.ch/Reservation/" + aHall + "/" + aDay + "/" + aShow + ".html?1234"
	
		
		theTd.appendChild(theFrame)
	}

}

function ShowTable(aData, aTable)
{
	var theTBody = aTable.getElementsByTagName('tbody')[0]
	
	var theTr = document.createElement('tr')

	for (var theDayID in aData.root.day)
	{
		var theDate = new Date(theDayID.substr(0, 4), theDayID.substr(5, 2) - 1, theDayID.substr(8, 2))

		var theTd = document.createElement('td')
		theTd.className = "dates"

		theTd.appendChild(document.createTextNode(gDay[theDate.getDay()]))
		theTd.appendChild(document.createElement('br'))

		var theSmall = document.createElement('small')
		theSmall.appendChild(document.createTextNode(theDayID.substring(8, 10) + '.' + theDayID.substring(5, 7)))

		theTd.appendChild(theSmall)

		theTr.appendChild(theTd)
	}
	theTBody.appendChild(theTr)

	var theNow = new Date
	theNow.setMilliseconds(theNow.getMilliseconds() + 45 * 60 * 1000)

	var theTr = document.createElement('tr')
	for (var theDayID in aData.root.day)
	{
		var theTd = document.createElement('td')
		theTd.className = "times"

		if (aData.root.day[theDayID].show)
		{
		
			var theFlag1 = false
			var theFlag2 = false
			var theFlag3 = false
			for (var theShowID in aData.root.day[theDayID].show)
			{
				var theShow = aData.root.day[theDayID].show[theShowID]
				if (theShow.place + "-" + theShow.spec == aTable.id)
				{
					if (theShow.time < '17:00')
						theFlag1 = true
					else if (theShow.time < '19:00')
					{
						theFlag2 = true
						if (!theFlag1)
						{
							theFlag1 = true
							theTd.appendChild(document.createElement('br'))
						}
					}
					else if (theShow.time < '22:00')
					{
						theFlag3 = true
						if (!theFlag1)
						{
							theFlag1 = true
							theTd.appendChild(document.createElement('br'))
						}
						if (!theFlag2)
						{
							theFlag2 = true
							theTd.appendChild(document.createElement('br'))
						}
					}
					else
					{
						if (!theFlag1)
						{
							theFlag1 = true
							theTd.appendChild(document.createElement('br'))
						}
						if (!theFlag2)
						{
							theFlag2 = true
							theTd.appendChild(document.createElement('br'))
						}
						if (!theFlag3)
						{
							theFlag3 = true
							theTd.appendChild(document.createElement('br'))
						}
					}


					var theDate = new Date(theDayID.substr(0, 4), theDayID.substr(5, 2) - 1, theDayID.substr(8, 2), theShow.time.substr(0, 2), theShow.time.substr(3, 2), 0)
					if (theDate < theNow)
					{
						var theSpan = document.createElement('span')
						theSpan.appendChild(document.createTextNode(theShow.time))
						theTd.appendChild(theSpan)
					}
					else
					{
						var theA = document.createElement('a')
						theA.id = theDayID + " " + theShowID

//						if ((location.hostname == "localhost") || (location.hostname == "212.147.32.36"))
//							theA.href = "javascript:ShowFrame('" + aTable.id + "','" + theShowID + "','" + theShowID.substring(0, 3) + "','" + theDayID + "','" + theShowID.substring(4, 6) + "','" + theShow.time + "')"
//						else
//						theA.href = "http://www.advance-ticket.ch/Reservation/" + theShowID.substring(0, 3) + "/" + theDayID + "/" + theShowID.substring(4, 6) + ".html"
//						theA.href = theShow.url + '&mode=0'
						theA.href = theShow.url

						theA.rel = "nofollow"
						theA.appendChild(document.createTextNode(theShow.time))
						theTd.appendChild(theA)
					}
	
					theTd.appendChild(document.createElement('br'))
				}
			}
		
		}
		
		theTr.appendChild(theTd)
		
	}
	theTBody.appendChild(theTr)

	aTable.appendChild(theTBody)
}

function ProgAction(aData)
{
	var theTableList = document.getElementsByTagName("table")
	for (var i = 0; i < theTableList.length; i++)
		if ((theTableList[i].id) && (theTableList[i].id.substr(3, 1) == '-'))
			ShowTable(aData, theTableList[i])
}

function DocumentEventListener(event)
{
	if ((event.target.tagName != 'A') || (event.target.id != gLast))
	{

		var theA = document.getElementById(gLast)
		if (theA)
		{
			theA.style.backgroundColor = "#286BC4"
			gLast = null
		}



		var theFrame = document.getElementById("reserv")
		if (theFrame)
		{
			var theTd = theFrame.parentNode
			var theTr = theTd.parentNode
			var theTBody = theTr.parentNode
			
			theTd.removeChild(theFrame)
			theTr.removeChild(theTd)		
			theTBody.removeChild(theTr)
				
		}
	}
}

function Prog(aFile)
{
//	if ((location.hostname == "localhost") || (location.hostname == "212.147.32.36"))
//		document.addEventListener("click", DocumentEventListener, true)
	GetXMLFile(aFile, ProgAction)
}

function GetXMLFile(aFile, anAction)
{
	if (window.XMLHttpRequest)
		var theRequest = new XMLHttpRequest() // native XMLHttpRequest
	else if (window.ActiveXObject)
		var theRequest = new ActiveXObject("Microsoft.XMLHTTP") // IE/Windows ActiveX
	if (theRequest)
	{
		theRequest.open("GET", aFile, false)
		theRequest.send(null)
		if (theRequest.responseXML)
			anAction(NodeListToObject(theRequest.responseXML.childNodes))
		else
			alert(theRequest.status)
	}
}

function NodeListToObject(aNodeList)
{
	var theData

	for (var i = 0; i < aNodeList.length; i++)
		if (aNodeList[i].nodeType == 1)
		{
			if (aNodeList[i].childNodes.length == 0)
				var theVal = ""
			else if ((aNodeList[i].childNodes.length == 1) && ((aNodeList[i].firstChild.nodeType == 3) || (aNodeList[i].firstChild.nodeType == 4)))
				var theVal = aNodeList[i].firstChild.nodeValue
			else
				var theVal = NodeListToObject(aNodeList[i].childNodes)
			if (theData == null)
				theData = new Object()
			if (theData[aNodeList[i].tagName] == null)
				theData[aNodeList[i].tagName] = new Object()
			if (aNodeList[i].attributes[0])
				theData[aNodeList[i].tagName][aNodeList[i].attributes[0].value] = theVal
			else
				theData[aNodeList[i].tagName] = theVal
		}
	return theData
}