 // If you have PHP you can set the post values like this
//var postState = '<?= $_POST["state"] ?>';
//var postCountry = '<?= $_POST["country"] ?>';
var postState = '';
var postCountry = '';

// 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 state = '\
USA:AK:Alaska|\
USA:AL:Alabama|\
USA:AR:Arkansas|\
USA:AS:American Samoa|\
USA:AZ:Arizona|\
USA:CA:California|\
USA:CO:Colorado|\
USA:CT:Connecticut|\
USA:DC:D.C.|\
USA:DE:Delaware|\
USA:FL:Florida|\
USA:FM:Micronesia|\
USA:GA:Georgia|\
USA:GU:Guam|\
USA:HI:Hawaii|\
USA:IA:Iowa|\
USA:ID:Idaho|\
USA:IL:Illinois|\
USA:IN:Indiana|\
USA:KS:Kansas|\
USA:KY:Kentucky|\
USA:LA:Louisiana|\
USA:MA:Massachusetts|\
USA:MD:Maryland|\
USA:ME:Maine|\
USA:MH:Marshall Islands|\
USA:MI:Michigan|\
USA:MN:Minnesota|\
USA:MO:Missouri|\
USA:MP:Marianas|\
USA:MS:Mississippi|\
USA:MT:Montana|\
USA:NC:North Carolina|\
USA:ND:North Dakota|\
USA:NE:Nebraska|\
USA:NH:New Hampshire|\
USA:NJ:New Jersey|\
USA:NM:New Mexico|\
USA:NV:Nevada|\
USA:NY:New York|\
USA:OH:Ohio|\
USA:OK:Oklahoma|\
USA:OR:Oregon|\
USA:PA:Pennsylvania|\
USA:PR:Puerto Rico|\
USA:PW:Palau|\
USA:RI:Rhode Island|\
USA:SC:South Carolina|\
USA:SD:South Dakota|\
USA:TN:Tennessee|\
USA:TX:Texas|\
USA:UT:Utah|\
USA:VA:Virginia|\
USA:VI:Virgin Islands|\
USA:VT:Vermont|\
USA:WA:Washington|\
USA:WI:Wisconsin|\
USA:WV:West Virginia|\
USA:WY:Wyoming|\
USA:AA:Military Americas|\
USA:AE:Military Europe/ME/Canada|\
USA:AP:Military Pacific|\
CAN:AB:Alberta|\
CAN:MB:Manitoba|\
CAN:AB:Alberta|\
CAN:BC:British Columbia|\
CAN:MB:Manitoba|\
CAN:NB:New Brunswick|\
CAN:NL:Newfoundland and Labrador|\
CAN:NS:Nova Scotia|\
CAN:NT:Northwest Territories|\
CAN:NU:Nunavut|\
CAN:ON:Ontario|\
CAN:PE:Prince Edward Island|\
CAN:QC:Quebec|\
CAN:SK:Saskatchewan|\
CAN:YT:Yukon Territory|\
AUS:AAT:Australian Antarctic Territory|\
AUS:ACT:Australian Capital Territory|\
AUS:NT:Northern Territory|\
AUS:NSW:New South Wales|\
AUS:QLD:Queensland|\
AUS:SA:South Australia|\
AUS:TAS:Tasmania|\
AUS:VIC:Victoria|\
AUS:WA:Western Australia|\
BRA:AC:Acre|\
BRA:AL:Alagoas|\
BRA:AM:Amazonas|\
BRA:AP:Amapa|\
BRA:BA:Baia|\
BRA:CE:Ceara|\
BRA:DF:Distrito Federal|\
BRA:ES:Espirito Santo|\
BRA:FN:Fernando de Noronha|\
BRA:GO:Goias|\
BRA:MA:Maranhao|\
BRA:MG:Minas Gerais|\
BRA:MS:Mato Grosso do Sul|\
BRA:MT:Mato Grosso|\
BRA:PA:Para|\
BRA:PB:Paraiba|\
BRA:PE:Pernambuco|\
BRA:PI:Piaui|\
BRA:PR:Parana|\
BRA:RJ:Rio de Janeiro|\
BRA:RN:Rio Grande do Norte|\
BRA:RO:Rondonia|\
BRA:RR:Roraima|\
BRA:RS:Rio Grande do Sul|\
BRA:SC:Santa Catarina|\
BRA:SE:Sergipe|\
BRA:SP:Sao Paulo|\
BRA:TO:Tocatins|\
NLD:DR:Drente|\
NLD:FL:Flevoland|\
NLD:FR:Friesland|\
NLD:GL:Gelderland|\
NLD:GR:Groningen|\
NLD:LB:Limburg|\
NLD:NB:Noord Brabant|\
NLD:NH:Noord Holland|\
NLD:OV:Overijssel|\
NLD:UT:Utrecht|\
NLD:ZH:Zuid Holland|\
NLD:ZL:Zeeland|\
GBR:AVON:Avon|\
GBR:BEDS:Bedfordshire|\
GBR:BERKS:Berkshire|\
GBR:BUCKS:Buckinghamshire|\
GBR:CAMBS:Cambridgeshire|\
GBR:CHESH:Cheshire|\
GBR:CLEVE:Cleveland|\
GBR:CORN:Cornwall|\
GBR:CUMB:Cumbria|\
GBR:DERBY:Derbyshire|\
GBR:DEVON:Devon|\
GBR:DORSET:Dorset|\
GBR:DURHAM:Durham|\
GBR:ESSEX:Essex|\
GBR:GLOUSA:Gloucestershire|\
GBR:GLONDON:Greater London|\
GBR:GMANCH:Greater Manchester|\
GBR:HANTS:Hampshire|\
GBR:HERWOR:Hereford & Worcestershire|\
GBR:HERTS:Hertfordshire|\
GBR:HUMBER:Humberside|\
GBR:IOM:Isle of Man|\
GBR:IOW:Isle of Wight|\
GBR:KENT:Kent|\
GBR:LANCS:Lancashire|\
GBR:LEICS:Leicestershire|\
GBR:LINCS:Lincolnshire|\
GBR:MERSEY:Merseyside|\
GBR:NORF:Norfolk|\
GBR:NHANTS:Northamptonshire|\
GBR:NTHUMB:Northumberland|\
GBR:NOTTS:Nottinghamshire|\
GBR:OXON:Oxfordshire|\
GBR:SHROPS:Shropshire|\
GBR:SOM:Somerset|\
GBR:STAFFS:Staffordshire|\
GBR:SUFF:Suffolk|\
GBR:SURREY:Surrey|\
GBR:SUSS:Sussex|\
GBR:WARKS:Warwickshire|\
GBR:WMID:West Midlands|\
GBR:WILTS:Wiltshire|\
GBR:YORK:Yorkshire|\
IRL:CO ANTRIM:County Antrim|\
IRL:CO ARMAGH:County Armagh|\
IRL:CO DOWN:County Down|\
IRL:CO FERMANAGH:County Fermanagh|\
IRL:CO DERRY:County Londonderry|\
IRL:CO TYRONE:County Tyrone|\
IRL:CO CAVAN:County Cavan|\
IRL:CO DONEGAL:County Donegal|\
IRL:CO MONAGHAN:County Monaghan|\
IRL:CO DUBLIN:County Dublin|\
IRL:CO CARLOW:County Carlow|\
IRL:CO KILDARE:County Kildare|\
IRL:CO KILKENNY:County Kilkenny|\
IRL:CO LAOIS:County Laois|\
IRL:CO LONGFORD:County Longford|\
IRL:CO LOUTH:County Louth|\
IRL:CO MEATH:County Meath|\
IRL:CO OFFALY:County Offaly|\
IRL:CO WESTMEATH:County Westmeath|\
IRL:CO WEXFORD:County Wexford|\
IRL:CO WICKLOW:County Wicklow|\
IRL:CO GALWAY:County Galway|\
IRL:CO MAYO:County Mayo|\
IRL:CO LEITRIM:County Leitrim|\
IRL:CO ROSCOMMON:County Roscommon|\
IRL:CO SLIGO:County Sligo|\
IRL:CO CLARE:County Clare|\
IRL:CO CORK:County Cork|\
IRL:CO KERRY:County Kerry|\
IRL:CO LIMERICK:County Limerick|\
IRL:CO TIPPERARY:County Tipperary|\
IRL:CO WATERFORD:County Waterford|\
CRI:SJO:San Jose|\
CRI:ALA:Alajuela|\
CRI:HER:Heredia|\
CRI:CAR:Cartago|\
CRI:LIM:Limon|\
CRI:PUN:Puntarenas|\
CRI:GUA:Guanacaste|\
';

// 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 = '\
AFG:Afghanistan|\
ALB:Albania|\
DZA:Algeria|\
ASM:American Samoa|\
AND:Andorra|\
AGO:Angola|\
AIA:Anguilla|\
ATG:Antigua and Barbuda|\
ARG:Argentina|\
ARM:Armenia|\
ABW:Aruba|\
AUS:Australia|\
AUT:Austria|\
AZE:Azerbaijan|\
BHS:Bahamas|\
BHR:Bahrain|\
BGD:Bangladesh|\
BRB:Barbados|\
BLR:Belarus|\
BEL:Belgium|\
BLZ:Belize|\
BEN:Benin|\
BMU:Bermuda|\
BTN:Bhutan|\
BOL:Bolivia|\
BIH:Bosnia and Herzegovina|\
BWA:Botswana|\
BRA:Brazil|\
BRN:Brunei Darussalam|\
BGR:Bulgaria|\
BFA:Burkina Faso|\
BDI:Burundi|\
KHM:Cambodia|\
CMR:Cameroon|\
CAN:Canada|\
CPV:Cape Verde|\
CYM:Cayman Islands|\
CAF:Central African Republic|\
TCD:Chad|\
CHL:Chile|\
CHN:China|\
COL:Colombia|\
COM:Comoros|\
COG:Congo|\
COD:Congo, the Democratic Republic of the|\
COK:Cook Islands|\
CRI:Costa Rica|\
CIV:Cote D\'Ivoire|\
HRV:Croatia|\
CUB:Cuba|\
CYP:Cyprus|\
CZE:Czech Republic|\
DNK:Denmark|\
DJI:Djibouti|\
DMA:Dominica|\
DOM:Dominican Republic|\
ECU:Ecuador|\
EGY:Egypt|\
SLV:El Salvador|\
GNQ:Equatorial Guinea|\
ERI:Eritrea|\
EST:Estonia|\
ETH:Ethiopia|\
FLK:Falkland Islands (Malvinas)|\
FRO:Faroe Islands|\
FJI:Fiji|\
FIN:Finland|\
FRA:France|\
GUF:French Guiana|\
PYF:French Polynesia|\
GAB:Gabon|\
GMB:Gambia|\
GEO:Georgia|\
DEU:Germany|\
GHA:Ghana|\
GIB:Gibraltar|\
GRC:Greece|\
GRL:Greenland|\
GRD:Grenada|\
GLP:Guadeloupe|\
GUM:Guam|\
GTM:Guatemala|\
GIN:Guinea|\
GNB:Guinea-Bissau|\
GUY:Guyana|\
HTI:Haiti|\
VAT:Holy See (Vatican City State)|\
HND:Honduras|\
HKG:Hong Kong|\
HUN:Hungary|\
ISL:Iceland|\
IND:India|\
IDN:Indonesia|\
IRN:Iran, Islamic Republic of|\
IRQ:Iraq|\
IRL:Ireland|\
ISR:Israel|\
ITA:Italy|\
JAM:Jamaica|\
JPN:Japan|\
JOR:Jordan|\
KAZ:Kazakhstan|\
KEN:Kenya|\
KIR:Kiribati|\
PRK:Korea, Democratic People\'s Republic of|\
KOR:Korea, Republic of|\
KWT:Kuwait|\
KGZ:Kyrgyzstan|\
LAO:Lao People\'s Democratic Republic|\
LVA:Latvia|\
LBN:Lebanon|\
LSO:Lesotho|\
LBR:Liberia|\
LBY:Libyan Arab Jamahiriya|\
LIE:Liechtenstein|\
LTU:Lithuania|\
LUX:Luxembourg|\
MAC:Macao|\
MKD:Macedonia, the Former Yugoslav Republic of|\
MDG:Madagascar|\
MWI:Malawi|\
MYS:Malaysia|\
MDV:Maldives|\
MLI:Mali|\
MLT:Malta|\
MHL:Marshall Islands|\
MTQ:Martinique|\
MRT:Mauritania|\
MUS:Mauritius|\
MEX:Mexico|\
FSM:Micronesia, Federated States of|\
MDA:Moldova, Republic of|\
MCO:Monaco|\
MNG:Mongolia|\
MSR:Montserrat|\
MAR:Morocco|\
MOZ:Mozambique|\
MMR:Myanmar|\
NAM:Namibia|\
NRU:Nauru|\
NPL:Nepal|\
NLD:Netherlands|\
ANT:Netherlands Antilles|\
NCL:New Caledonia|\
NZL:New Zealand|\
NIC:Nicaragua|\
NER:Niger|\
NGA:Nigeria|\
NIU:Niue|\
NFK:Norfolk Island|\
MNP:Northern Mariana Islands|\
NOR:Norway|\
OMN:Oman|\
PAK:Pakistan|\
PLW:Palau|\
PAN:Panama|\
PNG:Papua New Guinea|\
PRY:Paraguay|\
PER:Peru|\
PHL:Philippines|\
PCN:Pitcairn|\
POL:Poland|\
PRT:Portugal|\
PRI:Puerto Rico|\
QAT:Qatar|\
REU:Reunion|\
ROM:Romania|\
RUS:Russian Federation|\
RWA:Rwanda|\
SHN:Saint Helena|\
KNA:Saint Kitts and Nevis|\
LCA:Saint Lucia|\
SPM:Saint Pierre and Miquelon|\
VCT:Saint Vincent and the Grenadines|\
WSM:Samoa|\
SMR:San Marino|\
STP:Sao Tome and Principe|\
SAU:Saudi Arabia|\
SEN:Senegal|\
SCG:Serbia and Montenegro\
SYC:Seychelles|\
SLE:Sierra Leone|\
SGP:Singapore|\
SVK:Slovakia|\
SVN:Slovenia|\
SLB:Solomon Islands|\
SOM:Somalia|\
ZAF:South Africa|\
ESP:Spain|\
LKA:Sri Lanka|\
SDN:Sudan|\
SUR:Suriname|\
SJM:Svalbard and Jan Mayen|\
SWZ:Swaziland|\
SWE:Sweden|\
CHE:Switzerland|\
SYR:Syrian Arab Republic|\
TWN:Taiwan, Province of China|\
TJK:Tajikistan|\
TZA:Tanzania, United Republic of|\
THA:Thailand|\
TGO:Togo|\
TKL:Tokelau|\
TON:Tonga|\
TTO:Trinidad and Tobago|\
TUN:Tunisia|\
TUR:Turkey|\
TKM:Turkmenistan|\
TCA:Turks and Caicos Islands|\
TUV:Tuvalu|\
UGA:Uganda|\
UKR:Ukraine|\
ARE:United Arab Emirates|\
GBR:United Kingdom|\
USA:United States|\
URY:Uruguay|\
UZB:Uzbekistan|\
VUT:Vanuatu|\
VEN:Venezuela|\
VNM:Viet Nam|\
VGB:Virgin Islands, British|\
VIR:Virgin Islands, U.s.|\
WLF:Wallis and Futuna|\
ESH:Western Sahara|\
YEM:Yemen|\
ZMB:Zambia|\
ZWE:Zimbabwe|\
';

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) {
  if ( postCountry != '' ) {
    defaultCountry = postCountry;
  }
  var countryLineArray = country.split('|');  // Split into lines
  if (typeof(sel_cou)=="undefined")
  	  sel_cou = 'Select Country';
  var selObj = document.getElementById('countrySelect');
  selObj.options[0] = new Option(sel_cou,'');
  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(cmbCountryId, cmbStatesId, defaultState, textBoxClass, cmbClass) {        
  var selObj = document.getElementById(cmbStatesId);  
  var foundState = false;
  // Empty options just in case new drop down is shorter  
  if ( selObj.type == 'select-one' ) {
    for (var i = 0; i < selObj.options.length; i++) {
      selObj.options[i] = null;
    }
    selObj.options.length=null;
    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;
  var selIndex = document.getElementById(cmbCountryId).selectedIndex;
  var selCountry = document.getElementById(cmbCountryId).options[selIndex].value;  
  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 (selCountry == countryCode && countryCode != '' ) {
    // If it's a input element, change it to a select
	  
      if ( selObj.type == 'text' ) {
        parentObj = document.getElementById(cmbStatesId).parentNode;
        childObj = document.getElementById(cmbStatesId);
        removeChildrenFromNode(parentObj, childObj);
        var inputSel = document.createElement("SELECT");
        inputSel.setAttribute("name","state");
        inputSel.setAttribute("id",cmbStatesId);
        inputSel.setAttribute("class",cmbClass);
        inputSel.setAttribute("onchange","validateState();");		
        parentObj.appendChild(inputSel) ;
		// re-create error span
		
		var newSpan = document.createElement("SPAN");
		newSpan.setAttribute("name","StateError");
        newSpan.setAttribute("id","StateError");		
		parentObj.appendChild(newSpan) ;
		
        selObj = document.getElementById(cmbStatesId);
        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 == postState && countryCode == postCountry ) {
        selObj.selectedIndex = optionCntr;
      }
	  if ( defaultState == stateCode ) {
        selObj.selectedIndex = optionCntr;
      }	  
      foundState = true;
      optionCntr++
    }
  }
  // If the country has no states, change the select to a text box
  if ( ! foundState ) {
    parentObj = document.getElementById(cmbStatesId).parentNode;
    childObj = document.getElementById(cmbStatesId);
    removeChildrenFromNode(parentObj, childObj);
  // Create the Input Field
    var inputEl = document.createElement("INPUT");
    inputEl.setAttribute("id", cmbStatesId);
    inputEl.setAttribute("type", "text");
    inputEl.setAttribute("name", "state");
    inputEl.setAttribute("size", 20);
    inputEl.setAttribute("value", postState);
    inputEl.setAttribute("class", textBoxClass);
	if (defaultState != null)
		inputEl.setAttribute("value", defaultState);
   // inputEl.setAttribute("onblur", "validateStateInput();");
    parentObj.appendChild(inputEl) ;
	/*
	// re-create error span
	var newSpan = document.createElement("SPAN");
	var br =  document.createElement("BR");
	newSpan.setAttribute("name","StateError");
	newSpan.setAttribute("id","StateError");
	parentObj.appendChild(br) ;
	parentObj.appendChild(newSpan) ;
	*/
  }
}

function removeChildrenFromNode(parentNode, childNode)
{   	
	parentNode.removeChild(childNode);
}

function initCountry(country) {
  populateCountry(country);
  populateState();
}
function initCountry(country,state) {
  populateCountry(country);
  populateState(state);
}
