// This file will validate the parts of the Sign-Up form
// using AJAX if they have it, otherwise, it will submit
// normally

$(document).ready(function() {

  // since buttons and inputs are unreliable, we're going to remove
  // the buttons and replace them with straight links.  If the user
  // doesn't have JS, then all of the validation will be done on the
  // server, where the backend can handle the two button elements
  replaceButtons();

  // start by checking what part we are on
  var currentPart = checkFormPart();

  // intercept the completion of the form (this means the user hit
  // enter instead of picking one of the buttons - we'll advance
  // them forward to the next step) and run validation before submitting
  setupFormValidator(currentPart)
  
  // intercept any of the navigation buttons for this form, meaning
  // that the user has clicked on one of the buttons rather than
  // submitting the form by hitting Enter or Return in a form field.
  // Submit to validator for checks before loading the next form
  setupButtonValidator(currentPart);
  
});

function replaceButtons() {
  $("form button#back").replaceWith("<a href=\"#back\" class=\"back\">Back</a>");
  $("form button#next").replaceWith("<a href=\"#next\" class=\"next\">Next</a>");
  $("form button#submit").replaceWith("<a href=\"#submit\" class=\"submit\">Submit</a>");
}

function checkFormPart() {
  // which part are we currently validating?
  for (var i=1; i<=4; i++) {
    if ($("#part" + i).length == 1) {
      // we found the part
      return i;
    }
  }
}

function changeFormStep(current, next) {
  // update the paragraph for the form steps so that we match with the
  // form part they are now on
  $("#content p.step" + current).removeClass("step" + current).addClass("step" + next);
}

function setupButtonValidator(c) {
  
  $("#content form p.navbuttons a").bind("click", function() {
    // what direction are we going?
    var direction = $(this).attr("class");

    // submit the form for validation
    $.getJSON("test.php?part=" + c, function(data) {
      // get the number of errors
      numErrors = data.errors.count;

      // list out the errors in a list above the form
      if (numErrors != 0) {
        $("#content form h2").after("<ul class=\"errors\"></ul>");
        $.each(data.errors.items, function(i, item) {
          $("#content form ul.errors").append("<li>" + item + "</li>");
        });
      } else {
        
        // you'll want to post the results of this form back to the server
        
        // load up the form the user is requesting
        if (direction == "next") {
          var nextPart = c + 1;
          $("#content form").load("signup" + nextPart + ".php?formonly=1", function() {
            changeFormStep(c, nextPart);
            replaceButtons();
            c = checkFormPart();
            setupFormValidator(c);
            setupButtonValidator(c);
          });
        } else if (direction == "back") {
          var prevPart = c - 1;
          
          if (prevPart == 1) {
            $("#content form").load("signup.php?formonly=1", function() {
              changeFormStep(c, prevPart);
              replaceButtons();
              c = checkFormPart();
              setupFormValidator(c);
              setupButtonValidator(c);
            });
          } else {
            $("#content form").load("signup" + prevPart + ".php?formonly=1", function() {
              changeFormStep(c, prevPart);
              replaceButtons();
              c = checkFormPart();
              setupFormValidator(c);
              setupButtonValidator(c);
            });
          }
        } else {
          alert ("Um, something is going wrong - what direction are you headed?");
        }
      }
    });

    return false;
  });

}

function setupFormValidator(c) {
  
  $("#content form").bind("submit", function() {
    // we will always head forward when submitting the
    // form by hitting the Enter key
    var direction = "next";

    // submit the form for validation
    $.getJSON("test.php?part=" + c, function(data) {
      // get the number of errors
      numErrors = data.errors.count;

      // list out the errors in a list above the form
      if (numErrors != 0) {
        $("#content form h2").after("<ul class=\"errors\"></ul>");
        $.each(data.errors.items, function(i, item) {
          $("#content form ul.errors").append("<li>" + item + "</li>");
        });
      } else {
        
        // you'll want to post the results of this form back to the server
        
        var nextPart = c + 1;
        $("#content form").load("signup" + nextPart + ".php?formonly=1", function() {
          changeFormStep(c, nextPart);
          replaceButtons();
          c = checkFormPart();
          setupFormValidator(c);
          setupButtonValidator(c);
        });
      }
    });

    return false;
  });

}