<?if(!defined("B_PROLOG_INCLUDED") || B_PROLOG_INCLUDED!==true)die();
if($arParams['~COURCES_IBLOCK_ID'] && CModule::IncludeModule("iblock"))
{
	$allowTmst=time()-24*60*60*$arParams['~END_BOOKING'];
	$allowDate=date("Y-m-d 00:00:00",$allowTmst);
	$arFilter=array("IBLOCK_ID"=>$arParams['~COURCES_IBLOCK_ID'], "ACTIVE"=>"Y", "ACTIVE_DATE"=>"Y", ">PROPERTY_TIME_START"=>$allowDate);
	$resElem=CIBlockElement::GetList(Array("NAME"=>"asc"), $arFilter,false,false,array("ID","NAME","IBLOCK_SECTION_ID","IBLOCK_ID","CODE"));
	$resArrElem=array();
	while($obRes =$resElem->GetNextElement())
	{
		$arrElem= $obRes->GetFields();
		$resArrElem[$arrElem['IBLOCK_SECTION_ID']][$arrElem['ID']]['NAME']=$arrElem['NAME'];
		$resArrElem[$arrElem['IBLOCK_SECTION_ID']][$arrElem['ID']]['CODE']=$arrElem['CODE'];
		$arPropStart = $obRes->GetProperty("TIME_START");
		$arPropEnd = $obRes->GetProperty("TIME_END");
	
  		$allowDate=date("Y-m-d",$allowTmst);
  		foreach($arPropStart['VALUE'] as $key => $date)
  		{	  			
  			$arrDt = ParseDateTime($date, "DD.MM.YYYY");
  			$newDate=$arrDt['YYYY']."-".$arrDt['MM']."-".$arrDt['DD'];    	
  			
  			if($newDate>$allowDate)
  			{
  				$resArrElem[$arrElem['IBLOCK_SECTION_ID']][$arrElem['ID']]['DATES'][$key]=$arrDt['DD']." ".ToLower(GetMessage("MONTH_".intval($arrDt["MM"])."_S"));
  				if($arPropEnd['VALUE'][$key])
	  			{
	  				$arrDt = ParseDateTime($arPropEnd['VALUE'][$key], "DD.MM.YYYY");
	  				$resArrElem[$arrElem['IBLOCK_SECTION_ID']][$arrElem['ID']]['DATES'][$key].=" - ".$arrDt['DD']." ".ToLower(GetMessage("MONTH_".intval($arrDt["MM"])."_S"));
	  			}  	
  			}  					
  		}
	}

	$arFilter=array("IBLOCK_ID"=>$arParams['~COURCES_IBLOCK_ID'], "ACTIVE"=>"Y", "DATE_ACTIVE");
	$resSect=CIBlockSection::GetList(Array("NAME"=>"asc"), $arFilter,false);
	$resArrSect=array();
	while($arrSect=$resSect->GetNext())
	{
		if(is_array($resArrElem[$arrSect['ID']]))
		{
			$resArrSect[$arrSect['ID']]=array("NAME"=>$arrSect['NAME'], "ELEMENTS"=>$resArrElem[$arrSect['ID']],"CODE"=>$arrSect['CODE']);
		}
	}
	
	$arResult["QUESTIONS"]['course']['HTML_CODE']='<select id="form_text_'.$arResult["QUESTIONS"]['course']['STRUCTURE'][0]['QUESTION_ID'].'" name="form_text_'.$arResult["QUESTIONS"]['course']['STRUCTURE'][0]['QUESTION_ID'].'" class="inputtext" onChange="var cours=$(\'#form_text_'.$arResult["QUESTIONS"]['course']['STRUCTURE'][0]['QUESTION_ID'].' option:selected\').attr(\'course\'); $(\'.course_date\').css(\'display\',\'none\'); $(\'#course_date_\'+cours).css(\'display\',\'block\'); $(\'.course_date select\').attr(\'name\',\'\'); $(\'#course_date_\'+cours+\' select\').attr(\'name\',\'form_text_'.$arResult["QUESTIONS"]['course_date']['STRUCTURE'][0]['QUESTION_ID'].'\');">';
	$arResult["QUESTIONS"]['course_date']['HTML_CODE']="";	
	$j=0;
	
	foreach($resArrSect as $sectId => $sectArr)
	{
		$arResult["QUESTIONS"]['course']['HTML_CODE'].='<optgroup label="'.$sectArr['NAME'].'">';
		$i=0;
		foreach($sectArr["ELEMENTS"] as $elId => $el)
		{
			if(($j==0 && !$_REQUEST['SECTION_CODE'] && !$_REQUEST['ELEMENT_CODE']) || ($i==0 && $_REQUEST['SECTION_CODE']==$sectArr['CODE'] && !$_REQUEST['ELEMENT_CODE']) || $_REQUEST['ELEMENT_CODE']==$el['CODE'])
			{
				$selected='selected="selected"';
				$name='form_text_'.$arResult["QUESTIONS"]['course_date']['STRUCTURE'][0]['QUESTION_ID'];
				$display='';
			}
			else
			{
				$selected='';
				$name='';
				$display='style="display:none;"';
			}
			$arResult["QUESTIONS"]['course']['HTML_CODE'].='<option course="'.$elId.'" '.$selected.' value="'.$sectArr['NAME'].': ['.$elId.'] '.$el['NAME'].'">'.$el['NAME'].'</option>';
			$arResult["QUESTIONS"]['course_date']['HTML_CODE'].='<div id="course_date_'.$elId.'" '.$display.' class="course_date"><select name="'.$name.'" class="inputtext">';
			foreach($el['DATES'] as $date)
			{
				$arResult["QUESTIONS"]['course_date']['HTML_CODE'].='<option value="'.$date.'">'.$date.'</option>';
			}
			$arResult["QUESTIONS"]['course_date']['HTML_CODE'].='</select></div>';
			$i++;	
			$j++;		
		}
		$arResult["QUESTIONS"]['course']['HTML_CODE'].='</optgroup>';
	}
	$arResult["QUESTIONS"]['course']['HTML_CODE'].='</select>';
	
}

?>