function requestWhatson(ch) {

	var now = new Date();
	var secs = now.getTime();
		
	if (xmlHttp.readyState == 4 || xmlHttp.readyState == 0) {

		xmlHttp.open("GET", "/ajax/whatson.php?channel=" + ch + "&t=" + secs, true);
		
		xmlHttp.onreadystatechange = responseWhatson;

		xmlHttp.send(null);
		
	} else {

		setTimeout('requestWhatson()', 1000);
	}
}

function responseWhatson() {

	if (xmlHttp.readyState == 4) {
  
		if (xmlHttp.status == 200) {
		
			// extract the XML retrieved from the server
			xmlResponse = xmlHttp.responseXML;
			// obtain the document element (the root element) of the XML structure
			xmlDocumentElement = xmlResponse.documentElement;
			
			var sChannel = xmlDocumentElement.getElementsByTagName('channel')[0];
			var aProgs = xmlDocumentElement.getElementsByTagName('programme');
			
			if(aProgs.length > 0) {
						
				var oWhatson = document.getElementById('whatsOn');
				
				var sList = '<table cellspacing="0">';
		
				for(var i=0; i < aProgs.length; i++) {
								
					var sTime = aProgs[i].getElementsByTagName('time')[0];
					var sTitle = aProgs[i].getElementsByTagName('title')[0];
						
					if(sTitle.firstChild.data == 'Programmes start at') {
					
						sList += '<tr>';
						sList += '<td><span>' + sTitle.firstChild.data + ' ' + sTime.firstChild.data + '</span></td>';
						sList += '<tr>';
						
						continue;
					
					} else {

						sList += '<tr>';
						sList += '<td>' + sTime.firstChild.data + '</td>';
						sList += '<td><span>' + sTitle.firstChild.data + '</span></td>';
						sList += '<tr>';
					}										
				}

				sList += '</table>';
				
				/* Whats on menu */
				
				oWhatson = document.getElementById('whatsOn');
				
				var sWhats = '<div>';
				
				switch(sChannel.firstChild.data) {
				
					case 'CLI1':
					
						sWhats += '1&nbsp;<a href="javascript:requestWhatson(\'CLI2\')">2</a>&nbsp;<a href="javascript:requestWhatson(\'CLI3\')">3</a>&nbsp;';
						oWhatson.style.background = 'url(/images/nav/whats_on_title_3.1.gif) no-repeat';
						break;
						
					case 'CLI2':
					
						sWhats += '<a href="javascript:requestWhatson(\'CLI1\')">1</a>&nbsp;2&nbsp;<a href="javascript:requestWhatson(\'CLI3\')">3</a>&nbsp;';
						oWhatson.style.background = 'url(/images/nav/whats_on_title_3.2.gif) no-repeat';
						break;
						
					case 'CLI3':
					
						sWhats += '<a href="javascript:requestWhatson(\'CLI1\')">1</a>&nbsp;<a href="javascript:requestWhatson(\'CLI2\')">2</a>&nbsp;3&nbsp;';
						oWhatson.style.background = 'url(/images/nav/whats_on_title_3.3.gif) no-repeat';
						break;
						
				
				}
				
				sWhats += '</div>';

				oWhatson.innerHTML = sList + sWhats;
				
				var oTSscroller = document.getElementById('TSscroller');
				
				if(oTSscroller)	requestSchedule(sChannel.firstChild.data);
				
			}
			
		} else {
		
			alert("There was a problem accessing the server: " + xmlHttp.statusText);
		}
	}
}


