var xmlHttpIssue;
var xmlHttpPrevIssue;
var xmlHttpNews;
var xmlHttpNewsItem;
var xmlHttpFeatured;
var xmlHttpCalendar;
var xmlHttpSource;
var xmlHttpArticle;
var xmlHttpCover;
var xmlHttpTOC;
var xmlHttpArchives;
var xmlHttpPoll;
var xmlHttpAds;
var pollSelection = 0;

function setCrumbs($crumb) {
	document.getElementById("divCrumbs").innerHTML = $crumb;
}
function createXMLHttpRequest(x) {
	if(window.ActiveXObject) {
		x = new ActiveXObject("Microsoft.XMLHTTP");
		}
	else if (window.XMLHttpRequest) {
		x = new XMLHttpRequest();
		}
	else x = null;
	return x;
}
function trim(str)
{ return str.replace(/^\s*|\s*$/g,"");
}
function save(id)
{
	pollSelection = id;
}
function getArgs(str) {
	var args = new String(trim(location.search));
	var n = args.length;
	if(n > 0) args = str+args.substring(1, n);
	else args = "";
	return args; 
}
function getCheckedValue(radioObj) {
	if(!radioObj) return "";
	var radioLength = radioObj.length;
	if(radioLength == undefined)
		if(radioObj.checked)
			return radioObj.value;
		else
			return "0";
	for(var i = 0; i < radioLength; i++) {
		if(radioObj[i].checked) return radioObj[i].value;
	}
	return "";
}
function getAds(ads)
{
	var src = new String("/pages/ndxAds.php?ads="+ads)
	xmlHttpAds = createXMLHttpRequest(xmlHttpAds);
  	xmlHttpAds.onreadystatechange = displayAds;
	xmlHttpAds.open("GET", src, true);
	xmlHttpAds.send(null);
}
function getPoll(action)
{
	var src = new String("pages/ndxPoll.php");
	var args = getArgs("?");
	var vote = new String();
	if(action == 1) {
		// user has clicked submit; need to retrieve selected answer
		//var el = document.getElementById("response");
		//if(el) vote = getCheckedValue(el);
		//if(vote.length > 0) src = src+"?vote="+vote;
		if(pollSelection > 0) src = src+"?vote="+pollSelection;
	}
	else if(args.length > 1) src = src+args;
	//alert("action("+action+") src("+src+") args("+args+")");
	xmlHttpPoll = createXMLHttpRequest(xmlHttpPoll);
	xmlHttpPoll.onreadystatechange = displayPoll;
	xmlHttpPoll.open("GET", src, true);
	xmlHttpPoll.send(null);
}
function getArchives(id) {
	// alert(id);
	var args = new String();
	if(id == undefined) args = getArgs("?");
	else if(parseInt(id) == 0) args = "";
	else if(parseInt(id) > 0) args = "?id="+id;
	else if(id.substr(0, 1) == "N") args = "?ns="+id.substr(1); 
	else args = getArgs("?");
	var src = new String("pages/ndxArchives.php"+args);
	// alert(src);
	xmlHttpArchives = createXMLHttpRequest(xmlHttpArchives);
	xmlHttpArchives.onreadystatechange = displayArchives;
	xmlHttpArchives.open("GET", src, true);
	xmlHttpArchives.send(null);
}
function getIssue(id) {
	var src = new String("pages/ndxIssue.php?id="+id);
	if(id == 0) {
		xmlHttpIssue = createXMLHttpRequest(xmlHttpIssue);
		xmlHttpIssue.onreadystatechange = displayIssue;
		xmlHttpIssue.open("GET", src, true);
		xmlHttpIssue.send(null);
	}
	else {
		xmlHttpPrevIssue = createXMLHttpRequest(xmlHttpPrevIssue);
		xmlHttpPrevIssue.onreadystatechange = displayPrevIssue;
		xmlHttpPrevIssue.open("GET", src, true);
		xmlHttpPrevIssue.send(null);
	}
}
function getTOC() {
	var args = getArgs("?");
	var src = new String("pages/ndxTOC.php"+args);
	xmlHttpTOC = createXMLHttpRequest(xmlHttpTOC);
  	xmlHttpTOC.onreadystatechange = displayTOC;
	xmlHttpTOC.open("GET", src, true);
	xmlHttpTOC.send(null);
}
function getNews(id) {
	var args = getArgs("?");
	if(!(args.indexOf("ns") > 0)) {
		if(id > 0) args = "?ns="+id;
		else args = "";
	}
	var src = new String("pages/ndxNews.php"+args);
	// alert("id("+id+")\nsrc("+src+")")
	xmlHttpNews = createXMLHttpRequest(xmlHttpNews);
	xmlHttpNews.onreadystatechange = displayNews;
	xmlHttpNews.open("GET", src, true);
	xmlHttpNews.send(null);
}
function getCover() { 
	var args = getArgs("?");
	var src = new String("pages/ndxCover.php"+args);
	xmlHttpCover = createXMLHttpRequest(xmlHttpCover);
	xmlHttpCover.onreadystatechange = displayCover;
	xmlHttpCover.open("GET", src, true);
	xmlHttpCover.send(null);
}
function getFeatured(id) { 
	var src = new String("pages/ndxFeatured.php?id="+id);
	xmlHttpFeatured = createXMLHttpRequest(xmlHttpFeatured);
	xmlHttpFeatured.onreadystatechange = displayFeatured;
	xmlHttpFeatured.open("GET", src, true);
	xmlHttpFeatured.send(null);
}
function getCalendar() { 
	xmlHttpCalendar = createXMLHttpRequest(xmlHttpCalendar);
	xmlHttpCalendar.onreadystatechange = displayCalendar;
	xmlHttpCalendar.open("GET", "pages/ndxCalendar.php", true);
	xmlHttpCalendar.send(null);
}
function getSource() {
	var src = new String("pages/ndxSource.php");
	var url = new String(location.search);
	var id = new String();
	var found = 0;

	url = url.replace(/\?/,"");
	dataArray = url.split(/&|=/);
		
	for(var ndx = 0; ndx < dataArray.length; ndx++) {
		if(dataArray[ndx] == "id") {
			id = dataArray[++ndx];
			src = src+"?id="+id;
			ndx = dataArray.length;
			found = -1;
		}
	}
	
	if(found == 0) src = src+location.search;
	
	xmlHttpSource = createXMLHttpRequest(xmlHttpSource);
	xmlHttpSource.onreadystatechange = displaySource;
	xmlHttpSource.open("GET", src, true);
	xmlHttpSource.send(null);
}
function getSourceX() {
	var src = new String("pages/ndxSourceX.php");
	var url = new String(location.search);
	var id = new String();
	var found = 0;

	url = url.replace(/\?/,"");
	dataArray = url.split(/&|=/);
		
	for(var ndx = 0; ndx < dataArray.length; ndx++) {
		if(dataArray[ndx] == "id") {
			id = dataArray[++ndx];
			src = src+"?id="+id;
			ndx = dataArray.length;
			found = -1;
		}
	}
	
	if(found == 0) src = src+location.search;
	
	xmlHttpSource = createXMLHttpRequest(xmlHttpSource);
	xmlHttpSource.onreadystatechange = displaySource;
	xmlHttpSource.open("GET", src, true);
	xmlHttpSource.send(null);
}
function getArticle(id) {
	var args = new String();
	var src = new String("pages/ndxArticle.php");
	var url = new String(location.search);
	var ns = id;
	
	url = url.replace(/\?/,"");
	dataArray = url.split(/&|=/);
		
	for(var ndx = 0; ndx < dataArray.length; ndx++) {
		if(dataArray[ndx] == "ns") { ++ndx; if(id == 0) ns = dataArray[ndx]; }
		else if(trim(dataArray[ndx]).length  > 0) { args = '&'+dataArray[ndx]+'='+dataArray[++ndx]; }
	}
	
	src = src+"?ns="+ns+args;
	
	xmlHttpArticle = createXMLHttpRequest(xmlHttpArticle);
	xmlHttpArticle.onreadystatechange = displayArticle;
	xmlHttpArticle.open("GET", src, true);
	xmlHttpArticle.send(null);
}
function getNewsItem(id) {
	var src = new String("pages/ndxNewsItem.php");
	var url = new String(location.search);
	var ns = id;
	if(id == 0) {
		url = url.replace(/\?/,"");
		dataArray = url.split(/&|=/);
		
		for(var ndx = 0; ndx < dataArray.length; ndx++) {
			if(dataArray[ndx] == "ns") {
				ns = dataArray[++ndx];
				ndx = dataArray.length;
			}
		}
	}
	if(ns > 0) src = src+"?ns="+ns;
	xmlHttpNewsItem = createXMLHttpRequest(xmlHttpNewsItem);
	xmlHttpNewsItem.onreadystatechange = displayNewsItem;
	xmlHttpNewsItem.open("GET", src, true);
	xmlHttpNewsItem.send(null);
}
function displayPoll() {
	if(xmlHttpPoll.readyState == 4) {
		if(xmlHttpPoll.status == 200) {
			document.getElementById("divPoll").innerHTML = xmlHttpPoll.responseText;
			}
		}
}
function displayArchives() {
	if(xmlHttpArchives.readyState == 4) {
		if(xmlHttpArchives.status == 200) {
			var response = xmlHttpArchives.responseText.split("===");
			if(response.length > 1) {
				setCrumbs(response[1]);
				response = response[0];
			}
			document.getElementById("divArchives").innerHTML = response;
			document.location.hash = "top";
			}
		}
}
function displayIssue(){
	if(xmlHttpIssue.readyState == 4) {
		if(xmlHttpIssue.status == 200) {
			document.getElementById("divIssue").innerHTML = xmlHttpIssue.responseText;
			}
		}
}
function displayPrevIssue(){
	if(xmlHttpPrevIssue.readyState == 4) {
		if(xmlHttpPrevIssue.status == 200) {
			document.getElementById("divPrevIssue").innerHTML = xmlHttpPrevIssue.responseText;
			}
		}
}
function displayTOC() { 
	if(xmlHttpTOC.readyState == 4) { 
		if(xmlHttpTOC.status == 200) { 
			document.getElementById("divTOC").innerHTML = xmlHttpTOC.responseText;
		}
	}
}
function displayNews() {
	if(xmlHttpNews.readyState == 4) {
		if(xmlHttpNews.status == 200) {
			var response = xmlHttpNews.responseText.split("===");
			if(response.length > 1) {
				setCrumbs(response[1]);
				response = response[0];
			}
			document.getElementById("divNews").innerHTML = response;
			document.location.hash = "top";
			}
		}
}
function displayNewsItem() {
	if(xmlHttpNewsItem.readyState == 4) {
		if(xmlHttpNewsItem.status == 200) {
			document.getElementById("divNewsItem").innerHTML = xmlHttpNewsItem.responseText;
			document.location.hash = "top";
			}
		}
}
function displayCover() { 
	if(xmlHttpCover.readyState == 4) { 
		if(xmlHttpCover.status == 200) { 
			document.getElementById("divCover").innerHTML = xmlHttpCover.responseText;
		}
	}
}
function displayFeatured() { 
	if(xmlHttpFeatured.readyState == 4) { 
		if(xmlHttpFeatured.status == 200) { 
			document.getElementById("divFeatured").innerHTML = xmlHttpFeatured.responseText;
		}
	}
}
function displayCalendar() { 
	if(xmlHttpCalendar.readyState == 4) { 
		if(xmlHttpCalendar.status == 200) { 
			document.getElementById("divCalendar").innerHTML = xmlHttpCalendar.responseText;
		}
	}
}
function displayAds() { 
	if(xmlHttpAds.readyState == 4) { 
		if(xmlHttpAds.status == 200) {
			var ads = xmlHttpAds.responseText.split("===");
			for(var i = 0; i < ads.length; ++i) {
				var ad = ads[i].split("|||");
				var el = document.getElementById("divAd"+ad[0]);
				if(el) el.style.display = 'block';
				el = document.getElementById("divAdImage"+ad[0]);
				if(el) el.innerHTML = ad[1];
				el = document.getElementById("divAdText"+ad[0]);
				if(el) {
					el.innerHTML = ad[2];
					el.style.display = 'block';
				}
			}
		}
	}
}
function displaySource() { 
	var hash = new String();
	if(xmlHttpSource.readyState == 4) { 
		if(xmlHttpSource.status == 200) {
			var data = new String(xmlHttpSource.responseText);
			var pos = data.indexOf("|");
			if(pos < 9) {
				hash = data.substring(0,pos);
				data = data.substr(pos+1);
			}
			document.getElementById("divSource").innerHTML = data;
			if(hash.length > 0) document.location.hash = hash;
		}
	}
}
function displaySourceX() { 
	if(xmlHttpSourceX.readyState == 4) { 
		if(xmlHttpSourceX.status == 200) { 
			document.getElementById("divSource").innerHTML = xmlHttpSourceX.reponseText;
		}
	}
}
function displayArticle() { 
	if(xmlHttpArticle.readyState == 4) { 
		if(xmlHttpArticle.status == 200) { 
			document.getElementById("divArticle").innerHTML = xmlHttpArticle.responseText;
		}
	}
}