//Gets the browser specific XmlHttpRequest Object
function getXmlHttpRequestObject() {
	if (window.XMLHttpRequest) {
		return new XMLHttpRequest();
	}
	else if(window.ActiveXObject) {
		return new ActiveXObject("Microsoft.XMLHTTP");
	}
	else {
		window.alert('Cound not create XmlHttpRequest Object.' +
			         'Consider upgrading your browser.');
	}
}

// synchronous XML loader
function loadXML( url )
{
	var xmlDoc;
	if (window.ActiveXObject) {
		xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
	}
	else if (document.implementation &&	document.implementation.createDocument)	{
		xmlDoc = document.implementation.createDocument("","",null);
	}
	else {
		window.alert('Your browser cannot handle this script');
		return;
	}

	xmlDoc.async = false;
	xmlDoc.load( url );

	return xmlDoc;
}

// global variables
// var StationAlias = null;
// var HistoryUrl = '';
// var Locale = '';
var WishListTimer;
var WishListReq = getXmlHttpRequestObject();
// var WishListXslDoc = loadXML('?Action=WishListXSL&Locale='+Locale);
var WishListXslDoc = loadXML('?Action=WishListXSL');

var HistoryTimer;
var HistoryReq = getXmlHttpRequestObject();
// var HistoryXslDoc = loadXML('?Action=HistoryXSL&Locale='+Locale);
var HistoryXslDoc = loadXML('?Action=HistoryXSL');

function handleReceiveWishList() {

	if (WishListReq.readyState != 4 || WishListReq.status != 200)
 		return;

	var xmldoc = WishListReq.responseXML;
    var html;
    if (window.XSLTProcessor) {
        var xsltProcessor = new XSLTProcessor();
        xsltProcessor.importStylesheet(WishListXslDoc);
        var node = xsltProcessor.transformToFragment(xmldoc, document);
        html = (new XMLSerializer()).serializeToString( node );
    }
    else if(window.ActiveXObject) {
        html = xmldoc.transformNode(WishListXslDoc);
    }

	var div_requests = document.getElementById('div_requests');
	div_requests.innerHTML = html;

	WishListTimer = setTimeout('getWishList();', 5000);
}

function getWishList() {
	if (WishListReq.readyState == 4 || WishListReq.readyState == 0) {
	    var url = '?Action=WishListXML&StationAlias='+StationAlias;
		WishListReq.open("GET", url, true);
		WishListReq.onreadystatechange = handleReceiveWishList; 
		WishListReq.send(null);
	}
}

//Function for handling the return of chat text
function handleReceiveHistory() {

	if (HistoryReq.readyState != 4 )
 		return;

	var div_offair = document.getElementById('div_offair');
	var div_History = document.getElementById('div_History');
	var div_listen = document.getElementById('div_listen');

	if( HistoryReq.status == 404 )
	{
		div_offair.style.display = '';
		div_History.style.display = 'none';
		if( div_listen != null )
			div_listen.style.display = 'none';
	}
	else if( HistoryReq.status == 200 )
	{
		var xmldoc = HistoryReq.responseXML;
		var onAir = xmldoc.documentElement.getAttribute('OnAir');
		if( onAir == 1 )
		{
		    var html;
		    if (window.XSLTProcessor) {
		        var xsltProcessor = new XSLTProcessor();
		        xsltProcessor.importStylesheet(HistoryXslDoc);
		        var node = xsltProcessor.transformToFragment(xmldoc, document);
		        html = (new XMLSerializer()).serializeToString( node );
		    }
		    else if(window.ActiveXObject) {
		        html = xmldoc.transformNode(HistoryXslDoc);
		    }
		

			div_offair.style.display = 'none';
			div_History.style.display = '';
			if( div_listen != null )
				div_listen.style.display = '';

			div_History.innerHTML = html;
		}
		else
		{
			div_offair.style.display = '';
			div_History.style.display = 'none';
			if( div_listen != null )
				div_listen.style.display = 'none';
		}
	}

	HistoryTimer = setTimeout('getHistory();', 3000);
}

//Gets the current messages from the server
function getHistory() {
	if (HistoryReq.readyState == 4 || HistoryReq.readyState == 0) {
		HistoryReq.open("GET", HistoryUrl, true);
		HistoryReq.setRequestHeader('If-Modified-Since', 'Sat, 1 Jan 2000 00:00:00 GMT');
		HistoryReq.onreadystatechange = handleReceiveHistory; 
		HistoryReq.send(null);
	}
}

function stopChat() {
	clearInterval(WishListTimer);
	clearInterval(HistoryTimer);
}

//Function for initializating the page.
window.onload = function() {
	//Start Recieving Messages.
	WishListTimer = setTimeout('getWishList();', 5000);
	if( HistoryUrl )
		getHistory();
}

