var xmlHttp;

function refreshArchive(year, month, category) {

	var strMonth;
	
	if (month < 10) {
		strMonth = "0" + month;	
	}
	else {
		strMonth = "" + month;	
	}
	
	var archiveList = document.getElementById("archive" + year + strMonth);
	if (archiveList) {
		if (archiveList.innerHTML.length > 10)
		{
			if (archiveList.style.display == 'none') {
				archiveList.style.display = 'block';	
			}
			else {
				archiveList.style.display = 'none';
			}
		}
		else {
			// Get the archive list since the current element doesn't have one	
			xmlHttp = initializeXmlHTTP();
			xmlHttp.onreadystatechange = onCompleteRefreshArchive;
			xmlHttp.open('GET', '/blog/wp-content/themes/zooyork/get-archive-list.php?date=' + year + strMonth + '&category=' + category);
			xmlHttp.send(null);
		}
	}
}


function onCompleteRefreshArchive() {
	if(xmlHttp.readyState==4) {
		var xml = xmlHttp.responseXML.documentElement;
		var results = xml.getElementsByTagName("results")[0];
		if (results) {
			var date = results.getAttribute('date');
			if (date.length == 6) {
				var archiveList = document.getElementById("archive" + date);
				if (archiveList) {
					archiveList.innerHTML = results.childNodes[0].nodeValue;
				}
			}
			
		}
	}
}







// Helper functions

function initializeXmlHTTP() {
	var xmlHttp;
	
	try {
		// Firefox, Opera 8.0+, Safari
		xmlHttp=new XMLHttpRequest();
	}
	catch (e) {
		// Internet Explorer
		try {
			xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
		}
		catch (e) {
			try {
				xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
			}
			catch (e) {
				alert("Your browser does not support AJAX!");
				return;
			}
		}
	} // end outer catch
	
	return xmlHttp;
}
