function sel_period(id) {
	var form = document.getElementById('calculator');
	
	
	var element = form.elements;
	var checkbox = Array();
	var radio = Array();
	var input = Array();

	var sum = 0;
	var sum_install = 0;
	var sum_ab = 0;

	var j=0;
	var k=0;
	var l=0;

	for (i=0; i<element.length; i++){
		if (element[i].type=='radio')
		{
			radio[j] = element[i];
			j++;
		}
		if (element[i].type=='checkbox')
		{
			checkbox[k] = element[i];
			k++;
		}
		
		if (element[i].type=='hidden')
		{
			input[l] = element[i];
			l++;
		}
	}

	//стираем все ячейки
	for (i=0; i<checkbox.length; i++){
		for (j=0; j<radio.length; j++){
			td = document.getElementById('td_'+radio[j].value+'_'+checkbox[i].value);
			td.style.background = '#ECF2F9';

			td.style.fontWeight = 'normal';

			th = document.getElementById('td'+radio[j].value);
			th.style.background = '#ECF2F9';
		}
	}
	
	//выделяем столбец
	for (i=0; i<checkbox.length; i++){
		td = document.getElementById('td_'+id+'_'+checkbox[i].value);
		td.style.background = '#C8E9BC';
	}

	td = document.getElementById('td'+id);
	td.style.background = '#C8E9BC';

	//выделяем строку
	for (i=0; i<radio.length; i++){
		for (j=0; j<checkbox.length; j++){
			if (checkbox[j].checked) {
				td = document.getElementById('td_'+radio[i].value+'_'+checkbox[j].value);
				td.style.background = '#C8E9BC';
			}
		}
	}
	

	var count = 0;
	//выделяем ячейку
	for (i=0; i<checkbox.length; i++){
		price = document.getElementById('price_'+id+'_'+checkbox[i].value);
		price_connect = document.getElementById('price_connect_'+checkbox[i].value);

		if (checkbox[i].checked && price.value>0)
		{
			td = document.getElementById('td_'+id+'_'+checkbox[i].value);
			td.style.background = '#F7E0A6';
			td.style.fontWeight = 'bold';
			sum += parseInt(price.value);
			sum += parseInt(price_connect.value);

			sum_ab += parseInt(price.value);
			sum_install += parseInt(price_connect.value);

			count++;
		}
	}

	disc = document.getElementById('discount');
	disc_inst = document.getElementById('discount_install');
	if (discount[count])
	{
		//sum = sum - sum*(discount[count]/100);
		d = sum_ab*(discount[count]/100);
		sum_ab = sum_ab-d;
		disc.innerHTML = ',&nbsp;&nbsp;скидка '+addkop(d)+'&nbsp;('+discount[count]+'%)';
	} else {
		disc.innerHTML = '';
	}

	if (discount_inst[count])
	{
		//sum = sum - sum*(discount_inst[count]/100);
		d = sum_install*(discount_inst[count]/100);
		sum_install = sum_install-d;
		disc_inst.innerHTML = ',&nbsp;&nbsp;скидка: '+addkop(d)+'&nbsp;('+discount_inst[count]+'%)';
	} else {
		disc_inst.innerHTML = '';
	}

	sum = sum_install+sum_ab;

	rez = document.getElementById('sum');
	rez.innerHTML = addkop(sum);
	hidd = document.getElementById('sum1');
	hidd.value = sum;

	rez_install = document.getElementById('sum_install');
	rez_install.innerHTML = addkop(sum_install);
	hidd = document.getElementById('sum3');
	hidd.value = sum_install;
	
	rez_ab = document.getElementById('sum_ab');
	rez_ab.innerHTML = addkop(sum_ab);
	hidd = document.getElementById('sum2');
	hidd.value = sum_ab;
}

function sel_service(id) {
	var form = document.getElementById('calculator');
	
	
	var element = form.elements;
	var checkbox = Array();
	var radio = Array();
	var input = Array();

	var sum = 0;
	var sum_install = 0;
	var sum_ab = 0;

	var j=0;
	var k=0;
	var l=0;

	for (i=0; i<element.length; i++){
		if (element[i].type=='radio')
		{
			radio[j] = element[i];
			j++;
		}
		if (element[i].type=='checkbox')
		{
			checkbox[k] = element[i];
			k++;
		}
		
		if (element[i].type=='text')
		{
			input[l] = element[i];
			l++;
		}
	}

	//стираем все ячейки
	for (i=0; i<checkbox.length; i++){
		for (j=0; j<radio.length; j++){
			td = document.getElementById('td_'+radio[j].value+'_'+checkbox[i].value);
			td.style.background = '#ECF2F9';
			td.style.fontWeight = 'normal';
			tr = document.getElementById('tr'+checkbox[i].value);
			tr.style.background = '#ECF2F9';
		}
	}
	
	//выделяем строку
	for (i=0; i<radio.length; i++){
		for (j=0; j<checkbox.length; j++){
			if (checkbox[j].checked) {
				td = document.getElementById('td_'+radio[i].value+'_'+checkbox[j].value);
				td.style.background = '#C8E9BC';

				tr = document.getElementById('tr'+checkbox[j].value);
				tr.style.background = '#C8E9BC';
			}
		}
	}

	

	//выделяем столбец
	for (i=0; i<radio.length; i++){
		if (radio[i].checked) {
			for (j=0; j<checkbox.length; j++){
				td = document.getElementById('td_'+radio[i].value+'_'+checkbox[j].value);
				td.style.background = '#C8E9BC';
			}
		}
	}
	
	var count = 0;
	//выделяем ячейку
	for (i=0; i<checkbox.length; i++){
		for (j=0; j<radio.length; j++){
			price = document.getElementById('price_'+radio[j].value+'_'+checkbox[i].value);
			price_connect = document.getElementById('price_connect_'+checkbox[i].value);

			if (checkbox[i].checked && price.value>0 && radio[j].checked)
			{
				td = document.getElementById('td_'+radio[j].value+'_'+checkbox[i].value);
				td.style.background = '#F7E0A6';
				td.style.fontWeight = 'bold';
				sum += parseInt(price.value);
				sum += parseInt(price_connect.value);

				sum_ab += parseInt(price.value);
				sum_install += parseInt(price_connect.value);

				count++;
			}
		}
	}


	disc = document.getElementById('discount');
	disc_inst = document.getElementById('discount_install');
	if (discount[count])
	{
		//sum = sum - sum*(discount[count]/100);
		d = sum_ab*(discount[count]/100);
		sum_ab = sum_ab-d;
		disc.innerHTML = ',&nbsp;&nbsp;скидка '+addkop(d)+'&nbsp;('+discount[count]+'%)';
	} else {
		disc.innerHTML = '';
	}

	if (discount_inst[count])
	{
		//sum = sum - sum*(discount_inst[count]/100);
		d = sum_install*(discount_inst[count]/100);
		sum_install = sum_install-d;
		disc_inst.innerHTML = ',&nbsp;&nbsp;скидка: '+addkop(d)+'&nbsp;('+discount_inst[count]+'%)';
	} else {
		disc_inst.innerHTML = '';
	}

	sum = sum_install+sum_ab;

	rez = document.getElementById('sum');
	rez.innerHTML = addkop(sum);
	hidd = document.getElementById('sum1');
	hidd.value = sum;

	rez_install = document.getElementById('sum_install');
	rez_install.innerHTML = addkop(sum_install);
	hidd = document.getElementById('sum3');
	hidd.value = sum_install;
	
	rez_ab = document.getElementById('sum_ab');
	rez_ab.innerHTML = addkop(sum_ab);
	hidd = document.getElementById('sum2');
	hidd.value = sum_ab;
}


function addkop(sum) {
	r=Math.round(sum);
	fl = r-sum;
	if (fl == 0)
	{
		return cor_num(sum);
	} else {
		var str = new String(sum);
		arr = str.split('.');
		if(arr[1].length > 2){
			return cor_num(arr[0])+'.'+arr[1].substr(0,2);
		} else {
			if(arr[1].length > 0){
				return cor_num(arr[0])+'.'+arr[1];
			} else {
				return cor_num(sum);
			}
		}
	}
}

	function cor_num(val) {
		var val = new String(val);

		rez = '';
		for(i=1;i<=val.length;i++) {
			if (i%3==0) rez = ' ' + val.substr(val.length-i,1) + rez;
			else rez = val.substr(val.length-i,1) + rez;
		}
		
		return rez;
	}
