// fnpay.js - Payment Functions (prices, order forms, PayPal)

//*******************************************//
//************ ORDER FORM FUNCTIONS *********//
//*******************************************//

// Order Form Global variables
var cbgmultcost = 0; // multiplied cost (USD or CAD)
var cbgtotcost = 0; // total cost including shipping (USD or CAD)

// Get exchange Rate (CAD to USD)
function ExchangeRate()
{
    return 0.75;
}

// Converts from CAD to USD 
function ExchangeCalc(value)
{
   return Math.round(value * ExchangeRate());
}

// Convert to money format xx.yy
// use "" for no currency.
function MoneyFormat(value, currency) {   
   
   // Convert decimal value to dollars and cents
   var totcents = Math.floor(value * 100)
   var dollarstr = Math.floor(totcents/100)
   var centstr = totcents%100

   // Add leading zero for values below 10 cents
   if (centstr < 10)
      centstr = "0" + centstr;
  
   // Return formatted string
   return ("$" + dollarstr + "." + centstr + " " + currency);
}

// Calculate updated total cost of purchase order.
function CostUpdate() {
   var ncopies = document.BUY.QUANTITY.value;	// number or copies to buy
   var currency = document.BUY.CURRENCY.value;  // currency to display (US or CAD)
   var shipcost;  // shipping cost
   var percopy;   // per copy cost
   
   // Get Per Copy Value
   percopy = PerCopyGet(currency);
   document.BUY.PERCOPY.value = MoneyFormat(percopy, currency);

   // Get Multiplied cost
   cbgmultcost = ncopies * percopy;
   document.BUY.MULTCOST.value = MoneyFormat(cbgmultcost, currency);

   // Get Shipping cost
   shipcost = ShipCalc(ncopies, currency);
   document.BUY.SHIPCOST.value = MoneyFormat(shipcost, currency);  

   // Calculate total cost
   cbgtotcost = cbgmultcost + shipcost;
   document.BUY.TOTCOST.value = MoneyFormat(cbgtotcost, currency); 
} 

// Calculate Shipping Cost
function ShipCalc(number, currency)
{
   var shipping = 0; // init shipping value

   // Shipping amount (CAD)
   if ((number >= 1) && (number <= 2)) 
      shipping = 4.50;
   else if ((number >= 3) && (number <= 4)) 
      shipping = 6.00;
   else if ((number >= 5) && (number <= 8)) 
      shipping = 10.00; 
   else                  
      shipping = 20.00;
  
   // Convert to USD if necessary. 
   if (currency == "USD") 
      shipping = ExchangeCalc(shipping);

  // Return shipping value
  return shipping;
}

// Display Shipping Costs
function ShipDisplay()
{
  var tagwrite = "";
  var quantity, desc, currency, pricecad, priceusd; // loop variables

  // Shipping Categories [quantity, desc]
  var parray = new Array();
  parray[0] = [1, "1 to 2"];
  parray[1] = [3, "3 to 4"];
  parray[2] = [5, "5 to 8"];
  parray[3] = [9, "9 or more"];
  var count = 4;
  var catid = 0;

  tagwrite = tagwrite + "<table class='TBBACK' align='top' border='1' bordercolor='black' cellspacing='0' cellpadding='5'>";
  tagwrite = tagwrite + "<caption align='top'><span class='BIG'>CD-ROM SHIPPING CHARGES</span></caption>";
  tagwrite = tagwrite + "<tr class='TBHEAD'>";
  tagwrite = tagwrite + "<td>Number of CDs Ordered</td>";
  tagwrite = tagwrite + "<td>Price ( CAD )</td>";
  tagwrite = tagwrite + "<td>Price ( USD )</td>";
  tagwrite = tagwrite + "</tr>";
  
  // Shipping Details for each category
  for (catid = 0; catid < count; catid ++)
  {
     quantity = parray[catid][0];   // quantity
     desc = parray[catid][1];  // description
     currency = "";  // no currency mentioned
     pricecad = MoneyFormat(ShipCalc(quantity, "CAD"), currency);
     priceusd = MoneyFormat(ShipCalc(quantity, "USD"), currency);

     // Write category tag    
     tagwrite = tagwrite + "<tr><td class='TBLFT'>" + desc + "</td>";
     tagwrite = tagwrite + "<td align='right'>" + pricecad + "</td>";
     tagwrite = tagwrite + "<td align='right'>" + priceusd + "</td></tr>";
  }
  tagwrite = tagwrite + "</table>";

  // Write tag
  document.write(tagwrite);
}

// Get Price per copy
function PerCopyGet(currency)
{
   // Per Copy value (CAD)
   var percopy = 22.00;     

   // Convert to USD if necessary. 
   if (currency == "USD") 
      percopy = ExchangeCalc(percopy);

  // Return percopy value
  return percopy;
}


/***********************/
/***** PAYMENT PAGE ****/
/***********************/

// Create Payment Page
function PaymentPageCreate()
{
  var tagwrite = "";
  var quantity = document.BUY.QUANTITY.value;	// number or copies to buy
  var currency = document.BUY.CURRENCY.value;  // currency to display (US or CAD)
  var price = document.BUY.PERCOPY.value;
  var subtotal = MoneyFormat(cbgmultcost, currency);
  var shipping = document.BUY.SHIPCOST.value;
  var total = document.BUY.TOTCOST.value;
  var prodname = document.BUY.PRODNAME.value;
  var pmethod = document.BUY.PAYMETHOD.value;
  var platform = document.BUY.PLATFORM.value;

  
  // PAGE START
  tagwrite = tagwrite + "<html>";
  tagwrite = tagwrite + "<head>";
  tagwrite = tagwrite + "<link href='style.css' rel='stylesheet' type='text/css'>";
  tagwrite = tagwrite + "<script>function PrintForm() {print();return(true);}</script>";
  tagwrite = tagwrite + "</head>";
  tagwrite = tagwrite + "<body>";


  // LOGO
  tagwrite = tagwrite + "<table width='100%' border='0' bordercolor='black' cellpadding='0' cellspacing='0'>";
 // tagwrite = tagwrite + LogoContents();
  tagwrite = tagwrite + "</table>";
  tagwrite = tagwrite + "<br />";

  // PURCHASE SUMMARY
  tagwrite = tagwrite + "<span class='BIG'>PURCHASE SUMMARY:</span><br /><br />";
  tagwrite = tagwrite + "<table cellpadding='10' cellspacing ='0' border='1' bordercolor='black'>";
  tagwrite = tagwrite + "<tr class='TBHEAD'>";
  tagwrite = tagwrite + "<td>Product Name</td>";
  tagwrite = tagwrite + "<td>Platform</td>";
  tagwrite = tagwrite + "<td>Quantity</td>";
  tagwrite = tagwrite + "<td>Price per CD</td>";
  tagwrite = tagwrite + "<td>Subtotal</td>";
  tagwrite = tagwrite + "<td>Shipping</td>";
  tagwrite = tagwrite + "<td>Total Price</td>";
  tagwrite = tagwrite + "</tr>";
  tagwrite = tagwrite + "<tr class='MEDIUM'>";
  tagwrite = tagwrite + "<td class='PURCHASE'>" + prodname + "</td>";
  tagwrite = tagwrite + "<td>" + platform + "</td>";
  tagwrite = tagwrite + "<td>" + quantity + "</td>";
  tagwrite = tagwrite + "<td>" + price + "</td>";
  tagwrite = tagwrite + "<td>" + subtotal + "</td>";
  tagwrite = tagwrite + "<td>" + shipping + "</td>";
  tagwrite = tagwrite + "<td class='PURCHASE'>" + total + "</td>";
  tagwrite = tagwrite + "</tr>";
  tagwrite = tagwrite + "</table>";

  // Payment Method
  switch (pmethod)
  {
    case "CRED":
      tagwrite = tagwrite + PayPalButton(currency, cbgtotcost, prodname); break;

    case "CHK":
      tagwrite = tagwrite + ShipInfoForm("CHEQUE"); break;

    case "MO":
      tagwrite = tagwrite + ShipInfoForm("MONEY ORDER"); break;

  }

  // PAGE END
  tagwrite = tagwrite + "</body></html>";

  // Write image tag
  document.write(tagwrite);
}

// PayPal Button
function PayPalButton(currency, total, prodname)
{
  var tagwrite = "";

  // Instructions
  tagwrite = tagwrite + "<p align='left' class='FRMINS'>";
  tagwrite = tagwrite + "You have chosen to pay by <span class='REDBOLD'>CREDIT CARD.</span><br /><br />";
  tagwrite = tagwrite + "Click the <span class='REDBOLD'>BUY NOW</span> button below to continue.<br />(You will be directed to PayPal's <span class='REDBOLD'>SECURE</span> Payment Page)";
  tagwrite = tagwrite + "</p>";

  // PAYPAL BUYNOW BUTTON
  tagwrite = tagwrite + "<form action='https://www.paypal.com/cgi-bin/webscr' method='post'>";
  tagwrite = tagwrite + "<input type='hidden' name='cmd' value='_xclick'>";
  tagwrite = tagwrite + "<input type='hidden' name='business' value='naharon@candybyte.com'>";
  tagwrite = tagwrite + "<input type='hidden' name='item_name' value='" + prodname + "'>";
  tagwrite = tagwrite + "<input type='hidden' name='amount' value='$" + total + "'>";
  tagwrite = tagwrite + "<input type='hidden' name='return' value='http://www.candybyte.com/buyyes.html'>";
  tagwrite = tagwrite + "<input type='hidden' name='cancel_return' value='http://www.candybyte.com/buycancel.html'>";
  tagwrite = tagwrite + "<input type='hidden' name='no_note' value='1'>";
  tagwrite = tagwrite + "<input type='hidden' name='currency_code' value='" + currency + "'>";
  tagwrite = tagwrite + "<input type='image' src='https://www.paypal.com/en_US/i/btn/x-click-butcc.gif' border='0' name='submit' alt='Make payments with PayPal - it's fast, free and secure!'>";
  tagwrite = tagwrite + "</form>";

  // Return tag
  return tagwrite;
}

// Shipping Info Form
function ShipInfoForm(pmethod)
{
  var tagwrite = "";

  // Instructions
  tagwrite = tagwrite + "<p align='left' class='FRMINS'>";
  tagwrite = tagwrite + "You have chosen to pay by <span class='REDBOLD'>" + pmethod + ".</span><br /><br />";
  tagwrite = tagwrite + "1) Please select <span class='REDBOLD'>File/Print</span> from the Browser Menu to print out this page.<br /><br />";
  tagwrite = tagwrite + "2) Fill out the printed SHIPPING INFO form and <span class='REDBOLD'>MAIL</span> the whole page along with your <span class='REDBOLD'>" + pmethod + "</span> to the following mailing address:<br /><br />";
  tagwrite = tagwrite + "<span class='REDBOLD'>";
  tagwrite = tagwrite + "CANDY BYTE SOFTWARE<br />";
  tagwrite = tagwrite + "141 - 6200 McKay Avenue, Box 406<br />";
  tagwrite = tagwrite + "Burnaby, B.C. CANADA, V5H 4M9<br />";
  tagwrite = tagwrite + "</span>";
  tagwrite = tagwrite + "</p>";

  // START FORM
  tagwrite = tagwrite + "<form name='SHIP'>";

  // HIDDEN FIELDS

  // Recipient - naharon@candybyte.com
  tagwrite = tagwrite + "<input type='hidden' name='recipient' value='naharon@candybyte.com'>";

  // Subject - Purchase Order
  tagwrite = tagwrite + "<input type='hidden' name='subject' value='Purchase Order'>";

  // Title - title appearing on the web page informing the user that the message was sent
  tagwrite = tagwrite + "<input type='hidden' name='title' value='Purchase Order Completed'>";

  // Return URL Title - name to use for the Return URL 
  tagwrite = tagwrite + "<input type='hidden' name='return_link_title' value='RETURN TO CANDYBYTE WEB PAGE'>";

  // Redirect - URL to use for the post-registration page 
  tagwrite = tagwrite + "<input type='hidden' name='redirect' value='http://www.candybyte.com/orderdone.html'>";

  // Email address appearing in From:
  tagwrite = tagwrite + "<input type='hidden' name='email' value='naharon@candybyte.com'>";

  // Name appearing in From:
  tagwrite = tagwrite + "<input type='hidden' name='name' value='Candy Byte Form'>";

  // END HIDDEN FIELDS

  // FORM ELEMENTS 
  tagwrite = tagwrite + "<div class='FORM'>";
  tagwrite = tagwrite + "<table cellpadding='2' cellspacing='0'>";

  // Section Title: SHIPPING INFORMATION FORM
  tagwrite = tagwrite + "<tr><td colspan='2' class='SECLBL'>SHIPPING INFORMATION FORM:<br /><br /></td></tr>";
 
  // First Name
  tagwrite = tagwrite + "<tr>";
  tagwrite = tagwrite + "<td align='left' class='FLDLBL'>First Name:</td>"; 
  tagwrite = tagwrite + "<td><input name='FIRSTNAME' value ='' size='30' maxlength='30'></td>";
  tagwrite = tagwrite + "</tr>";
    
  // Last Name
  tagwrite = tagwrite + "<tr>";
  tagwrite = tagwrite + "<td align='left' class='FLDLBL'>Last Name:</td>"; 
  tagwrite = tagwrite + "<td><input name='LASTNAME' value='' size='30' maxlength='30'></td>";
  tagwrite = tagwrite + "</tr>";
     
  // Street
  tagwrite = tagwrite + "<tr>";
  tagwrite = tagwrite + "<td align='left' class='FLDLBL'>Street Address:</td>";
  tagwrite = tagwrite + "<td><input name='STREET' value ='' size='30' maxlength='30'></td>";
  tagwrite = tagwrite + "</tr>";

  // City
  tagwrite = tagwrite + "<tr>";
  tagwrite = tagwrite + "<td align='left' class='FLDLBL'>City:</td>";
  tagwrite = tagwrite + "<td><input name='CITY' value='' size='30' maxlength='30'></td>";
  tagwrite = tagwrite + "</tr>";

  // Province/State
  tagwrite = tagwrite + "<tr>";
  tagwrite = tagwrite + "<td align='left' class='FLDLBL'>Province/State:</td>"; 
  tagwrite = tagwrite + "<td><input name='PROV' value='' size='4' maxlength='4'></td>";
  tagwrite = tagwrite + "</tr>";

  // Postal Code
  tagwrite = tagwrite + "<tr>";
  tagwrite = tagwrite + "<td align='left' class='FLDLBL'>Postal Code:</td>";
  tagwrite = tagwrite + "<td><input name='ZIPCODE' value='' size='10' maxlength='10'></td>";
  tagwrite = tagwrite + "</tr>";

  // Country
  tagwrite = tagwrite + "<tr>";
  tagwrite = tagwrite + "<td align='left' class='FLDLBL'>Country:</td>";
  tagwrite = tagwrite + "<td><input name='COUNTRY' value='' size='30' maxlength='30'></td>";
  tagwrite = tagwrite + "</tr>";

  // Email address
  tagwrite = tagwrite + "<tr>";
  tagwrite = tagwrite + "<td align='left' class='FLDLBL'>Email Address:</td>";
  tagwrite = tagwrite + "<td><input name='EMAILADDR' value='' size='60' maxlength='60'></td>";
  tagwrite = tagwrite + "</tr>";

  // Continue Button
  tagwrite = tagwrite + "<tr><td>";
  tagwrite = tagwrite + "<br />"; 
  tagwrite = tagwrite + "<a href='orderdone.html'><img src='img/btncont.gif'></a>";
  tagwrite = tagwrite + "<br />"; 
  tagwrite = tagwrite + "</td></tr>";

  // END FORM
  tagwrite = tagwrite + "</table>";
  tagwrite = tagwrite + "</div>";
  tagwrite = tagwrite + "</form>";

  // Return tag
  return tagwrite;
}

/*****************************/
/********* BUYING NOTES ******/
/*****************************/

// Buying Notes
function BuyNotes(type)
{
  var tagwrite="";

  tagwrite = tagwrite + "<span class='MEDIUM'>NOTES:</span>"; 
  tagwrite = tagwrite + "<ul class='PRIVACY'>";
  tagwrite = tagwrite + "<li><a href = 'privacy.html'>Privacy Policy</a>";
 
  if (type != "LibEd")
     tagwrite = tagwrite + "<li>Products are only shipped after payment approval and verification.";
  
  tagwrite = tagwrite + "<li>Delivery is by Canada Post Regular Mail and takes 3 to 5 business days within North America."; 
  tagwrite = tagwrite + "<li>All prices already include taxes.<br />";
  tagwrite = tagwrite + "<li>Prices are subject to change at any time.";
  tagwrite = tagwrite + "<li>No Refunds, Returns or Exchanges."; 
  tagwrite = tagwrite + "</ul>";

  // Return tag
  return tagwrite;
}

