var xmlHttp;

/*
 *  Cross-browser method of creating the request object
 */
function createXMLHttpRequest() {
    if (window.XMLHttpRequest) {
	    xmlHttp = new XMLHttpRequest();
    }
	else if (window.ActiveXObject) {
        xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
    } 
}


/*
 * Sends an AJAX request using GET protocol.
 */
function sendGetRequest(command, callback, params) {
	createXMLHttpRequest();
    
    var url = "/ajax/" + command + ".php?timeStamp=" + new Date().getTime();

    // make sure we got some data in the params...
    if ((params != null) && (typeof(params) == 'string') && (params.length > 0)) {
		url = url + "&" + params;
	}
    
    xmlHttp.open("GET", url, true);
    xmlHttp.onreadystatechange = callback;
    xmlHttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");    
    xmlHttp.send(null);
}

/*
 * Sends an AJAX request using PUT protocol.
 */
function sendPostRequest(command, callback, data, params) {
	createXMLHttpRequest();
    
    var url = "/ajax/" + command + ".php?timeStamp=" + new Date().getTime();
    // make sure we got some data in the params...
    if ((params != null) && (typeof(params) == 'string') && (params.length > 0)) {
		url = url + "&" + params;
	}
    
    xmlHttp.open("POST", url, true);
    xmlHttp.onreadystatechange = callback;
    xmlHttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");    
    xmlHttp.send(data);
}


function updatePlaylist() {
	sendGetRequest("getSchedule2", setScheduleResponseHandler);
	setTimeout("updatePlaylist()", 60000);
}

function setScheduleResponseHandler()
{
    // Make sure the request is loaded (readyState = 4)
    if (xmlHttp.readyState == 4)
    {
        // Make sure the status is "OK"
        if (xmlHttp.status == 200)
        {
			var element = document.getElementById("new_nowplaying");
			element.innerHTML = xmlHttp.responseText;
        }
        else
        {
            alert("There was a problem retrieving the XML data:\n" + xmlHttp.statusText);
        }
    }
} 
