$(document).ready(function(){
  $last_key = '';
  $run_focus = false;

   //hide default way
  $(".no-javascript").hide();
  $("form#quick-order").attr("action","quick-order-process.asp");
  
  //focus on a particular quantity field
  function run_quantity_focus()
  {
    if($last_key != '' && $run_focus){$("#quantity" + $last_key).focus();}
    $run_focus = false;
  }

  //show/hide submit button
  function can_we_submit()
  {
    var submit_us = true;
    var item_count = 0;

    $(".ajax_code td.code input.product_code").each(function(){
      if($(this).hasClass("invalid")){submit_us = false;}
      if($(this).hasClass("verified")){item_count ++;}
    });

    if(item_count == 0){submit_us = false;}

    if(submit_us){
      $(".javascript-available").show();
      $(".make-sure-valid").hide();
    }else{
      $(".javascript-available").hide();
      $(".make-sure-valid").show();
    }
  }

  //show listing of related products
  function load_product_codes(item_key, product_code)
  {
    $.post("_ajax-quickOrder.asp", {action: "generate_list", item_key : item_key, product_code : product_code}, 
      function(data){
        $("#pcodes" + item_key).html(data);
        $("#pcodes" + item_key).show();

        //when user clicks on related product: 1. save into input box, 2. verify details exist, 3. grab details and display them
        $(".ajax_code_choice").click(function(){
          var product_code = $(this).find("a").text();
          var product_id = $(this).find(".product_id").val();
          var count = $("#quantity" + item_key).val();

          //check if there is a count to see if its first time
          var first_time = (count == '') ? 1 : 0;

          verify_row(item_key,product_code,count,first_time);

          //when user clicks away from input, close related listings
          $(this).blur($("#pcodes" + item_key).hide());
        });
      }
    );
  }
  
  //check all rows to load data
  function verify_rows()
  {
    $(".ajax_code td.code input.product_code").each(function(){
      var input_id = $(this).attr("id");
      var item_key = input_id.substring(4);
      var product_code = $(this).val();
      var count = $("#quantity" + item_key).val();

      //check if there is a count to see if its first time
      var first_time = (count == '') ? 1 : 0;
      
      if(product_code.length > 0){verify_row(item_key, product_code,count, first_time)};
    });
  }

  //verify one row
  function verify_row(item_key,product_code,count,first_time)
  {
    $.post("_ajax-quickOrder.asp", {action: "verify_row", item_key : item_key, product_code : product_code, count : count, first_time : first_time},
      function(data)
      {
        $("#row" + item_key).html(data);
        init_events();
      }
     );
  }

  //load event listeners
  function init_events()
  {
    //set an event listener for when an input is invalid
    $(".ajax_code").change(function(event){
      if($(event.target).is("input.invalid"))
      {
        $(".ajax_code td.code input.invalid").each(function(){
          var input_id = $(this).attr("id");
          var item_key = input_id.substring(4);
          var product_code = $(this).val();
          var count = $("#quantity" + item_key).val();

          //check if there is a count to see if its first time
          var first_time = (count == '') ? 1 : 0;

          if($(this).val() == '')
          {
            verify_row(item_key, product_code, count, first_time);
          }
        });
      }
    });
    

    //when user starts entering characters into input box load related products
    $(".ajax_code td.code input.product_code, .ajax_code td.code input.invalid").keyup(function(){
      var input_id = $(this).attr("id");
      var item_key = input_id.substring(4);
      var product_code = $(this).val();

      load_product_codes(item_key, product_code);
    });

    //when user starts entering characters into input box load related products
    $(".ajax_code td.code input.verified").blur(function(){
      var input_id = $(this).attr("id");
      var item_key = input_id.substring(4);
      var product_code = $(this).val();
      var count = $("#quantity" + item_key).val();

      //check if there is a count to see if its first time
      var first_time = (count == '') ? 1 : 0;
      
      verify_row(item_key, product_code, count, first_time);
    });

    //when user starts entering characters into input box load related products
    $(".ajax_code td.quantity input").blur(function(){
      var input_id = $(this).attr("id");
      var item_key = input_id.substring(8);
      var product_code = $("#code" + item_key).val();
      var count = $(this).val();

      //check if there is a count to see if its first time
      var first_time = (count == '') ? 1 : 0;

      verify_row(item_key, product_code, count, first_time);
    });

    //when user presses enter in quantity input box load related products
    $(".ajax_code td.quantity input").keypress(function(event){
      //if enter is pressed in the quantity input
      if(event.keyCode == 13)
      {
        var input_id = $(this).attr("id");
        var item_key = input_id.substring(8);
        var product_code = $("#code" + item_key).val();
        var count = $(this).val();

        //check if there is a count to see if its first time
        var first_time = (count == '') ? 1 : 0;

        verify_row(item_key, product_code, count, first_time);

        //stop the enter key submitting the form
        return false;
      }
    });
    
    //when user presses enter in code input box load related products
    $(".ajax_code td.code input").keypress(function(event){
      //if enter is pressed in the quantity input
      if(event.keyCode == 13)
      {
        var input_id = $(this).attr("id");
        var item_key = input_id.substring(4);
        var product_code = $(this).val();
        var count = $("#quantity" + item_key).val();

        //check if there is a count to see if its first time
        var first_time = (count == '') ? 1 : 0;
        verify_row(item_key, product_code, count, first_time);
        
        //stop the enter key submitting the form - then foxus on quantity field
        $last_key = item_key;
        $run_focus = (product_code != '') ? true : false;
        return false;
      }
    });

    can_we_submit();
    tb_init('.ajax_code a.thickbox');
    run_quantity_focus();
  }

  verify_rows();
  init_events();
});