/*
 * @author stuartb
 * @date 2008.10.08
 * @description Wizard forms made easy.
 */
 
 function verif_date(sdate1, sdate2){
		var date1 = new Date();
		date1.setFullYear(sdate1.substr(6,4));
		date1.setMonth(parseInt(sdate1.substr(0,2)) - 1);
		date1.setDate(sdate1.substr(3,2));
		date1.setHours(0);
		date1.setMinutes(0);
		date1.setSeconds(0);
		date1.setMilliseconds(0);
		
		var date2 = new Date();
		date2.setFullYear(sdate2.substr(6,4));
		date2.setMonth(parseInt(sdate2.substr(0,2)) - 1);
		date2.setDate(sdate2.substr(3,2));
		date2.setHours(0);
		date2.setMinutes(0);
		date2.setSeconds(0);
		date2.setMilliseconds(0);
		
		if ((date1.getTime()/1000)  > (date2.getTime()/1000)) {
			return false
		}
		else{
			return true;
		}
	}
 
jQuery.fn.wizard = function(settings)
{
    settings = jQuery.extend({
         show: function(element) { return true; },
         prevnext: true,
         submitpage: null
      }, settings);

    // Hide all pages save the first.
   // jQuery(this).children(".wizardpage").hide();
   // jQuery(this).children(".wizardpage:first").show();
   // settings.show(jQuery(this).children(".wizardpage:first"));
    
    // Also highlight the first nav item.
    jQuery(this).children(".wizard-nav").children("a:first").addClass("active");
    
    // Wire progress thingy
    jQuery(this).children(".wizard-nav").children("a").click(function(){
        var target = jQuery(this).attr("href");
        jQuery(this).parent().parent().children(".wizardpage").hide();
        jQuery(target).fadeIn('slow');
        settings.show(jQuery(target));
        jQuery(this).parent().children('a').removeClass('active', 'slow');
        jQuery(this).addClass('active', 'slow');
        return false;
    });
    
    // Prevent form submission on a wizard page...
    jQuery(this).children(".wizardpage").each(function(i){
        // unless there is a submit button on this page
        if((settings.submitpage == null && jQuery(this).find('input[type="submit"]').length < 1) ||
           (settings.submitpage != null && !$(this).is(settings.submitpage)))
        {
            $(this).find('input,select').keypress(function(event){
                return event.keyCode != 13;
            });
        }
    });
    
    if(settings.prevnext)
    {
        // Add prev/next step buttons
        jQuery(this).children(".wizardpage")
        .append('<div class="row wizardcontrols"></div>')
        .children(".wizardcontrols")
            .append('<input type="button" class="wizardprev" value="< Back" /><input type="button" class="wizardnext" value="Next >" />');
        jQuery('.wizardpage:first input[type="button"].wizardprev').hide(); // hide prev button on first page
        jQuery('.wizardpage:last input[type="button"].wizardnext').hide();  // hide next button on last page

        // Wire prev/next step buttons
        jQuery(this).children(".wizardpage")
        .children(".wizardcontrols")
        .children('input[type="button"].wizardprev').click(function(){
            var wizardpage = jQuery(this).parent().parent(); // wizardcontrols div, wizardpage div
            var wizardnav  = wizardpage.parent().children(".wizard-nav")
            
            wizardpage.hide();
            wizardpage.prev().fadeIn();
            settings.show(wizardpage.prev());
            
            try{ wizardpage.prev().find("input:first").focus(); } catch(err) {}
            wizardnav.children('a').removeClass('active', 'slow');
            wizardnav.children('a[href="#' + wizardpage.attr('id') + '"]').prev().addClass('active', 'slow');
        });
        jQuery(this).children(".wizardpage")
        .children(".wizardcontrols")
        .children('input[type="button"].wizardnext').click(function(){
            var wizardpage = jQuery(this).parent().parent(); // wizardcontrols div, wizardpage div
            var wizardnav  = wizardpage.parent().children(".wizard-nav");
            var msg = '';
            
            //Si deuxieme etape ?
            if (wizardpage.prev().attr('class') == 'FormBloc wizardpage') {
        		if (!isBlank(document.form1.DateBegin.value) && !isBlank(document.form1.DateEnd.value) ) {
					if (!verif_date(document.form1.DateBegin.value, document.form1.DateEnd.value))
						msg += "*Check below for errors \n*Please enter dates in increasing chronological order. \n";
				}
    		}
    		if (msg != ''){
    			alert(msg);
			}
			else {
	            wizardpage.hide();
	            wizardpage.next().fadeIn();            
				settings.show(wizardpage.next());
				try{ 
					wizardpage.prev().find("input:first").focus(); 
	
				} catch(err) {}
	            wizardpage.prev().find("input:first").focus();
	            wizardnav.children('a').removeClass('active', 'slow');
	            wizardnav.children('a[href="#' + wizardpage.attr('id') + '"]').next().addClass('active', 'slow');
			}
        });
    }
    
    return jQuery(this);
};
