function formcheck_newsletter()
{
  var a = 0;
  var error_text = 'ok';
  var fields = new Array('newsletter_name','newsletter_email');
  var email = document.getElementById('newsletter_email').value;
  var emailfilter  = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
  for(a=0;a<fields.length;a++) {
    if(document.getElementById(fields[a]).value == '') {
      error_text='<?php echo $texts_newsletter["must_be_filled"]; ?>';
      return error_text;
    }
  }
  if(!emailfilter.test(email)) {
    error_text='<?php echo $texts_newsletter["bad_email"]; ?>';
    return error_text;
  }
  return error_text;
}

function formatText(index, panel)
{
  return index + "";
}

function onlyNumbers(e)
{
  var keynum
  var keychar
  var numcheck

  if(window.event) // IE
  {
    keynum = e.keyCode
  }
  else if(e.which) // Netscape/Firefox/Opera
  {
    keynum = e.which
  }

  keychar = String.fromCharCode(keynum)
  numcheck = /\d/
  return (numcheck.test(keychar)||keynum==8||keynum==9||(keynum>95&&keynum<106))
}

function slideSwitch() {
  var $active = $('#slideshowTop IMG.active');

  if ( $active.length == 0 ) $active = $('#slideshowTop IMG:last');

  // use this to pull the images in the order they appear in the markup
  var $next =  $active.next().length ? $active.next()
  : $('#slideshowTop IMG:first');

  // uncomment the 3 lines below to pull the images in random order

  // var $sibs  = $active.siblings();
  // var rndNum = Math.floor(Math.random() * $sibs.length );
  // var $next  = $( $sibs[ rndNum ] );

  $active.addClass('last-active');

  $next.css({
    opacity: 0.0
  })
  .addClass('active')
  .animate({
    opacity: 1.0
  }, 1000, function() {
    $active.removeClass('active last-active');
  });
}

function calculate()
{
  var total_price = 0;
  document.getElementById('debug').innerHTML = '';
  $('tr.product_row').each(function()
  {
    var productId    = $(this).attr('id');

    if(document.getElementById('price_' + productId).value && document.getElementById('price_' + productId).value.length)
    {
      if(document.getElementById('count_' + productId).value && document.getElementById('count_' + productId).value.length){
        count = parseInt(document.getElementById('count_' + productId).value);
        price = parseInt(document.getElementById('price_' + productId).value);
        sum_price = count * price;
        total_price = total_price + sum_price;
        document.getElementById('sum_price_' + productId).value = number_format(sum_price, 0, ',', ' ') + ' Ft';
      }
    }
  });

  if (parseInt(total_price) < 82000) /* = br. 102500 */
  {
    $('#shipping_1').css('display', 'block');
    document.getElementById('shipping_1').style.display = 'block';
    document.getElementById('shipping_2').style.display = 'block';
    total_price = total_price + 2000;
  }
  else
  {
    //document.getElementById('shipping').style.display = 'none';
//    $('div.shipping').css('display') = 'none';
    document.getElementById('shipping_1').style.display = 'block';
    document.getElementById('shipping_2').style.display = 'none';
    $('div.shipping').css('display', 'none');
  }

  $('span.total_price').each(function(){
    this.innerHTML =  number_format(total_price, 0, ',', ' ');
  });
  $('span.total_price_vat').each(function(){
    this.innerHTML = number_format(Math.round(total_price * 1.25), 0, ',', ' ');
  });

  return total_price;
}

function formcheck_contact()
{
  total_price = calculate();
  if (parseInt(total_price) < 24000)
    return 'A megrendelés összege nem érte el a minimálisan rendelhető 30 000 Ft-os értékhatárt!';

  var error_text = 'ok';
  var fields = new Array('company','name','address','email','phone');
  var contact_email = document.getElementById('email').value;
  var emailfilter  = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
  for(a=0;a<fields.length;a++) {
    if(document.getElementById(fields[a]).value == '') {
      error_text='A *-gal jelölt mezők kitöltése kötelező!';
      return error_text;
    }
  }
  if(!emailfilter.test(contact_email)) {
    error_text='Hibás formátumú e-mail cím!';
    return error_text;
  }
  return error_text;
}

$(function () {
  $('.anythingSlider').anythingSlider({
    easing: "easeInOutExpo",        // Anything other than "linear" or "swing" requires the easing plugin
    autoPlay: true,                 // This turns off the entire FUNCTIONALY, not just if it starts running or not.
    delay: 3000,                    // How long between slide transitions in AutoPlay mode
    startStopped: false,            // If autoPlay is on, this can force it to start stopped
    animationTime: 600,             // How long the slide transition takes
    hashTags: true,                 // Should links change the hashtag in the URL?
    buildNavigation: true,          // If true, builds and list of anchor links to link to each slide
    pauseOnHover: true,             // If true, and autoPlay is enabled, the show will pause on hover
    startText: "",                // Start text
    stopText: "",               // Stop text
    navigationFormatter: formatText       // Details at the top of the file on this use (advanced use)
  });
  $("#slide-jump").click(function(){
    $('.anythingSlider').anythingSlider(6);
  });
});

/* Made by Mathias Bynens <http://mathiasbynens.be/> */
function number_format(a, b, c, d) {
  a = Math.round(a * Math.pow(10, b)) / Math.pow(10, b);
  e = a + '';
  f = e.split('.');
  if (!f[0]) {
    f[0] = '0';
  }
  if (!f[1]) {
    f[1] = '';
  }
  if (f[1].length < b) {
    g = f[1];
    for (i=f[1].length + 1; i <= b; i++) {
      g += '0';
    }
    f[1] = g;
  }
  if(d != '' && f[0].length > 3) {
    h = f[0];
    f[0] = '';
    for(j = 3; j < h.length; j+=3) {
      i = h.slice(h.length - j, h.length - j + 3);
      f[0] = d + i +  f[0] + '';
    }
    j = h.substr(0, (h.length % 3 == 0) ? 3 : (h.length % 3));
    f[0] = j + f[0];
  }
  c = (b <= 0) ? '' : c;
  return f[0] + c + f[1];
}


$(document).ready(function() {
  $("a#example2").fancybox({
    'titleShow'     : false,
    'transitionIn'	: 'elastic',
    'transitionOut'	: 'elastic'
  });

  $("a[rel=example_group]").fancybox({
    'transitionIn'	: 'none',
    'transitionOut'	: 'none',
    'titlePosition' 	: 'over',
    'titleFormat'       : function(title, currentArray, currentIndex, currentOpts) {
      return '<span id="fancybox-title-over">Image ' +  (currentIndex + 1) + ' / ' + currentArray.length + '</span>';
    }
  });
});

$(function() {
  setInterval( "slideSwitch()", 3000 );
});

