/*
	Country State Drop Downs v1.0.  

	(c) Copyright 2005 by Down Home Consulting, Inc (www.DownHomeConsulting.com)

	Permission is hereby granted, free of charge, to any person obtaining a
	copy of this software and associated documentation files (the "Software"),
	to deal in the Software without restriction, including without limitation
	the rights to use, copy, modify, merge, publish, distribute, sublicense,
	and/or sell copies of the Software, and to permit persons to whom the
	Software is furnished to do so, subject to the following conditions:

	The above copyright notice and this permission notice shall be included
	in all copies or substantial portions of the Software.

	THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
	OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
	ITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
	AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
	LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
	FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
	DEALINGS IN THE SOFTWARE.


	To install, add the following to the top of your page:

	<SCRIPT LANGUAGE="JavaScript" SRC="country_state.js"></SCRIPT>


	Add these select statements to your form:

	<select id='countrySelect' name='country' onchange='populateState()'>
	</select>
	<select id='stateSelect' name='state'>
	</select>
	<SCRIPT LANGUAGE="JavaScript">initCountry('US'); </SCRIPT>

*/

// If you have PHP you can set the post values like this
//var postState = '<?= $_POST["state"] ?>';
//var postCountry = '<?= $_POST["country"] ?>';
var postState = '';
var postCountry = '';

// State table
// 
// To edit the list, just delete a line or add a line.  Order is important.  The order 
// displayed is the order it appears on the drop down.
//
var state = '\
USA:AK:Alaska|\
USA:AL:Alabama|\
USA:AR:Arkansas|\
USA:AS:American Samoa|\
USA:AZ:Arizona|\
USA:CA:California|\
USA:CO:Colorado|\
USA:CT:Connecticut|\
USA:DC:D.C.|\
USA:DE:Delaware|\
USA:FL:Florida|\
USA:FM:Micronesia|\
USA:GA:Georgia|\
USA:GU:Guam|\
USA:HI:Hawaii|\
USA:IA:Iowa|\
USA:ID:Idaho|\
USA:IL:Illinois|\
USA:IN:Indiana|\
USA:KS:Kansas|\
USA:KY:Kentucky|\
USA:LA:Louisiana|\
USA:MA:Massachusetts|\
USA:MD:Maryland|\
USA:ME:Maine|\
USA:MH:Marshall Islands|\
USA:MI:Michigan|\
USA:MN:Minnesota|\
USA:MO:Missouri|\
USA:MP:Marianas|\
USA:MS:Mississippi|\
USA:MT:Montana|\
USA:NC:North Carolina|\
USA:ND:North Dakota|\
USA:NE:Nebraska|\
USA:NH:New Hampshire|\
USA:NJ:New Jersey|\
USA:NM:New Mexico|\
USA:NV:Nevada|\
USA:NY:New York|\
USA:OH:Ohio|\
USA:OK:Oklahoma|\
USA:OR:Oregon|\
USA:PA:Pennsylvania|\
USA:PR:Puerto Rico|\
USA:PW:Palau|\
USA:RI:Rhode Island|\
USA:SC:South Carolina|\
USA:SD:South Dakota|\
USA:TN:Tennessee|\
USA:TX:Texas|\
USA:UT:Utah|\
USA:VA:Virginia|\
USA:VI:Virgin Islands|\
USA:VT:Vermont|\
USA:WA:Washington|\
USA:WI:Wisconsin|\
USA:WV:West Virginia|\
USA:WY:Wyoming|\
USA:AA:Military Americas|\
USA:AE:Military Europe/ME/Canada|\
USA:AP:Military Pacific|\
Canada:AB:Alberta|\
Canada:MB:Manitoba|\
Canada:AB:Alberta|\
Canada:BC:British Columbia|\
Canada:MB:Manitoba|\
Canada:NB:New Brunswick|\
Canada:NL:Newfoundland and Labrador|\
Canada:NS:Nova Scotia|\
Canada:NT:Northwest Territories|\
Canada:NU:Nunavut|\
Canada:ON:Ontario|\
Canada:PE:Prince Edward Island|\
Canada:QC:Quebec|\
Canada:SK:Saskatchewan|\
Canada:YT:Yukon Territory|\
Australia:AAT:Australian Antarctic Territory|\
Australia:ACT:Australian Capital Territory|\
Australia:NT:Northern Territory|\
Australia:NSW:New South Wales|\
Australia:QLD:Queensland|\
Australia:SA:South Australia|\
Australia:TAS:Tasmania|\
Australia:VIC:Victoria|\
Australia:WA:Western Australia|\
Brazil:AC:Acre|\
Brazil:AL:Alagoas|\
Brazil:AM:Amazonas|\
Brazil:AP:Amapa|\
Brazil:BA:Baia|\
Brazil:CE:Ceara|\
Brazil:DF:Distrito Federal|\
Brazil:ES:Espirito Santo|\
Brazil:FN:Fernando de Noronha|\
Brazil:GO:Goias|\
Brazil:MA:Maranhao|\
Brazil:MG:Minas Gerais|\
Brazil:MS:Mato Grosso do Sul|\
Brazil:MT:Mato Grosso|\
Brazil:PA:Para|\
Brazil:PB:Paraiba|\
Brazil:PE:Pernambuco|\
Brazil:PI:Piaui|\
Brazil:PR:Parana|\
Brazil:RJ:Rio de Janeiro|\
Brazil:RN:Rio Grande do Norte|\
Brazil:RO:Rondonia|\
Brazil:RR:Roraima|\
Brazil:RS:Rio Grande do Sul|\
Brazil:SC:Santa Catarina|\
Brazil:SE:Sergipe|\
Brazil:SP:Sao Paulo|\
Brazil:TO:Tocatins|\
Netherlands:DR:Drente|\
Netherlands:FL:Flevoland|\
Netherlands:FR:Friesland|\
Netherlands:GL:Gelderland|\
Netherlands:GR:Groningen|\
Netherlands:LB:Limburg|\
Netherlands:NB:Noord Brabant|\
Netherlands:NH:Noord Holland|\
Netherlands:OV:Overijssel|\
Netherlands:UT:Utrecht|\
Netherlands:ZH:Zuid Holland|\
Netherlands:ZL:Zeeland|\
United Kingdom:AVON:Avon|\
United Kingdom:BEDS:Bedfordshire|\
United Kingdom:BERKS:Berkshire|\
United Kingdom:BUCKS:Buckinghamshire|\
United Kingdom:CAMBS:Cambridgeshire|\
United Kingdom:CHESH:Cheshire|\
United Kingdom:CLEVE:Cleveland|\
United Kingdom:CORN:Cornwall|\
United Kingdom:CUMB:Cumbria|\
United Kingdom:DERBY:Derbyshire|\
United Kingdom:DEVON:Devon|\
United Kingdom:DORSET:Dorset|\
United Kingdom:DURHAM:Durham|\
United Kingdom:ESSEX:Essex|\
United Kingdom:GREATBRIT:Great Britain|\
United Kingdom:GLOUS:Gloucestershire|\
United Kingdom:GLONDON:Greater London|\
United Kingdom:GMANCH:Greater Manchester|\
United Kingdom:HANTS:Hampshire|\
United Kingdom:HERWOR:Hereford & Worcestershire|\
United Kingdom:HERTS:Hertfordshire|\
United Kingdom:HUMBER:Humberside|\
United Kingdom:IOM:Isle of Man|\
United Kingdom:IOW:Isle of Wight|\
United Kingdom:KENT:Kent|\
United Kingdom:LANCS:Lancashire|\
United Kingdom:LEICS:Leicestershire|\
United Kingdom:LINCS:Lincolnshire|\
United Kingdom:MERSEY:Merseyside|\
United Kingdom:NORF:Norfolk|\
United Kingdom:NHANTS:Northamptonshire|\
United Kingdom:NTHUMB:Northumberland|\
United Kingdom:NOTTS:Nottinghamshire|\
United Kingdom:OXON:Oxfordshire|\
United Kingdom:SHROPS:Shropshire|\
United Kingdom:SOM:Somerset|\
United Kingdom:STAFFS:Staffordshire|\
United Kingdom:SUFF:Suffolk|\
United Kingdom:SURREY:Surrey|\
United Kingdom:SUSS:Sussex|\
United Kingdom:WARKS:Warwickshire|\
United Kingdom:WMID:West Midlands|\
United Kingdom:WILTS:Wiltshire|\
United Kingdom:YORK:Yorkshire|\
Ireland:CO ANTRIM:County Antrim|\
Ireland:CO ARMAGH:County Armagh|\
Ireland:CO DOWN:County Down|\
Ireland:CO FERMANAGH:County Fermanagh|\
Ireland:CO DERRY:County Londonderry|\
Ireland:CO TYRONE:County Tyrone|\
Ireland:CO CAVAN:County Cavan|\
Ireland:CO DONEGAL:County Donegal|\
Ireland:CO MONAGHAN:County Monaghan|\
Ireland:CO DUBLIN:County Dublin|\
Ireland:CO CARLOW:County Carlow|\
Ireland:CO KILDARE:County Kildare|\
Ireland:CO KILKENNY:County Kilkenny|\
Ireland:CO LAOIS:County Laois|\
Ireland:CO LONGFORD:County Longford|\
Ireland:CO LOUTH:County Louth|\
Ireland:CO MEATH:County Meath|\
Ireland:CO OFFALY:County Offaly|\
Ireland:CO WESTMEATH:County Westmeath|\
Ireland:CO WEXFORD:County Wexford|\
Ireland:CO WICKLOW:County Wicklow|\
Ireland:CO GALWAY:County Galway|\
Ireland:CO MAYO:County Mayo|\
Ireland:CO LEITRIM:County Leitrim|\
Ireland:CO ROSCOMMON:County Roscommon|\
Ireland:CO SLIGO:County Sligo|\
Ireland:CO CLARE:County Clare|\
Ireland:CO CORK:County Cork|\
Ireland:CO KERRY:County Kerry|\
Ireland:CO LIMERICK:County Limerick|\
Ireland:CO TIPPERARY:County Tipperary|\
Ireland:CO WATERFORD:County Waterford|\
';

// Country data table
//
// 
// To edit the list, just delete a line or add a line.  Order is important.  The order 
// displayed is the order it appears on the drop down.
//
var country = '\
Afghanistan:Afghanistan|\
Albania:Albania|\
Algeria:Algeria|\
American Samoa:American Samoa|\
Andorra:Andorra|\
Angola:Angola|\
Anguilla:Anguilla|\
Antarctica:Antarctica|\
Antigua and Barbuda:Antigua and Barbuda|\
Argentina:Argentina|\
Armenia:Armenia|\
Aruba:Aruba|\
Australia:Australia|\
Austria:Austria|\
Azerbaijan:Azerbaijan|\
Azores:Azores|\
Bahamas:Bahamas|\
Bahrain:Bahrain|\
Bangladesh:Bangladesh|\
Barbados:Barbados|\
Belarus:Belarus|\
Belgium:Belgium|\
Belize:Belize|\
Benin:Benin|\
Bermuda:Bermuda|\
Bhutan:Bhutan|\
Bolivia:Bolivia|\
Bosnia And Herzegowina:Bosnia And Herzegowina|\
Bosnia-Herzegovina:Bosnia-Herzegovina|\
Botswana:Botswana|\
Bouvet Island:Bouvet Island|\
Brazil:Brazil|\
British Indian Ocean Territory:British Indian Ocean Territory|\
British Virgin Islands:British Virgin Islands|\
Brunei Darussalam:Brunei Darussalam|\
Bulgaria:Bulgaria|\
Burkina Faso:Burkina Faso|\
Burundi:Burundi|\
Cambodia:Cambodia|\
Cameroon:Cameroon|\
Canada:Canada|\
Cape Verde:Cape Verde|\
Cayman Islands:Cayman Islands|\
Central African Republic:Central African Republic|\
Chad:Chad|\
Chile:Chile|\
China:China|\
Christmas Island:Christmas Island|\
Cocos (Keeling) Islands:Cocos (Keeling) Islands|\
Colombia:Colombia|\
Comoros:Comoros|\
Congo:Congo|\
Congo, The Democratic Republic:Congo, The Democratic Republic O|\
Cook Islands:Cook Islands|\
Corsica:Corsica|\
Costa Rica:Costa Rica|\
Cote d` Ivoire (Ivory Coast):Cote d` Ivoire (Ivory Coast)|\
Croatia:Croatia|\
Cuba:Cuba|\
Cyprus:Cyprus|\
Czech Republic:Czech Republic|\
Denmark:Denmark|\
Djibouti:Djibouti|\
Dominica:Dominica|\
Dominican Republic:Dominican Republic|\
East Timor:East Timor|\
Ecuador:Ecuador|\
Egypt:Egypt|\
El Salvador:El Salvador|\
Equatorial Guinea:Equatorial Guinea|\
Eritrea:Eritrea|\
Estonia:Estonia|\
Ethiopia:Ethiopia|\
Falkland Islands (Malvinas):Falkland Islands (Malvinas)|\
Faroe Islands:Faroe Islands|\
Fiji:Fiji|\
Finland:Finland|\
France (Includes Monaco):France (Includes Monaco)|\
France, Metropolitan:France, Metropolitan|\
French Guiana:French Guiana|\
French Polynesia:French Polynesia|\
French Polynesia (Tahiti):French Polynesia (Tahiti)|\
French Southern Territories:French Southern Territories|\
Gabon:Gabon|\
Gambia:Gambia|\
Georgia:Georgia|\
Germany:Germany|\
Ghana:Ghana|\
Gibraltar:Gibraltar|\
Greece:Greece|\
Greenland:Greenland|\
Grenada:Grenada|\
Guadeloupe:Guadeloupe|\
Guam:Guam|\
Guatemala:Guatemala|\
Guinea:Guinea|\
Guinea-Bissau:Guinea-Bissau|\
Guyana:Guyana|\
Haiti:Haiti|\
Heard And Mc Donald Islands:Heard And Mc Donald Islands|\
Holy See (Vatican City State):Holy See (Vatican City State)|\
Honduras:Honduras|\
Hong Kong:Hong Kong|\
Hungary:Hungary|\
Iceland:Iceland|\
India:India|\
Indonesia:Indonesia|\
Iran:Iran|\
Iraq:Iraq|\
Ireland:Ireland|\
Israel:Israel|\
Italy:Italy|\
Jamaica:Jamaica|\
Japan:Japan|\
Jordan:Jordan|\
Kazakhstan:Kazakhstan|\
Kenya:Kenya|\
Kiribati:Kiribati|\
Korea, Democratic People\'S Repub:Korea, Democratic People\'S Repub|\
Kuwait:Kuwait|\
Kyrgyzstan:Kyrgyzstan|\
Laos:Laos|\
Latvia:Latvia|\
Lebanon:Lebanon|\
Lesotho:Lesotho|\
Liberia:Liberia|\
Libya:Libya|\
Liechtenstein:Liechtenstein|\
Lithuania:Lithuania|\
Luxembourg:Luxembourg|\
Macao:Macao|\
Macedonia:Macedonia|\
Madagascar:Madagascar|\
Madeira Islands:Madeira Islands|\
Malawi:Malawi|\
Malaysia:Malaysia|\
Maldives:Maldives|\
Mali:Mali|\
Malta:Malta|\
Marshall Islands:Marshall Islands|\
Martinique:Martinique|\
Mauritania:Mauritania|\
Mauritius:Mauritius|\
Mayotte:Mayotte|\
Mexico:Mexico|\
Micronesia, Federated States Of:Micronesia, Federated States Of|\
Moldova, Republic Of:Moldova, Republic Of|\
Monaco:Monaco|\
Mongolia:Mongolia|\
Montserrat:Montserrat|\
Morocco:Morocco|\
Mozambique:Mozambique|\
Myanmar (Burma):Myanmar (Burma)|\
Namibia:Namibia|\
Nauru:Nauru|\
Nepal:Nepal|\
Netherlands:Netherlands|\
Netherlands Antilles:Netherlands Antilles|\
New Caledonia:New Caledonia|\
New Zealand:New Zealand|\
Nicaragua:Nicaragua|\
Niger:Niger|\
Nigeria:Nigeria|\
Niue:Niue|\
Norfolk Island:Norfolk Island|\
Northern Mariana Islands:Northern Mariana Islands|\
Norway:Norway|\
Oman:Oman|\
Pakistan:Pakistan|\
Palau:Palau|\
Palestinian Territory, Occupied:Palestinian Territory, Occupied|\
Panama:Panama|\
Papua New Guinea:Papua New Guinea|\
Paraguay:Paraguay|\
Peru:Peru|\
Philippines:Philippines|\
Pitcairn:Pitcairn|\
Poland:Poland|\
Portugal:Portugal|\
Puerto Rico:Puerto Rico|\
Qatar:Qatar|\
Reunion:Reunion|\
Romania:Romania|\
Russian Federation:Russian Federation|\
Rwanda:Rwanda|\
Saint Kitts And Nevis:Saint Kitts And Nevis|\
San Marino:San Marino|\
Sao Tome and Principe:Sao Tome and Principe|\
Saudi Arabia:Saudi Arabia|\
Senegal:Senegal|\
Serbia-Montenegro:Serbia-Montenegro|\
Seychelles:Seychelles|\
Sierra Leone:Sierra Leone|\
Singapore:Singapore|\
Slovak Republic:Slovak Republic|\
Slovenia:Slovenia|\
Solomon Islands:Solomon Islands|\
Somalia:Somalia|\
South Africa:South Africa|\
South Georgia And The South Sand:South Georgia And The South Sand|\
South Korea:South Korea|\
Spain:Spain|\
Sri Lanka:Sri Lanka|\
St. Christopher and Nevis:St. Christopher and Nevis|\
St. Helena:St. Helena|\
St. Lucia:St. Lucia|\
St. Pierre and Miquelon:St. Pierre and Miquelon|\
St. Vincent and the Grenadines:St. Vincent and the Grenadines|\
Sudan:Sudan|\
Suriname:Suriname|\
Svalbard And Jan Mayen Islands:Svalbard And Jan Mayen Islands|\
Swaziland:Swaziland|\
Sweden:Sweden|\
Switzerland:Switzerland|\
Syrian Arab Republic:Syrian Arab Republic|\
Taiwan:Taiwan|\
Tajikistan:Tajikistan|\
Tanzania:Tanzania|\
Thailand:Thailand|\
Togo:Togo|\
Tokelau:Tokelau|\
Tonga:Tonga|\
Trinidad and Tobago:Trinidad and Tobago|\
Tristan da Cunha:Tristan da Cunha|\
Tunisia:Tunisia|\
Turkey:Turkey|\
Turkmenistan:Turkmenistan|\
Turks and Caicos Islands:Turks and Caicos Islands|\
Tuvalu:Tuvalu|\
Uganda:Uganda|\
Ukraine:Ukraine|\
United Arab Emirates:United Arab Emirates|\
United Kingdom:United Kingdom|\
USA:USA|\
USA Minor Outlying Isl:USA Minor Outlying Isl|\
Uruguay:Uruguay|\
Uzbekistan:Uzbekistan|\
Vanuatu:Vanuatu|\
Vatican City:Vatican City|\
Venezuela:Venezuela|\
Vietnam:Vietnam|\
Virgin Islands (U.S.):Virgin Islands (U.S.)|\
Wallis and Furuna Islands:Wallis and Furuna Islands|\
Western Sahara:Western Sahara|\
Western Samoa:Western Samoa|\
Yemen:Yemen|\
Yugoslavia:Yugoslavia|\
Zaire:Zaire|\
Zambia:Zambia|\
Zimbabwe:Zimbabwe|\
';

function TrimString(sInString) {
   
   if ( sInString ) {

      sInString = sInString.replace( /^\s+/g, "" );// strip leading
      return sInString.replace( /\s+$/g, "" );// strip trailing
   }
}
// Populates the country select with the counties from the country list
//
function populateCountry(defaultCountry) {

   if ( postCountry != '' ) {

      defaultCountry = postCountry;
   }

   var countryLineArray = country.split('|');      // Split into lines

   var selObj = document.getElementById('countrySelect');

   selObj.options[0] = new Option('Select Country','');
   selObj.selectedIndex = 0;

   for (var loop = 0; loop < countryLineArray.length; loop++) {

      lineArray = countryLineArray[loop].split(':');

      countryCode  = TrimString(lineArray[0]);
      countryName  = TrimString(lineArray[1]);
   
      if ( countryCode != '' ) {

         selObj.options[loop + 1] = new Option(countryName, countryCode);
      }

      if ( defaultCountry == countryCode ) {

         selObj.selectedIndex = loop + 1;
      }
   }
}
function populateState() {

   var selObj = document.getElementById('stateSelect');
   var foundState = false;
   
   // Empty options just in case new drop down is shorter
   //
   if ( selObj.type == 'select-one' ) {

      selObj.options.length = 0;

      selObj.options[0] = new Option('ALL','');
      selObj.selectedIndex = 0;
   }
   // Populate the drop down with states from the selected country
   //
   var stateLineArray   = state.split("|");        // Split into lines

   var optionCntr = 1;

   for (var loop = 0; loop < stateLineArray.length; loop++) {

      lineArray = stateLineArray[loop].split(":");

      countryCode  = TrimString(lineArray[0]);
      stateCode    = TrimString(lineArray[1]);
      stateName    = TrimString(lineArray[2]);

      if ( document.getElementById('countrySelect').value == countryCode && countryCode != '' ) {

         // If it's a input element, change it to a select
         //
         if ( selObj.type == 'text' ) {

            parentObj = document.getElementById('stateSelect').parentNode;
            parentObj.removeChild(selObj);

            var inputSel = document.createElement("SELECT");
            inputSel.setAttribute("name","state"); 
            inputSel.setAttribute("id","stateSelect"); 

            parentObj.appendChild(inputSel) ;

            selObj = document.getElementById('stateSelect');
            selObj.options[0] = new Option('Select State','');
            selObj.selectedIndex = 0;
         }

         if ( stateCode != '' ) {

            selObj.options[optionCntr] = new Option(stateName, stateCode);
         }
         // See if it's selected from a previous post
         //
         if ( stateCode == postState && countryCode == postCountry ) {

            selObj.selectedIndex = optionCntr;
         }
         foundState = true;
         optionCntr++
      }
   }
   // If the country has no states, change the select to a text box
   //
   if ( ! foundState ) {

      parentObj = document.getElementById('stateSelect').parentNode;
      parentObj.removeChild(selObj);
 
      // Create the Input Field
      var inputEl = document.createElement("INPUT");

      inputEl.setAttribute("id", "stateSelect"); 
      inputEl.setAttribute("type", "text"); 
      inputEl.setAttribute("name", "state"); 
      inputEl.setAttribute("size", 20); 
      inputEl.setAttribute("value", postState); 
      parentObj.appendChild(inputEl) ;
   }
   
}
function initCountry(country) {

   populateCountry(country);
   populateState();
}


