
    var blnRunSearch = true;

    function ajaxQuickOrderProductSearch(strKeyword, strAutocompleteID)
    {
      if (strKeyword.length > 1)
      {
        var blnShowAutocomplete = false;

        iRowID = strAutocompleteID.replace(/product_reference_/, '');
        //alert(iRowID);
        $('#product_reference_' + iRowID + '_price').attr('innerHTML', '')

        $('#product_autocomplete').html(' ');
        $.ajaxSetup({ scriptCharset: "utf-8" , contentType: "application/json; charset=utf-8"});
        $.getJSON(
          '/ajax/lookup_child_products/' + strKeyword,
          function(data){
            //$('#product_autocomplete').append('<select name="product_option[]">');
            $.each(data.products, function(i,product){

              var strPrice = '';

              iRowID = strAutocompleteID.replace(/product_reference_/, '');

              if (product.price_breaks != undefined) {
                arrPriceBreaks = product.price_breaks.split('_');
                for (i = 0; i < arrPriceBreaks.length; i++) {
                  if (arrPriceBreaks[i] == 1) {
                    strOpenPriceBreakSpan = '<span id="new_price_break_' + i + '" name="' + arrPriceBreaks[i] + '">';
                  }
                  else {
                    strOpenPriceBreakSpan = '<span id="new_price_break_' + i + '" name="' + arrPriceBreaks[i] + '" style="display:none;">';
                  }
                  strPrice = strPrice + strOpenPriceBreakSpan + product.price_break[arrPriceBreaks[i]] + '</span>';
                }
              }
              else {
                strPrice = '<span class="prices_' + iRowID + '" id="new_price_break_' + product.product_id + '" style="display:none;">' + product.price_low + '</span>';
              }

              strPrice = URLDecode(unescape(strPrice));
              // ah hello funny "A-hat" characters... so, we meet again.... DIE! DIE! DIE!
              strPrice = strPrice.replace(/Â/g, "");

              //alert(URLDecode(unescape(product.price_low)));

              if (product.num_options == 1) {
                populateQuickOrderProduct(strAutocompleteID, product.title, product.product_id, product.reference, strPrice, product.attribute_summary, '');
              }
              else
              {
                 //$('#product_reference_' + iRowID + '_name').attr('innerHTML', '');
                 $('#product_reference_' + iRowID + '_name').attr('innerHTML', product.title);
                 //$('#product_reference_' + iRowID + '_price').attr('innerHTML', '');
                //alert(strPrice);
                 $('#product_reference_' + iRowID + '_price').attr('innerHTML', $('#product_reference_' + iRowID + '_price').attr('innerHTML') + strPrice );
                 //$('#' + strPrefixID + '_price').text(strProductPrice);


                 var strProduct = '<option value="' + product.product_id + '">';
                 if(!blnShowAutocomplete)
                 {
                   //strProduct = strProduct + '<strong>Please choose your option:</strong></li><li>';
                 }

                 //strProduct = strProduct + '<a href="#" onclick="populateQuickOrderProduct(\'' + (strAutocompleteID) + '\', \'' + product.title + '\', \'' + (product.product_id) + '\', \'' + (product.reference) + '\', \''+strPrice+'\', \'' + (product.attribute_summary) + '\', \'\'); return false;">';
                 //strProduct = strProduct + '<span class="product_title">' + product.title + '</span>';
                 //strProduct = strProduct + '<span class="product_attributes"> '+(product.attribute_summary)+'</span>';
                 //strProduct = strProduct + '<span class="product_reference"> (' + product.reference + ')</span>';
                 //strProduct = strProduct + '</a>';
                 if(product.attribute_summary != '')
                 {
                   strProduct = strProduct + product.attribute_summary;
                 }
                 else
                 {
                   strProduct = strProduct + product.title;
                 }
                 strProduct = strProduct + '</option>';
                 blnShowAutocomplete = true;
                 $('#product_autocomplete').append(strProduct);
              }

            });

            if (blnShowAutocomplete) {
              $('#product_autocomplete').attr('innerHTML', '<div><select name="product_option[]" id="product_option_' + iRowID + '" onchange="updateQuickOrderPrices(' + iRowID + ');"><option value="">-- Please Choose --</option>' + $('#product_autocomplete').attr('innerHTML') + '</select></div>');
              $('#new_price_break_' + $('#product_option_' + iRowID).val()).show();
              iRowID = strAutocompleteID.replace(/product_reference_/, '');

              $('#product_reference_' + iRowID + '_options').attr('innerHTML', $('#product_autocomplete').attr('innerHTML'));
              //$('#product_reference_' + iRowID + '_price').attr('innerHTML', unescape($('#product_reference_' + iRowID + '_price').attr('innerHTML')));

              //alert($('#product_autocomplete').attr('innerHTML'));

              addQuickOrderRow();
            }
            blnRunSearch = true;
          }
        )
      }
      else
      {
        blnRunSearch = true;
      }
    }

    function updateQuickOrderPrices(iRowID)
    {
      $('.prices_' + iRowID).hide();
      $('#new_price_break_' + $('#product_option_' + iRowID).val() ).show();
    }


    function populateQuickOrderProduct(strPrefixID, strProductTitle, strProductID, strProductReference, strProductPrice, strAttributes, strProductOptions)
    {
      strProductPrice = unescape(strProductPrice.replace(/\+/g,  " "));
      // ah hello funny "A-hat" characters... so, we meet again.... DIE! DIE! DIE!
      strProductPrice = strProductPrice.replace(/Â/g, "");

      $('#' + strPrefixID).val(strProductReference);
      if (strAttributes) {
        $('#' + strPrefixID + '_name').text(strProductTitle + ' - ' + strAttributes);
      }
      else
      {
        $('#' + strPrefixID + '_name').text(strProductTitle);
      }
      $('#' + strPrefixID + '_price').text('');
      $('#' + strPrefixID + '_price').text(strProductPrice);
      $('#' + strPrefixID + '_price').decHTMLifEnc();

      var i = 0;
      while($('#new_price_break_' + i).length > 0)
      {
        $('#new_price_break_' + i).attr('className', 'price_break_' + strProductID);
        $('#new_price_break_' + i).attr('id', 'price_break_' + strProductID + '_' + $('#new_price_break_' + i).attr('name'));
        i++;
      }
      strPriceBreakID = 'price_break_' + strProductID;
      $('#' + strPrefixID + '_quantity').attr('name', 'multi_quantity[' + strProductID + ']');
      $('#' + strPrefixID + '_quantity').bind('change', function (){
        togglePriceBands(this.value, strPriceBreakID);
      });
      $('#' + strPrefixID + '_options').attr('innerHTML', '<input type="hidden" name="product_option[]" value="' + strProductID + '" />');

      $('#' + strPrefixID + '_plus').unbind('click');
      $('#' + strPrefixID + '_minus').unbind('click');
      $('#' + strPrefixID + '_plus').bind('click', function(){
        simpleAlterQuantity(1, 0, 0, strPrefixID + '_quantity');
        togglePriceBands($('#' + strPrefixID + '_quantity').val(), strPriceBreakID);
      });
      $('#' + strPrefixID + '_minus').bind('click', function(){
        simpleAlterQuantity(-1, 0, 0, strPrefixID + '_quantity');
        togglePriceBands($('#' + strPrefixID + '_quantity').val(), strPriceBreakID);
      });

      /*if(strProductOptions == '')
      {
        $('#' + strPrefixID + '_options').text('--');
      }
      else
      {
        $('#' + strPrefixID + '_options').text(strProductOptions);
      }*/
      //$('#product_autocomplete').hide();
      addQuickOrderRow();
    }

    function addQuickOrderRow()
    {
      var iPointer = 1;
      while($('#quick_order_row' + iPointer).length > 0)
      {
        if($('#product_reference_' + iPointer).val() != '')
        {
          iPointer++;
        }
        else
        {
          return
        }
      }

      strNewRowHTML  = '<tr id="quick_order_row'+iPointer+'">';
      strNewRowHTML += '<td class="row_number">'+iPointer+'</td>';
      //strNewRowHTML += '<td class="product_reference"><input type="text" class="textbox_code ajax_product_search" name="product_reference['+iPointer+']" id="product_reference_'+iPointer+'" value="" /> <input type="submit" class="ajax_product_search_button" name="'+iPointer+'" id="product_go_'+iPointer+'" value="Go" /></td>';
      strNewRowHTML += '<td class="product_reference"><input type="text" class="textbox_code ajax_product_search" name="product_reference['+iPointer+']" id="product_reference_'+iPointer+'" value="" /></td>';
      strNewRowHTML += '<td class="product_name"><span id="product_reference_'+iPointer+'_name"></span></td>';
      strNewRowHTML += '<td class="product_options"><span id="product_reference_'+iPointer+'_options"></span></td>';
      strNewRowHTML += '<td class="product_quantity" align="center">';
      strNewRowHTML += '<table>';
      strNewRowHTML += '<tr>';
      strNewRowHTML += '<td><img src="/images/img_minus.gif" class="quantity_link" id="product_reference_'+iPointer+'_minus" alt="-" /></td>';
      strNewRowHTML += '<td><span><input type="text" name="multi_quantity[]" id="product_reference_'+iPointer+'_quantity" class="quick_qty" value="1" /></span></td>';
      strNewRowHTML += '<td><img src="/images/img_plus.gif" class="quantity_link" id="product_reference_'+iPointer+'_plus" alt="+"  /></td>';
      strNewRowHTML += '</tr>';
      strNewRowHTML += '</table>';
      strNewRowHTML += '</td>';
      strNewRowHTML += '<td class="product_price"><span id="product_reference_'+iPointer+'_price"></span></td>';
      strNewRowHTML += '<td class="product_delete"><span id="product_reference_'+iPointer+'_delete"></span></td>';
      strNewRowHTML += '</tr>';

      $('#quick_table').append(strNewRowHTML);
      $('#product_reference_'+iPointer).focus();
      //$('#product_autocomplete').html(' ');
      initVS3Ajax();
      initQuickBuySearchButtons();
    }

    function initQuickBuySearchButtons(){
      $('.ajax_product_search').unbind('blur');
      $('.ajax_product_search').keydown(function(e)
      {
        if(e.keyCode == 13) {
          strTemp = $(this).attr('id');
          strID = strTemp.replace(/product_reference_/, '');
          //alert(strID + '/' + $(this).val());
          runQuickOrderProductSearch($(this).val(), 'product_reference_' + strID);
          return false;
        }
      });
      $('.ajax_product_search_button').bind('click', function() {
        //alert('woo');
        runQuickOrderProductSearch($('#product_reference_' + this.name).val(), 'product_reference_' + this.name);
        return false;
      });
    }

    function initQuickOrderTextBoxes()
    {
      $('.ajax_product_search').blur(function () {
        objTextBox = $('#'+$(this).attr('id'));
        runQuickOrderProductSearch(objTextBox.val(), $(this).attr('id'));
      });

      $('.ajax_product_search').keydown(function(e) {
        if(e.keyCode == 13)
        {
          objTextBox = $('#'+$(this).attr('id'));
          runQuickOrderProductSearch(objTextBox.val(), $(this).attr('id'));
        }
      });
    }

    function runQuickOrderProductSearch(strValue, strID)
    {
      if (blnRunSearch) {
        //alert(strValue + '/' + strID);
        blnRunSearch = false;
        //window.setTimeout('alert("' + strValue + '"); blnRunSearch = true;', 300);
        window.setTimeout("ajaxQuickOrderProductSearch('" + strValue + "', '" + strID + "');", 300);
      }
    }
