var countryNames = new Array("Afghanistan","Albania","Algeria","American Samoa","Andorra","Angola","Anguilla","Antarctica","Antigua and Barbuda","Argentina","Armenia","Aruba","Australia","Austria","Azerbaijan","The Bahamas","Bahrain","Bangladesh","Barbados","Belarus","Belgium","Belize","Benin","Bermuda","Bhutan","Bolivia","Bosnia","Botswana","Bouvet Island","Brazil","British","British Indian Ocean Territory","Brunei","Bulgaria","Burkina Faso","Burundi","Cambodia","Cameroon","Canada","Cape Verde","Cayman Islands","Central African Republic","Chad","Chile","China","Christmas Island","Cocos (Keeling) Islands","Colombia ","Comoros","Congo, Republic of the","Congo, The Democratic Republic Of The","Cook Islands","Costa Rica","Cote D'ivoire","Croatia","Cuba","Curacao","Cyprus","Czech Republic","Denmark","Djibouti","Dominica","Dominican Republic","Ecuador","Egypt","El Salvador","Equatorial Guinea","Eritrea","Estonia","Ethiopia","Falkland Islands (Malvinas)","Faroe Islands","Fiji","Finland","France","French Guiana","French Polynesia","French Southern Territories","Gabon","Gambia","Georgia","Germany","Ghana","Gibraltar","Greece","Greenland","Grenada","Guadeloupe","Guam","Guatemala","Guernsey","Guinea","Guinea-Bissau ","Guyana","Haiti","Heard Island and McDonald Islands","Holy See (Vatican City State)","Honduras","Hong Kong","Hungary","Iceland","India","Indonesia","Iran","Iraq","Ireland","Isle of Man","Israel","Italy","Jamaica","Japan","Jersey","Jordan","Kazakhstan ","Kenya","Kiribati","Korea, North","Kuwait","Kyrgyzstan ","Lao People's Democratic Republic","Latvia ","Lebanon","Lesotho","Liberia","Libya","Liechtenstein","Lithuania ","Luxembourg","Macao","Macedonia, The Former Yugoslav Republic of","Madagascar","Malawi","Malaysia","Maldives","Mali","Malta","Marshall Islands","Martinique","Mauritania","Mauritius","Mayotte","Mexico","Micronesia, Federated States of","Moldova ","Monaco","Mongolia","Montenegro","Montserrat","Morocco","Mozambique","Myanmar","Namibia","Nauru","Nepal","Netherlands","Netherlands Antilles","New Caledonia","New Zealand","Nicaragua","Niger","Nigeria","Niue","Norfolk Island","North Korea","Northern Mariana Islands","Norway","Oman","Pakistan","Palau","Panama","Papua New Guinea","Paraguay","Peru","Philippines","Pitcairn Islands","Poland","Portugal","Puerto Rico","Qatar","Reunion","Romania ","Russian Federation","Rwanda","Saba & St. Eustatius","Saint Helena","Saint Pierre and Miquelon","Samoa","San Marino","Sao Tome & Principe","Sao Tome and Principe","Saudi Arabia","Scotland","Senegal","Serbia","Seychelles","Sierra Leone","Sierre Leone","Singapore","Slovakia","Slovenia ","Solomon Islands","Somalia","South Africa","South Georgia and the South Sandwich Islands","South Korea","Spain","Sri Lanka","St Vincent and the Grenadines","St. Barts","St. Croix","St. Kitts and Nevis","Saint Lucia","St. Martin/Sint Maarten","St. Thomas","St. Vincent and the Grenadines","Sudan","Suriname","Svalbard And Jan Mayen","Swaziland","Sweden","Switzerland","Syria","Taiwan","Tajikistan","Tanzania","Thailand","The Former Yugoslav Republic of Macedonia ","Togo","Tokelau","Tonga","Tortola","Trinidad and Tobago","Tunisia","Turkey","Turkmenistan ","Turks and Caicos","Tuvalu","Uganda","Ukraine ","United Arab Emirates","United Kingdom","United States","United States Minor Outlying Islands","Unknown/None","Uruguay","Uzbekistan ","Vanuatu","Venezuela","Viet Nam","Virgin Gorda","Virgin Islands, British","Virgin Islands, U.S.","Wales","Wallis and Futuna","Western Sahara","Western Samoa","Yemen","Zaire see Congo,The Democratic Republic Of The Zambia","Zimbabwe"); 
var countryIds = new Array(55,251,1,119,400,2,168,401,169,205,252,171,105,227,253,172,316,59,173,254,228,206,3,174,60,207,255,4,407,208,61,408,63,257,5,6,65,7,38,8,175,9,10,209,66,411,413,210,11,415,414,120,211,14,258,176,340,317,229,230,15,177,178,212,16,213,17,18,260,19,214,419,106,232,233,215,121,421,20,21,261,234,22,424,235,426,179,180,122,217,427,23,24,218,181,428,249,338,429,236,237,69,70,318,319,239,431,320,238,182,74,435,321,263,25,107,439,322,264,79,265,324,26,27,28,440,266,441,442,443,29,30,81,82,31,444,108,183,32,33,445,313,446,268,448,83,449,184,34,35,64,36,110,84,240,185,123,111,220,37,155,124,125,85,452,241,325,326,112,337,113,222,336,88,127,242,243,187,327,455,270,271,39,186,456,457,114,458,40,459,328,244,41,460,42,43,462,92,245,273,115,44,45,463,94,246,95,192,188,202,189,190,191,204,468,46,224,469,47,247,248,329,96,274,48,98,267,49,128,116,199,195,50,331,275,196,117,51,276,333,339,223,473,474,225,277,118,226,103,200,197,201,250,129,52,479,335,53,54);
var countryRegions = new Array(3,9,1,4,13,1,6,13,6,7,9,6,4,8,9,6,11,3,6,9,8,7,1,6,3,7,9,1,13,7,3,13,3,9,1,1,3,1,12,1,6,1,1,7,3,13,13,7,1,13,13,4,7,1,9,6,6,11,8,8,1,6,6,7,1,7,1,1,9,1,7,13,4,8,8,7,4,13,1,1,9,8,1,13,8,13,6,6,4,7,13,1,1,7,6,13,8,7,13,8,8,3,3,11,11,8,13,11,8,6,3,13,11,9,1,4,13,11,9,3,9,11,1,1,1,13,9,13,13,13,1,1,3,3,1,13,4,6,1,1,13,10,13,9,13,3,13,6,1,1,3,1,4,3,8,6,4,4,7,1,1,4,4,3,13,8,11,11,4,7,4,7,7,3,4,8,8,6,11,13,9,9,1,6,13,13,4,13,1,13,11,8,1,13,1,1,13,3,8,9,4,1,1,13,3,8,3,6,6,6,6,6,6,6,13,1,7,13,1,8,8,11,3,9,1,3,9,1,4,4,6,6,1,11,9,6,4,1,9,11,8,12,13,13,7,9,4,7,3,6,6,6,8,4,1,13,11,1,1);
var stateCountries = new Array(223,223,38,223,223,38,223,223,223,223,223,223,223,223,223,223,223,223,223,223,223,38,223,223,223,223,223,223,223,223,223,38,223,223,223,223,38,223,223,38,38,38,223,223,38,223,223,38,38,223,38,223,223,223,223,223,223,223,223,223,223,223,223,38);
var stateIds = new Array(1,2,66,3,4,67,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,68,20,21,22,23,24,25,26,27,28,70,29,30,31,32,69,33,34,72,71,73,35,36,74,37,38,75,76,39,77,40,41,42,43,44,45,46,48,47,49,50,51,78);
var stateNames = new Array("Alabama (AL)","Alaska (AK)","Alberta","Arizona (AZ)","Arkansas (AR)","British Columbia","California (CA)","Colorado (CO)","Connecticut (CT)","Delaware (DE)","Florida (FL)","Georgia (GA)","Hawaii (HI)","Idaho (ID)","Illinois (IL)","Indiana (IN)","Iowa (IA)","Kansas (KS)","Kentucky (KY)","Louisiana (LA)","Maine (ME)","Manitoba","Maryland (MD)","Massachusetts (MA)","Michigan (MI)","Minnesota (MN)","Mississippi (MS)","Missouri (MO)","Montana (MT)","Nebraska (NE)","Nevada (NV)","New Brunswick","New Hampshire (NH)","New Jersey (NJ)","New Mexico (NM)","New York (NY)","Newfoundland","North Carolina (NC)","North Dakota (ND)","Northwest Territories","Nova Scotia","Nunavut","Ohio (OH)","Oklahoma (OK)","Ontario","Oregon (OR)","Pennsylvania (PA)","Prince Edward Island","Quebec","Rhode Island (RI)","Saskatchewan","South Carolina (SC)","South Dakota (SD)","Tennessee (TN)","Texas (TX)","Utah (UT)","Vermont (VT)","Virginia (VA)","Washington (WA)","Washington D.C.","West Virginia (WV)","Wisconsin (WI)","Wyoming (WY)","Yukon Territory");
var separator = ';';

function addOption(element, option) {
	try {
	
			element.add(option,null);
		}
		catch (e) {
			element.add(option,element.length);
		}
	}
function addOptionInSortedList(element, option)
	{
		try
		{
			var idx = Search(element, option);
			if ((idx < element.length) && (option.text>element.options[idx].text)) idx++;
			element.options.add(option,idx);
			return idx;
		}
		catch (e)
		{
			element.options.add(option,element.length);
		}
	}

	function InitAirportComboBox(combo, comboValue)
	{
		combo.innerHTML = airportCombo;
	}
	function OnSelectRegion(oRegions, oCountries, oStates, oCountryValue, oStateValue) {				
		
		while (oCountries.length > 0)
			oCountries.remove(0);
		if (oRegions.selectedIndex >=0) {
			var regionId;
			var countryIdx = null; 
			var j = 0;
			regionId = oRegions.options[oRegions.selectedIndex].value;
			for (var i = 0; i < countryRegions.length; i++)
				if (regionId == countryRegions[i]) {
					var oOption = document.createElement('OPTION');
					oOption.text=countryNames[i];
					oOption.value=countryIds[i];
					addOption(oCountries,oOption);
					if (oOption.value == oCountryValue.value) 
						countryIdx = j; 
					j++
				}

			if (countryIdx != null)
				oCountries.selectedIndex = countryIdx;		
		}
		oCountries.disabled = (oCountries.options.length == 0);				
		OnSelectCountry(oCountries, oStates, oCountryValue, oStateValue);
	}
	
	function OnSelectRegionLocalized(oRegions, oCountries, oStates, oCountryValue, oStateValue, language) {				
		while (oCountries.length > 0)
			oCountries.remove(0);
			
		var neededCountryIds = new Array();
		var neededCountryNames = new Array();

		if (oRegions.selectedIndex >=0) {
			var regionId;
			var countryIdx = null; 
			var j = 0;
			regionId = oRegions.options[oRegions.selectedIndex].value;
			for (var i = 0; i < countryRegions.length; i++) {
				if (regionId == countryRegions[i])
					neededCountryIds.push(countryIds[i]);
			}
			var neededCountryNamesStr = 
			Brochures.Util.GeographyAJAXCallbacks.GetCountryNames(neededCountryIds, language);
			
			
			if ((typeof(neededCountryNamesStr)== "undefined")||
			(typeof(neededCountryNamesStr.value)== "undefined")||
			(neededCountryNamesStr.value == null))
			{
				return;
			}
			if (neededCountryNamesStr.value.indexOf(separator)>=0){
				neededCountryNames=neededCountryNamesStr.value.split(separator)
			}
			else if (neededCountryNamesStr.value.length>0){
				neededCountryNames[0]=neededCountryNamesStr.value
			}
			for (var k = 0; k < neededCountryNames.length; k++)
			{
				var option=document.createElement('OPTION');
				option.value=neededCountryIds[k];
				option.text=neededCountryNames[k];
				var idx = addOptionInSortedList(oCountries,option);
				if (option.value == oCountryValue.value)
				{
						countryIdx = j; 
				}
				j++;
			}
			if (countryIdx != null)
				oCountries.selectedIndex = countryIdx;		
		}
		oCountries.disabled = (oCountries.options.length == 0);				
		OnSelectCountryLocalized(oCountries, oStates, oCountryValue, oStateValue, language);
	}



	function OnSelectCountry(oCountries, oStates, oCountryValue, oStateValue) {
		while (oStates.length > 0)
			oStates.remove(0);
		oCountryValue.value = null;
		if (oCountries.selectedIndex >=0) {
			var stateIdx = null;
			var j = 0;
			oCountryValue.value = oCountries.options[oCountries.selectedIndex].value;
			for (var i = 0; i < stateCountries.length; i++)
				if (oCountryValue.value == stateCountries[i]) {
					var oOption = document.createElement('OPTION');
					oOption.text=stateNames[i];
					oOption.value=stateIds[i];
					addOption(oStates,oOption);
					if (oOption.value == oStateValue.value)
						stateIdx = j;
					j++;
				}

			if (stateIdx != null)
				oStates.selectedIndex = stateIdx;
		}

		oStates.disabled = (oStates.options.length == 0);
		OnSelectState(oStates,oStateValue);
	}

    function OnSelectCountryLocalized(oCountries, oStates, oCountryValue, oStateValue, language) {
	
		while (oStates.length > 0)
			oStates.remove(0);
		oCountryValue.value = null;
			
		var neededStateIds = new Array();
		var neededStateNames = new Array();
		
		if (oCountries.selectedIndex >=0) {
			var stateIdx = null;
			var j = 0;
			oCountryValue.value = oCountries.options[oCountries.selectedIndex].value;
			for (var i = 0; i < stateCountries.length; i++) {
				if (oCountryValue.value == stateCountries[i])
					neededStateIds.push(stateIds[i]);
			}
			var neededStateNamesStr = 
			Brochures.Util.GeographyAJAXCallbacks.GetStateNames(neededStateIds, language);
			
			
			if ((typeof(neededStateNamesStr)== "undefined")||
			(typeof(neededStateNamesStr.value)== "undefined")||
			(neededStateNamesStr.value == null))
			{
				return;
			}
			if (neededStateNamesStr.value.indexOf(separator)>=0){
				neededStateNames=neededStateNamesStr.value.split(separator)
			}
			else if (neededStateNamesStr.value.length>0){
				neededStateNames[0]=neededStateNamesStr.value
			}
			for (var k = 0; k < neededStateNames.length; k++)
			{
				var option=document.createElement('OPTION');
				option.value=neededStateIds[k];
				option.text=neededStateNames[k];
				var idx = addOptionInSortedList(oStates,option);
				if (option.value == oStateValue.value)
						stateIdx = j;
					j++;
				}
			if (stateIdx != null)
				oStates.selectedIndex = stateIdx;
		}
		oStates.disabled = (oStates.options.length == 0);
		OnSelectState(oStates,oStateValue);
	}


	function OnSelectState(oStates, oStateValue) {
		oStateValue.value = null;
		if (oStates.selectedIndex >=0) {
			oStateValue.value = oStates.options[oStates.selectedIndex].value;
		}
	}

	
