function CheckCurrency()
{
   return "CAD";
}

function UpdateTotals()
{
   // update hidden fields for sending to Paypal
   var total    = 0.0;
   var total2   = 0.0;
   var subtotal = 0.0;
   var price    = 0.0;
   var formObj = document.PaypalForm;

   for(i=0; i<QuantA.length; i++)
   {
      price = priceItem(i, "CAD");
	  subtotal = price * QuantA[i];
	  total += subtotal;
      // discount doesn't apply to ebooks or elessons. 
      if(DescA[i].indexOf("E-Book") < 0 && DescA[i].indexOf("E-Lesson") < 0)
         total2 += subtotal;
   }
   // no discount for less than 3 books.
   Discount = CalcDiscount(BookCount, total2);
   total -= Discount;

   // calculate shipping and handling
   CalcShipping("CAD");

   total += S_and_H;
   Taxes = 0.0;
}

function AddOrderToCart(prefix, pcode)
{
/*
   'prefix' serves to identify related item data fields like size, shepe, and quantity
   ie - the brown wood group has a prefix of 'WB' which identifies its option lists 
   and type gives pricing information - wood types have one price, and stone types another. 
   this provides access to all information needed to add an item to the shopping cart and 
   calculate totals for the purchase order.
*/

    var intval = 0;
    var sname = '';
    var added = false;
    var storeForm = document.WebstoreForm;
	
	sname = prefix + 'Description';
	if( inStock(pcode))
	{
    if( QuantA.length < 24 )
    {
	// add given quantity of selected item to cart
		
	added = DescA.push(storeForm.elements[sname].value);
	window.alert("'" + storeForm.elements[sname].value + "' added to cart");
		
	sname = prefix + 'Quantity';
	added = QuantA.push(storeForm.elements[sname].value);
	intval = parseInt ( storeForm.elements[sname].value, 10 );
	
	sname = prefix + 'Description';
	if(storeForm.elements[sname].value.indexOf("E-Book") < 0 && storeForm.elements[sname].value.indexOf("E-Lesson") < 0)
	{
	    BookCount += intval;
		//window.alert('Adding a textbook to the cart');
	}	
	// save the item type for pricing
	PriceCodeA.push(pcode);
    } 
    else
        window.alert('Sorry, this shopping cart only holds 25 selections at a time. If you want to order more than 25, please pay for this order first and then come back for the rest.');
	}
	else
	{
        window.alert('Sorry - ' + storeForm.elements[sname].value + ' is temporarily out of stock');
	}
}

function CreateHTMLPurchaseOrderTable()
{
// used to display a purchase order when requested, and when checking out with Paypal.
var invdate = new Date();
var total    = 0.0;
var subtotal = 0.0;
var price    = 0.0;
//var BillForm = document.BillingAddressForm;

// create table with invoice, insert header, add row for each orders, close table
var reportline = '<table width="100%" border = "1" bordercolor="black"><tr><td width="221" colspan="2"><img src="Images/ArdmoreLogo.gif"></td>';
    reportline += '<td align="center"><b><font size="+4">Shopping Cart</font></b><br>Date: ';
    reportline += invdate.toLocaleString() + '</td><td align="center" colspan="2"><font size="+2"><b>Dyslexia Victoria Online';
    reportline += '</b></font><br>875 Ardmore Dr.<br>North Saanich, British Columbia<br/> Canada V8L 5G2<br>Phone: (250) 656-4503<br/> Fax:  (250) 656-4573<br/> Email:  thi.sales@dyslexiavictoria.ca</td></tr>';
    reportline += '<td align="center" colspan="5"><b>Your Shopping cart order</b></td></tr><tr>';
    reportline += '<tr><td align="center" width="6%"><b>Quantity</b></td><td align="center" colspan="2"><b>Description</b></td>';
    reportline += '<td align="center"><b>Price</b></td><td align="center"><b>Subtotal</b></td></tr>';
    for(i=0; i<QuantA.length; i++)
    {
        reportline += '<tr><td align="center">' + QuantA[i] + '</td><td align="left" colspan="2"><b>' + DescA[i] + '</b></td>';
        reportline += '<td align="right">$';
        price = priceItem(i, "CAD");
        subtotal = price * QuantA[i];
        total += subtotal;
        reportline += TrimCents(price) + '</td><td align="right">$' + TrimCents(subtotal) + '</td></tr>';
    }

    // trim decimals past 2 places.
    reportline += '<tr><td colspan="4">Subtotal</td><td align="right">$' + TrimCents(total) + '</td></tr>';
    reportline += '<tr><td colspan="4">Discount</td><td align="right">$' + TrimCents(Discount) + '</td></tr>';
    reportline += '<tr><td colspan="4">Shipping and Handling</td><td align="right">$';
    reportline += TrimCents(S_and_H) + '</td></tr>';

    total += S_and_H;
    total -= Discount;
    //if( BillForm.country.value == 'Canada (outside BC)' || BillForm.country.value == 'BC, Canada')
    //{
    //    Taxes = total * 0.06;
    //    if(BillForm.country.value == 'BC, Canada')
    //    {
    //        Taxes += total * 0.07; 
    //    }
    //    total += Taxes;
    //}
	Taxes = 0.0;
    reportline += '<tr><td colspan="4">Taxes (GST & PST if applicable)</td><td align="right">$' + TrimCents(Taxes) + '</td></tr>';
    reportline += '<tr><td colspan="4"><b>Total</b></td><td align="right">$' + TrimCents(total) + '</td></tr>';
    reportline += '</table>';

return reportline;
}

function ShowCart()
{
   var MyCart = '';
   var invoice = window.open("","Shopping Cart");
   UpdateTotals();
   MyCart += '<html><head></head><body><center>' + CreateHTMLPurchaseOrderTable() + '<br><a href="" onClick="self.close()"><img src="Images/OwlReadingBackTobtn.jpg"></a></center></body></html>';
   // Taxes, s_and_h, and discount should always be updated when an order is viewed.
   invoice.document.write(MyCart); 
}

function SubmitCart()
{
   var invoice    = window.open("","Shopping Cart");
   var reportline = "";
   var ind        = 0;
   var price      = 0.0;
   var idiscount  = 1.0;
   var sBtn       = '<input type="submit" name="" value="Checkout with Paypal">';
   var PPForm     = '<form name="PaypalForm" method="post" ';
       PPForm    += 'action="https://www.paypal.com/cgi-bin/webscr"><fieldset>';
   var PPForm1    = '<input type="hidden" name="cmd" value="_cart""><input type="hidden" name="upload" id="upload" value="1">';
   var PPForm2    = '<input type="hidden" name="business" id="business" value="thi.sales@dyslexiavictoria.ca">';
   var PPForm3    = '<input type="hidden" name="currency_code" id="currency_code" value="CAD">';
   var PPForm4    = '<input type="hidden" name="return" value="http://www.dyslexiavictoria.ca/approved.php">';
   var PPForm5    = '<input type="hidden" name="cancel_return" value="http://www.dyslexiavictoria.ca/cancelled.php">';
   
   // Taxes, s_and_h, and discount should always be updated when an order is submitted for payment.
   UpdateTotals();

   if( QuantA.length > 0 )
   {
      // less than 3 - no discount, 3 - 9, 5%, more than 9 - 10%
      if( BookCount > 3 )
         idiscount = 0.95; 
      if( BookCount > 9 )
         idiscount = 0.90; 

      reportline = '<html><title>Webstore - Shopping Cart</title><head></head><body>';
      reportline += CreateHTMLPurchaseOrderTable();
      reportline += '<center>';
      reportline += '<table border="2" bgcolor="yellow"><tr><td><font size="+1">All purchases will be in Canadian funds. A copy of this purchase order will be returned';
      reportline += ' to you from Paypal after payment is complete, and one will also be included with your order. You may print an ';
      reportline += 'additional copy now for your records using your print function if ';
      reportline += 'you wish. If your purchase includes E-Books or E-Lessons, you will receive an email within two business days';
      reportline += ' containing instructions on how to download your purchase from our password protected facility.';
      reportline += ' If you have purchased textbook(s), you will receive an email within two business days confirming the shipment date.';
	  reportline += '  All products have a thirty day full money back guarantee, no questions asked.  </font></td></tr></table>';
      reportline += '<br><br><a href="" onClick="self.close()"><img src="Images/OwlReadingBackTobtn.jpg"></a>' + PPForm;
      reportline += PPForm1 + PPForm2 + PPForm3 + '<input type="hidden" name="tax_cart" id="tax_cart" value="0.0">';
	  reportline += '<input type="hidden" name="handling_cart" id="handling_cart" value="' + S_and_H + '">' + PPForm4 + PPForm5;
      reportline += '<input type="hidden" name="rm" value="2"> <input"type="hidden" name="no_shipping" value="2">';
      reportline += '<input type="hidden" name="cs" value="1"><input type="hidden" name="tax_cart" value="' + TrimCents(Taxes) + '">';
      reportline += '<input type="hidden" name="handling_cart" value="' + TrimCents(S_and_H) + '">';
	
      for(i=0; i<QuantA.length; i++)
      {
         ind = i+1;
         price = priceItem(i, "CAD");
	     // discounts don't apply to electronic documents
         if(DescA[i].indexOf("E-Book") < 0 && DescA[i].indexOf("E-Lesson") < 0)
	     {
            price *= idiscount;
		 }
         reportline += '<input type="hidden" name="item_name_' + ind + '" value="' + DescA[i] + '">';
         reportline += '<input type="hidden" name="quantity_' + ind + '" value="' + QuantA[i] + '">';
         reportline += '<input type="hidden" name="amount_' + ind + '" value="' + TrimCents(price) + '">';
      }

      reportline += sBtn + '</fieldset></form></center></body></html>';
      invoice.document.write(reportline);
   }
   else
   {
      window.alert('Sorry, your shopping cart is empty. Please add an item(s) to your cart before submitting to Paypal.');
   }
}
