
function getXMLHTTP() { //fuction to return the xml http object
	var xmlhttp=false;	
	try{
		xmlhttp=new XMLHttpRequest();
	} catch(e) {		
		try {			
			xmlhttp= new ActiveXObject("Microsoft.XMLHTTP");
		} catch(e){
			try{
				xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
			} catch(e1){
				xmlhttp=false;
			}
		}
	}
		 	
	return xmlhttp;
}
	
function getCity(districtId) {		
		
	var req = getXMLHTTP();		
	if (req) {			
		req.onreadystatechange = function() {
			if (req.readyState == 4) {
				// only if "OK"
				if (req.status == 200) {						
					document.getElementById('searchCityList').innerHTML=req.responseText;						
				} else {
					alert("There was a problem while using XMLHTTP:\n" + req.statusText);
				}
			}				
		}
		var url = "/index2.php?option=com_finnportal&task=getCitiesChecks&no_html=1&districtId="+districtId;			
		req.open("GET", url, true);
		req.send(null);
	}				
}

function getCityList() {
	var districtId = document.getElementById('district').getValue();
	getCity(districtId);
}


var finn_cities = [['Äänekoski',5],['Alavus',3],['Espoo',2],['Heinola',5],['Helsinki',2],['Hyvinkää',2],['Imatra',4],
	['Inari',1],['Jämsä',5],['Joensuu',4],['Jyväskylä',5],['Kangasniemi',5],['Kemijärvi',1],['Kirkkonummi',2],[ 'Kouvola',4],['Kuopio',4],
	['Kuusamo',1],['Lappeenranta',4],['Lahti',5],['Lohja',2],['Mikkeli',4],['Oulu',1],['Pori',3],
	['Porvoo',4],['Rovaniemi',1],['Salo',3],['Savonlinna',4],['Sodankylä',1],['Tampere',3],['Turku',3],
	['Tuusula',2],['Vaasa',3],['Vantaa',2],['Vihti',2],['Virrat',3]];
var finn_regions = [1,0,0,0,0,0];
var cities_texts = ['',''];

function selectUnselectCities()
{
	var first_selected = document.getElementById('other_cities').checked;
	for(var i = 0; i < finn_cities.length; i++ ) 
	{
		if(document.getElementById('city[' + i + ']'))
			document.getElementsByName('city[' + i + ']')[1].checked = !first_selected;
	}
	document.getElementById('other_cities').checked = !first_selected;
}

function finnMapSelected(map_id) 
{
	var iNewVal = (finn_regions[map_id] == 0) ? 1 : 0;
	
	document.getElementById('extra_map'+ map_id).style.display = (iNewVal == 1) ? "block":"none";

	finn_regions[map_id] = iNewVal;

	var sDistrs = '';
	for(var i = 1; i < finn_regions.length; i++ ) 
		if(finn_regions[i] == 1)
			sDistrs += (sDistrs ? ',' : '') + i; 	
	document.getElementById('district').value = sDistrs;

	updateCitiesList();
}

function setOtherCities(name, selecter, empty_txt)
{
	cities_texts[0] = name;		
	cities_texts[1] = selecter;
	cities_texts[2] = empty_txt		
} 

function updateCitiesList() 
{		
		var sResult = '';
		var iTotal = 0;
		for(var i = 0; i < finn_cities.length; i++ ) 
		{
			var region = finn_cities[i][1];
			if(finn_regions[region] == 0)
				continue;
			if((iTotal % 6) == 0) {
				sResult += '<tr height=25px>';
			}			

			sResult += '<td><label><input type="hidden" name="city[' + (i+1) + ']" id="city[' + (i+1) + ']" value="0">' +
					      '<input type="checkbox" checked name="city[' + (i+1) + ']" id="city[' + (i+1) + ']" value="1">' + finn_cities[i][0] + '</input></label></td>';

			iTotal++;			
		}        
         
		if(iTotal > 1 && sResult != '')            
			sResult = '<table width=97% align=left>' + sResult + '<tr height=40px>\
				<td colspan=3 align=left valign=center><label><a href="javascript:;" onclick="selectUnselectCities();">' + cities_texts[1] + '</a></td>\
				<td colspan=3 align=right valign=center><label><input type="checkbox" checked name="other_cities" id="other_cities" value="1" >' + cities_texts[0] + '</input></label></td>\
				</table>';
		else
			sResult = cities_texts[2]; 
	
		document.getElementById('searchCityList').innerHTML = sResult;
}

