var xmlHttpObject;

function getXmlHttpObject() {
        xmlHttpObject = null;
        try {
                xmlHttpObject = new XMLHttpRequest();
        } catch(e) {
                try {
                        xmlHttpObject = new ActiveXObject("Msxml2.XMLHTTP");
                } catch(e) {
                        try {
                                xmlHttpObject = new ActiveXObject("Microsoft.XMLHTTP");
                        } catch(e) {
                                return false;
                        }
                }
        }
        return xmlHttpObject;
}

function stateChanged() {
	if (xmlHttpObject.readyState == 4 || xmlHttpObject.readyState == "complete") {
		var splitRes = xmlHttpObject.responseText.split(",,,,,,");
		if (splitRes[0] == "WEBCAM") {
			var webcam_div = document.getElementById('div_webcam');
			webcam_div.innerHTML = splitRes[1];
		} else if (splitRes[0] == "SECONDSLEFT") {
			var div_id = document.getElementById(splitRes[2]);
			div_id.innerHTML = splitRes[1] + ": " + splitRes[3] + " seconds";
		}
	}
}

function updateWebcamImage() {
	xmlHttpObject = getXmlHttpObject();
        if (xmlHttpObject == null) {
                alert("Browser doesn't accept JavaScript !");
                return;
        }
	var url = "jsGetWebcam.php";
	xmlHttpObject.onreadystatechange = stateChanged;
        xmlHttpObject.open("GET", url, "true");
        xmlHttpObject.send(null);
	setTimeout("updateWebcamImage()", 5000);
}

function updateTimeLeftSeconds(title, div_id, limit) {
	xmlHttpObject = getXmlHttpObject();
        if (xmlHttpObject == null) {
                alert("Browser doesn't accept JavaScript !");
                return;
        }
	var url = "aj/getTimeLeft.php?title=" + title + "&div_id=" + div_id + "&limit=" + limit;
	xmlHttpObject.onreadystatechange = stateChanged;
        xmlHttpObject.open("GET", url, "true");
        xmlHttpObject.send(null);
	setTimeout("updateTimeLeftSeconds('"+title+"','" + div_id + "','" + limit + "')", 2000);
}

function bodyLoad() {
	updateTimeLeftSeconds('Work day', 'div_work_day', '2010-08-24 18:01:00');
}

