/* This script and many more are available free online at The JavaScript Source :: http://javascript.internet.com Created by: Down Home Consulting :: http://downhomeconsulting.com */ /* Country State Drop Downs v1.0. (c) Copyright 2005 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. */ // If you have PHP you can set the post values like this //var postState = ''; //var postCountry = ''; var postState = 'US'; var postCountry = 'FL'; // State table // // To edit the list, just delete a line or add a line. Order is important. // The order displayed here is the order it appears on the drop down. // var stateList = '\ US:AL:Alabama|\ US:AK:Alaska|\ CA:AB:Alberta|\ US:AZ:Arizona|\ US:AR:Arkansas|\ US:AA:Armed Forces Americas|\ US:AE:Armed Forces Europe|\ US:AP:Armed Forces Pacific|\ CA:BC:British Columbia|\ US:CA:California|\ US:CO:Colorado|\ US:CT:Connecticut|\ US:DE:Delaware|\ US:DC:District of Columbia|\ US:FL:Florida|\ US:GA:Georgia|\ 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|\ CA:MB:Manitoba|\ 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|\ CA:NB:New Brunswick|\ US:NH:New Hampshire|\ US:NJ:New Jersey|\ US:NM:New Mexico|\ US:NY:New York|\ CA:NL:Newfoundland and Labrador|\ US:NC:North Carolina|\ US:ND:North Dakota|\ CA:NT:Northwest Territories|\ CA:NS:Nova Scotia|\ CA:NU:Nunavut|\ US:OH:Ohio|\ US:OK:Oklahoma|\ CA:ON:Ontario|\ US:OR:Oregon|\ US:PA:Pennsylvania|\ CA:PE:Prince Edward Island|\ US:PR:Puerto Rico|\ CA:QC:Quebec|\ US:RI:Rhode Island|\ CA:SK:Saskatchewan|\ 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|\ CA:YT:Yukon Territory|\ '; // Country data table // // To edit the list, just delete a line or add a line. Order is important. // The order displayed here is the order it appears on the drop down. // var country = '\ US:United States|\ '; country = country.substring(0, country.length-1) // Deleting the last "|" character function get_Element(i) { return document.getElementById(i) || document.getElementsByName(i).item(0); } function TrimString(sInString) { if ( sInString ) { sInString = sInString.replace( /^\s+/g, "" );// strip leading return sInString.replace( /\s+$/g, "" );// strip trailing } } // Populates the country selected with the counties from the country list function populateCountry(defaultCountry,countryfieldname) { if (defaultCountry=="") {defaultCountry="US"} var countryLineArray = country.split('|'); // Split into lines var selObj = get_Element(countryfieldname); selObj.options[0] = new Option('',''); 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] = new Option(countryName, countryCode); } if ( defaultCountry == countryCode ) { selObj.selectedIndex = loop; } } } function populateState(statefieldname,countryfieldname,state1,optionalCreateTextField) { //optionalCreateTextField = optional parameter, true/false. When a country doesn't have state, it'll determine if the code creates a text field for the state or just let the dropdown empty. var isOpera = false, isIE = false; var strClassName; if(typeof(window.opera) != 'undefined') {isOpera = true;} if(!isOpera && (navigator.userAgent.indexOf('Internet Explorer') > 0 || navigator.userAgent.indexOf('MSIE') > 0)) {isIE = true;} if (state1==undefined) {state1='';} postCountry=get_Element(countryfieldname); postCountry=postCountry.value; var selObj = get_Element(statefieldname); 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 = stateList.split("|"); // Split into lines var optionCntr = 0; 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 ( get_Element(countryfieldname).value == countryCode && countryCode != '' ) { // If it's a input element, change it to a select // if ( selObj.type == 'text' ) { strClassName = selObj.className; parentObj = get_Element(statefieldname).parentNode; parentObj.removeChild(selObj); // Create the Input Field if(!isIE){ var inputSel = document.createElement("SELECT"); inputSel.setAttribute("name", statefieldname); if (strClassName != 'undefined' && strClassName != '') inputSel.setAttribute("class",strClassName); else inputSel.setAttribute("class","txtBoxStyle"); inputSel.setAttribute("id", statefieldname); } else { var inputSel = document.createElement(""); if (strClassName != 'undefined' && strClassName != '') inputEl.setAttribute("className",strClassName); else inputEl.setAttribute("className","txtBoxStyle"); } inputEl.setAttribute("type", "text"); inputEl.setAttribute("size", 20); inputEl.setAttribute("value", state1); parentObj.appendChild(inputEl); } } } } function insertCountry(strCountryName,countryfieldname) { var selObj = get_Element(countryfieldname); selObj.options[selObj.options.length] = new Option('ALL','ALL'); selObj.selectedIndex = selObj.options.length-1; } function initCountry(country,state1,statefieldname,countryfieldname) { populateCountry(country,countryfieldname); populateState(statefieldname,countryfieldname,state1); } function GetValue(formx,name1) { //alert(name1); var i; for(i=0;i