
function CLocations(parent, options)
{
	if (typeof parent == "string")
	{
		parent = document.getElementById(parent);
	}

	
	this.scomboCountries = new CSearchCombo(parent, 
		// options
		{
			css : 
			{
				listClassName : "scombo_list",
				textBoxClassName : "countries_textbox inputbox",
				itemClassName : "scombo_item",
				selectionClassName : "scombo_selection"
			},
			
			showOnFocus : true,

			maxlength : 60,
			
			getItemFullText : CLocations.getCountryText,
			
			getItemSearchText : CLocations.getCountryText,
			
			searchFill : CLocations.searchFillCountries
			
		}// END options
	); // END new CSearchCombo
	

	this.scomboLocations = new CSearchCombo(parent, 
		// options
		{
			css : 
			{
				listClassName : "scombo_list",
				textBoxClassName : "locations_textbox inputbox",
				itemClassName : "scombo_item",
				selectionClassName : "scombo_selection"
			},
			
			showOnFocus : true,
			
			maxlength : 122,
			
			getItemFullText : CLocations.getLocationItemFullText,
			
			getItemSearchText : CLocations.getLocationItemSearchText,
			
			searchFill : CLocations.searchFillLocations,

			customData : options
		}// END options
	); // END new CSearchCombo

}


CLocations.locationsList = BT.data.Locations;
CLocations.regionsList = BT.data.Regions;
CLocations.countriesList = BT.data.Countries;


CLocations.create = function(parent, options)
{
	var loc = new CLocations(parent, options);

	return loc;
}


CLocations.getText = function(loc, reg, country, rom)
{
	var s = "";
	
	if (loc && loc != "")
	{
		s = s + loc;
	}
	
	if (reg && reg != "")
	{
		if (s != "") s += ", ";
		s += reg;
	}
	
	if (country && country != "" && (s == "" || country != "Romania" || rom))
	{
		if (s != "") s += ", ";
		s += country;
	}
	
	return s;
}


/* public functions
-----------------------------------------------
*/

CLocations.prototype.setLocationText = function(loc, reg)
{
	var ltext = "";
	var lindex0 = 0;
	var lindex1 = 0;

	if (loc == "")
	{
		if (reg != "")
		{
			this.scomboLocations.setText("jud. " + reg);
			ltext = reg;
			lindex0 = 0;
			lindex1 = 41;
		}
		else
		{
			this.scomboLocations.setText("");
			return;
		}
	}
	else
	{
		if (reg == "")
		{
			this.scomboLocations.setText(loc);
			ltext = loc;
			lindex0 = 41;
			lindex1 = 42;
		}
		else
		{
			this.scomboLocations.setText(loc + ", " + reg);
			ltext = loc;
			
			var num = CLocations.regionsList.length;
			lindex0 = 42;
			
			for (var i = 2; i <  num; i++)
			{
				if (CLocations.regionsList[i][1] == reg)
				{
					lindex1 = lindex0 + CLocations.regionsList[i][2];
					break;
				}
				
				lindex0 += CLocations.regionsList[i][2];
			}
		}

	}
	

	this.scomboLocations.removeAll();
	
	for (var i = lindex0; i < lindex1; i++)
	{
		if (CLocations.locationsList[i] == ltext)
		{
			var atext = ["<div style='float:left;'><b style='color:#1084F2'>", "", "</b>",  "", "</div>",
						"<div style='float:right;'>", "", "</div><br style='clear:both'/>"];
			
			// the bold text in location name
			atext[1] = "";
			// normal text in location name
			atext[3] = ltext;
			// region name
			atext[6] = (loc == "") ? "(jud)" : reg;
			
			this.scomboLocations.addItem(atext, i);

			this.scomboLocations.setSelectedIndex(0);
			
			break;
		}
	}


}


CLocations.prototype.getLocationText = function()
{
	return this.scomboLocations.getText().trim();
}


CLocations.prototype.getLocation = function()
{
	var sel = this.scomboLocations.getSelectedData();
	if (sel != null && sel <= 40 && this.scomboLocations.getText() == "jud. " + CLocations.locationsList[sel])
	{
		return "";
	}


	var a = this.scomboLocations.getText().split(",");
	
	if (a.length > 0) return a[0];
	
	return "";
}


CLocations.prototype.getRegion = function()
{
	var sel = this.scomboLocations.getSelectedData();
	if (sel != null && sel <= 40 && this.scomboLocations.getText() == "jud. " + CLocations.locationsList[sel])
	{
		return CLocations.locationsList[sel];
	}


	var a = this.scomboLocations.getText().split(",");
	
	if (a.length > 1) return a[1].trim();
	
	return "";
}


CLocations.prototype.setCountryText = function(country)
{
	this.scomboCountries.setText(country);
}


CLocations.prototype.getCountry = function()
{
	return this.scomboCountries.getText().trim();
}





/* helper functions
---------------------------------------------------------
*/

CLocations.searchFillLocations = function(combo, stext)
{
	var tbCountry = combo.textbox.parentNode.getElementsByTagName("input")[0];
	if (tbCountry.value != "Romania") return;

	var locations = CLocations.locationsList;
	var regions = CLocations.regionsList;
	var max = Math.pow(4, stext.length) + 8;
	var nitems = 0;
	var locname;
	var atext = ["<div style='float:left;'><b style='color:#1084F2'>", "", "</b>",  "", "</div>",
				"<div style='float:right;'>", "", "</div><br style='clear:both'/>"];
	
	// search variables
	var slen = stext.length;
	var clen;	
	var c1;
	var c2;

	stext = stext.toLowerCase();
	var i = (combo.customData.showRegions == true) ? 0 : 41;
	var num = locations.length;
	

	for (; i < num; i++)
	{
		locname = locations[i];

		// compare case insensitive
		clen = locname.length;			
		if (slen > clen) continue;			
		for (var j = 0; j < slen; j++)
		{
			c1 = stext.charCodeAt(j);
			c2 = locname.charCodeAt(j);

			if (c2 >= 65 && c2 <= 90) c2 += 32;
			
			if (c1 != c2)
			{
				if (c2 == 354 || c2 == 355) c2 = 116; // T, t,
				if (c2 == 350 || c2 == 351) c2 = 115; // S, s,
				if (c2 == 206 || c2 == 238) c2 = 105; // I^ i^

				if (c2 == 226 || c2 == 194 || c2 == 258 || c2 == 259) c2 = 97; // a~ a^	
				
				if (c1 != c2) break;
			}			
		}
		
		// match ?
		if (j == slen)
		{
			// the bold text in location name
			atext[1] = locname.substring(0, slen);
			// normal text in location name
			atext[3] = locname.substring(slen);
			// region name
			atext[6] = CLocations.getRegionName(i);
			
			combo.addItem(atext, i);
			
			nitems++;
		}

		if (nitems > max) break;
	}
}


CLocations.getRegionName = function(index)
{
	var s = 0;
	var regions = CLocations.regionsList;

	for (var i = 0, num = regions.length; i < num; i++)
	{
		s += regions[i][2];
		if (s > index) return regions[i][1];
	}
	
	return "";
}



CLocations.getLocationItemFullText = function(index)
{
	var regname = CLocations.getRegionName(index);
	
	if (regname == "") return CLocations.locationsList[index];
	if (regname == "(jud)") return "jud. " + CLocations.locationsList[index];

	return CLocations.locationsList[index] + ", " + regname;
}


CLocations.getLocationItemSearchText = function(index)
{
	return CLocations.locationsList[index];
}




CLocations.searchFillCountries = function(combo, stext)
{	
	var countries = CLocations.countriesList;
	var nitems = 0;
	var countryName;
	var atext = ["<b style='color:blue'>", "", "</b>",  ""];
	
	// search variables
	var slen = stext.length;
	var clen;	
	var c1;
	var c2;

	stext = stext.toLowerCase();
	for (var i = 0, num = countries.length; i < num; i++)
	{
		countryName = countries[i];

		// compare case insensitive
		clen = countryName.length;			
		if (slen > clen) continue;			
		for (var j = 0; j < slen; j++)
		{
			c1 = stext.charCodeAt(j);
			c2 = countryName.charCodeAt(j);
			
			if (c2 >= 65 && c2 <= 90) c2 += 32;
			
			if (c1 != c2) break;
		}
		
		// match ?
		if (j == slen)
		{
			// the bold text in country name
			atext[1] = countryName.substring(0, slen);
			// normal text in country name
			atext[3] = countryName.substring(slen);
		
			combo.addItem(atext, i);
			
			nitems++;
		}
	}
}


CLocations.getCountryText = function(index)
{
	return CLocations.countriesList[index];
}

