function getDimensions() {		
	if (document.all) {document.body.scroll='auto';}
	var pageBody = document.getElementById("pagebody");
	var wrapped = document.getElementById("wrapped");
	var theheight = pageBody.clientHeight;
	var thewidth = pageBody.clientWidth;
	wrapped.style.height = theheight + "px";
	wrapped.style.width = thewidth + "px";

	var closeOverlayHeight = theheight - 2;
	var closeOverlayWidth = thewidth - 2;

	wrapped.style.height = theheight + "px";

	var overlayTop = (closeOverlayHeight - 360) / 2;
	var overlayLeft = (closeOverlayWidth - 340) / 2;
	var closeOverLay = document.getElementById("closeoverlay");
	if (closeOverLay) {
		var overLay = document.getElementById("overlay");

		closeOverLay.style.height = closeOverlayHeight + "px";
		closeOverLay.style.width = closeOverlayWidth + "px";

		overLay.style.top = overlayTop + "px";
		overLay.style.left = overlayLeft + "px";
	}
	if (document.all) {document.body.scroll='auto';}
}


function addToCart() {

	document.getElementById("overlay-midshell").style.height = "120px";
	document.getElementById("overlay-midshellin").style.height = "120px";
	document.getElementById("overlay-inshell").style.height = "120px";
	document.getElementById("overlay-contents").innerHTML = document.getElementById("overlay-contents-added").innerHTML;
	openPop();
}


function openPop() {
	displayItem("closeoverlay");
	displayItem("overlay");
}

function closePopConfirm() {
	if (confirm("Would you like to close the overlay window?")) {	closePop(); }
}

function closePop() {
	hideItem("closeoverlay");
	hideItem("overlay");	
	document.getElementById("overlay-contents").innerHTML = "";
}

/* */
function hideBDropItem(theid) {
	hideItem(theid);
	changeClass(theid + '-launch','bdrop-link');
}

function displayBDropItem(theid) {
	displayItem(theid);
	changeClass(theid + '-launch','bdrop-link bdrop-link-active');
}

function hideNavDrop(theid) {
	hideItem(theid);
	changeClass(theid + '-launch','navdrop-link');
}
function displayNavDrop(theid) {
	displayItem(theid);
	changeClass(theid + '-launch','navdrop-link navdrop-link-active');
}

function hideTechDrop(theid) {
	hideItem(theid);
	changeClass(theid + '-launch','tdrop-link');
}

function displayTechDrop(theid) {
	displayItem(theid);
	changeClass(theid + '-launch','tdrop-link tdrop-link-active');
}

function clearSearch(theid) {	if (theid.value == "Search keyword") { theid.value = ""; } }

function dropDestroyer() {
	if (document.getElementById("navdrop-home")) {hideNavDrop("navdrop-home");}
	if (document.getElementById("navdrop-ind")) {hideNavDrop("navdrop-ind");}
	if (document.getElementById("bdrop-profiles")) {hideBDropItem("bdrop-profiles");}
	if (document.getElementById("bdrop-resources")) {hideBDropItem("bdrop-resources");}
	if (document.getElementById("tdrop-tech")) {hideTechDrop("tdrop-tech");}
}

function manageDetailPanel(theon) {
	changeClass('dpanel-tab-authors','');
	changeClass('dpanel-tab-reviews','');
	changeClass('dpanel-tab-share','');
	changeClass('dpanel-tab-' + theon,'panelon');
	hideItem('dpanel-authors');
	hideItem('dpanel-reviews');
	hideItem('dpanel-share');
	displayItem('dpanel-' + theon);
}

/* SEARCH */
function manageLeftPanelSearch(theval) {
	if (theval == "BOOK") {
		displayItem("search-criteria-book");
		hideItem("search-criteria-sae");
	} else {
		hideItem("search-criteria-book");
		displayItem("search-criteria-sae");
	}
}


/* LIBRARY */
function manageLibCollapse(theact,theid) {
	if (theact) {
		displayItem('dlib' + theid + '-chapters');
		hideItem('dlib' + theid + '-plus');
		displayItem('dlib' + theid + '-minus');
		displayItem('dlib' + theid + '-ch-exp');
		hideItem('dlib' + theid + '-ch-coll');
	} else {
		hideItem('dlib' + theid + '-chapters');
		displayItem('dlib' + theid + '-plus');
		hideItem('dlib' + theid + '-minus');
		hideItem('dlib' + theid + '-ch-exp');
		displayItem('dlib' + theid + '-ch-coll');
	}
}

function manageUnBought(theact,theid) {
	if (theact) {
		changeClass('dlib' + theid + '-chapters',"");
		displayItem('dlib' + theid + '-sh-purch');
		hideItem('dlib' + theid + '-sh-all');
	} else {
		changeClass('dlib' + theid + '-chapters',"gdl-hide-buyit");
		hideItem('dlib' + theid + '-sh-purch');
		displayItem('dlib' + theid + '-sh-all');
	}
}


function downloadItem(prodCd,prc,epub,pdf) {
	document.getElementById("overlay-midshell").style.height = "185px";
	document.getElementById("overlay-midshellin").style.height = "185px";
	document.getElementById("overlay-inshell").style.height = "185px";
	var sdf = document.getElementById("overlay-contents");
	if(prc!="" ||epub !="" ||pdf !="") {
		var a ='<strong>Select a Format to Download</strong><div class="overlay-select"><table>';

		if(pdf!="")	{
			var a = a + buildTableRow(pdf,'apub','PDF',prodCd);
		}
		if(epub!="") {
			var a = a + buildTableRow(epub,'epub','eBook',prodCd);
		}
		if(prc!="") {
			var a = a + buildTableRow(prc,'mpub','Mobi (.prc)',prodCd);
		}
		var a = a + '</table>';
		sdf.innerHTML =sdf.innerHTML + a;
	} else {
		sdf.innerHTML =sdf.innerHTML + '<strong>No ebook type available for downlaod</strong>';
	}
	openPop();
}

function downloadFail() {
	alert("Item needs to be purchased before it is available to download.");
}

function launchUpdate(theitem) {
	document.getElementById("overlay-midshell").style.height = "205px";
	document.getElementById("overlay-midshellin").style.height = "205px";
	document.getElementById("overlay-inshell").style.height = "205px";
	document.getElementById("overlay-contents").innerHTML = document.getElementById("overlay-contents-updated").innerHTML;
	openPop();
}

function buildTableRow(isbn,type, name,prodCd) {
	var row = '<tr><td class="gleft"><div class="overlay-select-format">'+name+'</div><div class="overlay-select-isbn">ISBN:'+ isbn +'</div></td><td class="gright"><div class="ordersmbtn"><div><a target="_blank" href="/servlets/bookstore/downloadMyItem.do?method=downloadMyItem&prodCd='+prodCd+'&pubType='+type+'">Select</a></div></div></td></tr>';
	return row;
}


function selectPurchaseType(e,bookId,prc_prntBook,isbn_pdf,isbn_epub,isbn_mobi,prc_eBook,printFormatkAvl,eBookFormatAvl,onlineFormatkAvl,softwareFormatkAvl) {
	try { if (!e) e = window.event;   e.returnValue = false; e.preventDefault();  } catch( ex ) { /* do Nothing */ }

	//prc_eBook = prc_eBook.replace(/<.*?>/, '');
	//prc_eBook = prc_eBook.replace(/<.*?>/, '');

	//prc_prntBook = prc_prntBook.replace(/<.*?>/, '');
	//prc_prntBook = prc_prntBook.replace(/<.*?>/, '');

	document.getElementById("overlay-midshell").style.height = "320px";
	document.getElementById("overlay-midshellin").style.height = "320px";
	document.getElementById("overlay-inshell").style.height = "320px";
	document.getElementById("overlay-contents").innerHTML = buildSelectPurchaseTypeInnerHTML(bookId,prc_prntBook,isbn_pdf,isbn_epub,isbn_mobi,prc_eBook,printFormatkAvl,eBookFormatAvl,onlineFormatkAvl,softwareFormatkAvl);

	openPop();
}

	/** return function hook for shopping cart*/
	function ebookPurchase() {

		document.getElementById("overlay-midshell").style.height = "300px";
		document.getElementById("overlay-midshellin").style.height = "300px";
		document.getElementById("overlay-inshell").style.height = "300px";
		var strg = '<div id="overlay-contents-ebook" class="overlay-resource1"><strong>eBook Added to Shopping Cart</strong>'+
				   '<div class="overlay-select-heading"><span>You will have access to all of the below eBook Downloadable Formats after purchase</span></div>'+
				   '<div class="overlay-select"><table>';

		if(isbnPdf!=null && isbnPdf!='')
		{
				   strg = strg+'<tr><td class="gleft"><div class="overlay-select-format">PDF</div>'+
				   '<div class="overlay-select-isbn">ISBN: '+isbnPdf+'</div></td><td class="gright"></td></tr>';
		}
		if(isbnEpub!=null && isbnEpub!='')
		{
				   strg = strg+'<tr><td class="gleft"><div class="overlay-select-format">eBook</div>'+
				   '<div class="overlay-select-isbn">ISBN: '+isbnEpub+'</div></td><td class="gright"></td></tr>';
		}
		if(isbnMobi!=null && isbnMobi!='')
		{
				   strg = strg+'<tr><td class="gleft"><div class="overlay-select-format">Mobi (.prc)</div>'+
				   '<div class="overlay-select-isbn">ISBN: '+isbnMobi+'</div></td><td class="gright"></td></tr>';
		}

	   strg = strg+'</table></div>'+
		'<div class="overlay-shopcart">'+
		'<table><tr>'+
		'<td class="gleft"><div class="ordersmbtn"><div><a href="javascript:closePop();">Continue Browsing</a></div></div></td>'+
		'<td class="gright"><div class="ordersmbtn"><div><a href="/servlets/MaintainCartServlet?REQUEST_TYPE=MaintainCart">Proceed to Shopping Cart</a></div></div></td></tr>'+
		'</table></div>'+
		'<div class="overlay-ebook-msg">*  Downloadable anytime, after purchase, from <a href="/servlets/bookstore/library.do">eLibrary</a>.</div>'+
		'</div>';
		document.getElementById("overlay-contents").innerHTML = strg;
		openPop();
	}


function printFormatPurchase(){formatPurchase('Print');}
function onlineFormatPurchase(){formatPurchase('Online');}
function softwareFormatPurchase(){formatPurchase('Software');}
function downloadFormatPurchase(){formatPurchase('Download');}
function mailFormatPurchase(){formatPurchase('Mail');}
function faxFormatPurchase(){formatPurchase('FAX');}


function formatPurchase(formatType) {
document.getElementById("overlay-midshell").style.height = "140px";
document.getElementById("overlay-midshellin").style.height = "140px";
document.getElementById("overlay-inshell").style.height = "140px";
var strg ='<div id="overlay-contents-ebook" class="overlay-resource1"><strong>'+formatType+' Format Added to Shopping Cart</strong>'+
	'<div class="overlay-shopcart">'+
	'<table><tr>'+
	'<td class="gleft"><div class="ordersmbtn"><div><a href="javascript:closePop();">Continue Browsing</a></div></div></td>'+
	'<td class="gright"><div class="ordersmbtn"><div><a href="/servlets/MaintainCartServlet?REQUEST_TYPE=MaintainCart">Proceed to Shopping Cart</a></div></div></td>'+
	'</tr></table></div></div>';

document.getElementById("overlay-contents").innerHTML = strg;//buildPrintbookPurchaseInnerHTML();
openPop();


}



function printbookPurchase() {
document.getElementById("overlay-midshell").style.height = "140px";
document.getElementById("overlay-midshellin").style.height = "140px";
document.getElementById("overlay-inshell").style.height = "140px";
var strg ='<div id="overlay-contents-ebook" class="overlay-resource1"><strong>Print Book Added to Shopping Cart</strong>'+
	'<div class="overlay-shopcart">'+
	'<table><tr>'+
	'<td class="gleft"><div class="ordersmbtn"><div><a href="javascript:closePop();">Continue Browsing</a></div></div></td>'+
	'<td class="gright"><div class="ordersmbtn"><div><a href="/servlets/MaintainCartServlet?REQUEST_TYPE=MaintainCart">Proceed to Shopping Cart</a></div></div></td>'+
	'</tr></table></div></div>';

document.getElementById("overlay-contents").innerHTML = strg;//buildPrintbookPurchaseInnerHTML();
openPop();
}

var isbnPdf,isbnEpub,isbnMobi;
function buildSelectPurchaseTypeInnerHTML(bookId,prc_prntBook,isbn_pdf,isbn_epub,isbn_mobi,prc_eBook,printFormatkAvl,eBookFormatAvl,onlineFormatkAvl,softwareFormatkAvl)
{
isbnPdf=isbn_pdf;isbnEpub=isbn_epub;isbnMobi=isbn_mobi;

	var innerHtml = '<!-- ,start -->';//<div id="overlay-contents-selectpurch" class="overlay-resource">
	innerHtml = innerHtml+'<strong>Select Delivery Method</strong>';
	innerHtml = innerHtml+'<div class="overlay-selectdel"><table>';
	if(eBookFormatAvl!=null && eBookFormatAvl=='Y')
	{
		innerHtml = innerHtml+'<tr><td class="selectdel-title">eBook</td>';
		innerHtml = innerHtml+'<td class="selectdel-price">'+prc_eBook+'</td>';
		innerHtml = innerHtml+'<td class="selectdel-cart"><div class="ordersmbtn"><div><a href="#" onclick="';
		innerHtml = innerHtml+'addToCart(event,\'/servlets/MaintainCartServlet?REQUEST_TYPE=AddItem&ITEM_ID='+bookId+'&DELIV_CD=DNL&PROD_TYP=BOOK&RENDER_TYP=JSON\',ebookPurchase);';
		innerHtml = innerHtml+'">Add to Cart</a></div></div></td></tr>';

		innerHtml = innerHtml+'<tr><td class="selectdel-group" colspan=3>';
		innerHtml = innerHtml+'<div class="overlay-select-heading">eBook Downloadable Formats</div>';
		innerHtml = innerHtml+'<div class="overlay-select">';
		innerHtml = innerHtml+'<table>';
		if(isbn_pdf!=null && isbn_pdf!='')
		{
			innerHtml = innerHtml+'<tr><td class="gleft"><div class="overlay-select-format">PDF</div>';
			innerHtml = innerHtml+'<div class="overlay-select-isbn">ISBN: '+isbn_pdf+'</div>';
			innerHtml = innerHtml+'</td><td class="gright"></td></tr>';
		}
		if(isbn_epub!=null && isbn_epub!='')
		{
			innerHtml = innerHtml+'<tr><td class="gleft">';
			innerHtml = innerHtml+'<div class="overlay-select-format">EPUB</div>';
			innerHtml = innerHtml+'<div class="overlay-select-isbn">ISBN: '+isbn_epub+'</div>';
			innerHtml = innerHtml+'</td><td class="gright"></td></tr>';
		}
		if(isbn_mobi!=null && isbn_mobi!='')
		{
			innerHtml = innerHtml+'<tr><td class="gleft">';
			innerHtml = innerHtml+'<div class="overlay-select-format">Mobi (.prc)</div>';
			innerHtml = innerHtml+'<div class="overlay-select-isbn">ISBN: '+isbn_mobi+'</div>';
			innerHtml = innerHtml+'</td><td class="gright"></td></tr>';
		}
		innerHtml = innerHtml+'</table></div></td></tr>';
	}

	if(printFormatkAvl!=null && printFormatkAvl=='Y')
	{
		innerHtml = innerHtml+'<tr><td class="selectdel-title">Print Format</td>';
		innerHtml = innerHtml+'<td class="selectdel-price">'+prc_prntBook+'</td>';
		innerHtml = innerHtml+'<td class="selectdel-cart"><div class="ordersmbtn"><div><a href="#" onclick="';
		innerHtml = innerHtml+'addToCart(event,\'/servlets/MaintainCartServlet?REQUEST_TYPE=AddItem&ITEM_ID='+bookId+'&DELIV_CD=PST&PROD_TYP=BOOK&RENDER_TYP=JSON\',printbookPurchase);';
		innerHtml = innerHtml+'">Add to Cart</a></div></div></td></tr>';
	}
	if(onlineFormatkAvl!=null && onlineFormatkAvl=='Y')
	{
		innerHtml = innerHtml+'<tr><td class="selectdel-title">Online Format</td>';
		innerHtml = innerHtml+'<td class="selectdel-price">'+prc_eBook+'</td>';
		innerHtml = innerHtml+'<td class="selectdel-cart"><div class="ordersmbtn"><div><a href="#" onclick="';
		innerHtml = innerHtml+'addToCart(event,\'/servlets/MaintainCartServlet?REQUEST_TYPE=AddItem&ITEM_ID='+bookId+'&DELIV_CD=DNL&PROD_TYP=BOOK&RENDER_TYP=JSON\',onlineFormatPurchase);';
		innerHtml = innerHtml+'">Add to Cart</a></div></div></td></tr>';
	}
	if(softwareFormatkAvl!=null && softwareFormatkAvl=='Y')
	{
		innerHtml = innerHtml+'<tr><td class="selectdel-title">Software Format</td>';
		innerHtml = innerHtml+'<td class="selectdel-price">'+prc_prntBook+'</td>';
		innerHtml = innerHtml+'<td class="selectdel-cart"><div class="ordersmbtn"><div><a href="#" onclick="';
		innerHtml = innerHtml+'addToCart(event,\'/servlets/MaintainCartServlet?REQUEST_TYPE=AddItem&ITEM_ID='+bookId+'&DELIV_CD=PST&PROD_TYP=BOOK&RENDER_TYP=JSON\',softwareFormatPurchase);';
		innerHtml = innerHtml+'">Add to Cart</a></div></div></td></tr>';
	}
	innerHtml = innerHtml+'</table></div></div>';
	innerHtml = innerHtml+'<!-- ,end -->';
	return innerHtml;
}


	/** Methods moved from open.jsp */
    function trim(str) {   if(str == null) return '';   return str.replace(/^\s*/, '').replace(/\s*$/, ''); }

    /** add-2-cart js function.*/
    function addToCart(e,_url_, fnDialogPtr) {	
      try { if (!e) e = window.event;   e.returnValue = false; e.preventDefault();  } catch( ex ) { /* do Nothing */ }
      updateCart(_url_,'AddItem',fnDialogPtr);
      return false;
    }
    /** update shopping cart */
    function updateCart(_url_,requestType, fnDialogPtr) {
       _url_ = makeAjaxURL(_url_);    
      var xhttp = xhr(); 
     
      xhttp.open("GET", _url_,true);         
      xhttp.onreadystatechange=function() {
          if (xhttp.readyState==4) { 
          	handleResponse(xhttp.responseText, requestType, fnDialogPtr);        	
          }
      }
      xhttp.send(null);
    }

    /** creates dyna url for all sub-domains of sae.org (Bug# 1160) */
    function makeAjaxURL( _url_ ) {
        var curl = window.location.protocol+"//"+ window.location.hostname;
        if( trim(window.location.port) != "" )
             curl = curl + ":" + window.location.port;
        if( window.location.hostname.indexOf("books") > 0 ) {
             _url_ = "/shoppingcart"+_url_.substring(_url_.indexOf("?"), _url_.length)
        }
        
        return curl + _url_;
    }

    /** Handle json request. */
    function handleResponse( jsonString, requestType , fnDialogPtr) {   	
       
        var json = evalJSON(jsonString); 
        
        if( json.errorcode != null && trim(json.errorcode) != "null" &&
            json.error != null && trim(json.error) != "null"  ){
        	closePop();
            alert(" Shopping Cart Exception::\n "+ json.errorcode +" \n "+ json.error );
        } else {        
           /* document.getElementById("cart_items").innerHTML = json.item; */
            if( requestType == 'AddItem') {          
                closePop();
                if (typeof fnDialogPtr == "function") {
                    try {                   	  
                    	   fnDialogPtr();
                    } catch( ex ) { alert(" Wrong function Pointer Passed "); }
                }
                else
                    alert(" Item added successfully to the shopping cart. ");
            }
        }
    }
    /** Gets the Xhttp Transport layer */
    function xhr(){
        return window.ActiveXObject ? new ActiveXObject("Microsoft.XMLHTTP") : new XMLHttpRequest();
    }
    /** do json eval */
    function evalJSON(jsonString) {
        try {       	
            return eval("("+jsonString+")");
         } catch(e){
               alert(e);
               return null
         }
    }

/** Moved from index.jsp */
	function addFlashToCart(bookId,memberPrc,listPrc,
		                isbnPdf, isbnEPub, isbnPrc, isbnPrint,issnOnline,issnSw,
		                mdlprc,ldlprice,
		                print_avail, ebook_avail, online_avail, software_avail,
		                priceType)
	{
	  memberPrc = memberPrc == 0 ? "" : memberPrc;
	  listPrc = listPrc == 0 ? "" : listPrc;
	  mdlprc = mdlprc == 0 ? "" : mdlprc;
	  ldlprice = ldlprice == 0 ? "" : ldlprice;
	  if( priceType == 'MBR') {  selectPurchaseType(null,bookId,memberPrc,isbnPdf,isbnEPub,isbnPrc,mdlprc,print_avail, ebook_avail, online_avail, software_avail) ;
	  } else {	  selectPurchaseType(null,bookId,listPrc,isbnPdf,isbnEPub,isbnPrc,ldlprice,print_avail, ebook_avail, online_avail, software_avail) ;  }
	}

	/** Moved from techProfLandingPage.jsp && searchresults.jsp */
	function showXML() { window.open('/servlets/bookstore/bookListAction.do?operation=bookListXML'); }

	/** Moved from bookDetails.jsp  */
	function manageAddToCart(e,prodCode,formatType,delTyp,isbn_pdf,isbn_epub,isbn_mobi)
	{
		var shoppingCartUrl = "/servlets/MaintainCartServlet?REQUEST_TYPE=AddItem&ITEM_ID="+prodCode+"&DELIV_CD="+delTyp+"&PROD_TYP=BOOK&RENDER_TYP=JSON";
		//alert(shoppingCartUrl);
		if(formatType=='eBook')
		{
			if(isbn_pdf!=null && isbn_pdf.trim() !='')   { isbnPdf=isbn_pdf;   }
			if(isbn_epub!=null && isbn_epub.trim() !='') { isbnEpub=isbn_epub; }
			if(isbn_mobi!=null && isbn_mobi.trim() !='') { isbnMobi=isbn_mobi; }
			addToCart(e,shoppingCartUrl,ebookPurchase);
		}
		else if(formatType=='print')  {
			addToCart(e,shoppingCartUrl,printFormatPurchase);
		}
		else if(formatType=='online') {
			addToCart(e,shoppingCartUrl,onlineFormatPurchase);
		}
		else if(formatType=='software')	{
			addToCart(e,shoppingCartUrl,softwareFormatPurchase);
		}
  }
  
  function add(e,requestType,prodCD,delType,prodTyp){
    var shoppingCartUrl = "/servlets/MaintainCartServlet?REQUEST_TYPE="+requestType+"&ITEM_ID="+prodCD+"&DELIV_CD="+delType+"&PROD_TYP="+prodTyp+"&RENDER_TYP=JSON";   
    
    getDimensions();
    
    if(delType=='DNL' || delType=='ONL') 
    {addToCart(e,shoppingCartUrl,downloadFormatPurchase);}
    else if(delType=='PST') 
    	{addToCart(e,shoppingCartUrl,mailFormatPurchase);}
    	else if(delType=='FAX')	
    	  	{addToCart(e,shoppingCartUrl,faxFormatPurchase);}
    	     else{addToCart(e,shoppingCartUrl,downloadFormatPurchase);}	
    }
    
  
  function openMailtoWin(sub,mailBody)
  {
    mlsub = escape(sub);
    mlbody = escape(mailBody);
    var win_uid = "win"+new Date().getTime();
    window.location.replace('mailto:?subject='+mlsub+'&body='+mlbody,win_uid);
  }

  /** Moved from library.jsp  manageAddToCart is already there renamed to libManageAddToCart */
  function libManageAddToCart(e,prodCode,isbn_pdf,isbn_epub,isbn_mobi)
  {
  	var shoppingCartUrl = "/servlets/MaintainCartServlet?REQUEST_TYPE=AddItem&ITEM_ID="+prodCode+"&DELIV_CD=DNL&PROD_TYP=BOOK&RENDER_TYP=JSON";
  	isbnPdf=isbn_pdf;isbnEpub=isbn_epub;isbnMobi=isbn_mobi;
  	addToCart(e,shoppingCartUrl,ebookPurchase);
  }
