//removes all options in a dropdownlist
function nullOptions(obj){
	var tot=obj.options.length
	for (i=0;i<tot;i++)
	{
	obj.options[i]=null;
	}
	obj.options.length=0;
}
//populates a dropdownlist with cities 
function populateCitySelect(id, selectName, hiddenName) 
{
	var iSelected = 0;
	var arrId  = id.split('#');
	var arrCities = getCityArray(arrId[0]);
	if (arrCities != null){
		var tmpSelect = document.getElementById(selectName);
		tmpSelect.innerHTML = "";
		nullOptions(tmpSelect);
		var arrCity;

		try{
			with(tmpSelect){
				for(var i = 0; i < arrCities.length; i++){
					arrCity = arrCities[i].split('|');
					options[i]=new Option(unescape(arrCity[0]), arrCity[1]);

					// If we have a value in the hidden field, it's been chosen and we select it in the dropdown
					if ((document.getElementById(hiddenName).value != "") && (document.getElementById(hiddenName).value != "-1"))
					{
						if (arrCity[1] == document.getElementById(hiddenName).value){
							if(iSelected == 0)
								{iSelected = i;}
						}										
					}
					// otherwise its a main city and we select it in the dropdown
					else
					{
						if (arrCity[1] == arrId[1])
						{
							if(iSelected == 0)
								{iSelected = i;}
						}
						
						document.getElementById(hiddenName).value = options[iSelected].value;
					}										
				}
				
				// if iSelected is 0 here, nothing is selected, it was wrong citycollection
				if (iSelected == 0)
				{
					for(var i = 0; i < arrCities.length; i++){
						arrCity = arrCities[i].split('|');
						options[i]=new Option(unescape(arrCity[0]), arrCity[1]);

					if (arrCity[1] == arrId[1])
					{
						if(iSelected == 0)
							iSelected = i;
					}
						
					document.getElementById(hiddenName).value = options[iSelected].value;							
					}				
				}
				
				options[iSelected].selected=true								
			}
		}catch(e){
			nullOptions(tmpSelect);
		}
	}else{
		try{
			nullOptions(document.getElementById(selectName));
		}catch(e){	
		}
	}	
}
// gets the selected index of a dropdown
function getDdSelectedIndex(id)
{
	var ddl = document.getElementById(id);
	return ddl.selectedIndex;
}

// sets the hidden field for city to the city id
function setCitySelectValue(index, hiddenName) 
{
	document.getElementById(hiddenName).value = index;
}
function initSelectedCities()
{
	try{
	var ddlFromCountry = document.getElementById(getFromCountryId());
	var FromCountryValue = ddlFromCountry.options[ddlFromCountry.selectedIndex].value

	var ddlToCountry = document.getElementById(getToCountryId());
	var ToCountryValue = ddlToCountry.options[ddlToCountry.selectedIndex].value

	if (getHiddenCityFromId().value != null)
		populateCitySelect(FromCountryValue, getSelectCityFromId(),getHiddenCityFromId());
		
	if (getHiddenCityToId().value != null)		
		populateCitySelect(ToCountryValue, getSelectCityToId(),getHiddenCityToId());
		
	}catch(e){	
		}
}