

function add(sel,v,n,pos){  
  var opt =new Option(n,v);
  document.getElementById(sel).options[pos] = opt;
  if(v==zaklad_id) document.getElementById(sel).options[pos].selected=true;
}



function reload(){   
  var rndval = new Date().getTime();
  document.getElementById('dle-captcha').innerHTML = '<img src="/kcaptcha/kcaptcha_view.php?f='+rndval+'" width="120" height="50" border="0" alt="" /><br /><a onclick="reload(); return false;" href="#">обновить код</a>';
}

var max_mess_len = 250;
function count_chars(){
   var mess_len_temp = document.getElementById("message").value;
   var mess_len_ = mess_len_temp.length;
   
   if (mess_len_ < max_mess_len){
     var raz = max_mess_len - mess_len_;
     document.getElementById("mess_len").innerHTML = '(осталось '+raz+' символов)';
   }
   else{
     document.getElementById("mess_len").innerHTML = '<i style="color:red;">(лимит исчерпан)</i>';
     document.getElementById("message").value = mess_len_temp.substr(0, max_mess_len);
   }
}

function frm_search(){
  document.getElementById('search').method='get';
  document.getElementById('search').action='/search.html';
  document.getElementById('search').submit();
}

function frm_recount(){
  document.getElementById('basket').method='post';
  document.getElementById('basket').action='/recount.html';
  document.getElementById('basket').submit();
}

function frm_order(){
  document.getElementById('basket').method='post';
  document.getElementById('basket').action='/order.html';
  document.getElementById('basket').submit();
}

function frm_sub(name){
  document.getElementById(name).submit();
}

function add_to_cart(){
  $(".addcart").click(function(){
  
    id = $(this).attr("it");
    sub = $(this).attr("sub");
    if(id > 0){
      $.getJSON('/ajax.php',{mode:'add', id:id, sub:sub},function(result){
        if(result.res!=''){
          $("#kor").html(result.res);
          $(".add_kor").html('<p>Товар уже в корзине</p>');
        }
      })
    }
  });
  
  
  $(".img_small_right img").click(function(){
    new_src_small = $(this).attr('src');
    new_src_big = $(this).parent().attr('href');
    
    x_new = $(this).attr('width');
    y_new = $(this).attr('height');
    
    old_src_small = $(".img_big img").attr('src');
    old_src_big = $(".img_big a").attr('href');
    
    x_old = $(".img_big img").attr('width');
    y_old = $(".img_big img").attr('height');
    
    $(this).attr({src:old_src_small, width:x_old, height:y_old});
    $(this).parent().attr({href:old_src_big});
    
    $(".img_big img").attr({src:new_src_small, width:x_new, height:y_new});
    $(".img_big a").attr({href:new_src_big});
    
    y_old_ = y_old+5;
    $(this).parent().parent().attr({style:'height:'+y_old_+'px;'})
    
    return false;
  });
  
  $(".img_small_bottom img").click(function(){
    new_src_small = $(this).attr('src');
    new_src_middle = $(this).attr('src2');
    new_src_big = $(this).parent().attr('href');
    
    old_src_small = $(".img_big img").attr('src2');
    old_src_middle = $(".img_big img").attr('src');
    old_src_big = $(".img_big a").attr('href');
    
    $(this).attr({src:old_src_small});
    $(this).attr({src2:old_src_middle});
    $(this).parent().attr({href:old_src_big});
    
    $(".img_big a.jqzoom img").attr({src:new_src_middle});
    $(".img_big a.jqzoom img").attr({src2:new_src_small});
    $(".img_big a.jqzoom").attr({href:new_src_big});
    
    return false;
  });
  
  $(".col").focus(function(e){valBefore = $(this).val();});
  $(".col").keyup(function(e){
    if ((( 48 <= e.which) && (e.which <= 57))||(( 96 <= e.which) && (e.which <= 105))||(e.which == 8)||(e.which == 39)||(e.which == 37)||(e.which == 46)||(e.which == 27)||(e.which == 17)||(e.which == 16)){
      thisKolvo = $(this).val();
      id = $(this).attr('iid');
      sub = $(this).attr('sub');
      cena1 = $(this).parent().parent().find(".cena1").text();
      cena2 = (cena1*thisKolvo);
      $(this).parent().parent().find('.cena2').html(cena2);
      $.getJSON('/ajax.php',{id:id, cnt:thisKolvo, mode:'rcart', sub:sub},function(data){
        if(data.total_summ > 0){
          $("#itogo").html(data.total_summ);
          if(data.total_summ > 0) $("#discont").html(data.diff);
          if(data.summ_percent > 0) $("#itogo_summ").html(data.summ_percent);
          $("#kor").html(data.res);
        }
      })

    }else{
        $(this).val(valBefore);
    }
  });
}


function select_brand(){
  var ajaxlink;
  var asd;
  $(".cat_select select").change(function(){
    ajaxlink = $(this).find("option:selected").attr("ajaxlink");
    window.location.href = ajaxlink;
  });
}

$(document).ready(function(){
  add_to_cart();
  select_brand();
}) 