
function $go(url){window.location.href = url;}

/*--------------------------------------------------------------------------------------------------------------------------------*/

function $save(id){
  if ( $(id) ) $(id).submit();
  return false;
  }	

/*--------------------------------------------------------------------------------------------------------------------------------*/

function menu (obj){
  var li = $(obj).getParent();
  var ul = li.getElement('ul');  
  ul.toggleClass('hide');  
  if ( li.hasClass('active' ) )
    li.removeClass('active')
  else if ( !ul.hasClass('hide') ) 
    li.addClass('active');
  }
  
/*--------------------------------------------------------------------------------------------------------------------------------*/
  
function swich_order_row ( obj ){
  var obj = $(obj);
  var className = obj.get('name') ;
  if( className ){
    if ( obj.get('value') )
      $$('.'+className).removeClass('hide');
    else
      $$('.'+className).addClass('hide');
    }
  }   

/*--------------------------------------------------------------------------------------------------------------------------------*/
  
function clear_input( input ){
  var input = $(input);
  if ( input ){
    input.addEvent('focus',function(){  if ( input.get('title') == input.get('value') ) input.set('value',''); });
    input.addEvent('blur',function(){ if ( input.get('value') == '' ) input.set('value', input.get('title') ); });
    }
  }

/*--------------------------------------------------------------------------------------------------------------------------------*/  

function add_comment(sub){
  var comment_form = $('comment_form');
  new Element('input',{'name':'sub','value':sub,'type':'hidden'}).inject(comment_form);
  comment_form.submit();  
  }

/*--------------------------------------------------------------------------------------------------------------------------------*/

function show_comment_form(obj){
  $('comment_form').removeClass('hide');
  obj.setStyle('display','none'); 
  }

/*--------------------------------------------------------------------------------------------------------------------------------*/

function add_to_basket(prod_basket){
  var prod_basket = $(prod_basket);
  if ( prod_basket ){
    var valid = true;
    var choose_options = prod_basket.getElements('.choose_options');
    if ( choose_options ){
      choose_options.each(function(el){
        if ( el.get('value') == '' && valid ){
          valid = false;
          alert('Wybierz '+el.get('title')+'.' );      
          el.focus();          
          }
        });
      }
    if ( valid ){
      prod_basket.submit();
      }      
    }  
  }

/*--------------------------------------------------------------------------------------------------------------------------------*/

window.addEvent('domready', function() {
  clear_input('search_string');
  var forms = document.getElements('form');
  forms.addEvent('submit',function(e){
    //new Event(e).stop();
    this.getElements('input').each(function(el){      
      var title = el.get('title');      
      if ( title && title == el.get('value') ){ 
        el.set('value','');
        }
      });
    //this.submit();
    });
});

/*--------------------------------------------------------------------------------------------------------------------------------*/
  
window.addEvent('domready', function() {
  var dodajdokoszyka = $('dodajdokoszyka') ;  
  if ( dodajdokoszyka ){
    dodajdokoszyka.addEvent('click',function(e){
      new Event(e).stop();
      $('order_add_form').submit();
      });
    }
});

/*--------------------------------------------------------------------------------------------------------------------------------*/
 
function change_sub( obj ){
  var obj = $(obj);
  if ( obj ){
    value = obj.get('value');
    var group_list = $('group_list');
    if ( group_list ){
      var sub_group_list = group_list.getElements('div.sub_group');
      if ( sub_group_list ){
        sub_group_list.addClass('hide'); 
        sub_group_list.getElements('select').each(function(el){el.set('name','');});
        }
      var active_sub = group_list.getElements('#sub_group_'+value);
      active_sub.removeClass('hide');
      active_sub.getElement('select').set('name','c2');
      }
    }
  }

/*--------------------------------------------------------------------------------------------------------------------------------*/
 
window.addEvent('domready',function(){
  var product_list = $('product_list');
  if ( product_list ){
    $$('.order_weight').addEvent('keyup',function(e){
      var pid = $(this).get('id').replace('order_weight_','');
      weight = $(this).get('value').replace(',','.').toFloat();
      var product_weight = $('product_weight_'+pid).get('value').replace(',','.').toFloat();
      var quantity = product_weight ? ( weight / product_weight ).round(2) : 1 ;  
      $('order_quantity_'+pid).set('value',quantity);
      });
    
    $$('.order_quantity').addEvent('keyup',function(e){
      var pid = $(this).get('id').replace('order_quantity_','');
      quantity = $(this).get('value').replace(',','.').toFloat();
      var product_weight = $('product_weight_'+pid).get('value').replace(',','.').toFloat();
      var weight = ( product_weight * quantity ).round(2);
      $('order_weight_'+pid).set('value',(weight+' kg').replace('.',','));
      });    
    }
  }); 
 

