<?php
  // ini_set("display_errors", 1);
    include_once(PATH_TO_ROOT."/webcati/_plugins/dateselect/class/class.dateselect.plugin.php");
    $data = new DateSelect($db);
    $data->load($projectId, $q->id);
    
   
   /* print "<pre>";
    var_dump($fieldsValues);
    var_dump($ansArr);*/
   
    //print "</pre>";
   /* print "#############################################################################################################################################################";*/
  
  
  $showFormat = "d.m.Y";
  if($data->getVal('timepicker'))
			$showFormat .= " H:i";

    $defaultOptions = array(
      // "formatTime"   =>'H:i',
        "format"   => $showFormat,
        "defaultDate"  =>'8.12.1986',
        "lang" => $data->getVal('language'),
        "defaultTime" =>'10:00',
        "timepickerScrollbar" => 'false'
       );
       
       if(strlen($data->getVal('minDate')) > 0 )
           $defaultOptions['minDate'] = $data->getVal('minDate'); 
       if(strlen($data->getVal('maxDate')) > 0 )
           $defaultOptions['maxDate'] = $data->getVal('maxDate'); 
       if(strlen($data->getVal('select_year_variable')) > 0)
       {
            $inserted_year = getVar($data->getVal('select_year_variable'));
            $inserted_year = $inserted_year[$data->getVal('select_year_variable')];
            
            if(checkdate('1', '1', $inserted_year))
            {
                $defaultOptions['minDate'] = '1.1.' . $inserted_year;
                $defaultOptions['maxDate'] = '31.12.' . $inserted_year;
                $defaultOptions['defaultDate'] =  '1.1.' . $inserted_year;
            }
       }
	   
	   	if($tmpDate = strtotime($data->getVal('default_date')))
		{	
			
			$defaultOptions['defaultDate'] = date("d.m.Y", $tmpDate);
		}
		
		
       
   /*if(count($ansArr) == 1)
        $defaultOptions['inline'] = 'true';*/
    
?>     
<link rel="stylesheet" type="text/css" href="<?php echo PATH_TO_ROOT."/webcati/_plugins/dateselect/web/css/";?>jquery.datetimepicker.css"/>
<script src="<?php echo PATH_TO_ROOT."/webcati/_plugins/dateselect/web/js/";?>jquery.datetimepicker.js"></script>

<script>
$(function()
{   
    function Create(selector, options)
    {
        $(selector).datetimepicker(options); 
        //we add event to image next to the field
        var imgUrl = "<?php echo $data->getVal('icon_trigger'); ?>";
        if(imgUrl.length > 0 && <?php echo count($ansArr); ?> > 0 ) 
        {
            $(selector + '_img').click(function(){
                jQuery(selector).datetimepicker('show');
            });
            
        }  
    }
    
    <?php foreach($ansArr as $answer): ?>
        Create("#<?php echo $q->q_name; ?>_<?php echo $answer[0]; ?>", <?php echo $data->getOptions($defaultOptions); ?>);
     <?php endforeach; ?>
     
});
    
    
</script>
<?php 
$pluginMustCondition = array();
$html .= "<table width=100% cellpadding=0 cellspacing=0 style='text-align:left;padding:0'>"; // Borut, 2015.10.15, dodal width=100% cellpadding=0 cellspacing=0
foreach($ansArr as $answer)
{
    $i = array_search($answer, $ansArr) + 1;
    $value = $fieldsValues[$q->q_name.'_'. $i];
	if($data->getVal('show_default_date') && strlen($value) == 0)
	{
		$value = $data->getVal('default_date');
	}
	else if(strlen($value) == 0)
    {
        $value = date('Y/m/d H:i');
        if(checkdate('1', '1', $inserted_year))
            $value = "1.1." . $inserted_year;
    }
	// <td>'. $answer[1] .'</td>, Borut, 2015.10.15, prestavil sem gor v komentar iz spodnje prazne vrstice... dajmo to podpret z opcijo...
    $html .= '<tr>
        
        <td width=100px><input type="text" name="answers['.$q->q_name.'_'. $i .']" value="'. $value . '" id="'.$q->q_name.'_'.$answer[0].'"/></td>'; // Borut, 2015.10.15, dodal width=100px
    if(strlen($data->getVal('icon_trigger')) > 0 && count($ansArr) > 0)
    {
        $html .= '<td id="'.$q->q_name.'_'.$answer[0].'_img"><img src="' . $data->getVal('icon_trigger') . '" /></td>';
    }
    $html .= '</tr>';
    $pluginMustCondition[] = "$" . $q->q_name . "_" . $i ." <> '" . $value ."' ";
}
$html .= "</table>";
if ($q->must_answer &&  $pluginMustCondition) $_SESSION['mustAnswerCondition'] = implode(" AND ",$pluginMustCondition);