var imgSrc="http://gentleplace.com/images/buynow01.gif";
function $(id) {return document.getElementById(id)}
function item(id, desc, price, type)
{
// modifiables
this.numError="Please enter only numbers in the Quantity fields.";
this.id = id;
this.desc=desc;
this.price=price;
this.type=type;
this.AddCosts=addCosts;
this.AddShipping=addShipping;
this.qty=0
function addCosts()
{
elem = $(this.id);
this.qty=0;
if (elem.value!="")
this.qty=parseInt(elem.value);
if (isNaN(this.qty))
{
elem.value="";
alert(this.numError);
this.qty=0;
}
if (TypeTotals[this.type.id])
TypeTotals[this.type.id]+=this.qty;
else
TypeTotals[this.type.id]=this.qty;
TotalItemsPrice+=this.qty*this.price;
if (this.qty>0)
Description+=this.qty+ " x " + this.desc + ", ";
}
function addShipping(shipRegion)
{
TotalShippingCost+=this.type.GetShippingCost(this.qty,shipRegion);
}
}

function alertErr()
{
alert("Please enter some items first.");
}

function Clear() {
TypeTotals = new Array();
TotalItemsPrice = 0.0;
TotalShippingCost = 0.0;
Description = "";
$("goodsDiv").innerHTML='';
$("shippingDiv").innerHTML='';
$("totalDiv").innerHTML='';
$("buyDiv").innerHTML="";
}
// shipping cost is related to an objects type
function itemType(id, singleShippingCostArray, multipleShippingCostArray)
{
this.id = id;
this.GetShippingCost=getShippingCost
this.singleShipCost=singleShippingCostArray;
this.multShipCost=multipleShippingCostArray;
function getShippingCost(quantity, shipRegion)
{
if (TypeTotals[this.id] > 1)
return this.multShipCost[shipRegion]*quantity;
return this.singleShipCost[shipRegion]*quantity;
}
}
function getTotals()
{
Clear();
// item types have a numeric ID (increasing from 0)
// single shipping cost array (usa, canada, planet)
// multiple shipping cost array (usa, canada, planet)
// note total items price =<50 and chekc box then 40% discount = cost times .6
cd = new itemType(0, new Array(1.5,1.75,4), new Array(1.25,1.5,3.75));
book = new itemType(1, new Array(3.0,5.75,12), new Array(2.0,5.5,9.0));
items = new Array(
new item("QTY-OD", "Ocean Dreams", 14.95, cd),
new item("QTY-DTD", "During Thy Days", 14.95, cd),
new item("QTY-AB", "Ancient Breeze", 14.95, cd),
new item("QTY-GOG", "Gate of the Garden", 14.95, cd),
new item("QTY-WW", "Whispering Waves", 14.95, cd),
new item("QTY-GU", "Glow of Understanding", 19.95, book),
new item("QTY-SR", "Still Reflections", 19.95, book));
regionStrings=new Array(
"ANY STATE IN THE USA", "ANY DESTINATION IN CANADA", "ANY COUNTRY EXCEPT THE USA AND CANADA");
shipRegion=$("SHIP-LOC").selectedIndex;
for (i=0; i<items.length; i++)
items[i].AddCosts();
for (i=0; i<items.length; i++)
items[i].AddShipping(shipRegion);
if($("discount-checkbox").checked && TotalItemsPrice>=50) TotalItemsPrice=TotalItemsPrice*.60;
TotalCost=TotalItemsPrice+TotalShippingCost;
if (Description!="")
{
Description=Description.substring(0, Description.length-2);
Description+=" with shipping and handling paid to " + regionStrings[shipRegion];
string1='<form action="https://www.paypal.com/cgi-bin/webscr" target="_parent" method="post"><input name="cmd" value="_xclick" type="hidden"><input name="business" value="admin16@gentleplace.com" type="hidden"><input name="item_name" value="Item Name" type="hidden"><input name="currency_code" value="USD" type="hidden"><input name="amount" value="'
string2='" type="hidden"><input name="shipping" value="';
string3='" type="hidden"><input name="item_name" value="';
string4='" type="hidden"><input src="'+imgSrc+'" name="submit" alt="';
string5="Buy now on our secure server!";
string6='" type="image"></form>';
shippingCost=roundOff(TotalShippingCost);
itemsCost=roundOff(TotalItemsPrice);
totalCost=roundOff(TotalCost);
buyNowContents=string1+itemsCost.substring(1)+string2+shippingCost.substring(1)+string3+Description+string4+string5+string6;
$("goodsDiv").innerHTML=itemsCost;
$("shippingDiv").innerHTML=shippingCost;
$("totalDiv").innerHTML=totalCost;
$("buyDiv").innerHTML=buyNowContents;
}
}
function roundOff(number)
{
number=number*100;
number=Math.round(number);
number=number/100;
number = "$" + number;
dotIndex=number.indexOf(".");
if (dotIndex==-1)
return number + ".00";
else if(dotIndex==number.length-2)
return number + "0";
else
return number;
}

