$(document).ready(function() {
    groupPriceAdded = "No";
     $(document).on('click','.busAddGroupPrice',function(){
        var productOptionCount =   $(this).attr('productoptioncount');
        addGroupPrice(productOptionCount);
    });
    
    $(document).on('click','.deleteGroupPrice',function(){
        var groupIncr       =   $(this).attr('groupIncr');
        var productOptionCount =   $(this).attr('productoptioncount');
        deleteGroupPrice(groupIncr,productOptionCount);
    });
    
    $(document).on('submit','#productBusOptForm',function(){
        var validate = sellerSubmit();
        if(!validate){
                return false;
        }else{
                return true;
        }
    });
    
    $(document).on("click",".sellThisOption",function(){
        checkSellThisOption($(this));
    });
    
    $(".sellThisOption").each(function(){
        checkSellThisOption($(this))
    });
    
    
});

function checkSellThisOption(thisVal){
    var thisID      =       thisVal.attr("id");
    var expThisID   =       thisID.split('_');
    var optionIncr  =       expThisID[1];
    if(thisVal.prop("checked")==true){
        $("#business_option_sec_"+optionIncr).removeClass("hide");
        $("#sellThisOptionCheck_"+optionIncr).val("1");
    }else{
        $("#business_option_sec_"+optionIncr).addClass("hide");
        $("#sellThisOptionCheck_"+optionIncr).val("0");
    }
}

function addGroupPrice(productOptionCount){
    if(groupPriceAdded=="No"){
        groupPriceAdded     =   "Yes";
        var currGroupCount  = $('.groupContainer_'+productOptionCount).length;
        var groupIncrCount  = parseInt(currGroupCount)+1;
           $.ajax({
                type: "POST",
                url: GLOBAL_BASE_PATH+"/product/ajaxAddGroupPrice/",
                data: "groupIncr="+groupIncrCount+"&productOptionCount="+productOptionCount,
                success: function(msg){
                    if(msg){
                        $("#business_group_price_container_"+productOptionCount).append(msg);
                        $('#groupIncrCount_'+productOptionCount).val(groupIncrCount);
                        customerGroupFilter(productOptionCount);
                        groupPriceAdded     =   "No";
                    }
                }
            });
    }
}

function deleteGroupPrice(groupIncr,productOptionCount){
    jConfirm(msgDeleteItem, 'Alert', function (r) {
        if (r == true) {
            $('#groupContainer_'+productOptionCount+'_'+groupIncr).remove();
            checkGroupAvail(groupIncr,productOptionCount);
        }
    });
}

function customerGroupFilter(productOptionCount){
    var groupIncr   =   $('#groupIncrCount_'+productOptionCount).val();
    checkGroupAvail(groupIncr,productOptionCount);
	
}

function checkGroupAvail(groupIncr,productOptionCount){
    if($("#productOptionsGroup_"+productOptionCount+"_businessOptionsGroup_group_price_"+groupIncr+"_userGroupID > option").length==1){
        $('#grpPrice_'+productOptionCount+'_'+groupIncr).addClass("hide");
                }else{
        $('#grpPrice_'+productOptionCount+'_'+groupIncr).removeClass("hide");
                }
}

function sellerSubmit(){
    var validate=true;
    
    var checkGroupPrice     =   groupPriceCheck();
    var checkOptionSelected =   optionSelectedCheck();
    if(checkGroupPrice>0){
        validate = false;
    }
    if(checkOptionSelected>0){
        validate = false;
    }
    return validate;
        
}

function optionSelectedCheck(){
    var optionCount         =   $("#hiddenOptionCount").val();
    var sellOptionCount     =   $(".sellThisOption:checked").length;
    if(optionCount>1 && sellOptionCount<=0){
        $('#common_validate_message').removeClass('hide').html(errNoDetailsForMerchants);
        return 1;
    }else{
        return 0;
    }
}

function groupPriceCheck(){
    var groupPriceValidate  =    0;
    $('.option-values').each(function(){
        var groupPriceDuplicateValidate  =    0;
        var productoptioncount  =    $(this).attr('productoptioncount');
        $(this).find('.option_group_price_sp_price').each(function(){
            var groupPriceID    =    $(this).attr('id');
            var priceIDSplit    =    groupPriceID.split('_');
            var groupIncr       =    priceIDSplit[2];
            var selValue        =    $(this).val();
            if(selValue<=0 || selValue==""){
                groupPriceValidate = groupPriceValidate+1;
                $('#validate_group_price_'+productoptioncount+'_'+groupIncr).removeClass('hide').html(errRequired);
            }else{
                $('#validate_group_price_'+productoptioncount+'_'+groupIncr).addClass('hide').html("");
                
                var validPrice  =  /^\d{0,4}(\.\d{0,2})?$/.test(selValue);
                if(validPrice==false){
                    groupPriceValidate = groupPriceValidate+1;
                    $('#validate_group_price_'+productoptioncount+'_'+groupIncr).removeClass('hide').html(errValidNumber);
                }else{
                    $('#validate_group_price_'+productoptioncount+'_'+groupIncr).addClass('hide').html("");
            }
        }
    });

        var duplicationEle = [];
        $(this).find('.option_group_price_user_group').each(function(){
            var thisValue = $(this).val();
            if($.in_array(thisValue,duplicationEle)){
                groupPriceDuplicateValidate = groupPriceDuplicateValidate+1;
                groupPriceValidate = groupPriceValidate+1;
            }else{
                duplicationEle.push(thisValue);
            }
});

        if(groupPriceDuplicateValidate>0){
            $('#validate_usergroup_'+productoptioncount).removeClass('hide').html(errDuplicateCombination);
        }else{
            $('#validate_usergroup_'+productoptioncount).addClass('hide').html("");
                }
        
    });
    return groupPriceValidate;
}
