Ext.namespace('mdc.qtyselect')
$(function(){var $selectBox,$selectContainer,$list,$listItems,$qtyTextBox;mdc.qtyselect.init=function(){$selectBox=$('.qty-select');$selectContainer=$('div.qty-select-container');$list=$('.qty-select-options');$listItems=$list.children('li');$qtyTextBox=$('.skuRowInputCheck');var productSelectorsEnabled=$('#productFamilySelectors').length;$selectContainer.off('click');$selectContainer.on('click',function(e){e.stopPropagation();$('div.qty-select-container.active').not(this).each(function(){$(this).removeClass('active').next($list).hide();});$(this).toggleClass('active').next($list).toggle();var scrollTop=$(window).scrollTop();var topOffset=$(this).closest($selectBox).offset().top;var relativeOffset=topOffset-scrollTop;var windowHeight=$(window).height();if(relativeOffset>windowHeight/1.5){$($list).addClass("options-reverse");}
else{$($list).removeClass("options-reverse");}});$listItems.off('click');$listItems.on('click',function(e){e.stopPropagation();var qtyId=$(this).attr('data-qtyid');var chkId=$(this).attr('data-checkid');var personalMessageAccepted=$('#'+chkId).attr('data-isaccountlinkeddatalayer');var acctpersMsgDataLayer="";if($('#'+chkId).attr('data-acctpersmsgdatalayer')==='undefined'){acctpersMsgDataLayer=$('#'+chkId).attr('data-acctpersmsgdatalayer').trim();}
var productName=$(this).closest('tr').find('*[data-skudescriptiondatalayer="skudescriptiondatalayer"]').text().trim();var accountPersonalizationMsg=(personalMessageAccepted==="true")?acctpersMsgDataLayer:"";$(this).closest($selectBox).find($selectContainer).text($(this).text()).removeClass('active');$('#'+qtyId).val($(this).attr('data-value')).trigger('keydown').trigger('blur');$('#'+chkId).prop('checked',true).attr('skuqty',$(this).attr('data-value'));$list.hide();if(productSelectorsEnabled>0){mdc.pages.productdetail.pushToLocalDataOnSelect(this,$selectBox,productName,accountPersonalizationMsg);}
if($(this).is($(this).closest($list).children('li').last())){$('#'+qtyId).removeClass('mdcHidden').focus();$(this).closest($selectBox).hide();}});$qtyTextBox.on('blur',function(){var qtySelector=$('#'+$(this).attr('data-qtyselectid'));var rowCheckboxId=$('#'+$(this).attr('data-qtyselectid').replace('qty_select','Row_Checkbox'));var minQty=Number($(this).first().attr('data-minqty'));var enteredQty=$(this).val();var skuNumber=$(rowCheckboxId).attr('skunumber');var personalMessageAccepted=$(rowCheckboxId).attr('data-isaccountlinkeddatalayer');var acctpersMsgDataLayer="";if($(rowCheckboxId).attr('data-acctpersmsgdatalayer')==='undefined'){acctpersMsgDataLayer=$(rowCheckboxId).attr('data-acctpersmsgdatalayer').trim();}
var accountPersonalizationMsg=(personalMessageAccepted==="true")?acctpersMsgDataLayer:"";var productName=$(rowCheckboxId).closest('tr').find('*[data-skudescriptiondatalayer="skudescriptiondatalayer"]').text().trim();if(productSelectorsEnabled>0){mdc.pages.productdetail.pushToLocalDataOnBlur(skuNumber,enteredQty,productName,accountPersonalizationMsg);}
var booleanForEnteredQuantity=enteredQty=='0'||enteredQty==''||enteredQty==null;if(booleanForEnteredQuantity){if(rowCheckboxId.length!==0&&enteredQty==''){mdc.qtyselect.reset(qtySelector,true);}else{setTimeout(function(){mdc.qtyselect.reset(qtySelector,true)},100)}}
if(booleanForEnteredQuantity||enteredQty>minQty){$(this).removeClass('minQtyRedBorder');}});$(document).click(function(){if($selectContainer!==undefined&&$selectContainer.length>0){$selectContainer.removeClass('active');$list.hide();}});$('body').on('click','.gritter-close',function(){$(this).closest('.gritter-item-error').remove();})}
mdc.qtyselect.resetVal=function(selectEl){var resetQtyVal=selectEl.find($list).children('li').first().attr('data-value');var materialNumber=$(selectEl).closest('tr').find($('.medGridViewMaterial a')).text().trim();var formName=$(selectEl).attr('data-formname');var qtyId=$('#'+selectEl.attr('data-qtyid'));materialNumber=mdc.pages.cart.getMaterialNumber(formName,qtyId,materialNumber);selectEl.find($selectContainer).text(resetQtyVal);$('#'+selectEl.attr('data-qtyid')).val(resetQtyVal);mdc.pages.order.entry.resetErrorMessages(materialNumber);if((!$(qtyId).hasClass('minQtyRedBorder')||$(qtyId).text().trim()==='')&&$(qtyId).attr('data-tooltipcreated')==='true'){$(qtyId).tooltip('destroy');$(qtyId).attr('data-tooltipcreated','false');}
if($('#productFamilySelectors').length>0){mdc.pages.productdetail.removeSkuFromLocalData(materialNumber);}}
mdc.qtyselect.reset=function(selectEl,hideTxtBox){var targetId=selectEl.attr('data-target');var qtyId=$('#'+selectEl.attr('data-qtyid'));mdc.qtyselect.resetVal(selectEl);selectEl.show();if(hideTxtBox){qtyId.addClass('mdcHidden').removeClass('minQtyRedBorder');selectEl.focus();}
if(qtyId.attr('id').indexOf('product')!==-1&&targetId!=='skuDetails'){qtyId.val('');}
if(targetId==="skuDetails"||targetId==="crossRef"){var $qtyBox=$('#'+selectEl.attr('data-qtyid'));$qtyBox.val($qtyBox.attr('data-minqty'));}}
mdc.qtyselect.resetAll=function(hideTxtBox){if($selectBox!==undefined&&$selectBox.length>0){$selectBox.each(function(){var qtyId=$('#'+$(this).attr('data-qtyid'));mdc.qtyselect.resetVal($(this));$(this).show();if(hideTxtBox){qtyId.addClass('mdcHidden').removeClass('minQtyRedBorder');}
if(qtyId.attr('id')!==undefined){if(qtyId.attr('id').indexOf('product')!==-1&&$(this).attr('data-target')!=='skuDetails'){qtyId.val('');}
$('input[type="checkbox"]:visible').each(function(){$(this).attr('checked',false)})}});$('.gritter-item-error').find('.gritter-close').click();}}});