/* load an existing document */
try
	{
    	 document_product_list = document.implementation.createDocument("","root",null);
  	}
catch(error)
  	{
  	 try  /* Microsoft insist on being fucktards with Internet Explorer; so I have to do this */
    	 	{
  		 document_product_list = new ActiveXObject("Microsoft.XMLDOM");
    		}
  	 catch(error) 
		{
	        document.write("This page did not load correctly. Here is the error: " + error.message);
	  	}
	}
 	
try
  	{
  	 document_product_list.async = false;
  	 document_product_list.load("../documents/pipes.xml");
        parse_products(document_product_list.getElementsByTagName("*"));
  	}
catch(error) 
	{
	 document.write("This document did not load correctly. Here is the error: " + error.message);
	}


function parse_products(product_list)
{
 /* I'm going to go through each product and display it's information; html style */
 var element_counter = 0;
 do
 {
  switch(product_list[element_counter].nodeName)
  {
   case "product_list":
    element_counter++;  
    break;
   case "product":
    document.write("<br />");
    element_counter++;  
    break;
   case "name":
    document.write(product_list[element_counter].childNodes[0].nodeValue + "<br /><br />");
    element_counter++;
    break;   
   case "image":
    document.write("<a href=" + product_list[element_counter].childNodes[0].nodeValue + " target=_blank>" + "<img src=" + product_list[element_counter].childNodes[0].nodeValue + " /></a> <br />");
    element_counter++;
    break;
   case "description":
    document.write(product_list[element_counter].childNodes[0].nodeValue + "<br /><br />");
    element_counter++;
    break;
   case "button":
    if(product_list[element_counter].hasChildNodes())
    {
     element_counter = assemble_button(product_list,element_counter);
     break;
    }
    element_counter++;
    break;    
   default:
    element_counter++;    
  }
 }
 while(element_counter < product_list.length)
}


function assemble_button(current_node,button_element_counter)
{
 var children;
 var child_count = 0;
 var current_child = 0;
 var grandchild_count = 0;
 var current_grandchild = 0;
 var attributes = [];
 var attribute_count = 0;
 var current_attrubute = 0;
 var depth = 0;
 var button_html = "";
 var closing_tag = "";

 if(current_node[button_element_counter].hasChildNodes())
 {
  children = current_node[button_element_counter].childNodes;
  child_count = children.length;

  for(current_child = 0;current_child < child_count;current_child++)
  {
   if(children[current_child].nodeType == 1)
   {
    button_html = button_html + "<" + children[current_child].nodeName;
    if(children[current_child].hasAttributes())
    {
     attributes = children[current_child].attributes;
     attribute_count = attributes.length;
     for(current_attribute = 0;current_attribute < attribute_count;current_attribute++)
      {
       button_html = button_html + " " + attributes[current_attribute].nodeName + "='" + attributes[current_attribute].nodeValue + "'";
      } 
    }
    switch(children[current_child].nodeValue)
    {
     case null:
      if(children[current_child].hasChildNodes())
      {
       button_html = button_html + ">";
       closing_tag = "</" + children[current_child].nodeName + ">";
       depth++;

       while(depth > 0)
       {
        children = children[current_child].childNodes;
        grandchild_count = children.length;
        for(current_grandchild = 0;current_grandchild < grandchild_count;current_grandchild++)
        {
         if(children[current_grandchild].nodeType == 1)
         {
          button_html = button_html + "<" + children[current_grandchild].nodeName;
          if(children[current_grandchild].hasAttributes())
          {
           attributes = children[current_grandchild].attributes;
           attribute_count = attributes.length;
           for(current_attribute = 0;current_attribute < attribute_count;current_attribute++)
           {
            button_html = button_html + " " + attributes[current_attribute].nodeName + "='" + attributes[current_attribute].nodeValue + "'";
           }
          }
          button_html = button_html + " />";
         } 
        }
        children = children[0].parentNode.parentNode.childNodes;
        depth--;
       }

       button_html = button_html + closing_tag;       
      }
      else
      {
      button_html = button_html + " />";       
      } 
      break;
     default:if(children[current_child].hasChildNodes())
      {
       button_html = button_html + ">";
       closing_tag = "</" + children[current_child].nodeName + ">";
       depth++;

       while(depth > 0)
       {
        children = children[current_child].childNodes;
        grandchild_count = children.length;
        for(current_grandchild = 0;current_grandchild < grandchild_count;current_grandchild++)
        {
         if(children[current_grandchild].nodeType == 1)
         {
          button_html = button_html + "<" + children[current_grandchild].nodeName;
          if(children[current_grandchild].hasAttributes())
          {
           attributes = children[current_grandchild].attributes;
           attribute_count = attributes.length;
           for(current_attribute = 0;current_attribute < attribute_count;current_attribute++)
           {
            button_html = button_html + " " + attributes[current_attribute].nodeName + "='" + attributes[current_attribute].nodeValue + "'";
           }
          }
          button_html = button_html + " />";
         } 
        }
        children = children[0].parentNode.parentNode.childNodes;
        depth--;
       }

       button_html = button_html + children[current_child].nodeValue + closing_tag;       
      }
      else
      {
      button_html = button_html + children[current_child].nodeValue + closing_tag;       
      }
    } 
   } 
  }
 }
 document.write(button_html);
 button_element_counter++;
 return button_element_counter;
}

