How to compare values in JavaScript for minimum order quantity
<p>Ok, basically I have been given the task of producing a payment page for us.
I'm having a little problem. My boss wants me to integrate google checkout, and I have that working fine.
He also wants me to define an order minimum.</p>

<p>For example:  we sell labels at "£11.00" per "1000", but the minimum invoice is "£33.00"</p>

<p>I have setup my page with 4 boxes. One is qty (Quantity), another is cost (Price) and two hidden fields.
One of these fields is for google checkout (the product prices)
the final field is a hidden field I put there, to try to compare the prices and update the cost.</p>

<p>Currently, I have on my quantity change textbox onChange event, this:</p>

<pre><code>function labels_1 () {
    var cost;
    var costper1k = '11.00';
    var qty = orderform.labels_1_qty.value;
    if ( isNaN( qty ) ) {
        orderform.labels_1_cost.value = "Not a Valid Quantity";
    }
    else if ( qty &lt; 0 ) {
        orderform.labels_1_cost.value = "Negative Quantity";
    }
    else {
         cost = ((qty/1000)*costper1k);
     }

    orderform.labels_1_cost.value = formatCurrency(cost);
    orderform.hiddenprice_1.value = cost;
    orderform.prodprice_1.value = orderform.labels_1_cost.value;
    orderform.productname_1.value='26x44mm (4a) 76mm Cores'+' x '+orderform.labels_1_qty.value;
 }
</code></pre>

<p>This is working well, without the validation of minimum invoice amount.</p>

<p>I have this 6 functions, all like this because we sell 6 label types. As I said, google checkout works fine with all these.</p>

<p>I tried creating this:</p>

<pre><code>    function hidden_1 () {
    var cost = '33.00';
    var originalcost = orderform.labels_1_cost.value;
    var a = Number(orderform.hidden_1.value);
    var b = Number(orderform.hidden_2.value);
    var c = Number(orderform.hidden_3.value);
    var d = Number(orderform.hidden_4.value);
    var e = Number(orderform.hidden_5.value);
    var f = Number(orderform.hidden_6.value);
    var total = a+b+c+d+e+f;
    if (total.value &lt; '33'){
    orderform.labels_1_cost.value = formatCurrency(cost);}
    if (total.value &gt; '33') {
    orderform.labels_1_cost.value = originalcost;}

}
</code></pre>

<p>and appending hidden_1(); to the end of the label_1() function.</p>

<p>It doesn't work, And I am tearing my hair out trying to fix it. I'm not even sure I am doing that right.</p>

<p>Can Anyone offer any help?
many thanks</p>

<p><strong>edit</strong></p>

<p>Sorry, here is my formatCurrency function:</p>

<pre><code>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&lt;10)
cents = "0" + cents;
for (var i = 0; i &lt; 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);
</code></pre>

<p>}</p>

<p>Full Site Code:
Order.php:</p>

<pre><code>&lt;html&gt;
&lt;head&gt;
&lt;style type="text/css" media="screen"&gt;@import "css/forms-style.css";&lt;/style&gt;
&lt;script type="text/javascript" src="js/labels.js"&gt;&lt;/script&gt;
&lt;/head&gt;

&lt;script id="googlecart-script" type="text/javascript"
  src="http://checkout.google.com/seller/gsc/v2/cart.js?mid=REMOVED!!!"
  currency="GBP"
  highlight-time="5000"
  highlight-color="#FF7878"
  cart-opening-time="500"
  hide-cart-when-empty="true"
  close-cart-when-click-away="false"
  post-cart-to-sandbox="true"&gt;
&lt;/script&gt;

&lt;body&gt;
&lt;div id="wrapper"&gt;

&lt;form name="orderform"&gt;
&lt;!-- ----------------------------------------------------------------------- --&gt;
&lt;div id="content"&gt;
&lt;div class="heading-section"&gt;
&lt;h2&gt;Printed Labels&lt;/h2&gt;
&lt;p&gt;All Printed Labels come outside wound, with a standard &lt;strong&gt;10 item setups per roll.&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;The minimum Invoice amount is &lt;strong&gt;&amp;pound;30.00&lt;/strong&gt;&lt;/p&gt;
&lt;/div&gt;
&lt;div align="center" id="statement"&gt;Hover Over Images To Zoom&lt;/div&gt;
&lt;br/&gt;
&lt;p&gt;
  &lt;?php tableformat( '1', '26x44mm (4a) 76mm Cores','26x44mm Labels. Custom options are available for these labels. They can include the text SPECIAL BUY or SPECIAL PURCHASE overprinted in &lt;font color=red&gt;&lt;strong&gt;RED&lt;/strong&gt;&lt;/font&gt; ink. These labels can also include a Country of Origin and the John lewis Address. The labels are also available in blockout (opaque) backing, to be used for repricing, or generally covering other labels.','Style: &lt;select class="product-attr-color"&gt;
          &lt;option&gt;Normal&lt;/option&gt;
          &lt;option&gt;Special Buy&lt;/option&gt;
      &lt;option&gt;Special Purchase&lt;/option&gt;
      &lt;option&gt;Opaque Backing Material&lt;/option&gt;
        &lt;/select&gt;','img/img_1.png','img/img_7.png','img/img_8.png','img/img_1_thumb.png','img/img_7_thumb.png','img/img_8_thumb.png' ); ?&gt;
  &lt;?php tableformat( '2', '44x26mm (2a) 76mm Cores','44x26mm Labels. Custom options are available for these labels. They can include the text SPECIAL BUY or SPECIAL PURCHASE overprinted in &lt;font color=red&gt;&lt;strong&gt;RED&lt;/strong&gt;&lt;/font&gt; ink. These labels can also include a Country of Origin and the John lewis Address. The labels are also available in blockout (opaque) backing, to be used for repricing, or generally covering other labels.','Style: &lt;select class="product-attr-color"&gt;
          &lt;option&gt;Normal&lt;/option&gt;
          &lt;option&gt;Special Buy&lt;/option&gt;
      &lt;option&gt;Special Purchase&lt;/option&gt;
      &lt;option&gt;Opaque Backing Material&lt;/option&gt;
        &lt;/select&gt;','img/img_2.png','','','img/img_2_thumb.png','','' ); ?&gt;
  &lt;?php tableformat( '3', '35x13mm (2a) 76mm Cores','35x13mm Labels. These Labels can include a Country of Origin. Please Note, the size of the label is fairly limited','','img/img_3.png','','','img/img_3_thumb.png','','' ); ?&gt;
  &lt;?php tableformat( '4', '80x10mm [dumbells] (1a) 76mm Cores','80x10mm Dumbell Labels. These Labels can be produced with either an EAN8 or an EAN13 consumer code','','img/img_4.png','','','img/img_4_thumb.png','','' ); ?&gt;
  &lt;?php tableformat( '5', '25x10mm [price only] (4a) 76mm Cores','25x10mm Labels. These Small labels may include a Price Only. No text will be printed on these labels. If you require Text, please look at some of the other sizes above','','img/img_5.png','','','img/img_5_thumb.png','','' ); ?&gt;
  &lt;?php tableformat( '6', '15x8mm [price only] (4a) 76mm Cores','15x8mm Labels. These Small labels may include a Price Only. No text will be printed on these labels. If you require Text, please look at some of the other sizes above','','img/img_6.png','','','img/img_6_thumb.png','','' ); ?&gt;
&lt;/p&gt;  
&lt;div class="heading-section" align="right"&gt;
&lt;input type="reset"&gt;

&lt;!-- ----------------------------------------------------------------------- --&gt;
&lt;/hr&gt;
&lt;/h2&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/form&gt;

&lt;br/&gt;&lt;br/&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/body&gt;
&lt;/html&gt;

&lt;?php

function tableformat( $num, $label, $labeldesc, $options, $pic1, $pic2, $pic3, $thumb1, $thumb2, $thumb3) {

echo
'&lt;div id="'.$num.'" class="section"&gt;
&lt;div class="product"&gt;
&lt;table border=0 width=100% align=center class="product-table"&gt;
&lt;tr valign=top bgcolor=3366cc&gt;
        &lt;td colspan=4&gt;&lt;span class="product-name"&gt;'.$label.'&lt;/span&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;a class="p1" href="#v"&gt;&lt;img onerror="this.style.display=\'none\'" src="'.$thumb1.'" alt=""&gt;&lt;b&gt;&lt;img class="large" onerror="this.style.display=\'none\'" src="'.$pic1.'" alt=""&gt;&lt;/b&gt;&lt;/a&gt;&amp;nbsp;&amp;nbsp;&lt;a class="p1" onerror="this.style.display=\'none\'" ;href="#v"&gt;&lt;img onerror="this.style.display=\'none\'" src="'.$thumb2.'" alt=""&gt;&lt;b&gt;&lt;img onerror="this.style.display=\'none\'" class="large" src="'.$pic2.'" alt=""&gt;&lt;/b&gt;&lt;/a&gt;&amp;nbsp;&amp;nbsp;&lt;a class="p1" href="#v"&gt;&lt;img onerror="this.style.display=\'none\'" src="'.$thumb3.'" alt=""&gt;&lt;b&gt;&lt;img class="large" onerror="this.style.display=\'none\'" src="'.$pic3.'" alt=""&gt;&lt;/b&gt;&lt;/a&gt;&lt;/td&gt;
        &lt;input type="hidden" name="productname_'.$num.'" class="product-title" value="0"/&gt;
    &lt;/tr&gt;
    &lt;tr&gt;&lt;td&gt;&amp;nbsp;&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
    &lt;td align="left" colspan="2"&gt;
        &lt;span id="labels_'.$num.'_exp"&gt;'.$labeldesc.'&lt;/span&gt;
    &lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;&lt;td&gt;&amp;nbsp;&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr valign=center align=right&gt;
    &lt;td width=30%&gt;
    &amp;nbsp;
    &lt;/td&gt;
    &lt;td width=auto&gt;
    '.$options.'
    &lt;strong&gt;Quantity:&lt;/strong&gt; &lt;input type="text" name="labels_'.$num.'_qty" size="10" value=0 onChange="OnChan();"&gt;
    &lt;strong&gt;Price:&lt;/strong&gt; &lt;input type="text" name="labels_'.$num.'_cost" class="form-cost" size="10" value="0.00" onChange="OnChan();"&gt;
    &lt;input type="hidden" name="prodprice_'.$num.'" class="product-price" value="£0.00"/&gt;
    &lt;input type="hidden" name="hiddenprice_'.$num.'" value="0.00"/&gt;
    &lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
    &lt;td&gt;&amp;nbsp;&lt;/td&gt;
    &lt;/tr&gt;
    &lt;td width=30%&gt;
    &amp;nbsp;
    &lt;/td&gt;
    &lt;td width=auto align=right&gt;
    &lt;div role="button" alt="Add to cart" tabindex="0" class="googlecart-add-button" onClick="hidden_'.$num.';"&gt;
    &lt;/div&gt;
    &lt;/td&gt;
    &lt;/tr&gt;
    &lt;/table&gt;
&lt;br/&gt;
&lt;br/&gt;

&lt;/div&gt;&lt;/div&gt;';


} // tableformat

?&gt;
</code></pre>

<p>labels.js is here:</p>

<pre><code>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&lt;10)
    cents = "0" + cents;
    for (var i = 0; i &lt; 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 isPositiveInteger(val){
    if(val==null){return false;}
    if (val.length==0){return false;}
    for (var i = 0; i &lt; val.length; i++) {
        var ch = val.charAt(i)
        if (ch &lt; "0" || ch &gt; "9") {
        return false
        }
    }
    return true;
}

/* -----------------------------------------------------------------------------
    LABELS - 1
----------------------------------------------------------------------------- */

function labels_1 () {
    var cost, desc;
    var costper1k = '11.00';
    var qty = orderform.labels_1_qty.value;
    if ( isNaN( qty ) ) {
        orderform.labels_1_cost.value = "Not a Valid Quantity";
        desc = 'Not a Valid Quantity';
    }
    else if ( qty &lt; 0 ) {
        orderform.labels_1_cost.value = "Negative Quantity";
        desc = 'Not a Valid Quantity';
    }
    else {
         cost = ((qty/1000)*costper1k);
         desc = qty+' ordered at a cost of '+formatCurrency(cost);
         }

    orderform.labels_1_cost.value = formatCurrency(cost);
    alert("test - end of normal");
    hidden_1();
}


/* -----------------------------------------------------------------------------
    Hidden - 1
----------------------------------------------------------------------------- */
function hidden_1 () {
    alert("starting");
    var costed = '33.00';
    var originalcost = orderform.labels_1_cost.value;
    var a = Number(orderform.hidden_1.value);
    var b = Number(orderform.hidden_2.value);
    var c = Number(orderform.hidden_3.value);
    var d = Number(orderform.hidden_4.value);
    var e = Number(orderform.hidden_5.value);
    var f = Number(orderform.hidden_6.value);
    var total = a+b+c+d+e+f;
    if (total &lt; 33){
    orderform.labels_1_cost.value = costed;}
    if (total =&gt; 33) {
    orderform.labels_1_cost.value = originalcost;}
    alert("ending");
}


/* -----------------------------------------------------------------------------
    LABELS - 2
----------------------------------------------------------------------------- */




function labels_2 () {
    var cost, desc;
    var costper1k = '11.00';
    var qty = orderform.labels_2_qty.value;
    if ( isNaN( qty ) ) {
        orderform.labels_2_cost.value = "Not a Valid Quantity";
        desc = 'Not a Valid Quantity';
    }
    else if ( qty &lt; 0 ) {
        orderform.labels_2_cost.value = "Negative Quantity";
        desc = 'Not a Valid Quantity';
    }
    else {
         cost = ((qty/1000)*costper1k);
         desc = qty+' ordered at a cost of '+formatCurrency(cost);
         }
    orderform.labels_2_cost.value = formatCurrency(cost);
}

/* -----------------------------------------------------------------------------
    LABELS - 3
----------------------------------------------------------------------------- */

function labels_3 () {
    var cost, desc;
    var costper1k = '11.00';
    var qty = orderform.labels_3_qty.value;
    if ( isNaN( qty ) ) {
        orderform.labels_3_cost.value = "Not a Valid Quantity";
        desc = 'Not a Valid Quantity';
    }
    else if ( qty &lt; 0 ) {
        orderform.labels_3_cost.value = "Negative Quantity";
        desc = 'Not a Valid Quantity';
    }
    else{
         cost = ((qty/1000)*costper1k);
         desc = qty+' ordered at a cost of '+formatCurrency(cost);
        }
    orderform.labels_3_cost.value = formatCurrency(cost);
}

/* -----------------------------------------------------------------------------
    LABELS - 4
----------------------------------------------------------------------------- */

function labels_4 () {
    var cost, desc;
    var costper1k = '13.75';
    var qty = orderform.labels_4_qty.value;
    if ( isNaN( qty ) ) {
        orderform.labels_4_cost.value = "Not a Valid Quantity";
        desc = 'Not a Valid Quantity';
    }
    else if ( qty &lt; 0 ) {
        orderform.labels_4_cost.value = "Negative Quantity";
        desc = 'Not a Valid Quantity';
    }
    else {
         cost = ((qty/1000)*costper1k);
         desc = qty+' ordered at a cost of '+formatCurrency(cost);
        }
    orderform.labels_4_cost.value = formatCurrency(cost);
}

/* -----------------------------------------------------------------------------
    LABELS - 5
----------------------------------------------------------------------------- */

function labels_5 () {
    var cost, desc;
    var costper1k = '7.89';
    var qty = orderform.labels_5_qty.value;
    if ( isNaN( qty ) ) {
        orderform.labels_5_cost.value = "Not a Valid Quantity";
        desc = 'Not a Valid Quantity';
    }
    else if ( qty &lt; 0 ) {
        orderform.labels_5_cost.value = "Negative Quantity";
        desc = 'Not a Valid Quantity';
    }
    else {
         cost = ((qty/1000)*costper1k);
         desc = qty+' ordered at a cost of '+formatCurrency(cost);
        }
    orderform.labels_5_cost.value = formatCurrency(cost);
}


/* -----------------------------------------------------------------------------
    LABELS - 6
----------------------------------------------------------------------------- */
function labels_6 () {
    var cost, desc;
    var costper1k = '10.14';
    var qty = orderform.labels_6_qty.value;
    if ( isNaN( qty ) ) {
        orderform.labels_6_cost.value = "Not a Valid Quantity";
        desc = 'Not a Valid Quantity';
    }
    else if ( qty &lt; 0 ) {
        orderform.labels_6_cost.value = "Negative Quantity";
        desc = 'Not a Valid Quantity';
    }
    else {
         cost = ((qty/1000)*costper1k);
         desc = qty+' ordered at a cost of '+formatCurrency(cost);
        }
    orderform.labels_6_cost.value = formatCurrency(cost);
}

/* -----------------------------------------------------------------------------
    OnChan (Changes)
----------------------------------------------------------------------------- */

function OnChan () {
labels_1();
labels_2();
labels_3();
labels_4();
labels_5();
labels_6();
orderform.prodprice_1.value=orderform.labels_1_cost.value;
orderform.prodprice_2.value=orderform.labels_2_cost.value;
orderform.prodprice_3.value=orderform.labels_3_cost.value;
orderform.prodprice_4.value=orderform.labels_4_cost.value;
orderform.prodprice_5.value=orderform.labels_5_cost.value;
orderform.prodprice_6.value=orderform.labels_6_cost.value;
orderform.productname_1.value='26x44mm (4a) 76mm Cores'+' x '+orderform.labels_1_qty.value;
orderform.productname_2.value='44x26mm (2a) 76mm Cores'+' x '+orderform.labels_2_qty.value;
orderform.productname_3.value='35x13mm (2a) 76mm Cores'+' x '+orderform.labels_3_qty.value;
orderform.productname_4.value='80x10mm [dumbells] (1a) 76mm Cores'+' x '+orderform.labels_4_qty.value;
orderform.productname_5.value='25x10mm [price only] (4a) 76mm Cores'+' x '+orderform.labels_5_qty.value;
orderform.productname_6.value='15x8mm [price only] (4a) 76mm Cores'+' x '+orderform.labels_6_qty.value;

}
</code></pre>

<p>and a test website is up at:</p>

<p><a href="http://mrotacon.heliohost.org/testing/order.php" rel="nofollow">http://mrotacon.heliohost.org/testing/order.php</a></p>

<p>also - here is a test version without the comparison, which works fine... but i need the minimum invoice amount</p>

<p><a href="http://mrotacon.heliohost.org/testing/orderworking.php" rel="nofollow">http://mrotacon.heliohost.org/testing/orderworking.php</a></p>
