// Requires JQuery (http://jquery.com)
var stepFormValidator = (function(){
// Private Stuff
  var validatorOptions = {
    validateFunction : null,
    submitFunction   : null,
    updateElement    : null,
    caller           : null
  };
  
  // Gets all the buttons and binds the onclick methods
  function bindQuotesEvents() {
    var button = null;
    var stepDiv = null;
    // The buttons with class will be binded forward
    $.each( $(".stepButtonForward"),  function(index, item) {
      button = $(item);
      stepDiv =  button.parents(".dataContainer");
      bindStep(button, 'forward', stepDiv.attr('id') );
    } );
    $.each( $(".stepButtonBackward"), function(index, item) {
      button = $(item);
      stepDiv =  button.parents(".dataContainer");
      bindStep(button, 'back', stepDiv.attr('id') );
    } );
    $.each( $(".stepButtonSubmit"), function(index, item) {
      button = $(item);
      stepDiv =  button.parents(".dataContainer");
      bindFinalValidation(button, stepDiv.attr('id'));
    } );
    
  };
  
  function runValidation(stepDiv){
    if ( validatorOptions.validateFunction != null ){
      return validatorOptions.validateFunction(stepDiv);
    }
    return true;
  };
  
  // for the button passed, bind the 'final' validation
  function bindFinalValidation(button, stepDiv){
    button.click( function() {
      if ( runValidation(stepDiv) && validatorOptions.submitFunction != null ){
        return validatorOptions.submitFunction(validatorOptions.updateElement);
      }
    } );
  };
  
  // for the button passed, bind the validation and step methods
  function bindStep(button, direction, stepDiv){
    button.click( function() {
      var ok = true;
      if ( direction == "forward" ){
        ok = runValidation(stepDiv);
      }
      if (ok) move(direction, stepDiv);
    } );
  };
  
  function move(dir, currentStep) {
    var stepNumber = currentStep.match(/\d*$/);
    stepNumber = stepNumber ? parseInt(stepNumber.pop()) : NaN;
    switch(dir){
      case "forward":
        var currentDiv = $("#step" + stepNumber);
        var nextDiv = isNaN(stepNumber) ? [] : $("#step" + (stepNumber+1));
        if (nextDiv.length > 0 ) {
          currentDiv.hide();
          nextDiv.show();
        }
        break;
      case "back":
        var currentDiv = $("#step" + stepNumber);
        var nextDiv = isNaN(stepNumber) ? [] : $("#step" + (stepNumber-1));
        if (nextDiv.length > 0 ) {
          currentDiv.hide();
          nextDiv.show();
        }
        break;
    };
    $("div.error").hide();
  };
  
  function createElement(element) {
    if (typeof document.createElementNS != 'undefined') {
      return document.createElementNS('http://www.w3.org/1999/xhtml', element);
    }
    if (typeof document.createElement != 'undefined') {
      return document.createElement(element);
    }
    return false;
  };
  
  function getNamesAndValues(jQObj){
    var divContainer = $(createElement('div'));
    var inputElement = $(createElement('input'));
    inputElement.attr('type', 'hidden');
    jQObj.each( function (){
      if (this.id){
        $(this).find('.inputElement, .selectElement').each(function(){
          if (this.id && this.value){
            inputElement.attr('name','form'+this.id);
            inputElement.attr('id','form'+this.id);
            inputElement.attr('value',this.value);
            divContainer[0].appendChild(inputElement[0]);
            inputElement = $(createElement('input'));
            inputElement.attr('type','hidden');
          }
        });
      }        
    });
    if (validatorOptions.caller){
      inputElement = $(createElement('input'));
      inputElement.attr('type', 'hidden');
      inputElement.attr('name','formCaller');
      inputElement.attr('id','formCaller');
      inputElement.attr('value',validatorOptions.caller);
      divContainer[0].appendChild(inputElement[0]);  
    }
    return divContainer[0];
  };
// Public Stuff
return {
  prepareQuoteForm: function( options ) {
    // Get all the divs that have the class dataContainer
    var stepDivs = $(".dataContainer");
    if (options) {
      validatorOptions = options;
    }
    stepDivs.hide();
    bindQuotesEvents();  
    // Shor the first div, since we are initializing the step form
    $(stepDivs[0]).show();
  },
  prepareFinalForm: function(formObj){
    $(".dataContainer").each(function(){
       formObj[0].appendChild(getNamesAndValues($(this)));
    });
  }
};
})();

/*

*/