// ---------------------------------------------------------------------------
// chkString()
// ---------------------------------------------------------------------------
function chkString(elem, len, info)
{
  var field = eval("window.document.dialog." + elem);
  if (field.value.length < len) {
    if (info.length) { alert(info); }
    field.focus();
    return false;
  }

  return true;
}


// ---------------------------------------------------------------------------
// chkEMail()
// ---------------------------------------------------------------------------
function chkEMail(elem, len, info)
{
  var bOk = true;
  var field = eval("window.document.dialog." + elem);
  if (field.value.length < len) { bOk = false; }
  
  if (bOk == true) {
    if ((field.value.indexOf('@') == -1) || (field.value.indexOf('.') == -1)) { bOk = false; }
  }
  
  if (bOk == false) {
    if (info.length) { alert(info); }
    field.focus();
    return false;
  } 

  return true;
}


// ---------------------------------------------------------------------------
// chkFormGewinn()
// ---------------------------------------------------------------------------
function chkFormGewinn()
{
  $.ajax({
    type: 'POST',
    url: '/ajax/form_check?id=gewinn',
    data: $('#form_gewinn').serialize(),
    dataType: 'json',
    success: function(data) {
      if (data.nok.length > 0) {
        $('#captcha').attr('src', '/img/captcha?' + Math.random());
        $('#captcha_code').attr('value', '');

        name = data.nok[0];
        if (name == 'cb_bestaetigung') {
          name = 'captcha_code';
        }
        $('#form_gewinn [name=' + name + ']').focus().select();
      } else {
        $('#gewinnform').hide();
        $('#bedingungen').hide();

        $('#danke').show();
      }
    }
  });
}


// ---------------------------------------------------------------------------
// chkFormOrder()
// ---------------------------------------------------------------------------
function chkFormOrder()
{
  $.ajax({
    type: 'POST',
    url: '/ajax/form_check?id=order',
    data: $('#form_order').serialize(),
    dataType: 'json',
    success: function(data) {
      if (data.nok.length > 0) {
        var err_txt = '';
        for (var idx = 0; idx < data.err.length; idx++) {
          if (data.err[idx].length > 0) {
            if (err_txt.length > 0) {
              err_txt = err_txt + '<br />';
            }
            err_txt = err_txt + data.err[idx]
          }
        }

        if (err_txt.length > 0) {
          $('.pflichtfeld_text').html(err_txt);
        }

        $('#form_order [name=' + data.nok[0] + ']').focus().select();
        
        $('#termin_min').text(data.termin_min);
      } else {
        location.href = $('#form_order input:submit').attr('src');
      }
    }
  });
}


// ---------------------------------------------------------------------------
// getCartCount()
// ---------------------------------------------------------------------------
function getCartCount()
{
  return parseInt($('#cartCount').text());
}


// ---------------------------------------------------------------------------
// addToCart()
// ---------------------------------------------------------------------------
function addToCart(ar_no, quantity, img)
{
  $.ajax({
    type: 'POST',
    url: '/ajax/cart?act=add',
    data: { ar_no: escape(ar_no), quantity: quantity },
    dataType: 'json',
    success: function(data) {
      $('#cartCount').text(data.count);
    },
    beforeSend: function() {
      $(img).attr('src', '/img/shop/loading.gif');
    },
    complete: function() {
      $(img).attr('src', '/img/shop/ico_cart.jpg');
    }
  });
}


// ---------------------------------------------------------------------------
// delFromCart()
// ---------------------------------------------------------------------------
function delFromCart(ar_no, img)
{
  $.ajax({
    type: 'POST',
    url: '/ajax/cart?act=del',
    data: { ar_no: escape(ar_no) },
    dataType: 'json',
    success: function(data) {
      location.href = location.href;
    },
    beforeSend: function() {
      $(img).attr('src', '/img/shop/loading.gif');
    },
    complete: function() {
      $(img).attr('src', '/img/shop/ico_delete.jpg');
    }
  });
}


// ---------------------------------------------------------------------------
// updateCart()
// ---------------------------------------------------------------------------
function updateCart()
{
  $.ajax({
    type: 'POST',
    url: '/ajax/cart?act=update',
    data: $('#form_cart').serialize(),
    dataType: 'json',
    success: function(data) {
      location.href = location.href;
    }
  });
}


// ---------------------------------------------------------------------------
// clearCart()
// ---------------------------------------------------------------------------
function clearCart()
{
  $.ajax({
    type: 'POST',
    url: '/ajax/cart?act=clear',
    dataType: 'json',
    success: function(data) {
      location.href = location.href;
    }
  });
}


// ---------------------------------------------------------------------------
// main
// ---------------------------------------------------------------------------
$(function() {
  // -------------------------------------------------------------------------
  // Lightbox
  // -------------------------------------------------------------------------
  $('a[rel=lightbox]').lightBox();

  // -------------------------------------------------------------------------
  // Inputs/Textareas immer hervorheben
  // -------------------------------------------------------------------------
  $('input,textarea').focus(function() {
    $(this).css('backgroundColor', '#FFF7E2');
    $(this).select();
  }).blur(function() {
    $(this).css('backgroundColor', '#FFFFFF');    
  });

  // -------------------------------------------------------------------------
  // allg_kontakt
  // -------------------------------------------------------------------------
  if ($('#form_kontakt').length > 0) {
    $('#form_kontakt #name').focus();

    $('#form_kontakt').submit(function() {
      if (!chkString("Name", 3, "Bitte geben Sie Ihren Namen ein !")) { return false; }
      if (!chkString("Strasse", 3, "Bitte geben Sie Ihre Strasse und Hausnummer an !")) { return false; }
      if (!chkString("PLZ_Ort", 3, "Es fehlen die Postleitzahl und der Ort !")) { return false; }
      if (!( (chkEMail("Email", 10, "")) || (chkString("Telefon", 8, "")) || (chkString("Fax", 8, "")) )) {
        alert("Bitte geben Sie entweder eine Telefon-, Telefaxnummer bzw. E-Mail an !");
        window.document.dialog.Email.focus();
        return false;
      }    
      return true;
    });
  }

  // -------------------------------------------------------------------------
  // frisch_filial
  // -------------------------------------------------------------------------
  if ($('#form_filial').length > 0) {
    $('#form_filial input[type!=hidden]:first').focus().select();
  }

  // -------------------------------------------------------------------------
  // frostig_sb
  // -------------------------------------------------------------------------
  if ($('#form_sb_login').length > 0) {
    $('#form_sb_login input[type!=hidden]:first').focus().select();
  }

  // -------------------------------------------------------------------------
  // Shop-Login
  // -------------------------------------------------------------------------
  if ($('body.de_shop_login').length > 0) {
    $('#col2 input[type!=hidden]:first').focus().select();
  }

  // -------------------------------------------------------------------------
  // Shop-Produktliste
  // -------------------------------------------------------------------------
  if ($('body.de_shop_prodlist').length > 0) {
    $('#col2 input[type!=hidden]:first').focus().select();

    $('#col2 input[type=text]').keypress(function(event) {
      if (event.keyCode == 13) {
        $(this).closest('td').next('td.scb').children('img').click();      
      }
    });

    $('.addToCart').click(function() {
      var inputs = $(this).closest('td').prev('td.sqty').children('input');
      var ar_no = $(inputs[0]).attr('value');
      var quantity = parseInt($(inputs[1]).attr('value'));
  
      if (quantity > 0) {
        addToCart(ar_no, quantity, this);
      }
    });

    $('#change_cat').change(function() {
      var value = $(this).val();
      if (value != '#') {
        location.href = value;
      }
    });
  }

  // -------------------------------------------------------------------------
  // Shop-Warenkorb
  // -------------------------------------------------------------------------
  if ($('body.de_shop_cart').length > 0) {
    $('#col2 input[type!=hidden]:not(:button):first').focus().select();

    $('.delFromCart').click(function() {
      var inputs = $(this).closest('td').prev('td.sqty').children('input');
      var ar_no = $(inputs[0]).attr('value');
  
      delFromCart(ar_no, this);
    });

    $('#form_cart').submit(function() {
      return false;
    });

    $('#updateCart').click(function() {
      if (getCartCount() > 0) {
        updateCart();
      }
    });

    $('#clearCart').click(function() {
      if (getCartCount() > 0) {
        clearCart();
      }
    });
  
    $('#orderForm').click(function() {
      if (getCartCount() > 0) {
        location.href = $(this).attr('src');
      }
    });
  }

  // -------------------------------------------------------------------------
  // Shop-Bestellformular
  // -------------------------------------------------------------------------
  if ($('body.de_shop_form').length > 0) {
    $('#col2 input[type!=hidden]:first').focus().select();

    $('#form_order').submit(function() {
      chkFormOrder();
      return false;
    });
    
    $('input:reset').click(function() {
      $('#form_order input[type=text], #form_order textarea').not('input:hidden').val('');
      $('#form_order input[name=cb_bestaetigung]').attr('checked', true);

      $('#col2 input[type!=hidden]:first').focus().select();
      $('.pflichtfeld_text').html('');

      return false;
    });
  }

  // -------------------------------------------------------------------------
  // Gewinnspiel
  // -------------------------------------------------------------------------
  if ($('#form_gewinn').length > 0) {
    $('#form_gewinn').submit(function() {
      chkFormGewinn();
      return false;
    });
  }
});
