var newsCount = 5;  // number of news items to show in index.html
var feedsCount = 5; // number of feeds items to show in index.html
var issuesCount = 6; // number of issue images to show 
var yearsCount = 0;  // number of years of past issues to show in archive (0 -> all)
var newsMonths = 3; // number of months of news items to show 
var feedsMonths = 3; // number of months of news feeds to show 

var xmlHttpAds;
var xmlHttpNews;
var xmlHttpFeeds;
var xmlHttpIssues;
var xmlHttpCal;
var xmlHttpBGuide;

function createXMLHttpRequest(x) {
	if(window.ActiveXObject) {
		x = new ActiveXObject("Microsoft.XMLHTTP");
		}
	else if (window.XMLHttpRequest) {
		x = new XMLHttpRequest();
		}
	else {
		x = null;
		// alert("create null");
		}
	return x;
}
function trim(str)
{ return str.replace(/^\s*|\s*$/g,"");
}
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 getAds(ads)
{
	var src = new String("pages/ndxAds.php?ads="+ads)
	// alert(src);
	xmlHttpAds = createXMLHttpRequest(xmlHttpAds);
  	xmlHttpAds.onreadystatechange = displayAds;
	xmlHttpAds.open("GET", src, true);
	xmlHttpAds.send(null);
}
function getNews(items)
{
	var src = new String("pages/ndxNews.php?nw="+items);
	xmlHttpNews = createXMLHttpRequest(xmlHttpNews);
  	xmlHttpNews.onreadystatechange = displayNews;
	xmlHttpNews.open("GET", src, true);
	xmlHttpNews.send(null);
}
function getFeeds(items)
{
	var src = new String("pages/ndxFeeds.php?fd="+items);
	xmlHttpFeeds = createXMLHttpRequest(xmlHttpFeeds);
  	xmlHttpFeeds.onreadystatechange = displayFeeds;
	xmlHttpFeeds.open("GET", src, true);
	xmlHttpFeeds.send(null);
}
function getNewsRoom(ndx)
{
	// alert(ndx);
	var src;
	var args = new String(getArgs(""));
	if(ndx < 0) window.location.search = "";		
	if((ndx < 12) && (args.length == 0)) {
		src = new String("pages/ndxNewsRoom.php?mn="+newsMonths);
		}
	else if((ndx > 12) || (args.length == 0)) {
		src = new String("pages/ndxNewsStory.php?ns="+ndx+"&mn="+newsMonths);
		}
	else {
		src = new String("pages/ndxNewsStory.php?"+args+"&mn="+newsMonths);
		}
	// alert(src);
	xmlHttpNews = createXMLHttpRequest(xmlHttpNews);
  	xmlHttpNews.onreadystatechange = displayNewsRoom;
	xmlHttpNews.open("GET", src, true);
	xmlHttpNews.send(null);
}
function getFeedsRoom()
{
	var src = new String("pages/ndxFeedsRoom.php?mn="+feedsMonths);
	// alert(src);
	xmlHttpFeeds = createXMLHttpRequest(xmlHttpFeeds);
  	xmlHttpFeeds.onreadystatechange = displayFeedsRoom;
	xmlHttpFeeds.open("GET", src, true);
	xmlHttpFeeds.send(null);
}
function getIssues(id)
{
	var src = new String("pages/ndxIssues.php?no="+issuesCount);
	if(id < 0) window.location.search = "";
	else {
		var args = new String(getArgs(""));
		// alert(args);
		if(args.length > 0)  {
			argElements = args.split("=");
			if(argElements[0] == "ns")
				src = new String("pages/ndxIssuesArticle.php?id="+argElements[1]);
			else if(argElements[0] == "id")
				src = new String("pages/ndxIssuesSelected.php?"+args);
			}
		}
	// alert(src);
	xmlHttpIssues = createXMLHttpRequest(xmlHttpIssues);
  	xmlHttpIssues.onreadystatechange = displayIssues;
	xmlHttpIssues.open("GET", src, true);
	xmlHttpIssues.send(null);
}
function getIssuesSelected(id)
{
	var args = new String(getArgs(""));
	if(args.length > 0) {
		argElements = args.split("=");
		if(argElements[0] == "ns") 	window.location.search = "id="+id;
		// alert(args);
		}
	var src = new String("pages/ndxIssuesSelected.php?id="+id);
	// alert(src);
	xmlHttpIssues = createXMLHttpRequest(xmlHttpIssues);
  	xmlHttpIssues.onreadystatechange = displayIssues;
	xmlHttpIssues.open("GET", src, true);
	xmlHttpIssues.send(null);
}
function getIssuesArticle(id)
{
	var args = new String(getArgs(""));
	if(args.length > 0) {
		argElements = args.split("=");
		if(argElements[0] == "id") 	window.location.search = "ns="+id;
		// alert(args);
		}
		var src = new String("pages/ndxIssuesArticle.php?id="+id);
	// alert(src);
	xmlHttpIssues = createXMLHttpRequest(xmlHttpIssues);
  	xmlHttpIssues.onreadystatechange = displayIssues;
	xmlHttpIssues.open("GET", src, true);
	xmlHttpIssues.send(null);
}
function getIssuesArchive()
{
	var src = new String("pages/ndxIssuesArchive.php?yr="+yearsCount);
	// alert(src);
	xmlHttpIssues = createXMLHttpRequest(xmlHttpIssues);
  	xmlHttpIssues.onreadystatechange = displayIssues;
	xmlHttpIssues.open("GET", src, true);
	xmlHttpIssues.send(null);
}
function getBGuideInit()
{
	var src = new String("pages/ndxBuyersGuide.php?ndx=init");
	// alert(src);
	xmlHttpBGuide = createXMLHttpRequest(xmlHttpBGuide);
	xmlHttpBGuide.onreadystatechange = displayBGuideInit;
	xmlHttpBGuide.open("GET", src, true);
	xmlHttpBGuide.send(null);
}
function getBGuideAlpha(first)
{
	var src = new String("pages/ndxBuyersGuide.php?ndx=alpha_"+first);
	xmlHttpBGuide = createXMLHttpRequest(xmlHttpBGuide);
	xmlHttpBGuide.onreadystatechange = displayBGuide;
	xmlHttpBGuide.open("GET", src, true);
	xmlHttpBGuide.send(null);
}
function getBGuideAlphaAll()
{
	var src = new String("pages/ndxBuyersGuide.php?ndx=alpha");
	xmlHttpBGuide = createXMLHttpRequest(xmlHttpBGuide);
	xmlHttpBGuide.onreadystatechange = displayBGuide;
	xmlHttpBGuide.open("GET", src, true);
	xmlHttpBGuide.send(null);
}
function getBGuideCat(cat,company)
{
	var src = new String("pages/ndxBuyersGuide.php?ndx=cat_"+cat);
	if(company > 0) src += "&co="+company;
	xmlHttpBGuide = createXMLHttpRequest(xmlHttpBGuide);
	xmlHttpBGuide.onreadystatechange = displayBGuide;
	xmlHttpBGuide.open("GET", src, true);
	xmlHttpBGuide.send(null);
}
function getCalendar()
{
	var src = new String("pages/ndxCalendar.php");
	// alert(src);
	xmlHttpCal = createXMLHttpRequest(xmlHttpCal);
  	xmlHttpCal.onreadystatechange = displayCalendar;
	xmlHttpCal.open("GET", src, true);
	xmlHttpCal.send(null);
}


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("|||");
				// alert("ndx("+ad[0]+") length("+ad.length+")");
				var el = document.getElementById("divAd"+ad[0]);
				if(el) el.style.display = 'block';
				var adImage = new String("");
				var adText = new String("");
				for(var j = 1; j < ad.length; ) {
					adImage += ad[j++];
					// adText += ad[j++];
				}
				el = document.getElementById("divAdImage"+ad[0]);
				if(el) el.innerHTML = adImage;
			}
		}
	}
}
function displayNews() 
{
	if(xmlHttpNews.readyState == 4) { 
		if(xmlHttpNews.status == 200) {
			var el = document.getElementById("divNews");
			if(el) el.innerHTML = xmlHttpNews.responseText;
			}
		}
}
function displayFeeds() 
{
	if(xmlHttpFeeds.readyState == 4) { 
		if(xmlHttpFeeds.status == 200) {
			var el = document.getElementById("divFeeds");
			if(el) el.innerHTML = xmlHttpFeeds.responseText;
			}
		}
}
function displayIssues() 
{
	if(xmlHttpIssues.readyState == 4) { 
		if(xmlHttpIssues.status == 200) {
			var response = new String(xmlHttpIssues.responseText);
			response = response.split("|||");
			var el = document.getElementById("divIssues");
			if(el) el.innerHTML = response[0];
			if(response.length > 1) {
				el = document.getElementById("divBreadCrumb");
				if(el) el.innerHTML = response[1];
				}
			}
		}
}
function displayNewsRoom() 
{
	if(xmlHttpNews.readyState == 4) { 
		if(xmlHttpNews.status == 200) {
			var response = new String(xmlHttpNews.responseText);
			response = response.split("|||");
			var el = document.getElementById("divNews");
			if(el) el.innerHTML = response[0];
			if(response.length > 1) {
				el = document.getElementById("divBreadCrumb");
				if(el) el.innerHTML = response[1];
				}
			}
		}
}
function displayFeedsRoom() 
{
	if(xmlHttpFeeds.readyState == 4) { 
		if(xmlHttpFeeds.status == 200) {
			var el = document.getElementById("divFeeds");
			if(el) el.innerHTML = xmlHttpFeeds.responseText;
			}
		}
}
function displayCalendar() 
{
	if(xmlHttpCal.readyState == 4) { 
		if(xmlHttpCal.status == 200) {
			var el = document.getElementById("divCalendar");
			if(el) el.innerHTML = xmlHttpCal.responseText;
			}
		}
}
function displayBGuide()
{
	if(xmlHttpBGuide.readyState == 4) {
		if(xmlHttpBGuide.status == 200) {
			response = xmlHttpBGuide.responseText;
			response = response.split("###");
			var el = document.getElementById("divBGmain");
			if(el) el.innerHTML = response[0];
			if(response.length > 1) {
				window.location.hash = response[1];
				}
			}
	}
}
function displayBGuideInit()
{
	if(xmlHttpBGuide.readyState == 4) {
		if(xmlHttpBGuide.status == 200) {
			var response = xmlHttpBGuide.responseText;
			response = response.split("|||");
			var el = document.getElementById("divBGcats");
			if(el) el.innerHTML = response[0];
			if(response.length > 1) {
				el = document.getElementById("divBGalpha");
				if(el) el.innerHTML = response[1];
				if(response.length > 2) {
					el = document.getElementById("divBGmain");
					if(el) el.innerHTML = response[2];
					}
				}
			}
		}
}
