 
<!-- Paste this code into an external JavaScript file  -->

/* 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 = '<?= $_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 = '\
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: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 ISLANDS|\
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 EUROPE/ME/CANADA|\
US:AP:MILITARY PACIFIC|\
US:NO:NO APLICABLE|\
CA:AB:ALBERTA|\
//CA:MB:MANITOBA|\
//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:NU:NUNAVUT|\
CA:ON:ONTARIO|\
CA:PE:PRINCE EDWARD ISLAND|\
CA:QC:QUEBEC|\
CA:SK:SASKATCHEWAN|\
CA:NO:NO APLICABLE|\
MX:AG:AGUASCALIENTES|\
MX:BC:BAJA CALIFORNIA|\
MX:BS:BAJA CALIFORNIASUR|\
MX:CM:CAMPECHE|\
MX:CS:CHIAPAS|\
MX:CH:CHIHUAHUA|\
MX:CO:COAHUILA|\
MX:CL:COLIMA|\
MX:DF:FEDERAL DISTRICT|\
MX:DG:DURANGO|\
MX:GT:GUANAJUATO|\
MX:GR:GUERRERO|\
MX:HG:HIDALGO|\
MX:JA:JALISCO|\
MX:ME:MEXICO STATE|\
MX:MI:MICHOACAN|\
MX:MO:MORELOS|\
MX:NA:NAYARIT|\
MX:NL:NUEVO LEON|\
MX:OA:OAXACA|\
MX:PB:PUEBLA|\
MX:QE:QUERETARO|\
MX:QR:QUINTANA ROO|\
MX:SL:SAN LUIS POTOSI|\
MX:SI:SINALOA|\
MX:SO:SONORA|\
MX:TB:TABASCO|\
MX:TM:TAMAULIPAS|\
MX:TL:TLAXCALA|\
MX:VE:VERACRUZ|\
MX:YU:YUCATAN|\
MX:ZA:ZACATECAS|\
MX:NO:NO APLICABLE|\
';

// 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|\
CA:CANADA|\
MX:MEXICO|\
-:--------------------|\
AD:ABU DABHI|\
AL:ALBANIA|\
AG:ALGERIA|\
AN:ANDORRA|\
AO:ANGOLA|\
AU:ANGUILLA|\
AT:ANTIGUA|\
AR:ARGENTINA|\
AM:ARMENIA|\
AA:ARUBA|\
AS:AUSTRALIA|\
AI:AUSTRIA|\
AZ:AZERBAIJAN|\
BA:BAHAMAS|\
BH:BAHRAIN|\
BD:BANGLADESH|\
BR:BARBADOS|\
BE:BELARUS|\
BL:BELGIUM|\
BI:BELIZE|\
BN:BENIN|\
BU:BERMUDA|\
BT:BHUTAN|\
BV:BOLIVIA|\
BO:BONAIRE|\
BG:BOSNIA-HERZEG|\
BW:BOTSWANA|\
BZ:BRAZIL|\
BS:BRITISH VIRGIN ISL.|\
BB:BRUNEI|\
BJ:BULGARIA|\
BF:BURKINA|\
BK:BURUNDI|\
CR:C.AFRICAN REPUBLIC|\
CM:CAMBODIA|\
CE:CAMEROON|\
CI:CANARY ISLANDS|\
CV:CAPE|\
CY:CAYMAN ISLANDS|\
CH:CHAD|\
CN:CHANNEL ISLANDS|\
CL:CHILE|\
CC:CHINA|\
CL:COLUMBIA|\
CO:COMOROS|\
CG:CONGO|\
CK:COOK ISLANDS|\
CF:CORSICA - FRANCE|\
CT:COSTA RICA|\
CJ:CROATIA|\
CU:CUBA|\
CP:CYPRUS|\
CZ:CZECH REPUBLIC|\
DM:DENMARK|\
DJ:DJIBOUTI|\
DR:DOMINICAN REPUBLIC|\
EC:ECUADOR|\
EG:EGYPT|\
EL:EL SALVADOR|\
EN:ENGLAND|\
EQ:EQUATORIAL GUINEA|\
ER:ERITREA|\
ES:ESTONIA|\
ET:ETHIOPIA|\
FI:FALKLAND ISLANDS|\
FD:FAROE ISL. DENMARK|\
FJ:FIJI|\
FL:FINLAND|\
FR:FRANCE|\
FG:FRENCH GUIANA|\
GA:GABON|\
GM:GAMBIA|\
GE:GEORGIA REPUBLIC|\
GR:GERMANY|\
GH:GHANA|\
GB:GIBRALTAR|\
GC:GREECE|\
GL:GREENLAND|\
GN:GRENADA|\
GO:GUADELOUPE|\
GU:GUAM|\
GT:GUATEMALA|\
GK:GUINEA|\
GS:GUINEA-BISSAU|\
GY:GUYANA|\
HT:HAITI|\
HN:HONDURAS|\
HK:HONG KONG|\
HG:HUNGARY|\
IC:ICELAND|\
IN:INDIA|\
ID:INDONESIA|\
IR:IRAN|\
IQ:IRAQ|\
IS:IRELAND REPUBLIC - S.|\
IE:ISRAEL|\
IT:ITALY|\
IV:IVORY|\
JM:JAMAICA|\
JN:JAPAN|\
JO:JORDAN|\
KM:KAMPUCHEA|\
KZ:KAZAKHSTAN|\
KN:KENYA|\
KR:KIRIBATI|\
KU:KUWAIT|\
KS:KYRGYZSTAN|\
LA:LAOS|\
LT:LATVIA|\
LE:LEBANON|\
LS:LESOTHO|\
LB:LIBERIA|\
LC:LIECHTENSTEIN|\
LU:LITHUANIA|\
LX:LUXEMBOURG|\
MC:MACAO|\
ME:MACEDONIA|\
MS:MADAGASCAR|\
ML:MALAWI|\
MA:MALAYSIA|\
MD:MALDIVES|\
MT:MALI|\
MW:MALTA|\
MI:MARSHALL ISL.|\
MR:MARTINIQUE|\
MU:MAURITANIA|\
MK:MAURITIUS|\
MJ:MICRONESIA|\
MO:MOLDOVA|\
MF:MONACO - FRANCE|\
MN:MONGOLIA|\
MQ:MONTSERRAT|\
MP:MOROCCO|\
MZ:MOZAMBIQUE|\
NM:NAMIBIA|\
NA:NAURU|\
NE:NEPAL|\
NT:NETHERLANDS|\
NV:NEVIS|\
NC:NEW CALEDONIA|\
NZ:NEW ZEALAND|\
NR:NICARAGUA|\
NG:NIGER|\
NI:NIGERIA|\
NU:NIUE|\
NK:NORTH KOREA|\
NW:NORWAY|\
OM:OMAN|\
PK:PAKISTAN|\
PL:PALAU|\
PN:PANAMA|\
PG:PAPUA NEW GUINEA|\
PE:PERU|\
PH:PHILIPPINES|\
PO:POLAND|\
PR:PORTUGAL|\
RA:RUSSIA|\
SA:SAIPAN|\
SB:SAMOA - AMERICAN|\
SC:SAN MARINO|\
SD:SAO TOME|\
SE:SAUDI ARABIA|\
SF:SCOTLAND|\
SG:SENEGAL|\
SH:SEYCHELLES|\
SI:SIERRA LEONE|\
SJ:SINGAPORE|\
SK:SLOVAKIA - SLOVAK|\
SL:SLOVENIA|\
SM:SOLOMAN ISLANDS|\
SN:SOMALIA|\
SO:SOUTH KOREA|\
SP:SPAIN|\
SQ:SRI LANKA|\
SR:ST. BARTHELEMY|\
SS:ST. EUSTATIUS|\
ST:ST. KITTS|\
SU:ST. LUCIA|\
SV:ST. MAARTEN|\
SW:SUDAN|\
SX:SURINAME|\
SY:SWAZILAND|\
SZ:SWEDEN|\
S0:SWITZERLAND|\
S1:SYRIA|\
TA:TAHITI|\
TB:TAIWAN|\
TC:TANZANIA|\
TD:TARKISTAN|\
TE:THAILAND|\
TF:TOGO|\
TG:TONGA|\
TH:TRINIDAD & TOBAGO|\
TI:TUNISIA|\
TJ:TURKEY|\
TK:TURKMENISTAN|\
TL:TURKS & CAICOS|\
TM:TUVALU|\
UG:UGANDA|\
UR:UKRAINE|\
UA:UNITED ARAB EMIR.|\
UK:UNITED KINGDOM|\
UU:URUGUAY|\
UZ:UZBEKISTAN|\
VA:VANUATU|\
VC:VATICAN  CITY|\
VE:VENEZUELA|\
VE:VIETNAM|\
VI:VIRGIN ISLANDS|\
WA:WAKE ISLAND|\
WA:WALES|\
WI:WEST INDIES|\
WS:WESTERN SAMOA|\
YI:YEMEN|\
YU:YUGOSLAVIA|\
ZA:ZAIRE|\
ZM:ZAMBIA|\
ZB: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
  var selObj = document.getElementById('countrySelect');
  selObj.options[0] = new Option('Seleccione Pais',''); // Select Country --- Seleccione Pais (Spanish Conversion)
  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);
	  //selObj.options[loop + 1] = new Option(countryName, countryName);
    }
    if ( defaultCountry == countryCode ) {
      selObj.selectedIndex = loop + 1;
    }
  }
}

function populateState() {
  var selObj = document.getElementById('stateSelect');
  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.options[0] = new Option('Elija Provincia',''); //Select Province ---- Seleccione Provincia (Spanish Conversion)
	if (document.getElementById('countrySelect').value == 'CA' )
	{
		 selObj.options[0] = new Option('Seleccione Provincia',''); //Select Province ---- Seleccione Provincia (Spanish Conversion)
	}
	else
	{
		selObj.options[0] = new Option('Seleccione Estado',''); //Select State ---- Seleccione Estado (Spanish Conversion)
	}

    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('countrySelect').value == countryCode && countryCode != '' ) 
  {
    // If it's a input element, change it to a select
      if ( selObj.type == 'text' ) {
        parentObj = document.getElementById('stateSelect').parentNode;
        parentObj.removeChild(selObj);
        var inputSel = document.createElement("SELECT");
        inputSel.setAttribute("name","customer_state");
        inputSel.setAttribute("id","stateSelect");
		parentObj.appendChild(inputSel) ;

		document.getElementById("stateSelect").style.width="155px";
		document.getElementById("stateSelect").style.border="1px";
		document.getElementById("stateSelect").style.backgroundColor="#ffffff";
		document.getElementById("stateSelect").style.color="#897E5C";
		document.getElementById("stateSelect").style.fontWeight="normal";
		document.getElementById("stateSelect").style.fontSize="10px";
		document.getElementById("stateSelect").style.fontFamily="Arial, Helvetica, sans-serif";
		document.getElementById("stateSelect").style.borderColor="#ACA084";
		document.getElementById("stateSelect").style.marginTop="0px";
		document.getElementById("stateSelect").style.height="19px";

        selObj = document.getElementById('stateSelect');
       // selObj.options[0] = new Option('Select Province','');

		if (document.getElementById('countrySelect').value == 'CA' ){selObj.options[0] = new Option('Seleccione Provincia','');}else{selObj.options[0] = new Option('Seleccione Estado','');}
		//selObj.options[0] = new Option('Seleccione Provincia','');
        selObj.selectedIndex = 0;
      }
      if ( stateCode != '' ) {
        //selObj.options[optionCntr] = new Option(stateName, stateCode);
		selObj.options[optionCntr] = new Option(stateName, stateName);
      }
      // See if it's selected from a previous post
      if ( stateCode == postState && countryCode == postCountry ) {
        selObj.selectedIndex = optionCntr;
      }
      foundState = true;
      optionCntr++
    }
  }
  // If the country has no states, change the select to a text box
  if ( ! foundState ) {
    parentObj = document.getElementById('stateSelect').parentNode;
    parentObj.removeChild(selObj);
  // Create the Input Field
    var inputEl = document.createElement("INPUT");
    inputEl.setAttribute("id", "stateSelect");
    inputEl.setAttribute("type", "text");
	inputEl.setAttribute("class", "contact");
    inputEl.setAttribute("name", "customer_state");
	inputEl.setAttribute("style", "width:155px");
    inputEl.setAttribute("size", 22);
    inputEl.setAttribute("value", postState);
    parentObj.appendChild(inputEl) ;

	document.getElementById("stateSelect").style.color="#897E5C";
	document.getElementById("stateSelect").style.fontWeight="normal";
	document.getElementById("stateSelect").style.fontSize="10px";
	document.getElementById("stateSelect").style.fontFamily="Arial, Helvetica, sans-serif";
	document.getElementById("stateSelect").style.width="155px";
			
  }
}

function initCountry(country) {
  populateCountry(country);
  populateState();
}
