var USstates = new Array();
USstates[0] = 'AL|Alabama';
USstates[1] = 'AK|Alaska';
USstates[2] = 'AS|American Samoa';
USstates[3] = 'AZ|Arizona';
USstates[4] = 'AR|Arkansas';
USstates[5] = 'CA|California';
USstates[6] = 'CO|Colorado';
USstates[7] = 'CT|Connecticut';
USstates[8] = 'DC|D.C.';
USstates[9] = 'DE|Delaware';
USstates[10] = 'FL|Florida';
USstates[11] = 'GA|Georgia';
USstates[12] = 'GU|Guam';
USstates[13] = 'HI|Hawaii';
USstates[14] = 'ID|Idaho';
USstates[15] = 'IL|Illinois';
USstates[16] = 'IN|Indiana';
USstates[17] = 'IA|Iowa';
USstates[18] = 'KS|Kansas';
USstates[19] = 'KY|Kentucky';
USstates[20] = 'LA|Louisiana';
USstates[21] = 'ME|Maine';
USstates[22] = 'MP|Marianas';
USstates[23] = 'MH|Marshall Islands';
USstates[24] = 'MD|Maryland';
USstates[25] = 'MA|Massachusetts';
USstates[26] = 'MI|Michigan';
USstates[27] = 'FM|Micronesia';
USstates[28] = 'AA|Military Americas';
USstates[29] = 'AE|Military Europe/ME/Canada';
USstates[30] = 'AP|Military Pacific';
USstates[31] = 'MN|Minnesota';
USstates[32] = 'MS|Mississippi';
USstates[33] = 'MO|Missouri';
USstates[34] = 'MT|Montana';
USstates[35] = 'NE|Nebraska';
USstates[36] = 'NV|Nevada';
USstates[37] = 'NH|New Hampshire';
USstates[38] = 'NJ|New Jersey';
USstates[39] = 'NM|New Mexico';
USstates[40] = 'NY|New York';
USstates[41] = 'NC|North Carolina';
USstates[42] = 'ND|North Dakota';
USstates[43] = 'OH|Ohio';
USstates[44] = 'OK|Oklahoma';
USstates[45] = 'OR|Oregon';
USstates[46] = 'PW|Palau';
USstates[47] = 'PA|Pennsylvania';
USstates[48] = 'PR|Puerto Rico';
USstates[49] = 'RI|Rhode Island';
USstates[50] = 'SC|South Carolina';
USstates[51] = 'SD|South Dakota';
USstates[52] = 'TN|Tennessee';
USstates[53] = 'TX|Texas';
USstates[54] = 'UT|Utah';
USstates[55] = 'VT|Vermont';
USstates[56] = 'VI|Virgin Islands';
USstates[57] = 'VA|Virginia';
USstates[58] = 'WA|Washington';
USstates[59] = 'WV|West Virginia';
USstates[60] = 'WI|Wisconsin';
USstates[61] = 'WY|Wyoming';

var CAprovinces = new Array();
CAprovinces[0] = 'AB|Alberta';
CAprovinces[1] = 'BC|British Columbia';
CAprovinces[2] = 'MB|Manitoba';
CAprovinces[3] = 'NB|New Brunswick';
CAprovinces[4] = 'NL|Newfoundland and Labrador';
CAprovinces[5] = 'NS|Nova Scotia';
CAprovinces[6] = 'NT|Northwest Territories';
CAprovinces[7] = 'NU|Nunavut';
CAprovinces[8] = 'ON|Ontario';
CAprovinces[9] = 'PE|Prince Edward Island';
CAprovinces[10] = 'QC|Quebec';
CAprovinces[11] = 'SK|Saskatchewan';
CAprovinces[12] = 'YT|Yukon';


var JPprefecture = new Array();
JPprefecture[0] = '北海道|北海道';
JPprefecture[1] = '青森県|青森県';
JPprefecture[2] = '岩手県|岩手県';
JPprefecture[3] = '宮城県|宮城県';
JPprefecture[4] = '秋田県|秋田県';
JPprefecture[5] = '山形県|山形県';
JPprefecture[6] = '福島県|福島県';
JPprefecture[7] = '茨城県|茨城県';
JPprefecture[8] = '栃木県|栃木県';
JPprefecture[9] = '群馬県|群馬県';
JPprefecture[10] = '埼玉県|埼玉県';
JPprefecture[11] = '千葉県|千葉県';
JPprefecture[12] = '東京都|東京都';
JPprefecture[13] = '神奈川県|神奈川県';
JPprefecture[14] = '新潟県|新潟県';
JPprefecture[15] = '富山県|富山県';
JPprefecture[16] = '石川県|石川県';
JPprefecture[17] = '福井県|福井県';
JPprefecture[18] = '山梨県|山梨県';
JPprefecture[19] = '長野県|長野県';
JPprefecture[20] = '岐阜県|岐阜県';
JPprefecture[21] = '静岡県|静岡県';
JPprefecture[22] = '愛知県|愛知県';
JPprefecture[23] = '三重県|三重県';
JPprefecture[24] = '滋賀県|滋賀県';
JPprefecture[25] = '京都府|京都府';
JPprefecture[26] = '大阪府|大阪府';
JPprefecture[27] = '兵庫県|兵庫県';
JPprefecture[28] = '奈良県|奈良県';
JPprefecture[29] = '和歌山県|和歌山県';
JPprefecture[30] = '鳥取県|鳥取県';
JPprefecture[31] = '島根県|島根県';
JPprefecture[32] = '岡山県|岡山県';
JPprefecture[33] = '広島県|広島県';
JPprefecture[34] = '山口県|山口県';
JPprefecture[35] = '徳島県|徳島県';
JPprefecture[36] = '香川県|香川県';
JPprefecture[37] = '愛媛県|愛媛県';
JPprefecture[38] = '高知県|高知県';
JPprefecture[39] = '福岡県|福岡県';
JPprefecture[40] = '佐賀県|佐賀県';
JPprefecture[41] = '長崎県|長崎県';
JPprefecture[42] = '熊本県|熊本県';
JPprefecture[43] = '大分県|大分県';
JPprefecture[44] = '宮崎県|宮崎県';
JPprefecture[45] = '鹿児島県|鹿児島県';
JPprefecture[46] = '沖縄県|沖縄県';

function updateStates(country) {
   var areas, label;
   if(country == 'US') {
      setZipLabel('Zip code'); 
   } else {
      setZipLabel('Postal code');
   }


   if (country == 'US') {
      areas = USstates;
      label = 'State';
   }
   else if (country == 'CA') {
      areas = CAprovinces;
      label = 'Province';
   }
   else if (country == 'JP') {
      areas = JPprefecture;
      label = 'Prefecture';
   }
   else {
      $('#state').replaceWith('<input id="state" name="state" value="' + $('#state').val() + '"/>');
      label = 'Region';
      setStateLabel(label);   
      return;
   }

   setStateLabel(label);
   val = $('#state').val();
   $('#state').replaceWith('<select id="state" name="state"></select>');
   $('#state').append('<option value="">-Select-</option>\n');
   jQuery.each(areas, function() {
      bits = this.split("|",2);
      sel = bits[0] == val ? 'SELECTED' : '';
      $('#state').append('<option value="'+bits[0]+'" '+sel+'>'+bits[1]+'</option>');
   });
}

function setStateLabel(label) {
   $('#state').closest('tr').find('label').html(label + ':');
}

function setZipLabel(label) {
   $('#zip').closest('tr').find('label').html(label + ':');
}

