// Support construction of dynamic PayPal args.
var blk1  = "https://www.paypal.com/cgi-bin/webscr" +
            "?cmd=_cart";
var blk1a = "&add=1";
var blk1d = "&display=1";
var blk2  = "&business=beaverboxes%40comcast.net";
var blk2a = "&quantity=";
var blk2q = "";
var blk3  = "&item_name=";
var blk3n = "Test";
var blk4  = "&amount=";
var blk4a = "6.66";
var blk5  = "";
var winpar = "width=600,height=400,scrollbars," +
             "location," +  // some users delete this
             "resizable,status";
			 

var BagForm = 0


// Forms
bagdescricp_loc = new Array(16,17,18)
zipBoxLocation = 19
qty_loc = 20
ship_loc = 21
total_loc = 22

homeimagebags = 1
BasePrice = 3.50
var currentZip = 0

AllColors = new Array("Barn Red","Royal Blue","Gray","Black","Yellow","Neon Orange","Neon Green","Neon Pink","Purple","White","Kelly Green","Maroon","Navy")
AllColorsPP = new Array("Red","R Blue","Gray","Black","Yellow","Neon Orange","Neon Green","Neon Pink","Purple","White","Kelly Green","Maroon","Navy")
TotalColors = AllColors.length

zone = new Array()
zonePriceBagsString = "6 6 7 7 7 7 7 6 6 7 7 8 8 8 6 7 7 8 8 8 9 6 7 8 8 8 9 9 7 7 8 8 9 9 10 7 " +
                      "7 8 9 9 9 10 7 7 8 9 9 10 11 7 8 8 9 10 10 11 8 8 9 9 10 11 12 8 8 9 9 10 " +
                      "11 13 8 8 9 10 11 12 13 8 8 9 10 11 13 14 8 9 9 10 11 13 15 8 9 9 10 12 14 " +
                      "16 9 9 9 10 12 15 17 9 9 10 11 13 15 18 9 10 10 11 14 16 18 9 10 10 12 14 " +
                      "17 19 9 10 11 12 15 18 20 9 10 11 12 15 18 21 10 11 11 13 16 19 22 10 11 12 " +
                      "13 17 20 23 10 11 12 14 17 20 24 10 12 12 14 18 21 25 10 12 13 15 18 22 26 " +
                      "11 12 13 15 19 22 26 11 12 13 15 19 23 27 11 13 14 16 20 24 28 11 13 14 16 " +
                      "21 24 29 11 13 14 17 21 25 30 12 13 15 17 22 26 31 12 14 15 18 22 26 31 12 " +
                      "14 15 18 23 27 32 12 14 16 18 24 28 33 12 14 16 19 24 29 34 ";

var bagsShipping = new Array(35) // up to 35 bags (35lbs) in 7 ups zones
for (i=0; i <35; i++){
 bagsShipping[i] = new Array(8)
  }

var count = 0
var price = ""
var bags = 0



for (i=0; i <zonePriceBagsString.length; i++){ // one char at a time
if (zonePriceBagsString.charAt(i) == " "){ // found the end of cost
   bagsShipping[bags][count] = price;
   price = ""; //reset
   count = count +1
   if (count == 7){bags = bags +1; count = 0;} // 7 zones
}
else{
price = price + zonePriceBagsString.charAt(i);
}

}
 


var ZipCodeZone = "Pick-UpZ1004 Thru 005Z5010 Thru 046Z5047Z6048 Thru 089Z5100 Thru 129Z5130 Thru 132Z4133 Thru 139Z5140 Thru 179Z4180 Thru 199Z5200 Thru 213Z4" + // info from UPS
"214Z5215Z4216Z5217Z4218 Thru 219Z5220 Thru 229Z4230 Thru 238Z5239 Thru 274Z4275 Thru 279Z5280 Thru 282Z4283 Thru 285Z5" +
"286 Thru 289Z4290 Thru 292Z5293Z4294 Thru 295Z5296 Thru 297Z4298 Thru 299Z5300 Thru 303Z4304Z5305 Thru 307Z4308 Thru 310Z5311Z4" +
"312 Thru 329Z5330 Thru 334Z6335 Thru 338Z5339Z6341Z6342 Thru 347Z5349Z6350 Thru 359Z4360 Thru 361Z5362Z4363 Thru 369Z5370 Thru 389Z4" +
"390 Thru 396Z5397Z4398Z5399Z4400 Thru 406Z3407 Thru 409Z4410Z3411 Thru 418Z4420Z3421 Thru 422Z4423 Thru 424Z3425 Thru 426Z4" +
"427 Thru 438Z3439Z4440 Thru 443Z3444 Thru 447Z4448 Thru 456Z3457Z4458 Thru 459Z3460 Thru 469Z2470 Thru 472Z3473Z2474 Thru 478Z3479Z2480 Thru 487Z3" +
"488 Thru 491Z2492Z3493 Thru 495Z2496 Thru 499Z3500 Thru 504Z3505Z4506 Thru 507Z3508Z4509Z3510 Thru 516Z4520Z2521 Thru 526Z3" +
"527 Thru 539Z2540Z4541 Thru 547Z3548Z4549Z3550 Thru 558Z4559Z3560 Thru 574Z4575 Thru 577Z5580 Thru 581Z4582 Thru 588Z5" +
"590 Thru 591Z6592 Thru 593Z5594 Thru 599Z6600 Thru 619Z2620 Thru 636Z3637 Thru 649Z4650 Thru 652Z3653 Thru 675Z4676 Thru 679Z5680 Thru 689Z4" +
"690 Thru 693Z5700 Thru 714Z5716 Thru 717Z4718Z5719 Thru 729Z4730 Thru 739Z5740 Thru 744Z4745Z5746Z4747 Thru 748Z5749Z4750 Thru 778Z5" +
"779 Thru 785Z6786 Thru 787Z5788Z6789 Thru 796Z5797 Thru 799Z6800 Thru 812Z5813 Thru 816Z6820Z5821Z6822 Thru 823Z5824 Thru 825Z6" +
"826 Thru 828Z5829 Thru 834Z6835 Thru 838Z7840 Thru 847Z6850 Thru 854Z7855Z6856 Thru 858Z7859 Thru 863Z6864Z7865 Thru 880Z6881Z5" +
"882 Thru 883Z6884Z5885Z6889 Thru 898Z7900 Thru 933Z7934Z8935 Thru 938Z7939 Thru 941Z8942Z7943 Thru 951Z8952 Thru 953Z7954 Thru 955Z8" +
"956 Thru 961Z7970 Thru 994Z7"; // UPS ground zone chart in a single string  




function cent(amount) {
// returns the amount in the .99 format
    amount -= 0;
    return (amount == Math.floor(amount)) ? amount + '.00' : (  (amount*10 == Math.floor(amount*10)) ? amount + '0' : amount);
}




function InitialZones (FormNumber){ // ups shipping zones - each form will have one of these if the shipping cost varies 
var idx = 0
var position = 0
document.forms[FormNumber].elements[zipBoxLocation].length = 152 // retreive from UPS ground zone string ( the letter "Z" was added
                                                   // to the text file for use here)   
  for (i=0;i<ZipCodeZone.length; i++){ // one letter at a time 
   if (ZipCodeZone.charAt(i) == "Z"){ // found the end of zone range
    idx = idx + 1; // increment form
    document.forms[FormNumber].elements[zipBoxLocation].options[idx].text = ZipCodeZone.substring(position,i);
	i = i + 1;  
    zone[idx] = ZipCodeZone.charAt(i);
	position = i +1;// reset pointer
   }
   }
} // function end





function upDateAll () { // display shipping costs in all forms when zip is selected anywhere
currentZip = document.forms[0].elements[19].selectedIndex;// set global zip  
updbags(0,0);
} // function end








function initializeBags(formNumber){ // bags
var dropsStartAt = 3
 for (j=dropsStartAt;j<(TotalColors+dropsStartAt); j++){
   document.forms[formNumber].elements[j].length = 36
  for (i=0; i<36; i++){
   document.forms[formNumber].elements[j].options[i].value = i;
   document.forms[formNumber].elements[j].options[i].text = i;
   }
}
} // function end






function updbags (arg,alrt) { // arg = 1 call paypal
document.forms[BagForm].elements[bagdescricp_loc[0]].value = "";                        // line 1
document.forms[BagForm].elements[bagdescricp_loc[1]].value = "Select bag colors.";// line 2
document.forms[BagForm].elements[bagdescricp_loc[2]].value = "";                        // line 3
document.forms[BagForm].elements[qty_loc].value = "To Display Total";
document.forms[BagForm].elements[ship_loc].value = "Select Zipcode";
document.forms[BagForm].elements[total_loc].value = "& Bag Colors";
var dropsStartAt = 3
var totalBags = 0;
var bagString = "";
var bagStringPP = "";
for (j=dropsStartAt;j<TotalColors+dropsStartAt; j++){
totalBags = totalBags +parseFloat(document.forms[BagForm].elements[j].options[document.forms[BagForm].elements[j].selectedIndex].value);
if (document.forms[BagForm].elements[j].options[document.forms[BagForm].elements[j].selectedIndex].value != 0){
 bagString = bagString + document.forms[BagForm].elements[j].options[document.forms[BagForm].elements[j].selectedIndex].value + "-" + AllColors[j-dropsStartAt] + " ";
 bagStringPP = bagStringPP + document.forms[BagForm].elements[j].options[document.forms[BagForm].elements[j].selectedIndex].value + "-" + AllColorsPP[j-dropsStartAt] + " ";
}
}
if (totalBags > 35){
bagString = "";
bagStringPP = "";
totalBags = 0;
startString = "";
alert ("35 Bags per box limit! For orders over 35 break up into multiple orders.");
for (j=dropsStartAt;j<TotalColors+dropsStartAt; j++){
document.forms[BagForm].elements[j].selectedIndex = 0};
}
if (totalBags != 0) {
 if (totalBags > 1){plural = " Bags";}else{plural = " Bag ";}
 bagString1 = bagString + " ("+ totalBags + plural +")";
 bagString2 = "";
 bagString3 = "";
 if (bagString1.length > 60)  {
 startString = bagString1;
if (bagString1.length < 120) { 
 splitPoint = splitstring (bagString1,2);
 bagString1 = startString.substring(0,splitPoint);
 bagString2 = startString.substring(splitPoint,startString.length +1);   
 }
else
 {
 splitPoint = splitstring (bagString1,3);
 bagString1 = startString.substring(0,splitPoint);
 bagString2 = startString.substring(splitPoint,startString.length +1);
 startString = bagString2;
 splitPoint = splitstring (bagString2,2);
 bagString2 = startString.substring(0,splitPoint);
 bagString3 = startString.substring(splitPoint,startString.length +1);
 }
 } 
// put in middle box if only one string

if (bagString2 == "") {
bagString2 = bagString1
bagString1 ="";
}
 document.forms[BagForm].elements[bagdescricp_loc[0]].value = bagString1; // line 1
 document.forms[BagForm].elements[bagdescricp_loc[1]].value = bagString2; // line 2
 document.forms[BagForm].elements[bagdescricp_loc[2]].value = bagString3; // line 3
if (currentZip !=0){
  if (totalBags <16) { boxCost = 3.00;} else {boxCost = 4.00;}
   if (currentZip == 1){// free shipping
    document.forms[BagForm].elements[ship_loc].value = " FREE ";
    totalBagCost = BasePrice*totalBags;
    document.forms[BagForm].elements[total_loc].value = "$" + totalBagCost + ".00";
    }
    else
	{
   pricePlusBox =  parseFloat(bagsShipping[totalBags -1][zone[currentZip]-2]) + boxCost;
   document.forms[BagForm].elements[ship_loc].value = "$" + pricePlusBox + ".00 ";
   totalBagCost = BasePrice*totalBags + pricePlusBox;
   document.forms[BagForm].elements[total_loc].value = "$" + totalBagCost + ".00";
    }
  }
  document.forms[BagForm].elements[qty_loc].value = "$" + BasePrice*totalBags + ".00";
}
if (arg != "0"){//call paypal
 if(currentZip != 0){
  if (totalBags != 0){
  blk2q = "1";
  blk4a = cent(totalBagCost);
  blk3n = escape (totalBags + " Cordura Bean "+ plural +">" + bagStringPP);
  CallPay ();
  }else {alert ("Please select quantity.");}
 }
 else{alert ("Please select a zip code.");}
}//arg
} // function end





function splitstring (strng,divisions){
midpnt = Math.round(strng.length / divisions);
found = 0
 while (found == 0){
 if (strng.charAt(midpnt) == "-") {found = 1;}
 else {midpnt = midpnt +1;}	
 }
found = 0
 while (found == 0){
 if (strng.charAt(midpnt) == " "){found = 1;}
 else {midpnt = midpnt -1;}	
 }
return midpnt;
} // function end





function CallPay () { // call the PayPal shopping cart
  window.open (blk1 + blk1a + // open the cart window
               blk2 + blk2a + blk2q +blk3 + blk3n + blk4 + blk4a + blk5,
               "paypal",
               winpar);
} // function end

function CallView () { // call the PayPal shopping cart view
  window.open (blk1 + blk1d +  // open the PayPal cart window
               blk2,
               "paypal",
               winpar);
} // function end





function preload(){
  Low21 = new Image();
 Low21.scr = "images/bag21.gif";
 High21 = new Image();
 High21.scr = "images/bag21h.gif";

 Low22 = new Image();
 Low22.scr = "images/bag22.gif";
 High22 = new Image();
 High22.scr = "images/bag22h.gif";

 Low23 = new Image();
 Low23.scr = "images/bag23.gif";
 High23 = new Image();
 High23.scr = "images/bag23h.gif";

 Low24 = new Image();
 Low24.scr = "images/bag24.gif";
 High24 = new Image();
 High24.scr = "images/bag24h.gif";
} // function end





function dopicswap (imageno){
if (homeimagebags == imageno)
{
 document.bag21.src="images/bag21h.gif"; // put clickbox hi res image in 1
 switch(imageno){ // low res back
 case 2:
 document.bag22.src="images/bag22.gif"; // put clickbox
 break;
 case 3:
 document.bag23.src="images/bag23.gif"; // put clickbox
 break;
 case 4:
 document.bag24.src="images/bag24.gif"; // put clickbox
 break;
  } //switch
 homeimagebags = 1
}//if
else{
 switch(imageno){ 
 case 2:
 document.bag21.src="images/bag2" + imageno + "h.gif"; // put clickbox hi res image in 1
 document.bag22.src="images/bag21.gif"; // put low res 1 in clickbox
 break;
 case 3:
 document.bag21.src="images/bag2" + imageno + "h.gif"; // put clickbox hi res image in 1
 document.bag23.src="images/bag21.gif"; // put low res 1 in clickbox
 break;
 case 4:
 document.bag21.src="images/bag2" + imageno + "h.gif"; // put clickbox hi res image in 1
 document.bag24.src="images/bag21.gif"; // put low res 1 in clickbox
 break;
 } //switch
 if (homeimagebags != 1){ 
  switch(homeimagebags){ 
  case 2:
  document.bag22.src="images/bag22.gif"; // 
  break;
  case 3:
  document.bag23.src="images/bag23.gif"; // 
  break;
  case 4:
  document.bag24.src="images/bag24.gif"; // 
  break;
  } //switch
 }//if
homeimagebags = imageno
}
displayinfo (homeimagebags)
} // function end 






function displayinfo (imageno){
switch(imageno) {// image switch
  case 1 :
  document.forms[BagForm].elements[0].value = "Our waterproof beaver bags look and feel great.";
  document.forms[BagForm].elements[1].value = "Competition one pound bags are made with";
  document.forms[BagForm].elements[2].value = "Cordura® fabric and a water proof filler.";
  break;
  case 2 :
  document.forms[BagForm].elements[0].value = "Cordura® - for maximum durability.";
  document.forms[BagForm].elements[1].value = "3 x more durable than polyester or nylon";
  document.forms[BagForm].elements[2].value = "10 x more durable than cotton duck";
  break;
  case 3 :
  document.forms[BagForm].elements[0].value = "The stiffness of the bag is controlled using a filler";
  document.forms[BagForm].elements[1].value = "mix. A perfect balance of weight (1lb) and stiffness is";
  document.forms[BagForm].elements[2].value = "achieved, making the bags feel loose and easy to throw.";
  break;
  case 4 :
  document.forms[BagForm].elements[0].value = "Thirteen colors to choose from.";
  document.forms[BagForm].elements[1].value = "Bags are filled with waterproof filler and";
  document.forms[BagForm].elements[2].value = "sewn with high performance nylon thread.";
  break;
}// image switch 
} // function end





function initialazeAll (){
 initializeBags (BagForm);
 updbags (0,0);
 InitialZones (BagForm);
 displayinfo (1);
 
} // function end