﻿/* 
   paypal user experience:
   'buy now' -> opens new window; if cancel, window remains open.
   'add to cart' -> opens new window; if continue shopping, window closes.
   'view cart'   -> opens new window; if continue shopping, window closes.
   => 'buy now' needs to mimic other behaviors or vice versa.
*/

function OnBuyOrAdd(cmd, item_name, item_name_encoded, item_number, amountT)
{
   OnBuyOrAddInternational(cmd, item_name, item_name_encoded, item_number, amountT, '0');
}

function OnBuyOrAddInternational(cmd, item_name, item_name_encoded, item_number, amountT, internationalShipping)
{
   // Convert text to number for calculations, then back to text.
   var amountN = Number(amountT); 
   if(document.getElementById('member').checked == true) amountN *= .9;
   if(document.getElementById('international'))
   {
      if(document.getElementById('international').checked == true) amountN += Number(internationalShipping);
   }

   var amount = amountN.toFixed(2);
   
   var hrefAddOrBuy = "https://www.paypal.com/cgi-bin/webscr";

   var aForm = document.createElement('form');
      aForm.method = 'post';
      aForm.action = hrefAddOrBuy;
   
   var inputCmd = document.createElement('input');
      inputCmd.type = 'hidden';
      inputCmd.name = 'cmd';
   var inputUploadOrAdd = document.createElement('input');
      inputUploadOrAdd.type = 'hidden';
      inputUploadOrAdd.value = '1';
   var inputBusiness = document.createElement('input');
      inputBusiness.type = 'hidden';
      inputBusiness.name = 'business';
      inputBusiness.value = 'sijo@wingchundo.com';
   var inputReturn = document.createElement('input');
      inputReturn.type = 'hidden';
      inputReturn.name = 'return';
      inputReturn.value = 'http://www.wingchundo.com/store/success.htm';
   var inputCancelReturn = document.createElement('input');
      inputCancelReturn.type = 'hidden';
      inputCancelReturn.name = 'cancel_return';
      inputCancelReturn.value = window.location;
      //inputCancelReturn.value = 'http://www.wingchundo.com/store/cancel.htm';
   
   var inputItemName = document.createElement('input');
      inputItemName.type = 'hidden';
      inputItemName.name = 'item_name';
      inputItemName.value = item_name;
   var inputItemNumber = document.createElement('input');
      inputItemNumber.type = 'hidden';
      inputItemNumber.name = 'item_number';
      inputItemNumber.value = item_number;
   var inputQuantity;  // Only created if cmd=='Add'.
   var inputAmount = document.createElement('input');
      inputAmount.type = 'hidden';
      inputAmount.name = 'amount';
      inputAmount.value = amount;
   var inputTax;       // Only created if cmd=='Add'.
   
   var inputLC = document.createElement('input');
      inputLC.type = 'hidden';
      inputLC.name = 'lc';
      inputLC.value = 'US';
   var inputCurrencyCode = document.createElement('input');
      inputCurrencyCode.type = 'hidden';
      inputCurrencyCode.name = 'currency_code';
      inputCurrencyCode.value = 'USD';
   var inputCustom = document.createElement('input');
      inputCustom.type = 'hidden';
      inputCustom.name = 'custom';
      inputCustom.value = '0';
   var inputSiteID = document.createElement('input');
      inputSiteID.type = 'hidden';
      inputSiteID.name = 'site_id';
      inputSiteID.value = '1';
   
   switch(cmd)
   {
      case "Buy":
         aForm.id = 'paypal_form_9_buy_now_button';
         inputCmd.value = '_xclick';
         inputUploadOrAdd.name = 'upload';
      break;
      
      case "Add":
         aForm.id = 'paypal_form_9_add_to_cart_button';
         inputCmd.value = '_cart';
         inputUploadOrAdd.name = 'add';
         
         inputQuantity = document.createElement('input');
         inputQuantity.type = 'hidden';
         inputQuantity.name = 'quantity';
         inputQuantity.value = '1';
         inputTax = document.createElement('input');
         inputTax.type = 'hidden';
         inputTax.name = 'tax';
         inputTax.value = '0';
      break;
      
      default:
         alert("Bad 'cmd' input to OnBuyOrAdd(): " + cmd);
         return;
   }

   aForm.appendChild(inputCmd);
   aForm.appendChild(inputUploadOrAdd);
   aForm.appendChild(inputBusiness);
   aForm.appendChild(inputReturn);
   aForm.appendChild(inputCancelReturn);
   aForm.appendChild(inputItemName);
   aForm.appendChild(inputItemNumber);
   if(cmd == 'Add') aForm.appendChild(inputQuantity);
   aForm.appendChild(inputAmount);
   if(cmd == 'Add') aForm.appendChild(inputTax);
   aForm.appendChild(inputLC);
   aForm.appendChild(inputCurrencyCode);
   aForm.appendChild(inputCustom);
   aForm.appendChild(inputSiteID);
   
   aForm.submit();
   
   var hrefBusiness = "&business=sijo%40wingchundo.com&return=http%3A%2F%2Fwww.wingchundo.com%2Fstore%2Fsuccess.htm&cancel_return=" + window.location + "&item_name=";
   //var hrefBusiness = "&business=sijo%40wingchundo.com&return=http%3A%2F%2Fwww.wingchundo.com%2Fstore%2Fsuccess.htm&cancel_return=http%3A%2F%2Fwww.wingchundo.com%2Fstore%2Fcancel.htm&item_name=";

   var href = hrefAddOrBuy;
   if(cmd == 'Buy')
   {
      href += "?cmd=_xclick&upload=1" + hrefBusiness + item_name_encoded + "&item_number=" + item_number + 
                         "&amount=" + amount +       "&lc=US&currency_code=USD&custom=0";
      window.location = href;
   }
   else // cmd == 'Add'
   {
      href += "?cmd=_cart&add=1"      + hrefBusiness + item_name_encoded + "&item_number=" + item_number + 
              "&quantity=1&amount=" + amount + "&tax=0&lc=US&currency_code=USD&custom=0";
      window.open(href);
   }
}

function OnView()
{
   var hrefView = "https://www.paypal.com/cart/display=1&bn=tm_gl_2.0&business=sijo@wingchundo.com";

   var aForm = document.createElement('form');
   aForm.id = 'paypal_form_9_view_cart_button';
   aForm.method = 'post';
   aForm.action = hrefView;
   
   var inputSiteID = document.createElement('input');
   inputSiteID.type = 'hidden';
   inputSiteID.name = 'site_id';
   inputSiteID.value = '1';
   aForm.appendChild(inputSiteID);
      
   aForm.submit();
   window.open(hrefView);
}

/*
var hrefBuy   = baseHref + "cgi-bin/webscr?cmd=_xclick&upload=1&business=sijo%40wingchundo.com&return=http%3A%2F%2Fwingchundo.com%2Findex.php%2Fsite%2Fsuccess%2F&cancel_return=http%3A%2F%2Fwingchundo.com%2Findex.php%2Fsite%2F&item_name=PERSONAL+CONFIDENCE+-+%2418.00&item_number=6           &amount=18.00      &lc=US&currency_code=USD&custom=0";
var hrefAdd   = baseHref + "cgi-bin/webscr?cmd=_cart&add     =1&business=sijo%40wingchundo.com&return=http%3A%2F%2Fwingchundo.com%2Findex.php%2Fsite%2Fsuccess%2F&cancel_return=http%3A%2F%2Fwingchundo.com%2Findex.php%2Fsite%2F&item_name=PERSONAL+CONFIDENCE+-+%2418.00&item_number=6&quantity=1&amount=18.00&tax=0&lc=US&currency_code=USD&custom=0";
var hrefBuy2  = baseHref + "cgi-bin/webscr?cmd=_xclick&upload=1&business=sijo%40wingchundo.com&return=http%3A%2F%2Fwingchundo.com%2Findex.php%2Fsite%2Fsuccess%2F&cancel_return=http%3A%2F%2Fwingchundo.com%2Findex.php%2Fsite%2F&item_name=" + "PERSONAL+CONFIDENCE" + "+-+%24" + "18.00" + "&item_number=" + "6" + "&amount=" + "18.00" + "&lc=US&currency_code=USD&custom=0";
var hrefView  = baseHref + "cart/display=1&bn=tm_gl_2.0&business=sijo@wingchundo.com";

var onclick = "window.open(this.href);return false;";
var idBuy  = "paypal_form_6_buy_now_button";
var idAdd  = "paypal_form_6_add_to_cart_button";
var idView = "paypal_form_6_view_cart_button";

var cmdBuy = "_xclick";
var upload = "1";
var business = "sijo@wingchundo.com";
//var returnUrl        = "http://wingchundo.com/index.php/site/success/";
//var cancel_returnUrl = "http://wingchundo.com/index.php/site/";
//var returnUrl        = "http://wingchundo.com/store/success.htm";
var cancel_returnUrl = "http://wingchundo.com/store/cancel.htm";
var lc = "US";
var currency_code = "USD";
var custom = "0";
var site_id = "1";

var cmdAdd = "_cart";
var add = "1";
var quantity = "1";
var tax = "0";

var item_name         = "PERSONAL CONFIDENCE &#45; .00";
var item_name_encoded = "PERSONAL+CONFIDENCE+-+%2418.00";
var item_number = "6";
var amount = "18.00";

var actionBuyOrAdd = "https://www.paypal.com/cgi-bin/webscr";
var clickUpload    = "?cmd=_xclick&upload=1";
var cartAdd        = "?cmd=_cart&add=1";
var hrefBusiness   = "&business=sijo%40wingchundo.com&return=http%3A%2F%2Fwingchundo.com%2Findex.php%2Fsite%2Fsuccess%2F&cancel_return=http%3A%2F%2Fwingchundo.com%2Findex.php%2Fsite%2F&item_name=";

var hrefBuy  = actionBuyOrAdd + 
               clickUpload + 
               hrefBusiness + 
               item_name_encoded + 
               "&item_number=" + 
               item_number + 
               "&amount=" + 
               amount + 
               "&lc=US&currency_code=USD&custom=0";
var hrefAdd  = actionBuyOrAdd + 
               cartAdd + 
               hrefBusiness + 
               item_name_encoded + 
               "&item_number=" + 
               item_number + 
               "&quantity=1&amount=" + 
               amount + 
               "&tax=0" + 
               "&lc=US&currency_code=USD&custom=0";
var hrefView = "https://www.paypal.com/cart/display=1&bn=tm_gl_2.0&business=sijo@wingchundo.com";

var actionView = hrefView;
*/

