function showBranches() {
	var provinceElement = document.getElementById('selectProvince');
	var cityElement = document.getElementById('selectCity');
	var typeElement = document.getElementById('selectType');

	var url = ajaxURL;
	vars = {};

	if (provinceElement.value) {
//		url += '/province/' + provinceElement.value;
		vars.province = provinceElement.value;
	}
	if (cityElement.value) {
//		url += '/city/' + cityElement.value;
		vars.city = cityElement.value;
	}
	if (typeElement && typeElement.value) {
//		url += '/type/' + typeElement.value;
		vars.type = typeElement.value;
	}

	document.body.style.cursor = 'wait';
	$.post(ajaxURL, vars, updateContent);
}

function initBranchFilter() {
	$('#branchFilter').click(showBranches);
	$('#selectProvince').change(function() {updateCitySelect(); updateTypeSelect(); });
	$('#selectCity').change(updateTypeSelect);
}

$().ready(initBranchFilter);

function updateContent(newContent) {
	document.getElementById('branchContent').innerHTML = newContent;
	document.body.style.cursor = 'default';
}

function updateCitySelect() {
	var provinceElement = document.getElementById('selectProvince');
	var cityElement = document.getElementById('selectCity');

	var beforeValue = cityElement.value;
	cityElement.innerHTML = '';

	var newOp = document.createElement('option');
	newOp.text = allCities;
	newOp.value = '';
	try {
		cityElement.add(newOp, null);
	} catch (e) {
		cityElement.add(newOp);
	}

	if (provinceElement.value && provinceElement.value.length > 0) {
		if (!distributorInfo.provinceList) return;
		if (distributorInfo.provinceList[provinceElement.value] == undefined) return;
		if (distributorInfo.provinceList[provinceElement.value].cities == undefined) return;

		var c = distributorInfo.provinceList[provinceElement.value].cities;
	} else {
		if (distributorInfo.cityList == undefined) return;
		var c = distributorInfo.cityList;
	}

	for (i in c) {
		var newOp = document.createElement('option');
		newOp.text = i;
		newOp.value = i;
		try {
			cityElement.add(newOp, null);
		} catch (e) {
			cityElement.add(newOp);
		}
	}

/*
		var l = c.length;

		for (i = 0; i < l; i++) {
			var newOp = document.createElement('option');
			newOp.text = c[i];
			newOp.value = c[i];
			try {
				cityElement.add(newOp, null);
			} catch (e) {
				cityElement.add(newOp);
			}
		}
*/

	if (beforeValue && beforeValue.length) {
		cityElement.value = beforeValue;
	}
}

function updateTypeSelect() {

	var typeElement = document.getElementById('selectType');

	if (!typeElement) return;

	var beforeValue = typeElement.value;

	typeElement.innerHTML = '<option value=""></option>';

	var cityElement = document.getElementById('selectCity');
	var provinceElement = document.getElementById('selectProvince');

	var typeList = distributorInfo.typeList;

	if (cityElement.value) {
		if (distributorInfo.cityList[cityElement.value] == undefined) return;
		typeList = distributorInfo.cityList[cityElement.value];
	} else if (provinceElement.value) {
		if (distributorInfo.provinceList[provinceElement.value] == undefined) return;
		if (distributorInfo.provinceList[provinceElement.value].types == undefined) return;		

		typeList = distributorInfo.provinceList[provinceElement.value].types;
	}

	var l = typeList.length;
	for (var i = 0; i < l; i++) {
		var typeId = typeList[i];

		if (typeNames[typeId] == undefined) continue;

		typeElement.innerHTML += '<option value="' + typeId + '">' + typeNames[typeId] + '</option>';
	}

	typeElement.value = beforeValue;
}


function showLayer(layerName) {
	var provSel = document.getElementById('selectProvince');
	if (!provSel) return;
	switch (layerName) {
		case 'dolnoslaskie':
			provSel.value = 'D';
			break;
		case 'kujawskopomorskie':
			provSel.value = 'C';
			break;
		case 'lubelskie':
			provSel.value = 'L';
			break;
		case 'lubuskie':
			provSel.value = 'F';
			break;
		case 'lodzkie':
			provSel.value = 'E';
			break;
		case 'malopolskie':
			provSel.value = 'K';
			break;
		case 'mazowieckie':
			provSel.value = 'W';
			break;
		case 'opolskie':
			provSel.value = 'O';
			break;
		case 'podkarpackie':
			provSel.value = 'R';
			break;
		case 'podlaskie':
			provSel.value = 'B';
			break;
		case 'pomorskie':
			provSel.value = 'G';
			break;
		case 'slaskie':
			provSel.value = 'S';
			break;
		case 'swietokrzyskie':
			provSel.value = 'T';
			break;
		case 'warminskomazurskie':
			provSel.value = 'N';
			break;
		case 'wielkopolskie':
			provSel.value = 'P';
			break;
		case 'zachodniopomorskie':
			provSel.value = 'Z';
			break;
	}

	updateCitySelect();
	updateTypeSelect();

	showBranches();
//	$('#mapContent').hide();
}

