var xmlHttp;
var xmlDoc;

function init(){
internetExplorer();
loadXMLDoc('members.xml');
}


function internetExplorer(){
  if(navigator.appName == "Microsoft Internet Explorer")
    document.getElementById('styleSheet').href="tableStyles2.css"
}

function loadXMLDoc(url){
	xmlHttp=null;
	// code for Mozilla, etc.
	if (window.XMLHttpRequest)
	{
		xmlHttp=new XMLHttpRequest();
	}
	// code for IE
	else if (window.ActiveXObject)
	{
		xmlHttp=new ActiveXObject("Microsoft.xmlHttp");
	}
	if (xmlHttp!=null)
	{
		xmlHttp.onreadystatechange=state_Change;
		xmlHttp.open("GET",url,false);
		xmlHttp.send(null);
	}
	else
	{
		alert("Your browser does not support xmlHttp.");
	}
}

function state_Change()
{
	// if xmlHttp shows "loaded"
	if (xmlHttp.readyState==4)
	{
		// if "OK"
		if (xmlHttp.status==200)
		{
			xmlDoc=xmlHttp.responseXML.documentElement;
			loadTable();
		}
		else
		{
			alert("Problem retrieving XML data:" + xmlHttp.statusText)
		}
	}
}

function loadTable(){
	var tbl = document.getElementById('tab');
	removeAllRows(tbl);
	var num=0;
	var arry = xmlDoc.getElementsByTagName('member');
	document.getElementById('heading').innerHTML = "All Members";	
	for(; num < arry.length; num++){
		
		var name = document.createTextNode(xmlDoc.getElementsByTagName('member')[num].getElementsByTagName('name')[0].firstChild.nodeValue);
		var company = document.createTextNode(xmlDoc.getElementsByTagName('member')[num].getElementsByTagName('company')[0].firstChild.nodeValue);
		
		var addressTemp = xmlDoc.getElementsByTagName('member')[num].getElementsByTagName('address')[0].firstChild.nodeValue;
		var lineBreak = addressTemp.indexOf('%');
		var addLine1 = addressTemp.substring(0, lineBreak);
		var addLine2 = addressTemp.substring(lineBreak+1, addressTemp.length);
		
		var address = addLine1 + "<br />" + addLine2;
		
		var email = document.createTextNode(xmlDoc.getElementsByTagName('member')[num].getElementsByTagName('email')[0].firstChild.nodeValue);
		var phone = document.createTextNode("Phone: " + xmlDoc.getElementsByTagName('member')[num].getElementsByTagName('phone')[0].firstChild.nodeValue);
		var fax = document.createTextNode("Fax: " + xmlDoc.getElementsByTagName('member')[num].getElementsByTagName('fax')[0].firstChild.nodeValue);		
		
		var newRow = tbl.insertRow(tbl.rows.length);
		
		var curCell = newRow.insertCell(0);
		curCell.className = "name";
		curCell.appendChild(name);
		
		curCell = newRow.insertCell(1);
		curCell.className = "company";
		curCell.appendChild(company);
	
		newRow = tbl.insertRow(tbl.rows.length);
		
		curCell = newRow.insertCell(0);
		curCell.className = "email";
		curCell.appendChild(email);
		
		curCell = newRow.insertCell(1);
		curCell.rowSpan = "3";
		curCell.className = "address";
		curCell.innerHTML = address;	
		
		newRow = tbl.insertRow(tbl.rows.length);
		
		curCell = newRow.insertCell(0);
		curCell.className = "phone";
		curCell.appendChild(phone);
		
		newRow = tbl.insertRow(tbl.rows.length);
		
		curCell = newRow.insertCell(0);
		curCell.className = "fax";
		curCell.appendChild(fax);
	}
	
	
}

function search(){
	var where = document.getElementById('where').selectedIndex;
	if(where==1){where = "name";}
	if(where==2){where = "company";}
	if(where==3){where = "address";}
	var what = document.getElementById('criteria').value;
	var tbl = document.getElementById('tab');
	
	var num=0;
	var arry = xmlDoc.getElementsByTagName('member');
	if(where != 0){	
	removeAllRows(tbl);
	document.getElementById('heading').innerHTML = "Members with \'" + what + "\' in their " + where;
		for(; num < arry.length; num++){
			var temp = xmlDoc.getElementsByTagName('member')[num].getElementsByTagName(where)[0].firstChild.nodeValue.toLowerCase();
			what = what.toLowerCase();
			if(temp.indexOf(what) > -1){
				var name = document.createTextNode(xmlDoc.getElementsByTagName('member')[num].getElementsByTagName('name')[0].firstChild.nodeValue);
				var company = document.createTextNode(xmlDoc.getElementsByTagName('member')[num].getElementsByTagName('company')[0].firstChild.nodeValue);
			
				var addressTemp = xmlDoc.getElementsByTagName('member')[num].getElementsByTagName('address')[0].firstChild.nodeValue;
				var lineBreak = addressTemp.indexOf('%');
				var addLine1 = addressTemp.substring(0, lineBreak);
				var addLine2 = addressTemp.substring(lineBreak+1, addressTemp.length);
				
				var address = addLine1 + "<br />" + addLine2;
				
				var email = document.createTextNode(xmlDoc.getElementsByTagName('member')[num].getElementsByTagName('email')[0].firstChild.nodeValue);
				var phone = document.createTextNode("Phone: " + xmlDoc.getElementsByTagName('member')[num].getElementsByTagName('phone')[0].firstChild.nodeValue);
				var fax = document.createTextNode("Fax: " + xmlDoc.getElementsByTagName('member')[num].getElementsByTagName('fax')[0].firstChild.nodeValue);		
				
				var newRow = tbl.insertRow(tbl.rows.length);
						
				var curCell = newRow.insertCell(0);
				curCell.className = "name";
				curCell.appendChild(name);
					
				curCell = newRow.insertCell(1);
				curCell.className = "company";
				curCell.appendChild(company);
				
				newRow = tbl.insertRow(tbl.rows.length);
						
				curCell = newRow.insertCell(0);
				curCell.className = "email";
				curCell.appendChild(email);
					
				curCell = newRow.insertCell(1);
				curCell.rowSpan = "3";
				curCell.className = "address";
				curCell.innerHTML = address;	
				
				newRow = tbl.insertRow(tbl.rows.length);
						
				curCell = newRow.insertCell(0);
				curCell.className = "phone";
				curCell.appendChild(phone);
					
				newRow = tbl.insertRow(tbl.rows.length);
					
				curCell = newRow.insertCell(0);
				curCell.className = "fax";
				curCell.appendChild(fax);
			}
		}	
	}	
}

function removeAllRows(tbl){
	var rows = tbl.rows; 
	while(rows.length - 1)
		tbl.deleteRow(rows.length-1); 	
}
