var BROWSER_INFO = getBrowserType();


/*
------------------------------------------
Function name  -  getBrowserType
Description - queries the user's BROWSER and sets properties in an object 
			  for a specific type of BROWSER look at the objects properties
------------------------------------------	
*/
function getBrowserType() 
{
	var myAgent = navigator.userAgent;
	var isMac = myAgent.toString().toUpperCase().indexOf("MAC") != -1;
	var isNS = myAgent.toString().toUpperCase().indexOf("MOZILLA") != -1;
	var isNS6 = myAgent.toString().toUpperCase().indexOf("NETSCAPE6/6") != -1;
	var isNS7 = myAgent.toString().toUpperCase().indexOf("NETSCAPE/7") != -1;
	var isMoz = ((parseInt(navigator.appVersion) >= 5) && myAgent.toString().toUpperCase().indexOf("NETSCAPE") == -1);
	var isFireFox = myAgent.toString().toUpperCase().indexOf("FIREFOX") != -1;
	

	this.dom = document.getElementById ? 1 : 0;
	this.fireFox = ((isFireFox) && (this.dom)) ? 1 : 0;
	this.ie = document.all ? 1 : 0;
	this.ns4 = document.layers ? 1 : 0;
	this.ns6 = ((!ie) && (isNS6)) ? 1 : 0;
	this.ns6plus = ((!ie) && (isNS) && (!this.ns4) ) ? 1 : 0;
	this.ns7 = ((!ie) && (isNS7)) ? 1 : 0;
	this.moz = ((!ie) && (isNS) && (isMoz)) ? 1 : 0;
	this.ns = ((!ie) && (isNS) && (!isNS6) && (!isNS7) && (!isMoz) ) ? 1 : 0;
	this.iemac = ((ie) && (isMac)) ? 1 : 0;
	this.isMac = isMac;
	return this;
}

/*
------------------------------------------
Function name  -  createMenuItem
Description - creates menu items
------------------------------------------	
*/
function createMenuItem(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');
		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.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){

			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){
						newUL1 = newUL1.cloneNode(true);
						newUL1.appendChild(li[0]);
					}
							newinnerDiv = newinnerDiv.cloneNode(true);
							newinnerDiv.appendChild(newUL1);

					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;

							var newUL1 = document.createElement('ul');
							
							var newUL2 = document.createElement('ul');
						
							var index=0;
							for(;index<halfway;++index){
								newUL1 = newUL1.cloneNode(true);
								newUL1.appendChild(li[0]);
							}
							
							newinnerDiv = newinnerDiv.cloneNode(true);
							newinnerDiv.appendChild(newUL1);
							index=li.length;
							for (;index>0;--index){
								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);
		document.getElementById('content').appendChild(newDiv);
		
	}
}

/*
------------------------------------------
Function name  -  layerVis
Description - show/hide div layers options: on,off
------------------------------------------	
*/
function layerVis(id,dir) {
   if (document.getElementById) {
        v = (dir == "on") ? 'visible' : 'hidden';
        document.getElementById(id).style.visibility = v;
    }
}

/*
------------------------------------------
Function name  -  findPosY
Description - finds the position of nav item when
			  user mouses over left nav
------------------------------------------	
*/
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 continueShopping(){
	var url	= document.cartgo.caturl.value;
	if(url =="" || url=="/category/"){
		url="/index.php";
	}
	document.location = url;
}


function continueShopping_mhhe(){
   var url="/mhhe_index.php";
        document.location=url;
}

/*
------------------------------------------
Function name  -  goToCart
Description - redirect to cart called by addToCart
------------------------------------------	
*/
function goToCart(){
	document.location=document.cartgo.carturl.value;
}

/*
------------------------------------------
Function name  -  addToCart
Description - calls add_item.php and passes 
			  isbn, adkey, and promo code, quantity is not used.
------------------------------------------	
*/
function _disabled_addToCart_old(isbn,pc,ak){
	//This exists only to provide a method for which an AJAX action will replace the current redirect to the cart.
	//initialize the count
	var countcurrent = document.getElementById("cartcurrentitems").innerHTML;
	//document.getElementById("cartcurrentitems").innerHTML=parseInt(countcurrent)+1;


	xmlHttp=GetXmlHttpObject();
	if (xmlHttp==null)
	{
	//document.location="/cart/add_item.php?isbn="+isbn;
		return false;
	}

	var url="/cart/add_item.php";
	url=url+"?isbn="+isbn;
	//url=url+"&quantity=1"
	if (ak) {url=url+"&adkey="+ak}
	if (pc) {url=url+"&promocode="+pc};
	url=url+"&nocache="+Math.random();
	xmlHttp.open("GET",url,true);
	xmlHttp.send(null);
	//redirecting to cart page when request successful
	xmlHttp.onreadystatechange=goToCart;
}

function addToCart(isbn,pc,ak){
	var url="/cart/add_item.php";
	url=url+"?isbn="+isbn;
	//url=url+"&quantity=1"
	if (ak) {url=url+"&adkey="+ak}
	if (pc) {url=url+"&promocode="+pc};
	url=url+"&nocache="+Math.random();
	document.location=url;
}

function mhhe_addToCart(isbn,pc,ak){
        var url="/mhhe_cart/add_item.php";
        url=url+"?isbn="+isbn;
        //url=url+"&quantity=1"
        if (ak) {url=url+"&adkey="+ak}
        if (pc) {url=url+"&promocode="+pc};
        url=url+"&nocache="+Math.random();
        document.location=url;
}

function esl_addToCart(isbn,pc,ak){
        var url="/esl_cart/add_item.php";
        url=url+"?isbn="+isbn;
        //url=url+"&quantity=1"
        if (ak) {url=url+"&adkey="+ak}
        if (pc) {url=url+"&promocode="+pc};
        url=url+"&nocache="+Math.random();
        document.location=url;
}


function addEbookToCart(isbn,pc,ak) {	
	for (i=0; i<document.ebook_formats.length; i++) {
		if (document.ebook_formats[i].checked) {
//			alert(isbn + document.ebook_formats[i].value);
			addToCart(isbn + document.ebook_formats[i].value,pc,ak);
			return false;
		}
	}
	
	alert ("Please select an ebook format.");
}

/*
------------------------------------------
Function name  -  saveItems
Description - calls save_item.php and passes isbn
------------------------------------------	
*/
function saveItems(isbn){

//initialize the count
var countcurrent = document.getElementById("cartsaveditems").innerHTML;
document.getElementById("cartsaveditems").innerHTML=parseInt(countcurrent)+1;

xmlHttp=GetXmlHttpObject();
if (xmlHttp==null)
{
return
} 
var url="/cart/save_item.php";
url=url+"?isbn="+isbn;
url=url+"&nocache="+Math.random();
xmlHttp.onreadystatechange=adjustCart;
xmlHttp.open("GET",url,true);
xmlHttp.send(null);

}

/*
------------------------------------------
Function name  -  GetXmlHttpObject
Description - checks for XML Http Object, AJAX
------------------------------------------	
*/
function GetXmlHttpObject()
{ 
	var objXMLHttp=null
	if (window.XMLHttpRequest)
	{
		objXMLHttp=new XMLHttpRequest()
	}
	else if (window.ActiveXObject)
	{
	objXMLHttp=new ActiveXObject("Microsoft.XMLHTTP")
	}
	return objXMLHttp
} 

/*
------------------------------------------
Function name  -  getPromo
Description - gets the promo code and submits the form
------------------------------------------	
*/
function getPromo()
{
	var promoCode = document.getElementById("promocode").value;
	//alert("value "+ promoCode);
	if (promoCode != "") document.promoform.submit();
	return true;
}


function MM_jumpMenu(targ,selObj,restore){ //v3.0
  
eval(targ+".location='"+selObj.options[selObj.selectedIndex].value+"'");
  if (restore) selObj.selectedIndex=0;
}

/* *****************************************************************************
	Popup Window (Survey)
******************************************************************************* */
function myLoader()
	{
		var popupWindow = document.getElementById("thePopup");
		var popupButton = document.getElementById("closePopup");
		var takeSurveyLink = document.getElementById("takesurvey");
		
		if (popupButton.addEventListener)
		{
			popupButton.addEventListener('click',fadeBox,false);
			takeSurveyLink.addEventListener('click',fadeBox,false);
		}
		else
		{
			//support for IE
			popupButton.attachEvent('onclick',fadeBox);
			takeSurveyLink.attachEvent('onclick',fadeBox);
		}
		if (getCookie('popup')=="hideit")
		{			
			popupWindow.style.display = "none";
		}
		else
		{
			popupWindow.style.display = "block";
		}
	}
	
	function fadeBox()
	{
		var popupBox = document.getElementById("thePopup");
		popupBox.style.display = "none";
		var currDate = new Date();
		currDate.setTime(currDate.getTime() + (1000 * 60 * 60 * 24 * 60));
		document.cookie = "popup=hideit;expires=" + currDate.toGMTString();
	}
	
	function getCookie(c_name)
	{
		if (document.cookie.length>0)
		{
			c_start=document.cookie.indexOf(c_name + "=");
			if (c_start!=-1)
			{
				c_start=c_start + c_name.length+1;
				c_end=document.cookie.indexOf(";",c_start);
				if (c_end==-1)
				{
					c_end=document.cookie.length;
				}
				return unescape(document.cookie.substring(c_start,c_end));
			}
		}
		return "";
	}
	
	function deleteCookie(c_name)
	{
		var cookie = getCookie(c_name);
		if (cookie)
		{
		var currDate = new Date();
		currDate.setTime(currDate.getTime() - (1000 * 60 * 60 * 24 * 60));
			document.cookie = c_name + "=nomorecookie;expires=" + currDate.toGMTString();
			alert("deleted " + cookie + " and will reload page automatically after you close this");
			window.location.reload(true);
		}
	}

/* *****************************************************************************
	Toggle on Detail page for More (meta) Information
******************************************************************************* */
	function toggleDisplay(theObj)
	{
		var currentDisplay = (document.getElementById(theObj));
		if (currentDisplay.style.display != 'block')
		{
			currentDisplay.style.display = 'block';
		}
		else
		{
			currentDisplay.style.display = 'none';
		}
		
	}