function toggledata(img,divid){
	var div=document.getElementById(divid);
	if(div.style.display=='none'){
		img.src="/images/close.gif";
	}else{
		img.src="/bitrix/templates/nks/i/ico-vopz.gif";
	}
	$('#'+divid).toggle();
}
function ServicesCollectionClass() {
	this.coll = new Array();
	this.htmlHolderId = 'services_calculator';
	this.totalOpened = false;

	this.summ = {
		onConnect: 0,
		monthlyOnFirstYear: 0,
		monthlyOnNextYears: 0,

		tarifsTitles: new Array(),
		tarifsPrice: 0,
		priceOneTime: 0,
		devBuyTitles: new Array(),
		devBuyIds: new Array(),
		devBuyPrice: 0,

		devLendTitles: new Array(),
		devLendIds: new Array(),
		devLendPrice: 0,

		optionsTitles: new Array(),
		optionsIds: new Array(),
		optionsPrice: 0
	};
}

ServicesCollectionClass.prototype.getById = function(id) {
	for (var i = 0; i < this.coll.length; i++) {
		if (this.coll[i] && (typeof(this.coll[i]) != 'undefined') && (this.coll[i].id == id)) {
			return this.coll[i];
		}
	}

	return false;
}

ServicesCollectionClass.prototype.add = function(type, active) {
	if (this.count(type) < 5) {
		this.coll[this.coll.length] = new ServiceClass(++globalId, type, this.count(type) + 1);
		if (this.coll[this.coll.length - 1] && (typeof(this.coll[this.coll.length - 1]) != 'undefined') && active && (typeof(active) != 'undefined')) {
			this.coll[this.coll.length - 1].active = true;
			this.coll[this.coll.length - 1].opened = true;
			this.coll[this.coll.length - 1].currentTarif = 0;
			if ((type != 'net') && (this.coll[this.coll.length - 1].devices.length > 0)) {
				this.coll[this.coll.length - 1].devices[0].enabled = true;
			}
		}
		this.redraw();
	}
}

ServicesCollectionClass.prototype.remove = function(id) {
	for (var i = 0; i < this.coll.length; i++) {
		if (this.coll[i] && (typeof(this.coll[i]) != 'undefined') && (this.coll[i].id == id)) {
			this.coll[i] = null;
		}
	}

	this.redraw();
}

ServicesCollectionClass.prototype.count = function(type) {
	var n = 0;

	for (var i = 0; i < this.coll.length; i++) {
		if (this.coll[i] && (typeof(this.coll[i]) != 'undefined') && (this.coll[i].type == type)) {
			n++;
		}
	}

	return n;
}

ServicesCollectionClass.prototype.countActive = function() {
	var n = 0;

	for (var i = 0; i < this.coll.length; i++) {
		if (this.coll[i] && (typeof(this.coll[i]) != 'undefined') && this.coll[i].active) {
			n++;
		}
	}

	return n;
}

ServicesCollectionClass.prototype.redraw = function() {
	this.paste();
}

ServicesCollectionClass.prototype.commitChanges = function(inp) {
	var re = new RegExp('^(tarif|option|device)_(net|tv|ip)_(\\d+)(_(\\d+))?$', 'i');
	var service, arr, arr1;

	//alert(re + '.test(' + inp.name + ') = ' + re.test(inp.name));
	if (re.test(inp.id) && (arr = re.exec(inp.id)) && (service = this.getById(arr[3]))) {
		switch (arr[1]) {
			case 'tarif':
				service.currentTarif = inp.value;
				break;
			case 'option':
				if ((arr[5] !== null) && (arr[5] != '') && !isNaN(arr[5])) {
					service.options[arr[5]].enabled = inp.checked;
				}
				break;
			case 'device':
				if (arr[2] == 'ip') {
					service.enebleSingleDevice(inp.value);
				} else if ((arr[5] !== null) && (arr[5] != '') && !isNaN(arr[5])) {
					service.devices[arr[5]].enabled = inp.checked;
				}
				break;
		}
	}

	this.redraw();
}

ServicesCollectionClass.prototype.paste = function(toPrint) {
	if ($('#' + this.htmlHolderId).length == 1) {
		var html = '';
		var htmlTotal = '';

		this.resetSumm();

		this.sortCollection();

		html += '<ul class="caltar">';

		for (var i = 0; i < this.coll.length; i++) {
			if (this.coll[i] && (typeof(this.coll[i]) != 'undefined')) {
				html += '<li>';
				html += '<div class="tipah ' + (this.coll[i].opened ? ' opn' : '') + '" style="height: 20px">' +
					'<span class="aop' + (this.coll[i].opened ? ' poa' : '') + '" onclick="services.openCloseServiceDetails(' + this.coll[i].id + ', this);"></span>' +

					'<input type="checkbox" name="service[' + this.coll[i].type + '][' + this.coll[i].id + '][enabled]" value="1" class="checkbox" ' + (this.coll[i].active ? 'checked' : '') + ' onclick="services.getById(' + this.coll[i].id + ').toggleActivity();" />' +
					'<label><strong>' + this.coll[i].name + '</strong></label>';


//					if (this.coll[i].active) {
//						html += '<div class="conplus"><small>подключение ' + this.coll[i].number + '</small>';
//						if (this.count(this.coll[i].type) < 5) {
//							html += '<a href="#" onclick="services.add(\'' + this.coll[i].type + '\', true); return false;"><img src="/images/plus.gif" width="14" height="14" alt="добавить подключение" /></a>';
//						}
//						html += '</div>';
//					}
				html += '</div>';

				html += '<div class="tabloholder"' + (this.coll[i].opened ? 'style="display: block;"' : '') + '>';
				html += '<table class="tablotar">';

				//Tarifs
				html += '<tr class="th1"><th class="last-child" colspan="' + ((this.coll[i].type == 'net') ? 5 : 4) + '"><div>Тарифные планы</div></th></tr>';
				html += '<tr class="th2">';
				html += '<th style="width: 110px;">Тарифный план</th>';
				if (this.coll[i].type == 'net') {
					html += '<th>Скорость вх./исх. канала <span>(кбит/сек)</span></th>';
					html += '<th>Объём<br />предоплаченного трафика <span>(Мбайт)</span></th>';
				} else {
					html += '<th>Описание</th>';
				}
				html += '<th>Абонентская плата <span>(руб/мес)</span></th>';
				html += '<th class="last-child"></th></tr>';

				for (var t = 0; t < this.coll[i].tarifs.length; t++) {
					html += this.pasteTarif(this.coll[i].tarifs[t], this.coll[i].type, this.coll[i].id, t == this.coll[i].currentTarif);
				}

				//Options
				if(this.coll[i].type != 'tv'){html += '<tr class="th1"><th class="last-child" colspan="' + ((this.coll[i].type == 'net') ? 5 : 4) + '"><div>Дополнительные опции</div></th></tr>';
				html += '<tr class="th2">';
				html += '<th>Название</th>';
				html += '<th' +((this.coll[i].type == 'net') ? ' colspan="2"' : '') + '>Описание</th>';
				html += '<th>Абонентская плата</th>';
				html += '<th class="last-child"></th></tr>';

				for (var o = 0; o < this.coll[i].options.length; o++) {
					html += this.pasteOption(this.coll[i].options[o], this.coll[i].type, this.coll[i].id);
				}
				}
				//Devices
				html += '<tr class="th1"><th class="last-child" colspan="' + ((this.coll[i].type == 'net') ? 5 : 4) + '"><div>Оборудование</div></th></tr>';
				html += '<tr class="th2">';
				html += '<th>Название</th>';
				html += '<th' +((this.coll[i].type == 'net') ? ' colspan="2"' : '') + '>Описание</th>';
				if (this.coll[i].type == 'net') {
					html += '<th>Аренда</th>';
				} else {
					html += '<th>Аренда</th>';
					//Отключено по просьбе клиента
					/*
					html += '<th>';
					html += '<input type="radio" value="0" name="fake_buysell_' + this.coll[i].id + '" class="radio" onclick="services.getById(' + this.coll[i].id + ').setBuyLend(false);" ' + (!this.coll[i].globalBuyLend ? 'checked' : '') + ' ' + (!this.coll[i].active ? 'disabled' : '') + ' />';
					html += '<label>Аренда</label><br />';
					html += '<input type="radio" value="1" name="fake_buysell_' + this.coll[i].id + '" class="radio" onclick="services.getById(' + this.coll[i].id + ').setBuyLend(true);" ' + (this.coll[i].globalBuyLend ? 'checked' : '') + ' ' + (!this.coll[i].active ? 'disabled' : '') + ' />';
					html += '<label>Покупка</label>';
					html += '</th>';
					*/
				}
				html += '<th class="last-child"></th></tr>';

				for (var d = 0; d < this.coll[i].devices.length; d++) {
					html += this.pasteDevice(this.coll[i].devices[d], this.coll[i].type, this.coll[i].id);
				}

				html += '</table>';
				html += '</div>';
				html += '</li>';
			}
		}

		if (this.countActive() > 0) {
			this.fixSumm();

			htmlTotal += '<li class="last-child">' +
			'<div class="tipah itogo clear' + (this.totalOpened ? ' opn' : '') + '">' +
				'<span class="aop' + (this.totalOpened ? ' poa' : '') + '" style="margin-right: 35px;" onclick="services.openCloseServiceDetails(\'total\', this);"></span>' +

				'<label><strong>Итого:</strong></label>' +

				'<div class="itog">' +
					'<strong class="base"><big class="red">' + this.summ.monthlyOnFirstYear + '</big> руб. в месяц <!--a href=http://onlime.ru/offer/1456>*</a--> ' + (false ? '<span>(в первый год)</span><br />' + this.summ.monthlyOnNextYears + ' руб в месяц <span>(следующие годы)</span>' : '') + '</strong>' +
					'<strong class="add"> + ' + this.summ.onConnect + ' руб при подключении + ' + this.summ.priceOneTime +' руб единовременно</strong>' +
				'</div>' +
			'</div>';
			htmlTotal += '<div class="tabloholder"' + (this.totalOpened ? ' style="display: block;"' : '') + '>' +
					'<table class="tablotar">' +
					'<tr class="th2">' +
					'<th>Название услуги</th>' +
					'<th style="width: 110px;">Стоимость <span>(со&nbsp;скидками)</span></th>' +
					'</tr>';

			htmlTotal += '<tr>';
			htmlTotal += '<td>';
			if (this.summ.tarifsTitles['net'].length > 0) {
				htmlTotal += 'услуга Интернет (' + this.summ.tarifsTitles['net'] + ')';
				if ((this.summ.tarifsTitles['tv'].length > 0) || (this.summ.tarifsTitles['ip'].length > 0)) {
					htmlTotal += ', ';
				}
			}
			if (this.summ.tarifsTitles['tv'].length > 0) {
				htmlTotal += 'услуга Телевидение (' + this.summ.tarifsTitles['tv'] + ')';
				if (this.summ.tarifsTitles['ip'].length > 0) {
					htmlTotal += ', ';
				}
			}
			if (this.summ.tarifsTitles['ip'].length > 0) {
				htmlTotal += 'услуга Телефония (' + this.summ.tarifsTitles['ip'] + ')';
			}
			htmlTotal += '</td>';
			htmlTotal += '<td>' + this.summ.tarifsPrice + ' руб/мес</td>';
			htmlTotal += '</tr>';

			htmlTotal += '<tr>';
			htmlTotal += '<td>Покупка оборудования';
			if ((this.summ.devBuyTitles['net'].length > 0) || (this.summ.devBuyTitles['tv'].length > 0) || (this.summ.devBuyTitles['ip'].length > 0)) {
				htmlTotal += ' (';
				if (this.summ.devBuyTitles['net'].length > 0) {
					htmlTotal += '<strong>' + this.summ.devBuyTitles['net'] + '</strong> для услуги Интернет';
					if ((this.summ.devBuyTitles['tv'].length > 0) || (this.summ.devBuyTitles['ip'].length > 0)) {
						htmlTotal += ', ';
					}
				}
				if (this.summ.devBuyTitles['tv'].length > 0) {
					htmlTotal += '<strong>' + this.summ.devBuyTitles['tv'] + '</strong> для услуги Телевидение';
					if (this.summ.devBuyTitles['ip'].length > 0) {
						htmlTotal += ', ';

					}
				}
				if (this.summ.devBuyTitles['ip'].length > 0) {
					htmlTotal += '<strong>' + this.summ.devBuyTitles['ip'] + '</strong> для услуги Телефония';
				}
				htmlTotal += ')';


			}
			htmlTotal += '</td>';
			htmlTotal += '<td>' + this.summ.devBuyPrice + ' руб</td>';
			htmlTotal += '</tr>';

			if (this.summ.devLendTitles['special'] && (typeof(this.summ.devLendTitles['special']) != 'undefined') && (this.summ.devLendTitles['special'].length == 2)) {
				htmlTotal += '<tr>';
				htmlTotal += '<td>Особые условия платежей (<strong>' + this.summ.devLendTitles['special'][0] + '</strong> для услуги Интернет)</td>';
				htmlTotal += '<td>' + this.summ.devLendTitles['special'][1] + '</td>';
				htmlTotal += '</tr>';
			}

			htmlTotal += '<tr>';
			htmlTotal += '<td>Аренда оборудования';
			if ((this.summ.devLendTitles['net'].length > 0) || (this.summ.devLendTitles['tv'].length > 0) || (this.summ.devLendTitles['ip'].length > 0)) {
				htmlTotal += ' (';
				if (this.summ.devLendTitles['net'].length > 0) {
					htmlTotal += '<strong>' + this.summ.devLendTitles['net'] + '</strong> для услуги Интернет';
					if ((this.summ.devLendTitles['tv'].length > 0) || (this.summ.devLendTitles['ip'].length > 0)) {
						htmlTotal += ', ';
					}
				}
				if (this.summ.devLendTitles['tv'].length > 0) {
					htmlTotal += '<strong>' + this.summ.devLendTitles['tv'] + '</strong> для услуги Телевидение';
					if (this.summ.devLendTitles['ip'].length > 0) {
						htmlTotal += ', ';
					}
				}
				if (this.summ.devLendTitles['ip'].length > 0) {
					htmlTotal += '<strong>' + this.summ.devLendTitles['ip'] + '</strong> для услуги Телефония';
				}
				htmlTotal += ')';
			}
			htmlTotal += '</td>';
			htmlTotal += '<td>' + this.summ.devLendPrice + ' руб/мес</td>';
			htmlTotal += '</tr>';
			htmlTotal += '<tr>';

			htmlTotal += '<td>Дополнительные опции';
			if ((this.summ.optionsTitles['net'].length > 0) || (this.summ.optionsTitles['tv'].length > 0) || (this.summ.optionsTitles['ip'].length > 0)) {
				htmlTotal += ' (';
				if (this.summ.optionsTitles['net'].length > 0) {
					htmlTotal += '<strong>' + this.summ.optionsTitles['net'] + '</strong> для услуги Интернет';
					if ((this.summ.optionsTitles['tv'].length > 0) || (this.summ.optionsTitles['ip'].length > 0)) {
						htmlTotal += ', ';
					}
				}
				if (this.summ.optionsTitles['tv'].length > 0) {
					htmlTotal += '<strong>' + this.summ.optionsTitles['tv'] + '</strong> для услуги Телевидение';
					if (this.summ.optionsTitles['ip'].length > 0) {
						htmlTotal += ', ';
					}
				}
				if (this.summ.optionsTitles['ip'].length > 0) {
					htmlTotal += '<strong>' + this.summ.optionsTitles['ip'] + '</strong> для услуги Телефония';
				}
				htmlTotal += ')';
			}
			htmlTotal += '</td>';
			htmlTotal += '<td>' + this.summ.optionsPrice + ' руб/мес</td>';
			htmlTotal += '</tr>';


			htmlTotal += '</table>';
			htmlTotal += '</div>';
			htmlTotal += '</li>';

			html += htmlTotal;
		}

		$('#total_info').val(htmlTotal.replace('services.openCloseServiceDetails(\'total\', this);', "$(this).parent().next().toggle();$(this).parent().toggleClass('opn');$(this).toggleClass('poa');"));

		html += '</ul>';

		if (this.countActive() > 0) {
			html += '<div>' +
						'<div style="float: left;">' +
							'<a href="#" onclick="services.print(); return false;">Распечатать</a><br /><small>(не отмеченные элементы будут скрыты)</small>' +
						'</div>' +


						'<div style="float: right;">' +
							'<strong><a href="#" onclick="document.calk.submit(); return false;">';
			if(addrselected==1)html+='<img src="/bitrix/templates/nks/i/check_form.gif" border=0>';else html+='<img src="/bitrix/templates/nks/i/connect_check.gif" border=0>';
			html+='</a></strong>' +
						'</div>' +
					'</div><div class="clear">&nbsp;</div>';
		} else {
			html += '<div class="clear"><strong>Выберите одну из услуг!</strong></div>';
		}

		if (toPrint === true) {
			return html;
		} else {
			$('#' + this.htmlHolderId).html(html);
		}
	}
}

ServicesCollectionClass.prototype.pasteTarif = function(tarif, type, serviceId, enabled) {
	var html = '<tr>';

	html += '<td nowrap><strong>' + tarif.title + '</strong></td>';

	switch (type) {
		case 'net':
			html += '<td>' + tarif.speed;
			if(typeof(tarif.speeddescr)!="undefined")html+=tarif.speeddescr+'</td>';else html+="</td>";
			html += '<td>' + tarif.limit + '</td>';
			break;
		case 'tv':
			html += '<td>' + tarif.descr + '</td>';
			break;
		case 'ip':
			html += '<td>' + tarif.descr + '</td>';
			break;
	}
	html += '<td><strong>' + tarif.price + '</strong>';
	if((tarif.price==200||tarif.price==250)&&type=='net')html+='<div style="float:right"><a href="/offer/1580"><img src="http://onlime.ru/images/bonus50.gif"></a></div>';
	html+='</td>';
	html += '<td class="last-child"><input type="radio" class="radio" id="tarif_' + type + '_' + serviceId + '" name="service[' + type + '][' + serviceId + '][tarif]" value="' + tarif.id + '" ' + (enabled ? 'checked' : '') + ' onclick="services.getById(' + serviceId + ').activate(); services.commitChanges(this);" /></td>';
	html += '</tr>';

	if (enabled && this.getById(serviceId).active)  {
		this.summ.monthlyOnFirstYear += (tarif.price - (this.discountAvaible() * tarif.price));
		this.summ.monthlyOnNextYears += (tarif.price - (this.discountAvaible() * tarif.price));

		this.summ.tarifsTitles[type] += 'тарифный план <strong>' + tarif.title + '</strong>, ';
		this.summ.tarifsPrice += (tarif.price - (this.discountAvaible() * tarif.price));
	}

	return html;
}

ServicesCollectionClass.prototype.pasteOption = function(option, type, serviceId) {
	var html = '<tr>';
	var onclick = '';

	html += '<td>' + option.title + '</td>';
	html += '<td' +((type == 'net') ? ' colspan="2"' : '') + '>' + option.descr + '</td>';
	html += '<td>' + ((option.price > 0) ? option.price : 'бесплатно') + '</td>';
	if(option.disabling && (typeof(option.disabling) != 'undefined') && (option.disabling.length > 0)){
		onclick = 'services.getById(' + serviceId + ').disableDeviceDependentTo(' + option.id + ', this.checked);';
	}
	html += '<td class="last-child"><input type="checkbox" id="option_' + type + '_' + serviceId + '_' + option.id + '" name="service[' + type + '][' + serviceId + '][options][' + option.id + ']" value="1" onclick="' + ((onclick != '') ? onclick : '') + ' services.commitChanges(this);" ' + (option.enabled ? 'checked' : '') + ' ' + (this.getById(serviceId).active ? '' : 'disabled') + ' /></td>';

	html += '</tr>';

	if (option.enabled && this.getById(serviceId).active)  {
		if (!this.summ.optionsIds[type][option.id] || (typeof(this.summ.optionsIds[type][option.id]) == 'undefined')) {
			this.summ.optionsTitles[type] += option.title + ' ' + this.countSameOptions(type, option.id) + ', ';
			this.summ.optionsIds[type][option.id] = true;
		}

		this.summ.monthlyOnFirstYear += option.price;
		this.summ.monthlyOnNextYears += option.price;
		this.summ.optionsPrice += option.price;
		this.summ.onConnect+=option.priceOnConnect;
	}

	return html;
}

ServicesCollectionClass.prototype.pasteDevice = function(dev, type, serviceId) {
	var html = '<tr' + (((this.getById(serviceId).devices.length - 1) == dev.id) ? ' class="last-child"' : '') + '>';

	if (dev.toggleAlreadyHave === true) {
		html += '<td colspan="' +((type == 'net') ? '4' : '3') + '">' + dev.title + '</td>';
	} else {
		html += '<td>' + dev.title + '</td>';
		html += '<td' +((type == 'net') ? ' colspan="2"' : '') + '>' + dev.descr;
		if (dev.img != '') {
			html += '&nbsp;<a href="#" target="_blank" onclick="openPopup(\'/device_popup.html?img_src=' + dev.img + '&id=' + type + dev.id + '\', 450, 400, true, true, this); return false;"><img src="/images/lupa.gif" width="14" height="14" alt="изображение устройства" class="ili" /></a><br><a href="';
			if(type=='tv')html+='/service/tv/equipment/';else if(type=='net')html+='/service/internet/additional/dop_wifi.php';
			html+='">подробнее</a>';
		}
		html += '</td>';

		html += '<td>';
		if (!isNaN(dev.price)) {
			if(type=='tv')html += (((dev.priceDescr != '') && (typeof(dev.priceDescr) != 'undefined')) ? dev.priceDescr : dev.priceOneTime);
			else html += (((dev.priceDescr != '') && (typeof(dev.priceDescr) != 'undefined')) ? dev.priceDescr : dev.price);
		} else if ((typeof(dev.price) != 'undefined') && (dev.price.length == 2)) {
			html += '<span id="price_' + serviceId + '_' + dev.id + '">' + (dev.buy ? dev.price[0] : dev.price[1]) + '</span>';
		}
		html += '</td>';
	}

	if (type == 'ip') {
		html += '<td class="last-child"><input type="radio" id="device_' + type + '_' + serviceId + '_' + dev.id + '" name="service[' + type + '][' + serviceId + '][device]" value="' + dev.id + '" onclick="services.commitChanges(this);" ' + (dev.enabled ? 'checked' : '') + ' ' + ((dev.readonly || !this.getById(serviceId).active) ? 'disabled' : '') + ' /></td>';
	} else {
		html += '<td class="last-child"><input type="checkbox" id="device_' + type + '_' + serviceId + '_' + dev.id + '" name="service[' + type + '][' + serviceId + '][devices][' + dev.id + ']" value="1" onclick="services.commitChanges(this);" ' + (dev.enabled ? 'checked' : '') + ' ' + ((dev.readonly || !this.getById(serviceId).active) ? 'disabled' : '') + ' /></td>';
	}
	html += '</tr>';

	if (this.getById(serviceId).active && dev.enabled) {
		if (!isNaN(dev.price)) {
			if (dev.lendOnly){
		if(!isNaN(dev.priceOneTime))this.summ.priceOneTime += dev.priceOneTime;
				this.summ.onConnect += dev.priceOnConnect;
				this.summ.devLendTitles['special'][0] = dev.title + this.countSameSpecialDevices(type, dev.id);
				if (this.countSameSpecialDevices(type, dev.id, true) > 1) {
					var conn = dev.priceDescrTplConnect * this.countSameSpecialDevices(type, dev.id, true);
					var mnth = dev.priceDescrTplMnth * this.countSameSpecialDevices(type, dev.id, true);
					var str = dev.priceDescrTpl.replace('[[connect]]', conn);
					str = str.replace('[[mnth]]', mnth);
					this.summ.devLendTitles['special'][1] = str;
				} else {
					this.summ.devLendTitles['special'][1] = (((dev.priceDescr != '') && (typeof(dev.priceDescr) != 'undefined')) ? dev.priceDescr : dev.price);
				}

				if (!dev.freeFirstYear) {
					this.summ.monthlyOnFirstYear += dev.price;
				}
				this.summ.monthlyOnNextYears += dev.price;
			} else {
				if (!dev.freeFirstYear) {
					this.summ.monthlyOnFirstYear += dev.price;
				}
				this.summ.monthlyOnNextYears += dev.price;

				if (!this.summ.devLendIds[type][dev.id] || (typeof(this.summ.devLendIds[type][dev.id]) == 'undefined')) {
					this.summ.devLendTitles[type] += dev.title + ' ' + this.countSameDevices(type, dev.id, false) + ', ';
					this.summ.devLendIds[type][dev.id] = true;
				}

				this.summ.devLendPrice += dev.price;
			}
		} else if (isNaN(dev.price) && (typeof(dev.price) != 'undefined') && (dev.price.length == 2)){
			
			if (dev.buy) {
				this.summ.onConnect += dev.price[0];
				if (!this.summ.devBuyIds[type][dev.id] || (typeof(this.summ.devBuyIds[type][dev.id]) == 'undefined')) {
					this.summ.devBuyTitles[type] += dev.title + ' ' + this.countSameDevices(type, dev.id, true) + ', ';
					this.summ.devBuyIds[type][dev.id] = true;
				}
				this.summ.devBuyPrice += dev.price[0];
			} else {
				if (!dev.freeFirstYear) {
					this.summ.monthlyOnFirstYear += dev.price[1];
				}
				this.summ.monthlyOnNextYears += dev.price[1];

				if (!this.summ.devLendIds[type][dev.id] || (typeof(this.summ.devLendIds[type][dev.id]) == 'undefined')) {
					this.summ.devLendTitles[type] += dev.title + ' ' + this.countSameDevices(type, dev.id, false) + ', ';
					this.summ.devLendIds[type][dev.id] = true;
				}

				this.summ.devLendPrice += dev.price[1];
			}
		}
	}

	return html;
}

ServicesCollectionClass.prototype.sortCollection = function() {
	var net = new Array(); var netN = 0;
	var tv = new Array(); var tvN = 0;
	var ip = new Array(); var ipN = 0;

	for (var i = 0; i < this.coll.length; i++) {
		if (this.coll[i] && (typeof(this.coll[i]) != 'undefined')) {
			switch (this.coll[i].type) {
				case 'net':
					this.coll[i].number = ++netN;
					net[net.length] = this.coll[i];
					break;
				case 'tv':
					this.coll[i].number = ++tvN;
					tv[tv.length] = this.coll[i];
					break;
				case 'ip':
					this.coll[i].number = ++ipN;
					ip[ip.length] = this.coll[i];
					break;
			}
		}
	}

	this.coll = net.concat(tv, ip);
}

ServicesCollectionClass.prototype.discountAvaible = function() {
	var netExists = false;
	var tvExists = false;
	var ipExists = false;

	for (var i = 0; i < this.coll.length; i++) {
		if (this.coll[i] && (typeof(this.coll[i]) != 'undefined') && (this.coll[i].active === true)) {
			switch (this.coll[i].type) {
				case 'net':
					netExists = true;
					break;
				case 'tv':
					tvExists = true;
					break;
				case 'ip':
					ipExists = true;
					break;
			}
		}
	}

	switch (true) {
		case netExists && tvExists && ipExists:
			return 0;//0.2;
		case (netExists && tvExists) || (netExists && ipExists) || (tvExists && ipExists):
			return 0;//0.1;
		default:
			return 0;
	}
}

ServicesCollectionClass.prototype.countSameOptions = function(type, id) {
	var n = 0;

	//Services
	for (var i = 0; i < this.coll.length; i++) {
		if ((this.coll[i].type == type) && this.coll[i].active) {
			if (this.coll[i].options[id] && (typeof(this.coll[i].options[id]) != 'undefined') && (this.coll[i].options[id].enabled === true)) {
				n++;
			}
		}
	}

	return ((n > 1) ? ' X' + n : '');
}

ServicesCollectionClass.prototype.countSameDevices = function(type, id, buy) {
	var n = 0;

	//Services
	for (var i = 0; i < this.coll.length; i++) {
		if ((this.coll[i].type == type) && this.coll[i].active) {
			if (this.coll[i].devices[id] && (typeof(this.coll[i].devices[id]) != 'undefined') && (this.coll[i].devices[id].enabled === true) && (this.coll[i].devices[id].buy == buy)) {
				n++;
			}
		}
	}

	return ((n > 1) ? ' X' + n : '');
}

ServicesCollectionClass.prototype.countSameSpecialDevices = function(type, id, returnInt) {
	var n = 0;

	//Services
	for (var i = 0; i < this.coll.length; i++) {
		if ((this.coll[i].type == type) && this.coll[i].active) {
			if (this.coll[i].devices[id] && (typeof(this.coll[i].devices[id]) != 'undefined') && (this.coll[i].devices[id].enabled === true) && (this.coll[i].devices[id].lendOnly === true)) {
				n++;
			}
		}
	}

	if (returnInt === true) {
		return n;
	} else {
		return ((n > 1) ? ' X' + n : '');
	}
}

ServicesCollectionClass.prototype.resetSumm = function() {
	this.summ.onConnect = 0;
	this.summ.monthlyOnFirstYear = 0;
	this.summ.monthlyOnNextYears = 0;

	this.summ.tarifsTitles['net'] = '';
	this.summ.tarifsTitles['tv'] = '';
	this.summ.tarifsTitles['ip'] = '';
	this.summ.tarifsPrice = 0;

	this.summ.devBuyTitles['net'] = '';
	this.summ.devBuyTitles['tv'] = '';
	this.summ.devBuyTitles['ip'] = '';
	this.summ.devBuyIds['net'] = new Array();
	this.summ.devBuyIds['tv'] = new Array();
	this.summ.devBuyIds['ip'] = new Array();
	this.summ.devBuyPrice = 0;

	this.summ.devLendTitles['net'] = '';
	this.summ.devLendTitles['tv'] = '';
	this.summ.devLendTitles['ip'] = '';
	this.summ.devLendTitles['special'] = new Array();
	this.summ.devLendIds['net'] = new Array();
	this.summ.devLendIds['tv'] = new Array();
	this.summ.devLendIds['ip'] = new Array();
	this.summ.devLendPrice = 0;

	this.summ.optionsTitles['net'] = '';
	this.summ.optionsTitles['tv'] = '';
	this.summ.optionsTitles['ip'] = '';
	this.summ.optionsIds['net'] = new Array();
	this.summ.optionsIds['tv'] = new Array();
	this.summ.optionsIds['ip'] = new Array();
	this.summ.optionsPrice = 0;
	this.summ.priceOneTime = 0;
}

ServicesCollectionClass.prototype.fixSumm = function() {
	this.summ.tarifsTitles['net'] = this.summ.tarifsTitles['net'].slice(0, this.summ.tarifsTitles['net'].length - 2);
	this.summ.tarifsTitles['tv'] = this.summ.tarifsTitles['tv'].slice(0, this.summ.tarifsTitles['tv'].length - 2);
	this.summ.tarifsTitles['ip'] = this.summ.tarifsTitles['ip'].slice(0, this.summ.tarifsTitles['ip'].length - 2);

	this.summ.devBuyTitles['net'] = this.summ.devBuyTitles['net'].slice(0, this.summ.devBuyTitles['net'].length - 2);
	this.summ.devBuyTitles['tv'] = this.summ.devBuyTitles['tv'].slice(0, this.summ.devBuyTitles['tv'].length - 2);
	this.summ.devBuyTitles['ip'] = this.summ.devBuyTitles['ip'].slice(0, this.summ.devBuyTitles['ip'].length - 2);

	this.summ.devLendTitles['net'] = this.summ.devLendTitles['net'].slice(0, this.summ.devLendTitles['net'].length - 2);
	this.summ.devLendTitles['tv'] = this.summ.devLendTitles['tv'].slice(0, this.summ.devLendTitles['tv'].length - 2);
	this.summ.devLendTitles['ip'] = this.summ.devLendTitles['ip'].slice(0, this.summ.devLendTitles['ip'].length - 2);

	this.summ.optionsTitles['net'] = this.summ.optionsTitles['net'].slice(0, this.summ.optionsTitles['net'].length - 2);
	this.summ.optionsTitles['tv'] = this.summ.optionsTitles['tv'].slice(0, this.summ.optionsTitles['tv'].length - 2);
	this.summ.optionsTitles['ip'] = this.summ.optionsTitles['ip'].slice(0, this.summ.optionsTitles['ip'].length - 2);
}

ServicesCollectionClass.prototype.openCloseServiceDetails = function(id, elem) {
	var srv;

	if (id == 'total') {
		$(elem).parent().next().toggle();
		$(elem).parent().toggleClass('opn');
		$(elem).toggleClass('poa');

		this.totalOpened = !this.totalOpened;
	} else if ((srv = this.getById(id)) && (typeof(srv) != 'undefined')) {
		$(elem).parent().next().toggle();
		$(elem).parent().toggleClass('opn');
		$(elem).toggleClass('poa');

		srv.opened = !srv.opened;
	}
}

ServicesCollectionClass.prototype.print = function() {
	if (this.countActive() > 0) {
		this.wndPrint = openPopup('/calk_print.html', 800, 600, true, true);
	} else {
		alert('Nothing to print!');
	}
}

ServicesCollectionClass.prototype.pastePrintContent = function() {
	var html = this.generatePrintHtml();
	var div = this.wndPrint.document.getElementById(this.htmlHolderId + '_print');
	if (div && (typeof(div) != 'undefined')) {
		div.innerHTML = html;
	}

	this.wndPrint.print();
}

ServicesCollectionClass.prototype.generatePrintHtml = function() {
	var html = '';

	for (var i = 0; i < this.coll.length; i++) {
		if (this.coll[i] && (typeof(this.coll[i]) != 'undefined') && (this.coll[i].active === true)) {
			html += '<strong>' + this.coll[i].name + '</strong>';
			html += ' <small>(подключение ' + this.coll[i].number + ')</small>';


			html += '<table class="tablotar">';

			//Tarifs
			html += '<tr class="th1"><th class="last-child" colspan="' + ((this.coll[i].type == 'net') ? 4 : 3) + '"><div>Тарифные планы</div></th></tr>';
			html += '<tr class="th2">';
			html += '<th style="width: 110px;">Тарифный план</th>';
			if (this.coll[i].type == 'net') {
				html += '<th>Скорость вх./исх. канала <span>(кбит/сек)</span></th>';
				html += '<th>Объём<br />предоплаченного трафика <span>(Мбайт)</span></th>';
			} else {
				html += '<th>Описание</th>';
			}
			html += '<th>Абонентская плата <span>(руб/мес)</span></th>';
			html += '</tr>';

			for (var t = 0; t < this.coll[i].tarifs.length; t++) {
				if (this.coll[i].tarifs[t].id == this.coll[i].currentTarif) {
					html += '<tr>';
					html += '<td><strong>' + this.coll[i].tarifs[t].title + '</strong></td>';

					switch (this.coll[i].type) {
						case 'net':
							html += '<td>' + this.coll[i].tarifs[t].speed+'</td>';
							html += '<td>' + this.coll[i].tarifs[t].limit + '</td>';
							break;
						case 'tv':
							html += '<td>' + this.coll[i].tarifs[t].descr + '</td>';
							break;
						case 'ip':
							html += '<td>' + this.coll[i].tarifs[t].descr + '</td>';
							break;
					}
					html += '<td><strong>' + this.coll[i].tarifs[t].price + '</strong></td>';
					html += '</tr>';
				}
			}

			//Options
			//html += '<tr class="th1"><th class="last-child" colspan="' + ((this.coll[i].type == 'net') ? 4 : 3) + '"><div>Дополнительные опции</div></th></tr>';
			html += '<tr class="th2">';
			html += '<th>Название</th>';
			html += '<th' +((this.coll[i].type == 'net') ? ' colspan="2"' : '') + '>Описание</th>';
			html += '<th>Абонентская плата</th>';
			html += '</tr>';

			for (var o = 0; o < this.coll[i].options.length; o++) {
				if (this.coll[i].options[o].enabled === true) {
					html += '<tr>';
					html += '<td>' + this.coll[i].options[o].title + '</td>';
					html += '<td' +((this.coll[i].type == 'net') ? ' colspan="2"' : '') + '>' + this.coll[i].options[o].descr + '</td>';
					html += '<td>' + ((this.coll[i].options[o].price > 0) ? this.coll[i].options[o].price : 'бесплатно') + '</td>';
					html += '</tr>';
				}
			}

			//Devices
			html += '<tr class="th1"><th class="last-child" colspan="' + ((this.coll[i].type == 'net') ? 4 : 3) + '"><div>Оборудование</div></th></tr>';
			html += '<tr class="th2">';
			html += '<th>Название</th>';
			html += '<th' +((this.coll[i].type == 'net') ? ' colspan="2"' : '') + '>Описание</th>';
			html += '<th>Аренда</th>';
			html += '</tr>';

			for (var d = 0; d < this.coll[i].devices.length; d++) {
				if (this.coll[i].devices[d].enabled === true) {
					html += '<tr>';

					if (this.coll[i].devices[d].toggleAlreadyHave === true) {
						html += '<td colspan="' +((this.coll[i].type == 'net') ? '4' : '3') + '">' + this.coll[i].devices[d].title + '</td>';
					} else {
						html += '<td>' + this.coll[i].devices[d].title + '</td>';
						html += '<td' +((this.coll[i].type == 'net') ? ' colspan="2"' : '') + '>' + this.coll[i].devices[d].descr + '</td>';
						html += '<td>';
						if (!isNaN(this.coll[i].devices[d].price)) {
							html += (((this.coll[i].devices[d].priceDescr != '') && (typeof(this.coll[i].devices[d].priceDescr) != 'undefined')) ? this.coll[i].devices[d].priceDescr : this.coll[i].devices[d].price);
						} else if ((typeof(this.coll[i].devices[d].price) != 'undefined') && (this.coll[i].devices[d].price.length == 2)) {
							html += (this.coll[i].devices[d].buy ? this.coll[i].devices[d].price[0] : this.coll[i].devices[d].price[1]);
						}
						html += '</td>';
					}

					html += '</tr>';
				}
			}

			html += '</table>';
		}
	}

	html += '<strong>Итого:</strong><br />' +
			'<strong><big>' + this.summ.onConnect + '</big> руб за подключение +<br />' +
			this.summ.monthlyOnFirstYear + ' руб в месяц <small>(в первый год)</small><br />' + this.summ.monthlyOnNextYears + ' руб в месяц <small>(следующие годы)</small></strong>';

	html += '<div>' +
			'<table>' +
			'<tr class="th2">' +
			'<th>Название услуги</th>' +
			'<th style="width: 110px;">Стоимость <span>(со&nbsp;скидками)</span></th>' +
			'</tr>';

	html += '<tr>';
	html += '<td>';
	if (this.summ.tarifsTitles['net'].length > 0) {
		html += 'услуга Интернет (' + this.summ.tarifsTitles['net'] + ')';
		if ((this.summ.tarifsTitles['tv'].length > 0) || (this.summ.tarifsTitles['ip'].length > 0)) {
			html += ', ';
		}
	}
	if (this.summ.tarifsTitles['tv'].length > 0) {
		html += 'услуга Телевидение (' + this.summ.tarifsTitles['tv'] + ')';
		if (this.summ.tarifsTitles['ip'].length > 0) {
			html += ', ';
		}
	}
	if (this.summ.tarifsTitles['ip'].length > 0) {
		html += 'услуга Телефония (' + this.summ.tarifsTitles['ip'] + ')';
	}
	html += '</td>';
	html += '<td>' + this.summ.tarifsPrice + ' руб/мес</td>';
	html += '</tr>';

	html += '<tr>';
	html += '<td>Покупка оборудования';
	if ((this.summ.devBuyTitles['net'].length > 0) || (this.summ.devBuyTitles['tv'].length > 0) || (this.summ.devBuyTitles['ip'].length > 0)) {
		html += ' (';
		if (this.summ.devBuyTitles['net'].length > 0) {
			html += '<strong>' + this.summ.devBuyTitles['net'] + '</strong> для услуги Интернет';
			if ((this.summ.devBuyTitles['tv'].length > 0) || (this.summ.devBuyTitles['ip'].length > 0)) {
				html += ', ';
			}
		}
		if (this.summ.devBuyTitles['tv'].length > 0) {
			html += '<strong>' + this.summ.devBuyTitles['tv'] + '</strong> для услуги Телевидение';
			if (this.summ.devBuyTitles['ip'].length > 0) {
				html += ', ';
			}
		}
		if (this.summ.devBuyTitles['ip'].length > 0) {
			html += '<strong>' + this.summ.devBuyTitles['ip'] + '</strong> для услуги Телефония';
		}
		html += ')';
	}
	html += '</td>';
	html += '<td>' + this.summ.devBuyPrice + ' руб</td>';
	html += '</tr>';

	if (this.summ.devLendTitles['special'] && (typeof(this.summ.devLendTitles['special']) != 'undefined') && (this.summ.devLendTitles['special'].length == 2)) {
		html += '<tr>';
		html += '<td>Особые условия платежей (<strong>' + this.summ.devLendTitles['special'][0] + '</strong> для услуги Интернет)</td>';
		html += '<td>' + this.summ.devLendTitles['special'][1] + '</td>';
		html += '</tr>';
		html += '<tr>';
	}

	html += '<td>Аренда оборудования';
	if ((this.summ.devLendTitles['net'].length > 0) || (this.summ.devLendTitles['tv'].length > 0) || (this.summ.devLendTitles['ip'].length > 0)) {
		html += ' (';
		if (this.summ.devLendTitles['net'].length > 0) {
			html += '<strong>' + this.summ.devLendTitles['net'] + '</strong> для услуги Интернет';
			if ((this.summ.devLendTitles['tv'].length > 0) || (this.summ.devLendTitles['ip'].length > 0)) {
				html += ', ';
			}
		}
		if (this.summ.devLendTitles['tv'].length > 0) {
			html += '<strong>' + this.summ.devLendTitles['tv'] + '</strong> для услуги Телевидение';
			if (this.summ.devLendTitles['ip'].length > 0) {
				html += ', ';
			}
		}
		if (this.summ.devLendTitles['ip'].length > 0) {
			html += '<strong>' + this.summ.devLendTitles['ip'] + '</strong> для услуги Телефония';
		}
		html += ')';
	}
	html += '</td>';
	html += '<td>' + this.summ.devLendPrice + ' руб/мес</td>';
	html += '</tr>';
	html += '<tr>';

	//html += '<td>Дополнительные опции';
	if ((this.summ.optionsTitles['net'].length > 0) || (this.summ.optionsTitles['tv'].length > 0) || (this.summ.optionsTitles['ip'].length > 0)) {
		html += ' (';
		if (this.summ.optionsTitles['net'].length > 0) {
			html += '<strong>' + this.summ.optionsTitles['net'] + '</strong> для услуги Интернет';
			if ((this.summ.optionsTitles['tv'].length > 0) || (this.summ.optionsTitles['ip'].length > 0)) {
				html += ', ';
			}
		}
		if (this.summ.optionsTitles['tv'].length > 0) {
			html += '<strong>' + this.summ.optionsTitles['tv'] + '</strong> для услуги Телевидение';
			if (this.summ.optionsTitles['ip'].length > 0) {
				html += ', ';
			}
		}
		if (this.summ.optionsTitles['ip'].length > 0) {
			html += '<strong>' + this.summ.optionsTitles['ip'] + '</strong> для услуги Телефония';
		}
		html += ')';
	}
	html += '</td>';
	html += '<td>' + this.summ.optionsPrice + ' руб/мес</td>';
	html += '</tr>';


	html += '</table>';
	html += '</div>';
	html += '<br /><br /><br />';

	return html;
}

ServicesCollectionClass.prototype.proto = function() {
}

/* ========================================================================== */

function ServiceClass(id, type, num) {
	this.id = id;
	this.type = type;
	this.number = num;
	this.active = false;
	this.opened = false;
	this.globalBuyLend = false;

	switch (type){
		case 'net':
			this.name = 'ИНТЕРНЕТ';
			this.opened = true;
			break;
		case 'tv':
			this.name = 'ЦИФРОВОЕ ТЕЛЕВИДЕНИЕ';
			this.opened = true;
			break;
		case 'ip':
			this.name = 'ЦИФРОВАЯ ТЕЛЕФОНИЯ';
			break;
		default:
			services.remove(this.id);
			alert('WARNING! Wrong ServiceClass::type param!');
	}

	this.initVars();
	this.currentTarif = -1;
}

ServiceClass.prototype.initVars = function() {
	this.tarifs = new Array();
	this.currentTarif = 0; //Force "select first"

	this.options = new Array();

	this.devices = new Array();

	switch (this.type) {
		case 'net':
			this.tarifs[this.tarifs.length] = {
				id: this.tarifs.length,
				price: 300,
				title: 'ОнЛайм 1024',
				speed: '1024 / 512',
				limit: 'неограничен'
			}
			this.tarifs[this.tarifs.length] = {
				id: this.tarifs.length,
				price: 200,
				title: 'ОнЛайм 4096',
				speed: '4096 / 2048',
				limit: 'неограничен'
			}
			this.tarifs[this.tarifs.length] = {
				id: this.tarifs.length,
				price: 250,			   
				title: 'ОнЛайм 10240',
				speed: '10240 / 5120',
				limit: 'неограничен'
			}
			this.tarifs[this.tarifs.length] = {
				id: this.tarifs.length,
				price: 700,
				title: 'ОнЛайм 13000',
				speed: '<div style="background:#94D400; height:13px; float:left; padding:1px; margin-right:1px; color:white">13312 / 7680</div>',
				speeddescr:" <div style=\"float:left\"><img src=\"/bitrix/templates/nks/i/ico-vopz.gif\" style=\"cursor: pointer;display:inline\" border=0 onclick=\"toggledata(this,'div700');\"></div><div style=\"display:none; position:absolute;background: white; border:2px solid #94D400; margin-top:-79px\" id=\"div700\"><div style=\"padding:3px\"><table border=\"0\" width=\"100%\"><tr><td>Время &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Скорость</td></tr></table></div><div style=\"padding:3px\">С 11:00 до 01:00 &nbsp;&nbsp;&nbsp; 13312 / 7680</div><div style=\"padding:3px\">С 01:00 до 11:00 &nbsp;&nbsp;&nbsp; 20480 / 20480</div><div style=\"background:url(/images/info_arr.gif) 42% 100% no-repeat; margin-bottom:-6px\">&nbsp;</div></div>",
				limit: 'неограничен'
			}
			this.tarifs[this.tarifs.length] = {
				id: this.tarifs.length,
				price: 950,
				title: 'ОнЛайм 25600',
				speed: '25600 / 10240',
				limit: 'неограничен'
			}
			
			this.tarifs[this.tarifs.length] = {
				id: this.tarifs.length,
				price: 1300,
				title: 'ОнЛайм 51200',
				speed: '51200 / 25600',
				limit: 'неограничен'
			}

			this.options[this.options.length] = {
				id: this.options.length,
				price: 120,
				priceOnConnect: 0,
				title: 'Фильтрация трафика',
				descr: 'позволяет исключить доступ к опасному контенту',
				enabled: false
			}

			this.options[this.options.length] = {
				id: this.options.length,
				price: 150,
				priceOnConnect: 150,
				priceDescr: '150 руб./мес<br />+ 150 руб. за подключение',
				title: 'Статический IP-адрес',
				descr: 'внешний статический IP-адрес на постоянной основе, предоставляется только после подключения услуги интернет',
				enabled: false
			}

			this.devices[this.devices.length] = {
				id: this.devices.length,
				price: 20,
				priceOnConnect: 600,
				lendOnly: true,
				priceDescr: 'С 1 по 12 месяц 600 руб./год<br />+ 20 руб. в месяц с 13-го месяца аренды',
				priceDescrTpl: 'С 1 по 12 месяц [[connect]] руб./год<br />+ [[mnth]] руб. в месяц с 13-го месяца аренды',
				priceDescrTplConnect: 600,
				priceDescrTplMnth: 20,
				title: 'Домашний Wi-Fi',
				descr: 'беспроводной интернет-доступ внутри вашей квартиры',
				enabled: false,
				readonly: false,
				freeFirstYear: true,
				img: '/images/wifi.jpg'
			}
			break;
		case 'tv':
			this.tarifs[this.tarifs.length] = {
				id: this.tarifs.length,
				price: 250,
				title: 'Базовый',
				descr: '72 канала'
			}

			this.devices[this.devices.length] = {
				id: this.devices.length,
				price: 0,
				priceOnConnect: 0,
				priceOneTime: 1440,
				lendOnly: true,
				title: 'Комплект оборудования',
				descr: 'STB, пульт, карта условного доступа',
				priceDescr: '1440 руб./год (с 1 по 12 месяц),<br>с 13-го месяца передается в собственность',
				enabled: this.active,
				readonly: false,
				freeFirstYear: false,
				img: '/service/tv/equipment/setboxfull.jpg'
			}
			break;
		case 'ip':
			this.tarifs[this.tarifs.length] = {
				id: this.tarifs.length,
				price: 0,
				title: 'Повременной',
				descr: '???'
			}
			this.tarifs[this.tarifs.length] = {
				id: this.tarifs.length,
				price: 0,
				title: 'Комбинированный',
				descr: '???'
			}
			this.tarifs[this.tarifs.length] = {
				id: this.tarifs.length,
				price: 0,
				title: 'Абонентский',
				descr: '???'
			}

			this.options[this.options.length] = {
				id: this.options.length,
				price: 0,
				title: 'Видеотелефония',
				descr: '???',
				enabled: false,
				disabling: [0, 1, 2, 3]
			}

			this.devices[this.devices.length] = {
				id: this.devices.length,
				price: 0,
				priceOnConnect: 0,
				lendOnly: false,
				title: 'Audiocodes MP-202',
				descr: '??? w/o videophone',
				enabled: false,
				readonly: false,
				freeFirstYear: false,
				img: ''
			}
			this.devices[this.devices.length] = {
				id: this.devices.length,
				price: 0,
				priceOnConnect: 0,
				lendOnly: false,
				title: 'Zyxel P-2300RDL EE',
				descr: '??? w/o videophone',
				enabled: false,
				readonly: false,
				freeFirstYear: false,
				img: ''
			}
			this.devices[this.devices.length] = {
				id: this.devices.length,
				price: 0,
				priceOnConnect: 0,
				lendOnly: false,
				title: 'Grandstream Budgetone 101',
				descr: '??? w/o videophone',
				enabled: false,
				readonly: false,
				freeFirstYear: false,
				img: ''
			}
			this.devices[this.devices.length] = {
				id: this.devices.length,
				price: 0,
				priceOnConnect: 0,
				lendOnly: false,
				title: 'Grandstream Budgetone 102',
				descr: '??? w/o videophone',
				enabled: false,
				readonly: false,
				freeFirstYear: false,
				img: ''
			}
			this.devices[this.devices.length] = {
				id: this.devices.length,
				price: 0,
				priceOnConnect: 0,
				lendOnly: false,
				title: 'Grandstream GXV-3000 IP Video Phone',
				descr: '??? with videophone',
				enabled: false,
				readonly: false,
				freeFirstYear: false,
				img: ''
			}
			this.devices[this.devices.length] = {
				id: this.devices.length,
				toggleAlreadyHave: true,
				price: 0,
				priceOnConnect: 0,
				lendOnly: false,
				title: 'Оборудование уже есть',
				descr: '',
				enabled: false,
				readonly: false,
				freeFirstYear: false,
				img: ''
			}
			break;
	}
}

ServiceClass.prototype.toggleActivity = function() {
	this.active = !this.active;
	this.opened = this.active;

	if (this.active) {
		this.initVars();
		if ((this.type != 'net') && (this.devices.length > 0)) {
			this.devices[0].enabled = true;
		}
	} else {
		this.initVars();
		this.currentTarif = -1;
	}
	this.setBuyLend(false);

	services.redraw();
}

ServiceClass.prototype.activate = function() {
	if (!this.active) {
		this.toggleActivity();
	}
}

ServiceClass.prototype.setBuyLend = function(flag) {
	for (var id = 0; id < this.devices.length; id++) {
		if (isNaN(this.devices[id].price) && (typeof(this.devices[id].price) != 'undefined') && (this.devices[id].price.length == 2)) {
			this.setDevicePrice(id, flag);
		}
	}

	this.globalBuyLend = flag;

	services.redraw();
}

ServiceClass.prototype.setDevicePrice = function(devId, buy) {
	this.devices[devId].buy = buy;

	$('#price_' + this.id + '_' + devId).html(buy ? this.devices[devId].price[0] : this.devices[devId].price[1]);
}

ServiceClass.prototype.enebleSingleDevice = function(devId) {
	for (var i = 0; i < this.devices.length; i++) {
		if (i == devId) {
			this.devices[i].enabled = true;
		} else {
			this.devices[i].enabled = false;
		}
	}
}

ServiceClass.prototype.disableDeviceDependentTo = function(optId, checked) {
	if (this.options[optId].disabling && (typeof(this.options[optId].disabling) != 'undefined') && (this.options[optId].disabling.length > 0)) {
		var recheckNedded = false;
		for(var i = 0; i < this.options[optId].disabling.length; i++) {
			if (checked) {
				if ((this.type == 'ip') && !recheckNedded) {
					recheckNedded = this.devices[this.options[optId].disabling[i]].enabled;
				}
				this.devices[this.options[optId].disabling[i]].enabled = false;
				this.devices[this.options[optId].disabling[i]].readonly = true;
			} else {
				this.devices[this.options[optId].disabling[i]].readonly = false;
			}
		}

		if (recheckNedded) {
			for (i = 0; i < this.devices.length; i++) {
				if (jQuery.inArray(i, this.options[optId].disabling) == -1) {
					this.devices[i].enabled = true;
					break;
				}
			}
		}
	}
}

ServiceClass.prototype.proto = function() {
}

/* ========================================================================== */

var globalId = 0;

var services = new ServicesCollectionClass();


function $$(id) {
	var elem = document.getElementById(id);

	if (elem && (typeof(elem) != 'undefined')) {
		return elem;
	}

	return false;
}

function openPopup(path, width, height, allowScroll, resizeable, alignByElem) {
	//Make uniq index
	var dt = new Date();
	var uniq = dt.getTime();
	var top = 200;
	var left = 400;

	//allowScroll = (bool)allowScroll
	if (allowScroll && (typeof(allowScroll) != 'undefined')) {
		allowScroll = true;
	} else {
		allowScroll = false;
	}

	//resizeable = (bool)resizeable
	if (resizeable && (typeof(resizeable) != 'undefined')) {
		resizeable = true;
	} else {
		resizeable = false;
	}

	//open popup
	var wnd = window.open(path, 'popup_' + uniq, 'width=' + width + ',height=' + height + ',top=' + top + ',left=' + left + ',titlebar=0,toolbar=0,location=0,statusbar=0,menubar=0,scrollbars=' + (allowScroll ? '1' : '0') + ',resizable=' + (resizeable ? '1' : '0'));

	return wnd;
}

/*
int toInt(mixed num);
Преобразует принятые данные в целочисленное значение (по алгоритму PHP).
*/
function toInt(num) {
	if (/^(-)?(\d+)/.test(num)) {
		var arr = /^(-)?(\d+)/.exec(num);
		if (arr && (typeof(arr) != 'undefined') && (arr[2] > 0)) {
			arr[2]--; arr[2]++;
			if (arr[1] == '-') {
				arr[2] = - arr[2];
			}
			return arr[2];
		}
	}

	return 0;
}