function validate_basket()
{
    if (
            ( $('eraty_potwierdzenie') )
            &&
            ( $('eraty_potwierdzenie').checked == false )
            &&
            ( $('sprzedaz_ratalna').checked == true )
        )
    {
        alert ("Zanim złożysz zamówienie korzystajac z systemu ratalnego, zapoznaj sie z procedura udzielenia kredytu ratalnego eRaty Żagiel.");
        return false;
    }
    else if (
            ( $('platnosc_elektroniczna') )
            &&
            ( $('platnosc_elektroniczna').checked == true )
            &&
            ( document.getElementsByName("pay_type")[0].value == "" )
        )
    {
        alert ("Musisz wybrać rodzaj płatności elektronicznej!");
        return false;
    }
    else
    {
        return true;
    }
}

function preloader(base)
{
  var i = 0;
  imageObj = new Image();
  images = new Array();
  images[0]="artbox_up_b.jpg";
  images[1]="artbox_down_b.jpg";
  images[2]="promobox_left_b.jpg";
  images[3]="promobox_right_b.jpg";
  images[4]="page_nav_left_b.gif";
  images[5]="page_nav_right_b.gif";
  images[6]="bmbtn_a_a2.jpg";

  for(i=0; i<=3; i++) 
  {
    imageObj.src=base + images[i];
  }
}

function input_label(input, password)
{
  if (input.value == '')
  {
    if (password)
    {
      var newO = document.createElement('input');
      newO.setAttribute('type', 'text');
      newO.setAttribute('value', 'e-mail');
      newO.setAttribute('name', input.getAttribute('name'));
      newO.setAttribute('onfocus', 'input_label(this,true)');
      input.parentNode.replaceChild(newO, input);
    }
    else
    {
      input.value = input.defaultValue;
    }
  }
  else if (input.value == input.defaultValue)
  {
    if (password)
    {
      var newO = document.createElement('input');
      newO.setAttribute('type', 'password');
      newO.setAttribute('name', input.getAttribute('name'));
      newO.setAttribute('onblur', 'input_label(this,true)');
      input.parentNode.replaceChild(newO, input);
      newO.focus();
    }
    else
    {
      input.value = '';
    }
  }
}

function menu_bar_select(a)
{
  var temp = ['opony', 'felgi', 'bagazniki', 'lancuchy', 'serwis', 'inwest'];
  for (var i = 0; i < 6; i++)
  {
    if (temp[i] != a)
    {
      $('#bar_a_btn_' + temp[i]).removeClass('active');
      $('#bar_a_btn_a_' + temp[i]).hide();
      $('#bar_a_btn_c_' + temp[i]).hide();
      $('#bar_a_menu_' + temp[i]).hide();
    }
    else
    {
      $('#bar_a_btn_' + a).addClass('active');
      $('#bar_a_btn_a_' + a).show();
      $('#bar_a_btn_c_' + a).show();
      $('#bar_a_menu_' + a).show();
    }
  }
}

function bm_menu(type, a)
{
  var temp = null;
  if (type == 1)
  {
    temp = ['aktualnosci', 'eopon', 'efelg'];
  }
  else if (type == 2)
  {
    temp = ['opony', 'felgi', 'bagazniki', 'lancuchy', 'serwis', 'inwest'];
  }
  else if (type == 3)
  {
    temp = ['opis', 'gwarancja', 'raty', 'zapytaj', 'oznaczenia'];
  }
  for (var i = 0; i < temp.length; i++)
  {
    if (temp[i] != a)
    {
      $('#bm_btn_b_' + temp[i]).removeClass('active');
      if (i == 0)
      {
        $('#bm_btn_a_' + temp[i]).attr('src', img_path + 'bmbtn_a_a2.jpg');
      }
      else
      {
        $('#bm_btn_a_' + temp[i]).attr('src', img_path + 'bmbtn_a_a.jpg');
      }
      $('#bm_btn_c_' + temp[i]).attr('src', img_path + 'bmbtn_a_c.jpg');
      $('#bm_' + temp[i]).hide();
    }
    else
    {
      $('#bm_btn_b_' + a).addClass('active');
      $('#bm_btn_a_' + a).attr('src', img_path + 'bmbtn_b_a.jpg');
      $('#bm_btn_c_' + a).attr('src', img_path + 'bmbtn_b_c.jpg');
      $('#bm_' + a).show();
    }
  }
}

function img_swap(img, file)
{
  img.src = file;
}

/* slider START */

var slider_id = 1;
var slider_count = 7;
var slider_timer = null;

function slider_sel(new_id)
{
  if (new_id != slider_id)
  {
    if (slider_timer)
    {
      clearTimeout(slider_timer);
    }
    $('#slider_' + slider_id).fadeOut(750);
    $('#slider_sel_' + slider_id).attr('class', 'clear');
    slider_id = new_id;
    $('#slider_' + slider_id).fadeIn(750);
    $('#slider_sel_' + slider_id).attr('class', 'selected');
    slider_timer = setTimeout('slider_down()', 5000);
  }
}

function slider_up()
{
  new_id = slider_id - 1;
  if (new_id == 0)
  {
    new_id = slider_count;
  }
  slider_sel(new_id);
}

function slider_down()
{
  new_id = slider_id + 1;
  if (new_id > slider_count)
  {
    new_id = 1;
  }
  slider_sel(new_id);
}

/* slider END */

function init_2()
{
  if (typeof document.addEventListener == 'function')
  {
    mppb_timer = setTimeout('mppb_right()', 3500);
    slider_timer = setTimeout('slider_down()', 5000);
  }
}

/* mainpage_product_box START */

var mppb_id = 0;
var mppb_timer = null;
var mppb_stp = false;

function mppb_animate()
{
  if (mppb_timer)
  {
    clearTimeout(mppb_timer);
  }
  $('#mppb_content').animate({marginLeft: "-" + (mppb_id * 180) + "px"}, 750);
  if (!mppb_stp)
  {
    mppb_timer = setTimeout('mppb_right()', 3500);
  }
}

function mppb_left()
{
  mppb_id++;
  if (mppb_id > mppb_count)
  {
    mppb_id = 1;
    $('#mppb_content').animate({marginLeft: "0px"}, 0);
  }
  mppb_animate();
}

function mppb_right()
{
  mppb_id--;
  if (mppb_id < 0)
  {
    mppb_id = mppb_count - 1;
    $('#mppb_content').animate({marginLeft: "-" + (mppb_count * 180) + "px"}, 0);
  }
  mppb_animate();
}

function mppb_stop()
{
  if (mppb_timer)
  {
    clearTimeout(mppb_timer);
  }
  mppb_stp = true;
}

function mppb_start()
{
  mppb_timer = setTimeout('mppb_left()', 3500);
  mppb_stp = false;
}

/* mainpage_product_box END */

function total_price(s, price)
{
  $('#total_price').html(parseFloat(price * parseFloat(s.options[s.selectedIndex].value)).toFixed(2));
}

/* konfigurator START */ 

function rimChange( target, type )
{
    if ( !type )
        type = 'alloy';

    if ( type == 'scrub' )
    {
        if ( target == 'marka' )
        {
            removeOptions( 'seriascrub_id' );
            addOptions( 'markascrub', 'seria', type );
            submitDisabled();
        }
        else if ( target == 'seria' )
        {
            if ( $('#seriascrub_id').val() > 0 )
                submitEnabled();    
            else
                submitDisabled();
        }
    }
    else if ( target == 'marka' )
    {
        removeOptions( 'seria_id' );
        removeOptions( 'model_id' );
        removeOptions( 'srednica_id' );
        addOptions( 'marka', 'seria', type );
        submitDisabled();
    }
    else if ( target == 'seria' )
    {
        removeOptions( 'model_id' );
        removeOptions( 'srednica_id' );
        addOptions( 'seria', 'model', type );
        submitDisabled();
    }
    else if ( target == 'model' )
    {
        removeOptions( 'srednica_id' );
        addOptions( 'model', 'srednica', type );
        submitDisabled();
    }
    else if ( target == 'srednica' )
    {
        if ( $('#srednica_id').val() > 0 )
            submitEnabled();    
        else
            submitDisabled();
    }
}

function rimCheck()
{
    if ( ( $('#srednica_id2').val() > 0 ) && ( $('#producent_id2').val() != '' ) )
        submitEnabled( '2' );   
    else
        submitDisabled( '2' );
}

function removeOptions( target )
{
    /*for ( i = $( target ).length - 1; i >= 0; i-- )
    {
        $( target ).remove(i);
    }*/
    $( '#' + target ).html('<option value="">-- wybierz --</option>');
    $( '#' + target ).attr('disabled', 'disabled'); 
}

function addOptions( source, target, type )
{
    if (type == 'scrub')
    {
      target2 = target + 'scrub';
    }
    else
    {
      target2 = target;
    }
    id = $('#'+source+'_id').val();

    if ( id > 0 )
    {
        // alert( '/ajax/rimparam/type/'+type+'/selected_param/'+target+'/id/' + id );
        $( '#'+target2+'_id').load( '/ajax/rimparam/type/'+type+'/selected_param/'+target+'/id/' + id );
        $( '#'+target2+'_id' ).attr('disabled','');
    }
}

function submitDisabled( suffix )
{
    if ( !suffix )
        suffix = '';
    $( '#szukaj_felgi'+suffix ).attr('disabled', 'disabled');
    $( '#szukaj_felgi'+suffix ).attr( 'class', 'szukaj_disabled' );
}

function submitEnabled( suffix )
{
    if ( !suffix )
        suffix = '';
    $( '#szukaj_felgi'+suffix ).attr('disabled', '');
    $( '#szukaj_felgi'+suffix ).attr( 'class', 'szukaj_enabled' );
}

/* konfigruator END */

/* promocje w zakladkach START */

var promo_curr = new Array();
promo_curr['opony'] = 1;
promo_curr['felgi'] = 1;
promo_curr['bagazniki'] = 1;
promo_curr['lancuchy'] = 1;
promo_curr['serwis'] = 1;
promo_curr['inwest'] = 1;

function promo_page(id, page)
{
  $('.promo_link_' + id).removeClass('active');
  $('#promo_' + id + '_' + page).addClass('active');
  $('.bm_' + id).hide();
  $('#bm_' + id + '_' + page).show();
  promo_curr[id] = page;
}

function promo_page_left(id)
{
  if (promo_curr[id] > 1)
  {
    promo_page(id, promo_curr[id] - 1);
  }
}

function promo_page_right(id)
{
  if ($('#bm_' + id + '_' + (promo_curr[id] + 1)).length)
  {
    promo_page(id, promo_curr[id] + 1);
  }
}

/* promocje w zakladkach END */
