// JavaScript Document
function addLoadEvent(func) {
	//acknowledged use from DOM Scripting by J Keith
  var oldonload = window.onload;
  if (typeof window.onload != 'function') {
    window.onload = func;
  } else {
    window.onload = function() {
      oldonload();
      func();
    }
  }
}

function addelement(elementID)
	{	
		//check element exists
		
		if (!document.getElementById(elementID)) return false;
		// get all a tags
		 var content = document.getElementById("products");
  		 var links = content.getElementsByTagName("a");
		 
		 
		 // loop through until you find the one with the right class
		for (var i=0; i<links.length; i++ ) 
		{
			
			if (links[i].className.indexOf(elementID)) continue; // not the right class so try again 
			
			var divs=document.getElementsByTagName("div"); // look through divs in document
		
		//loop through divs 	
		for(var x=0;x<divs.length;x++)
			{
				
			// does this id match the one we are looking for?
			if (divs[x].getAttribute("id") == elementID) {
						
						  var newdiv = document.createElement("div");
						  var divIdName = "stock";
						  var container=GetParentNodeWithClassName(divs[x], 'productcontainer');
						  
						  if(!document.getElementById("stock"))
						  {
							  	newdiv.setAttribute("id",divIdName);
						  		newdiv.innerHTML = divs[x].innerHTML;
						  		//container.style.height='auto';
								container.appendChild(newdiv);
								
						  }
						 else
						 	{
								var throwaway=document.getElementById("stock");
								if (container.hasChildNodes()&& throwaway.parentNode==container)
									{
										container.removeChild(throwaway);
									}
								else
									{
										throwaway.parentNode.removeChild(throwaway);
									}
									
							}
						


		} 
					
				}
		}
			
}// end function
		
function GetParentNodeWithClassName( objNode, strClassName ){
			// Lowercase the class name for comparison.
			strClassName = strClassName.toLowerCase();
			
			// Crawl up the parent node chain. Keep crawling until we find the 
			// node with the proper class name, we hit a null node, or we hit 
			// a non-text node that has no tag name (the document object).
			for ( 
				objNode = objNode.parentNode ; 
				(
					objNode && (			
						(objNode.tagName && (objNode.className.toLowerCase() != strClassName)) ||
						(!objNode.tagName && (objNode.nodeType != 3)) 
					)
				);
				objNode = objNode.parentNode
				){
				// Nothing has to be done within in the FOR loop. We are purely
				// using the FOR loop to crawl up the DOM structure.
			}
		
			// Return the node. At this point, it might contains a valid
			// parent node, or it might be null.
			return( objNode );
		}
			

var sys_initiate =function ()
	{
	
	//is dom compatable 
	if (!document.getElementsByTagName) return false;
  	if (!document.getElementById) return false;	
		
		// identify the div holding the a tags
 var content = document.getElementById("products");
  var links = content.getElementsByTagName("a");

//go through each a tag
for (var i=0; i<links.length; i++ ) {
    var sectionId = links[i].className; // get its class name
    
	if (!document.getElementById(sectionId))continue; // check if corresponding div
   	 document.getElementById(sectionId).style.display = "none"; // make sure it is not displayed
	 
    links[i].destination = sectionId; // set the link destination
    links[i].onclick = function() {
     
	 addelement(this.destination); // assign function to onclick event 
      return false;
    }
  }
		
}
function insertAfter(newElement,targetElement) {
  var parent = targetElement.parentNode;
  if (parent.lastChild == targetElement) {
    parent.appendChild(newElement);
  } else {
    parent.insertBefore(newElement,targetElement.nextSibling);
  }
}
addLoadEvent(sys_initiate);
