<?php
$form = $this->beginWidget('ext.PlGrid.PlActiveForm', array(
    'id' => 'toc-form',
    'loadCoreScripts' => true,
    'action' => array("index")
        ));

echo '<h3 data-translation-id="WIZARD_TOC_HEADER_TEXT">' . __('WIZARD_TOC_HEADER_TEXT') . '</h3>';
echo '<div '
    . 'class="well well-small" '
    . 'data-translation-id="WIZARD_TOC_TEXT"'
    . 'style="max-height: 500px;overflow: scroll;'
    . '>' . $content . '</div>';

    echo $form->checkBoxRow($wizardModel, "agreeTOC", array("data-translation-id" => "WIZARD_AGREE"));

echo CHtml::submitButton("next", array(
                    'type' => 'htmlSubmit',
                    'value' => 'next',
                    'label' => __('WIZARD_NEXT_BTN'),
                    'name' => 'WizardForm[next]',
                    'class' => 'btn btn-primary',
                    'id' => 'originalNextBtn',
                    'data-translation-id' => 'WIZARD_SKIP_BTN',
                    'style' => 'display:none',
                ));



$this->endWidget();

$navigation = $this->beginWidget('ext.PlGrid.PlActiveForm', array(
    'id' => 'nav-form',
    'loadCoreScripts' => true,
    'clientOptions' => array('validateOnSubmit'=>false), // FIXME
    'action' => array("index")
));

echo CHtml::submitButton("prevButton", array(
                    'type' => 'htmlSubmit',
                    'value' => 'prev',
                    'label' => __('WIZARD_PREV_BTN'),
                    'name' => 'WizardForm[prev]',
                    'class' => 'btn',
                    'data-translation-id' => 'WIZARD_PREV_BTN',
                    'id' => 'originalPrevBtn',
                    'style' => 'display:none',
));

echo CHtml::submitButton("skip", array(
                    'type' => 'htmlSubmit',
                    'value' => 'skip',
                    'label' => __('WIZARD_NEXT_BTN'),
                    'name' => 'WizardForm[skip]',
                    'class' => 'btn btn-primary',
                    'id' => 'originalSkipBtn',
                    'data-translation-id' => 'WIZARD_SKIP_BTN',
                    'style' => 'display:none',
                    "disabled" => "disabled"
                ));
$this->endWidget();
?>

<script>
    $(function () {
        
        $('[data-translation-id="WIZARD_TOC_TEXT"]').find("a").each(function (i,e) { $(e).attr("target","_blank") });
        
        // Handler for .ready() called.
        $('#nextBtn').attr('disabled', (!$('#Wizard_agreeTOC').is(':checked')));
        $('#prevBtn').attr('disabled', $('#originalPrevBtn').attr('disabled'));
        $('#skipBtn').attr('disabled', $('#originalSkipBtn').attr('disabled'));

        $('#skipBtn').click(function () {
            $('#originalSkipBtn').click();
        });

        $('#nextBtn').click(function () {
            $('#originalNextBtn').click();
        });

        $('#prevBtn').click(function () {
            $('#originalPrevBtn').click();
        });
        
        $('#nextBtn').data("loading-text", $('#nextBtn').text());        
        $('#Wizard_agreeTOC').change(function () {            
            setNextBtnEnabledState(this);
        });
    });
    
    function setNextBtnEnabledState(element) {
        if ($(element).is(":checked")) {
                $('#nextBtn').button("reset");
        } else {
            $('#nextBtn').button("loading");
        }
    };

</script>