// JavaScript Document
function getData(fname)
{
	var XMLHttpRequestObject = false;
	var mozillaFlag = false;
	if (window.XMLHttpRequest) {
	XMLHttpRequestObject = new XMLHttpRequest();
		if(XMLHttpRequestObject.overrideMimeType) {
		XMLHttpRequestObject.overrideMimeType('text/xml');		
		}
	mozillaFlag = true;
	} else if(window.ActiveXObject) {

		XMLHttpRequestObject = new ActiveXObject("Microsoft.XMLHTTP");
	}

	if(XMLHttpRequestObject) {

		XMLHttpRequestObject.open("GET", fname);
		XMLHttpRequestObject.onreadystatechange = function()
		{
			if(XMLHttpRequestObject.readyState==4 && XMLHttpRequestObject.status==200) {
			var xmlDoc = XMLHttpRequestObject.responseXML;

				if(mozillaFlag){
				  removeWhitespace(xmlDoc);
				}
			displayData(xmlDoc);
			}
		}
	}
		
		XMLHttpRequestObject.send(null)
}		

function displayData (xmlDoc)
{
var displayText = "";
var displayImage = "";

m_ids = xmlDoc.getElementsByTagName("m_id");
titles = xmlDoc.getElementsByTagName("title");
descriptions = xmlDoc.getElementsByTagName("description");
images = xmlDoc.getElementsByTagName("cover_image");
urls = xmlDoc.getElementsByTagName("url");
emails = xmlDoc.getElementsByTagName("email");
frequencies = xmlDoc.getElementsByTagName("frequency");
newsstand_prices = xmlDoc.getElementsByTagName("newsstand_price");
subscription_prices = xmlDoc.getElementsByTagName("subscription_price");
names = xmlDoc.getElementsByTagName("pub_name");
address1s = xmlDoc.getElementsByTagName("address1");
address2s = xmlDoc.getElementsByTagName("address2");
cities = xmlDoc.getElementsByTagName("city");
provinces = xmlDoc.getElementsByTagName("province");
countries = xmlDoc.getElementsByTagName("country");
pcodes = xmlDoc.getElementsByTagName("pcode");
phones = xmlDoc.getElementsByTagName("phone");
faxes = xmlDoc.getElementsByTagName("fax");

for(loopIndex=0; loopIndex < titles.length; loopIndex++)
{
	//if(m_ids[loopIndex].firstChild.data === mem_id) {
		displayImage = "<img src='../media/members/images/" + images[loopIndex].firstChild.data + "' alt ='" + titles[loopIndex].firstChild.data + "'>" + "</br>";

		displayText = displayText + "<div id='membertitle'>" + titles[loopIndex].firstChild.data + "</div></br>";
		displayText = displayText + "<p>" + descriptions[loopIndex].firstChild.data + "</p>";
		displayText = displayText + "<table align='center' cellpadding='2' cellspacing='2'>";
		
		if(names[loopIndex].firstChild.data!='NA') {
			displayText = displayText + "<tr valign='top'><td width='149' align='right' nowrap='nowrap'>Address:</td>";
			displayText = displayText + "<td width='282'>" + names[loopIndex].firstChild.data + "<br />";
			//Safari browsers break if XML elements are empty
	  		//We therefore always set empty elements = "_" then
	  		//check if length is greater than one before writing data.
			
			if(address1s[loopIndex].firstChild.data.length>1) {
				displayText = displayText + address1s[loopIndex].firstChild.data + "<br />";
			}
			if(address2s[loopIndex].firstChild.data.length>1) {
				displayText = displayText + address2s[loopIndex].firstChild.data + "<br />";
			}	
			if(cities[loopIndex].firstChild.data.length>1) {
				displayText = displayText + cities[loopIndex].firstChild.data + ",&nbsp;";
			}
			if(provinces[loopIndex].firstChild.data.length>1) {
				displayText = displayText + provinces[loopIndex].firstChild.data + "<br />";
			}
			if(pcodes[loopIndex].firstChild.data.length>1) {
				displayText = displayText + pcodes[loopIndex].firstChild.data + "<br /><td>";
			}
		}
			if(urls[loopIndex].firstChild.data.length>1) {
				displayText = displayText + "</tr><tr valign='top'><td align='right'>Website:</td>";
				displayText = displayText + "<td><a href='http://" + urls[loopIndex].firstChild.data + "' target ='_blank'>" + urls[loopIndex].firstChild.data + "</a><br /></td></tr>";
			}
			if(emails[loopIndex].firstChild.data.length>1) {
				displayText = displayText + "<tr valign='top'><td  align='right'>e-mail:</td>";
				displayText = displayText + "<td><a href='mailto:" + emails[loopIndex].firstChild.data + "'>" + emails[loopIndex].firstChild.data + "</a><br /></td></tr>";
			}
			if(frequencies[loopIndex].firstChild.data.length>1) {
				displayText = displayText + "<tr valign='top'><td align='right'>Frequency:</td><td>" + frequencies[loopIndex].firstChild.data + "<br /></td></tr>";
			}
			if(newsstand_prices[loopIndex].firstChild.data.length>1) {
				displayText = displayText + "<tr valign='top'><td align='right'>Newsstand Price:</td><td>" + newsstand_prices[loopIndex].firstChild.data + "<br /></td></tr>";
			}
			if(subscription_prices[loopIndex].firstChild.data.length>1) {
				displayText = displayText + "<tr valign='top'><td align='right'>Subscription Price:</td>";
				displayText = displayText + "<td>" + subscription_prices[loopIndex].firstChild.data + "<br /></td></tr>";
			}
			if(phones[loopIndex].firstChild.data.length>1) {
				displayText = displayText + "<tr valign='top'><td align='right'>Phone:</td><td>" + phones[loopIndex].firstChild.data + "</br></td></tr>";
			}
			if(faxes[loopIndex].firstChild.data.length>1) {
				displayText = displayText + "<tr valign='top'><td  align='right'>Fax:</td><td>" + faxes[loopIndex].firstChild.data + "</br></td></tr></table>";
			}
		
	//}
}


var targetText = document.getElementById("live_content");
var targetImage = document.getElementById("sub_left_column");
targetText.innerHTML=displayText;
targetImage.innerHTML=displayImage;

}

function removeWhitespace(xml) 
{
var loopIndex;

for (loopIndex = 0; loopIndex < xml.childNodes.length; 
  loopIndex++) {

  var currentNode = xml.childNodes[loopIndex];

  if (currentNode.nodeType == 1) {
	removeWhitespace(currentNode);
  }

  if (((/^\s+$/.test(currentNode.nodeValue))) &&   
	(currentNode.nodeType == 3)) {
	  xml.removeChild(xml.childNodes[loopIndex--]);
  }
}
}
