var newWindow
var myIsbn
var myTitle
// var amamazonUrl = http://
//var bnUrl
//var bordersUrl
//var fatbrainUrl
function setISBN(isbn){
	myIsbn = isbn;
	popBookWindow();
}
function orderbook() 
{
	//get isbn number to populate page
	myIsbn = document.order.isbn.value;
	myTitle = document.title;
	popBookWindow();
}
function popBookWindow(){
	if (!newWindow || newWindow.closed) {
		newWindow = window.open("","","status,scrollbars,height=640,width=640");
		if (!newWindow.opener)
		{
			newWindow.opener  = window
		}
			
		//newWindow = window.open("","","status,location,height=640,width=640");
		
	
		// assemble content for new window
		var newContent = '<html><head><title>Order This Book Today</title><link href="/css/inner_pages.css" type="text/css" rel="stylesheet" media="all" /></head>';
		newContent += '<body style="margin:6px 12px 6px 9px;"><div id="book_order_body" style="width:600px"><h2>Order Now!<\/h2>';
		newContent += '<p>For single copy purchases of any AMACOM title, you can connect directly to the online retailer of your choice, from the list below, to buy the title you have selected.  Most of our links will take you directly to that title on the site, making your shopping experience easier. You can also visit your local retailer, and if the book is not on their shelves they can special order it for you.</p>'
		if(myIsbn.legnth == 10){
			newContent += '<ul class=\"arrowlink\"><li><a href="http://www.amazon.com/exec/obidos/ASIN/' + myIsbn +'/wwwamacombooorg" target="_blank">Amazon - US</a></li>'	
		}else{
			newContent += '<ul class=\"arrowlink\"><li><a href="http://www.amazon.com/exec/obidos/ASIN/' + calcEan13(myIsbn) +'/wwwamacombooorg" target="_blank">Amazon - US</a></li>'	
		}
		newContent += '<li><a href="http://www.amazon.ca" target="_blank">Amazon - Canada</a></li>'
		newContent += '<li><a href="http://search.barnesandnoble.com/booksearch/isbnInquiry.asp?z=y&isbn=' + myIsbn + '" target="_blank">BN.com</a></li>'
		newContent += '<li><a href="http://www.booksense.com" target="_blank">Booksense.com</a></li>'
		newContent += '<li><a href="http://www.booksforbusiness.com" target="_blank">Books for Business - Canada</a></li>'
		newContent += '<li><a href="http://www.Borders.com" target="_blank">Borders.com</a></li>'
		newContent += '<li><a href="http://www.chapters.indigo.ca" target="_blank">Chapters-Indigo Canada</a></li>'
		newContent += '<li><a href="http://www.entinst.ca" target="_blank">The Entrepreneurship Institute of Canada</a></li>'
		newContent += '<li><a href="http://shrmstore.shrm.org" target="_blank">SHRM Store</a></li></ul>'
		newContent += '<p><strong> Retailers: Please <a href="mailto:rcarlough@amanet.org">contact</a> us to change or add a listing.</strong></p>'
		newContent += '<h3>Buying in Bulk?<\/h3>';
		newContent += '<p>We have very competitive discounts starting at 5 copies, as well as personal service, for bulk orders. Simply contact our Special Sales Department.  Call 800-250-5308 or 212-903-8420 and ask for Special Sales. You can also email: <a href=\'mailto:SpecSlsWeb@amanet.org\'>SpecSlsWeb@amanet.org</a></p>'
		
		newContent  += '<p align="right"><a href="#" onClick="window.close()">Close this Window</a></p>'
		newContent += '</div></body></html>'
		newWindow.document.write(newContent);
		newWindow.document.close()
		
	}else{
		newWindow.focus()
	}
}

function calcEan13(isbn13)
{

  var isbn      = isbn13;
  var eanprefix = 978;
  var factor    = "131313131313";
  var eancheck  = 0;
  var err = "";

  switch(isbn.length)
  {
	case 10: isbn = isbn.substr(0,9); break;
	case 12:
	case 13:
	{
	  if(isbn.substr(0,3)!=eanprefix)
		err = "Invalid ISBN prefix";
	  else
		isbn = isbn.substr(3,9);
	  break;
	}
  }

  if(err != "")
	return "";
  if(isbn.length!=9)
	err = "Invalid ISBN length";
  else if(isNaN(isbn))
	err = "Invalid ISBN number";
  else
  {
	var newean = eanprefix+isbn;
	for(e=0; e<=11; e++)
	{
	  eancheck = eancheck + (newean.substr(e,1)*factor.substr(e,1));
	}
	eancheck = eancheck%10;
	if(eancheck!=0)
	  eancheck=10-eancheck;

	isbnCheck = ( 11 - ( isbn.substr(0,1)*10
						+isbn.substr(1,1)*9
						+isbn.substr(2,1)*8
						+isbn.substr(3,1)*7
						+isbn.substr(4,1)*6
						+isbn.substr(5,1)*5
						+isbn.substr(6,1)*4
						+isbn.substr(7,1)*3
						+isbn.substr(8,1)*2 ) % 11) % 11;
	if(isbnCheck==10)
	  isbn=isbn+"X";
	else
	  isbn=isbn+isbnCheck;

	//document.all.newean.value  = newean+eancheck;
	return isbn;
	//document.all.newisbn.value = isbn;
  }
  return "";
}
