// 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=paypal%40belleregalo.com";
var blk3  = "&item_name=";
var blk3n = "Error";
var opt0a = "&on0=WholeSale+Info";
var opt0b = "&os0=";
var opt0v = "";
var optsn = "&shipping_extra=";
var optsv = "";
var blk4  = "&amount=";
var blk4a = "6.66";
var blk5  = "&item_number=";
var blk5n = "";
var blk6  = "&image_url=http%3a//" + 
            "belleregalo.com/images/ppbrlogo.jpg"
var winname = "cartwin";
var winpar = "width=600,height=400,scrollbars," +
             "location," +        // testing only, comment out for Production
             "resizable,status";
var mycart = new Object();

// Make the call to PayPal Shopping Cart
function CallPay () { 
   var cartstr = blk1 + blk1a + blk2 + blk3 + blk3n;

   if (opt0v.length > 0) {                 // check for wholesale options
      cartstr = cartstr + opt0a + opt0b + opt0v;
   }
   if (optsv.length > 0) {                 // check for extra shipping cost
      cartstr = cartstr + optsn + optsv;
   }
   cartstr = cartstr + blk4 + blk4a + blk5 + blk5n;   // + blk6;

   if (!mycart.closed && mycart.location) {
      // alert("Found a PayPal Cart Window Open" + mycart.location);
      mycart.location = cartstr;                         // update existing cart window
   } else {
      mycart = window.open (cartstr, winname, winpar);   // open a new cart window
   }
}

// Build shopping cart code with given inputs
function add_to_cart(iName, iNum, iAmount) {
    blk3n = escape (iName);       // normalize for web posting
    blk4a = iAmount;
    blk5n = escape (iNum);
    // alert("Name: " + iName + "\nNumber: " + iNum + "\nAmount: " + iAmount );
    CallPay();
}

// Show the current Shopping Cart Contents
function view_cart() {
    window.open (blk1 + blk1d + blk2, winname, winpar);
}

// Validate Form Data and then Add Entry to Shopping Cart
function CheckValid(form) {
   var def_msg = "Missing Required Fields or Invalid Data:\n";
   var alert_msg = def_msg;

   if (form.biz_name.value.length < 2) {
      alert_msg = alert_msg + "Business Name\n";
   }
   if (form.biz_usage.options[0].selected) {
      alert_msg = alert_msg + "Type of Sales\n";
   }
   if (form.biz_phone.value.length < 10) {
      alert_msg = alert_msg + "Phone Number\n";
   }
   if (form.biz_taxid.value.length < 9) {
      alert_msg = alert_msg + "Tax ID / Resale #\n";
   }

   if (def_msg != alert_msg) {     // Data Input Problem, dont continue
      alert(alert_msg);
      form.biz_name.focus();
      return false;
   }

   opt0v = "Business=" + form.biz_name.value;
   opt0v = opt0v + ",Sales=" +
      form.biz_usage.options[form.biz_usage.selectedIndex].text;
   opt0v = opt0v + ",Phone=" + form.biz_phone.value;
   opt0v = opt0v + ",TaxID=" + form.biz_taxid.value;
   opt0v = escape (opt0v);         // prep for web posting

   if (form.shipping.value.length > 0) {
      optsv = form.shipping.value;
   }

   add_to_cart(form.iName.value, form.iNum.value, form.iAmount.value);

   opt0v = "";        // reset so these wont show up on other cart items
   optsv = "";
   return true;
}

// popUpImg - Display an Image with its price in a popup window; reuse same window for each item.
function popUpImg(filename, imageWidth, sellPrice) {

// TODO More work on this routine: ADD2CART, More input parameters, better Width's, etc.

    var newWin = window.open("", "myPic",
             "height=200,width=150,scrollbars=0,resizable=1,menubar=0,statusbar=0,locationbar=0,left=0,top=0");

    var d = newWin.document;

        d.write('<html><head><title>View Image</title></head><body><center>');
        d.write('<form method="get" action="noop" enctype="application/x-www-form-urlencoded" name="dispitem">');
        d.write('<table border="1" cellpadding="0" cellspacing="0" bordercolor="#800080"><tr>');
        d.write('<td align="center" valign="top" style="font-size: small; line-height: 1">');
        d.write('<img border="0" src="' + filename + '" width="' + imageWidth + '">');
        d.write('<br>');
        d.write(sellPrice);
        d.write('</td></tr></table>');
//        d.write('<br><input type="button" name="submit" value="Close" onclick="window.close();" />');
        d.write('<br><a href="xx" onclick="window.close();">Close</a>');
        d.write('</form>');
        d.write('</center></body></html>');
        d.close();
}

// D E A D   C O D E   H E R E
// Build and call shopping cart code with given input
function Xadd_to_cart(iName, iNum, iAmount) {
    iName = iName.replace(/\s/g,"+");   // replace blanks with "+" for Netscape(?)

    // alert("Name: " + iName + "\nNumber: " + iNum + "\nAmount: " + iAmount );
    window.open('https://www.paypal.com/cart/add=1&' + 
    'business=service%40belleregalo.com&amp;item_name=' +
    iName +
    '&amount=' +
    iAmount + 
    '&item_number=' +
    iNum + 
    '&amp;cn=Special+Instructions+(optional)' +
//    '&amp;image_url=http%3A//belleregalo.com/images/ppbrlogo.jpg' +         
    '&amp;return=http%3A//belleregalo.com/pp_success.htm' + 
    '&amp;cancel_return=http%3A//belleregalo.com/pp_cancel.htm',
    'cartwin','width=600,height=800,scrollbars,location,resizable,status');
}

