var aqty = new Array ();  // amount qty breakpoint
var aamt = new Array ();  // amount to charge
var an   = 0;             // number of discount brkpts

var hqty = new Array ();  // handling qty breakpoints
var hamt = new Array ();  // amount charged
var hn   = 0;             // number of handling brkpts

var qqty = new Array ();  // quantity discount breakpoints
var qamt = new Array ();  // amount of discount
var qd   = 0;             // number of qty breakpoints

var sqty = new Array ();  // shipping qty breakpoints
var samt = new Array ();  // amount charged
var sn   = 0;             // number of shipping brkpts
var stxt = "";            // shipping type text

var taxp = 0;             // tax percent for this order

var coup = -1;               // discount coupon not active
var cdis = 0;                // amount of coupon discount (%)
var coupons = new Array ();  // array of valid coupon names
coupons[0] = "coup1";        // list as many as you want...
coupons[1] = "coup2";


function ChkCoup (val, amt) {  // check for a discount coupon
var i;
  coup = -1;              // assume the worst
  cdis = 0;
  if (amt == "" || isNaN (amt)) {
    alert ("\n\n\n   Coup arg not valid percent (01)  \n\n\n");
    return;
  }
  for (i=0; i<coupons.length; i++) {
    if (val == coupons[i]) {
      coup = 1;           // user hit the coupon value
      cdis = amt;         // remember the discount amt
      alert ("\n\n\n   Valid coupon code!  \n\n\n");
      return;
    }
  }
  alert ("\n\n\n  '" + val + "' not a valid code!  \n\n");
}

function ChkFlg (amt, temp) { // check for special flag char
var pos;
  pos  = temp.indexOf ("@");  // is there an initial value?  
  if (pos > 0) amt = temp.substring (pos + 1)*1.0;
  pos  = temp.indexOf ("+");  // is there a price adjustment?  
  if (pos > 0) amt = amt + temp.substring (pos + 1)*1.0;
  pos  = temp.indexOf ("%");  // is there a percent adjustment?  
  if (pos > 0) amt = amt * (1.0 + temp.substring (pos + 1)/100.0);
  return amt;
}

function ClearAll () {
  sn = 0;  // reset shipping indicator
}

function Dollar (val) {  // force to valid dollar amount
var str,pos,rnd=0;
  if (val < 1) rnd = 1;
  str = escape (val*1.0 + 0.005001 + rnd);  // float, round, escape
  pos = str.indexOf (".");
  if (pos > 0) str = str.substring (rnd, pos + 3);
  return str;
}

function Process (obj1) {      // process the form, but no submit
var i,j,obj,bqty,bamt,btxt,tdis,thnd,tshp,ttax,val,pos;
  if (sn == 0) {
    alert("Select shipping method!");
    return false;
  }
  amt = 0;  // shipping amt we must init to make sure it works
  btxt = obj1.basedes.value;          // reload desc
  bamt = obj1.baseamt.value*1.0;      //  and amount
  bqty = obj1.quantity.value;         // selected quantity
  if (isNaN(bqty) || bqty == "" || bqty < 1) {
    alert ("\nYou did not select a quantity. \n1 Book will be added to your shopping cart  \n");
    bqty = 1;
    obj1.quantity.value = "1";
  }
  bqty = bqty*1.0;                    // float that sucker

  for (i=0; i<obj1.length; i++) {     // run whole form
    obj = obj1.elements[i];           // ref particular element
    if (obj.name != "") continue;     // skip named elements
    if (obj.type == "select-one") {   // dropdowns
      pos = obj.selectedIndex;        // which option selected
      val = obj.options[pos].value;   // get selection
      bamt = ChkFlg (bamt, val);      // any flag chars
      btxt = btxt + ", " + val;
    } else
    if (obj.type == "select-multiple") { // one or more
      for (j=0; j<obj.options.length; j++) { // run all options
        if (obj.options[j].selected) {
          val = obj.options[j].value;
          bamt = ChkFlg (bamt, val);     // any flag chars
          btxt = btxt + ", " + val;
        }
      }
    } else
    if (obj.type == "checkbox") {     // checkboxes
      if (obj.checked) {              // just the selected ones
        val = obj.value;              // the value of it
        bamt = ChkFlg (bamt, val);    // any flag chars
        btxt = btxt + ", " + val;
      }
    } else
    if (obj.type == "text") {         // text
      if (obj.value.length > 0) {
        val = obj.value;              // the value of it
        btxt = btxt + "," + val;
      }
    }
  }

  for (i=an-1; i>=0; i--) {       // qty amount?
    if (bqty >= aqty[i]) {        // qty brkpt
      bamt = aamt[i]*1.0;         // set amount from qty
      btxt = btxt + ", AMT=" + Dollar (bamt);
      break;                      // get out, now
    }
  }

  tdis = 0;                       // qty discounts
  for (i=qd-1; i>=0; i--) {       // run backwards
    if (bqty >= qqty[i]) {        // qty brkpt
      tdis = qamt[i]*1.0;
      bamt = bamt - tdis;         // Deduct from item price
      btxt = btxt + ", $" + Dollar (tdis) +" Discount per book";
      break;                      // get out, now
    }
  }

  if (coup > 0 &&                 // coupon discount percent?
      cdis > 0) {
    bamt = bamt * (1.0 - cdis/100.0);   
    btxt = btxt + ", COUP=" + cdis + "%";
  }

  ttax = 0;
  if (taxp > 0 &&              // have a percent recorded?
      obj1.tax) {              // check for override
    ttax = bamt * taxp/100.0;  // yep - calculate it.
    obj1.tax.value = Dollar (ttax * bqty);
  }
 
  thnd = 0;                  // handling charges
  for (i=hn-1; i>=0; i--) {  // run backwards
    if (bqty >= hqty[i]) {   // qty brkpt
      thnd = hamt[i];        // set hand amount
      obj1.handling.value = Dollar (thnd);
      break;                 // get out, now
    }
  }

  tshp = 0;                  // shipping charges
  for (i=sn-1; i>=0; i--) {  // run backwards
    if (bqty >= sqty[i]) {   // qty brkpt
      tshp = samt[i]*1.0;    // set shipping amount
      // btxt = btxt + ", SHP:" + stxt;  // Matt - I commented out this line so the SHP text doe not appear in the PayPal description
      break;                 // get out, now
    }
  }

  obj1.shipping.value = Dollar (tshp*1.0 + amt*1.0);
  obj1.amount.value = Dollar (bamt);  // plug amount
  obj1.item_name.value = btxt;        // and descriptiuon
}

function SetHN (q1, h1) {      // set qty handling breakpoints
var i;
  hn = 0;                      // count of breakpoints
  for (i=0; i<arguments.length; i=i+2) {
    hqty[hn] = arguments[i];   // qty breakpoint
    hamt[hn] = arguments[i+1]; // handling charge
    hn = hn + 1;               // number of bkpts
  }
}

function SetQA (q1, a1) {      // set qty amount breakpoints
var i;
  an = 0;                      // count of breakpoints
  for (i=0; i<arguments.length; i=i+2) {
    aqty[an] = arguments[i];   // quantity
    aamt[an] = arguments[i+1]; // amount
    an = an + 1;               // number of discount bkpts
  }
}

function SetQD (q1, d1) {      // set qty discount breakpoints
var i;
  qd = 0;                      // count of breakpoints
  for (i=0; i<arguments.length; i=i+2) {
    qqty[qd] = arguments[i];   // qty breakpoint
    qamt[qd] = arguments[i+1]; // discount amount
    qd = qd + 1;               // number of bkpts
  }
}

function SetSH (q1, s1) {      // set shipping breakpoints
var i;
  sn = 0;                      // count of breakpoints
  for (i=0; i<arguments.length; i=i+2) {
    sqty[sn] = arguments[i];   // price breakpoint
    samt[sn] = arguments[i+1]; // shipping charge
    sn = sn + 1;               // number of bkpts
  }
}

function SetTX (obj1) {  // Set tax percent for this order
var pos;
  pos  = obj1.selectedIndex;           // which item selected
  taxp = obj1.options[pos].value*1.0;  // float it
}

function Shipper (obj1) {  // process user-selected shipping
var obj,pos;
/*  What we're gonna do here is to assign shipping based on the user selection of where items are being shipped to.  We MUST leave it up to the user to tell us where he is (or where he wants the stuff shipped to.)  There is no other choice!  If a user wants something shipped to a place other than where he lives how do you test that?  You cannot!

After he pays (PayPal IPN confirmation, or just e-mail confirmation) you have no choice other than to ship to the specified destination. UNLESS, the shipping costa do NOT allow shipping to that destination!
*/
  SetSH ();  // Don't delete this!  Shows bad selection was made.
  pos = obj1.selectedIndex;       // which item was selected?
  stxt = obj1.options[pos].text;  // user selection...
  switch (pos) {
    case 0:  // no selection made, return.
      break;
    default:
      SetSH (1, 99);  // outlandish to detect.
      break;
  }
}

// Set Cart Variables
// this is the set-list of the site...
//SetQD (2, 10);   // quantity discount table
//  SetHN (1, 3, 2, 5);   // handling charges table
SetSH (1,5,2,10,3,15,4,20,5,25,6,30,7,35,8,40,9,45,10,50);         // shipping options set on this page










/****************************************************
     AUTHOR: WWW.CGISCRIPT.NET, LLC
     URL: http://www.cgiscript.net
     Use the code for FREE but leave this message intact.
     Download your FREE CGI/Perl Scripts today!
     ( http://www.cgiscript.net/scripts.htm )
****************************************************/
var win=null;
function NewWindow(mypage,myname,w,h,pos,infocus){
if(pos=="random"){myleft=(screen.width)?Math.floor(Math.random()*(screen.width-w)):100;mytop=(screen.height)?Math.floor(Math.random()*((screen.height-h)-75)):100;}
if(pos=="center"){myleft=(screen.width)?(screen.width-w)/2:100;mytop=(screen.height)?(screen.height-h)/2:100;}
else if((pos!='center' && pos!="random") || pos==null){myleft=0;mytop=20}
settings="width=" + w + ",height=" + h + ",top=" + mytop + ",left=" + myleft + ",scrollbars=no,location=no,directories=no,status=no,menubar=no,toolbar=no,resizable=no";win=window.open(mypage,myname,settings);
win.focus();}

