var data = { regions: [
  {name: "Elementary", schools: ["Alcott Elem School", "Allen Elem School", "Almeda Elem School", "Anderson Elem School", "Ashford Elem School", "Askew Elem School", "Atherton Elem School", "Barrick Elem School", "Bastian Elem School", "Bell Elem School", "Bellfort Academy", "Benavidez Elem School", "Benbrook Elem School", "Berry Elem School", "Blackshear Elem School", "Bonham Elem School", "Bonner Elem School", "Braeburn Elem School", "Briargrove Elem School", "Briscoe Elem School", "Brookline Elem School", "Browning Elem School", "Bruce Elem School", "Burbank Elem School", "Burnet Elem School", "Burrus Elem School", "Bush Elem School", "C Martinez Elem School", "Cage Elem School", "Carrillo Elem School", "Codwell Elem School", "Condit Elem School", "Cook Elem School", "Coop Elem School", "Cornelius Elem School", "Crawford Elem School", "Crespo Elem School", "Crockett Elem School", "Cunningham Elem School", "Daily Elem School", "Davila Elem School", "De Zavala Elem School", "DeChaumes Elem School", "Dodson Elem School", "Dogan Elem School", "Durham Elem School", "Durkee Elem School", "Eliot Elem School", "Elrod Elem School", "Emerson Elem School", "Energized for Excellence (Elem) ES", "Field Elem School", "Foerster Elem School", "Fondren Elem School", "Foster Elem School", "Franklin Elem School", "Frost Elem School", "Gallegos Elem School", "Garcia Elem School", "Garden Oaks Elem School", "Garden Villas Elem School", "Golfcrest Elem School", "Gordon Elem School", "Gregg Elem School", "Grimes Elem School", "Grissom Elem School", "Gross Elem School", "Hartsfield Elem School", "Harvard Elem School", "Helms Community Learning Center", "Herod Elem School", "Herrera Elem School", "Highland Hts Elem School (Acres)", "Hines-Caldwell Elem School", "Hobby Elem School", "Hohl Elem School", "Horn Elem School", "Houston Gardens Elem School", "Inspired for Excellence Academy North 5-6", "Inspired for Excellence Academy West  5-6", "Isaacs Elem School", "J P Henderson Elem School", "J R Harris Elem School", "J W Jones Elem School", "Janowski Elem School", "Jefferson Elem School", "K Smith Elem School", "Kashmere Gardens Elem School", "Kelso Elem School", "Kennedy Elem School", "Ketelsen Elem School", "Kolter Elem School", "Lantrip Elem School", "Law Elem School", "Lewis Elem School", "Lockhart Elem School", "Longfellow Elem School", "Looscan Elem School", "Love Elem School", "Lovett Elem School", "Lyons Elem School", "MacGregor Elem School", "Mading Elem School", "McDade Elem School", "McNamara Elem School", "Memorial Elem School", "Milne Elem School", "Mitchell Elem School", "Montgomery Elem School", "Moreno Elem School", "N Q Henderson Elem School", "Neff Elem School", "North Alternative Elem School", "Northline Elem School", "Oak Forest Elem School", "Oates Elem School", "Osborne Elem (Acres)", "Paige Elem School", "Park Place Elem School", "Parker Elem School", "Patterson Elem School", "Peck Elem School", "Petersen Elem School", "Piney Point Elem School", "Pleasantville Elem School", "Poe Elem School", "Port Houston Elem School", "Pugh Elem School", "R Martinez Elem School", "R P Harris Elem School", "Red Elem School", "Reynolds Elem School", "Rhoads Elem School", "Rice Elem School", "River Oaks Elem School", "Roberts Elem School", "Robinson Elem School", "Rodriguez Elem School", "Roosevelt Elem School", "Ross Elem School", "Rucker Elem School", "Sanchez Elem School", "Sanderson Elem School", "Sands Point Elem School", "Scarborough Elem School", "School at St George Place", "Scott Elem School", "Scroggins Elem School", "Seguin Elem School", "Shearn Elem School", "Sherman Elem School", "Sinclair Elem School", "Smith Education Center (ES)", "South District Alternative", "Southmayd Elem School", "Stevens Elem School", "Stevenson Elem School", "Sugar Grove Elem School", "Sutton Elem School", "T H Rogers Elem School", "TSU Charter Lab School  ES", "Thompson Elem School", "Tijerina Elem School", "Tinsley Elem School", "Travis Elem School", "Turner Elem School", "Twain Elem School", "Valley West Elem School", "Wainwright Elem School", "Walnut Bend Elem School", "Wesley Elem School (Acres)", "West University Elem School", "Wharton Elem School", "Whidby Elem School", "White Elem School", "Whittier Elem School", "Wilson Elem School", "Windsor Village Elem School", "Woodson Elem School", "Young Elem School (Sunny Side)", "Young Scholars Academy  ES"]},
  {name: "Middle", schools: ["Attucks Middle School", "Black Middle School", "Burbank Middle School", "Clifton Middle School", "Contemporary Learning Center (Middle) MS", "Cullen Middle School", "Deady Middle School", "Dominion Academy  MS", "Dowling Middle School", "Edison Middle School", "Energized for Excellence (Middle) MS", "Fleming Middle School", "Fondren Middle School", "Fonville Middle School", "Grady Middle School", "Hamilton Middle School", "Hartman Middle School", "Henry Middle School", "Hogg Middle School", "Holland Middle School", "Jackson Middle School", "Johnston Middle School", "Kaleidoscope Middle School", "Key Middle School", "Lanier Charter Middle School", "Las Americas Middle School", "Long Middle School", "Marshall Middle School", "McReynolds Middle School", "North Alternative Middle School", "Ortiz Middle School", "Pershing Middle School", "Pin Oak Middle School", "Project Chrysalis (MS)", "Revere Middle School", "Ryan Middle School", "Sharpstown Middle School", "Smith Education Center (MS)", "Stevenson Middle School", "T H Rogers Middle School", "Thomas Middle School", "WALIPP  MS", "Welch Middle School", "West Briar Middle School", "Williams Middle School (Acres)"]},
  {name: "High", schools: ["Austin High School", "Bellaire High School", "Carnegie Vanguard High School", "Challenge High School", "Chavez High School", "Contemporary Learning Center (High) HS", "Davis High School", "DeBakey HS For Health Prof", "East Early College High School", "Eastwood Academy High School", "Empowerment High School", "Energized for STEM Academy  HS", "Furr High School", "Gregory-Lincoln Educ Ctr", "HS for Law and Criminal Justice", "HS for Performing and Vis Arts", "Houston Acad for Intl Studies", "Jones High School", "Jordan High School for Careers", "Kashmere High School", "Lamar High School", "Leader's Academy  HS", "Lee High School", "Liberty High School", "Madison High School", "Milby High School", "Mount Carmel Academy  HS", "New Aspirations HS", "Ninth Grade College-Prep Academy", "North Houston Early College High School", "Reach Charter School", "Reagan High School", "Sam Houston Math, Science, and Tech Center", "Scarborough High School", "Sharpstown High School", "Sharpstown Intl High School", "Sterling High School", "Waltrip High School", "Washington High School", "Westbury High School", "Westside High School", "Wheatley High School", "Worthing High School", "Yates High School"]},
  {name: "Multilevel", schools: ["Briarmeadow Charter School", "CEP SE High School  6-12", "CEP SW High School  6-12", "Carter Career Center (High) 6-12", "Community Services  K-12", "Crossroads Alternative School 6-12", "HCC Life Skills 6-12", "HISD JJAEP  6-12", "Harper Alt School  6-12", "Kandy Stripe Academy  K-8", "Pilgrim Academy", "Pro-Vision School 5-8", "Rusk Elem School", "Shadowbriar Elem School", "The Rice School", "Woodson Elem-Middle School"]}
]}

Event.observe(document, 'dom:loaded', function(event) {
  Event.observe('region', 'change', onSelectRegion)
  Event.observe('school', 'change', onSelectSchool)
  Event.observe('download', 'click', onDownload)
  
  populateRegions();
  onSelectRegion(null);
})

function populateRegions() {
  data.regions.each(function(region) {
    var newRegion = new Element('option')
    newRegion.update(region.name)
    newRegion.value = region.name.toLowerCase().replace(/ /g, '-')
    
    $('region').insert(newRegion)
  })  
}

function onSelectRegion(event) {
  $('school').disabled = $('region').getValue() == 'none'
  $('school').value = 'none';
  
  schools = $('school').childElements()
  schools.each(function(option) {
    if (option.value != 'none') {
      option.remove()
    }
  })
  
  data.regions.each(function(region) {
    if (region.name.toLowerCase() == $('region').value) {
      region.schools.each(function(schoolName) {
        var newSchool = new Element('option')
        newSchool.update(schoolName)
        newSchool.value = schoolName.toLowerCase().replace(/ /g, '-')
        
        $('school').insert(newSchool)
      })
    }
  })
  
  onSelectSchool(null)
}

function onSelectSchool(event) {
  $('download').disabled = $('region').getValue() == 'none' || $('school').getValue() == 'none';
}

function onDownload(event) {
  if ($('name').getValue().strip() == '' || $('email').getValue().strip() == '') {
    alert('Please enter your name and email address before downloading');
    event.stop();
    return;
  }
  
  if ($('email').getValue().match(/@houstonisd\.org$/) == null) {
    alert('Please enter a valid HISD email address');
    event.stop();
    return;
  }

  $('url').value = "http://assets.educaide.com/hisd/0507/acces4-installer-" + $('school').getValue() + ".exe"
  $('userAgent').value = navigator.userAgent
}
