var blk1  = "https://www.paypal.com/cgi-bin/webscr?cmd=_cart";
var blk1a = "&add=1";
var blk1d = "&display=1";
var blk1u = "&upload=1";
var blk2  = "&business=";
var blk2a = "&quantity=";
var blk2q = 0;
var blk3  = "&item_name=";
var blk3n = "";
var bcode = "";
var blkcc = "";
var bmisc = "";  // stuff not supported anywhere else.
var blk4  = "&amount=";
var blk4a = "6.66";
var imgx  = "dummy.jpg";  // place for cart thumbnail image
var cart_winpar = "width=800,scrollbars,location,resizable,status,height=";
var detail_winpar = "width=650,height=450,scrollbars,location,resizable,status";
var paypal_winpar = "width=800,height=650,scrollbars,location,resizable,status";
var print_winpar = "width=800,height=550,scrollbars,location,resizable,status";
	      
var cmax  = 16;            // max items in cart
var cats  = 10;             // max number of categories allowed
var pcat  = 0;             // item product category
var ctot  = new Object (); // amt & qty totals by pcat
var ttax  = 0;
var hand = 0;           // cart totals
var tpkg;           // total packages
var qtyd;
var tqty = 0;       // the total quantity of items ordered
var cook;           // place for item cookie object
var tprc = 0;       // total price of items in cart
var gtot;           // order grand total
 
var dqty = new Array ();  // item discount qty breakpoint
var damt = new Array ();  // percent discount
var dn   = 0;             // number of discount brkpts
var aqty = new Array ();  // item quantity amt breakpoint
var aamt = new Array ();  // price at breakpoint
var an   = 0;             // number of amt brkpts
var coupval = "";         // coupon value user entered
var coupons = new Array ("coup1", "coup2");
var coupond = new Array (10, 15);  // discount aray
var coup = -1;            // cart discount coupon not active
// cart display options...
var on  = 1;       // on and off variables
var off = 0;

var discount_amount = 0;
var discount_cds = 0;
var total_book = 0;
var total_video = 0;
var total_audio = 0;
var total_sticker = 0;

function submit_page()
{
  SetDesc('Tax Deductable Donation to Alive & Well'); 
  SetCode('tf_donation'); SetPcat('5'); 

  if (ReadForm (this)) 
    {
      CallView (); 
    }

  ClearAll (); 
  return false;
}

function CallPrint()
{
  var cartwin;
  cartwin = window.open ("donations_print.html", "", print_winpar);
}

function CallPay () { // call the PayPal shopping cart
var strn,cartwin;
  blkcc = "";                  // check default settings
  if (root.xx_cur.length > 0)  // record currency entry 
    blkcc = blkcc + "&currency_code=" + root.xx_cur;
  if (root.xx_lc.length > 0)   // record user entry
    blkcc = blkcc + "&lc=" + root.xx_lc;
  if (root.xx_can.length > 0)  // set cancel return path
    blkcc = blkcc + "&cancel_return=" + root.xx_can;
  if (root.xx_ret.length > 0)  // set return path 
    blkcc = blkcc + "&return=" + root.xx_ret;
  if (root.xx_sty.length > 0)  // set display style
    blkcc = blkcc + "&page_style=" + root.xx_sty;

  strn = blk1 + blk1u + blk2 + root.xx_id + blkcc + bmisc + Xbild ();
  ClearAll ();
  cartwin = window.open (strn, "paypal", paypal_winpar);
  cartwin.focus ();
}

function call_detail_win(strn)
{
  var cartwin;
  cartwin = window.open (strn, "", detail_winpar);
  cartwin.focus ();
}

function CallView () 
{ 
  var cartwin;

  var cart_param_str = cart_winpar + (240);

  cartwin = window.open ("donations_cart.html", "", cart_param_str);
  cartwin.focus ();
}

function ChkOpts () 
{  
  cook = new Cookie (document, "paycart1");
  cook.load();

  if (cook.prc <= 0.0)
    {
      alert("This donation is too small");
      return false;
    }

  if (isNaN(cook.prc))
    {
      alert("Please enter a valid number");
      return false;
    }

  return true;
}

function ClearAll () {  // wipe out the last entry
  blk2q = "";  // quantity
  blk3n = "";  // name
  blk4a = "0"; // price
  bcode = "";  // item number
  bmisc = "";  // extra data
  imgx  = "dummy.jpg";
  dn    = 0;
  an    = 0;
  pcat  = 0;
}

function ClearCart () 
{     
  ClearEntry();
}

function ClearEntry () 
{  
  cook = new Cookie (document, "paycart1");
  InitData ();
}

function Cookie(document, name, hours, path, domain, secure) 
{
  this.$document = document;  // required
  this.$name = name;          // required

  if (hours) this.$expiration = new Date((new Date()).getTime() + hours * 3600000);
  else this.$expiration = null;

  this.$path = "/";  // force, for now

  if (domain) this.$domain = domain; else this.$domain = null;
  if (secure) this.$secure = true;   else this.$secure = false;
}

function _Cookie_store() 
{  
  var cookieval = "";  // clear actual cookie value

  for(var prop in this) 
    {  // Ignore "$" properties, and methods
      if ((prop.charAt(0) == '$') || ((typeof this[prop]) == 'function')) 
        continue;
      if (cookieval != "") 
	cookieval += '&';

      cookieval += prop + ':' + escape(this[prop]);
    }

  var cookie = this.$name + '=' + cookieval;

  if (this.$expiration) cookie += '; expires=' + this.$expiration.toGMTString();
  if (this.$path) cookie += '; path=' + this.$path;
  if (this.$domain) cookie += '; domain=' + this.$domain;
  if (this.$secure) cookie += '; secure';
  if (root.xx_cdmp == on)           // cookie diagnostic dump
    alert(cookie.length + " chars - " + cookie);

  this.$document.cookie = cookie;  // store with magic property
}

function _Cookie_load() 
{  
  var i;
  var allcookies = this.$document.cookie;

  if (allcookies == "") return false;

  var start = allcookies.indexOf(this.$name + '=');

  if (start == -1) return false;   

  start += this.$name.length + 1;  
  var end = allcookies.indexOf(';', start);

  if (end == -1) end = allcookies.length;

  var cookieval = allcookies.substring(start, end);
  var a = cookieval.split('&');  // array of name/value pairs.

  for(i=0; i < a.length; i++)  // Break each pair into an array.
    a[i] = a[i].split(':');

  for(i=0; i<a.length; i++)
    this[a[i][0]] = unescape(a[i][1]);

  return true;
}

function _Cookie_remove() 
{  
  var cookie;
  cookie = this.$name + '=';

  if (this.$path) cookie += '; path=' + this.$path;
  if (this.$domain) cookie += '; domain=' + this.$domain;

  cookie += '; expires=Fri, 02-Jan-1970 00:00:00 GMT';

  this.$document.cookie = cookie;  // magic store
}

function Dollar (val) 
{  
  var str,pos,rnd=0;

  if (val == 0) return "0.00";

  if (val < .995) 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 InitData () {  // zap a data cookie
  cook.pcat = 0;
  cook.qty  = 0;
  cook.cod  = "";
  cook.ihnd = 0;
  cook.des  = "";
  cook.prc  = 0;
  cook.imgx = "dummy.jpg";
  cook.store();    // stash it off
}

function InitRoot () {  // Build the base root cookie
var i;
  root.coup  = coup;
  root.chkby = off;
  root.stxt0 = "Select Location";
  root.ptxt0 = "Paypal";
  root.xx_can  = "www.aliveandwell.org/html/top_bar_pages/cart.html";
  root.xx_cdmp = off;
  root.xx_cper = 0;
  root.xx_cur  = "USD";
  root.xx_dadd = on;
  root.xx_lc   = "US";
  root.xx_mamt = 0;
  root.xx_mqty = 0;
  root.xx_prt  = on;
  root.xx_ret  = "";
  root.xx_sty  = "";

  root.store();
}

function PrintCart (remove) 
{  
  var i;
  var columns = 3;
  tprc = 0;                 // total price of cart
  gtot = 0;                 // Grand total

  document.writeln ('<tr align=right class=MAINSTYLE>\n');

  document.writeln ('<th align=left>Description</th>\n');

  document.writeln ('<th>Amount</th>\n');
  document.writeln ('</tr>\n');

  cook = new Cookie (document, "paycart1");
  cook.load();

  if (cook.qty > 0) { PrintItem (i, remove); }

  tprc = Dollar (tprc)*1.0;             // round, float

  gtot = Dollar (tprc)*1.0;
  root.store();

  root.store();
}

function PrintItem (i, remove) 
{  
  var tm,t1,t2, tmp, tax_amount;
  document.writeln ('<tr valign = "top" align = "right">');

  document.writeln ('  <td class=MAINSTYLE align = "left">', cook.des,'</td>');

  tm = Dollar (cook.prc);
  document.writeln ('  <td width=75 class=MAINSTYLE>', tm,'</td>');

  document.writeln ('</tr>');
}

function ReadForm (obj1) 
{ 
  blk4a = 0.0;

  if (document.forms['donation_levels'].DONATION_AMT_10.checked) { blk4a = document.forms['donation_levels'].DONATION_AMT_10.value; }
  if (document.forms['donation_levels'].DONATION_AMT_25.checked == 1) { blk4a = document.forms['donation_levels'].DONATION_AMT_25.value; }
  if (document.forms['donation_levels'].DONATION_AMT_50.checked == 1) { blk4a = document.forms['donation_levels'].DONATION_AMT_50.value; }
  if (document.forms['donation_levels'].DONATION_AMT_100.checked == 1) { blk4a = document.forms['donation_levels'].DONATION_AMT_100.value; }
  if (document.forms['donation_levels'].DONATION_AMT_OTHER.checked == 1) { blk4a = document.forms['donation_levels'].DONATION_AMT_OTHER_VALUE.value; }

  blk2q = 1;
  StoreCart ();
  return ChkOpts();
}

function SetCode (cd) { // set product code
  bcode = cd;
}

function SetDesc (strn) {  // set the desc field
  blk3n = strn;
}

function SetID (strn) {  // set the PayPal ID of this user
  blk2b = strn;
}

function SetImg (strn) {  // set the image for the cart
  imgx = strn;
}

function SetPcat (val) {
  if (val >= cats) {
    alert ("Only " + cats + " product categories allowed!\n\n" +
           "Correct your HTML!");
    return;
  }
  pcat = val;
}

function SetPrice (strn) 
{  
  ClearCart();
  blk4a = strn;
}

function zero_field()
{
  document.forms['donation_levels'].DONATION_AMT_OTHER_VALUE.value = "";
  SetPrice(0.0);
}

function check_other_amount()
{
  document.forms['donation_levels'].DONATION_AMT_OTHER.checked = 1;
}

function StoreCart () 
{  
  cook = new Cookie(document, "paycart1");
  cook.pcat = pcat
  cook.cod  = bcode;
  cook.des  = blk3n;
  cook.prc  = blk4a;
  cook.imgx = imgx;
  cook.qty = 1;
  cook.store();
  ClearAll ();
}

function Xbild () 
{
  var str  = "";              // string to PayPal
  var tmps = root.stxt0;
  var j = 1;
  
      cook = new Cookie (document, "paycart1");
      cook.load();

      if (cook.qty > 0) 
        {  
          str = str + "&quantity_"    + j + "=" + cook.qty;
          str += "&item_name_"   + j + "=" + escape (cook.des);
          str += "&amount_"      + j + "=" + Dollar (cook.prc*1.0);
	  str += "&os0_"       + j + "=" + escape (tmps);
        }    

  return str;
}

function check_out(strn)
{
  if (ChkOpts ()) 
    {
      if (strn == "paypal")
	{
          CallPay();
	}
      else
	{
	  CallPrint();
	}
    }
}

function payment_method(obj) 
  {  
    var pos;

    pos = obj.selectedIndex;  // which option selected
  
    root.ptxt0 = obj.options[pos].text;
    root.store();
  }

function LoadIt () 
{
  Cookie.prototype.store  = _Cookie_store;   
  Cookie.prototype.load   = _Cookie_load;
  Cookie.prototype.remove = _Cookie_remove;

  root = new Cookie (document, "paycart0");  // special root cookie

  if (!root.load () || !root.coup) 
    {
      InitRoot ();                  // gotta create it
    } 
  else 
    {                        // load up globals
      coup  = root.coup;
    }
      cook = new Cookie (document, "paycart1");

      if (!cook.load () || !cook.des) 
        {
          InitData ();              
        }
}

