function getHTTPObject() {
  var xhr = false;
  if (window.XMLHttpRequest) {
    xhr = new XMLHttpRequest();
  } else if (window.ActiveXObject) {
    try {
      xhr = new ActiveXObject("Msxml2.XMLHTTP");
    } catch(e) {
      try {
        xhr = new ActiveXObject("Microsoft.XMLHTTP");
      } catch(e) {
        xhr = false;
      }
    }
  }
  return xhr;
}

function grabFile(file) {
  var request = getHTTPObject();
  if (request) {
	displayLoading(document.getElementById("details"));
    request.onreadystatechange = function() {
      parseResponse(request);
    };
    request.open("GET", file, true);
    request.send(null);
  }
}

function displayLoading(element) {
  while (element.hasChildNodes()) {
    element.removeChild(element.lastChild);
  }
  var image = document.createElement("img");
  image.setAttribute("src","loading.gif");
  image.setAttribute("alt","Loading...");
  element.appendChild(image);
}

function parseResponse(request) {
	if (request.readyState == 4) {
    	if (request.status == 200 || request.status == 304) {
       
	  	var data = request.responseXML;
		var children = data.getElementsByTagName("row");
	  	var totalChildNodes = children.length;
	 	//alert(totalChildNodes);
		
		//Catch the error if the XML file is empty
		if (totalChildNodes == 0) {
			
			//We'll create a p tag and add some text
			var altContent = document.createElement("p");
			var message = "Sorry, no albums at the moment.";
			var newText = document.createTextNode(message);
			altContent.appendChild(newText);
			
			//Wipe any content from the details div
			var details = document.getElementById("details");
			while (details.hasChildNodes()) {
				details.removeChild(details.lastChild);
			  }
			 //Write our apologies
			details.appendChild(altContent);
		
		} else { // XML File has content - Good News! Let's continue.
		
		//Create the list element to hold our list items
		var ourlist = document.createElement("ul");
		
		//Loop through the XML 
		for (i = 0; i < totalChildNodes; i++) {
			//Assign the content to variables
			var ourid = data.getElementsByTagName("theid")[i].firstChild.nodeValue;
		 	var ourname = data.getElementsByTagName("thename")[i].firstChild.nodeValue;
		 	var ourlink = "music.php?id_gms="+ourid;
			
			//Create a list item
			var content = document.createElement("li");
			
			//Create an anchor element
			var ourhref = document.createElement("a");
			//Set a href to the anchor
			ourhref.setAttribute("href",ourlink);
			
			//Create and append a text node to the href to act as text for the anchor tag
			var text = document.createTextNode(ourname);
			ourhref.appendChild(text);
			 
			//Append our anchor tag to the LI
			content.appendChild(ourhref);
			
			//Append our LI to the UL
			ourlist.appendChild(content);
			
			//if there any more children - loop again, adding LI's to UL's as we go
		} 
	  //Clear the current data
      var details = document.getElementById("details");
      while (details.hasChildNodes()) {
     	details.removeChild(details.lastChild);
	  }
	  
	  //Write our complete list to our details div
      details.appendChild(ourlist);
	  
    } 
	//Dude, our work is done here! - I thought that would make you feel at home :)
	}

  }
}