function FormRedirect(){
setTimeout('this.document.location.href = "submit.aspx"',5000);}

(function($) {
  $.fn.bindStep = function(nextStep,options) {

    var opts = $.extend({}, $.fn.bindStep.defaults, options);
    
    var nextBtnImg = new Image();
    var prevBtnImg = new Image();
    nextBtnImg.src = opts.nextBtn;
    prevBtnImg.src = opts.prevBtn;
   
    return this.each(function() {
      $currStep = $(this);

      var o = $.meta ? $.extend({}, opts, $this.data()) : opts;
      
      if(nextBtnImg.src != o.nextBtn) nextBtnImg.src = o.nextBtn;
      if(prevBtnImg.src != o.prevBtn) prevBtnImg.src = o.prevBtn;
      
      nextStep.each(function(){
        $nextStep = $(this);
        
        displayAndBind($currStep,$nextStep,o,'next');
        if(o.bindPrev) displayAndBind($nextStep,$currStep,o,'prev');
      });
      
    });
  };
 
  function displayAndBind(s1,s2,_opts,type){
    var imgId = s1.attr('id') + '_to_' + s2.attr('id') + '_btn';
    var containerId = imgId + '_container';
    
    var img = (type == 'next' ? _opts.nextBtn : _opts.prevBtn);
    var imgBtn = _opts.generateMarkup(containerId,imgId,img);
    
    _opts.layoutButton(s1,imgBtn,type);
    
    var imgRef = $('#'+ containerId);
    attachTransition(imgRef,_opts.transition,s1,s2,_opts.nextValidator);
  }
    
  function attachTransition(imgBtn,trans,s1,s2,valid){
    imgBtn.click(function(){
      if(valid && valid()) trans(s1,s2);
    });
  }
  
  function debug($obj) {
    if (window.console && window.console.log)
      window.console.log('babySteps: ' + $obj);
  };
  
  $.fn.bindStep.defaults = {
    nextBtn         :   '',
    prevBtn         :   '',
    bindPrev        :   true,
    nextValidator   :   function(){return true;},
    transition      :   function(currStep,nextStep){
      currStep.hide();  
      nextStep.show();
    },
    layoutButton    :   function(step,imgBtn,imgType){
      if(imgType=='next' || imgType=='prev'){
        step.append(imgBtn);
      }
    },
    generateMarkup  :   function(id1,id2,img){        
       return([
         '<div ',
           'id="', id1, '" ',
         '>',
           '<img ',
             'src="', img, '" ',
             'id="', id2 , '" ',
           '/>',
         '</div>'
       ].join(''));
     }
  };
})(jQuery);

$(document).ready(function() {

    $('.preload').each(function() {
        var f = this.attributes.psrc.nodeValue;
        $("<img>").attr("src", f);
    });

    $('.loginsubmit').click(function() {
        $('#loginform').submit();
    });

    $('#nav .preload').each(function() {
        var href = this.parentNode.attributes.href.nodeValue;
        var tmp_link = href.split('/');
        if (tmp_link.length != 1) {
            href = tmp_link[(tmp_link.length - 1)];
        }
        var curHref = location.pathname;
        var tmp = curHref.split('/');
        if (tmp.length != 1) {
            curHref = tmp[(tmp.length - 1)];
        }
        if (href == curHref) {
            $(this).attr('src', this.attributes.psrc.nodeValue);
        }
        if ((curHref == 'nrai_services.aspx' && href == 'place_an_order.aspx') || (curHref == 'corp_doc_retrieval_form.aspx' && href == 'place_an_order.aspx') || (curHref == 'ucc_form.aspx' && href == 'place_an_order.aspx') || (curHref == 'incorporation_worksheet.aspx' && href == 'form_a_company.aspx') || (curHref == 'llc.aspx' && href == 'form_a_company.aspx')) {
            $(this).attr('src', this.attributes.psrc.nodeValue);
        }
    });

    $.fn.bindStep.defaults.prevBtn = "images/prev.png";
    $.fn.bindStep.defaults.nextBtn = "images/next.png";
    $.fn.bindStep.defaults.generateMarkup = function(id1, id2, img) {
        return ([
        '<img class="btnprevnext ', id2, '"',
          'src="', img, '" ',
          'id="', id1, '" ',
        '/>'
      ].join(''));
    }

    $('#form1').submit(function() {
        if (($('.errormsg').length)) {
            alert('Please complete all required fields before submitting the request.');
            return false;
        } else {
            this.submit();
        }
    });

    function step1_validator() {
        var valid = true;
        if (!$('#conactName').val()) valid = false;
        if (!$('#conatctCompany').val()) valid = false;
        if (!$('#contactAddress1').val()) valid = false;
        if (!$('#contactCity').val()) valid = false;
        if (!$('#contactState').val()) valid = false;
        if (!$('#contactZip').val()) valid = false;
        if (!$('#contactEmail').val()) valid = false;
        valid = true;
        if (!valid) {
            alert('Please complete all required feilds before continuing');
        }
        return valid;
    }

    $('#nrai_services_form_container').each(function() {

        function stepify() {
            $('.step').hide();
            $("#step0").show();
            $('#stepify_btn').hide();

            var step0 = $('#step0');
            var step1 = $('#step1');
            var step2 = $('#step2');
            var step3 = $('#step3');
            var step4 = $('#step4');
            var step5 = $('#step5');
            var step6 = $('#step6');
            var step7 = $('#step7');
            var step8 = $('#step8');
            step0.bindStep(step1);
            step1.bindStep(step2, {
                nextValidator: step1_validator
            });
            step2.bindStep(step3);
            step3.bindStep(step4);
            step4.bindStep(step5);
            step5.bindStep(step6);
            step6.bindStep(step7);
            step7.bindStep(step8);

        }

        stepify();

    });

    $('#corp_doc_retrieval').each(function() {

        function stepify() {
            $('.step').hide();
            $("#step0").show();
            $('#stepify_btn').hide();

            var step0 = $('#step0');
            var step1 = $('#step1');
            var step2 = $('#step2');
            var step3 = $('#step3');
            var step4 = $('#step4');
            var step5 = $('#step5');

            step0.bindStep(step1);
            step1.bindStep(step2, {
                nextValidator: step1_validator
            });
            step2.bindStep(step3);
            step3.bindStep(step4);
            step4.bindStep(step5);

        }

        stepify();

    });

    $('#corp_doc_retrieval_frm').each(function() {

        function stepify() {
            $('.step').hide();
            $("#step0").show();
            $('#stepify_btn').hide();

            var step0 = $('#step0');
            var step1 = $('#step1');
            var step2 = $('#step2');
            var step3 = $('#step3');
            var step4 = $('#step4');
            var step5 = $('#step5');
            var step6 = $('#step6');

            step0.bindStep(step1);
            step1.bindStep(step2, {
                nextValidator: step1_validator
            });
            step2.bindStep(step3);
            step3.bindStep(step4);
            step4.bindStep(step5);
            step5.bindStep(step6);

        }

        stepify();

    });

    $('#incorporation_ws_form_container').each(function() {

        function stepify() {
            $('.step').hide();
            $("#step0").show();
            $('#stepify_btn').hide();

            var step0 = $('#step0');
            var step1 = $('#step1');
            var step2 = $('#step2');
            var step3 = $('#step3');
            var step4 = $('#step4');
            var step5 = $('#step5');
            var step6 = $('#step6');
            var step7 = $('#step7');
            var step8 = $('#step8');
            var step9 = $('#step9');
            step0.bindStep(step1);
            step1.bindStep(step2, {
                nextValidator: step1_validator
            });
            step2.bindStep(step3);
            step3.bindStep(step4);
            step4.bindStep(step5);
            step5.bindStep(step6);
            step6.bindStep(step7);
            step7.bindStep(step8);

        }

        stepify();

    });

    $('#llc_form_container').each(function() {

        function stepify() {
            $('.step').hide();
            $("#step0").show();
            $('#stepify_btn').hide();

            var step0 = $('#step0');
            var step1 = $('#step1');
            var step2 = $('#step2');
            var step3 = $('#step3');
            var step4 = $('#step4');
            var step5 = $('#step5');
            var step6 = $('#step6');
            var step7 = $('#step7');
            var step8 = $('#step8');
            var step9 = $('#step9');
            step0.bindStep(step1);
            step1.bindStep(step2, {
                nextValidator: step1_validator
            });
            step2.bindStep(step3);
            step3.bindStep(step4);
            step4.bindStep(step5);
            step5.bindStep(step6);
            step6.bindStep(step7);
            step7.bindStep(step8);
        }

        stepify();

    });

});
