<?php
//   ini_set("display_errors", 1);
    include_once(PATH_TO_ROOT."/webcati/_plugins/intervalselect/class/class.intervalselect.plugin.php");
    include_once(PATH_TO_ROOT . "_class/createQuestion/Question.php");

    $data = new IntervalSelect($db);
    $data->load($projectId, $q->id);


    $fields = Question::answerToArray($data->getVal('available_fields_list'));
/*    print "<pre>";
 //   var_dump($fieldsValues);
    print "</pre>";
  */
    function getField($str, $fields)
    {
        foreach($fields as $field)
        {
            if($field[0] == $str || $field[1] == $str)
                return $field;
        }

        return NULL;
    }

    function legendToOptions($legenda)
    {
        $str = '';
        foreach(Question::answerToArray($legenda) as $leg)
        {
                $str .= '<option value="' . $leg[0] . '">' . $leg[1] . '</option>';
        }

        return $str;
    }

if($q->tip_sql != 2)
{
    $html .= "You can use this plugin only with Scale question";

}
else if(!strlen($q->odg_sql) > 0 || !strlen($q->leg_sql) > 0 || !strlen($q->atr_sql) > 0 )
{
    $html .= "You have to set answers, legend and attributes";
}
else if(count($fields) == 0)
{
    $html .= "You have to set at least 1 field";
}
else {


$html .= "<table border='0px'>
              <tr>
                <td>
                    <select id='fields' name='fields' multiple style='width:250px;height:350px'>";
                    $first = true;
                    $selected = "selected";
                    foreach($fields as $f)
                    {
                        $html .= "<option " . $selected  . " value='" . $f[0] . "'> " . $f[1] . "</option>";
                        if($first)
                        {
                            $selected = "";
                            $first = false;
                        }
                    }

$html .=           "</select>
                </td>
                <td>
                <table border='0px'>
                    <tr>
                        <td></td>";
                    foreach(Question::answerToArray($q->odg_sql) as $ans)
                        $html .= "<td><b>" . $ans[1] . "</b></td>";

$html .=           "</tr>";

                    foreach(Question::answerToArray($q->atr_sql) as $atr)
                    {
                        $html .= "<tr>
                                    <td class='atribut'><b>" . $atr[1] . "</b></td>";
                        foreach(Question::answerToArray($q->odg_sql) as $ans)
                        {
                                $value = $fieldsValues[$q->q_name . "_" . $ans[0] . "_" . $atr[0]];

                                $html .= "<input type='hidden' name='answers[" . $q->q_name . "_" . $ans[0] . "_" . $atr[0] . "]' value='" . $value . "' id='" . $q->q_name . "_" . $ans[0] . "_" . $atr[0] . "' />";

                                $field = getField($value, $fields);

                                if(!is_null($field))
                                {
                                       $html .= "<td><span class='selected' id='" . $ans[0] . "_" . $atr[0] . "'  style='background:#BEBEBE;'>" . $field[1] . "</span></td>";
                                }
                                else
                                {
                                    //Marko 21.5.2015
                                    if($data->getVal('show_checkboxes')){
                                        $html .= "<td><input id='" . $ans[0] . "_" . $atr[0] . "' class='ddSelect' type=checkbox style='width:150px'/></td>";
                                    }else{
                                        $html .= "<td><select id='" . $ans[0] . "_" . $atr[0] . "' class='ddSelect' style='width:150px'>";
                                        $html .= legendToOptions($q->leg_sql);
                                        $html .= "</select></td>";
                                    }
                                    //!Marko 21.5.2015
                                }

                        }
                        $html .=  "</tr>";

                    }

$html .=        "</table>
                </td>
            </tr>
</table>";

}
?>
<script>

    $(function()
    {
        var focusedId;
        $(document.body).on('change', ".ddSelect", function(e){
       // $('.ddSelect').change(function(e){
            var field = $("#fields").find(":selected");

            //Marko 21.4.2015
            if(field.text().length > 0){
                var dd = $(this);
                var id;
                if(dd.is('input')){
                    id = dd.attr('id');
                    dd.replaceWith('<span id="' + id + '" class="selected" style="background:#BEBEBE;">'+ field.text() +'</span>');
                        $('#<?php echo $q->q_name; ?>_' + e.target.id).val(field.val());
                }else{
                    if(dd.find(":selected").val() == 2){
                        id = dd.attr('id');
                        dd.replaceWith('<span id="' + id + '" class="selected" style="background:#BEBEBE;">'+ field.text() +'</span>');
                        $('#<?php echo $q->q_name; ?>_' + e.target.id).val(field.val());
                    }
                }
            //!Marko 21.4.2015

           }
           else alert("You have to select field first!");
        });

        $(document.body).on('focus', ".ddSelect", function(e){
          focusedId = $(document.activeElement).attr('id');
        });

        $('.atribut').click(function(){
            var field = $("#fields").find(":selected");
            if(field.text().length > 0)
            {
                var tds = $(this).siblings('td');
                var id = "";

                var ids = [];
                var start_index  = 0;
                for(var i = 0; i < tds.length; i++)
                {
                    id = $(tds[i]).children().attr('id');
                    ids.push(id);

                    if($(tds[i]).children().attr('class') != 'ddSelect')
                        start_index = i;
                }

                if(ids.indexOf(focusedId) > -1)
                    start_index = ids.indexOf(focusedId);

                for(i = start_index; i < tds.length; i++)
                {
                    id = $(tds[i]).children().attr('id');
                    $(tds[i]).children().replaceWith('<span id="' + id + '" class="selected" style="background:#BEBEBE;">'+ field.text() +'</span>');
                }

                var inputs = $(this).siblings('input');
                inputs.val(field.val());
            }
            else alert("You have to select field first!");
        });

         $(document.body).on('dblclick', ".selected", function(e){
            var span = $(this);
            var id = span.attr('id');
            
            $('#<?php echo  $q->q_name; ?>_' + id).val('null');
            <?php
            if($data->getVal('show_checkboxes'))
            {
            ?>
                span.replaceWith('<input id="'+ id + '" class="ddSelect" type=checkbox style="width:150px"/>');
            <?php 
            }
            else 
            {
            ?>
                span.replaceWith('<select id="' + id + '" class="ddSelect" style="width:150px"><?php echo legendToOptions($q->leg_sql);?></select>');
            <?php
            }
            ?>
           
           
         });

    });

</script>

