﻿var d = document;

selectedPipe = 0;

var prices = [
	[1889.18, 1679, 1000, 1244.59, 488.918, 1112.74, 21, 23.72, 17.94, [300, 400, 1000, 1800], 0.5, 'Vitogas 100-F 35 kW', 'VK INT 324/1-5 31,5 kW', 'Vitotronic 100 KC3', 'Vitocell 100-V тип CVA, 200 л.'],
	[2266.78, 2464, 1200, 2333.39, 946.678, 1112.7, 21, 23.72, 17.94, [600, 800, 2000, 3600], 0.35, 'Vitogas 100-F 48 kW', 'VK INT 484/1-5 48,9 kW', 'Vitotronic 100 KC3', 'Vitocell 100-V тип CVA, 200 л.'],
	[6118.3, 5577, 2000, 4959.15, 1711.83, 1473.82, 40, 23.72, 17.94, [900, 1200, 3000, 5400], 0.3, 'Vitogas 100-F 108 kW', 'VK INT 1004/9 99 kW', 'Vitotronic 100 KC4', 'Vitocell 100-V тип CVA, 300 л.'],
	[7323.08, 8905, 5530, 7926.54, 2785.308, 1473.8, 54, 23.72, 17.94, [1500, 2000, 5000, 9000], 0.3, 'Vitogas 100-F 140 kW', 'VK INT 1604/9 157 kW', 'Vitotronic 100 KC4', 'Vitocell 100-V тип CVA, 300 л.'],
	[12236.6, 10000, 7530, 11883.3, 3976.66, 1473.8, 63, 23.72, 17.94, [2000, 3000, 7000, 12000], 0.25, 'Vitogas 100-F 108 kW - 2шт', 'VK INT 1004/9 99 kW - 2шт', 'Vitotronic 100 KC4+<br/>Vitotronic 300-K', 'Vitocell 100-V тип CVA, 300 л.']
	]; // Цены

jQuery(document).ready(function(){
	$(".calculator input[type='checkbox'], .calculator input[type='radio']").click(function() {
		Calculate();
	});
	
	$("#square").change(function() {
		znachenie_TeplihPolov = parseInt(jQuery('#squareAreaFloors').attr('value'));
		znachenie_ploschadiDoma = parseInt(jQuery('#square').attr('value'));

		if ( znachenie_TeplihPolov > znachenie_ploschadiDoma ) {
			jQuery('#squareAreaFloors').attr('value', znachenie_ploschadiDoma);
		}
		
		Calculate();
	});
	
	$("#squareAreaFloors").keyup(function() {
		znachenie_TeplihPolov = parseInt(jQuery('#squareAreaFloors').attr('value'));
		znachenie_ploschadiDoma = parseInt(jQuery('#square').attr('value'));

		if ( znachenie_TeplihPolov > znachenie_ploschadiDoma ) {
			jQuery('#squareAreaFloors').attr('value', znachenie_ploschadiDoma);
		}

		Calculate();
	});
	
	
	$("#field1 input[type='radio']").click(function() {
			$('#field1 LABEL').removeClass('bold');
			$(this).parents('LABEL').addClass('bold');
	});
});

function Calculate() {
    var ind = GetSquare();
	if (ind == 5) {
		alert('Максимальная площадь дома, 2000 квадратных метров');
		return false;
	};
	if (ind == 6) {
		alert('Минимальная площадь дома, 100 квадратных метров');
		return false;
	};
	plozhadDoma = parseInt(get_by_id('square').value); // площадь дома
	radiatornoe_stoimost = prices[ind][6] * plozhadDoma; // стоимость радиаторного отопления
	
    var res = prices[ind][selectedPipe]; // 1 - 0 выбранный котел
    res += automatic_adjustment_of_boiler(ind);
    res += costs_heat_supply_and_exhaust_ventilation(ind);
    res += heated_pool(ind);
    res += boiler_hot_water_with_pumps(ind);
    res += radiator_heating(ind);
    res += square_area_floors(ind);
    res += automatic_warm_floors_in_zones(ind);
    res += price_pipes(ind);
    var tempres = res;
	
    res = res * prices[ind][10]; // Стоимость монтажных работ

    get_by_id('work_cost').innerHTML = Math.round(res);
    get_by_id('circ_pipes').innerHTML = price_pipes(ind);
	
	get_by_id('marka_i_tip_kotla').innerHTML = prices[ind][selectedPipe + 11]; // Модель котла
	get_by_id('model_kontrollera_ypr_kotla').innerHTML = prices[ind][selectedPipe + 13]; // Модель контроллера управления котла
	get_by_id('cena_kotla').innerHTML = prices[ind][selectedPipe]; // Цена котла
	
	get_by_id('model_boilera').innerHTML = prices[ind][14]; // Бойлер
	var cena_boiler_var = boiler_hot_water_with_pumps(ind); get_by_id('cena_boiler').innerHTML = cena_boiler_var; // Бойлер: цена
	
	var cena_avtoKotelnoi_var = automatic_adjustment_of_boiler(ind); get_by_id('cena_avtoKotelnoi').innerHTML = cena_avtoKotelnoi_var; // Автоматика котельной: цена
	
	get_by_id('cena_radiatori').innerHTML = (radiator_heating(ind) * 0.37).toFixed(2); // Радиаторы: цена
	get_by_id('cena_ovyazkaRadiatorov').innerHTML = (radiator_heating(ind) * 0.17).toFixed(2); // Обвязка радиаторов: цена
	get_by_id('cena_trybiFitingi').innerHTML = (radiator_heating(ind) * 0.46).toFixed(2); // Трубы и фитинги: цена

	var cena_tepliePoli_var = square_area_floors(ind); get_by_id('cena_tepliePoli').innerHTML = cena_tepliePoli_var.toFixed(2); // Тёплые полы: цена
	var cena_avtomatikaTeplogoPola_var = automatic_warm_floors_in_zones(ind); get_by_id('cena_avtomatikaTeplogoPola').innerHTML = cena_avtomatikaTeplogoPola_var.toFixed(2); // Автоматика тёплого пола: цена
	
	
	/* Показываем-скрываем цены */
	if (cena_boiler_var == 0) {jQuery('#calcOt_boiler_TR').hide();}
	else {jQuery('#calcOt_boiler_TR').show();};

	if (cena_avtoKotelnoi_var == 0) {jQuery('#calcOt_avtoKotelnoi_TR').hide();}
	else {jQuery('#calcOt_avtoKotelnoi_TR').show();};

	if (radiator_heating(ind) == 0) {jQuery('.calcOt_radiatori_TR').hide();}
	else {jQuery('.calcOt_radiatori_TR').show();};

	if (cena_tepliePoli_var == 0) {jQuery('#calcOt_tepliePoli_TR').hide();}
	else {jQuery('#calcOt_tepliePoli_TR').show();};

	if (cena_avtomatikaTeplogoPola_var == 0) {jQuery('#calcOt_avtomatikaTeplogoPola_TR').hide();}
	else {jQuery('#calcOt_avtomatikaTeplogoPola_TR').show();};
	
	jQuery('#model_nasosov').text('Grundfoss');
	jQuery('#calcOt_kotel_TD').show();
	
	
	/* Ссылки */
	if (selectedPipe == 0) {
		jQuery('#marka_i_tip_kotla').attr('href', 'http://eurosystema.ru/otoplenie/kotel_equipment/viessman/kitliotoplenia/napolnye/gaz_prip_cziz_gaz/Vitogas_100_F/index.php');
		jQuery('#model_kontrollera_ypr_kotla_wrap').show();
	};
	if (selectedPipe == 1) {
		jQuery('#marka_i_tip_kotla').attr('href', 'http://eurosystema.ru/otoplenie/kotel_equipment/vaillant/gaz_kotly_nagrev/napol_kotly_s_gorelkoy/atmoVIT/index.php');
		jQuery('#model_kontrollera_ypr_kotla_wrap').hide();
	};
	
    var allsum = tempres + res; // Всего
    get_by_id('allsum').innerHTML = Math.round(allsum);
}

function marka_i_tip_kotla(ind) {
    return prices[ind][selectedPipe + 11];
}

function automatic_adjustment_of_boiler(ind) {
    return get_by_id('automatic_adjustment_of_boiler' + (selectedPipe + 1)).checked ? prices[ind][2] : 0; // С автоматической регулировкой котельной
}

function costs_heat_supply_and_exhaust_ventilation(ind) {
    return get_by_id('costs_heat_supply_and_exhaust_ventilation' + (selectedPipe + 1)).checked ? prices[ind][3] : 0; // Расходы тепла на приточно-вытяжную вентиляцию
}

function heated_pool(ind) {
    return get_by_id('heated_pool' + (selectedPipe + 1)).checked ? prices[ind][4] : 0; // Подогрев бассейнов
}

function boiler_hot_water_with_pumps(ind) {
    return get_by_id('boiler_hot_water_with_pumps' + (selectedPipe + 1)).checked ? prices[ind][5] : 0; // Бойлер ГВС, с насосами
}

function radiator_heating(ind) {
    return get_by_id('radiator_heating' + (selectedPipe + 1)).checked ? radiatornoe_stoimost : 0; // Радиаторное отопление
}

function square_area_floors(ind) {
    return (parseInt(get_by_id('squareAreaFloors').value == '' ? '0' : get_by_id('squareAreaFloors').value) * prices[ind][7]); // Площадь теплых полов
}

function automatic_warm_floors_in_zones(ind) {
    return get_by_id('automatic_warm_floors_in_zones').checked ? (prices[ind][8] * parseInt(get_by_id('squareAreaFloors').value)) : 0; // Автоматическая регулировка теплых полов по зонам
}

function price_pipes(ind) {
    var level = 0;
    for (var i = 1; i <= 4; i++) {
        if (get_by_id('level' + i).checked) {
            level = i - 1;
            break;
        }
    }
    return prices[ind][9][level];
}

function nullvalues(i) {
    selectedPipe = i - 1;
    get_by_id('automatic_adjustment_of_boiler' + i).checked = false;
    get_by_id('costs_heat_supply_and_exhaust_ventilation' + i).checked = false;
    get_by_id('heated_pool' + i).checked = false;
    get_by_id('boiler_hot_water_with_pumps' + i).checked = false;
    get_by_id('radiator_heating' + i).checked = false;
    get_by_id('automatic_warm_floors_in_zones').checked = false;

	Calculate();
}

function apply_classes(tabIndex) {
    for (i = 1; i <= 5; i++) {
        get_by_id('t' + tabIndex + '_lobj' + i).className = 'ch-no-bold';
    }
    get_by_id('awfiz').className = 'ch-no-bold';
}

function GetSquare() {
    sq = 0;
    square = parseInt(get_by_id('square').value); // Площадь дома
    if (square >= 100 && square <= 300) {
        sq = 0;
    }
    else if (square >= 300 && square <= 500) {
        sq = 1;
    }
    else if (square >= 500 && square <= 1000) {
        sq = 2;
    }
    else if (square >= 1000 && square <= 1500) {
        sq = 3;
    }
    else if (square >= 1500 && square <= 2000) { 
        sq = 4;
    }
    else if (square > 2000) { 
        sq = 5;
    }
	else{
		sq = 6;
	}
    return sq;
}

function get_by_id(name) {
    if (d.all)
        return d.all[name];
    else if (d.getElementById)
        return d.getElementById(name);
}
