 jQuery(document).ready(function() {
   
    // Catalog
    jQuery("#dialog_cat").dialog({
      bgiframe: true,
      autoOpen: false,
      width: 400,
      height: 400,
      modal: true,
      buttons: {
        OK: function() {
          jQuery.post("/mb360/catalog_request.php", { prename: jQuery("#prename").val(), name: jQuery("#name").val(), street: jQuery("#street").val(), plz: jQuery("#plz").val(), city: jQuery("#city").val() } );
          jQuery('#catalog_request_form').hide();
          jQuery('#cat_dia_response').show().delay(3000).queue(function() {
            jQuery("#dialog_cat").dialog('close');
            jQuery('#cat_dia_response').hide()
            jQuery('#catalog_request_form').show();
          });
        },
        Abbrechen: function() {
          jQuery(this).dialog('close');
        }
      }
    });
    jQuery('#catalog_request').click(function() {
      jQuery('#dialog_cat').dialog('open');
    });
    
    // Newsletter
    jQuery("#dialog_nl").dialog({
      bgiframe: true,
      autoOpen: false,
      width: 400,
      height: 400,
      modal: true,
      buttons: {
        OK: function() {
          jQuery.post("/mb360/nl_request.php", { mail: jQuery("#mail").val(), option: jQuery("input[name='group1']:checked").val() } );
          jQuery('#nl_request_form').hide();
          jQuery('#nl_dia_response').show().delay(3000).queue(function() {
            jQuery("#dialog_nl").dialog('close');
            jQuery('#nl_dia_response').hide()
            jQuery('#nl_request_form').show();
          });
        },
        Abbrechen: function() {
          jQuery(this).dialog('close');
        }
      }
    });
    jQuery('#nl_request').click(function() {
      jQuery('#dialog_nl').dialog('open');
    });
    
    
    // Return
    jQuery("#dialog_return_request").dialog({
      bgiframe: true,
      autoOpen: false,
      width: 500,
      height: 600,
      modal: true,
      buttons: {
        OK: function() {
          var bValid = true;
          jQuery('input[type=text]', this).removeClass( "ui-state-error" );
          
          jQuery('input[type=text]', this).each( function(){
            if( !this.value ) {
              bValid = false;
              jQuery(this).addClass('ui-state-error');
            }
          });

          if ( bValid ) {
            jQuery.post("/mb360/return_request.php", jQuery('#return_request_form').serialize() );
            jQuery('#return_request_form').hide();
            jQuery('#return_dia_response').show().delay(3000).queue(function() {
              jQuery("#dialog_return_request").dialog('close');
              jQuery('#return_dia_response').hide()
              jQuery('#return_request_form').show();
            });
          }
        },
        Abbrechen: function() {
          jQuery(this).dialog('close');
        }
      }
    });
    jQuery('#return_request').click(function() {
      jQuery('#dialog_return_request').dialog('open');
    });
    
    function remove_line(){
      jQuery('.remove_item').click(function() {
        jQuery(this).parent().remove();
      });
    }
    
    remove_line();
    // Item Line
    jQuery('#add_item').click(function() {
      var lines = 1;
      if(jQuery('.remove_item:last').attr('rel')) {
        lines = jQuery('.remove_item:last').attr('rel');
        lines++;
      }
      jQuery('#return-item-list').append('<div class="item-line"><label for="anr[' + lines + ']">' + lines + ') Artikelnummer / Menge / Preis</label><input type="text" name="anr[' + lines + ']" id="anr[' + lines + ']" size="20" class="anr-return-line text ui-widget-content ui-corner-all" /> / <input type="text" name="amount[1]" id="amount[' + lines + ']" size="5" class="amount-return-line text ui-widget-content ui-corner-all" /> / <input type="text" name="price[' + lines + ']" id="price[' + lines + ']" size="5" class="price-return-line text ui-widget-content ui-corner-all" /> <a class="remove_item" rel="' + lines + '" title="Remove Item">X</a></div>');
      remove_line();
    });
    
  });

