var wiz = {
    init: function() {
        if ( $(window).height() < 630 ) {
            $(window).scrollTop(100);
        }

        var ua = navigator.userAgent;

        if ( ua.match(/MSIE 6/) ) {
            $('#wizMain').html("<p>Sorry, this wizard does not support Internet Explorer 6.</p>" +
                               "<p>Please upgrade to a modern web browser, or use our " +
                               "<a href=\"/service/book\"><strong>basic service form</strong></a>.</p>");
        }
        else {
            this.main();
        }

        setTimeout(function() {
            wiz.finished=true;
            wiz.disableBack();
            wiz.disableNext();
            wiz.formData = {};
            wiz.wizError('Timeout','Sorry, this wizard has timed out - you must complete it within 30 minutes!');
            $('#wizMain').html('<p>Wizard timed out!  <a href="/apps/servicewiz/">Start again</a></p>');
        },1800000);
    },

    main: function() {
        $('div.wizTab').click(function() {
            wiz.doStep( parseInt($(this).attr('id').replace(/^wizTab/,'')) );
        });

        $(window).keyup( function(k) {
//            if ( k.keyCode == 13 && wiz.enterNext ) 
//                wiz.doStep ( wiz.step + 1 );
        });

        /* Initialize items */
        for ( wizItem in wiz_service_items ) {
            var item = wiz_service_items[wizItem];
            wiz.formData.items[item.id] = false;
        }

        this.doStep(1);
    },

    step: 0,
    enterNext: false,
    finished: false,

    complete: {
        0: true,
        1: false,
        2: false,
        3: false,
        4: false,
        5: false,
        6: false
    },

    formData: {
        submit: true,
        review: '',
        login: '',
        password: '',
        authenticated: false,
        firstTime: true,
        retrieveDetails: false,
        items: new Object,
        year: '',
        make: '',
        model: '',
        trim: '',
        style: '',
        licence: '',
        mileage: '',
        comments: '',
        service_date1: '',
        service_date2: '',
        service_time1: '7:00am',
        service_time2: '7:00am',
        loaner: false,
        shuttle: false,
        nightDrop: false,
        name: '',
        email: '',
        street: '',
        suite: '',
        city: '',
        province: '',
        postcode: '',
        home_phone: '',
        work_phone: '',
        mobile_phone: '',
        new_password1: '',
        new_password2: '',
        bulk: true
    },

    allowNext: function() {
        $('#wizNext').stop().animate({opacity:1.0}, 300, 'swing');    
        $('#wizNext').unbind('click').click(function() {
            wiz.doStep( wiz.step + 1 );
        });
    },

    disableNext: function() {
        $('#wizNext').stop().animate({opacity:0.5}, 300, 'swing');    
        $('#wizNext').unbind('click');
    },

    allowBack: function() {
        $('#wizBack').stop().animate({opacity:1.0}, 300, 'swing');    
        $('#wizBack').unbind('click').click(function() {
            wiz.doStep( wiz.step - 1 );
        });
    },

    disableBack: function() {
        $('#wizBack').stop().animate({opacity:0.5}, 300, 'swing');    
        $('#wizBack').unbind('click');
    },

    submit: function() {
        $('#wizMain').html("<div class=\"wizLoading\">Contacting service appointment computer..</div>");

        var sendData = new Object;
    
        for ( o in wiz.formData ) {
            if ( ( o == 'password' || o == 'new_password1' || o == 'new_password2' )
                && wiz.formData[o] != '' ) 
                sendData[o] = wiz.formData[o];
            else if ( o == 'items' ) 
                sendData[o] = JSON.stringify( wiz.formData[o] );
            else
                sendData[o] = wiz.formData[o];
        }

        $.ajax({
            url: 'submit.php',
            type: 'post',
            data: sendData,
            success: function(json_data) {
                var error = eval('('+json_data+')');
                
                if ( error == 0 ) {
                    var buff = '<p><img src="complete.png" alt="Wizard Complete" /></p>' +
                               '<p><strong>All done!</strong>  We are now processing your appointment, and will contact you shortly with confirmation.</p>' +
                               '<p>Within business hours, you will normally hear from us within <strong>2 hours</strong>, although depending on demand ' +
                               'it may take a little longer to ' +
                               'get back to you. If you made this request after 3pm, you will hear from us the next business day.</p>' +
                               '<p>We appreciate your business at ' + wiz_vars.biz_name + ', and we\'d like to thank you for choosing us as your vehicle ' +
                               'service provider!</p>';

                    if ( wiz.formData.firstTime && wiz.formData.new_password1 != '' ) {
                        buff += '<div class="wizAcctDetails"><p><strong>Your account details</strong></p>' +
                                '<p><strong>Login:</strong> ' + wiz.formData.email + '<br />' +
                                '<strong>Password:</strong> &lt;This will be emailed to you shortly!&gt;</p>' +
                                '</div>';
                    }

                    buff += '<p>Go to <a href="/"><strong>' + wiz_vars.biz_name + ' home</strong></a></p>';
                    $('#wizMain').html(buff);

                    wiz.finished = true;
                    wiz.disableNext();
                    wiz.disableBack();
                }
                else if ( error == 1 ) {
                    wiz.wizError('Data not submitted','Did not receive the data as expected, please try again');
                    wiz.doStep(6);
                }
                else if ( error == 2 ) {
                    wiz.wizError('Email address already in use','The email address you are using to create an account is already in use.<br /><br />' +
                                    'Please change to a different email address, or use the forgotten password option on step 1 to reset your password!');
                    wiz.doStep(5);
                }
                else if ( error = 3 ) {
                    wiz.wizError('Incompleted service request','Some of the data expected was missing, please try the wizard again.'),
                    wiz.doStep(1);
                }
                else {
                    wiz.wizError('Unexpected error','An unexpected error occured!');
                    wiz.doStep(6);
                }
            },
            error: function() {
                wiz.wizError('Network Error','Oops! There was a network error, please try again!');
                wiz.doStep(6);
            }
        });

    },

    wizError: function(title,msg) {
        $('body').append("<div id=\"wizError\" title=\""+title+"\">" + msg + "</div>");
        $('#wizError').dialog({
            close: function() {
                $('#wizError').remove();
            },
            width: 400,
            height: 200,
            resizable: false,
            modal: true,

            buttons: {
                'OK': function() {
                    $(this).dialog("close");
                }
            }
        });
    },

    doStep: function(step) {
        var buff;

        if ( wiz.finished )
            return;

        if ( step > 6  || step < 1 )
            return;

        if ( wiz.step == 1 ) {
            if ( wiz.firstTime ) {
                wiz.formData.login = '';
                wiz.formData.password = '';
            }
            else {
                if ( $('#loginEmail').is(':visible') ) {
                    wiz.formData.login = $('#loginEmail').val();
                    wiz.formData.password = $('#loginPassword').val();
                    wiz.formData.email = wiz.formData.login;
                }
            }
        }

        if ( wiz.step == 3 ) {
            wiz.formData.year = $('#vehicle_year').val();
            wiz.formData.make = $('#vehicle_make').val();
            wiz.formData.model = $('#vehicle_model').val();
            wiz.formData.trim = $('#vehicle_trim').val();
            wiz.formData.style = $('#vehicle_style').val();
            wiz.formData.licence = $('#vehicle_licence').val();
            wiz.formData.mileage = $('#vehicle_mileage').val();
            wiz.formData.comments = $('#comments').val();
        }

        if ( wiz.step == 4 ) {
            wiz.formData.service_date1 = $('#service_date1').val();
            wiz.formData.service_date2 = $('#service_date2').val();

            wiz.formData.service_time1 = $('#service_time1').val();
            wiz.formData.service_time2 = $('#service_time2').val();
        }

        if ( wiz.step == 5 ) {
            wiz.formData.name = $('#name').val();
            wiz.formData.email = $('#email').val();
            wiz.formData.street = $('#street').val();
            wiz.formData.city = $('#city').val();
            wiz.formData.province = $('#province').val();
            wiz.formData.postcode = $('#postcode').val();
            wiz.formData.suite = $('#suite').val();
            wiz.formData.home_phone = $('#home_phone').val();
            wiz.formData.work_phone = $('#work_phone').val();
            wiz.formData.mobile_phone = $('#mobile_phone').val();

            if ( wiz.formData.firstTime ) {
                wiz.formData.new_password1 = $('#new_password1').val();
                wiz.formData.new_password2 = $('#new_password2').val();
            }

        }

        if ( step > 1 ) {
            if ( wiz.formData.retrieveDetails ) {
                var step1ok = true;

                if ( wiz.formData.authenticated ) {
                    step1ok = true;
                }
                else if ( ! wiz.formData.login.match(/^.{3,}$/i) ) {
                    wiz.wizError('Please complete step 1.','If retrieving account details, you must enter a login name');
                    step1ok = false;
                }
                else if ( ! wiz.formData.password.match(/^.{6,}$/i) ) {
                    wiz.wizError('Please complete step 1.','If retrieving account details, you must enter a password (at least 6 characters)');
                    step1ok = false;
                }
                else if ( ! wiz.formData.authenticated ) {
                    $('#wizMain').html("<div class=\"wizLoading\">Authenticating your credentials, please wait...</div>");

                    $.ajax({
                        url: 'auth.php',
                        type: 'post',
                        data: 'login=' + encodeURIComponent(wiz.formData.login) +
                              '&password=' + wiz.formData.password,
                        error: function() {
                            wiz.wizError('Network Error','Oops! There was a network error, please try again!');
                            wiz.formData.authenticated = false;
                            wiz.formData.password = '';
                            wiz.doStep(1);
                        },
                        success: function(json_data) {
                            var jsd = eval('('+json_data+')');

                            if ( jsd == false ) {
                                wiz.wizError('Invalid Login','We were unable to authenticate your login and password, please try again!');
                                wiz.formData.authenticated = false;
                                wiz.formData.password = '';
                                wiz.doStep(1);
                            }
                            else {
                                wiz.formData.authenticated = true;
                                wiz.doStep(2);

                                wiz.formData.name = jsd.name;
                                wiz.formData.street = jsd.street;
                                wiz.formData.city = jsd.city;
                                wiz.formData.province = jsd.province;
                                wiz.formData.suite = jsd.suite;
                                wiz.formData.postcode = jsd.postcode;
                                wiz.formData.home_phone = jsd.home_phone;
                                wiz.formData.work_phone = jsd.work_phone;
                                wiz.formData.mobile_phone = jsd.mobile_phone;
                                wiz.formData.year = jsd.year;
                                wiz.formData.make = jsd.make;
                                wiz.formData.model = jsd.model;
                                wiz.formData.trim = jsd.trim;
                                wiz.formData.style = jsd.style;
                                wiz.formData.licence = jsd.licence;
                                wiz.formData.mileage = jsd.mileage;

                            }
        
                        }
                    });


                    return;
                }

                if ( ! step1ok ) {
                    wiz.doStep(1);
                    return;
                }
            }
        }

        if ( step > 2 ) {
            var step2ok = false;

            for ( wizItem in wiz_service_items ) {
                if ( wiz.formData.items[wiz_service_items[wizItem].id] == true ) {
                    step2ok = true;
                    break;
                }
            }

            if ( ! step2ok ) {
                wiz.wizError('Please complete step 2 first','Please select at least one service item from step 2 before ' +
                                'proceeding');
                wiz.doStep(2);
                return;
            }
        }

        if ( step == 3 && wiz.step == 2 ) {
            if ( wiz.formData.items.touchdeluxe == false &&
                 wiz.formData.items.touchpremium == false &&
                 wiz.formData.items.touchultimate == false ) {

                $('body').append("<div id=\"wizConfirm\" title=\"Would you like a detailing package?\">" +
                            '<p style="margin-top:10px;"><img src="touchBanner.jpg" width="375" height="156" alt="Toyota Touch" /></p>' +
                            '<p>We noticed that you did not choose a detailing package, we are currently ' +
                            'offering <strong>10% off</strong> all Toyota Touch detailing packages.</p>' +
                            '<p>Would you like to take advantage of this offer?</p>' +
                            '<p>Hit <strong>Yes</strong> to revise your selection and choose a detailing package, or <strong>No ' +
                            'thanks</strong> to continue ' +
                            'with step 3.</p>' +
                        "</div>");
                $('#wizConfirm').dialog({
                    close: function() { $('#wizConfirm').remove(); },
                    width: 400, height: 380, resizable: false, modal: true,
                    buttons: {
                        'Yes': function() { $(this).dialog("close"); wiz.doStep(2);  },
                        'No thanks': function() { $(this).dialog("close"); },
                        'Learn more..': function() { window.open('/toyotatouch/'); }
                    }
                });


            }
        }

        if ( step > 3 ) {
            var step3ok = true;

            if ( ! wiz.formData.year.match(/^[0-9]{4}$/) ) {
                wiz.wizError('Please complete Step 3.','Vehicle year is a required field, and can only contain 4 numbers');
                step3ok = false;
            }
            else if ( ! wiz.formData.make.match(/^[a-zA-Z0-9- ]{2,20}$/) ) {
                wiz.wizError('Please complete Step 3.','Vehicle make is a required field');
                step3ok = false;
            }
            else if ( ! wiz.formData.model.match(/^[a-zA-Z0-9- ]{2,20}$/) ) {
                wiz.wizError('Please complete Step 3.','Vehicle model is a required field');
                step3ok = false;
            }
            else if ( ! wiz.formData.mileage.match(/^[0-9]{1,20}$/) ) {
                wiz.wizError('Please complete Step 3.','Vehicle mileage is a required field, and can only be a number');
                step3ok = false;
            }

            if ( ! step3ok ) {
                wiz.doStep(3);
                return;
            }
                
        }

        if ( step > 4 ) {
            var step4ok = true;

            if ( ! wiz.formData.service_date1.match(/^[0-9]{4}\/[0-9]{2}\/[0-9]{2}$/) ) {
                wiz.wizError('Please complete Step 4.','Preferred date is a required field!');
                step4ok = false;
            }
            else if ( ! wiz.formData.service_date2.match(/^([0-9]{4}\/[0-9]{2}\/[0-9]{2})*$/) ) {
                wiz.wizError('Please complete Step 4.','Second choice date must be yyyy/mm/dd');
                step4ok = false;
            }

            if ( ! step4ok ) {
                wiz.doStep(4);
                return;
            }
        }

        if ( step > 5 ) {
            var step5ok = true;

            if ( ! wiz.formData.name.match(/^[a-zA-Z- ']{3,64}$/) ) {
                wiz.wizError('Please complete Step 5.','Please enter your name.<br /><br />(must be between 3 - 64 characters and can only contain a-z, A-Z, hyphens, spaces or apostrophes!)');
                step5ok = false;
            }
            else if ( ! wiz.formData.email.match(/^[A-Z0-9._%+-]+@(?:[A-Z0-9-]+\.)+[A-Z]{2,4}$/i) ) {
                wiz.wizError('Please complete Step 5.','Please enter a valid email address');
                step5ok = false;
            }
            else if ( wiz.formData.new_password1 != '' || wiz.formData.new_password2 != '' ) {
                if ( wiz.formData.new_password1 != wiz.formData.new_password2 ) {
                    wiz.wizError('Error creating new account (Step 5)', 'You are attempting to create a new account, but the passwords you entered do not match!<br /><br />' +
                                 'Please be sure to confirm the password.');
                    step5ok = false;
                }
                else if ( ! wiz.formData.new_password1.match(/^.{6,20}$/) ) {
                    wiz.wizError('Error creating new account (Step 5)', 'Your new password must be between 6 and 20 characters');
                    step5ok = false;
                }
            }

            if ( ! step5ok ) {
                wiz.doStep(5);
                return;
            }
        }

        $('div.wizTab').each(function() {
            if ( $(this).attr('id') == 'wizTab'+step )
                $('#wizTab'+step).stop().animate({marginLeft: '0px',opacity:1.0},300,'swing');
            else
                $(this).stop().animate({marginLeft: '10px',opacity:0.5},300,'swing');
        });

        wiz.step = step;
        wiz.enterNext = true;

        switch ( step ) {
            case 1: {
                buff = "<p>Welcome to the " + wiz_vars.biz_name + " <strong>Request Service Appointment Wizard!</strong></p>" +
                       "<p>If this is the first time you have used this wizard, please select &quot;This is my first time&quot; " +
                       "and hit <strong>next.</strong></p>" +
                       "<p>If you have been here before and opted to save your information, or have a " + wiz_vars.biz_name +
                       " online account, select &quot;I have an account&quot; and hit <strong>next.</strong><br /></p>";

                buff += "<div id=\"firstTime\" class=\"wizCB\" title=\"Choose this option if you do not have an account\">" +
                        "<span>This is my first time</span></div>";
                buff += "<div id=\"retrieveDetails\" class=\"wizCB\" title=\"Choose this option if you have an account\"><span>" +
                        "I have an account</span></div>";

                buff += "<div style=\"clear:both;height:10px;\"><!-- --></div>";

                buff += "<div id=\"wizLogin\">";
                buff += "<div class=\"wizInput\"><strong>Login or Email address*</strong><br />" +
                    "<input type=\"text\" id=\"loginEmail\" maxlength=\"64\" autocomplete=\"off\" /><br />" +
                        "<span class=\"wizEg\">(usually your email address)</span></div>";
                buff += "<div class=\"wizInput\"><strong>Password*</strong><br />" +
                    "<input type=\"password\" id=\"loginPassword\" maxlength=\"64\" autocomplete=\"off\" /><br />" +
                        "<a id=\"forgotPassword\" href=\"#\">Forgot password?</a></div>";

                buff += "<div style=\"clear:both;height:10px;\"><!-- --></div>";
                buff += "</div>";


                buff += "<p><span class=\"wizEg\"><strong>Note:</strong> Navigating away from this page " +
                            "(including using your browser's back button or refresh) " +
                        "before submitting will cause all the " +
                        "information you entered to be lost! Please use the wizard navigation buttons (below)</span></p>" +
                        "<p>Having problems with this wizard?  Please use our <a href=\"/service/book\">Old form</a>.</p>";

                $('#wizMain').html(buff);

                if ( wiz.formData.authenticated ) {
                    $('#loginEmail').attr('readonly','readonly');
                    $('#loginPassword').attr('readonly','readonly');
                }

                if ( wiz.formData.retrieveDetails == true )  {
                    $('#retrieveDetails').addClass('wizCBOn'); 
                    $('#loginEmail').val( wiz.formData.login );
                    $('#loginPassword').val( wiz.formData.password );
                }

                if ( wiz.formData.firstTime == true ) {
                    $('#firstTime').addClass('wizCBOn'); 
                    $('#wizLogin').hide();
                }

                $('#forgotPassword').unbind('click').click(function() {
                   if ( $('#loginEmail').val() == '' )
                        wiz.wizError('Identification','Please enter your valid login or email address and click <strong>Forgot Password</strong> again!');
                   else {
                        $('#forgotPassword').text('Please wait');

                        $.ajax({
                            url: 'password.php',
                            type: 'POST',
                            data: 'email=' + encodeURIComponent($('#loginEmail').val()),
                            success: function(json_data) {   
                                var error = eval('('+json_data+')');
                                
                                if ( error == '0' ) {
                                    wiz.wizError('Password sent','Your password has been sent to the email address on file.');
                                    $('#forgotPassword').text('Forgot password already sent');
                                }
                                else {
                                    $('#forgotPassword').text('Forgot password?');
                                    wiz.wizError('Identification','We cannot find record of the login or email you requested.');
                                }
                            }
                        });
                   }
               
                   return false; 
                });

                $('#retrieveDetails').unbind('click').click(function() {
                    $(this).addClass('wizCBOn');
                    $('#firstTime').removeClass('wizCBOn');
                    wiz.formData.retrieveDetails = true;
                    wiz.formData.firstTime = false;

                    wiz.formData.new_password1 = '';
                    wiz.formData.new_password2 = '';
                    $('#wizLogin').show();
                });

                $('#firstTime').unbind('click').click(function() {
                    $(this).addClass('wizCBOn');
                    $('#retrieveDetails').removeClass('wizCBOn');
                    wiz.formData.retrieveDetails = false;
                    wiz.formData.firstTime = true;
                    $('#wizLogin').hide();
                    $('#loginEmail').val('');
                    $('#loginPassword').val('');
                    wiz.formData.authenticated = false;

                    $('#loginEmail').removeAttr('readonly');
                    $('#loginPassword').removeAttr('readonly');
                });


                break;
            }
            case 2: {
                buff = "<p>Please choose the vehicle service(s) you require from the options below, and then hit " +
                       "<strong>next.</strong></p>" +
                       "<p>Unsure which service your Toyota needs?  Try our <a href=\"/service/schedule\">Maintenance Menu</a>" +
                       "<br /><br /></p>";                

                for ( wizItem in wiz_service_items ) {
                    var item = wiz_service_items[wizItem];
                    buff += "<div id=\"wizItem"+item.id+"\" class=\"wizCB\" title=\"" + item.hint +
                                "\"><img src=\"/apps/servicewiz/"+item.img+"\" alt=\""+
                                item.name+"\" /><span>"+item.name+"</span></div>";
                }

                buff += "<div style=\"clear:both\"><!-- --></div>";

                $('#wizMain').html(buff);

                for ( wizItem in wiz_service_items ) {
                    var item = wiz_service_items[wizItem];
                
                    if ( wiz.formData.items[item.id] == true )
                        $('#wizItem'+item.id).addClass('wizCBOn');

                    $('#wizItem'+item.id).unbind('click').click(function() {
                        var item_id = $(this).attr('id').replace(/^wizItem/,'');

                        if ( wiz.formData.items[item_id] == false ) {
                            $(this).addClass('wizCBOn');
                            wiz.formData.items[item_id] = true;
                        }
                        else {
                            $(this).removeClass('wizCBOn');
                            wiz.formData.items[item_id] = false;
                        }
                    });
                }
                
                break;
            }
            case 3: {
                wiz.enterNext = false;

                buff = "<p>Please tell us about your <strong>vehicle</strong>..</p>" +
                       "<p>*Fields in <strong>bold</strong> must be completed!</p>";                

                buff += "<div class=\"wizInput\"><strong>Vehicle year*</strong><br />" +    
                            "<input type=\"text\" id=\"vehicle_year\" maxlength=\"64\" autocomplete=\"off\" /></div>" +
                        "<div class=\"wizInput\"><strong>Vehicle make*</strong><br />" +
                            "<input type=\"text\" id=\"vehicle_make\" maxlength=\"64\" autocomplete=\"off\" /></div>" +
                        "<div class=\"wizInput\"><strong>Vehicle model*</strong><br />" +
                            "<input type=\"text\" id=\"vehicle_model\" maxlength=\"64\" autocomplete=\"off\" /></div>" +
                        "<div class=\"wizInput\">Vehicle trim<br />" +
                            "<input type=\"text\" id=\"vehicle_trim\" maxlength=\"64\" autocomplete=\"off\" /></div>" +
                        "<div class=\"wizInput\">Vehicle style<br />" +
                            "<input type=\"text\" id=\"vehicle_style\" maxlength=\"64\" autocomplete=\"off\" /></div>" +
                        "<div class=\"wizInput\">Licence<br />" +
                            "<input type=\"text\" id=\"vehicle_licence\" maxlength=\"64\" autocomplete=\"off\" /></div>" +
                        "<div class=\"wizInput\"><strong>Mileage*</strong><br />" +
                            "<input type=\"text\" id=\"vehicle_mileage\" maxlength=\"64\" autocomplete=\"off\" /></div>";

                buff += "<div style=\"clear:both\"><!-- --></div>";


                buff += "<div class=\"wizTextArea\">Enter any comments you have for this service appointment below:" +
                        "<br /><span class=\"wizEg\">eg: provide details about any problems your vehicle is having.</span>" +
                        "<br /><textarea id=\"comments\"></textarea></div>";

                $('#wizMain').html(buff);

                $('#vehicle_year').val(wiz.formData.year);
                $('#vehicle_make').val(wiz.formData.make);
                $('#vehicle_model').val(wiz.formData.model);
                $('#vehicle_trim').val(wiz.formData.trim);
                $('#vehicle_style').val(wiz.formData.style);
                $('#vehicle_licence').val(wiz.formData.licence);
                $('#vehicle_mileage').val(wiz.formData.mileage);
                $('#comments').val(wiz.formData.comments);

                do_vehicle_index();
                break;
            }
            case 4: {
                buff = "<p>Choose your <strong>preferred date(s)</strong> for this service appointment.</p>" +
                       "<p>Please note that all online appointments are tentative until confirmed by our service department" +
                       " via email.</p>" +
                       "<div class=\"wizInput\"><strong>Preferred Date*</strong><br />" +
                            "<span class=\"wizEg\">Format: yyyy/mm/dd</span><br />" +
                            "<input type=\"text\" id=\"service_date1\" readonly=\"readonly\" maxlength=\"64\" autocomplete=\"off\" /></div>" +
                       "<div class=\"wizInput\">Best Time<br />" +
                            "<span class=\"wizEg\">Select the time that best suits you</span><br />" +
                            "<select id=\"service_time1\">" +
                                "<option value=\"7:00am\">7:00am</option>" +
                                "<option value=\"7:30am\">7:30am</option>" +
                                "<option value=\"8:00am\">8:00am</option>" +
                                "<option value=\"8:30am\">8:30am</option>" +
                                "<option value=\"9:00am\">9:00am</option>" +
                                "<option value=\"9:30am\">9:30am</option>" +
                                "<option value=\"10:00am\">10:00am</option>" +
                                "<option value=\"10:30am\">10:30am</option>" +
                                "<option value=\"11:00am\">11:00am</option>" +
                                "<option value=\"11:30am\">11:30am</option>" +
                                "<option value=\"12:00pm\">12:00pm</option>" +
                                "<option value=\"12:30pm\">12:30pm</option>" +
                                "<option value=\"1:00pm\">1:00pm</option>" +
                                "<option value=\"1:30pm\">1:30pm</option>" +
                                "<option value=\"2:00pm\">2:00pm</option>" +
                                "<option value=\"2:30pm\">2:30pm</option>" +
                                "<option value=\"3:00pm\">3:00pm</option>" +
                            "</select></div>";

                buff += "<div style=\"clear:both;\"><!-- --></div>";

                buff += "<div class=\"wizInput\">Second choice date<br />" +
                            "<span class=\"wizEg\">Format: yyyy/mm/dd</span><br />" +
                            "<input type=\"text\" id=\"service_date2\" readonly=\"readonly\" maxlength=\"64\" autocomplete=\"off\" /></div>" +
                       "<div class=\"wizInput\">Best Time<br />" +
                            "<span class=\"wizEg\">Select the time that best suits you</span><br />" +
                            "<select id=\"service_time2\">" +
                                "<option value=\"7:00am\">7:00am</option>" +
                                "<option value=\"7:30am\">7:30am</option>" +
                                "<option value=\"8:00am\">8:00am</option>" +
                                "<option value=\"8:30am\">8:30am</option>" +
                                "<option value=\"9:00am\">9:00am</option>" +
                                "<option value=\"9:30am\">9:30am</option>" +
                                "<option value=\"10:00am\">10:00am</option>" +
                                "<option value=\"10:30am\">10:30am</option>" +
                                "<option value=\"11:00am\">11:00am</option>" +
                                "<option value=\"11:30am\">11:30am</option>" +
                                "<option value=\"12:00pm\">12:00pm</option>" +
                                "<option value=\"12:30pm\">12:30pm</option>" +
                                "<option value=\"1:00pm\">1:00pm</option>" +
                                "<option value=\"1:30pm\">1:30pm</option>" +
                                "<option value=\"2:00pm\">2:00pm</option>" +
                                "<option value=\"2:30pm\">2:30pm</option>" +
                                "<option value=\"3:00pm\">3:00pm</option>" +
                            "</select></div>";


                buff += "<div style=\"clear:both;height: 30px;\"><!-- --></div>";


                buff += "<p><strong>Request options:</strong> " +
                        "(Not all may be available to you - our service department will confirm via email):</p>";

                buff += "<div id=\"loaner\" class=\"wizCB\" title=\"Select to request a service loaner " +
                            "(subject to availability)\"><img src=\"loaner.png\" alt=\"Service Loaner\" />" +
                            "<span>Request loaner vehicle!</span></div>";
                buff += "<div id=\"shuttle\" class=\"wizCB\" title=\"Select to request a service shuttle - " +
                            "we will pick you up and drop you off\"><img src=\"shuttle.png\" alt=\"Shuttle\" />" +
                            "<span>Service shuttle</span></div>";
                buff += "<div id=\"nightDrop\" class=\"wizCB\" title=\"Select if you'd like to drop off keys " +
                            "overnight\"><img src=\"nightDrop.png\" alt=\"Night Drop\" /><span>Night key drop-off</span></div>";

                buff += "<div style=\"clear:both;\"><!-- --></div>";

                $('#wizMain').html(buff);

                $('#service_date1').val(wiz.formData.service_date1);
                $('#service_date2').val(wiz.formData.service_date2);

                $('#service_time1').val(wiz.formData.service_time1);
                $('#service_time2').val(wiz.formData.service_time2);

                var opts = [ 'loaner', 'shuttle', 'nightDrop' ];

                for ( opt in opts ) {
                    if ( wiz.formData[opts[opt]] )
                        $('#'+opts[opt]).addClass('wizCBOn');


                    $('#'+opts[opt]).unbind('click').click(function() {
                        var item_id = $(this).attr('id');

                        if ( wiz.formData[item_id] == false ) {
                            $(this).addClass('wizCBOn');
                            wiz.formData[item_id] = true;
                        }
                        else {
                            $(this).removeClass('wizCBOn');
                            wiz.formData[item_id] = false;
                        }
                    });
                }

                var today = new Date();
                var minDate = new Date();
                var maxDate = new Date();
                minDate.setDate(today.getDate()+2); 
                maxDate.setDate(today.getDate()+120);

                $('#service_date1, #service_date2').datepicker({
                    beforeShowDay: wiz.dateDays,
                    minDate: minDate,
                    maxDate: maxDate,
                    numberOfMonths: 2,
                    dateFormat: 'yy/mm/dd'
                });
            

                break;
            }
            case 5: {
                buff = "<p>Please tell us about <strong>yourself</strong>..</p>" +
                       "<p>*Fields in <strong>bold</strong> must be completed!</p>";                

                buff += "<div class=\"wizInput\"><strong>Your full name*</strong><br />" +    
                            "<input type=\"text\" id=\"name\" maxlength=\"64\" autocomplete=\"off\" /></div>" +
                        "<div class=\"wizInput\"><strong>Email address*</strong><br />" +    
                            "<input type=\"text\" id=\"email\" maxlength=\"64\" autocomplete=\"off\" /></div>" +
                        "<div class=\"wizInput\">Street Address<br />" +    
                            "<input type=\"text\" id=\"street\" maxlength=\"64\" autocomplete=\"off\" /></div>" +
                        "<div class=\"wizInput\">City<br />" +    
                            "<input type=\"text\" id=\"city\" maxlength=\"64\" autocomplete=\"off\" /></div>" +
                        "<div class=\"wizInput\">Province<br />" +    
                            "<input type=\"text\" id=\"province\" maxlength=\"64\" autocomplete=\"off\" /></div>" +
                        "<div class=\"wizInput\">Postal Code<br />" +    
                            "<input type=\"text\" id=\"postcode\" maxlength=\"64\" autocomplete=\"off\" /></div>" +
                        "<div class=\"wizInput\">Suite/Apt #<br />" +    
                            "<input type=\"text\" id=\"suite\" maxlength=\"64\" autocomplete=\"off\" /></div>" +
                        "<div class=\"wizInput\">Home Phone Number<br />" +    
                            "<input type=\"text\" id=\"home_phone\" maxlength=\"64\" autocomplete=\"off\" /></div>" +
                        "<div class=\"wizInput\">Work Phone Number<br />" +    
                            "<input type=\"text\" id=\"work_phone\" maxlength=\"64\" autocomplete=\"off\" /></div>" +
                        "<div class=\"wizInput\">Mobile Phone Number<br />" +    
                            "<input type=\"text\" id=\"mobile_phone\" maxlength=\"64\" autocomplete=\"off\" /></div>";

                buff += "<div style=\"clear:both;height:20px;\"><!-- --></div>";

                if ( wiz.formData.firstTime ) {
                    buff += "<p><strong>Time saver!</strong> Would you like us store your personal and vehicle details to " +
                            " speed things up next time you book a service with us?  If so, enter " +
                            "a password in both fields below to create an account:</p>" +
                            "<div class=\"wizInput\">New Account Password (optional)<br />" +    
                                "<input type=\"password\" id=\"new_password1\" maxlength=\"64\" autocomplete=\"off\" /></div>" +
                            "<div class=\"wizInput\">Confirm Password<br />" +    
                                "<input type=\"password\" id=\"new_password2\" maxlength=\"64\" autocomplete=\"off\" /></div>";
                }
        
                if ( wiz.formData.retrieveDetails ) 
                    buff += "<p><span class=\"wizEg\"><strong>Note:</strong> You cannot change your email address using this wizard. " +
                            "If your email address has changed, you must create a new account or login to the Account Manager.</span></p>"; 


                $('#wizMain').html(buff);

                if ( wiz.formData.retrieveDetails ) 
                    $('#email').attr('disabled','disabled');

                var flds = [ 'name', 'email', 'street', 'city', 'postcode', 'province', 'postcode',
                             'suite', 'home_phone', 'work_phone', 'mobile_phone' ];

                if ( wiz.formData.firstTime ) {
                    flds.push('new_password1');
                    flds.push('new_password2');
                }

                for ( fld in flds ) {
                    $('#'+flds[fld]).val(wiz.formData[flds[fld]]);
                }


                break;
            }
            case 6: {
                buff = "<p>Please verify all your details, and then hit <strong>Book Appointment</strong>! If you need " +
                       "to make changes, simply hit back, edit or the appropriate tab.</p>";

                buff += "<div id=\"wizReview\">";

                var review = {
                    account: { label: 'Online Account', display: 'New user, opted out of creating account', step: 5 },
                    email: { label: 'Confirmation', display: '', step: 5 },
                    services: { label: 'Selected Services', display: '', step: 2 },
                    vehicle: { label: 'Your Vehicle', display: '', step: 3 },
                    comments: { label: 'Comments', display: '(none provided)', step: 3 },
                    dates: { label: 'Date(s)', display: '', step: 4 },
                    options: { label: 'Options', display: '(none selected)', step: 4 },
                    name: { label: 'Your name', display: '', step: 5 },
                    address: { label: 'Address', display: '', step: 5 },
                    phones: { label: 'Phone(s)', display: '', step: 5 }
                };

                if ( wiz.formData.retrieveDetails ) {
                    review.account.display = 'Using existing account, ' + wiz.formData.email;
                    review.account.step = 1;
                }

                if ( wiz.formData.new_password1 != '' ) {
                    review.account.display = 'New account will be created<br />' +
                                             'Login: <strong>' + wiz.formData.email + '</strong><br />' +
                                             'Password: <strong>&lt;will be sent to you via email&gt;</strong>';
                }

                for ( wizItem in wiz_service_items ) {
                    var item = wiz_service_items[wizItem];
                    if ( wiz.formData.items[item.id] == true ) {
                        review.services.display += item.name + ", ";
                    }
                }
        
                review.services.display = review.services.display.replace(/, $/, '');

                review.vehicle.display = wiz.purify(wiz.formData.year) + " " + wiz.purify(wiz.formData.make) + " " +
                                        wiz.purify(wiz.formData.model);

                if ( wiz.formData.trim != '' )
                    review.vehicle.display += ' ' + wiz.purify(wiz.formData.trim);

                if ( wiz.formData.style != '' )
                    review.vehicle.display += ' ' + wiz.purify(wiz.formData.style);

                review.vehicle.display += '<br />Licence: ' +
                    ( ( wiz.formData.licence == '' ) ? 'n/a' : wiz.purify(wiz.formData.licence) ) +
                    ', Mileage: ' +
                    ( ( wiz.formData.mileage == '' ) ? 'n/a' : wiz.purify(wiz.formData.mileage) );

                if ( wiz.formData.comments != '' )
                    review.comments.display = wiz.purify( wiz.formData.comments.replace(/\n/g, "<br />") );
                    
                var dp = wiz.formData.service_date1.split(/\//);
        
                if ( dp.length == 3 )
                    review.dates.display = "Preferred date: " + $.datepicker.formatDate('D, M d, yy', new Date(dp[0], dp[1] - 1, dp[2]) );

                if ( wiz.formData.service_date2 != '' ) {
                    dp = wiz.formData.service_date2.split(/\//);

                    if ( dp.length == 3 )
                        review.dates.display += "<br />Second choice date: " + $.datepicker.formatDate('D, M d, yy', new Date(dp[0], dp[1] - 1, dp[2]) );
                }

                if ( wiz.formData.loaner || wiz.formData.shuttle || wiz.formData.nightDrop )  {
                    review.options.display = '';
                    
                    if ( wiz.formData.loaner )
                        review.options.display += 'Loaner vehicle requested, ';

                    if ( wiz.formData.shuttle )
                        review.options.display += 'Shuttle vehicle requested, ';

                    if ( wiz.formData.nightDrop )
                        review.options.display += 'Night key drop-off requested, ';

                    review.options.display = review.options.display.replace(/, $/, '');
                }

                review.email.display = 'Confirmation will be sent to <strong>' + wiz.formData.email + '</strong>';
                review.name.display = wiz.purify(wiz.formData.name);

                review.address.display = wiz.purify(wiz.formData.street) + ' ';
                
                if ( wiz.formData.suite != '' )
                    review.address.display += '(apt/suite ' + wiz.purify(wiz.formData.suite) + ')<br />';

                review.address.display += wiz.purify(wiz.formData.city) + ' ' + wiz.purify(wiz.formData.province) + ' ' +
                                          wiz.purify(wiz.formData.postcode);

                review.address.display = review.address.display.replace(/^\s*/, '');

                if ( review.address.display == '' )
                    review.address.display = '(not provided)';
                
                review.phones.display = 'Home: ' + ( ( wiz.formData.home_phone == '' ) ? '(not provided)' : wiz.purify(wiz.formData.home_phone) ) + '<br />' +
                                        'Work: ' + ( ( wiz.formData.work_phone == '' ) ? '(not provided)' : wiz.purify(wiz.formData.work_phone) ) + '<br />' +
                                        'Mobile: ' + ( ( wiz.formData.mobile_phone == '' ) ? '(not provided)' : wiz.purify(wiz.formData.mobile_phone) );
 

                    
                buff += "<table>";

                for ( row in review ) {
                    buff += "<tr>" +
                            "<td style=\"width: 160px\"><strong>" + review[row].label + ":</strong></td>" +
                            "<td style=\"width: 380px\">" + review[row].display + "</td>" +
                            "<td>[<a href=\"#\" onclick=\"javascript: wiz.doStep(" +
                                    review[row].step + ");return false;\">edit</a>]</td>" +
                            "</tr>";
                }

                buff += "</table>";

                buff += "</div>";
    
                buff += "<div id=\"wizBulk\"><p><input type=\"checkbox\" id=\"bulk\" /> <label for=\"bulk\">" +
                        "<strong>Yes</strong>, I am " +
                        "interested in hearing about promotions or offers!</label></p>" +
                        "<p>We care about your privacy - we will not pass this information on to any third parties " +
                        "without your consent.</p></div>";

                buff += "<div id=\"wizSubmit\"><!-- --></div>";

                $('#wizMain').html(buff);

                $('#wizSubmit').click(function() {
                    wiz.submit();
                });


                $('#wizMain table tr:nth-child(even)').css({
                    backgroundColor: "#dddddd"
                });

                if ( wiz.formData.bulk ) {
                    $('#bulk').attr('checked','checked');
                }

                $('#bulk').unbind('click').click(function() {
                    if ( $('#bulk').is(':checked') )
                        wiz.formData.bulk = true;
                    else
                        wiz.formData.bulk = false;
                }); 
            
                break;
            }
            default: {
                buff = "<p>In valid step!</p>";                
                $('#wizMain').html(buff);
            }
        }

        $('div.wizCB').hover(function() {
            $('#wizHint').text( $(this).attr('title') );
            return false;
        }, function() {
            $('#wizHint').text('');
            return false;
        });

        if ( step < 6 )
            wiz.allowNext();
        else
            wiz.disableNext();

        if ( step > 1 )
            wiz.allowBack();
        else
            wiz.disableBack();

    },

    dateDays: function(date) {
        var day = date.getDay();

        var ret = [ false, '', '' ];

        if ( day > 0 ) {
            ret[0] = true;

            if ( day == 6 )
                ret[2] = 'Open 8am - 4:30pm';
            else
                ret[2] = 'Open 7am - 5:30pm';
        }

        if ( date.getDate() == 25 && date.getMonth() == 11 )
            ret[0] = false;
        
        return ret;

    },
    
    purify: function(str) {
        return htmlentities(str);
    }
};

