Guest User

Untitled

a guest
Jul 16th, 2013
180
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. $(function() {
  2.     /*
  3.     number of fieldsets
  4.     */
  5.     var fieldsetCount = $('#formElem').children().length;
  6.    
  7.     /*
  8.     current position of fieldset / navigation link
  9.     */
  10.     var current     = 1;
  11.    
  12.     /*
  13.     sum and save the widths of each one of the fieldsets
  14.     set the final sum as the total width of the steps element
  15.     */
  16.     var stepsWidth  = 0;
  17.     var widths      = new Array();
  18.     $('#steps .step').each(function(i){
  19.         var $step       = $(this);
  20.         widths[i]       = stepsWidth;
  21.         stepsWidth      += $step.width();
  22.     });
  23.     $('#steps').width(stepsWidth);
  24.    
  25.     /*
  26.     to avoid problems in IE, focus the first input of the form
  27.     */
  28.     $('#formElem').children(':first').find(':input:first').focus();
  29.    
  30.     /*
  31.     show the navigation bar
  32.     */
  33.     $('#navigation').show();
  34.    
  35.     /*
  36.     when clicking on a navigation link
  37.     the form slides to the corresponding fieldset
  38.     */
  39.     $('#navigation a').bind('click',function(e){
  40.         var $this   = $(this);
  41.         var prev    = current;
  42.         $this.closest('ul').find('li').removeClass('selected');
  43.         $this.parent().addClass('selected');
  44.         /*
  45.         we store the position of the link
  46.         in the current variable
  47.         */
  48.         current = $this.parent().index() + 1;
  49.         /*
  50.         animate / slide to the next or to the corresponding
  51.         fieldset. The order of the links in the navigation
  52.         is the order of the fieldsets.
  53.         Also, after sliding, we trigger the focus on the first
  54.         input element of the new fieldset
  55.         If we clicked on the last link (confirmation), then we validate
  56.         all the fieldsets, otherwise we validate the previous one
  57.         before the form slided
  58.         */
  59.         $('#steps').stop().animate({
  60.             marginLeft: '-' + widths[current-1] + 'px'
  61.         },500,function(){
  62.             if(current == fieldsetCount)
  63.                 validateSteps();
  64.             else
  65.                 validateStep(prev);
  66.             $('#formElem').children(':nth-child('+ parseInt(current) +')').find(':input:first').focus();   
  67.         });
  68.         e.preventDefault();
  69.     });
  70.    
  71.     /*
  72.     clicking on the tab (on the last input of each fieldset), makes the form
  73.     slide to the next step
  74.     */
  75.     $('#formElem > fieldset').each(function(){
  76.         var $fieldset = $(this);
  77.         $fieldset.children(':last').find(':input').keydown(function(e){
  78.             if (e.which == 9){
  79.                 $('#navigation li:nth-child(' + (parseInt(current)+1) + ') a').click();
  80.                 /* force the blur for validation */
  81.                 $(this).blur();
  82.                 e.preventDefault();
  83.             }
  84.         });
  85.     });
  86.    
  87.     /*
  88.     validates errors on all the fieldsets
  89.     records if the Form has errors in $('#formElem').data()
  90.     */
  91.     function validateSteps(){
  92.         var FormErrors = false;
  93.         for(var i = 1; i < fieldsetCount; ++i){
  94.             var error = validateStep(i);
  95.             if(error == -1)
  96.                 FormErrors = true;
  97.         }
  98.         $('#formElem').data('errors',FormErrors);  
  99.     }
  100.    
  101.     /*
  102.     validates one fieldset
  103.     and returns -1 if errors found, or 1 if not
  104.     */
  105.     function validateStep(step){
  106.         if(step == fieldsetCount) return;
  107.        
  108.         var error = 1;
  109.         var hasError = false;
  110.         $('#formElem').children(':nth-child('+ parseInt(step) +')').find(':input:not(button)').each(function(){
  111.             var $this       = $(this);
  112.             var valueLength = jQuery.trim($this.val()).length;
  113.            
  114.             if(valueLength == ''){
  115.                 hasError = true;
  116.                 $this.css('background-color','#FFEDEF');
  117.             }
  118.             else
  119.                 $this.css('background-color','#FFFFFF');   
  120.         });
  121.         var $link = $('#navigation li:nth-child(' + parseInt(step) + ') a');
  122.         $link.parent().find('.error,.checked').remove();
  123.        
  124.         var valclass = 'checked';
  125.         if(hasError){
  126.             error = -1;
  127.             valclass = 'error';
  128.         }
  129.         $('<span class="'+valclass+'"></span>').insertAfter($link);
  130.        
  131.         return error;
  132.     }
  133.    
  134.     /*
  135.     if there are errors don't allow the user to submit
  136.     */
  137.     $('#registerButton').bind('click',function(){
  138.         if($('#formElem').data('errors')){
  139.             alert('Please correct the errors in the Form');
  140.             return false;
  141.         }  
  142.     });
  143. });
Add Comment
Please, Sign In to add comment