

// 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:AZ:Arizona|\
US:CA:California|\
US:CO:Colorado|\
US:CT:Connecticut|\
US:DC:D.C.|\
US:DE:Delaware|\
US:FL:Florida|\
US:GA:Georgia|\
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:MI:Michigan|\
US:MN:Minnesota|\
US:MO:Missouri|\
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:RI:Rhode Island|\
US:SC:South Carolina|\
US:SD:South Dakota|\
US:TN:Tennessee|\
US:TX:Texas|\
US:UT:Utah|\
US:VA:Virginia|\
US:VT:Vermont|\
US:WA:Washington|\
US:WI:Wisconsin|\
US:WV:West Virginia|\
US:WY:Wyoming|\
CA:AB:Alberta|\
CA:BC:British Columbia|\
CA:MB:Manitoba|\
CA:NB:New Brunswick|\
CA:NL:Newfoundland and Labrador|\
CA:NS:Nova Scotia|\
CA:NT:Northwest Territories|\
CA:ON:Ontario|\
CA:PE:Prince Edward Island|\
CA:QC:Quebec|\
CA:SK:Saskatchewan|\
CA:YT:Yukon Territory|\
AU:ACT:Australian Capital Territory|\
AU:NT:Northern Territory|\
AU:NSW:New South Wales|\
AU:QLD:Queensland|\
AU:SA:South Australia|\
AU:TAS:Tasmania|\
AU:VIC:Victoria|\
AU:WA:Western Australia|\
Brazil:Acre:Acre|\
Brazil:Alagoas:Alagoas|\
Brazil:Amazonas:Amazonas|\
Brazil:Amapa:Amapa|\
Brazil:Baia:Baia|\
Brazil:Ceara:Ceara|\
Brazil:Distrito Federal:Distrito Federal|\
Brazil:Espirito Santo:Espirito Santo|\
Brazil:Fernando de Noronha:Fernando de Noronha|\
Brazil:Goias:Goias|\
Brazil:Maranhao:Maranhao|\
Brazil:Minas Gerais:Minas Gerais|\
Brazil:Mato Grosso do Sul:Mato Grosso do Sul|\
Brazil:Mato Grosso:Mato Grosso|\
Brazil:Para:Para|\
Brazil:Paraiba:Paraiba|\
Brazil:Pernambuco:Pernambuco|\
Brazil:Piaui:Piaui|\
Brazil:Parana:Parana|\
Brazil:Rio de Janeiro:Rio de Janeiro|\
Brazil:Rio Grande do Norte:Rio Grande do Norte|\
Brazil:Rondonia:Rondonia|\
Brazil:Roraima:Roraima|\
Brazil:Rio Grande do Sul:Rio Grande do Sul|\
Brazil:Santa Catarina:Santa Catarina|\
Brazil:Sergipe:Sergipe|\
Brazil:Sao Paulo:Sao Paulo|\
Brazil:Tocatins:Tocatins|\
Netherlands:Drente:Drente|\
Netherlands:Flevoland:Flevoland|\
Netherlands:Friesland:Friesland|\
Netherlands:Gelderland:Gelderland|\
Netherlands:Groningen:Groningen|\
Netherlands:Limburg:Limburg|\
Netherlands:Noord Brabant:Noord Brabant|\
Netherlands:Noord Holland:Noord Holland|\
Netherlands:Overijssel:Overijssel|\
Netherlands:Utrecht:Utrecht|\
Netherlands:Zuid Holland:Zuid Holland|\
Netherlands:Zeeland:Zeeland|\
United Kingdom:Avon:Avon|\
United Kingdom:Bedfordshire:Bedfordshire|\
United Kingdom:Berkshire:Berkshire|\
United Kingdom:Buckinghamshire:Buckinghamshire|\
United Kingdom:Cambridgeshire:Cambridgeshire|\
United Kingdom:Cheshire:Cheshire|\
United Kingdom:Cleveland:Cleveland|\
United Kingdom:Cornwall:Cornwall|\
United Kingdom:Cumbria:Cumbria|\
United Kingdom:Derbyshire:Derbyshire|\
United Kingdom:Devon:Devon|\
United Kingdom:Dorset:Dorset|\
United Kingdom:Durham:Durham|\
United Kingdom:Essex:Essex|\
United Kingdom:Gloucestershire:Gloucestershire|\
United Kingdom:Greater London:Greater London|\
United Kingdom:Greater Manchester:Greater Manchester|\
United Kingdom:Hampshire:Hampshire|\
United Kingdom:Hereford & Worcestershire:Hereford & Worcestershire|\
United Kingdom:Hertfordshire:Hertfordshire|\
United Kingdom:Humberside:Humberside|\
United Kingdom:Isle Of Man:Isle Of Man|\
United Kingdom:Isle Of Wight:Isle Of Wight|\
United Kingdom:Kent:Kent|\
United Kingdom:Lancashire:Lancashire|\
United Kingdom:Leicestershire:Leicestershire|\
United Kingdom:Lincolnshire:Lincolnshire|\
United Kingdom:Merseyside:Merseyside|\
United Kingdom:Norfolk:Norfolk|\
United Kingdom:Northamptonshire:Northamptonshire|\
United Kingdom:Northumberland:Northumberland|\
United Kingdom:Nottinghamshire:Nottinghamshire|\
United Kingdom:Oxfordshire:Oxfordshire|\
United Kingdom:Shropshire:Shropshire|\
United Kingdom:Somerset:Somerset|\
United Kingdom:Staffordshire:Staffordshire|\
United Kingdom:Suffolk:Suffolk|\
United Kingdom:Surrey:Surrey|\
United Kingdom:Sussex:Sussex|\
United Kingdom:Warwickshire:Warwickshire|\
United Kingdom:West Midlands:West Midlands|\
United Kingdom:Wiltshire:Wiltshire|\
United Kingdom:Yorkshire:Yorkshire|\
Ireland (Eire):County Antrim:County Antrim|\
Ireland (Eire):County Armagh:County Armagh|\
Ireland (Eire):County Down:County Down|\
Ireland (Eire):County Fermanagh:County Fermanagh|\
Ireland (Eire):County Londonderry:County Londonderry|\
Ireland (Eire):County Tyrone:County Tyrone|\
Ireland (Eire):County Cavan:County Cavan|\
Ireland (Eire):County Donegal:County Donegal|\
Ireland (Eire):County Monaghan:County Monaghan|\
Ireland (Eire):County Dublin:County Dublin|\
Ireland (Eire):County Carlow:County Carlow|\
Ireland (Eire):County Kildare:County Kildare|\
Ireland (Eire):County Kilkenny:County Kilkenny|\
Ireland (Eire):County Laois:County Laois|\
Ireland (Eire):County Longford:County Longford|\
Ireland (Eire):County Louth:County Louth|\
Ireland (Eire):County Meath:County Meath|\
Ireland (Eire):County Offaly:County Offaly|\
Ireland (Eire):County Westmeath:County Westmeath|\
Ireland (Eire):County Wexford:County Wexford|\
Ireland (Eire):County Wicklow:County Wicklow|\
Ireland (Eire):County Galway:County Galway|\
Ireland (Eire):County Mayo:County Mayo|\
Ireland (Eire):County Leitrim:County Leitrim|\
Ireland (Eire):County Roscommon:County Roscommon|\
Ireland (Eire):County Sligo:County Sligo|\
Ireland (Eire):County Clare:County Clare|\
Ireland (Eire):County Cork:County Cork|\
Ireland (Eire):County Kerry:County Kerry|\
Ireland (Eire):County Limerick:County Limerick|\
Ireland (Eire):County Tipperary:County Tipperary|\
Ireland (Eire):County 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|\
Argentina:Argentina|\
Armenia:Armenia|\
AU:Australia|\
Austria:Austria|\
Azerbaijan:Azerbaijan|\
Bahrain:Bahrain|\
Bangladesh:Bangladesh|\
Belarus:Belarus|\
Belgium:Belgium|\
Belize:Belize|\
Bermuda:Bermuda|\
Bhutan:Bhutan|\
Bolivia:Bolivia|\
Bosnia-Herzegovina:Bosnia-Herzegovina|\
Botswana:Botswana|\
Brazil:Brazil|\
Brunei Darussalam:Brunei Darussalam|\
Bulgaria:Bulgaria|\
Burundi:Burundi|\
Cambodia:Cambodia|\
Cameroon:Cameroon|\
CA:Canada|\
Central African Republic:Central African Republic|\
Chad:Chad|\
Chile:Chile|\
China:China|\
Colombia:Colombia|\
Comoros:Comoros|\
Congo:Congo|\
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|\
East Timor:East Timor|\
Ecuador:Ecuador|\
Egypt:Egypt|\
El Salvador:El Salvador|\
Eritrea:Eritrea|\
Estonia:Estonia|\
Ethiopia:Ethiopia|\
Finland:Finland|\
France:France|\
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|\
Honduras:Honduras|\
Hong Kong:Hong Kong|\
Hungary:Hungary|\
Iceland:Iceland|\
India:India|\
Indonesia:Indonesia|\
Iran:Iran|\
Iraq:Iraq|\
Ireland:Ireland|\
Israel:Israel|\
Italy:Italy|\
Japan:Japan|\
Jordan:Jordan|\
Kazakhstan:Kazakhstan|\
Kenya:Kenya|\
Kuwait:Kuwait|\
Kyrgyzstan:Kyrgyzstan|\
Laos:Laos|\
Latvia:Latvia|\
Lebanon:Lebanon|\
Liberia:Liberia|\
Libya:Libya|\
Liechtenstein:Liechtenstein|\
Lithuania:Lithuania|\
Luxembourg:Luxembourg|\
Macedonia:Macedonia|\
Madagascar:Madagascar|\
Malaysia:Malaysia|\
Mali:Mali|\
Malta:Malta|\
Mexico:Mexico|\
Monaco:Monaco|\
Mongolia:Mongolia|\
Morocco:Morocco|\
Mozambique:Mozambique|\
Namibia:Namibia|\
Nepal:Nepal|\
Netherlands:Netherlands|\
New Zealand:New Zealand|\
Nicaragua:Nicaragua|\
Niger:Niger|\
Nigeria:Nigeria|\
Norway:Norway|\
Oman:Oman|\
Pakistan:Pakistan|\
Palau:Palau|\
Panama:Panama|\
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|\
Saudi Arabia:Saudi Arabia|\
Senegal:Senegal|\
Serbia-Montenegro:Serbia-Montenegro|\
Sierra Leone:Sierra Leone|\
Singapore:Singapore|\
Slovak Republic:Slovak Republic|\
Slovenia:Slovenia|\
Solomon Islands:Solomon Islands|\
Somalia:Somalia|\
South Africa:South Africa|\
South Korea:South Korea|\
Spain:Spain|\
Sri Lanka:Sri Lanka|\
Sudan:Sudan|\
Swaziland:Swaziland|\
Sweden:Sweden|\
Switzerland:Switzerland|\
Syrian Arab Republic:Syrian Arab Republic|\
Taiwan:Taiwan|\
Tajikistan:Tajikistan|\
Tanzania:Tanzania|\
Thailand:Thailand|\
Tunisia:Tunisia|\
Turkey:Turkey|\
Turkmenistan:Turkmenistan|\
Uganda:Uganda|\
Ukraine:Ukraine|\
United Arab Emirates:United Arab Emirates|\
United Kingdom:United Kingdom|\
US:United States|\
Uruguay:Uruguay|\
Uzbekistan:Uzbekistan|\
Venezuela:Venezuela|\
Vietnam:Vietnam|\
Yemen:Yemen|\
Yugoslavia:Yugoslavia|\
Zaire:Zaire|\
Zambia:Zambia|\
Zimbabwe: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 );
   selObj.disabled=false;
   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","state"); 
            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);
      selObj.disabled=true;
   
 
  /*    // Create the Input Field
      var inputEl = document.createElement("INPUT");

      inputEl.setAttribute("id",  stateIdName ); 
      inputEl.setAttribute("type", "text"); 
      inputEl.setAttribute("name", "state"); 
      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 );
   }
}

