window.onload = loadXML;

var xmlDoc;

function loadXML(){
 //load xml file
 // code for IE
 if (window.ActiveXObject){
  xmlDoc=new ActiveXObject("Microsoft.XMLDOM");
  xmlDoc.async=false;
  xmlDoc.load("contacts.xml");
  setupXML();
 }
 // code for Mozilla, Firefox, Opera, etc.
  else if (document.implementation && document.implementation.createDocument){
   xmlDoc=document.implementation.createDocument("","",null);
   xmlDoc.load("contacts.xml");
   xmlDoc.async=false;
   xmlDoc.onload = setupXML;
  }else{
  alert('Your browser cannot handle this script');
 }
}
function setupXML(){
//set variables to equal the right data
company = xmlDoc.getElementsByTagName('name');
contact = xmlDoc.getElementsByTagName('contact');
state = xmlDoc.getElementsByTagName('state');
address = xmlDoc.getElementsByTagName('address');
city = xmlDoc.getElementsByTagName('city');
zip = xmlDoc.getElementsByTagName('zip');
telephone = xmlDoc.getElementsByTagName('telephone');
//cellphone = xmlDoc.getElementsByTagName('cellphone');
fax = xmlDoc.getElementsByTagName('fax');
control = xmlDoc.getElementsByTagName('control');
email = xmlDoc.getElementsByTagName('email');

//-----
//CREATE TABLE TO POPULATE WITH DATA
var divwrap = document.createElement('div');
divwrap.setAttribute('id','TableHolder');
//var newtable = document.createElement('table');

//----populate-----//
for(i=0;i<company.length;i++){
var tdRow = document.createElement('ul');	
//var tablerow = document.createElement('tr');
//var tdRow = document.createElement('td'); 
//
var tdName = document.createElement('li'); 
tdName.setAttribute('id','Company_'+i);
tdName.innerHTML = '<b>' + company[i].firstChild.nodeValue + '</b>';
tdName.setAttribute('float','left');
tdRow.appendChild(tdName);
//Contact
var tdName = document.createElement('li'); 
tdName.setAttribute('id','Contact_'+i);
tdName.innerHTML = '<b>' + contact[i].firstChild.nodeValue + '</b>';
tdName.setAttribute('float','left');
tdRow.appendChild(tdName);
//Address
var tdAmount = document.createElement('li');
tdAmount.setAttribute('id','Address_'+i);
tdAmount.innerHTML = address[i].firstChild.nodeValue;
tdRow.appendChild(tdAmount);
//City, State Zip
var tdAmount = document.createElement('li');
tdAmount.setAttribute('id','State_'+i);
tdAmount.innerHTML = city[i].firstChild.nodeValue + ' ' + state[i].firstChild.nodeValue + ', ' + zip[i].firstChild.nodeValue;
tdRow.appendChild(tdAmount);
//telephone
var tdAmount = document.createElement('li');
tdAmount.setAttribute('id','Telephone_'+i);
tdAmount.innerHTML = telephone[i].firstChild.nodeValue + ' (Ph)';
tdRow.appendChild(tdAmount);
//cellphone
//  var tdAmount = document.createElement('li');
//  tdAmount.setAttribute('id','Cell_'+i);
//  tdAmount.innerHTML = cellphone[i].firstChild.nodeValue;
//  tdRow.appendChild(tdAmount);
//} else {
//	alert("something")
//  var tdAmount = document.createElement('li');
//  tdAmount.setAttribute('id','Cell_'+i);
//  tdAmount.innerHTML = ' ';
//  tdRow.appendChild(tdAmount); 
//}
//fax
var tdAmount = document.createElement('li');
tdAmount.setAttribute('id','Fax_'+i);
tdAmount.innerHTML = fax[i].firstChild.nodeValue + ' (Fax)';
tdRow.appendChild(tdAmount);
//email
var tdAmount = document.createElement('li');
tdAmount.setAttribute('id','Email_'+i);
tdAmount.innerHTML = '<a href=mailto:' + email[i].firstChild.nodeValue + '>' + email[i].firstChild.nodeValue + '</a>';
tdRow.appendChild(tdAmount);
//control
var tdAmount = document.createElement('li');
tdAmount.setAttribute('id','Control_'+i);
tdAmount.innerHTML = control[i].firstChild.nodeValue;
tdAmount.style.visibility = "hidden";
tdRow.appendChild(tdAmount);
//put it all together
//tablerow.appendChild(tdRow);	
//tablebody.appendChild(tablerow);	
//newtable.appendChild(trRow);
divwrap.appendChild(tdRow);
}
//shabam onto the page
document.body.appendChild(divwrap)
}

