var countries = [];
countries['US'] = 'United States';
countries['CA'] = 'Canada';
countries['AF'] = 'Afghanistan';
countries['AX'] = 'Aland Islands';
countries['AL'] = 'Albania';
countries['DZ'] = 'Algeria';
countries['AS'] = 'American Samoa';
countries['AD'] = 'Andorra';
countries['AO'] = 'Angola';
countries['AI'] = 'Anguilla';
countries['AQ'] = 'Antarctica';
countries['AG'] = 'Antigua and Barbuda';
countries['AR'] = 'Argentina';
countries['AM'] = 'Armenia';
countries['AW'] = 'Aruba';
countries['AU'] = 'Australia';
countries['AT'] = 'Austria';
countries['AZ'] = 'Azerbaijan';
countries['BS'] = 'Bahamas';
countries['BH'] = 'Bahrain';
countries['BD'] = 'Bangladesh';
countries['BB'] = 'Barbados';
countries['BY'] = 'Belarus';
countries['BZ'] = 'Belize';
countries['BE'] = 'Belgium';
countries['BJ'] = 'Benin';
countries['BM'] = 'Bermuda';
countries['BT'] = 'Bhutan';
countries['BO'] = 'Bolivia';
countries['BA'] = 'Bosnia and Herzegovina';
countries['BW'] = 'Botswana';
countries['BV'] = 'Bouvet Island';
countries['BR'] = 'Brazil';
countries['IO'] = 'British Indian Ocean Territory';
countries['BN'] = 'Brunei Darussalam';
countries['BG'] = 'Bulgaria';
countries['BF'] = 'Burkina Faso';
countries['BI'] = 'Burundi';
countries['KH'] = 'Cambodia';
countries['CM'] = 'Cameroon';
countries['CV'] = 'Cape Verde';
countries['KY'] = 'Cayman Islands';
countries['CF'] = 'Central African Republic';
countries['TD'] = 'Chad';
countries['CL'] = 'Chile';
countries['CN'] = 'China';
countries['CX'] = 'Christmas Island';
countries['CC'] = 'Cocos (Keeling) Islands';
countries['CO'] = 'Colombia';
countries['KM'] = 'Comoros';
countries['CG'] = 'Congo';
countries['CK'] = 'Cook Islands';
countries['CR'] = 'Costa Rica';
countries['CI'] = 'Cote D\'Ivoire (Ivory Coast)';
countries['HR'] = 'Croatia (Hrvatska)';
countries['CU'] = 'Cuba';
countries['CY'] = 'Cyprus';
countries['CZ'] = 'Czech Republic';
countries['CD'] = 'Democratic Republic of the Congo';
countries['DK'] = 'Denmark';
countries['DJ'] = 'Djibouti';
countries['DM'] = 'Dominica';
countries['DO'] = 'Dominican Republic';
countries['TP'] = 'East Timor';
countries['EC'] = 'Ecuador';
countries['EG'] = 'Egypt';
countries['SV'] = 'El Salvador';
countries['GQ'] = 'Equatorial Guinea';
countries['ER'] = 'Eritrea';
countries['EE'] = 'Estonia';
countries['ET'] = 'Ethiopia';
countries['FK'] = 'Falkland Islands (Malvinas)';
countries['FO'] = 'Faroe Islands';
countries['FM'] = 'Federated States of Micronesia';
countries['FJ'] = 'Fiji';
countries['FI'] = 'Finland';
countries['FR'] = 'France';
countries['FX'] = 'France, Metropolitan';
countries['GF'] = 'French Guiana';
countries['PF'] = 'French Polynesia';
countries['TF'] = 'French Southern Territories';
countries['GA'] = 'Gabon';
countries['GM'] = 'Gambia';
countries['GE'] = 'Georgia';
countries['DE'] = 'Germany';
countries['GH'] = 'Ghana';
countries['GI'] = 'Gibraltar';
countries['GB'] = 'Great Britain (UK)';
countries['GR'] = 'Greece';
countries['GL'] = 'Greenland';
countries['GD'] = 'Grenada';
countries['GP'] = 'Guadeloupe';
countries['GU'] = 'Guam';
countries['GT'] = 'Guatemala';
countries['GN'] = 'Guinea';
countries['GW'] = 'Guinea-Bissau';
countries['GY'] = 'Guyana';
countries['HT'] = 'Haiti';
countries['HM'] = 'Heard Island and McDonald Islands';
countries['HN'] = 'Honduras';
countries['HK'] = 'Hong Kong';
countries['HU'] = 'Hungary';
countries['IS'] = 'Iceland';
countries['IN'] = 'India';
countries['ID'] = 'Indonesia';
countries['IR'] = 'Iran';
countries['IQ'] = 'Iraq';
countries['IE'] = 'Ireland';
countries['IL'] = 'Israel';
countries['IT'] = 'Italy';
countries['JM'] = 'Jamaica';
countries['JP'] = 'Japan';
countries['JO'] = 'Jordan';
countries['KZ'] = 'Kazakhstan';
countries['KE'] = 'Kenya';
countries['KI'] = 'Kiribati';
countries['KP'] = 'Korea (North)';
countries['KR'] = 'Korea (South)';
countries['KW'] = 'Kuwait';
countries['KG'] = 'Kyrgyzstan';
countries['LA'] = 'Laos';
countries['LV'] = 'Latvia';
countries['LB'] = 'Lebanon';
countries['LS'] = 'Lesotho';
countries['LR'] = 'Liberia';
countries['LY'] = 'Libya';
countries['LI'] = 'Liechtenstein';
countries['LT'] = 'Lithuania';
countries['LU'] = 'Luxembourg';
countries['MO'] = 'Macao';
countries['MK'] = 'Macedonia';
countries['MG'] = 'Madagascar';
countries['MW'] = 'Malawi';
countries['MY'] = 'Malaysia';
countries['MV'] = 'Maldives';
countries['ML'] = 'Mali';
countries['MT'] = 'Malta';
countries['MH'] = 'Marshall Islands';
countries['MQ'] = 'Martinique';
countries['MR'] = 'Mauritania';
countries['MU'] = 'Mauritius';
countries['YT'] = 'Mayotte';
countries['MX'] = 'Mexico';
countries['MD'] = 'Moldova';
countries['MC'] = 'Monaco';
countries['MN'] = 'Mongolia';
countries['MS'] = 'Montserrat';
countries['MA'] = 'Morocco';
countries['MZ'] = 'Mozambique';
countries['MM'] = 'Myanmar';
countries['NA'] = 'Namibia';
countries['NR'] = 'Nauru';
countries['NI'] = 'Nicaragua';
countries['NE'] = 'Niger';
countries['NG'] = 'Nigeria';
countries['NP'] = 'Nepal';
countries['NL'] = 'Netherlands';
countries['AN'] = 'Netherlands Antilles';
countries['NC'] = 'New Caledonia';
countries['NZ'] = 'New Zealand (Aotearoa)';
countries['NU'] = 'Niue';
countries['NF'] = 'Norfolk Island';
countries['MP'] = 'Northern Mariana Islands';
countries['NO'] = 'Norway';
countries['OM'] = 'Oman';
countries['PK'] = 'Pakistan';
countries['PW'] = 'Palau';
countries['PS'] = 'Palestinian Territory';
countries['PA'] = 'Panama';
countries['PG'] = 'Papua New Guinea';
countries['PY'] = 'Paraguay';
countries['PE'] = 'Peru';
countries['PH'] = 'Philippines';
countries['PN'] = 'Pitcairn';
countries['PL'] = 'Poland';
countries['PT'] = 'Portugal';
countries['PR'] = 'Puerto Rico';
countries['QA'] = 'Qatar';
countries['RE'] = 'Reunion';
countries['RO'] = 'Romania';
countries['RU'] = 'Russian Federation';
countries['RW'] = 'Rwanda';
countries['GS'] = 'S. Georgia and S. Sandwich Islands';
countries['SH'] = 'Saint Helena';
countries['KN'] = 'Saint Kitts and Nevis';
countries['LC'] = 'Saint Lucia';
countries['PM'] = 'Saint Pierre and Miquelon';
countries['VC'] = 'Saint Vincent and the Grenadines';
countries['WS'] = 'Samoa';
countries['SM'] = 'San Marino';
countries['ST'] = 'Sao Tome and Principe';
countries['SA'] = 'Saudi Arabia';
countries['SN'] = 'Senegal';
countries['CS'] = 'Serbia and Montenegro';
countries['SC'] = 'Seychelles';
countries['SL'] = 'Sierra Leone';
countries['SG'] = 'Singapore';
countries['SK'] = 'Slovakia';
countries['SI'] = 'Slovenia';
countries['SB'] = 'Solomon Islands';
countries['SO'] = 'Somalia';
countries['ZA'] = 'South Africa';
countries['ES'] = 'Spain';
countries['LK'] = 'Sri Lanka';
countries['SD'] = 'Sudan';
countries['SR'] = 'Suriname';
countries['SJ'] = 'Svalbard and Jan Mayen';
countries['SZ'] = 'Swaziland';
countries['SE'] = 'Sweden';
countries['CH'] = 'Switzerland';
countries['SY'] = 'Syria';
countries['TW'] = 'Taiwan';
countries['TJ'] = 'Tajikistan';
countries['TZ'] = 'Tanzania';
countries['TH'] = 'Thailand';
countries['TL'] = 'Timor-Leste';
countries['TG'] = 'Togo';
countries['TK'] = 'Tokelau';
countries['TO'] = 'Tonga';
countries['TT'] = 'Trinidad and Tobago';
countries['TN'] = 'Tunisia';
countries['TR'] = 'Turkey';
countries['TM'] = 'Turkmenistan';
countries['TC'] = 'Turks and Caicos Islands';
countries['TV'] = 'Tuvalu';
countries['UG'] = 'Uganda';
countries['UA'] = 'Ukraine';
countries['AE'] = 'United Arab Emirates';
countries['UK'] = 'United Kingdom';
countries['UM'] = 'United States Minor Outlying Islands';
countries['UY'] = 'Uruguay';
countries['SU'] = 'USSR (former)';
countries['UZ'] = 'Uzbekistan';
countries['VU'] = 'Vanuatu';
countries['VA'] = 'Vatican City State (Holy See)';
countries['VE'] = 'Venezuela';
countries['VN'] = 'Viet Nam';
countries['VG'] = 'Virgin Islands (British)';
countries['VI'] = 'Virgin Islands (U.S.)';
countries['WF'] = 'Wallis and Futuna';
countries['EH'] = 'Western Sahara';
countries['YE'] = 'Yemen';
countries['YU'] = 'Yugoslavia (former)';
countries['ZR'] = 'Zaire (former)';
countries['ZM'] = 'Zambia';
countries['ZW'] = 'Zimbabwe';

var provinces = [];
provinces['US'] = ['AL', 'AK', 'AZ', 'AR', 'CA', 'CO', 'CT', 'DC', 'DE', 'FL', 'GA', 'HI', 'ID',
	'IL', 'IN', 'IA', 'KS', 'KY', 'LA', 'ME', 'MD', 'MA', 'MI', 'MN', 'MS', 'MO', 'MT', 'NE',
	'NV', 'NH', 'NJ', 'NM', 'NY', 'NC', 'ND', 'OH', 'OK', 'OR', 'PA', 'RI', 'SC', 'SD', 'TN',
	'TX', 'UT', 'VT', 'VA', 'WA', 'WV', 'WI', 'WY'];
	
provinces['CA'] = ['AB', 'BC', 'MB', 'NB', 'NL', 'NT', 'NS', 'NU', 'ON', 'PE', 'QC', 'SK', 'YT'];

/**
 * populate_countries()
 *
 * Populates a countryprovince field's country select element, identified by field ID 'fid', with options.
 * The options have a value corresponding to the two-letter country code, and text
 * with the full country name. The list is ordered by the country name, alphabetically ascending.
 * If selected_country is given, the selectedIndex of the select is set to the option with that
 * country code. This function also invokes populate_provinces(); if selected_province is given, 
 * that value will be passed to populate_provinces().
 * 
 * @param	int		fid					The field ID
 * @param	string	selected_country	Two-letter country code of the selected country
 * @param	string	selected_province	Two-letter province code of the selected province (US and Canada only)
 * @return	void
 */

function populate_countries(fid, selected_country, selected_province)
{
	var country_elt = document.getElementById('field_' + fid + '_country');
	if(!country_elt)
		country_elt = document.getElementById('f' + fid + 'c');
		
	var i=1;
	
	while(country_elt.firstChild) 
	{
		//The list is LIVE so it will re-index each call
		country_elt.removeChild(country_elt.firstChild);
	}
	
	//Populate the selects
	var newOption = document.createElement('option');
	newOption.value = 0;
	var newTextNode = document.createTextNode("Select a country...");
	newOption.appendChild(newTextNode);
	country_elt.appendChild(newOption);

	for(country_code in countries)
	{
		var this_country = countries[country_code];
		if(typeof(this_country) == "function")
			continue;
			
		newOption = document.createElement('option');
		newOption.value = country_code;
		newTextNode = document.createTextNode(this_country);
		newOption.appendChild(newTextNode);
		country_elt.appendChild(newOption);
		
		if(country_code == selected_country)
			country_elt.selectedIndex = i;
		
		i++;
	}
	
	if(selected_province)
		populate_provinces(fid, selected_province);
}

/**
 * populate_provinces()
 *
 * Populates a countryprovince field's province select element, identified by field ID 'fid', with options.
 * The options have a value corresponding to the two-letter province code, and text
 * the same (not the full province name). The list is ordered by the province code, alphabetically ascending.
 * Only US and Canadian provinces are supported. If the province select element does not exist (for instance
 * because the field's type data indicates that it does not use provinces), the function simply returns.
 * 
 * @param	int		fid					The field ID
 * @param	string	selected_province	Two-letter province code of the selected province (US and Canada only)
 * @return	void
 */

function populate_provinces(fid, selected_province)
{
	//Because the front-end search interfaces use abbreviate field names, there are two possible formats
	//for both the country and province field names.

	var country_elt = document.getElementById('field_' + fid + '_country');
	if(!country_elt)
		country_elt = document.getElementById('f' + fid + 'c');
		
	var province_elt = document.getElementById('field_' + fid + '_province');
	if(!province_elt)
		province_elt = document.getElementById('f' + fid + 'p');

	//If we still don't have a province_elt at this point, there is none, so bail out.
	if(!province_elt)
		return;

	var cur_country = country_elt.options[country_elt.selectedIndex].value;
	var i=1;
	
	while(province_elt.firstChild) 
	{
		//The list is LIVE so it will re-index each call
		province_elt.removeChild(province_elt.firstChild);
	}
	
	//Populate the selects
	var newOption = document.createElement('option');
	newOption.value = 0;
	var newTextNode = document.createTextNode("Select a state/province...");
	newOption.appendChild(newTextNode);
	province_elt.appendChild(newOption);
	
	if(provinces[cur_country])
	{
		for(var j=0; j<provinces[cur_country].length; j++)
		{
			var this_province = provinces[cur_country][j];
				
			newOption = document.createElement('option');
			newTextNode = document.createTextNode(this_province);
			newOption.appendChild(newTextNode);
			province_elt.appendChild(newOption);
			
			if(this_province == selected_province)
				province_elt.selectedIndex = i;
				
			i++;
		}
	}
	else
	{
		newOption = document.createElement('option');
		newOption.value = 0;
		newTextNode = document.createTextNode("Any state/province");
		newOption.appendChild(newTextNode);
		province_elt.appendChild(newOption);
		province_elt.selectedIndex = 1;
	}
}

