<!--
// <script language="JavaScript">		// not necessary if 

var Org = "JBHS";		// JBHS, AHS, MTBpics, etc
var OrgWebsite = "http://www.bowiephotos.com";

var varZero = 0;
var Subtotal = 0;		// order subtotal, pre-tax, pre-shipping
var TaxableSub = 0;		// subtotal of taxable portion
var Discount = 0;
var DiscountedSubtotal = 0;
var DiscountableSub = 0;	// subtotal of portion of order that is discountable
var TaxTotal = 0;		// sales tax total
var ShippingRate = 3.00 // shipping rate for pkg of prints
var ShippingRateLB2 = 1.50 // shipping rate for pkg of prints for Lisa Bradley-only orders
var ShippingTotal = 0;	// shipping total
var GrandTotal = 0;		// Total total
var LineItems = 0;
var TaxRate = 0.0825;
var IncludesDigitalFile=false;	// used to decide whether or not to add shipping to grand total
var IncludesPrint=false;		// used to decide whether or not to add shipping to grand total
var IncludesGiftCertificate=false;  // used to decide whether or not to add shipping to grand total
var IncludesLisaFiles=false;	// used to ID orders that include photos by Lisa Bradley
var IncludesNonLisaFiles=false; // used to ID orders that indluce photos by other than Lisa Bradley
var IsDigitalFile=false;
var IsGiftCertificate=false;
var DontChargeShipping=true;
var ShipDigitalFiles=false;		// option to ship digital files
var ShipDigitalFileCost=1.00;	// cost to have a CD burned of digital files

// Shopping cart data struct
var aDescriptor_1 = new Array();	// item descriptor #1, i.e., photo filename
var aDescriptor_2 = new Array();	// item descriptor #2, i.e., event name
var aDescriptor_3 = new Array();	// item descriptor #3
var aOption_1 = new Array();		// item option #1, i.e., size
var aOption_2 = new Array();		// item option #2
var aOption_3 = new Array();		// item option #2
var aQuantity = new Array();
var aQtyUpdate = new Array();		// for updating the quantity # in the shopping cart
var aSpecialInstructions = new Array();		// for user-entered instructions or comments
var aFullThumbnailURL = new Array();		// for pointer to thumbnail image
var aFullImageURL = new Array();		// for pointer to main/full-size image
var aFullThumbFrameURL = new Array();
var aFullImageFrameURL = new Array()

var aItemPrice = new Array();
var aPriceStructRef = new Array();
var aLineSubtotal = new Array();	// subtotal for this line in order form

/*
var UserFirstName = "John";
var UserLastName = "Silvey";
var UserPhone = "282-9233";
var UserEmail = "jsilj1@ev1.net";
var UserAddr1 = "3406 Socorro Tr";
var UserAddr2 = "";
var UserCity = "Austin";
var UserState = "TX";
var UserZipcode = "78739";
var UserComment = "No comment, your Honor";
*/
var UserFirstName = "";
var UserLastName = "";
var UserPhone = "";
var UserPhone2 = "";
var UserEmail = "";
var UserAddr1 = "";
var UserAddr2 = "";
var UserCity = "Austin";
var UserState = "Texas";
var UserZipcode = "";
var UserComment = "";

var CurrentUserName = "";			// For logon and Your Favorites
var CurrentUserID = 0;			// Database-related, unsigned int
var UserPassword = "";		
var CurrentGalleryName = "";	// The selected/active gallery to insert Favorites into
var CurrentGalleryID = 0;	// Database-related, unsigned invar UserReturnMainURL = "";
var CurrentImageID = 0;
var UserReturnMainLeftURL = ""; // - To return to when a session is interrupted to log in
var UserReturnHeaderURL = "";

var ReturnToIndexURL = "/index2.htm";   // used by "return to index" button on photo pages, dflt is home.html

var GiftCertTitle = "Not Specified";

var aFavoriteURL = new Array();		// array of URLs of favorite images
var FavCount = 0;
/* 
-------------------------------------------------------------------------------------------------------------------
   Add2cart(iDescriptor_1, iDescriptor_2, iDescriptor_3, iOption_1, iOption_2, iQuantity, iPriceStructRef)
	Called when customer clicks on "Add 2 cart".  For photo orders, the following standardized definition applies:
		Descriptor_1: photo filename
		Descriptor_2: event name
		Descriptor_3: image date/time from EXIF
		Option_1: size/layout
		Option_2: price for 1st unit
		Option_3: price each for additional units
	For gift certificates, the following standardized definition applies:
		Descriptor_1: "Gift Certificate", hard-coded
		Descriptor_2: event name
		Descriptor_3: User's computer date/time
		Option_1: "n/a", hard-coded
		Option_2: user-selected, or user-entered Gift Certificate value
		Option_3: 0, hard-coded
		SpecialInstructions: optional msg from gift giver - instructions for vendor, or a message for recipient
		iQuantity = as-entered
		iPriceStructRef: "GIFTCERT", hard-coded
		iSpecialInstructions: as entered
		iFullThumbnailURL: "/ecomg2/images/GiftCert.jpg", hard-coded
		iFullImageURL: "n/a", hard-coded

		Test for gift cert: if Descriptor_1="Gift Certificate" and iPriceStructRef="GIFTCERT"
-------------------------------------------------------------------------------------------------------------------
*/

function Add2Cart(iDescriptor_1, iDescriptor_2, iDescriptor_3, iOption_1, iOption_2, iOption_3,iQuantity, 
	iPriceStructRef, iSpecialInstructions, iFullThumbnailURL,iFullImageURL) {
	alert("Adding item or image " + iDescriptor_1 + " to order");
//	alert("Size, Price 1, and Price 2 are "+iOption_1+", "+iOption_2+", "+iOption_3);
//	alert("LineItems before 'add' = "+LineItems);
//  alert("Price struct is "+iPriceStructRef);
//	alert("Special Instructions are "+iSpecialInstructions);

	if(typeof iQuantity == "string"){
		iQuantity = parseInt(iQuantity);
	}

	IsGiftCertificate=false;
	if(iDescriptor_1=="Gift Certificate" && iPriceStructRef == "GIFTCERT"){
		IsGiftCertificate=true;
	}

	// First, check to see if this item is already in the list by matching descriptors 1, 2, and 3, and option 1
	var duplicateItem = false;
	for(i=0; i<LineItems; ++i){
		// alert("Checking cart entry "+i+" for duplicate order");
		if(!IsGiftCertificate && iDescriptor_1 == aDescriptor_1[i] && iDescriptor_2 == aDescriptor_2[i] && 
			iDescriptor_3 == aDescriptor_3[i] && iOption_1 == aOption_1[i]){
				if(window.confirm("Item already in your cart - would you like to add an additional copy?")){
					aQuantity[i] += iQuantity;
				}
				duplicateItem = true;
		}
	}

	if(!duplicateItem){
		aDescriptor_1[LineItems]=iDescriptor_1; aDescriptor_2[LineItems]=iDescriptor_2; 
		aDescriptor_3[LineItems]=iDescriptor_3;
		aOption_1[LineItems]=iOption_1; aOption_2[LineItems]=parseFloat(iOption_2); aOption_3[LineItems]=parseFloat(iOption_3);
		aQuantity[LineItems]=iQuantity;
		aQtyUpdate[LineItems]= (-1);		// -1 means unchanged ...
		aPriceStructRef[LineItems]=iPriceStructRef;
		if(iSpecialInstructions == null){
			aSpecialInstructions[LineItems]="event is pre-feature";
		} else {
			aSpecialInstructions[LineItems]=iSpecialInstructions;
			// NOTE: special instr string is "encodeURIComponent()" encoded at this point
		}
		if(iFullThumbnailURL == null){
			aFullThumbnailURL[LineItems]="pre-feature";
		} else {
			aFullThumbnailURL[LineItems]=iFullThumbnailURL;
			// NOTE: may need URI encoding here ...
		}
		if(iFullImageURL == null){
			aFullImageURL[LineItems]="pre-feature";
		} else {
			aFullImageURL[LineItems]=iFullImageURL;
			// NOTE: may need URI encoding here ...
		}
		LineItems++;
		//	alert("LineItems after 'add' = "+LineItems);
	}
	return;
}



/* 
-------------------------------------------------------------------------------------------------------------------
   RemoveFromCart(itemIndex)
	Called when customer clicks on "Remove".  This removes the line containing the specific "remove" link that
	the user clicked on. Index is '0' for the first line, '1' for the second line, etc.
-------------------------------------------------------------------------------------------------------------------
*/

function RemoveFromCart(itemIndex,targetWindow) {
	var localerr=false;
	// alert("Item index is "+itemIndex);
	if((LineItems >0) && confirm("Remove "+aDescriptor_1[itemIndex]+
		      ", size "+aOption_1[itemIndex]+", qty "+aQuantity[itemIndex]+" from cart?")){
		if(itemIndex >= LineItems){
			alert("Error 1 in RemoveFromCart function");
			localerr=true;
		} else if(itemIndex == (LineItems-1)){
			// to delete last line, do nothing - just need to truncate array
		} else if(itemIndex < (LineItems-1)){
			// Collapse higher-order array elements down onto the one to be deleted
			for(i=itemIndex; i<LineItems; ++i){
				aDescriptor_1[i]=aDescriptor_1[i+1];
				aDescriptor_2[i]=aDescriptor_2[i+1];
				aDescriptor_3[i]=aDescriptor_3[i+1];
				aOption_1[i]=aOption_1[i+1];
				aOption_2[i]=aOption_2[i+1];
				aOption_3[i]=aOption_3[i+1];
				aQuantity[i]=aQuantity[i+1];
				aQtyUpdate[i]=aQtyUpdate[i+1];
				aPriceStructRef[i]=aPriceStructRef[i+1];
				aSpecialInstructions[i]=aSpecialInstructions[i+1];
				aFullThumbnailURL[i]=aFullThumbnailURL[i+1];
				aFullImageURL[i]=aFullImageURL[i+1];

			}
		} else {
			alert("Error 2 in RemoveFromCart function");
			localerr=true;
		}
		// Truncate the array by chopping off the last entry in each array of interest
		if(!localerr){
			aDescriptor_1.length--; aDescriptor_2.length--; aDescriptor_3.length--;
			aOption_1.length--; aOption_2.length--; aOption_3.length--;
			aQuantity.length--;
			aQtyUpdate.length--;
			aPriceStructRef.length--;
			aSpecialInstructions.length--;
			aFullThumbnailURL.length--;
			aFullImageURL.length--;
		}
		LineItems = LineItems-1;
	}

//	for(i=0;i<LineItems;++i){
//		alert("dumping array after delete ..");
//		alert("file, size, price1, and price 2 are "+aDescriptor_1[i]+", "+aOption_1[i]+", "+aOption_2[i]+", "+aOption_3[i]);
//		alert("Descr2 and 3 are "+aDescriptor_2[i]+" "+aDescriptor_3[i]);
//	}

	targetWindow.location="/ecomg2/html/cart.html";		// re-display the updated cart
	return;
}	


/* 
-------------------------------------------------------------------------------------------------------------------
   Showcart(targetWindow);
	- overwrites current window with a summary of the shopping cart contents
-------------------------------------------------------------------------------------------------------------------
*/

// NOT HANDLED BY FUNCTION CALL - HANDLED BY CALL TO "SHOW_CART" page, which will have the javascript necessary
//    build the cart view dynamically

//-----------------------------------------------------------------------------
//--- ShowCart() -  does a document.clear, then write to show the items in the cart.


function ChangeDigFileShipping(iForm,targetWindow){

	// if(iForm) { alert("found iForm okay"); } else { alert("Didn't find iForm"); }
	// if(iForm.ShipDigFile) { alert("found iForm.ShipDigFile okay"); } else { alert("Didn't find iForm.ShipDigFile"); }
	// if(iForm.ShipDigFile[0].value) { alert("found iForm.ShipDigFile[0].value okay"); } else { alert("Didn't find iForm.ShipDigFile[0].value"); }

	if(iForm.ShipDigFile[0].checked && (iForm.ShipDigFile[0].value=="Download")){
		ShipDigitalFiles=false;
	} else if(iForm.ShipDigFile[1].checked && (iForm.ShipDigFile[1].value=="Send")){
		ShipDigitalFiles=true;
	} else {
		alert("Confused about changing dig file ship method");
	}

	targetWindow.location="/ecomg2/html/cart.html";		// re-display the updated cart
}



function UpdateQty_ShowCart(targetWindow){
	// NOT USED AT THE MOMENT - THE 'FORM' to change a QTY entry in the shopping cart calls qtyUpdate below, not this one
	alert("Processing update button click");
	for (i=0 ; i < LineItems; i++){
		if(aQtyUpdate[i] != (-1)){
			aQuantity[i]=aQtyUpdate[i];
			alert("Updated line item "+i+" to "+aQuantity[i]);
			aQtyUpdate[i] = (-1);
		}
	}
	targetWindow.location="/ecomg2/html/cart.html";	// Redraw the cart in the window with the updated total.
}	


function qtyUpdate(iForm,i,targetWindow)
{
	input_name="LineQty_"+i;

	k=false;
	if(confirm("Update this quantity")){
		for(j=0; j<iForm.elements.length; ++j){
			if(iForm.elements[j].name && (iForm.elements[j].name == input_name)){
				// aQtyUpdate[i]=iForm.elements[j].value;
				aQuantity[i]=iForm.elements[j].value;
				k=true;
				break;
			}
		}
		if(k){
			alert("Updated quantity to "+aQuantity[i]);
		} else {
			alert("Program error - couldn't find DOM element "+input_name);
		}
	} else {
		alert("No update done - orig quantity left unchanged");
	}

	// Check for and remove any lines with Quantity=0
	for(j=0; j<LineItems; ++j){
		// alert("Checking cart for lines with Quantity=0");
		if(aQuantity[j] == 0){
			alert("Image "+aDescriptor_1[j]+" in cart has zero quantity - removing line from cart");
			top.footer.RemoveFromCart(j,targetWindow);
		}
	}

	targetWindow.location="/ecomg2/html/cart.html";	// Redraw the cart in the window with the updated total.
}



function ShowCart(targetWindow)
{
  var doc = targetWindow.document;   // overwrite this document for now as a test

  doc.open();   // does a auto-clear upon call
  doc.write("<html><head>\n<title>Cart</title>\n");
  doc.write('<META http-equiv="pragma" content="no-cache">\n');
  doc.write('<META http-equiv="cache-control" content="no-cache">\n');
  doc.write('<META http-equiv="Expires" content="0">\n');
  
  doc.write("</head>\n");
  // doc.write("<body class='picmenu' topmargin=0 leftmargin=0 bgcolor='#8c8c8c'>\n");
  doc.write('<body style="background-color: #8c8c8c;">\n');
  // doc.write("<script language='JavaScript'>self.resizeTo(700,700); self.focus();</script>\n");

  doc.write("<p>&nbsp</p><p>&nbsp</p>\n");

  WriteCartTable(targetWindow,true);	// write out the shopping cart table & include the update-qty stuff
  doc.write("<br>\n");
  doc.write("<table width='640' border=0 cellpadding=2 cellspacing=2>\n");
  
  // doc.write('<form action="'+OrgWebsite+'/html/checkout1.html" method="post"\n');
  doc.write('<form action="/ecomg2/html/checkout1.html" method="post"\n');

  doc.write('    name="Order">\n');
  doc.write("<tr>\n");
  doc.write("<td align='center'><B>YOU MAY</B>&nbsp;&nbsp;<input type='button' value='Continue Shopping' onclick='javascript:window.location=\"/Shopcart.html\";'></td>\n");
  doc.write("<td align='center'><B>OR GO TO THE</B>&nbsp;&nbsp;<input type='submit' value='Next Checkout Step'></td></tr>\n");
  doc.write("</table></center>\n");
  doc.write("</form>\n");   
  doc.write("</body></html>\n");
  doc.close();

} // end function showcart()


/* 
-------------------------------------------------------------------------------------------------------------------
   WriteCartTable(targetWindow);
	- writes a table containing the shopping cart out to targetWindow
-------------------------------------------------------------------------------------------------------------------
*/

function WriteCartTable(targetWindow,includeQtyUpdate)
{
  var doc = targetWindow.document;   // overwrite this document for now as a test
  var lineCost;
  var digMatch;	// local var, used to test for inclusion of digital file in order

  doc.write("<table border=1 cellpadding=2 cellspacing=2>\n");

  doc.write("<tr><td colspan='5' class='hd1' width='460'><p align='center'>\n");
  doc.write("<B>YOUR SHOPPING CART</B><br>\n");
  doc.write("<font size='-1'>Large order discounts will be automatically added<br>\n");
  doc.write("<I>(Gift certificates excluded from discounts and sales tax)</I><br>\n");
  doc.write("10% break at $50, 15% break at $100, 20% break at $200</font></td>\n");
  doc.write("<td colspan='3' class='hd1' width='220'><p align='center'>\n");

  doc.write("<input type='button' name='clearcart' value='Clear Cart' onclick='top.footer.ClearCart(self.window);'>\n"); 
  doc.write("</td></tr>\n");
    
  doc.write("<tr>\n<td align='center' class='hd1' width='140'>Item or<br>image name</td>\n");
  doc.write("<td align='center' class='hd1' width='140'>Image Date/time<br>Event ID</td>\n");
  doc.write("<td align='center' class='hd1' width='40'>Unit<br>Price</td>\n");
  doc.write("<td align='center' class='hd1' width='40'>Size</td>\n");
  doc.write("<td align='center' class='hd1' width='40'>Qty</td>\n");
  doc.write("<td align='center' class='hd1' width='40'>Line<br>Total</td>\n");
  doc.write("<td align='center' class='hd1' width='40'>Remove<br>Item</td>\n");
  doc.write("<td align='center' class='hd1'>Thumbnail<br>- click image to<br>view full-size</td>\n");
  doc.write("</tr>\n");

  // Write out a line in the table for each item in the shopping cart
  if(includeQtyUpdate==null){
	  includeQtyUpdate=false;
  }
    IncludesDigitalFile=false;	// reset and re-assessed each time just in case cart is modified to add/delete
  IncludesPrint=false;			// reset and re-assessed each time just in case cart is modified to add/delete
  IncludesGiftCertificate=false;	// reset and re-assessed each time just in case cart is modified to add/delete
  IncludesLisaFiles=false;
  IncludesNonLisaFiles=false;
  Subtotal = 0;
  DiscountableSub = 0;
  TaxableSub = 0;
  DiscountedSubtotal=0;
  DontChargeShipping=true;
  // doc.write("<form name='cartQty'>\n");
  doc.write("<form name='InputQty' onsubmit='return(false);'>\n");
  for (i=0 ; i < LineItems; i++){
	if(aPriceStructRef[i] == "LB1" || aPriceStructRef[i] == "LB2" || aPriceStructRef[i] == "LB3"){
		IncludesLisaFiles=true;
	} else {
		IncludesNonLisaFiles=true;
	}

	// Is this a gift certificate or a digital file?  If not, assume it's a print and charge shipping
	IsGiftCertificate=false;
	if(decodeURIComponent(aDescriptor_1[i])=="Gift Certificate" && aPriceStructRef[i] == "GIFTCERT"){
		IncludesGiftCertificate=true;
		IsGiftCertificate=true;
	}
	IsDigitalFile=false;
	digmatch = aOption_1[i].match(/hiresdig/i);
	if (digmatch != null) {
		IncludesDigitalFile=true;
		IsDigitalFile=true;
	}
	if(!IsGiftCertificate && !IsDigitalFile){
		IncludesPrint=true;
		DontChargeShipping=false;
	}

	if(aQuantity[i]==0){
		lineCost = 0;
	} else {
		lineCost = aOption_2[i] + (aOption_3[i] * (aQuantity[i]-1));
	}
	Subtotal = Subtotal+lineCost;  // Subtotal of everything - gift certificates included
	if(!IsGiftCertificate){		// Gift certificates don't get taxed and don't get discounted.
		TaxableSub += lineCost;
		DiscountableSub += lineCost;
	}
	doc.write("<tr>\n");
	doc.write("<td class='sum1' width='140'><font size='-1'>" + decodeURIComponent(aDescriptor_1[i]) + "</font></td>\n");
	doc.write("<td class='sum1' width='140'><font size='-1'>" + aDescriptor_3[i] + "<br>&nbsp;<br>" + aDescriptor_2[i] +"</font></td>\n");
	doc.write("<td class='sum1' width='40'>" + aOption_2[i].toFixed(2));
	if(!IsGiftCertificate){
		doc.write("/" + aOption_3[i].toFixed(2));
	}
	doc.write("</td>\n");
	doc.write("<td align=center class='sum1' width='40'>" + aOption_1[i] + "</td>\n");

	if(includeQtyUpdate & !IsGiftCertificate){
	    doc.write("<td align=center class='sum1' width='40'><input type='text' name='LineQty_"+i+"' size='2' maxlength='2'\n");
	    doc.write("value='"+aQuantity[i]+"' onchange='top.footer.qtyUpdate(document.InputQty,"+i+",window);'>\n");
	    // doc.write("<BR>&nbsp;<BR><input type='button' value='update' onclick='top.footer.UpdateQty_ShowCart(window);'></td>\n");
	    doc.write("<BR>&nbsp;<BR><input type='button' value='update'></td>\n");   // this button really does not do anything right now
	} else {
		doc.write("<td align=center class='sum1' width='40'>"+aQuantity[i]+"</td>\n");   // when user can't have an update option
	}

	doc.write("<td align=right class='hd2' width='40'>" + lineCost.toFixed(2) + "</td>\n");	
	doc.write("<td align='center' class='sum1' width='40'><A class='nav1' href=\"javascript:top.footer.RemoveFromCart(" + i + ",window);\">\n");
	doc.write("  <font size='-2'>Delete</font></A></td>\n");
	if(aFullThumbnailURL[i]==null || aFullThumbnailURL[i].length < 5){
		doc.write("<td rowspan=2>Thumbnail unavail</td>\n");
    } else {
		if(!IsGiftCertificate){
			doc.write("<td rowspan=2><a href='"+aFullImageURL[i]+"' target='_blank'>\n");
			doc.write("  <img src='"+aFullThumbnailURL[i]+"'></a></td>\n");
		} else {
			doc.write("<td rowspan=2><img src='"+aFullThumbnailURL[i]+"'></td>\n");
		}
	}
	doc.write("</tr>\n");
	dec_spec_instr=decodeURIComponent(aSpecialInstructions[i]);
	dec_spec_instr=dec_spec_instr.replace(/&/g,"&amp;");   // should be no HTML char entities in this string ...
	doc.write("<tr><td colspan='7'><b><font size='-1'>Special instructions for above print</font></b><br>"+dec_spec_instr+"</td></tr>\n");
  }

  // Option to ship digital files
  if(IncludesDigitalFile){
	  if(includeQtyUpdate){
		  // includeQtyUpdate==true means we're in the first step of the checkout and the user may
		  //    choose to have a digital file burned to disk and shipped.  After this first step in
		  //    check-out, his choice is fixed .. non-changeable.
		  doc.write("<tr><td colspan=5 align=right><B>Please select the way you would like to receive your digital files:       </B><br>\n");
		  doc.write("Download (free & no shipping charge - link will be emailed to you) -&nbsp;&nbsp;");
		  doc.write("<input type=radio name='ShipDigFile' value='Download' onClick='top.footer.ChangeDigFileShipping(document.InputQty,window);'");
		  if(ShipDigitalFiles){
			  doc.write("><br>\n");
		  } else {
			  doc.write(" checked><br>\n");
		  }
		  doc.write("or ... Burn me a CD ($1+ std. shipping charge) -&nbsp;&nbsp;");
		  doc.write("<input type=radio name='ShipDigFile' value='Send' onClick='top.footer.ChangeDigFileShipping(document.InputQty,window);'");
		  if(ShipDigitalFiles){
			  doc.write(" checked>");
			  doc.write("</td><td align=right class='hd1'>"+ShipDigitalFileCost.toFixed(2)+"</td>\n");
		  } else {
			  doc.write(">");
			  doc.write("</td><td align=right class='hd1'>"+varZero.toFixed(2)+"</td>\n");
		  }	  
		  
	  } else {
		  if(ShipDigitalFiles){
			doc.write("<tr><td colspan=5 align=right>Burn CD of digital files</td>\n");
			doc.write("<td align=right class='hd1'>"+ShipDigitalFileCost.toFixed(2)+"</td>\n");
		  } else {
			doc.write("<tr><td colspan=5 align=right>Download digital files from server (no CD) - no charge</td>\n");
			doc.write("<td align=right class='hd1'>"+varZero.toFixed(2)+"</td>\n");
		  }
	  }
	  doc.write("</tr>\n");
	  if(ShipDigitalFiles) {
		  Subtotal = Subtotal+ShipDigitalFileCost;
	  }
  }
  doc.write("</form>\n");

  //------ Write subtotal, tax, and total to visible table
  doc.write("<tr>\n<td colspan=5 align=right>Your subtotal is: </td>\n");
  doc.write("<td align=right class='hd1'>" + Subtotal.toFixed(2) + "</td>\n</tr>\n");
  doc.write("<tr>\n<td colspan=5 align=right>Amount eligible for discount (Gift Certificates excluded): </td>\n");
  doc.write("<td align=right class='hd1'>" + DiscountableSub.toFixed(2) + "</td>\n</tr>\n");
  if(DiscountableSub >= 200.0){
	  Discount = (0.20 * DiscountableSub);
	  doc.write("<tr><td colspan=5 align=right>20% large-order discount: </td><td align=right class='hd1'>" 
	           + Discount.toFixed(2) + "</td></tr>\n");
  } else if (DiscountableSub >= 100.0){
	  Discount = (0.15 * DiscountableSub);
	  doc.write("<tr><td colspan=5 align=right>15% large-order discount: </td><td align=right class='hd1'>" 
	           + Discount.toFixed(2) + "</td></tr>\n");
  } else if (DiscountableSub >= 50.0){
	  Discount = (0.1 * DiscountableSub);
	  doc.write("<tr><td colspan=5 align=right>10% large-order discount: </td><td align=right class='hd1'>" 
	           + Discount.toFixed(2) + "</td></tr>\n");
  } else {
	  Discount = 0;
	  doc.write("<tr><td colspan=5 align=right>Discount (10% at $50, 15% at $100, 20% at $200)</td><td align=right class='hd1'>" 
	           + Discount.toFixed(2) + "</td></tr>\n");
  }

  DiscountedSubtotal=Subtotal-Discount;

  if(DiscountableSub >= 50.0){
  	  doc.write("<tr><td colspan=5 align=right> ... after-discount subtotal: </td><td align=right class='hd1'>" 
           + DiscountedSubtotal.toFixed(2) + "</td></tr>\n");
  }

  doc.write("<tr>\n<td colspan=5 align=right>Amount for sales tax (Gift Certificates excluded): </td>\n");
  doc.write("<td align=right class='hd1'>" + TaxableSub.toFixed(2) + "</td>\n</tr>\n");
  TaxTotal = (TaxRate*TaxableSub);

  ShippingTotal=ShippingRate;
  if((DontChargeShipping==true) || (DiscountedSubtotal == 0) || (IncludesDigitalFile & !IncludesPrint & !ShipDigitalFiles) ||
	       (IncludesGiftCertificate & !IncludesPrint)){
	  ShippingTotal=0;
  } else {
	  if(IncludesLisaFiles && !IncludesNonLisaFiles){
		  ShippingTotal=ShippingRateLB2;   // for orders with only Lisa Bradley files
	  }
  }
  
  GrandTotal = DiscountedSubtotal+TaxTotal+ShippingTotal;

  doc.write("<tr><td colspan=5 align=right>Sales tax (8.25%, Gift Certificates excluded) is: </td><td align=right class='hd1'>" 
	           + TaxTotal.toFixed(2) + "</td></tr>\n")
  doc.write("<tr><td colspan=5 align=right>Packaging and mailing: </td><td align=right class='hd1'>"
               + ShippingTotal.toFixed(2) + "</td></tr>\n")

  doc.write("<tr>\n<td colspan=5 align=right>Total (shipping included): </td>\n<td align=right class='hd1'>" 
	           + GrandTotal.toFixed(2) + "</td></tr>\n")

  return;
}

// aFavoriteURL  FavCount



function ClearCart(targetWindow) {

	if(confirm("Delete everything in the shopping cart")){
		aDescriptor_1.length=0; aDescriptor_2.length=0; aDescriptor_3.length=0;
		aOption_1.length=0; aOption_2.length=0; aOption_3.length=0;
		aQuantity.length=0;
		aQtyUpdate.length=0;
		aPriceStructRef.length=0;
		aSpecialInstructions.length=0;
		aFullThumbnailURL.length=0;
		aFullImageURL.length=0;
	}
	LineItems = 0;
	IncludesDigitalFile=false;	// used to decide whether or not to add shipping to grand total
	IncludesPrint=false;		// used to decide whether or not to add shipping to grand total

	targetWindow.location="/ecomg2/html/cart.html";		// re-display the updated cart
	return;

}

function clearCartNoPrompt() {
	// Clear cart without prompting user
	aDescriptor_1.length=0; aDescriptor_2.length=0; aDescriptor_3.length=0;
	aOption_1.length=0; aOption_2.length=0; aOption_3.length=0;
	aQuantity.length=0;
	aQtyUpdate.length=0;
	aPriceStructRef.length=0;
	aSpecialInstructions.length=0;
	aFullThumbnailURL.length=0;
	aFullImageURL.length=0;
	LineItems = 0;
	IncludesDigitalFile=false;	// used to decide whether or not to add shipping to grand total
	IncludesPrint=false;		// used to decide whether or not to add shipping to grand total
}

/* 
-------------------------------------------------------------------------------------------------------------------
   Cookie stuff, copied from O'Reilly Javascript book
-------------------------------------------------------------------------------------------------------------------
*/

function Cookie(document, name, hours, path, domain, secure)
{
    // All the predefined properties of this object begin with '$'
    // to distinguish them from other properties which are the values to
    // be stored in the cookie.
    this.$document = document;
    this.$name = name;
    if (hours)
        this.$expiration = new Date((new Date()).getTime() + hours*3600000);
    else this.$expiration = null;
    if (path) this.$path = path; else this.$path = null;
    if (domain) this.$domain = domain; else this.$domain = null;
    if (secure) this.$secure = true; else this.$secure = false;
}

// This function is the store() method of the Cookie object.
Cookie.prototype.store = function () {
    // First, loop through the properties of the Cookie object and
    // put together the value of the cookie. Since cookies use the
    // equals sign and semicolons as separators, we'll use colons
    // and ampersands for the individual state variables we store 
    // within a single cookie value. Note that we escape the value
    // of each state variable, in case it contains punctuation or other
    // illegal characters.
    var cookieval = "";
    for(var prop in this) {
        // Ignore properties with names that begin with '$' and also methods.
        if ((prop.charAt(0) == '$') || ((typeof this[prop]) == 'function')) 
            continue;
        if (cookieval != "") cookieval += '&';
        cookieval += prop + ':' + escape(this[prop]);
    }

    // Now that we have the value of the cookie, put together the 
    // complete cookie string, which includes the name and the various
    // attributes specified when the Cookie object was created.
    var cookie = this.$name + '=' + cookieval;
    if (this.$expiration)
        cookie += '; expires=' + this.$expiration.toGMTString();
    if (this.$path) cookie += '; path=' + this.$path;
    if (this.$domain) cookie += '; domain=' + this.$domain;
    if (this.$secure) cookie += '; secure';

    // Now store the cookie by setting the magic Document.cookie property.
    this.$document.cookie = cookie;
}

// This function is the load() method of the Cookie object.
Cookie.prototype.load = function() { 
    // First, get a list of all cookies that pertain to this document.
    // We do this by reading the magic Document.cookie property.
    var allcookies = this.$document.cookie;
    if (allcookies == "") return false;

    // Now extract just the named cookie from that list.
    var start = allcookies.indexOf(this.$name + '=');
    if (start == -1) return false;   // Cookie not defined for this page.
    start += this.$name.length + 1;  // Skip name and equals sign.
    var end = allcookies.indexOf(';', start);
    if (end == -1) end = allcookies.length;
    var cookieval = allcookies.substring(start, end);

    // Now that we've extracted the value of the named cookie, we've
    // got to break that value down into individual state variable 
    // names and values. The name/value pairs are separated from each
    // other by ampersands, and the individual names and values are
    // separated from each other by colons. We use the split method
    // to parse everything.
    var a = cookieval.split('&');    // Break it into array of name/value pairs.
    for(var i=0; i < a.length; i++)  // Break each pair into an array.
        a[i] = a[i].split(':');

    // Now that we've parsed the cookie value, set all the names and values
    // of the state variables in this Cookie object. Note that we unescape()
    // the property value, because we called escape() when we stored it.
    for(var i = 0; i < a.length; i++) {
        this[a[i][0]] = unescape(a[i][1]);
    }

    // We're done, so return the success code.
    return true;
}

// This function is the remove() method of the Cookie object.
Cookie.prototype.remove = function() {
    var cookie;
    cookie = this.$name + '=';
    if (this.$path) cookie += '; path=' + this.$path;
    if (this.$domain) cookie += '; domain=' + this.$domain;
    cookie += '; expires=Fri, 02-Jan-1970 00:00:00 GMT';

    this.$document.cookie = cookie;
}


/* 
-------------------------------------------------------------------------------------------------------------------
   Your Favorites dev code
-------------------------------------------------------------------------------------------------------------------
*/

function userLogIn(userID) {
	top.footer.CurrentUserName=userID;
	top.main.header.location = '/html/header.html';    // Force reload to display "Logged in as ..."
}

function userLogOut() {
    top.footer.CurrentUserName='';
    top.main.header.location = '/html/header.html';  // Force reload to un-display "Logged in as ..."
}

function Add2Fav(iURL) {
	var rtnURL=document.url;
	alert("Adding  " + iURL + " to Favorites array");
	aFavoriteURL[top.footer.FavCount]=iURL; 
	top.footer.FavCount++;
	location="/php/add2Fav.php?favURL="+rtnURL;  // call the PHP page that adds-2-fav and re-displays this same page
	return;
}

function ShowFavs(targetWindow)
{
  var doc = targetWindow.document;   // overwrite this document for now as a test

  doc.clear();
  doc.write("<html><head>\n<title>Favorites</title>\n");
  doc.write("</head>\n");
  doc.write("<body bgcolor='#8c8c8c'>\n");
  for(i=0;i<top.footer.FavCount;++i){
	  doc.write("URL = "+aFavoriteURL[i]+"<br>");
  }
  doc.write("</body></html>\n");
  doc.close();
} // end show favorites()