
function updateRegion()
{
	if (document.getElementById("ReferenceFilter_Country_Id")) {
		var country_id = document.getElementById("ReferenceFilter_Country_Id").value;
		document.getElementById("ReferenceFilter_Region_Id").disabled=false;
		document.getElementById("ReferenceFilter_District_Id").disabled=true;


		var url = 'ajax.php?Entity=LlentabDemandForm&Action=Detail&ScriptName=Detail_Ajax';
		url = url + '&Country_Id=' + country_id;
		// odeslání požadavku na aktualizaci dat
		if (!send_xmlhttprequest(updateRegionOption, 'GET', url)) {
			return false;
		}
		return true;
	}
}

function updateRegionOption(xmlhttp){
	if (xmlhttp.readyState == 4) {
		var selector = document.getElementById("ReferenceFilter_District_Id");
		// clear old select except the first one
		for(var i=selector.options.length-1;i>0;i--) {
			selector.remove(i);
		}

		selector = document.getElementById("ReferenceFilter_Region_Id");
		// clear old select except the first one
		for(var i=selector.options.length-1;i>0;i--) {
			selector.remove(i);
		}

		// fill new data
		var regionList = xmlhttp.responseXML.getElementsByTagName('Region');
		for (var i=0; i < regionList.length; i++) {
		    option = new Option(regionList[i].getElementsByTagName('RegionName')[0].firstChild.data, regionList[i].getElementsByTagName('Region_Id')[0].firstChild.data)
			try {
			    selector.add(option, null); // standard
			}
			catch(ex) {
			    selector.add(option); // IE only
			}
		}

		option = new Option("neuvedu / nevím", "");
		try {
		    selector.add(option, null); // standard
		}
		catch(ex) {
		    selector.add(option); // IE only
		}

	}
	return true;
}

function updateDistrict()
{
	if (document.getElementById("ReferenceFilter_Region_Id")) {
		document.getElementById("ReferenceFilter_District_Id").disabled=false;
		var region_id = document.getElementById("ReferenceFilter_Region_Id").value;

		var url = 'ajax.php?Entity=LlentabDemandForm&Action=Detail&ScriptName=Detail_Ajax';
		url = url + '&Region_Id=' + region_id;
		// odeslání požadavku na aktualizaci dat
		if (!send_xmlhttprequest(updateDistrictOption, 'GET', url)) {
			return false;
		}
		return true;
	}
}

function updateDistrictOption(xmlhttp){
	if (xmlhttp.readyState == 4) {
		var selector = document.getElementById("ReferenceFilter_District_Id");
		// clear old select except the first one
		for(var i=selector.options.length-1;i>0;i--) {
			selector.remove(i);
		}

		// fill new data
		var added = false;

		// fill new data
		var districtList = xmlhttp.responseXML.getElementsByTagName('District');
		for (var i=0; i < districtList.length; i++) {
		    option = new Option(districtList[i].getElementsByTagName('DistrictName')[0].firstChild.data, districtList[i].getElementsByTagName('District_Id')[0].firstChild.data)
			try {
			    selector.add(option, null); // standard
			}
			catch(ex) {
			    selector.add(option); // IE only
			}
			added = true;
		}

		if(added){
		    option = new Option("neuvedu / nevím", "");
			try {
			    selector.add(option, null); // standard
			}
			catch(ex) {
			    selector.add(option); // IE only
			}
			}else{
				document.getElementById("ReferenceFilter_District_Id").disabled=true;
			}
	}

}

function showCountry(country) {
	if (document.getElementById("ReferenceFilter_Country_Id")) {
		var countryfield =document.getElementById("ReferenceFilter_Country_Id");
		if(country == "2")	{ //Show SR
			hideElement("CRmap");
			showElement("SRmap");
			setElementStyleClass("ToggleButtonCR","ToggleButtonCR");
			setElementStyleClass("ToggleButtonSR","ToggleButtonSR select");
			countryfield.value = "2";
		}else{
			hideElement("SRmap");
			showElement("CRmap");
			setElementStyleClass("ToggleButtonCR","ToggleButtonCR select");
			setElementStyleClass("ToggleButtonSR","ToggleButtonSR");
			countryfield.value = "1";
		}
		updateRegion();
	}
	document.getElementById("ReferenceFilter_District_Id").disabled=true;
}

function showElement(id)
{
	if (document.getElementById(id)) {
		document.getElementById(id).style.display="block";
	}
}

function hideElement(id)
{
	if (document.getElementById(id)) {
		var District_Id = document.getElementById(id).style.display="none";
	}
}

function setElementStyleClass(id, sclass)
{
	if (document.getElementById(id)) {

		var currTabElem = document.getElementById(id);

		currTabElem.setAttribute("class", sclass);
		currTabElem.setAttribute("className", sclass);
	}
}

function selectRegion(value)
{
	if (document.getElementById("ReferenceFilter_Region_Id")) {
		var thefield = document.getElementById("ReferenceFilter_Region_Id");
		for( var i=0; i<thefield.length; i++ ) {
			if(thefield.options[i].value == value)
			{
				thefield.selectedIndex = thefield.options[i].index;
				updateDistrict();
				return;
			}
		}
	}
}

function selectDistrict(value)
{
	if (document.getElementById("ReferenceFilter_District_Id")) {
		var thefield = document.getElementById("ReferenceFilter_District_Id");
		for( var i=0; i<thefield.length; i++ ) {
			if(thefield.options[i].value == value)
			{
				thefield.selectedIndex = thefield.options[i].index;
				return;
			}
		}
	}
}

