<div class="col-xs-12">

    <?php
    $form = $this->beginWidget('bootstrap.widgets.TbActiveForm', array(
        'id' => 'poll-form',
            //'enableAjaxValidation'=>TRUE,
    ));
    ?>

    <?php echo $form->errorSummary($model); ?>

    <?php echo $form->textFieldControlGroup($model, 'title', array('size' => 60, 'maxlength' => 255)); ?>
    <?php echo $form->textAreaControlGroup($model, 'description', array('rows' => 6, 'cols' => 50)); ?>
    <?php echo $form->dropDownListControlGroup($model, 'status', $model->statusLabels()); ?>


    <div class="widget-box">
        <div class="widget-header">
            <h4><?php echo Yii::t('admin', 'Choices'); ?></h4>
            <div class="widget-toolbar">
            </div>
        </div>
        <div class="widget-body">
            <div class="control-group clearfix ">
                <table id="poll-choices " class="table ">
                    <thead>
                    <th>Weight</th>
                    <th>Label</th>
                    <th>Actions</th>
                    </thead>
                    <tbody>
                        <?php
                        $newChoiceCount = 0;
                        foreach ($choices as $choice) {
                            $this->renderPartial('/pollchoice/_formChoice', array(
                                'id' => isset($choice->id) ? $choice->id : 'new' . ++$newChoiceCount,
                                'choice' => $choice,
                            ));
                        }
                        ++$newChoiceCount; // Increase once more for Ajax additions
                        ?>
                        <tr id="add-pollchoice-row">
                            <td class="weight"></td>
                            <td class="cell-label">
                                <?php echo CHtml::textField('add_choice', '', array('size' => 60, 'id' => 'add_choice')); ?>
                                <div class="errorMessage display-none">You must enter a label.</div>
                            </td>
                            <td class="cell-actions">
                                <a href="#" class="btn btn-sm btn-primary" id="add-pollchoice">Add Choice</a>
                            </td> 
                        </tr>
                    </tbody>
                </table>
            </div>
        </div>
    </div>

    <?php echo TbHtml::formActions(array(TbHtml::submitButton($model->isNewRecord ? 'Create' : 'Save', array('color' => TbHtml::BUTTON_COLOR_PRIMARY)), TbHtml::resetButton('Reset'),)); ?>


    <?php $this->endWidget(); ?>

</div><!-- form -->

<?php
$callback = Yii::app()->createUrl('/admin/poll/pollchoice/ajaxcreate');
$js = <<<JS
var PollChoice = function(o) {
  this.target = o;
  this.label  = jQuery(".cell-label input", o);
  this.weight = jQuery(".cell-weight select", o);
  this.errorMessage = jQuery(".errorMessage", o);

  var pc = this;

  pc.label.blur(function() {
    pc.validate();
  });
}
PollChoice.prototype.validate = function() {
  var valid = true;

  if (this.label.val() == "") {
    valid = false;
    this.errorMessage.fadeIn();
  }
  else {
    this.errorMessage.fadeOut();
  }

  return valid;
}

var newChoiceCount = {$newChoiceCount};
var addPollChoice = new PollChoice(jQuery("#add-pollchoice-row"));

jQuery("tr", "#poll-choices tbody").each(function() {
  new PollChoice(jQuery(this));
});

jQuery("#add-pollchoice").click(function() {
  if (addPollChoice.validate()) {
    jQuery.ajax({
      url: "{$callback}",
      type: "POST",
      dataType: "json",
      data: {
        id: "new"+ newChoiceCount,
        label: addPollChoice.label.val()
      },
      success: function(data) {
        addPollChoice.target.before(data.html);
        addPollChoice.label.val('');
        new PollChoice(jQuery('#'+ data.id));
      }
    });

    newChoiceCount += 1;
  }

  return false;
});
JS;

Yii::app()->clientScript->registerCoreScript('jquery');
Yii::app()->clientScript->registerScript('pollHelp', $js, CClientScript::POS_END);
?>
