$j(function() { if (typeof prices == 'object') { $j('#quantity').change(function() { $j('div.product_box table.price_ranges tr').removeClass('current').filter('.qty_'+get_price_qty($j(this).val())).addClass('current'); // $j('div.product_box div.product_price').text(get_price_qty($j(this).val())); }); } }); function get_price(qty) { // expects global object prices to operate correctly. // prices format as following: {'qty1' : price1, 'qty2' : price 2} var k = 0; var v = 0; qty = qty*1; // "40"<"7" $j.each(prices, function(key, val) { key=key*1; if (key<=qty && key>=k) { v = val; k = key; } }); if (v == 0) { v = 'POA'; } return v; } function get_price_qty(qty) { // expects global object prices to operate correctly. // prices format as following: {'qty1' : price1, 'qty2' : price 2} var k = 0; var v = 0; qty = qty*1; // "40"<"7" $j.each(prices, function(key, val) { key=key*1; if (key<=qty && key>=k) { v = val; k = key; } }); return k; }