
// Province EN
var stateEN = '\
US:AK:Alaska|\
US:AL:Alabama|\
US:AR:Arkansas|\
US:AS:American Samoa|\
US:AZ:Arizona|\
US:CA:California|\
US:CO:Colorado|\
US:CT:Connecticut|\
US:DC:Dist. Columbia|\
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:Mariana Isl.|\
US:MP:Saipan|\
US:MP:Tinian|\
US:MS:Mississippi|\
US:MT:Montana|\
US:NC:N. Carolina|\
US:ND:N. 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:UM:Minor Islands|\
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:Armed Forces|\
CA:AB:Alberta|\
CA:BC:British Columbia|\
CA:MB:Manitoba|\
CA:NB:New Brunswick|\
CA:NL:Newfoundland|\
CA:NL:Labrador|\
CA:NS:Nova Scotia|\
CA:NT:NW Territories|\
CA:NU:Nunavut|\
CA:ON:Ontario|\
CA:PE:P-E-I|\
CA:QC:Quebec|\
CA:SK:Saskatchewan|\
CA:YT:Yukon|\
';

// Province FR
var stateFR = '\
US:AL:Alabama|\
US:AK:Alaska|\
US:AZ:Arizona|\
US:AR:Arkansas|\
US:CA:Californie|\
US:NC:Caroline Nord|\
US:SC:Caroline Sud|\
US:CO:Colorado|\
US:CT:Connecticut|\
US:ND:Dakota du Nord|\
US:SD:Dakota du Sud|\
US:DE:Delaware|\
US:DC:Columbia|\
US:FL:Floride|\
US:GA:Géorgie|\
US:GU:Guam|\
US:HI:Hawaï|\
US:ID:Idaho|\
US:MP:Îles Mariannes|\
US:MH:Îles Marshall|\
US:VI:Îles Vierges|\
US:IL:Illinois|\
US:IN:Indiana|\
US:IA:Iowa|\
US:KS:Kansas|\
US:KY:Kentucky|\
US:LA:Louisiane|\
US:ME:Maine|\
US:MD:Maryland|\
US:MA:Massachusetts|\
US:MI:Michigan|\
US:FM:Micronésie|\
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:NY:New York|\
US:NM:Nouveau-Mexique|\
US:OH:Ohio|\
US:OK:Oklahoma|\
US:OR:Oregon|\
US:PW:Palaos|\
US:PA:Pennsylvanie|\
US:UM:Petites îles|\
US:PR:Puerto Rico|\
US:RI:Rhode Island|\
US:MP:Saipan|\
US:AS:Samoa Américaines|\
US:TN:Tennessee|\
US:TX:Texas|\
US:MP:Tinian|\
US:UT:Utah|\
US:VT:Vermont|\
US:VA:Virginie|\
US:WV:Virginie-Occ.|\
US:WA:Washington|\
US:WI:Wisconsin|\
US:WY:Wyoming|\
US:AA:Forces armées|\
CA:AB:Alberta|\
CA:BC:Colombie-Britannique|\
CA:MB:Manitoba|\
CA:NB:Nouveau Brunswick|\
CA:NL:Terre-Neuve|\
CA:NL:Labrador|\
CA:NS:Nouvelle-Écosse|\
CA:NT:Territoires du NO|\
CA:NU:Nunavut|\
CA:ON:Ontario|\
CA:PE:Prince-Édouard|\
CA:QC:Québec|\
CA:SK:Saskatchewan|\
CA:YT:Yukon|\
';

// Country EN
var countryEN = '\
CA:Canada|\
FR:France|\
US:United States|\
--:----------|\
AF:Afghanistan|\
AX:Åland Islands|\
AL:Albania|\
DZ:Algeria|\
AS:American Samoa|\
AD:Andorra|\
AO:Angola|\
AI:Anguilla|\
AQ:Antarctica|\
AG:Antigua, 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|\
BA:Herzegovina|\
BW:Botswana|\
BV:Bouvet Island|\
BR:Brazil|\
IO:British Indian Ocean|\
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|\
TD:Chad|\
CL:Chile|\
CN:China|\
CX:Christmas Island|\
CC:Cocos Islands|\
CO:Colombia|\
KM:Comoros|\
CG:Congo|\
CD:Congo, Democratic Rep.|\
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|\
EC:Ecuador|\
EG:Egypt|\
SV:El Salvador|\
GQ:Equatorial Guinea|\
ER:Eritrea|\
EE:Estonia|\
ET:Ethiopia|\
FK:Falkland Islands|\
FO:Faroe Islands|\
FJ:Fiji|\
FI:Finland|\
FR:France|\
GF:French Guiana|\
PF:French Polynesia|\
TF:French Southern Ter.|\
GA:Gabon|\
GM:Gambia|\
GE:Georgia|\
DE:Germany|\
GH:Ghana|\
GI:Gibraltar|\
GR:Greece|\
GL:Greenland|\
GD:Grenada|\
GP:Guadeloupe|\
GU:Guam|\
GT:Guatemala|\
GG:Guernsey|\
GN:Guinea|\
GW:Guinea-bissau|\
GY:Guyana|\
HT:Haiti|\
HM:Heard Island|\
HM:Mcdonald Islands|\
VA:Vatican City|\
HN:Honduras|\
HK:Hong Kong|\
HU:Hungary|\
IS:Iceland|\
IN:India|\
ID:Indonesia|\
IR:Iran|\
IQ:Iraq|\
IE:Ireland|\
IM:Isle Of Man|\
IL:Israel|\
IT:Italy|\
JM:Jamaica|\
JP:Japan|\
JE:Jersey|\
JO:Jordan|\
KZ:Kazakhstan|\
KE:Kenya|\
KI:Kiribati|\
KR:Korea, Republic Of|\
KW:Kuwait|\
KG:Kyrgyzstan|\
LA:Lao|\
LV:Latvia|\
LB:Lebanon|\
LS:Lesotho|\
LR:Liberia|\
LY:Libyan Arab Jamahiriya|\
LI:Liechtenstein|\
LT:Lithuania|\
LU:Luxembourg|\
MO:Macao|\
MK:Macedonia|\
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|\
MD:Moldova|\
MC:Monaco|\
MN:Mongolia|\
ME:Montenegro|\
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|\
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 Federation|\
RW:Rwanda|\
SH:Saint Helena|\
KN:Saint Kitts Nevis|\
LC:Saint Lucia|\
PM:Saint Pierre Miquelon|\
VC:Saint Vincent Grenadines|\
WS:Samoa|\
SM:San Marino|\
ST:Sao Tome And Principe|\
SA:Saudi Arabia|\
SN:Senegal|\
RS:Serbia|\
SC:Seychelles|\
SL:Sierra Leone|\
SG:Singapore|\
SK:Slovakia|\
SI:Slovenia|\
SB:Solomon Islands|\
SO:Somalia|\
ZA:South Africa|\
GS:South Georgia|\
ES:Spain|\
LK:Sri Lanka|\
SD:Sudan|\
SR:Suriname|\
SJ:Svalbard And Jan Mayen|\
SZ:Swaziland|\
SE:Sweden|\
CH:Switzerland|\
SY:Syrian Arab Republic|\
TW:Taiwan, Prov. Of China|\
TJ:Tajikistan|\
TZ:Tanzania|\
TH:Thailand|\
TL:Timor-leste|\
TG:Togo|\
TK:Tokelau|\
TO:Tonga|\
TT:Trinidad And Tobago|\
TN:Tunisia|\
TR:Turkey|\
TM:Turkmenistan|\
TC:Turks And Caicos|\
TV:Tuvalu|\
UG:Uganda|\
UA:Ukraine|\
AE:United Arab Emirates|\
GB:United Kingdom|\
US:United States|\
UM:US Minor Islands|\
UY:Uruguay|\
UZ:Uzbekistan|\
VU:Vanuatu|\
VE:Venezuela|\
VN:Viet Nam|\
VG:Virgin Islands, British|\
VI:Virgin Islands, U.S.|\
WF:Wallis And Futuna|\
EH:Western Sahara|\
YE:Yemen|\
ZM:Zambia|\
ZW:Zimbabwe|\
';

// Country FR
var countryFR = '\
CA:Canada|\
FR:France|\
US:États-Unis|\
--:----------|\
AF:Afghanistan|\
ZA:Afrique Du Sud|\
AX:Åland, Îles|\
AL:Albanie|\
DZ:Algérie|\
DE:Allemagne|\
AD:Andorre|\
AO:Angola|\
AI:Anguilla|\
AQ:Antarctique|\
AG:Antigua-et-barbuda|\
AN:Antilles Néerlande|\
SA:Arabie Saoudite|\
AR:Argentine|\
AM:Arménie|\
AW:Aruba|\
AU:Australie|\
AT:Autriche|\
AZ:Azerbaïdjan|\
BS:Bahamas|\
BH:Bahreïn|\
BD:Bangladesh|\
BB:Barbade|\
BY:Bélarus|\
BE:Belgique|\
BZ:Belize|\
BJ:Bénin|\
BM:Bermudes|\
BT:Bhoutan|\
BO:Bolivie|\
BA:Bosnie-herzégovine|\
BW:Botswana|\
BV:Bouvet, Île|\
BR:Brésil|\
BN:Brunéi Darussalam|\
BG:Bulgarie|\
BF:Burkina Faso|\
BI:Burundi|\
KY:Caïmanes, Îles|\
KH:Cambodge|\
CM:Cameroun|\
CA:Canada|\
CV:Cap-vert|\
CF:Centrafricaine|\
CL:Chili|\
CN:Chine|\
CX:Christmas, Île|\
CY:Chypre|\
CC:Cocos (Keeling)|\
CO:Colombie|\
KM:Comores|\
CG:Congo|\
CK:Cook, Îles|\
KR:Corée, République De|\
CR:Costa Rica|\
CI:Côte D\'Ivoire|\
HR:Croatie|\
CU:Cuba|\
DK:Danemark|\
DJ:Djibouti|\
DO:Dominicaine|\
DM:Dominique|\
EG:Égypte|\
SV:El Salvador|\
AE:Émirats Arabes Unis|\
EC:Équateur|\
ER:Érythrée|\
ES:Espagne|\
EE:Estonie|\
US:États-unis|\
ET:Éthiopie|\
FK:Falkland, Îles|\
FO:Féroé, Îles|\
FJ:Fidji|\
FI:Finlande|\
FR:France|\
GA:Gabon|\
GM:Gambie|\
GE:Géorgie|\
GS:Géorgie Du Sud|\
GH:Ghana|\
GI:Gibraltar|\
GR:Grèce|\
GD:Grenade|\
GL:Groenland|\
GP:Guadeloupe|\
GU:Guam|\
GT:Guatemala|\
GG:Guernesey|\
GN:Guinée|\
GQ:Guinée Équatoriale|\
GW:Guinée-bissau|\
GY:Guyana|\
GF:Guyane Française|\
HT:Haïti|\
HM:Heard, Île Mcdonald|\
HN:Honduras|\
HK:Hong-kong|\
HU:Hongrie|\
IM:Île De Man|\
UM:Îles Mineures Des ÉU|\
VG:Îles Vierges Brit.|\
VI:Îles Vierges Des ÉU|\
IN:Inde|\
ID:Indonésie|\
IR:Iran|\
IQ:Iraq|\
IE:Irlande|\
IS:Islande|\
IL:Israël|\
IT:Italie|\
JM:Jamaïque|\
JP:Japon|\
JE:Jersey|\
JO:Jordanie|\
KZ:Kazakhstan|\
KE:Kenya|\
KG:Kirghizistan|\
KI:Kiribati|\
KW:Koweït|\
LA:Lao|\
LS:Lesotho|\
LV:Lettonie|\
LB:Liban|\
LR:Libéria|\
LY:Libyenne, Jamahiriya|\
LI:Liechtenstein|\
LT:Lituanie|\
LU:Luxembourg|\
MO:Macao|\
MK:Macédoine|\
MG:Madagascar|\
MY:Malaisie|\
MW:Malawi|\
MV:Maldives|\
ML:Mali|\
MT:Malte|\
MP:Mariannes Du Nord|\
MA:Maroc|\
MH:Marshall, Îles|\
MQ:Martinique|\
MU:Maurice|\
MR:Mauritanie|\
YT:Mayotte|\
MX:Mexique|\
FM:Micronésie|\
MD:Moldova|\
MC:Monaco|\
MN:Mongolie|\
ME:Montenegro|\
MS:Montserrat|\
MZ:Mozambique|\
MM:Myanmar|\
NA:Namibie|\
NR:Nauru|\
NP:Népal|\
NI:Nicaragua|\
NE:Niger|\
NG:Nigéria|\
NU:Niué|\
NF:Norfolk, Île|\
NO:Norvège|\
NC:Nouvelle-calédonie|\
NZ:Nouvelle-zélande|\
IO:Océan Indien|\
OM:Oman|\
UG:Ouganda|\
UZ:Ouzbékistan|\
PK:Pakistan|\
PW:Palaos|\
PS:Palestinien Occupé|\
PA:Panama|\
PG:Papouasie Nouv-Guinée|\
PY:Paraguay|\
NL:Pays-bas|\
PE:Pérou|\
PH:Philippines|\
PN:Pitcairn|\
PL:Pologne|\
PF:Polynésie Française|\
PR:Porto Rico|\
PT:Portugal|\
QA:Qatar|\
RE:Réunion|\
RO:Roumanie|\
GB:Royaume-uni|\
RU:Russie, Fédération De|\
RW:Rwanda|\
EH:Sahara Occidental|\
SH:Sainte-hélène|\
LC:Sainte-lucie|\
KN:Saint-kitts-et-nevis|\
SM:Saint-marin|\
PM:Saint-pierre-miquelon|\
VA:Saint-siège|\
VC:Saint-Vincent Grenadines|\
SB:Salomon, Îles|\
WS:Samoa|\
AS:Samoa Américaines|\
ST:Sao Tomé-et-principe|\
SN:Sénégal|\
RS:Serbie|\
SC:Seychelles|\
SL:Sierra Leone|\
SG:Singapour|\
SK:Slovaquie|\
SI:Slovénie|\
SO:Somalie|\
SD:Soudan|\
LK:Sri Lanka|\
SE:Suède|\
CH:Suisse|\
SR:Suriname|\
SJ:Svalbard, Île Jan Mayen|\
SZ:Swaziland|\
SY:Syrienne, République|\
TJ:Tadjikistan|\
TW:Taïwan, Prov. De Chine|\
TZ:Tanzanie|\
TD:Tchad|\
CZ:Tchèque, République|\
TF:Terres Australes Fr.|\
TH:Thaïlande|\
TL:Timor-leste|\
TG:Togo|\
TK:Tokelau|\
TO:Tonga|\
TT:Trinité-et-tobago|\
TN:Tunisie|\
TM:Turkménistan|\
TC:Turks Et Caïques|\
TR:Turquie|\
TV:Tuvalu|\
UA:Ukraine|\
UY:Uruguay|\
VU:Vanuatu|\
VE:Venezuela|\
VN:Viet Nam|\
WF:Wallis Et Futuna|\
YE:Yémen|\
ZM:Zambie|\
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 languageDefaultCfgArray   = document.getElementById('cs_config_language_default').value.split(' ');

var defaultState = false;
var defaultCountry = false;
var defaultLanguage = false;

var countryIdName = false;
var stateIdName = false;
var countryCode = false;
var countryName = false;
var stateCode = false;
var stateName = false;

var lineArray = new Array();

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) {

	if (defaultLanguage == "FR"){
		var state = stateFR;
		var country = countryFR;
	} else if (defaultLanguage == "EN"){
		var state = stateEN;
		var country = countryEN;
	} else {
		var state = stateEN;
		var country = countryEN;
	}

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

   var selObj = document.getElementById( idName );

   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 + 1] = new Option(countryName, countryCode);
		}
		
		if ( defaultCountry == countryCode ) {
			selObj.selectedIndex = loop + 1;
		}
	}
}

function populateState( statestateIdName, countryIdName ) {
	
	if (defaultLanguage == "FR"){
		var state = stateFR;
		var country = countryFR;
	} else if (defaultLanguage == "EN"){
		var state = stateEN;
		var country = countryEN;
	} else {
		var state = stateEN;
		var country = countryEN;
	}
	
	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('----------','');
		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 ); 
		inputSel.setAttribute("width", "172");
		inputSel.setAttribute("style", "width: 172px"); 
		
		parentObj.appendChild(inputSel) ;
		
		selObj = document.getElementById( stateIdName );
		selObj.options[0] = new Option('----------','');
		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("size", 30); 
		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;
      defaultLanguage   = document.getElementById( languageDefaultCfgArray[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;
		defaultLanguage   = document.getElementById( languageDefaultCfgArray[loop] ).value;
		
		populateCountry( countryIdName);
		populateState( stateIdName, countryIdName );
	}
}

