$(document).ready(function(){
  $("#submit").attr("disabled", "disabled");
  $("#js_form").css("display", "block");
  $('#form_prompt').text('Find your School')
  $("#form").css("display", "none");
  if($('#state').val() != "" ){
    jQuery.post("city.php", { state: $("#state").val() }, function(data){
      $("#city").replaceWith(data);
      initCity();
    });
  }
  
  $("#state").change(function(){
    $('#submit').attr("disabled", "disabled");
    $('#school').attr("disabled", "disabled");
    if($('#state').val() == ""){
      $("#city").attr("disabled", "disabled");
    } else {
      $("#city").replaceWith("<img src='loader.gif' id='city'>");
      jQuery.post("city.php", { state: $("#state").val() }, function(data){
        $("#city").replaceWith(data);
        initCity();
      });
    }
  });
  
  function initCity(){
    $("#city").change(function(){
      $('#submit').attr("disabled", "disabled");
      if($('#city').val() == ""){
        $("#school").attr("disabled", "disabled");
      } else {
        $("#school").replaceWith("<img src='loader.gif' id='school'>");
        jQuery.post("school.php", { city: $("#city").val(), state: $("#state").val() }, function(data){
          $("#school").replaceWith(data);
          initSchool();
        });
      }
    });  
  };
  
  function initSchool(){
    $("#school").change(function(){
      if($('#school').val() == ""){
        $("#submit").attr("disabled", "disabled");
      } else {
        $("#submit").removeAttr("disabled");
      };
    });
  }
  
  $('#submit').click(function(){
    window.location = "school-link.php?school=" + $('#school').val();
    return false;
  })
  
});
