/* Country State Drop Downs v1.1. (c) Copyright 2006 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 paste the html between the form tags from demo2.html or demo.html into your page. */ // 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 = '\ AT:BL:Burgenland|\ AT:KN:Kärnten|\ AT:NO:Niederösterreich|\ AT:OO:Oberösterreich|\ AT:SB:Salzburg|\ AT:ST:Steiermark|\ AT:TI:Tirol|\ AT:VB:Voralberg|\ AT:WI:Wien|\ CA:AB:Alberta|\ CA:BC:British Columbia|\ CA:MB:Manitoba|\ CA:NB:New Brunswick|\ CA:NF:Newfoundland|\ CA:NT:Northwest Territories|\ CA:NS:Nova Scotia|\ CA:NU:Nunavut|\ CA:ON:Ontario|\ CA:PE:Prince Edward Island|\ CA:QC:Quebec|\ CA:SK:Saskatchewan|\ CA:YT:Yukon Territory|\ DE:BAW:Baden-Württemberg|\ DE:BAV:Bavaria|\ DE:BAY:Bayern|\ DE:BER:Berlin|\ DE:BRG:Brandenburg|\ DE:BRE:Bremen|\ DE:HAM:Hamburg|\ DE:HES:Hessen|\ DE:MEC:Mecklenburg-Vorpommern|\ DE:NDS:Niedersachsen|\ DE:NRW:Nordrhein-Westfalen|\ DE:RHE:Rheinland-Pfalz|\ DE:SAR:Saarland|\ DE:SAS:Sachsen|\ DE:SAC:Sachsen-Anhalt|\ DE:SCN:Schleswig-Holstein|\ DE:THE:Thüringen|\ ES:A Coruña:A Coruña|\ ES:Alava:Alava|\ ES:Albacete:Albacete|\ ES:Alicante:Alicante|\ ES:Almeria:Almeria|\ ES:Asturias:Asturias|\ ES:Avila:Avila|\ ES:Badajoz:Badajoz|\ ES:Baleares:Baleares|\ ES:Barcelona:Barcelona|\ ES:Burgos:Burgos|\ ES:Caceres:Caceres|\ ES:Cadiz:Cadiz|\ ES:Cantabria:Cantabria|\ ES:Castellon:Castellon|\ ES:Ceuta:Ceuta|\ ES:Ciudad Real:Ciudad Real|\ ES:Cordoba:Cordoba|\ ES:Cuenca:Cuenca|\ ES:Girona:Girona|\ ES:Granada:Granada|\ ES:Guadalajara:Guadalajara|\ ES:Guipuzcoa:Guipuzcoa|\ ES:Huelva:Huelva|\ ES:Huesca:Huesca|\ ES:Jaen:Jaen|\ ES:La Rioja:La Rioja|\ ES:Las Palmas:Las Palmas|\ ES:Leon:Leon|\ ES:Lleida:Lleida|\ ES:Lugo:Lugo|\ ES:Madrid:Madrid|\ ES:Malaga:Malaga|\ ES:Melilla:Melilla|\ ES:Murcia:Murcia|\ ES:Navarra:Navarra|\ ES:Ourense:Ourense|\ ES:Palencia:Palencia|\ ES:Pontevedra:Pontevedra|\ ES:Salamanca:Salamanca|\ ES:Santa Cruz de Tenerife:Santa Cruz de Tenerife|\ ES:Segovia:Segovia|\ ES:Sevilla:Sevilla|\ ES:Soria:Soria|\ ES:Tarragona:Tarragona|\ ES:Teruel:Teruel|\ ES:Toledo:Toledo|\ ES:Valencia:Valencia|\ ES:Valladolid:Valladolid|\ ES:Vizcaya:Vizcaya|\ ES:Zamora:Zamora|\ ES:Zaragoza:Zaragoza|\ CH:AG:Aargau|\ CH:AR:Appenzell Ausserrhoden|\ CH:AI:Appenzell Innerrhoden|\ CH:BL:Basel-Landschaft|\ CH:BS:Basel-Stadt|\ CH:BE:Bern|\ CH:FR:Freiburg|\ CH:GE:Genf|\ CH:GL:Glarus|\ CH:JU:Graubünden|\ CH:JU:Jura|\ CH:LU:Luzern|\ CH:NE:Neuenburg|\ CH:NW:Nidwalden|\ CH:OW:Obwalden|\ CH:SH:Schaffhausen|\ CH:SZ:Schwyz|\ CH:SO:Solothurn|\ CH:SG:St. Gallen|\ CH:TI:Tessin|\ CH:TG:Thurgau|\ CH:UR:Uri|\ CH:VD:Waadt|\ CH:VS:Wallis|\ CH:ZG:Zug|\ CH:ZH:Zürich|\ US:AL:Alabama|\ US:AK:Alaska|\ US:AS:American Samoa|\ US:AZ:Arizona|\ US:AR:Arkansas|\ US:AF:Armed Forces Africa|\ US:AA:Armed Forces Americas|\ US:AC:Armed Forces Canada|\ US:AE:Armed Forces Europe|\ US:AM:Armed Forces Middle East|\ US:AP:Armed Forces Pacific|\ US:CA:California|\ US:CO:Colorado|\ US:CT:Connecticut|\ US:DE:Delaware|\ US:DC:District of Columbia|\ US:FM:Federated States Of Micronesia|\ US:FL:Florida|\ US:GA:Georgia|\ US:GU:Guam|\ US:HI:Hawaii|\ US:ID:Idaho|\ US:IL:Illinois|\ US:IN:Indiana|\ US:IA:Iowa|\ US:KS:Kansas|\ US:KY:Kentucky|\ US:LA:Louisiana|\ US:ME:Maine|\ US:MH:Marshall Islands|\ US:MD:Maryland|\ US:MA:Massachusetts|\ US:MI:Michigan|\ US:MN:Minnesota|\ US:MS:Mississippi|\ US:MO:Missouri|\ US:MT:Montana|\ US:NE:Nebraska|\ US:NV:Nevada|\ US:NH:New Hampshire|\ US:NJ:New Jersey|\ US:NM:New Mexico|\ US:NY:New York|\ US:NC:North Carolina|\ US:ND:North Dakota|\ US:MP:Northern Mariana Islands|\ US:OH:Ohio|\ US:OK:Oklahoma|\ US:OR:Oregon|\ US:PW:Palau|\ US:PA:Pennsylvania|\ US:PR:Puerto Rico|\ US:RI:Rhode Island|\ US:SC:South Carolina|\ US:SD:South Dakota|\ US:TN:Tennessee|\ US:TX:Texas|\ US:UT:Utah|\ US:VT:Vermont|\ US:VI:Virgin Islands|\ US:VA:Virginia|\ US:WA:Washington|\ US:WV:West Virginia|\ US:WI:Wisconsin|\ US:WY:Wyoming|\ '; // 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 = '\ AF:Afghanistan|\ AL:Albania|\ DZ:Algeria|\ AS:American Samoa|\ AD:Andorra|\ AO:Angola|\ AI:Anguilla|\ AQ:Antarctica|\ AG:Antigua and Barbuda|\ AR:Argentina|\ AM:Armenia|\ AW:Aruba|\ AU:Australia|\ AT:Austria|\ AZ:Azerbaijan|\ BS:Bahamas|\ BH:Bahrain|\ BD:Bangladesh|\ BB:Barbados|\ BY:Belarus|\ BE:Belgium|\ BZ:Belize|\ BJ:Benin|\ BM:Bermuda|\ BT:Bhutan|\ BO:Bolivia|\ BA:Bosnia and Herzegowina|\ BW:Botswana|\ BV:Bouvet Island|\ BR:Brazil|\ IO:British Indian Ocean Territory|\ BN:Brunei Darussalam|\ BG:Bulgaria|\ BF:Burkina Faso|\ BI:Burundi|\ KH:Cambodia|\ CM:Cameroon|\ CA:Canada|\ CV:Cape Verde|\ KY:Cayman Islands|\ CF:Central African Republic|\ TD:Chad|\ CL:Chile|\ CN:China|\ CX:Christmas Island|\ CC:Cocos (Keeling) Islands|\ CO:Colombia|\ KM:Comoros|\ CG:Congo|\ CK:Cook Islands|\ CR:Costa Rica|\ CI:Cote D\'Ivoire|\ HR:Croatia|\ CU:Cuba|\ CY:Cyprus|\ CZ:Czech Republic|\ DK:Denmark|\ DJ:Djibouti|\ DM:Dominica|\ DO:Dominican Republic|\ TP:East Timor|\ EC:Ecuador|\ EG:Egypt|\ SV:El Salvador|\ GQ:Equatorial Guinea|\ ER:Eritrea|\ EE:Estonia|\ ET:Ethiopia|\ FK:Falkland Islands (Malvinas)|\ FO:Faroe Islands|\ FJ:Fiji|\ FI:Finland|\ FR:France|\ FX:France, Metropolitan|\ GF:French Guiana|\ PF:French Polynesia|\ TF:French Southern Territories|\ GA:Gabon|\ GM:Gambia|\ GE:Georgia|\ DE:Germany|\ GH:Ghana|\ GI:Gibraltar|\ GR:Greece|\ GL:Greenland|\ GD:Grenada|\ GP:Guadeloupe|\ GU:Guam|\ GT:Guatemala|\ GN:Guinea|\ GW:Guinea-bissau|\ GY:Guyana|\ HT:Haiti|\ HM:Heard and Mc Donald Islands|\ HN:Honduras|\ HK:Hong Kong|\ HU:Hungary|\ IS:Iceland|\ IN:India|\ ID:Indonesia|\ IR:Iran (Islamic Republic of)|\ IQ:Iraq|\ IE:Ireland|\ IL:Israel|\ IT:Italy|\ JM:Jamaica|\ JP:Japan|\ JO:Jordan|\ KZ:Kazakhstan|\ KE:Kenya|\ KI:Kiribati|\ KP:Korea, Democratic People\'s Republic of|\ KR:Korea, Republic of|\ KW:Kuwait|\ KG:Kyrgyzstan|\ LA:Lao People\'s Democratic Republic|\ LV:Latvia|\ LB:Lebanon|\ LS:Lesotho|\ LR:Liberia|\ LY:Libyan Arab Jamahiriya|\ LI:Liechtenstein|\ LT:Lithuania|\ LU:Luxembourg|\ MO:Macau|\ MK:Macedonia, The Former Yugoslav Republic of|\ MG:Madagascar|\ MW:Malawi|\ MY:Malaysia|\ MV:Maldives|\ ML:Mali|\ MT:Malta|\ MH:Marshall Islands|\ MQ:Martinique|\ MR:Mauritania|\ MU:Mauritius|\ YT:Mayotte|\ MX:Mexico|\ FM:Micronesia, Federated States of|\ MD:Moldova, Republic of|\ MC:Monaco|\ MN:Mongolia|\ MS:Montserrat|\ MA:Morocco|\ MZ:Mozambique|\ MM:Myanmar|\ NA:Namibia|\ NR:Nauru|\ NP:Nepal|\ NL:Netherlands|\ AN:Netherlands Antilles|\ NC:New Caledonia|\ NZ:New Zealand|\ NI:Nicaragua|\ NE:Niger|\ NG:Nigeria|\ NU:Niue|\ NF:Norfolk Island|\ MP:Northern Mariana Islands|\ NO:Norway|\ OM:Oman|\ PK:Pakistan|\ PW:Palau|\ PA:Panama|\ PG:Papua New Guinea|\ PY:Paraguay|\ PE:Peru|\ PH:Philippines|\ PN:Pitcairn|\ PL:Poland|\ PT:Portugal|\ PR:Puerto Rico|\ QA:Qatar|\ RE:Reunion|\ RO:Romania|\ RU:Russian Federation|\ RW:Rwanda|\ KN:Saint Kitts and Nevis|\ LC:Saint Lucia|\ VC:Saint Vincent and the Grenadines|\ WS:Samoa|\ SM:San Marino|\ ST:Sao Tome and Principe|\ SA:Saudi Arabia|\ SN:Senegal|\ SC:Seychelles|\ SL:Sierra Leone|\ SG:Singapore|\ SK:Slovakia (Slovak Republic)|\ SI:Slovenia|\ SB:Solomon Islands|\ SO:Somalia|\ ZA:South Africa|\ GS:South Georgia and the South Sandwich Islands|\ ES:Spain|\ LK:Sri Lanka|\ SH:St. Helena|\ PM:St. Pierre and Miquelon|\ SD:Sudan|\ SR:Suriname|\ SJ:Svalbard and Jan Mayen Islands|\ SZ:Swaziland|\ SE:Sweden|\ CH:Switzerland|\ SY:Syrian Arab Republic|\ TW:Taiwan|\ TJ:Tajikistan|\ TZ:Tanzania, United Republic of|\ TH:Thailand|\ TG:Togo|\ TK:Tokelau|\ TO:Tonga|\ TT:Trinidad and Tobago|\ TN:Tunisia|\ TR:Turkey|\ TM:Turkmenistan|\ TC:Turks and Caicos Islands|\ TV:Tuvalu|\ UG:Uganda|\ UA:Ukraine|\ AE:United Arab Emirates|\ GB:United Kingdom|\ US:United States|\ UM:United States Minor Outlying Islands|\ UY:Uruguay|\ UZ:Uzbekistan|\ VU:Vanuatu|\ VA:Vatican City State (Holy See)|\ VE:Venezuela|\ VN:Viet Nam|\ VG:Virgin Islands (British)|\ VI:Virgin Islands (U.S.)|\ WF:Wallis and Futuna Islands|\ EH:Western Sahara|\ YE:Yemen|\ YU:Yugoslavia|\ ZR:Zaire|\ ZM:Zambia|\ ZW:Zimbabwe|\ '; // Save the country & state field names var countryFieldCfgArray = document.getElementById('cs_config_country_field').value.split(' '); var stateFieldCfgArray = document.getElementById('cs_config_state_field').value.split(' '); // Save the names of the fields that hold the country & state default values var countryDefaultCfgArray = document.getElementById('cs_config_country_default').value.split(' '); var stateDefaultCfgArray = document.getElementById('cs_config_state_default').value.split(' '); var defaultState = false; var defaultCountry = false; 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(idName) { var countryLineArray = country.split('|'); // Split into lines var selObj = document.getElementById( idName ); 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( statestateIdName, countryIdName ) { var selObj = document.getElementById( stateIdName ); 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('Select State',''); 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( countryIdName ).value == countryCode && countryCode != '' ) { // If it's a input element, change it to a select // if ( selObj.type == 'text' ) { parentObj = document.getElementById( stateIdName ).parentNode; parentObj.removeChild(selObj); var inputSel = document.createElement("SELECT"); inputSel.setAttribute("name",stateIdName); inputSel.setAttribute("id", stateIdName ); parentObj.appendChild(inputSel) ; selObj = document.getElementById( stateIdName ); 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 == defaultState && countryCode == defaultCountry ) { selObj.selectedIndex = optionCntr; } foundState = true; optionCntr++ } } // If the country has no states, change the select to a text box // if ( ! foundState ) { parentObj = document.getElementById( stateIdName ).parentNode; parentObj.removeChild(selObj); // Create the Input Field var inputEl = document.createElement("INPUT"); inputEl.setAttribute("id", stateIdName ); inputEl.setAttribute("type", "text"); inputEl.setAttribute("name", stateIdName); inputEl.setAttribute("size", 20); inputEl.setAttribute("value", defaultState); parentObj.appendChild(inputEl) ; } } // Called when state drop down is changed // function updateState( countryIdNameIn ) { for (var loop = 0; loop < countryFieldCfgArray.length; loop++) { countryIdName = countryFieldCfgArray[loop]; stateIdName = stateFieldCfgArray[loop]; // Read the default value hidden fields defaultCountry = document.getElementById( countryDefaultCfgArray[loop] ).value; defaultState = document.getElementById( stateDefaultCfgArray[loop] ).value; if ( countryIdNameIn == countryIdName ) { populateState( stateIdName, countryIdName ); } } } // Initialize the drop downs // function initCountry() { for (var loop = 0; loop < countryFieldCfgArray.length; loop++) { countryIdName = countryFieldCfgArray[loop]; stateIdName = stateFieldCfgArray[loop]; // Read the default value hidden fields defaultCountry = document.getElementById( countryDefaultCfgArray[loop] ).value; defaultState = document.getElementById( stateDefaultCfgArray[loop] ).value; populateCountry( countryIdName); populateState( stateIdName, countryIdName ); } }