function CitySelect() {
	this.continents = Array();
	this.continents["Africa"]       = "Afrikka";
	this.continents["Australia"]    = "Australia ja Oseania";
	this.continents["Europe"]       = "Eurooppa";
	this.continents["NorthAmerica"] = "Pohjois-Amerikka";
	this.continents["NorthAsia"]    = "Pohjois-Aasia";
	this.continents["SouthAmerica"] = "Etel&auml;-Amerikka";
	this.continents["SouthAsia"]    = "Etel&auml;-Aasia";
	this.continents["WestAsia"]     = "L&auml;hi-It&auml;";
	
	this.countrycache = Array();
	this.citycache    = Array();

	this.parent_element = null;

	this.curr_cid = null;
	this.curr_e   = null;

	this.sel_country = null;
	this.countryid   = null;
	this.curcountry  = null;

	this.sel_continent = null;
	this.curcontinent  = null;
	this.continentid   = null;

	this.varname = null;
	
	this.select_mode = 'continent';
}

CitySelect.prototype.setVarName = function(name) {
	this.varname = name;
}

CitySelect.prototype.getVarName = function() {
	return this.varname;
}

CitySelect.prototype.attach = function(element) {
	if(element != null)
		this.parent_element = element;

}


CitySelect.prototype.resetSelection = function() {
	var parent = document.getElementById(this.parent_element);
	var hdr = document.createElement('h3');
	var ul  = document.createElement('ul');
		
	ul.id  = 'csel_'   + this.varname;
	hdr.id = 'selhdr_' + this.varname;
	hdr.innerHTML = 'Valitse maanosa';
	
	while(parent.hasChildNodes())
			parent.removeChild(parent.firstChild);
	
	
	for(i in this.continents) {
		if(this.continents.hasOwnProperty(i)) {
			var li = document.createElement('li');
			li.innerHTML = '&raquo; <a href="#" class="selitem" onclick="'+ this.varname + '.loadDataWrap(\'' + i + '\', this); return false;">' + this.continents[i] + '</a>';
			ul.appendChild(li);
		}
	}

		parent.appendChild(hdr);
		parent.appendChild(ul);

		this.select_mode = 'continent';
}

CitySelect.prototype.loadDataWrap = function(cid, e) {
	if(req != null && (req.readyState != 4 && req.readyState != 0)) {
		return;
	}

	this.curr_cid = cid;
	this.curr_e   = e;
	
	if(this.select_mode == 'continent') {
		if(!this.countrycache[cid]) {
			this.loadData('continent', cid);
		} else {
			this.choose(cid, e);
		}
	} else if(this.select_mode == 'country') {
		if(!this.citycache[cid]) {
			this.loadData('country', cid);
		} else {
			this.choose(cid, e);
		}
	} else if(this.select_mode == 'city') {
		this.choose(cid, e);
	}
}

CitySelect.prototype.getDataResponse = function() {
	if(req.readyState == 4 && req.status == 200) {
		if(current_obj.select_mode == 'continent') {
			current_obj.countrycache[current_obj.curr_cid] = eval("(" + req.responseText + ")");
			
			// String.parseJSON() WILL crash Safari when data exceeds 7000 bytes
			//contcountry[curr_cid] = req.responseText.parseJSON();	
		} else if(current_obj.select_mode == 'country') {
			current_obj.citycache[current_obj.curr_cid] = eval("(" + req.responseText+ ")");
			
			// crashes here as well
			//countrycities[curr_cid] = req.responseText.parseJSON();
		}
			
		current_obj.choose(current_obj.curr_cid, current_obj.curr_e);
	}
}

CitySelect.prototype.loadData = function(type, name) {
	req = createXMLHttpRequest();
	
	current_obj = this;
	
	//req.overrideMimeType('text/xml'); // work around for old Mozilla browsers
	req.open("GET", "/backend/data.php?" + type + "=" + name, true);
	req.onreadystatechange = this.getDataResponse;
	req.send(null);
}

	
CitySelect.prototype.choose = function(cid, e) {
	var csel = document.getElementById("csel_" + this.varname);
	var cname = e.innerHTML;
	
	if(this.select_mode == 'continent') {
			this.sel_continent = cname;
			this.curcontinent  = e;
			this.continentid   = cid;
			
			document.getElementById('selhdr_' + this.varname).innerHTML = 'Valitse maa';

			while(csel.hasChildNodes())
				csel.removeChild(csel.firstChild);
			
			var li = document.createElement('li');
			li.innerHTML = '&laquo; <a href="#" class="selitem" onclick="' + this.varname + '.resetSelection(); return false;"><strong>' + this.sel_continent + '</strong></a>';
			
			var ul = document.createElement('ul');
			
			for(i in this.countrycache[cid]) {
				if(this.countrycache[cid].hasOwnProperty(i)) {
					var tmpli = document.createElement('li');
					tmpli.innerHTML = '&raquo; <a href="#" class="selitem" onclick="' + this.varname + '.loadDataWrap(\'' + i + '\', this); return false;">' +  this.countrycache[cid][i] + '</a></li>';
					ul.appendChild(tmpli);
				}
			}
			
			csel.appendChild(li);
			csel.appendChild(ul);
			
			this.select_mode = 'country';
	} else if(this.select_mode == 'country') {
			this.sel_country = cname;
			this.countryid = cid;
			this.curcountry = e;
			
			document.getElementById('selhdr_' + this.varname).innerHTML = 'Valitse kaupunki';
			
			while(csel.hasChildNodes())
				csel.removeChild(csel.firstChild);
			
			var li1 = document.createElement('li');
			li1.innerHTML = '&laquo; <a href="#" class="selitem" onclick="' + this.varname + '.resetSelection(); return false;"><strong>' + this.sel_continent + '</strong></a>';
			var li2 = document.createElement('li');
			li2.innerHTML = '&laquo; <a href="#" class="selitem" onclick="' + this.varname + ".select_mode = 'continent'; " + this.varname + '.loadDataWrap(\'' + this.continentid + '\', ' +  this.varname + '.curcontinent); return false;"><strong>' + this.sel_country + "</strong></a>";
			
			var ul1 = document.createElement('ul');
			ul1.appendChild(li2);
			var ul2 = document.createElement('ul');
			
			for(i in this.citycache[cid]) {
				if(this.citycache[cid].hasOwnProperty(i)) {
					var li = document.createElement('li');
					li.innerHTML = '&raquo; <a href="#" class="selitem" onclick="' + this.varname + '.loadDataWrap(\'' + i + '\', this); return false;">' + this.citycache[cid][i] + '</a></li>';
					ul2.appendChild(li);
				}
			}
		
			csel.appendChild(li1);
			ul1.appendChild(ul2);
			csel.appendChild(ul1);
		
			this.select_mode = 'city';

	} else if(this.select_mode == 'city') {
		document.getElementById(this.varname + "_form_id").value   = this.countryid + "_" + cid;
		document.getElementById("savecity").submit();
	}
}

