// $ID: $
// This is a javascript program to produce an HTML select box
// listing all the countries.
// This is done for performance reasons.



// global request and XML document objects
//var req;

var handleServerReturn = function(req)
    {
    var xml = req.responseXML;

    var region_span = document.getElementById("prov_state_selector");
    var regionhead = xml.getElementsByTagName("regions");
    var items = xml.getElementsByTagName("region");
    // loop through <item> elements, and add each nested
    // <title> element to Topics select element
    
    kids = region_span.childNodes;
    var numkids = kids.length;
    for(var i = 0; i < numkids ; i++)
	{
	region_span.removeChild(kids[i]);
	}

    if (items.length > 0)
	{
	var select = document.createElement('select');
	select.name = "iso_region";
	select.disabled = document.regiondisabled;
	region_span.appendChild(select);

	var regiontitle = regionhead[0].attributes.getNamedItem('title').value;

	var o = new Option(" - Choose " + regiontitle  + " -","", false, false);
	select.options[select.options.length] = o;

	for (var i = 0; i < items.length; i++)
	    {
	    var code = items[i].attributes.getNamedItem('code').value;
	    var name = items[i].attributes.getNamedItem('name').value;

	    var should_be_selected = false;
	    var dr = document.defaultregion;
	    if(dr && dr != "None")
		{
		var pat = '^\\w+-\\w+';
		var s1 = dr.match(pat)[0];
		var s2 = code.match(pat)[0];
		should_be_selected = (s1 == s2);
		}
	    o = new Option(name,code,should_be_selected,should_be_selected);

	    select.options[select.options.length] = o;
	    }
	}
    }

var handleServerError = function(req)
    {
    log("Problem.")
    }

function CountryLookup(code)
    {
    var control = new doSimpleXMLHttpRequest("/regions", {"country":code});

    control.addCallbacks(handleServerReturn, handleServerError);
    }



function display_country_options( defaultCountry, fieldname, defaultregion, disabled )
    {
    document.defaultregion = defaultregion;
    document.regiondisabled = disabled;

    var countries = new Object();

    countries[""] = " - Choose -"
    countries["AF"] = "Afghanistan"
    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["BE"] = "Belgium"
    countries["BZ"] = "Belize"
    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["CA"] = "Canada"
    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["CD"] = "Congo, The Democratic Republic of The"
    countries["CK"] = "Cook Islands"
    countries["CR"] = "Costa Rica"
    countries["CI"] = "Cote D'ivoire"
    countries["HR"] = "Croatia"
    countries["CU"] = "Cuba"
    countries["CY"] = "Cyprus"
    countries["CZ"] = "Czech Republic"
    countries["DK"] = "Denmark"
    countries["DJ"] = "Djibouti"
    countries["DM"] = "Dominica"
    countries["DO"] = "Dominican Republic"
    countries["TL"] = "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["FJ"] = "Fiji"
    countries["FI"] = "Finland"
    countries["FR"] = "France"
    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["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["VA"] = "Holy See (vatican City State)"
    countries["HN"] = "Honduras"
    countries["HK"] = "Hong Kong"
    countries["HU"] = "Hungary"
    countries["IS"] = "Iceland"
    countries["IN"] = "India"
    countries["ID"] = "Indonesia"
    countries["IR"] = "Iran, Islamic Republic of"
    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, Democratic People's Republic of"
    countries["KR"] = "Korea, Republic of"
    countries["KW"] = "Kuwait"
    countries["KG"] = "Kyrgyzstan"
    countries["LA"] = "Lao People's Democratic Republic"
    countries["LV"] = "Latvia"
    countries["LB"] = "Lebanon"
    countries["LS"] = "Lesotho"
    countries["LR"] = "Liberia"
    countries["LY"] = "Libyan Arab Jamahiriya"
    countries["LI"] = "Liechtenstein"
    countries["LT"] = "Lithuania"
    countries["LU"] = "Luxembourg"
    countries["MO"] = "Macao"
    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["FM"] = "Micronesia, Federated States of"
    countries["MD"] = "Moldova, Republic of"
    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["NP"] = "Nepal"
    countries["NL"] = "Netherlands"
    countries["AN"] = "Netherlands Antilles"
    countries["NC"] = "New Caledonia"
    countries["NZ"] = "New Zealand"
    countries["NI"] = "Nicaragua"
    countries["NE"] = "Niger"
    countries["NG"] = "Nigeria"
    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, Occupied"
    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["MK"] = "Republic of Macedonia"
    countries["RO"] = "Romania"
    countries["RU"] = "Russian Federation"
    countries["RW"] = "Rwanda"
    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"
    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["GS"] = "South Georgia and The South Sandwich Islands"
    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"] = "Syrian Arab Republic"
    countries["TW"] = "Taiwan, Province of China"
    countries["TJ"] = "Tajikistan"
    countries["TZ"] = "Tanzania, United Republic of"
    countries["TH"] = "Thailand"
    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["GB"] = "United Kingdom"
    countries["US"] = "United States"
    countries["UM"] = "United States Minor Outlying Islands"
    countries["UY"] = "Uruguay"
    countries["UZ"] = "Uzbekistan"
    countries["VU"] = "Vanuatu"
    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"
    countries["ZM"] = "Zambia"
    countries["ZW"] = "Zimbabwe"

//     var select = document.createElement('select');
//     for(var code in countries)
// 	{
// 	var o = new Option(countries[code], code, defaultCountry=code,
// 	    defaultCountry=code);
// 	//select.add(o);
// 
// 	select.options[select.options.length] = o;
// 	}
	

//     var outerspan = document.createElement('span');
//     outerspan.attributes['boobies'] = "yes please"
//     var innerspan = document.createElement('span');
//     outerspan.appendChild(select);
//     outerspan.appendChild(innerspan);
//     return outerspan;

    document.write('<span id="country_selector"> <span class="input">');

    var dis = disabled ? "disabled" : "";

    document.write("<select name='" + fieldname + "' " + dis + " onChange='return CountryLookup(this.value);'>")

    for( code in countries )
	{
	if( code == defaultCountry )
	    {
	    document.write("<option value='" + code + "' selected='true'>" +
		countries[code] + "</option>");
	    }
	else
	    {
	    document.write("<option value='" + code + "'>" + countries[code] +
		"</option>");
	    }
	}
    document.write("</select>")

    document.write("<span id='prov_state_selector' class='shown'></span>")
    document.write("</span></span>");

    if(defaultCountry)
	{
	CountryLookup(defaultCountry);
	}

    }
 

