// Requires JQuery (http://jquery.com)
var stepFormValidator = (function(){
// Private Stuff
  var validatorOptions = {
    validateFunction : null,
    submitFunction   : null
  };
//  var step = {};
  
  // 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) {
      //alert( "Item #" + index + ": " + item );
      button = $(item);
      stepDiv =  $(button[0].form).parent();
      bindStep(button, 'forward', stepDiv.attr('id') );
    } );
    $.each( $(".stepButtonBackward"), function(index, item) {
      //alert( "Item #" + index + ": " + item );
      button = $(item);
      stepDiv =  $(button[0].form).parent();
      bindStep(button, 'back', stepDiv.attr('id') );
    } );
    $.each( $(".stepButtonSubmit"), function(index, item) {
      //alert( "Item #" + index + ": " + item );
      button = $(item);
      stepDiv =  $(button[0].form).parent();
      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();
      }
    } );
  };
  
  // for the button passed, bind the validation and step methods
  function bindStep(button, direction, stepDiv){
    button.click( function() {
      if ( runValidation(stepDiv) ){
        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;
    };
  };
  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').each(function(){
                    if (this.id && this.value){
                        inputElement.attr('name',this.id);
                        inputElement.attr('id',this.id);
                        inputElement.attr('value',this.value);
                        divContainer[0].appendChild(inputElement[0]);
                        inputElement = $(createElement('input'));
                        inputElement.attr('type','hidden');
                    }
                }
            );
        }        
    });
    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)));
    });
  }
};
})();

/*

*/