
function createMenuItem(fl){
//arrayTest(fl);


	var x=document.getElementById(fl);
	var newID = "f"+fl;
	var parID = "t"+fl;
	
	
	if(document.getElementById(newID)){
		//if the layer is already in the dom turn it on.
		layerVis(newID,'on');
	}
	else{
	
		//create layer and process lists.
		var li=x.getElementsByTagName('LI');
		//alert(li.length);	
		var twoColth = 18;//18
		var threeColth = 36;//36
		
		//create wrapper divs
		var newDiv = document.createElement('div');
		newDiv.setAttribute('id',newID);
		var funon = "layerVis(\'"+newID+"\",\"on\")";
		var funoff = "layerVis(\'"+newID+"\",\"off\")";
		
		//newDiv.setAttribute('onmouseover',"layerVis('"+newID+"','on')");
		//newDiv.onmouseover=funon;

		newDiv.className="flyoutnav";
		newDiv.style.top = findPosY(document.getElementById(parID))+"px";
		
		
		
		var newinnerDiv = document.createElement('div');
		if (document.all) {
		//for IE DOM implementations
    		newinnerDiv =document.createElement('<div onmouseover="layerVis(\''+newID+'\',\'on\')" onmouseout="layerVis(\''+newID+'\',\'off\')"/>');
		  } 
		  else {
			newinnerDiv.setAttribute('onmouseover','layerVis(\"'+newID+'\",\"on\")');
			newinnerDiv.setAttribute('onmouseout','layerVis(\"'+newID+'\",\"off\")');
			}
		
		newinnerDiv.className="flyoutnavinner";
		newinnerDiv.style.pixelTop=0;
				var divwidth = 173;
		//look for 4th level children???
	var endofline = li.length;
		if(endofline >=twoColth){
			if(endofline >= threeColth){
			//alert(x.innerHTML);
			divwidth=493;
					var midlength = Math.ceil(endofline/3);
					var midlength2 = midlength*2;
					var newUL1 = document.createElement('ul');
					var newUL2 = document.createElement('ul');
					var newUL3 = document.createElement('ul');
					var index=0;
				
					for(;index<midlength;++index){
					//alert(index+" "+midlength+" "+li.length);
						newUL1 = newUL1.cloneNode(true);
						newUL1.appendChild(li[0]);
					}
							newinnerDiv = newinnerDiv.cloneNode(true);
							newinnerDiv.appendChild(newUL1);
					
					//alert(index+" "+midlength2+" "+li.length);
					for (;index<midlength2;++index){
					
						newUL2 = newUL2.cloneNode(true);
						newUL2.appendChild(li[0]);
						
					}
					
					newinnerDiv = newinnerDiv.cloneNode(true);
					newinnerDiv.appendChild(newUL2);
					
					index =li.length;
					for (;index>0;--index){
					
						newUL3 = newUL3.cloneNode(true);
						newUL3.appendChild(li[0]);
					}
					newinnerDiv = newinnerDiv.cloneNode(true);
					newinnerDiv.appendChild(newUL3);
					

			}
			else{
			//begin 2 col
			var halfway = Math.ceil(endofline/2);
			divwidth=333;
							//alert(divwidth);
							//alert(halfway+" "+endofline);
							var newUL1 = document.createElement('ul');
							
							var newUL2 = document.createElement('ul');
						
							var index=0;
							for(;index<halfway;++index){
								//alert(index+" "+halfway+" "+li.length);
								newUL1 = newUL1.cloneNode(true);
								newUL1.appendChild(li[0]);
								
							}
							
							newinnerDiv = newinnerDiv.cloneNode(true);
							newinnerDiv.appendChild(newUL1);
							index=li.length;
							for (;index>0;--index){
								//alert(index+" "+endofline+" "+li.length);
								newUL2 = newUL2.cloneNode(true);
								newUL2.appendChild(li[0]);
							}
							newinnerDiv = newinnerDiv.cloneNode(true);
							newinnerDiv.appendChild(newUL2);
			//end 2 col
			}
		}
		else{ 
		
			newinnerDiv.appendChild(x);
			
		}
		
		newDiv.style.width = divwidth+"px";
		newinnerDiv.style.width = "100%";
		
		//attach clear div
		var clearDiv = document.createElement('div');
		clearDiv.className="clear";
		
		
		newinnerDiv = newinnerDiv.cloneNode(true);
		newinnerDiv.appendChild(clearDiv);
		//drop new content in page
		newDiv.appendChild(newinnerDiv);
		//alert(newDiv.innerHTML);
		document.getElementById('content').appendChild(newDiv);
		
	}
}

function layerVis(id,dir) {
   if (document.getElementById) {
        v = (dir == "on") ? 'visible' : 'hidden';
        document.getElementById(id).style.visibility = v;
    }
}

function findPosY(obj)
{
	var curtop = 0;
	if (obj.offsetParent)
	{
		while (obj.offsetParent)
		{
			curtop += obj.offsetTop
			obj = obj.offsetParent;
		}
	}
	else if (obj.y)
		curtop += obj.y;
	return curtop;
}
function adjustCart(){

var allcookies = document.cookie;
var pos_current = allcookies.indexOf("cartcurrent");
var pos_saved = allcookies.indexOf("cartsaved");

//initialize the count
var countsaved = 0;
var countcurrent = 0;

if(pos_current!=-1){
	var start = pos_current+12;
	var end = allcookies.indexOf(";",start);
	if(end==-1) end = allcookies.length;
	var value = allcookies.substring(start,end);
	value = unescape(value);
	countcurrent = value;
}
if(pos_saved!=-1){
	var start = pos_saved+10;
	var end = allcookies.indexOf(";",start);
	if(end==-1) end = allcookies.length;
	var value = allcookies.substring(start,end);
	value = unescape(value);
	countsaved = value;
}
//count saved

//count current

document.getElementById("cartsaveditems").innerHTML = countsaved;
document.getElementById("cartcurrentitems").innerHTML = countcurrent;
}

function addToCart(isbn){
var allcookies = document.cookie;
var pos_current = allcookies.indexOf("cartcurrent");

//initialize the count
var countcurrent = 0;

if(pos_current!=-1){
	var start = pos_current+12;
	var end = allcookies.indexOf(";",start);
	if(end==-1) end = allcookies.length;
	var value = allcookies.substring(start,end);
	value = unescape(value);
	countcurrent = value;
}
countcurrent = parseInt(countcurrent)+1;
document.cookie = "cartcurrent="+escape(countcurrent)+"; path=/";

xmlHttp=GetXmlHttpObject();
if (xmlHttp==null)
{
//alert ("Browser does not support HTTP Request")
//document.location = "/addtocart.php?isbn="+isbn+"&quantity=1";
return
} 
var url="/addtocart.php";
url=url+"?isbn="+isbn;
url=url+"&quantity=1"
url=url+"&nocache="+Math.random();
xmlHttp.onreadystatechange=adjustCart;
xmlHttp.open("GET",url,true);
xmlHttp.send(null);

//adjustCart();
}

function GetXmlHttpObject()
{ 
	var objXMLHttp=null
	if (window.XMLHttpRequest)
	{
		objXMLHttp=new XMLHttpRequest()
	}
	else if (window.ActiveXObject)
	{
	objXMLHttp=new ActiveXObject("Microsoft.XMLHTTP")
	}
	return objXMLHttp
} 
