
// global variables

var debugLevel = 0; // set the debug message level (0 = off)

function setElementHTML(elementID, newHTML) {
	if (document.all) {
		document.all(elementID).innerHTML = newHTML;
	} else if (document.getElementById) {
		document.getElementById(elementID).innerHTML = newHTML;
	}				
}
		
function getMonthCalendar(cal_id, year, month) {
	var d = new Date();
	if (year == -1) { year = d.getFullYear(); }
	if (month == -1) { year = d.getMonth(); }

	var httpReq = makeHttpRequestObject();
	httpReq.onreadystatechange = function() { getCalendar(httpReq, cal_id, month, year); };
	httpReq.open('GET', '/calendar/month_ajax.php?cal_id=' + cal_id + '&year=' + year + '&month=' + month, true);
	httpReq.setRequestHeader( "If-Modified-Since", "Sat, 1 Jan 2000 00:00:00 GMT" );
	httpReq.send(null);
}

function getCalendar(http_req, id, month, year) {
	if (http_req.readyState == 4) {
		if (http_req.status == 200) {
			var html = http_req.responseText;
			if (debugLevel > 0) {
				alert('Sending data: ' + html);
			}
			setElementHTML(id, html);
		} else {
			alert('There was a problem with the request.\nreceived values: ' + html);
		}
	}
}

function makeHttpRequestObject() {
	var http_req = false;
	
	if (window.XMLHttpRequest) {
		http_req = new XMLHttpRequest();
		if (http_req.overrideMimeType) {
			http_req.overrideMimeType('text/xml');
		}
	} else if (window.ActiveXObject) {
		try {
			http_req = new ActiveXObject("MSXML2.XMLHTTP");
		} catch (e) {
			try {
				http_req = new ActiveXObject("Microsoft.XMLHTTP");
			} catch (e) {
				// don't do anything
			}
		}
	}
	
	if (!http_req) {
		alert('Giving up! Can\'t Connect');
		return false;
	}
	
	return http_req;
}
