function SelectAllSectors() {
	var i, c, tmp;
	
	c = parseInt($('count_branches').value);
	for (i=0; i<c; i++) {
		tmp = 'branch' + i;
		if ($('sector_all').checked) {
			$(tmp).checked = true;
			SelectBranch(i);
		} else {
			$(tmp).checked = false;
			SelectBranch(i);
		}
	}
}



function SelectBranch(j) {
	var i, c, tmp;
	
	c = parseInt($('count_sectors'+j).value);
	for (i=0; i<c; i++) {
		tmp = 'sector' + j + '_' + i;
		if ($('branch'+j).checked) {
			$(tmp).disabled = true;
		} else {
			$(tmp).disabled = false;
		}
	}
}



function SelectAllCompanies() {
	if ($('all_companies').checked) {
		$('companies_full').disabled = true;
		$('companies_sel').disabled = true;
	} else {
		$('companies_full').disabled = false;
		$('companies_sel').disabled = false;
	}
}

function AddCompanies() {
	var flag, tmp_val, len, i, j, len_sel;
	var cur_companies = $('companies').value;
	var tmp_arr = cur_companies.split(';'); 
	
	if (!$('all_companies').checked) {
		len = $('companies_full').length;
		for (i=0; i<len; i++) {
			if ($('companies_full').options[i].selected) {
				tmp_val = parseInt($('companies_full').options[i].value);
				if (tmp_val) {
					flag = false;
	
					for (j=0; j<tmp_arr.length; j++) {
						if (tmp_val == parseInt(tmp_arr[j])) {
							flag = true;
						}
					}
	
					if (!flag) {
						len_sel = $('companies_sel').length;
						$('companies_sel').options[len_sel] = new Option($('companies_full').options[i].text, $('companies_full').options[i].value);
						cur_companies += $('companies_full').options[i].value + ';';
					}
				}
			}
		}
	
		$('companies').value = cur_companies;
	}
}

function RemoveCompanies() {
	var cur_companies, len, i, tmp_val;
	
	cur_companies = '';
	if (!$('all_companies').checked) {
		len = $('companies_sel').length;
		for (i=len-1; i>=0; i--) {
			tmp_val = $('companies_sel').options[i].value;
			if (!$('companies_sel').options[i].selected) {
				cur_companies += tmp_val + ';';
			} else {
				$('companies_sel').options[i] = null;
			}
		}
	
		$('companies').value = cur_companies;
	}
}
