/*
Shop - Initialize
*/
$(document).ready(
  function() {
    function_shop_initialize();
  }
);

var string_error_selection = "Es wurde keine Anzahl an Tonträgern angegeben.";
var string_error_address = "Die Adresse ist unvollständig.";

function function_shop_initialize () {
  var number_shop_item_cost = 10;
  var number_shop_shipping_cost = 3;
  var object_shop_form = $("form.shop_form");
  var number_product_count = 0;
  var string_shop_table_tbody = "";
  var boolean_return = false;

  // Basic condition: Checks whether FORM.shop_form is present.
  if (object_shop_form.length > 0) {

    // Wizard
    $(".shop_wizard li span").each(
      function (number_step) {
        $(this).click(
          function () {
            for (var number_i=0;number_i<$(".shop_wizard li span").length;number_i++) {
              if (number_i == number_step) {

                $("form.shop_form input[name=step]").val(number_step);
                $(".shop-fieldset-"+number_i).css("display", "block");

                boolean_error = false;
                if ( number_step > 1 ) {
                  if ( number_product_count == 0 ) {

                    $(".shop-fieldset-0").css("display", "none");
                    $(".shop-fieldset-1").css("display", "block");
                    $(".shop-fieldset-2").css("display", "none");
                    $(".shop-fieldset-3").css("display", "none");
                    $(".shop_wizard li span").removeClass("active");
                    $(".shop_wizard li span").eq(1).addClass("active");
                    alert(string_error_selection);

                    return;
                  } else {
                    string_shop_table_tbody = "";
                    $('.shop-fieldset-1 div.shop_form_item input.form-text').each(
                      function () {
                        if ( $(this).val() != 0 ) {

                          string_shop_table_tbody += "<tr>";
                          string_shop_table_tbody += '<td class="shop-table-amount">' + $(this).parents('div.shop_form_item').find('input.form-text').val() + '</td>';

                          string_shop_table_tbody += '<td class="shop-table-description">' + $(this).parents('div.shop_form_item').find('label').text() + '</td>';
                          string_shop_table_tbody += '<td class="shop-table-price">' + (Number($(this).parents('div.shop_form_item').find('input.form-text').val()) * number_shop_item_cost) + ' €</td>';

                          string_shop_table_tbody += "</tr>";
                        }
                      }
                    )
                    $('.shop-fieldset-3 table tbody tr').each(
                      function (number_i) {
                        if (number_i < $('.shop-fieldset-3 table tbody tr').length) {
                          $(this).remove();
                        }
                      }
                    )
                    // string_shop_table_tbody = string_shop_table_tbody + '<tr><td class="shop-table-amount">1</td><td class="shop-table-description">Versandkosten</td><td class="shop-table-price">3 €</td></tr>';
                    $('.shop-fieldset-3 table tbody tr').remove();
                    $(string_shop_table_tbody).appendTo('.shop-fieldset-3 table tbody');

                    $('fieldset.shop-fieldset-3 table tfoot tr td.shop-table-price-sum').text(String( number_product_count * number_shop_item_cost /* + number_shop_shipping_cost */ ) + ' €');

                  }
                }

                if ( number_step > 2 ) {
                  $('.shop-fieldset-2 div.shop_form_item input.form-text').each(
                    function () {
                      if ( $(this).val() == "") {
                        boolean_error = true;
                      };
                    }
                  )

                  if (boolean_error == true) {
                    $(".shop-fieldset-0").css("display", "none");
                    $(".shop-fieldset-1").css("display", "none");
                    $(".shop-fieldset-2").css("display", "block");
                    $(".shop-fieldset-3").css("display", "none");
                    $(".shop_wizard li span").removeClass("active");
                    $(".shop_wizard li span").eq(2).addClass("active");
                    alert(string_error_address);
                    return;
                  }
                }

              } else {
                $(".shop-fieldset-"+number_i).css("display", "none");
              }
            }
            $(".shop_wizard li span").removeClass("active");
            $(this).addClass("active");
          }
        )
      }
    )

    // Pager
    $(".shop-fieldset .shop_form_pager").each(
      function (number_step) {
        $(this).find("li span.next").click(
          function () {
            boolean_error = false;
            $("form.shop_form input[name=step]").val(number_step+1);
            if ( number_step >= 1 ) {
              if ( number_product_count == 0 ) {
                alert("Es wurde keine Anzahl an Tonträgern angegeben.");
                return;
              } else {
                if ( number_step == 1 ) {
                  string_shop_table_tbody = "";
                  $('.shop-fieldset-1 div.shop_form_item input.form-text').each(
                    function () {
                      if ( $(this).val() != 0 ) {
                        string_shop_table_tbody += "<tr>";
                        string_shop_table_tbody += '<td class="shop-table-amount">' + $(this).parents('div.shop_form_item').find('input.form-text').val() + '</td>';

                        string_shop_table_tbody += '<td class="shop-table-description">' + $(this).parents('div.shop_form_item').find('label').text() + '</td>';
                        string_shop_table_tbody += '<td class="shop-table-price">' + (Number($(this).parents('div.shop_form_item').find('input.form-text').val()) * number_shop_item_cost) + ' €</td>';


                        string_shop_table_tbody += "</tr>";
                      }
                    }
                  )
                  // string_shop_table_tbody = string_shop_table_tbody + '<tr><td class="shop-table-amount">1</td><td class="shop-table-description">Versandkosten</td><td class="shop-table-price">3 €</td></tr>';
                  $('.shop-fieldset-3 table tbody tr').remove();
                  $(string_shop_table_tbody).appendTo('.shop-fieldset-3 table tbody');

                  $('fieldset.shop-fieldset-3 table tfoot tr td.shop-table-price-sum').text(String( number_product_count * number_shop_item_cost /* + number_shop_shipping_cost */ ) + ' €');
                }
              }
            }

            if ( number_step >= 2 ) {
              $('.shop-fieldset-2 div.shop_form_item input.form-text').each(
                function () {
                  if ( $(this).val() == "") {
                    boolean_error = true;
                  };
                }
              )

              if (boolean_error == true) {
                alert("Die Adresse ist unvollständig.");
                return;
              }
            }

            $(".shop-fieldset-"+(number_step)).css("display", "none");
            $(".shop-fieldset-"+(number_step+1)).css("display", "block");
            $(".shop_wizard li span").eq(number_step).removeClass("active");
            $(".shop_wizard li span").eq(number_step+1).addClass("active");
          }
        )
        $(this).find("li span.previous").click(
          function () {
            $("form.shop_form input[name=step]").val(number_step-1);
            $(".shop-fieldset-"+(number_step)).css("display", "none");
            $(".shop-fieldset-"+(number_step-1)).css("display", "block");
            $(".shop_wizard li span").eq(number_step).removeClass("active");
            $(".shop_wizard li span").eq(number_step-1).addClass("active");
          }
        )
      }
    )

    // Product selection: Updates INPUT fields
    $('.shop-fieldset-1 input.form-text').blur(
      function () {

        if ( isNaN( $(this).val() ) ) {
          //alert("Bitte geben Sie die Anzahl der Tonträger ein, die Sie erweben möchten.");
          $(this).val('0');
        } else {

          number_product_count = 0;
          $('.shop-fieldset-1 input.form-text').each(
            function () {
              number_product_count += Number( $(this).val() );
            }
          )
        }
      }
    )

    /**
     * Sum calculation: Used when user sends FORM by hitting the RETURN KEY
     */
    number_product_count = 0;
    $('form .shop-fieldset-1 input.form-text').each(
      function () {

        if ( isNaN( $(this).val() ) ) {
          $(this).val('0');
        } else {
          number_product_count += Number( $(this).val() );
        }
      }
    )

    /**
     * Error Messages: Used when user hits RETURN
     */
    if ( $('form.error_selection').length > 0 ) {
      alert(string_error_selection);
    }
    if ( $('form.error_address').length > 0 ) {
      alert(string_error_address);
    }
  }
}

