function fn_buy_together_extended_get_price_schema(chain_id){var $=Tygh.$;var result={};var prices={};var total_price=0;elms=$('div#content_tab_products_'+chain_id);$('.cm-chain-'+chain_id,elms).each(function(){var elm_id=$(this).val();if(elm_id!='{bt_id}'){prices[elm_id]={};prices[elm_id]['amount']=$('[name*=amount]',$(this).parent().parent()).val();if(!isNaN(parseInt(prices[elm_id]['amount']))){prices[elm_id]['amount']=parseInt(prices[elm_id]['amount']);}else{prices[elm_id]['amount']=0;}prices[elm_id]['price']=parseFloat($('#item_price_bt_'+chain_id+'_'+elm_id,elms).val());prices[elm_id]['modifier']=parseFloat($('#item_modifier_bt_'+chain_id+'_'+elm_id,elms).val());if(isNaN(prices[elm_id]['modifier'])){prices[elm_id]['modifier']=0;}prices[elm_id]['modifier_type']=$('#item_modifier_type_bt_'+chain_id+'_'+elm_id,elms).val();total_price+=prices[elm_id]['price']*prices[elm_id]['amount'];}});result['price_schema']=prices;result['total_price']=total_price;return result;}function fn_buy_together_extended_apply_discount(chain_id){var $=Tygh.$;var global_discount=0;elms=$('div#content_tab_products_'+chain_id);global_discount=parseFloat($('#elm_buy_together_ext_global_discount_'+chain_id,elms).val());console.debug(global_discount);if(isNaN(global_discount)){return false;}var prices={};var total_price=0;var discounted_price=0;price_schema=fn_buy_together_extended_get_price_schema(chain_id);prices=price_schema['price_schema'];total_price=price_schema['total_price'];if(global_discount>total_price){global_discount=total_price;$('#elm_buy_together_ext_global_discount_'+chain_id,elms).val(total_price);}for(i in prices){discount=prices[i]['price']/total_price*global_discount;discount=discount.toFixed(2);item_price=prices[i]['price']-discount;item_price=item_price.toFixed(2);$('#item_modifier_bt_'+chain_id+'_'+i,elms).val(discount);$('#item_modifier_type_bt_'+chain_id+'_'+i,elms).val('by_fixed');$('[id*=item_display_price_bt_'+chain_id+'_'+i+'_]',elms).text(prices[i]['price'].toFixed(2));$('[id*=item_discounted_price_bt_'+chain_id+'_'+i+'_]',elms).text(item_price);discounted_price+=item_price*prices[i]['amount'];}$('[id*=total_price_'+chain_id+']',elms).text(total_price.toFixed(2));$('[id*=price_for_all_'+chain_id+']',elms).text(discounted_price.toFixed(2));}function fn_buy_together_extended_recalculate(chain_id){var $=Tygh.$;var prices={};var total_price=0;var discounted_price=0;elms=$('div#content_tab_products_'+chain_id);price_schema=fn_buy_together_extended_get_price_schema(chain_id);prices=price_schema['price_schema'];total_price=price_schema['total_price'];for(i in prices){switch(prices[i]['modifier_type']){case'to_fixed':item_price=prices[i]['modifier'];break;case'by_fixed':item_price=prices[i]['price']-prices[i]['modifier'];break;case'to_percentage':item_price=(prices[i]['modifier']/100)*prices[i]['price'];break;case'by_percentage':item_price=prices[i]['price']-(prices[i]['modifier']/100)*prices[i]['price'];break;default:item_price=prices[i]['price'];}if(item_price<0){item_price=0;}item_price=item_price.toFixed(2);discounted_price+=item_price*prices[i]['amount'];$('[id*=item_display_price_bt_'+chain_id+'_'+i+'_]',elms).text(prices[i]['price'].toFixed(2));$('[id*=item_discounted_price_bt_'+chain_id+'_'+i+'_]',elms).text(item_price);}$('[id*=price_for_all_'+chain_id+']',elms).text(discounted_price.toFixed(2));$('[id*=total_price_'+chain_id+']',elms).text(total_price.toFixed(2));$('#elm_buy_together_ext_global_discount_'+chain_id,elms).val('');}function fn_buy_together_extended_share_discount(evt,chain_id){if(evt.keyCode){code=evt.keyCode;}else if(evt.which){code=evt.which;}if(code==13){fn_buy_together_extended_apply_discount(chain_id);}return false;}function fn_buy_together_extended_clean_checkbox_on_product_list(){$('input[id^="checkbox_id_"]:checked').each(function(index,element){$(element).attr('checked',false);});}function fn_buy_together_extended_save_update_form(chain_id){var chains_category_update_data={};$('#buy_together_ext_update_chains_category'+chain_id+' input, select, textarea').not('.cm-clone input').not('.cm-js-item>:hidden').each(function(index,element){if($(element).attr('name')=='item_data[unlimited]'||$(element).attr('name')=='item_data[status]'){if($(element).is(':checked')){chains_category_update_data[$(element).attr('name')]=$(element).val();}}else{chains_category_update_data[$(element).attr('name')]=$(element).val();}chains_category_update_data['chain_update']=true;});if($('#elm_buy_together_ext_promotions_'+chain_id+':checked').length!=0){chains_category_update_data[$('#elm_buy_together_ext_promotions_'+chain_id).attr('name')]='Y';}else{chains_category_update_data[$('#elm_buy_together_ext_promotions_'+chain_id).attr('name')]='N';}if($("div#content_tab_products_"+chain_id+" tr[id^='objects_']").length==0||!$("input[id='elm_buy_together_ext_name_"+chain_id+"']").val()){alert(Tygh.tr('buy_together_ext_comb_cant_saved'));return false;}$.ceAjax('request',fn_url('buy_together_extended_categories.update'),{method:'post',data:chains_category_update_data,callback:function(data){fn_buy_together_extended_clean_checkbox_on_product_list();}});}function fn_buy_together_extended_unlimited(id){if(!id)id='';if($("#elm_buy_together_ext_unlimited_"+id).is(':checked')){$('#elm_buy_together_ext_avail_from_'+id).attr('disabled','disabled');$('#elm_buy_together_ext_avail_till_'+id).attr('disabled','disabled');}else{$('#elm_buy_together_ext_avail_from_'+id).removeAttr('disabled');$('#elm_buy_together_ext_avail_till_'+id).removeAttr('disabled');}}function fn_buy_together_extended_save_adding_form(){var chains_category_adding_data={};$('#buy_together_ext_add_new_chains_category input, #buy_together_ext_add_new_chains_category select, #buy_together_ext_add_new_chains_category textarea').not('.cm-clone input').each(function(index,element){if($(element).attr('name')=='item_data[unlimited]'||$(element).attr('name')=='item_data[status]'){if($(element).is(':checked')){chains_category_adding_data[$(element).attr('name')]=$(element).val();}}else{chains_category_adding_data[$(element).attr('name')]=$(element).val();}chains_category_adding_data['chain_update']=true;});if($('#elm_buy_together_ext_promotions_:checked').length!=0){chains_category_adding_data[$('#elm_buy_together_ext_promotions_').attr('name')]='Y';}else{chains_category_adding_data[$('#elm_buy_together_ext_promotions_').attr('name')]='N';}if($("div#content_tab_products_ tr[id^='objects_']").length==0||!$("input[id='elm_buy_together_ext_name_']").val()){alert(Tygh.tr('buy_together_ext_comb_cant_saved'));return false;}$.ceAjax('request',fn_url('buy_together_extended_categories.update'),{method:'post',data:chains_category_adding_data,callback:function(data){fn_buy_together_extended_clean_checkbox_on_product_list();location.reload();}});}(function(_,$){$.ceEvent('on','ce.picker_add_js_item',function(data){if(data['var_prefix']=='p'){price=parseFloat(data.item_id.price);if(isNaN(price)){price=0;}data['append_obj_content']=data['append_obj_content'].str_replace('{bt_id}',data['item_id']['product_id']).str_replace('{price}',price);var content=$('