//illegal characters in GET cause issues - fix in PHP ?


var postState = '';
var postCounty = '';

// State table
var state = '\
CA:California|\
CO:Colorado|\
FL:Florida|\
ID:Idaho|\
IN:Indiana|\
MA:Massachusetts|\
MD:Maryland|\
MN:Minnesota|\
NC:North Carolina|\
NM:New Mexico|\
PA:Pennsylvania|\
SC:South Carolina|\
VA:Virginia';

// County data table
var county = '\
CA:Alameda|\
CA:Alpine|\
CA:Amador|\
CA:Butte|\
CA:Calaveras|\
CA:Colusa|\
CA:Contra Costa|\
CA:Del Norte|\
CA:El Dorado|\
CA:Fresno|\
CA:Glenn|\
CA:Humboldt|\
CA:Imperial|\
CA:Inyo|\
CA:Kern|\
CA:Kings|\
CA:Lake|\
CA:Lassen|\
CA:Los Angeles|\
CA:Madera|\
CA:Marin|\
CA:Mariposa|\
CA:Mendocino|\
CA:Merced|\
CA:Modoc|\
CA:Mono|\
CA:Monterey|\
CA:Napa|\
CA:Nevada|\
CA:Orange|\
CA:Placer|\
CA:Plumas|\
CA:Riverside|\
CA:Sacramento|\
CA:San Benito|\
CA:San Bernardino|\
CA:San Diego|\
CA:San Francisco|\
CA:San Joaquin|\
CA:San Luis Obispo|\
CA:San Mateo|\
CA:Santa Barbara|\
CA:Santa Clara|\
CA:Santa Cruz|\
CA:Shasta|\
CA:Sierra|\
CA:Siskiyou|\
CA:Solano|\
CA:Sonoma|\
CA:Stanislaus|\
CA:Sutter|\
CA:Tehama|\
CA:Trinity|\
CA:Tulare|\
CA:Tuolumne|\
CA:Ventura|\
CA:Yolo|\
CA:Yuba|\
CO:Adams|\
CO:Alamosa|\
CO:Arapahoe|\
CO:Archuleta|\
CO:Baca|\
CO:Bent|\
CO:Boulder|\
CO:Broomfield|\
CO:Chaffee|\
CO:Cheyenne|\
CO:Clear Creek|\
CO:Conejos|\
CO:Costilla|\
CO:Crowley|\
CO:Custer|\
CO:Delta|\
CO:Denver|\
CO:Dolores|\
CO:Douglas|\
CO:Eagle|\
CO:El Paso|\
CO:Elbert|\
CO:Fremont|\
CO:Garfield|\
CO:Gilpin|\
CO:Grand|\
CO:Gunnison|\
CO:Hinsdale|\
CO:Huerfano|\
CO:Jackson|\
CO:Jefferson|\
CO:Kiowa|\
CO:Kit Carson|\
CO:La Plata|\
CO:Lake|\
CO:Larimer|\
CO:Las Animas|\
CO:Lincoln|\
CO:Logan|\
CO:Mesa|\
CO:Mineral|\
CO:Moffat|\
CO:Montezuma|\
CO:Montrose|\
CO:Morgan|\
CO:Otero|\
CO:Ouray|\
CO:Park|\
CO:Phillips|\
CO:Pitkin|\
CO:Prowers|\
CO:Pueblo|\
CO:Rio Blanco|\
CO:Rio Grande|\
CO:Routt|\
CO:Saguache|\
CO:San Juan|\
CO:San Miguel|\
CO:Sedgwick|\
CO:Summit|\
CO:Teller|\
CO:Washington|\
CO:Weld|\
CO:Yuma|\
FL:Alachua|\
FL:Baker|\
FL:Bay|\
FL:Bradford|\
FL:Brevard|\
FL:Broward|\
FL:Calhoun|\
FL:Charlotte|\
FL:Citrus|\
FL:Clay|\
FL:Collier|\
FL:Columbia|\
FL:De Soto|\
FL:Dixie|\
FL:Duval|\
FL:Escambia|\
FL:Flagler|\
FL:Franklin|\
FL:Gadsden|\
FL:Gilchrist|\
FL:Glades|\
FL:Gulf|\
FL:Hamilton|\
FL:Hardee|\
FL:Hendry|\
FL:Hernando|\
FL:Highlands|\
FL:Hillsborough|\
FL:Holmes|\
FL:Indian River|\
FL:Jackson|\
FL:Jefferson|\
FL:Lafayette|\
FL:Lake|\
FL:Lee|\
FL:Leon|\
FL:Levy|\
FL:Liberty|\
FL:Madison|\
FL:Manatee|\
FL:Marion|\
FL:Martin|\
FL:Miamidade|\
FL:Monroe|\
FL:Nassau|\
FL:Okaloosa|\
FL:Okeechobee|\
FL:Orange|\
FL:Osceola|\
FL:Palm Beach|\
FL:Pasco|\
FL:Pinellas|\
FL:Polk|\
FL:Putnam|\
FL:Santa Rosa|\
FL:Sarasota|\
FL:Seminole|\
FL:St. Johns|\
FL:St. Lucie|\
FL:Sumter|\
FL:Suwannee|\
FL:Taylor|\
FL:Union|\
FL:Volusia|\
FL:Wakulla|\
FL:Walton|\
FL:Washington|\
ID:Ada|\
ID:Adams|\
ID:Bannock|\
ID:Bear Lake|\
ID:Benewah|\
ID:Bingham|\
ID:Blaine|\
ID:Boise|\
ID:Bonner|\
ID:Bonneville|\
ID:Boundary|\
ID:Butte|\
ID:Camas|\
ID:Canyon|\
ID:Caribou|\
ID:Cassia|\
ID:Clark|\
ID:Clearwater|\
ID:Custer|\
ID:Elmore|\
ID:Franklin|\
ID:Fremont|\
ID:Gem|\
ID:Gooding|\
ID:Idaho|\
ID:Jefferson|\
ID:Jerome|\
ID:Kootenai|\
ID:Latah|\
ID:Lemhi|\
ID:Lewis|\
ID:Lincoln|\
ID:Madison|\
ID:Minidoka|\
ID:Nez Perce|\
ID:Oneida|\
ID:Owyhee|\
ID:Payette|\
ID:Power|\
ID:Shoshone|\
ID:Teton|\
ID:Twin Falls|\
ID:Valley|\
ID:Washington|\
IN:Adams|\
IN:Allen|\
IN:Bartholomew|\
IN:Benton|\
IN:Blackford|\
IN:Boone|\
IN:Brown|\
IN:Carroll|\
IN:Cass|\
IN:Clark|\
IN:Clay|\
IN:Clinton|\
IN:Crawford|\
IN:Daviess|\
IN:De Kalb|\
IN:Dearborn|\
IN:Decatur|\
IN:Delaware|\
IN:Dubois|\
IN:Elkhart|\
IN:Fayette|\
IN:Floyd|\
IN:Fountain|\
IN:Franklin|\
IN:Fulton|\
IN:Gibson|\
IN:Grant|\
IN:Greene|\
IN:Hamilton|\
IN:Hancock|\
IN:Harrison|\
IN:Hendricks|\
IN:Henry|\
IN:Howard|\
IN:Huntington|\
IN:Jackson|\
IN:Jasper|\
IN:Jay|\
IN:Jefferson|\
IN:Jennings|\
IN:Johnson|\
IN:Knox|\
IN:Kosciusko|\
IN:La Porte|\
IN:Lagrange|\
IN:Lake|\
IN:Lawrence|\
IN:Madison|\
IN:Marion|\
IN:Marshall|\
IN:Martin|\
IN:Miami|\
IN:Monroe|\
IN:Montgomery|\
IN:Morgan|\
IN:Newton|\
IN:Noble|\
IN:Ohio|\
IN:Orange|\
IN:Owen|\
IN:Parke|\
IN:Perry|\
IN:Pike|\
IN:Porter|\
IN:Posey|\
IN:Pulaski|\
IN:Putnam|\
IN:Randolph|\
IN:Ripley|\
IN:Rush|\
IN:Scott|\
IN:Shelby|\
IN:Spencer|\
IN:St. Joseph|\
IN:Starke|\
IN:Steuben|\
IN:Sullivan|\
IN:Switzerland|\
IN:Tippecanoe|\
IN:Tipton|\
IN:Union|\
IN:Vanderburgh|\
IN:Vermillion|\
IN:Vigo|\
IN:Wabash|\
IN:Warren|\
IN:Warrick|\
IN:Washington|\
IN:Wayne|\
IN:Wells|\
IN:White|\
IN:Whitley|\
MA:Barnstable|\
MA:Berkshire|\
MA:Bristol|\
MA:Dukes|\
MA:Essex|\
MA:Franklin|\
MA:Hampden|\
MA:Hampshire|\
MA:Middlesex|\
MA:Nantucket|\
MA:Norfolk|\
MA:Plymouth|\
MA:Suffolk|\
MA:Worcester|\
MD:Allegany|\
MD:Anne Arundel|\
MD:Baltimore|\
MD:Baltimore City|\
MD:Calvert|\
MD:Caroline|\
MD:Carroll|\
MD:Cecil|\
MD:Charles|\
MD:Dorchester|\
MD:Frederick|\
MD:Garrett|\
MD:Harford|\
MD:Howard|\
MD:Kent|\
MD:Montgomery|\
MD:Prince Georges|\
MD:Queen Annes|\
MD:Somerset|\
MD:St. Marys|\
MD:Talbot|\
MD:Washington|\
MD:Wicomico|\
MD:Worcester|\
MN:Aitkin|\
MN:Anoka|\
MN:Becker|\
MN:Beltrami|\
MN:Benton|\
MN:Big Stone|\
MN:Blue Earth|\
MN:Brown|\
MN:Carlton|\
MN:Carver|\
MN:Cass|\
MN:Chippewa|\
MN:Chisago|\
MN:Clay|\
MN:Clearwater|\
MN:Cook|\
MN:Cottonwood|\
MN:Crow Wing|\
MN:Dakota|\
MN:Dodge|\
MN:Douglas|\
MN:Faribault|\
MN:Fillmore|\
MN:Freeborn|\
MN:Goodhue|\
MN:Grant|\
MN:Hennepin|\
MN:Houston|\
MN:Hubbard|\
MN:Isanti|\
MN:Itasca|\
MN:Jackson|\
MN:Kanabec|\
MN:Kandiyohi|\
MN:Kittson|\
MN:Koochiching|\
MN:Lac Qui Parle|\
MN:Lake|\
MN:Lake Of The Woo|\
MN:Le Sueur|\
MN:Lincoln|\
MN:Lyon|\
MN:Mahnomen|\
MN:Marshall|\
MN:Martin|\
MN:Mcleod|\
MN:Meeker|\
MN:Mille Lacs|\
MN:Morrison|\
MN:Mower|\
MN:Murray|\
MN:Nicollet|\
MN:Nobles|\
MN:Norman|\
MN:Olmsted|\
MN:Otter Tail|\
MN:Pennington|\
MN:Pine|\
MN:Pipestone|\
MN:Polk|\
MN:Pope|\
MN:Ramsey|\
MN:Red Lake|\
MN:Redwood|\
MN:Renville|\
MN:Rice|\
MN:Rock|\
MN:Roseau|\
MN:Scott|\
MN:Sherburne|\
MN:Sibley|\
MN:St. Louis|\
MN:Stearns|\
MN:Steele|\
MN:Stevens|\
MN:Swift|\
MN:Todd|\
MN:Traverse|\
MN:Wabasha|\
MN:Wadena|\
MN:Waseca|\
MN:Washington|\
MN:Watonwan|\
MN:Wilkin|\
MN:Winona|\
MN:Wright|\
MN:Yellow Medicine|\
NC:Alamance|\
NC:Alexander|\
NC:Alleghany|\
NC:Anson|\
NC:Ashe|\
NC:Avery|\
NC:Beaufort|\
NC:Bertie|\
NC:Bladen|\
NC:Brunswick|\
NC:Buncombe|\
NC:Burke|\
NC:Cabarrus|\
NC:Caldwell|\
NC:Camden|\
NC:Carteret|\
NC:Caswell|\
NC:Catawba|\
NC:Chatham|\
NC:Cherokee|\
NC:Chowan|\
NC:Clay|\
NC:Cleveland|\
NC:Columbus|\
NC:Craven|\
NC:Cumberland|\
NC:Currituck|\
NC:Dare|\
NC:Davidson|\
NC:Davie|\
NC:Duplin|\
NC:Durham|\
NC:Edgecombe|\
NC:Forsyth|\
NC:Franklin|\
NC:Gaston|\
NC:Gates|\
NC:Graham|\
NC:Granville|\
NC:Greene|\
NC:Guilford|\
NC:Halifax|\
NC:Harnett|\
NC:Haywood|\
NC:Henderson|\
NC:Hertford|\
NC:Hoke|\
NC:Hyde|\
NC:Iredell|\
NC:Jackson|\
NC:Johnston|\
NC:Jones|\
NC:Lee|\
NC:Lenoir|\
NC:Lincoln|\
NC:Macon|\
NC:Madison|\
NC:Martin|\
NC:Mcdowell|\
NC:Mecklenburg|\
NC:Mitchell|\
NC:Montgomery|\
NC:Moore|\
NC:Nash|\
NC:New Hanover|\
NC:Northampton|\
NC:Onslow|\
NC:Orange|\
NC:Pamlico|\
NC:Pasquotank|\
NC:Pender|\
NC:Perquimans|\
NC:Person|\
NC:Pitt|\
NC:Polk|\
NC:Randolph|\
NC:Richmond|\
NC:Robeson|\
NC:Rockingham|\
NC:Rowan|\
NC:Rutherford|\
NC:Sampson|\
NC:Scotland|\
NC:Stanly|\
NC:Stokes|\
NC:Surry|\
NC:Swain|\
NC:Transylvania|\
NC:Tyrrell|\
NC:Union|\
NC:Vance|\
NC:Wake|\
NC:Warren|\
NC:Washington|\
NC:Watauga|\
NC:Wayne|\
NC:Wilkes|\
NC:Wilson|\
NC:Yadkin|\
NC:Yancey|\
NM:Bernalillo|\
NM:Catron|\
NM:Chaves|\
NM:Cibola|\
NM:Colfax|\
NM:Curry|\
NM:De Baca|\
NM:Dona Ana|\
NM:Eddy|\
NM:Grant|\
NM:Guadalupe|\
NM:Harding|\
NM:Hidalgo|\
NM:Lea|\
NM:Lincoln|\
NM:Los Alamos|\
NM:Luna|\
NM:Mckinley|\
NM:Mora|\
NM:Otero|\
NM:Quay|\
NM:Rio Arriba|\
NM:Roosevelt|\
NM:San Juan|\
NM:San Miguel|\
NM:Sandoval|\
NM:Santa Fe|\
NM:Sierra|\
NM:Socorro|\
NM:Taos|\
NM:Torrance|\
NM:Union|\
NM:Valencia|\
PA:Adams|\
PA:Allegheny|\
PA:Armstrong|\
PA:Beaver|\
PA:Bedford|\
PA:Berks|\
PA:Blair|\
PA:Bradford|\
PA:Bucks|\
PA:Butler|\
PA:Cambria|\
PA:Cameron|\
PA:Carbon|\
PA:Centre|\
PA:Chester|\
PA:Clarion|\
PA:Clearfield|\
PA:Clinton|\
PA:Columbia|\
PA:Crawford|\
PA:Cumberland|\
PA:Dauphin|\
PA:Delaware|\
PA:Elk|\
PA:Erie|\
PA:Fayette|\
PA:Forest|\
PA:Franklin|\
PA:Fulton|\
PA:Greene|\
PA:Huntingdon|\
PA:Indiana|\
PA:Jefferson|\
PA:Juniata|\
PA:Lackawanna|\
PA:Lancaster|\
PA:Lawrence|\
PA:Lebanon|\
PA:Lehigh|\
PA:Luzerne|\
PA:Lycoming|\
PA:Mckean|\
PA:Mercer|\
PA:Mifflin|\
PA:Monroe|\
PA:Montgomery|\
PA:Montour|\
PA:Northampton|\
PA:Northumberland|\
PA:Perry|\
PA:Philadelphia|\
PA:Pike|\
PA:Potter|\
PA:Schuylkill|\
PA:Snyder|\
PA:Somerset|\
PA:Sullivan|\
PA:Susquehanna|\
PA:Tioga|\
PA:Union|\
PA:Venango|\
PA:Warren|\
PA:Washington|\
PA:Wayne|\
PA:Westmoreland|\
PA:Wyoming|\
PA:York|\
SC:Abbeville|\
SC:Aiken|\
SC:Allendale|\
SC:Anderson|\
SC:Bamberg|\
SC:Barnwell|\
SC:Beaufort|\
SC:Berkeley|\
SC:Calhoun|\
SC:Charleston|\
SC:Cherokee|\
SC:Chester|\
SC:Chesterfield|\
SC:Clarendon|\
SC:Colleton|\
SC:Darlington|\
SC:Dillon|\
SC:Dorchester|\
SC:Edgefield|\
SC:Fairfield|\
SC:Florence|\
SC:Georgetown|\
SC:Greenville|\
SC:Greenwood|\
SC:Hampton|\
SC:Horry|\
SC:Jasper|\
SC:Kershaw|\
SC:Lancaster|\
SC:Laurens|\
SC:Lee|\
SC:Lexington|\
SC:Marion|\
SC:Marlboro|\
SC:Mccormick|\
SC:Newberry|\
SC:Oconee|\
SC:Orangeburg|\
SC:Pickens|\
SC:Richland|\
SC:Saluda|\
SC:Spartanburg|\
SC:Sumter|\
SC:Union|\
SC:Williamsburg|\
SC:York|\
VA:Accomack|\
VA:Albemarle|\
VA:Alexandria|\
VA:Alleghany|\
VA:Amelia|\
VA:Amherst|\
VA:Appomattox|\
VA:Arlington|\
VA:Augusta|\
VA:Bath|\
VA:Bedford|\
VA:Bedford Ind|\
VA:Bland|\
VA:Botetourt|\
VA:Bristol|\
VA:Brunswick|\
VA:Buchanan|\
VA:Buckingham|\
VA:Buena Vista|\
VA:Campbell|\
VA:Caroline|\
VA:Carroll|\
VA:Charles City|\
VA:Charlotte|\
VA:Charlottesville|\
VA:Chesapeake|\
VA:Chesterfield|\
VA:Clarke|\
VA:Clifton Forge|\
VA:Colonial Height|\
VA:Covington|\
VA:Craig|\
VA:Culpeper|\
VA:Cumberland|\
VA:Danville|\
VA:Dickenson|\
VA:Dinwiddie|\
VA:Emporia|\
VA:Essex|\
VA:Fairfax|\
VA:Fairfax Ind|\
VA:Falls Church|\
VA:Fauquier|\
VA:Floyd|\
VA:Fluvanna|\
VA:Franklin|\
VA:Franklin Ind|\
VA:Frederick|\
VA:Fredericksburg|\
VA:Galax|\
VA:Giles|\
VA:Gloucester|\
VA:Goochland|\
VA:Grayson|\
VA:Greene|\
VA:Greensville|\
VA:Halifax|\
VA:Hampton|\
VA:Hanover|\
VA:Harrisonburg|\
VA:Henrico|\
VA:Henry|\
VA:Highland|\
VA:Hopewell|\
VA:Isle Of Wight|\
VA:James City|\
VA:King And Queen|\
VA:King George|\
VA:King William|\
VA:Lancaster|\
VA:Lee|\
VA:Lexington|\
VA:Loudoun|\
VA:Louisa|\
VA:Lunenburg|\
VA:Lynchburg|\
VA:Madison|\
VA:Manassas|\
VA:Manassas Park|\
VA:Martinsville|\
VA:Mathews|\
VA:Mecklenburg|\
VA:Middlesex|\
VA:Montgomery|\
VA:Nelson|\
VA:New Kent|\
VA:Newport News|\
VA:Norfolk|\
VA:Northampton|\
VA:Northumberland|\
VA:Norton|\
VA:Nottoway|\
VA:Orange|\
VA:Page|\
VA:Patrick|\
VA:Petersburg|\
VA:Pittsylvania|\
VA:Poquoson|\
VA:Portsmouth|\
VA:Powhatan|\
VA:Prince Edward|\
VA:Prince George|\
VA:Prince William|\
VA:Pulaski|\
VA:Radford|\
VA:Rappahannock|\
VA:Richmond|\
VA:Richmond Ind|\
VA:Roanoke|\
VA:Roanoke Ind|\
VA:Rockbridge|\
VA:Rockingham|\
VA:Russell|\
VA:Salem|\
VA:Scott|\
VA:Shenandoah|\
VA:Smyth|\
VA:Southampton|\
VA:Spotsylvania|\
VA:Stafford|\
VA:Staunton|\
VA:Suffolk|\
VA:Surry|\
VA:Sussex|\
VA:Tazewell|\
VA:Virginia Beach|\
VA:Warren|\
VA:Washington|\
VA:Waynesboro|\
VA:Westmoreland|\
VA:Williamsburg|\
VA:Winchester|\
VA:Wise|\
VA:Wythe|\
VA:York';


function TrimString(sInString) {
  if ( sInString ) {
    sInString = sInString.replace( /^\s+/g, "" );// strip leading
    return sInString.replace( /\s+$/g, "" );// strip trailing
  }
}

// Populates the state selected with the counties from the county list
function populateState(defaultState) {
  if ( postState != '' ) {
    defaultState = postState;
  }
  var stateLineArray = state.split('|');  // Split into lines
  var selObj = document.getElementById('stateSelect');
  selObj.options[0] = new Option('Select State:','');
  selObj.selectedIndex = 0;
  for (var loop = 0; loop < stateLineArray.length; loop++) {
    lineArray = stateLineArray[loop].split(':');
    stateCode  = TrimString(lineArray[0]);
    stateName  = TrimString(lineArray[1]);
    if ( stateCode != '' ) {
      selObj.options[loop + 1] = new Option(stateName, stateCode);
    }
    if ( defaultState == stateCode ) {
      selObj.selectedIndex = loop + 1;
    }
  }
}

function populateCounty(defaultCounty) {
  if ( postCounty != '' ) {
    defaultCounty = postCounty;
  }
  var selObj = document.getElementById('countySelect');
  var foundCounty = 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 County:','None');
    selObj.selectedIndex = 0;
  }
  // Populate the drop down with counties from the selected state
  var countyLineArray = county.split("|");  // Split into lines
  var optionCntr = 1;
  for (var loop = 0; loop < countyLineArray.length; loop++) {
    lineArray = countyLineArray[loop].split(":");
    stateCode  = TrimString(lineArray[0]);
    countyName    = TrimString(lineArray[1]);
  if (document.getElementById('stateSelect').value == stateCode && stateCode != '' ) {
    // If it's a input element, change it to a select
      if ( selObj.type == 'hidden' ) {
        parentObj = document.getElementById('countySelect').parentNode;
        parentObj.removeChild(selObj);
        var inputSel = document.createElement("SELECT");
        inputSel.setAttribute("name","county");
        inputSel.setAttribute("id","countySelect");
        //inputSel.setAttribute("class","select_long");
        parentObj.appendChild(inputSel) ;
        selObj = document.getElementById('countySelect');
        selObj.options[0] = new Option('Select County:','None');
        selObj.selectedIndex = 0;
      }
      if ( countyName != '' ) {
        selObj.options[optionCntr] = new Option(countyName, countyName);
      }
      // See if it's selected from a previous post
      if ( countyName == defaultCounty ) {
        selObj.selectedIndex = optionCntr;
      }
      foundCounty = true;
      optionCntr++
    }
  }
  // If the state has no counties, change the select to a text box
  if ( ! foundCounty ) {
    parentObj = document.getElementById('countySelect').parentNode;
    parentObj.removeChild(selObj);
  // Create the Input Field
    var inputEl = document.createElement("INPUT");
    inputEl.setAttribute("id", "countySelect");
    inputEl.setAttribute("type", "hidden");
    inputEl.setAttribute("name", "county");
    inputEl.setAttribute("size", 11);
    //inputEl.setAttribute("enabled", "false");
    inputEl.setAttribute("value", "None");
    parentObj.appendChild(inputEl) ;
  }
}

function init(state,county) {
  populateState(state);
  populateCounty(county);
}



