
var gecko = navigator.userAgent.toLowerCase().indexOf("gecko") != -1;

function addLoadFunc(func) {var oldonload = window.onload; if (typeof window.onload != 'function') {window.onload = func;  } else {window.onload = function() {if (oldonload) {oldonload();}func();}}}

function clearSelect(select) {
    select.options.length = 0;
}

function addNoValueEntry(select, value) {
    if (!gecko) {
        var cLength = select.length;
        select.length = cLength + 1;
        select.options[cLength].text = value;
        select.options[cLength].value = "";
    } else {
        var option = new Option(value, "", false, false);
        select.options[select.length] = option;
    }
}

function addStateEntry(select, value, code) {
    if (!code) code = value;
    if (!gecko) {
        var cLength = select.length;
        select.length = cLength + 1;
        select.options[cLength].text = value;
        select.options[cLength].value = code;
    } else {
        var option = new Option(value, code, false, false);
        select.options[select.length] = option;
    }
}

function updateCountryFromState(state, country) {
  var selected = state.options[state.selectedIndex].value;
  if (state.length > 0) {
    var desiredCountry = getCountryCodeFromState(selected);
    for (i = 0 ; i < country.options.length ; i++) {
      if (country.options[i].value == desiredCountry) {
        country.selectedIndex = i;
        break;
      }
    }
  }
}

function getCountryCodeFromState(code) {
  for (i = 0 ; i < stateCountry.length ; i++) {
    if (stateCountry[i][1] == code) {
      return stateCountry[i][0];
    }
  }
  return "US";
}

function updateState(country, state) { 
  var selected = state.options[state.selectedIndex].value;
  clearSelect(state);
  var value = country.options[country.selectedIndex].value;
  var foundSelected = false;
  
  if (country.selectedIndex == 0) {
    for (i = 0 ; i < stateCountry.length ; i++) {
      addStateEntry(state, stateCountry[i][2], stateCountry[i][3]);
      //if (stateCountry[i][1] == selectedId[0][state.id]){
      //  state.selectedIndex = state.options.length - 1;
      //  foundSelected = true;
      //}
    }
  } else {
    for (i = 0 ; i < stateCountry.length ; i++) {
      if (stateCountry[i][0] != value) continue;
      addStateEntry(state, stateCountry[i][2], stateCountry[i][3]);
      //if (stateCountry[i][1] == selectedId[0][state.id]) {
      //  state.selectedIndex = state.options.length - 1;
      //  foundSelected = true;
      //}
    }
    if (state.options.length == 0) {
      addNoValueEntry(state, '-- Not Applicable --'); }
      else {
      var option = new Option('Please Choose', '', false, false);
      state.options.add(option,0);
      if (foundSelected == false) state.selectedIndex = 0;
      }
      
  }
  if (!foundSelected && !gecko) state.selectedIndex = 0;
}
var stateCountry = new Array(74);
stateCountry[0]=['204', 'AL', 'Alabama','1']
stateCountry[1]=['204', 'AK', 'Alaska','2']
stateCountry[2]=['204', 'AZ', 'Arizona','3']
stateCountry[3]=['204', 'AR', 'Arkansas','4']
stateCountry[4]=['204', 'CA', 'California','5']
stateCountry[5]=['204', 'CO', 'Colorado','6']
stateCountry[6]=['204', 'CT', 'Connecticut','7']
stateCountry[7]=['204', 'DE', 'Delaware','8']
stateCountry[8]=['204', 'DC', 'District of Columbia','9']
stateCountry[9]=['204', 'FL', 'Florida','10']
stateCountry[10]=['204', 'GA', 'Georgia','11']
stateCountry[11]=['204', 'HI', 'Hawaii','12']
stateCountry[12]=['204', 'ID', 'Idaho','13']
stateCountry[13]=['204', 'IL', 'Illinois','14']
stateCountry[14]=['204', 'IN', 'Indiana','15']
stateCountry[15]=['204', 'IA', 'Iowa','16']
stateCountry[16]=['204', 'KS', 'Kansas','17']
stateCountry[17]=['204', 'KY', 'Kentucky','18']
stateCountry[18]=['204', 'LA', 'Louisiana','19']
stateCountry[19]=['204', 'ME', 'Maine','20']
stateCountry[20]=['204', 'MD', 'Maryland','21']
stateCountry[21]=['204', 'MA', 'Massachusetts','22']
stateCountry[22]=['204', 'MI', 'Michigan','23']
stateCountry[23]=['204', 'MN', 'Minnesota','24']
stateCountry[24]=['204', 'MS', 'Mississippi','25']
stateCountry[25]=['204', 'MO', 'Missouri','26']
stateCountry[26]=['204', 'MT', 'Montana','27']
stateCountry[27]=['204', 'NE', 'Nebraska','28']
stateCountry[28]=['204', 'NV', 'Nevada','29']
stateCountry[29]=['204', 'NH', 'New Hampshire','30']
stateCountry[30]=['204', 'NJ', 'New Jersey','31']
stateCountry[31]=['204', 'NM', 'New Mexico','32']
stateCountry[32]=['204', 'NY', 'New York','33']
stateCountry[33]=['204', 'NC', 'North Carolina','34']
stateCountry[34]=['204', 'ND', 'North Dakota','35']
stateCountry[35]=['204', 'OH', 'Ohio','36']
stateCountry[36]=['204', 'OK', 'Oklahoma','37']
stateCountry[37]=['204', 'OR', 'Oregon','38']
stateCountry[38]=['204', 'PA', 'Pennsylvania','39']
stateCountry[39]=['204', 'PR', 'Puerto Rico','40']
stateCountry[40]=['204', 'RI', 'Rhode Island','41']
stateCountry[41]=['204', 'SC', 'South Carolina','42']
stateCountry[42]=['204', 'SD', 'South Dakota','43']
stateCountry[43]=['204', 'TN', 'Tennessee','44']
stateCountry[44]=['204', 'TX', 'Texas','45']
stateCountry[45]=['204', 'UT', 'Utah','46']
stateCountry[46]=['204', 'VT', 'Vermont','47']
stateCountry[47]=['204', 'VA', 'Virginia','48']
stateCountry[48]=['204', 'WA', 'Washington','49']
stateCountry[49]=['204', 'WV', 'West Virginia','50']
stateCountry[50]=['204', 'WI', 'Wisconsin','51']
stateCountry[51]=['204', 'WY', 'Wyoming','52']
stateCountry[52]=['37', 'AB', 'Alberta','53']
stateCountry[53]=['37', 'BC', 'British Columbia','54']
stateCountry[54]=['37', 'MB', 'Manitoba','55']
stateCountry[55]=['37', 'NB', 'New Brunswick','56']
stateCountry[56]=['37', 'NF', 'Newfoundland','57']
stateCountry[57]=['37', 'NT', 'Northwest Territory','58']
stateCountry[58]=['37', 'NS', 'Nova Scotia','59']
stateCountry[59]=['37', 'ON', 'Ontario','60']
stateCountry[60]=['37', 'PE', 'Prince Edward Island','61']
stateCountry[61]=['37', 'PQ', 'Quebec','62']
stateCountry[62]=['37', 'SK', 'Saskatcheqan','63']
stateCountry[63]=['37', 'SP', 'St. Pierre & Miquelon','64']
stateCountry[64]=['37', 'YU', 'Yukon Territory','65']
stateCountry[65]=['12', 'AC', 'Australia Capital Territory','66']
stateCountry[66]=['12', 'NW', 'New South Wales','67']
stateCountry[67]=['12', 'NI', 'Norfolk Island','68']
stateCountry[68]=['12', 'NO', 'Northern Territory','69']
stateCountry[69]=['12', 'QL', 'Queensland','70']
stateCountry[70]=['12', 'SA', 'South Australia','71']
stateCountry[71]=['12', 'TS', 'Tasmania','72']
stateCountry[72]=['12', 'VC', 'Victoria','73']
stateCountry[73]=['12', 'WT', 'Western Australia','74']
