/***********************************************
* fcrc20.com
* merchandise functions
***********************************************/

var storeitems = 11;             // items in store

function popwin(srcimg, xsize, ysize) {
  var xwin = xsize + 32;
  var ywin = ysize + 64;
  var generator=window.open('','name','width='+xwin+',height='+ywin+',resizable=0,location=0,menubar=0,scrollbars=0,toolbar=0');
  
  generator.document.write('<html><head><title>ImageViewer</title>\n');
  generator.document.write('<style type="text/css">');
  generator.document.write('  body {text-align: center; background-color: white; font-family: verdana, sans-serif, helvetica; font-size: 10pt; }');
  generator.document.write('</style>');
  generator.document.write('</head><body>\n');
  generator.document.write('<p><img src=\''+srcimg+'\' width='+xsize+' height='+ysize+' border=\'0\'></p>\n');
  generator.document.write('<p><a href="javascript:self.close()">Close</a></p>\n');
  generator.document.write('</body></html>');
  generator.document.close();
}


//configure status message to show
var statusmsg=""

function hidestatus() {
  window.status=statusmsg
return true
}


function CartAdd(ibtn) {
  if ( document.fcrc20cart["itemqty" + ZeroPad(ibtn)].value == "") {
    alert("Quantity missing.");
    return false;
  }
  var itemprice,qty;
  itemprice = document.fcrc20cart["itemprice" + ZeroPad(ibtn)].value;
  qty = document.fcrc20cart["itemqty" + ZeroPad(ibtn)].value;
  document.fcrc20cart["itemsubtotal" + ZeroPad(ibtn)].value = formatCurrency(itemprice*qty);
  CalcTotal()
}


function CartRmv(ibtn) {
  document.fcrc20cart["itemqty" + ZeroPad(ibtn)].value="";
  document.fcrc20cart["itemsubtotal" + ZeroPad(ibtn)].value="";
  CalcTotal()
}


function CalcTotal() {
  var ttl,item;
  ttl = 0;
  for (var i=1; i<=storeitems; i++)
  {
  ttl = ttl + document.fcrc20cart["itemsubtotal" + ZeroPad(i)].value.replace(/[^\d\.-]/g,'')/1;
  }
  document.fcrc20cart.carttotal.value = formatCurrency(ttl);
}


function SendCart() {  // send the cart to PayPal
  var i,j,desc;

  var spost = "https://www.paypal.com/cgi-bin/webscr?cmd=_cart" +
              "&upload=1" + 
              "&business=diamonddot55" + "@" + "att.net" +
              "&currency_code=USD" +
              "&lc=US" +
              "&image_url=http://www.fcrc-mi20.com/images/paypal_logo.png" +
              "&cpp_header_image=http://www.fcrc-mi20.com/images/paypal_cart_banner.png" +
              "&return=http://www.fcrc-mi20.com";

  //how to add an optional Shipping & Handling fee of $2.50
  //spost = spost + "&handling_cart=2.50";

  j = 0;

  for (i=1; i<=storeitems; i++) {
    if (document.fcrc20cart["itemqty" + ZeroPad(i)].value > 0) {
      j = j + 1;

      desc = document.fcrc20cart["itemdesc" + ZeroPad(i)].value;

      if (document.fcrc20cart["itemopt" + ZeroPad(i)]){

        switch (document.fcrc20cart["itemopt" + ZeroPad(i)].value){
        case 'pickone':
          alert('Please select an option for:\n\n' + desc);
          return
          break;
        case '':
          alert('Please select a value for:\n\n' + desc);
          return
          break;
        }

        switch (ZeroPad(i)){
        case '09':
          desc = desc + ', CHAPTER ' + document.fcrc20cart["itemopt" + ZeroPad(i)].value;
          break;
        default:
          desc = desc + ', ' + document.fcrc20cart["itemopt" + ZeroPad(i)].value;
        }

      }
      
      spost = spost + "&item_name_"  + j + "=" + desc + 
                      "&quantity_"   + j + "=" + document.fcrc20cart["itemqty" + ZeroPad(i)].value + 
                      "&amount_"     + j + "=" + document.fcrc20cart["itemprice" + ZeroPad(i)].value
    }
  }

  if (j > 0) {
    //alert("width: "+screen.width+"     height: "+screen.height);
    //var lpos = (screen.width - 800)/2;
    //var tpos = (screen.height - 600)/2;
    //var winparm = "width=800,height=600,left="+lpos+",top="+tpos+",scrollbars,location,resizable,status";
    var winparm = "width=1012,height=675,scrollbars,location,resizable,status";
//    var winparm = "width=1024,height=768,scrollbars,location,resizable,status";

    
    window.open (spost, "paypal", winparm);
  }
  else {
    alert("There are no items selected.");
  }
}  

function formatCurrency(num) {
  num = num.toString().replace(/\$|\,/g,'');
  if(isNaN(num))
  num = "0";
  sign = (num == (num = Math.abs(num)));
  num = Math.floor(num*100+0.50000000001);
  cents = num%100;
  num = Math.floor(num/100).toString();
  if(cents<10)
  cents = "0" + cents;
  for (var i = 0; i < Math.floor((num.length-(1+i))/3); i++)
  num = num.substring(0,num.length-(4*i+3))+','+
  num.substring(num.length-(4*i+3));
  return (((sign)?'':'-') + '$' + num + '.' + cents);
}


function ZeroPad(n){
  return (n>9)?n:'0'+n;
}

