//
// Powder Inc. script for ordering blackpowder.
// All rights reserved, copyright 2002 Powder Inc.
// 2002-11-10 -- bk -- New page
//
var // Global values
   PIsum = 0;
   PIprice1 = 0;
   PIprice10 = 0;
   PIorder_descr = "";
   PIinit = false;
   
// common js code for network
//Clear inherited frames  
if (parent != self) {
                top.location=self.document.location;
}


function PI_Init() {
   if (PIinit) return;
   // window.alert ("PI_Init PI_bullets.js");
   PIinit = true;
}

function PI_SumItem (name, price1, price10) {
  // window.alert(name + " in PI_SumItem ");   
   if (document.bullets[name] == null) {
                window.alert(name + " is not defined; Please report this to WebMaster at webmaster@powderinc.com")
                return;
   }
   var qty = document.bullets[name].value;
   if (qty != "") {
      var myQty = parseInt (qty, 10);

	  if (isNaN (myQty)) {
	     window.alert("PI_SumItem: " + name + " Not a number: " + myQty);
         document.bullets[name].value = "";
      } else {
         PIsum += myQty;
         PIprice1  += myQty * price1;
         PIprice10 += myQty * price10;
	PIorder_descr += name + "-" + myQty + ",";
	}
   }
   //window.alert("PI_SumItem qty " + myQty + "   PISum " + PIsum);
}

function PI_twodigits(val) {
   var dollars, cents;
   dollars = Math.floor(val);
   cents = Math.floor((val - dollars) * 100);
   if (cents < 10) cents = "0" + cents;
   return (dollars + "." + cents);
}

function PI_Sum(){
   var ship = 0;
   var request_count = 0;
   
   PIsum = 0;
   PIprice1 = 0;
   PIprice10 = 0;
   PIorder_descr = "";

   PI_SumItem("G38358",     10.75,  10.75);
   PI_SumItem("G3840401",   12.65,  12.65);
   PI_SumItem("G44428",     12.90,  12.90);
   PI_SumItem("G44430",     12.90,  12.90);
   PI_SumItem("G45452",     14.40,  14.40);
   PI_SumItem("G45454",     14.40,  14.40);
   PI_SumItem("G45350458",  13.50,  13.50);
   PI_SumItem("G45405458",  15.50,  15.50);
   PI_SumItem("G45450458",  16.90,  16.90);
  // PI_SumItem("G45480458",  15.75,  14.95);
	
      
   var now = new Date();
   var hours, mins,months,days;
   months = 1 + now.getMonth(); if (months < 10) months = "0" + months;
   days = now.getDate(); if (days < 10) days = "0" + days;
   hours = now.getHours(); if (hours < 10) hours = "0" + hours;
   mins = now.getMinutes(); if (mins < 10) mins = "0" + mins;

   document.bullets.submit.disabled = false;

    var cartons = Math.floor((PIsum - 1) / 10) + 1;
	     freight = 12 * cartons;
         price = 0;
         order_descr = "(empty)";
         item_id = "PIB" + now.getFullYear() + months + days 
                                  + "." + hours + "." + mins
                                  + "." + PIsum;
 		 shipping_info = ", Shipping: $" + PI_twodigits(freight) + " for this item.";

   if (PIsum==51) { // DEBUGGING entry
     price = 1; 
     order_descr = "Test Order";
     freight = 0.51;
	 shipping_info = ", Shipping: $" + PI_twodigits(freight) + " for this item.";
   } else if (PIsum > 9) { 
     price = PIprice10; 
     order_descr = "10 or more box discount.";
   } else if (PIsum>0)  {
     price = PIprice1; 
     order_descr = "Goex Bullet order.";
   } else {
     document.bullets.submit.disabled = true;
     freight = 0; 
     item_id = '';
	 shipping_info = '';
   }
   
   // window.alert("PI_sum, PIsum " + PIsum
   //                      + ", item_id " + item_id);
   document.bullets.Order_Size.value = PIsum; 
   document.bullets.Ammo_Price.value = "$" + PI_twodigits(price);
   document.bullets.Freight.value = "$" + PI_twodigits(freight);
   document.bullets.ITEM_PRICE.value = "$" + PI_twodigits(price + freight);
   document.bullets.Order_Description.value = order_descr;


   document.bullets.ITEM_OPTION3.value = item_id + shipping_info;
   document.bullets.ITEM_OPTION2.value = PIorder_descr;									  
   document.bullets.ITEM_NAME.value = PIsum + " box Goex Bullets order.";

}


function PI_Submit () {
   PI_Sum();
   document.bullets.custom.value = "\nZipcode\n"
                                  + document.bullets.ZIP.value 
								  ;
	// document.powder["Powder_Price"].value += document.powder["shipping"].value;
   // document.bullets.submit(); // /cgi/formhandler.pl -- post
   return (false);
}

function PI_ClearCalc () {
   document.bullets.submit.disabled = true;
  // window.alert("PI_ClearCalc, PI_bullets.js");

   // document.bullets.ITEM_ID.value = " -- ";
   PI_Sum();
}
