/*
	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.

*/

var lastCountryCode="";
// 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 = '\
US:AK:Alaska|\
US:AL:Alabama|\
US:AR:Arkansas|\
US:AS:Amer Samoa|\
US:AZ:Arizona|\
US:CA:California|\
US:CO:Colorado|\
US:CT:Connecticut|\
US:DC:D.C.|\
US:DE:Delaware|\
US:FL:Florida|\
US:FM:Micronesia|\
US:GA:Georgia|\
US:GU:Guam|\
US:HI:Hawaii|\
US:IA:Iowa|\
US:ID:Idaho|\
US:IL:Illinois|\
US:IN:Indiana|\
US:KS:Kansas|\
US:KY:Kentucky|\
US:LA:Louisiana|\
US:MA:Massachusetts|\
US:MD:Maryland|\
US:ME:Maine|\
US:MH:Marshall Isl|\
US:MI:Michigan|\
US:MN:Minnesota|\
US:MO:Missouri|\
US:MP:Marianas|\
US:MS:Mississippi|\
US:MT:Montana|\
US:NC:North Carolina|\
US:ND:North Dakota|\
US:NE:Nebraska|\
US:NH:New Hampshire|\
US:NJ:New Jersey|\
US:NM:New Mexico|\
US:NV:Nevada|\
US:NY:New York|\
US:OH:Ohio|\
US:OK:Oklahoma|\
US:OR:Oregon|\
US:PA:Pennsylvania|\
US:PR:Puerto Rico|\
US:PW:Palau|\
US:RI:Rhode Island|\
US:SC:South Carolina|\
US:SD:South Dakota|\
US:TN:Tennessee|\
US:TX:Texas|\
US:UT:Utah|\
US:VA:Virginia|\
US:VI:Virgin Islands|\
US:VT:Vermont|\
US:WA:Washington|\
US:WI:Wisconsin|\
US:WV:West Virginia|\
US:WY:Wyoming|\
US:AA:Military Americas|\
US:AE:Military Eur Canada|\
US:AP:Military Pacific|\
CA:AB:Alberta|\
CA:MB:Manitoba|\
CA:AB:Alberta|\
CA:BC:British Columbia|\
CA:MB:Manitoba|\
CA:NB:New Brunswick|\
CA:NL:Newfoundlnd-Labrador|\
CA:NS:Nova Scotia|\
CA:NT:Northwest Terr|\
CA:NU:Nunavut|\
CA:ON:Ontario|\
CA:PE:Prince Edward Isl|\
CA:QC:Quebec|\
CA:SK:Saskatchewan|\
CA:YT:Yukon Territory|\
AU:AAT:Austr Antarctic Terr|\
AU:ACT:Austr Capital Terr|\
AU:NT:Northern Terr|\
AU:NSW:Nw South Wales|\
AU:QLD:Queensland|\
AU:SA:South Austr|\
AU:TAS:Tasmania|\
AU:VIC:Victoria|\
AU:WA:Western Austr|\
BR:AC:Acre|\
BR:AL:Alagoas|\
BR:AM:Amazonas|\
BR:AP:Amapa|\
BR:BA:Baia|\
BR:CE:Ceara|\
BR:DF:Distrito Fed|\
BR:ES:Espirito Santo|\
BR:FN:Fernando d Noronha|\
BR:GO:Goias|\
BR:MA:Maranhao|\
BR:MG:Minas Gerais|\
BR:MS:Mato Gr do Sul|\
BR:MT:Mato Grosso|\
BR:PA:Para|\
BR:PB:Paraiba|\
BR:PE:Pernambuco|\
BR:PI:Piaui|\
BR:PR:Parana|\
BR:RJ:Rio de Janeiro|\
BR:RN:Rio Grnde do No|\
BR:RO:Rondonia|\
BR:RR:Roraima|\
BR:RS:Rio Grande Sul|\
BR:SC:Santa Catarina|\
BR:SE:Sergipe|\
BR:SP:Sao Paulo|\
BR:TO:Tocatins|\
NL:DR:Drente|\
NL:FL:Flevoland|\
NL:FR:Friesland|\
NL:GL:Gelderland|\
NL:GR:Groningen|\
NL:LB:Limburg|\
NL:NB:Noord Brabant|\
NL:NH:Noord Holland|\
NL:OV:Overijssel|\
NL:UT:Utrecht|\
NL:ZH:Zuid Holland|\
NL:ZL:Zeeland|\
UK:AVON:Avon|\
UK:BEDS:Bedfordshire|\
UK:BERKS:Berkshire|\
UK:BUCKS:Bckinghmshre|\
UK:CAMBS:Cambrdgshre|\
UK:CHESH:Cheshire|\
UK:CLEVE:Cleveland|\
UK:CORN:Cornwall|\
UK:CUMB:Cumbria|\
UK:DERBY:Derbyshire|\
UK:DEVON:Devon|\
UK:DORSET:Dorset|\
UK:DURHAM:Durham|\
UK:ESSEX:Essex|\
UK:GLOUS:Gloucstrshre|\
UK:GLONDON:Greater London|\
UK:GMANCH:Greater Manchstr|\
UK:HANTS:Hampshire|\
UK:HERWOR:Hereford & Worch|\
UK:HERTS:Hertfordshire|\
UK:HUMBER:Humberside|\
UK:IOM:Isle of Man|\
UK:IOW:Isle of Wight|\
UK:KENT:Kent|\
UK:LANCS:Lancashire|\
UK:LEICS:Leicestershire|\
UK:LINCS:Lincolnshire|\
UK:MERSEY:Merseyside|\
UK:NORF:Norfolk|\
UK:NHANTS:Northamptnshre|\
UK:NTHUMB:Northumberland|\
UK:NOTTS:Nottinghamshire|\
UK:OXON:Oxfordshire|\
UK:SHROPS:Shropshire|\
UK:SOM:Somerset|\
UK:STAFFS:Staffordshire|\
UK:SUFF:Suffolk|\
UK:SURREY:Surrey|\
UK:SUSS:Sussex|\
UK:WARKS:Warwickshire|\
UK:WMID:West Midlands|\
UK:WILTS:Wiltshire|\
UK:YORK:Yorkshire|\
EI:CO ANTRIM:Cnty Antrim|\
EI:CO ARMAGH:Cnty Armagh|\
EI:CO DOWN:Cnty Down|\
EI:CO FERMANAGH:Cnty Frmngh|\
EI:CO DERRY:Cnty Londndry|\
EI:CO TYRONE:Cnty Tyrone|\
EI:CO CAVAN:Cnty Cavan|\
EI:CO DONEGAL:Cnty Donegal|\
EI:CO MONAGHAN:Cnty Mnghn|\
EI:CO DUBLIN:Cnty Dublin|\
EI:CO CARLOW:Cnty Carlow|\
EI:CO KILDARE:Cnty Kildare|\
EI:CO KILKENNY:Cnty Kilkny|\
EI:CO LAOIS:Cnty Laois|\
EI:CO LONGFORD:Cnty Longfrd|\
EI:CO LOUTH:Cnty Louth|\
EI:CO MEATH:Cnty Meath|\
EI:CO OFFALY:Cnty Offaly|\
EI:CO WESTMEATH:Cnty Westmth|\
EI:CO WEXFORD:Cnty Wexford|\
EI:CO WICKLOW:Cnty Wicklow|\
EI:CO GALWAY:Cnty Galway|\
EI:CO MAYO:Cnty Mayo|\
EI:CO LEITRIM:Cnty Leitrim|\
EI:CO ROSCOMMON:Cnty Roscmn|\
EI:CO SLIGO:Cnty Sligo|\
EI:CO CLARE:Cnty Clare|\
EI:CO CORK:Cnty Cork|\
EI:CO KERRY:Cnty Kerry|\
EI:CO LIMERICK:Cnty Limerick|\
EI:CO TIPPERARY:Cnty Tippry|\
EI:CO WATERFORD:Cnty Wterfrd|\
';

// 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 = '\
US:United States|\
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|\
AP:Azores|\
BS:Bahamas|\
BH:Bahrain|\
BD:Bangladesh|\
BB:Barbados|\
BY:Belarus|\
BE:Belgium|\
BZ:Belize|\
BJ:Benin|\
BM:Bermuda|\
BT:Bhutan|\
BO:Bolivia|\
BA:Bosnia-Herzegowina|\
XB:Bosnia-Herzegovina|\
BW:Botswana|\
BV:Bouvet Island|\
BR:Brazil|\
IO:British Indian Ocn|\
VG:British Virgin Isl|\
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 Rep|\
TD:Chad|\
CL:Chile|\
CN:China|\
CX:Christmas Island|\
CC:Cocos (Keeling) Isl|\
CO:Colombia|\
KM:Comoros|\
CG:Congo|\
CD:Congo, Dem. Rep.|\
CK:Cook Islands|\
XE:Corsica|\
CR:Costa Rica|\
CI:Cote d` Ivoire|\
HR:Croatia|\
CU:Cuba|\
CY:Cyprus|\
CZ:Czech Rep|\
DK:Denmark|\
DJ:Djibouti|\
DM:Dominica|\
DO:Dominican Rep|\
TP:East Timor|\
EC:Ecuador|\
EG:Egypt|\
SV:El Salvador|\
GQ:Equatorial Guin|\
ER:Eritrea|\
EE:Estonia|\
ET:Ethiopia|\
FK:Falkland Isl|\
FO:Faroe Isl|\
FJ:Fiji|\
FI:Finland|\
FR:France-Monaco|\
FX:France, Metro|\
GF:Fr. Guiana|\
PF:Fr. Polynesia|\
TA:Fr. Polynesia|\
TF:Fr. S. Terr|\
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 McDonald Isl|\
VA:Holy See|\
HN:Honduras|\
HK:Hong Kong|\
HU:Hungary|\
IS:Iceland|\
IN:India|\
ID:Indonesia|\
IR:Iran|\
IQ:Iraq|\
IE:Ireland|\
EI:Ireland (Eire)|\
IL:Israel|\
IT:Italy|\
JM:Jamaica|\
JP:Japan|\
JO:Jordan|\
KZ:Kazakhstan|\
KE:Kenya|\
KI:Kiribati|\
KP:Korea, Dmcrtic\'s|\
KW:Kuwait|\
KG:Kyrgyzstan|\
LA:Laos|\
LV:Latvia|\
LB:Lebanon|\
LS:Lesotho|\
LR:Liberia|\
LY:Libya|\
LI:Liechtenstein|\
LT:Lithuania|\
LU:Luxembourg|\
MO:Macao|\
MK:Macedonia|\
MG:Madagascar|\
ME:Madeira Isl|\
MW:Malawi|\
MY:Malaysia|\
MV:Maldives|\
ML:Mali|\
MT:Malta|\
MH:Marshall Isl|\
MQ:Martinique|\
MR:Mauritania|\
MU:Mauritius|\
YT:Mayotte|\
MX:Mexico|\
FM:Micronesia|\
MD:Moldova, Rep|\
MC:Monaco|\
MN:Mongolia|\
MS:Montserrat|\
MA:Morocco|\
MZ:Mozambique|\
MM:Myanmar (Burma)|\
NA:Namibia|\
NR:Nauru|\
NP:Nepal|\
NL:Netherlands|\
AN:Netherlands Ant|\
NC:New Caledonia|\
NZ:New Zealand|\
NI:Nicaragua|\
NE:Niger|\
NG:Nigeria|\
NU:Niue|\
NF:Norfolk Island|\
MP:No. Mariana Isl|\
NO:Norway|\
OM:Oman|\
PK:Pakistan|\
PW:Palau|\
PS:Palestinian Terr|\
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 Fed|\
RW:Rwanda|\
KN:Saint Kitts-Nevis|\
SM:San Marino|\
ST:Sao Tome-Principe|\
SA:Saudi Arabia|\
SN:Senegal|\
XS:Serbia-Montenegro|\
SC:Seychelles|\
SL:Sierra Leone|\
SG:Singapore|\
SK:Slovak Rep|\
SI:Slovenia|\
SB:Solomon Isl|\
SO:Somalia|\
ZA:South Africa|\
GS:South Georgia|\
KR:South Korea|\
ES:Spain|\
LK:Sri Lanka|\
NV:St. Christopher-Nevis|\
SH:St. Helena|\
LC:St. Lucia|\
PM:St. Pierre-Miquelon|\
VC:St. Vc, Grenadines|\
SD:Sudan|\
SR:Suriname|\
SJ:Svalbard, Jan Mayen|\
SZ:Swaziland|\
SE:Sweden|\
CH:Switzerland|\
SY:Syrian Arab Rep|\
TW:Taiwan|\
TJ:Tajikistan|\
TZ:Tanzania|\
TH:Thailand|\
TG:Togo|\
TK:Tokelau|\
TO:Tonga|\
TT:Trinidad-Tobago|\
XU:Tristan da Cunha|\
TN:Tunisia|\
TR:Turkey|\
TM:Turkmenistan|\
TC:Turks and Caicos Isl|\
TV:Tuvalu|\
UG:Uganda|\
UA:Ukraine|\
AE:United Arab Emrts|\
UK:United Kingdom|\
UM:US Outlying Isl|\
UY:Uruguay|\
UZ:Uzbekistan|\
VU:Vanuatu|\
XV:Vatican City|\
VE:Venezuela|\
VN:Vietnam|\
VI:Virgin Islands (US)|\
WF:Wallis-Furuna Islands|\
EH:Western Sahara|\
WS:Western Samoa|\
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 resetCountry( countryIdName ) {
    if(document.getElementById( countryIdName ).value!="US")
	  document.getElementById( countryIdName ).value="US";
	lastCountryCode="US";
  }

function populateState( stateIdName, countryIdName ) {

   var selObj = document.getElementById( stateIdName );
   var foundState = false;
   var currentCountryCode=document.getElementById( countryIdName ).value;
   //alert("c:"+currentCountryCode+",al:"+lastCountryCode);
   if(currentCountryCode!=""&&currentCountryCode==lastCountryCode)
     {
        return;
	 }

   if(currentCountryCode=="")
     currentCountryCode="US";
   // Empty options just in case new drop down is shorter
   //

   lastCountryCode=currentCountryCode;
   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 ( currentCountryCode == 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","state"); 
            inputSel.setAttribute("id", stateIdName ); 
			//inputSel.setAttribute("class", "validate-grp1");
            //inputSel.setAttribute("disabled", "" );
            inputSel.setAttribute("style", "width:160;padding:0px 0px 0px 0px" );

            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", "state"); 
      inputEl.setAttribute("value", defaultState); 
      inputEl.setAttribute("style", "width:160"); 
      inputEl.setAttribute("maxlength", "30");
      //inputEl.setAttribute("disabled", ""); 
	  
      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 );
   }
}


