//ПЕРВАЯ ЧАСТЬ
$(function() {
var update12 = function() {
var val = +this.value;
var showFirst = [4].indexOf(val) >= 0;
var showSecond = [3,4,5,6,8].indexOf(val) >= 0;
$('#f_lr1gr2').toggle(showFirst);
$('#f_lr1grt').toggle(showSecond);
!showFirst && $('#f_lr1gr2 select').val(1).change();
!showSecond && $('#f_lr1grt select').val(1).change();
};
var update44 = function() {
var val = +this.value;
var showFirst = [8].indexOf(val) >= 0;
$('#f_lr1gr2el1').toggle(showFirst);
$('#f_lr1gr2el2').toggle(showFirst);
$('#f_lr1gr2el3').toggle(showFirst);
$('#f_lr1gr2el4').toggle(showFirst);
$('#f_lr1gr2el5').toggle(showFirst);
!showFirst && $('#f_lr1gr2el1 select').val(1).change();
!showFirst && $('#f_lr1gr2el2 select').val(1).change();
!showFirst && $('#f_lr1gr2el3 select').val(1).change();
!showFirst && $('#f_lr1gr2el4 select').val(1).change();
!showFirst && $('#f_lr1gr2el5 select').val(1).change();
};
$('#lr1rt').change(update12).change();
$('#lr1rt').change(update44).change();
});
//ВТОРАЯ ЧАСТЬ
$(function() {
var update112 = function() {
var val = +this.value;
var showFirst = [4].indexOf(val) >= 0;
var showSecond = [3,4,5,6,8].indexOf(val) >= 0;
$('#f_sp1gr2').toggle(showFirst);
$('#f_sp1grt').toggle(showSecond);
!showFirst && $('#f_sp1gr2 select').val(1).change();
!showSecond && $('#f_sp1grt select').val(1).change();
};
var update144 = function() {
var val = +this.value;
var showFirst = [8].indexOf(val) >= 0;
$('#f_sp1gr2el1').toggle(showFirst);
$('#f_sp1gr2el2').toggle(showFirst);
$('#f_sp1gr2el3').toggle(showFirst);
$('#f_sp1gr2el4').toggle(showFirst);
$('#f_sp1gr2el5').toggle(showFirst);
!showFirst && $('#f_sp1gr2el1 select').val(1).change();
!showFirst && $('#f_sp1gr2el2 select').val(1).change();
!showFirst && $('#f_sp1gr2el3 select').val(1).change();
!showFirst && $('#f_sp1gr2el4 select').val(1).change();
!showFirst && $('#f_sp1gr2el5 select').val(1).change();
};
$('#sp1rt').change(update112).change();
$('#sp1rt').change(update144).change();
});
$('#f_sp1gr2el1').toggle(showFirst);
$('#f_sp1gr2el2').toggle(showFirst);
$('#f_sp1gr2el3').toggle(showFirst);
$('#f_sp1gr2el4').toggle(showFirst);
$('#f_sp1gr2el5').toggle(showFirst);
!showFirst && $('#f_sp1gr2el1 select').val(1).change();
!showFirst && $('#f_sp1gr2el2 select').val(1).change();
!showFirst && $('#f_sp1gr2el3 select').val(1).change();
!showFirst && $('#f_sp1gr2el4 select').val(1).change();
!showFirst && $('#f_sp1gr2el5 select').val(1).change();
var $elems = $('#f_sp1gr2el1, #f_sp1gr2el2 #f_sp1gr2el3, #f_sp1gr2el4, #f_sp1gr2el5');
$elems.toggle(showFirst);
!showFirst && $elems.find('select').val(1).change();
function Update(idFirst, idSecond,idThird,someAnotherNumberOfId,howMuchYouWant) {
update1 = function () {
var val = +this.value;
var showFirst = [4].indexOf(val) >= 0;
var showSecond = [3, 4, 5, 6, 8].indexOf(val) >= 0;
$(idFirst).toggle(showFirst);
$(idSecond).toggle(showSecond);
!showFirst && $(idFirst + ' select').val(1).change();
!showSecond && $(idSecond + ' select').val(1).change();
};
update2 = function(){
var iterations = 5;
if(howMuchYouWant){
iterations = howMuchYouWant;
}
var idS=[];
for(var i=0;i<iterations;i++){ //Создаем массив ИДшников
idS.push(idFirst+'el'+someAnotherNumberOfId);
someAnotherNumberOfId+=1;
}
console.log(idS);
var val = +this.value;
var showFirst = [8].indexOf(val) >= 0;
for(var i=0;i<iterations;i++){ //Переключаем на каждом .toggle(showFirst)
$(idS[i]).toggle(showFirst);
!showFirst && $(idS+' select').val(1).change();
}
}
$(idThird).change(update1).change();
$(idThird).change(update2).change();
}
var firstUpdate= new Update('#f_lr1gr2','#f_lr1grt','#lr1rt',1);
var secondUpdate= new Update('#f_lr1gr2','#f_lr1grt','#sp1rt',1);
firstUpdate();
secondUpdate();