<!-- hide script from nonscriptable browsers

// create array listing book costs in same sequence as they are listed in the table
var bookCost = new Array()
bookCost[0] = 12.95
bookCost[1] = 12.95
bookCost[2] = 19.95
bookCost[3] = 12.95
bookCost[4] = 11.95
bookCost[5] = 11.95
bookCost[6] = 11.95
bookCost[7] = 32.85
bookCost[8] = 11.95
bookCost[9] = 10.95
bookCost[10] = 9.95
bookCost[11] = 12.95
bookCost[12] = 12.95
bookCost[13] = 4.50
bookCost[14] = 10.50
bookCost[15] = 9.00
bookCost[16] = 6.50
bookCost[17] = 16.50
bookCost[18] = 12.95
bookCost[19] = 8.95
bookCost[20] = 17.50
bookCost[21] = 15.50
bookCost[22] = 1.95
bookCost[23] = 2.50
bookCost[24] = 1.50
bookCost[25] = 1.50
bookCost[26] = 1.50
bookCost[27] = 1.50
bookCost[28] = 1.25
bookCost[29] = 1.25
bookCost[30] = 1.00
bookCost[31] = 1.25
bookCost[32] = 0.50
bookCost[33] = 1.25
bookCost[34] = 1.25
bookCost[35] = 0.50
bookCost[36] = 0.75
bookCost[37] = 1.25
bookCost[38] = 1.25
bookCost[39] = 1.50
bookCost[40] = 1.50
bookCost[41] = 0.95
bookCost[42] = 0.95
bookCost[43] = 0.50


// ** BEGIN GLOBAL ADJUSTMENTS ** //
var numberOfRows = 45
var noSpace="";
var BooksetMsg = "";
var EFox1Msg = "";
var EFox2Msg = "";
var BooksetQty = 0;
var EFox1Qty = 0;
var EFox2Qty = 0;

// data entry row math
function extendRow(form,rowNum,bookQty,bookPrice) {
	// FIRST display error mesasge if user entry is NaN and ignore calculations
	//  alert("ExtRow: Row, Qty X cost= " + rowNum + ", " + form.Book_Qty[rowNum].value + " (" + bookQty + ") X " + bookCost[rowNum] + " (" + bookPrice + ")");
	//  var rowSum = form.Book_Qty[rowNum].value * bookCost[rowNum];

	var straightCost = "";
	// check and adjust for EmmetFox books in quantity - affects price !
	//  rowNum=20 = Science of Living discount
	if (rowNum==20)  {
		if (form.Book_Qty[20].value >19) {
			if (form.Book_Qty[20].value != EFox1Qty) {EFox1Msg=""; EFox1Qty=form.Book_Qty[20].value;}
			straightCost = form.Book_Qty[20].value * bookPrice;
			bookPrice = 9.25;
			if (EFox1Msg=="") {
	   			EFox1Msg = "QUANTITY DISCOUNT APPLIES ! \n   " + bookQty + " copies @ $" + formatNum(bookPrice,2) + " = $" + formatNum(bookQty * bookPrice,2) + "\n\nYou SAVE $" + formatNum(straightCost - bookQty * bookPrice,2); 
				alert(EFox1Msg);
			}
		} else {
			if (form.Book_Qty[20].value >2) {
				if (form.Book_Qty[21].value != EFox1Qty) {EFox1Msg=""; EFox1Qty=form.Book_Qty[20].value;}
				straightCost = form.Book_Qty[20].value * bookPrice;
				bookPrice = 10.90;
				if (EFox1Msg=="") {
		   			EFox1Msg = "QUANTITY DISCOUNT APPLIES ! \n   " + bookQty + " copies @ $" + formatNum(bookPrice,2) + " = $" + formatNum(bookQty * bookPrice,2) + "\n\nYou SAVE $" + formatNum(straightCost - bookQty * bookPrice,2); 
					alert(EFox1Msg);
				}	
			}
		}
	}

	//  rowNum=21 = EmmetFox Life Story discount
	if (rowNum==21)  {
		if (form.Book_Qty[21].value >19) {
			straightCost = form.Book_Qty[21].value * bookPrice;
			bookPrice = 8.75;
			if (EFox2Msg=="") {
   				EFox2Msg = "QUANTITY DISCOUNT APPLIES ! \n   " + bookQty + " copies @ $" + formatNum(bookPrice,2) + " = $" + formatNum(bookQty * bookPrice,2) + "\n\nYou SAVE $" + formatNum(straightCost - bookQty * bookPrice,2); 
				alert(EFox2Msg);
			}
		} else {
			if (form.Book_Qty[21].value >2) {
				straightCost = form.Book_Qty[21].value * bookPrice;
				bookPrice = 9.70;
				if (EFox2Msg=="") {
   				EFox2Msg = "QUANTITY DISCOUNT APPLIES ! \n   " + bookQty + " copies @ $" + formatNum(bookPrice,2) + " = $" + formatNum(bookQty * bookPrice,2) + "\n\nYou SAVE $" + formatNum(straightCost - bookQty * bookPrice,2); 
					alert(EFox2Msg);
				}
			}
		}
	}


	var rowSum = bookQty * bookPrice;
	//  alert("ExtRow: \nbookQty=[" + bookQty + "] \nbookPrice= [" + bookPrice + "]\n\nSetting rowTotal(" + rowNum + ") to " + rowSum);
	form.rowTotal[rowNum].value = formatNum(rowSum,2);
	if (isNaN(form.Book_Qty[rowNum].value)) {
	    alert("Please enter a number!");
	    form.Book_Qty[rowNum].value = "";
	    rowSum = 0;
	}
	if (rowSum == 0) {
	    form.Book_Qty[rowNum].value = "";
	    form.rowTotal[rowNum].value = "";
	    if (rowNum=21) {EFox1Msg = ""}
	    if (rowNum=22) {EFox2Msg = ""}
	}
}

function addTotals(form) {
   // alert("fx addTots \n # rowTotal rows= " + form.rowTotal.length + "\n Calculated number of bookCost rows: " + bookCost.length);
   var subTotal = 0;
   var bookCount = 0;
   var bookletCount = 0;   // for postage, 4 booklets = 1 book
	// [0] thru [22] are BOOKS; [23] thru [rowTotal.length] are BOOKLETS
   for (var i = 0; i < form.rowTotal.length; i++) {
	subTotal += (form.rowTotal[i].value != "") ? parseFloat(form.rowTotal[i].value) : 0;
	if (i<23) {
		bookCount += (form.Book_Qty[i].value != "") ? parseFloat(form.Book_Qty[i].value) : 0;
	} else {
		bookletCount += (form.Book_Qty[i].value != "") ? parseFloat(form.Book_Qty[i].value) : 0;
	}
   }

   // check and adjust for the 3-volume set where 1 set equals 3 books!
   if (form.Book_Qty[7].value != "") {
	adjustedCount = bookCount + (form.Book_Qty[7].value * 3)- form.Book_Qty[7].value;
	//  alert("Bookset check- BooksetMsg= [" + BooksetMsg + "]");
	if (BooksetMsg=="") {
   		BooksetMsg="Book set ordered! \n   " +form.Book_Qty[7].value+ " Set counts as " + form.Book_Qty[7].value * 3 + " books for postage."; 
		alert(BooksetMsg);
	}
	form.BookSet.value="includes 3-volume set!";
	bookCount = parseFloat(adjustedCount);
   } else {
	form.BookSet.value="";
	BooksetMsg = "";
   }
   if (bookletCount>0)  {
	//  adjustedCount = (bookCount + bookCount/4);
	//  bookCount = parseFloat(adjustedCount);  
	//  alert(bookletCount + " booklets ordered - adjusting book count (" + bookCount + ") for postage. . . ");
	if (bookCount==0  &&  bookletCount<5) {
		//  alert("One book Minimum !");
		bookCount=1;
		alert(bookletCount + " booklets count as ONE book for postage.");
	} else {
		bookCount = parseInt(bookCount + bookletCount/4);
		alert(bookletCount + " booklets count as " + parseInt(bookletCount/4) + " books for postage.");
	}
	

   }


   //  alert("Computing postage from bookCount= " + bookCount);
   if (form.Postage.value == "") {
	oldPostage = "0"; }
   else {
	oldPostage = form.Postage.value;
	}
   if (bookCount == "1") {
	form.Postage.value = "3.95";}
   else {
        if (bookCount == "2" || bookCount == "3" || bookCount == "4") {
	     form.Postage.value = "8.55";}
	else {
	     if (bookCount == "5" || bookCount == "6" || bookCount == "7" || bookCount == "8") {
	 	  form.Postage.value = "10.35";}
	     else {
		  if (bookCount == "0") {
		     form.Postage.value = "";}
	     	  else {
     		     form.Postage.value = "?";}
             }
	}
   }

   if (bookCount > "0") {
   	form.Postage.value = formatNum(form.Postage.value,2);
   	form.bookTotal.value = formatNum(subTotal,2);
   	form.grandQuant.value = bookCount;
   	form.grandTotal.value = "$" + formatNum((parseFloat(form.bookTotal.value) + parseFloat(form.Postage.value)),2); }
   else {
   	form.Postage.value = "";
   	form.bookTotal.value = "";
   	form.grandQuant.value = "";
   	form.grandTotal.value = "";
   }

   //  alert("Checking totals:  Postage= " + form.Postage.value + "\n   Grand total= " + form.grandTotal.value);
   //  When total books ordered >8 postage is unknown, so don't show any totals at all
   if (bookCount > "8") {
	form.Postage.value = "";
	form.grandTotal.value = "";
	form.BookSet.value=" Qty over 8; please call for rate.";
   }

}
// ** END GLOBAL ADJUSTMENTS ** //


function calculate(form,rowNum) {
	// alert("fx Calculate");
	//  extendRow(form,rowNum)
	extendRow(form,rowNum,form.Book_Qty[rowNum].value,bookCost[rowNum])
	addTotals(form)
}

function formatNum(expr,decplaces) {
	// alert("fx formatNum");
	var str = (Math.round(parseFloat(expr) * Math.pow(10,decplaces))).toString()
	while (str.length <= decplaces) {
		str = "0" + str
	} 
	var decpoint = str.length - decplaces
	return str.substring(0,decpoint) + "." + str.substring(decpoint,str.length)
}

function resetAllText() {
   for (var i = 0; i < document.forms[0].elements.length; i++) { 
      if (document.forms[0].elements[i].type == "text") { 
         document.forms[0].elements[i].value = "" 
      } 
   } 
   BooksetMsg = "";
   EFox1Msg = "";
   EFox2Msg = "";
   document.forms[0].ShipInstr.value = "";
}

// end script hiding -->

