<!--
// 09/30/2007 by HJR, Copyright 2007 HJR SOFTWARE.  All Rights Reserved
// Useful PayPal Buy and AddToCart Validation Functions.

function hjrsBuySubmit(oBtn,iCmd) {
  if ( ! hjrsValQty(oBtn) )
    return (false);
  var sItemNo, oPPino = hGetObj("item_number"), oPPnam = hGetObj("item_name");
  var oPPamt = hGetObj("amount"), oPPqty = hGetObj("quantity");
  var oPPcmd = hGetObj("cmd"), oPPbn = hGetObj("bn");
  sItemNo = oBtn.id.substring(7);
  var oHnam = hGetObj("hjrsIN_" + sItemNo), oHqty = hGetObj("hjrsIQ_" + sItemNo), oHprc = hGetObj("hjrsIP_" + sItemNo);
  oPPino.value = sItemNo;
  oPPnam.value = oHnam.value;
  oPPamt.value = hjrsFormatMoney(( oHqty.value > 1 ) ? oHprc.value / oHqty.value : oHprc.value);
  if ( iCmd == 2 ) {
    // oPPino.form.action = "https://www.paypal.com/cart/add";
    oPPcmd.value = "_cart";
    oPPbn.value = "PP-ShopCartBF";
    var xyz = hGetObj("on0");
    xyz.name = "add";
    xyz.value = oPPqty.value;
    oPPqty.name = "on0";
    // oPPqty.name = "add";
    // oPPqty.value = oHqty.value;
  } else {
    // oPPino.form.action = "https://www.paypal.com/cgi-bin/webscr";
    oPPcmd.value = "_xclick";
    oPPbn.value = "PP-BuyNowBF";
    oPPqty.name = "quantity";
    oPPqty.value = oHqty.value;
  }
  oPPino.form.submit();
  return (true);
}

function hjrsValQty(oBtn) {
  var sItemNo, x, s, fees, d, m, q;
  sItemNo = oBtn.id.substring(7);
  var oHnam = hGetObj("hjrsIN_" + sItemNo), oHqty = hGetObj("hjrsIQ_" + sItemNo), oHamt = hGetObj("hjrsIA_" + sItemNo), oHprc = hGetObj("hjrsIP_" + sItemNo), oHwht = hGetObj("hjrsIW_" + sItemNo);
  x = parseInt(oHqty.value);
  if ( isNaN(x) || x < 1 || x > 25 ) {
    alert("Invalid Quantity (QTY) specified for this item.  Specify a whole number between 1 and 25!" );
    oHqty.value = 1;
    oHqty.focus();
    return (false);
  }
  oHqty.value = x;
  x = parseFloat(oHwht.value);
  if ( isNaN(x) ) {
    alert("There is a problem with the WEIGHT of this item, therefore, this item CANNOT be purchased at this time.  Please contact Customer Service or the Webmaster, HJR SOFTWARE, to have this problem corrected!" );
    x = 1.0;
    oHwht.value = x;
  }
// alert( "Weight of item: " + x );
  if ( oHamt.value.toString().charAt(0) == "$" ) {
    x = parseFloat(oHamt.value.substring(1));
  } else {
    x = parseFloat(oHamt.value);
  }
  if ( isNaN(x) ) {
    alert("There is a problem with the PRICE of this item, therefore, this item CANNOT be purchased at this time.  Please contact Customer Service or the Webmaster, HJR SOFTWARE, to have this problem corrected!" );
    return (false);
  }
  d = oHamt.value.indexOf(";d");
  if ( d > -1 ) {		// Discount exist -- Amount is already discounted though, so we don't need to do anything with this unless the price is modified too...
    d = parseFloat( oHamt.value.substring(d + 2) );
  } else {
    d = 0;    
  }
  m = oHamt.value.indexOf(";m");
  if ( m > -1 ) {	    // Price should be modified by m Amount
    m = parseFloat( oHamt.value.substring(m + 2) );
// alert( "Price is being modified from " + x + " to " + ( x + m ) );
    x += m;
    if ( d != 0 ) {		// Apply Discount to add-on price
// alert( "Price is being modified by discount of " + d + " to " + ( x - ( m * d ) ) );    
      x -= ( m * d);
    }
  }
  q = parseFloat(oHqty.value);
  x *= q;
// alert( "Price of the Items you specified: " + x );
  s = (parseFloat(oHqty.value) * ( parseFloat(oHwht.value) * 3.25 ));  // Amount to left is the Shipping Amount per lb.
// alert( "Shipping for the Number of Items you specified: " + s );
  fees = hjrsFormatMoney(ComputePayPalFees(x+s));
// alert( "Fees to accept total amount of: " + hjrsFormatMoney( x + s ) + " = " + fees );  
  fees = parseFloat(fees);
    
  m = parseFloat(hjrsFormatMoney( AmountWithPayPalFees(x + s) / q ));
  m = m * q;
  oHprc.value = hjrsFormatMoney(m);  // AmountWithPayPalFees(x+s));
  return (true);
}

function hjrsValSel(oBtn) {
  var sItemNo, x, s, fees, iSel = oBtn.selectedIndex, aOpts;
  sItemNo = oBtn.id.substring(7);
  var oHnam = hGetObj("hjrsIN_" + sItemNo), oHqty = hGetObj("hjrsIQ_" + sItemNo), oHamt = hGetObj("hjrsIA_" + sItemNo), oHprc = hGetObj("hjrsIP_" + sItemNo), oHwht = hGetObj("hjrsIW_" + sItemNo);
  var oPPon0 = hGetObj("on0"), oPPos0 = hGetObj("os0");
  
  if ( iSel < 0 ) {
    oBtn.selectedIndex = 0;
    oBtn.click();
//    alert( "Please select one of the Options from the list!" );
//    return (false);
  }
// alert( "The select option is: " + oBtn.selectedIndex + " - " + oBtn.options[iSel].text + " with values of: " + oBtn.options[iSel].value );
  aOpts = oBtn.options[iSel].value.split(",");
//  s = "";
//  for ( x = 0; x < aOpts.length; x ++ ) {
//    s += "Option: " + x + " = " + aOpts[x] + "\n";
//  }
//  alert( "selected info: \n" + s );
  x = parseFloat(aOpts[1]);
  if ( x == 0 ) {
//    alert( "Price Modifier was $0, making sure Internal Price is not modified!" );
//    alert( oHamt.value );
  	if ( oHamt.value.indexOf(";m") > -1 ) {
  	  oHamt.value = oHamt.value.substr( 0, oHamt.value.indexOf(";m") );
    }
//    alert( oHamt.value );
  } else {
//  	alert( "Adjusting Internal Price of Item by: " +  x );
//  	alert( oHamt.value + " | " + oHamt.value.length );
  	s = ( oHamt.value.indexOf(";m") > -1 ) ? oHamt.value.indexOf(";m") : oHamt.value.length;
  	oHamt.value = oHamt.value.substr(0, s) + ";m" + hjrsFormatMoney(x).toString();
//  	alert( oHamt.value );
  }
  x = aOpts[0].indexOf(": ");
  oPPon0.name = "on0";
  oPPon0.value = aOpts[0].substr(0,x);
  oPPos0.name = "os0";
  oPPos0.value = aOpts[0].substring(x+2);
//  alert( "Values for Options now equal: on0: [" + oPPon0.value + "], os0: [" + oPPos0.value + "]" );
  hjrsValQty(oBtn);
}
// -->
