<?php
require_once($staticpath.'classes/Fields.class.php');
$DBFields = new DBFields;
// OTHER INFO
function _makeTextInput($column,$name,$size)
{	global $value,$field,$_SYSTEM,$html_field_title;
	if(($_REQUEST['student_id']=='new' || $_REQUEST['user_id']=='new' || $_REQUEST['new_school']=='true') && $field['DEFAULT_SELECTION'])
	{
		$value[$column] = $field['DEFAULT_SELECTION'];
		$div = false;
	}
	elseif(($_REQUEST['student_id']=='new' || $_REQUEST['user_id']=='new') || $_SYSTEM['other_info_blank']==true || $_REQUEST['new_school']== true || $_REQUEST['new_incident']==true)
		$div = false;
	else
		$div = 'force';

		/*
		!preg_match('/size/', $size) is used for check, if field has the 'size' param.
		if we don't have it, we will display textfield with size=30. But if we have size, we will
		display textfield with this size - for example, state field -> size=2, we will see a little
		textinput, instead of field with size=30
		*/
	if(!$value[$column] AND !preg_match('/size/', $size))
		$size .= ' size=30';

	if($field['TYPE']=='numeric'){
		$value[$column] = str_replace('.00','',$value[$column]);
                $size .= ' onkeyup="return check_numeric(this);" onchange="return check_numeric(this);" ';
                $size .=  ' class="numeric" ';
        }

	if($html_field_title=='user' && $column=='CFIELD_40003' && AllowEdit())
		$addition = "<A HREF=# onclick='window.open(\"modloader.php?servicemod=mod_ssetup/SchInventoryCenter.php&choose_room=true&course_section_id=".$_REQUEST['course_section_id']."&course_manage=true&useontop=false\",\"\",\"scrollbars=yes,resizable=yes,width=1000,height=400\");'>".'<IMG SRC=images/add_button.gif width=20 border=0></A>';

	return TextInput($value[$column],$html_field_title.'['.$column.']',$name,$size,$div,false,$ss).$addition;
}

function _makeDateInput($column,$name)
{	global $value,$field,$_SYSTEM,$html_field_title;

	if(($_REQUEST['student_id']=='new' || $_REQUEST['user_id']=='new' || $_REQUEST['new_school']=='true') && $field['DEFAULT_SELECTION'])
	{
		$value[$column] = $field['DEFAULT_SELECTION'];
		$div = false;
	}
	elseif(($_REQUEST['student_id']=='new' || $_REQUEST['user_id']=='new' || $_REQUEST['new_school']=='true') || $_SYSTEM['other_info_blank']==true || $_REQUEST['new_incident']==true)
		$div = false;
	else
		$div = 'force';
//	echo "c>".$value[$column];
//	die();
	if(($value[$column] && $html_field_title=='students' && $column=='CFIELD_50004') || ($value[$column] && $html_field_title=='user' && $column=='CFIELD_40004'))
	{
		if(strlen($value[$column])==19 || strlen($value[$column]==23))
			$value[$column] = substr($value[$column],0,10);

		if($value[$column] && strlen($value[$column])==9)
		{
			$year = substr($value[$column],7);
			$month = MonthNWSwitch(substr($value[$column],3,3));
			$day = substr($value[$column],0,2)*1;
		}
		elseif(strlen($value[$column])==18)
		{
	    	if(strlen($value[$column])==18)
	    		$value[$column] = substr($value[$column],0,4).' '.substr($value[$column],4);
			$year = substr($value[$column],7,4);
			$month = MonthNWSwitch(substr($value[$column],0,3));
			$day = substr($value[$column],4,2);
		}
		elseif($value[$column]) // strlen==10 - postgres formatted or truncated mssql
		{
			$year = substr($value[$column],0,4);
			$month = substr($value[$column],5,2)*1;
			$day = substr($value[$column],8)*1;
		}

		$time = mktime(0,0,0,$month,$day,$year);
		$diff = time() - $time;
		$days = floor($diff / 86400);
		if($days > 1)
		{
//			if($days < 31)
//				$addition = ' ('.$days.' '._('days old)');
//			elseif($days < 365)
//				$addition = ' ('.round($days/30.417,1).' '._('months old)');
//			else
//				$addition = ' ('.floor($days/365).' '._('years').' '.round(($days%365)/30.417,0).' '._('months old)');
				$addition = ' ('.floor($days/365).' '._('years old').')';
		}
	}
	return '<div style="display:inline;float:left;">'.DateInput($value[$column],$html_field_title.'['.$column.']',$name,$div,true,'postgres').'</div><div style="display:inline;padding:4px;">'.$addition.'</div>';
}

function _makeSelectInput($column,$name)
{	global $value,$field,$_SYSTEM,$html_field_title,$_Config,$DBFields;
//var_dump($field);
	if(($_REQUEST['student_id']=='new' || $_REQUEST['user_id']=='new' || $_REQUEST['new_school']=='true') && $field['DEFAULT_SELECTION'])
	{
		$value[$column] = $field['DEFAULT_SELECTION'];
		$div = false;
	}
	elseif(($_REQUEST['student_id']=='new' || $_REQUEST['user_id']=='new') || $_REQUEST['new_school']=='true' || $_SYSTEM['other_info_blank']==true || $_REQUEST['new_incident']==true)
		$div = false;
	else
		$div = 'force';
//var_dump($field);
	$DBFields->source_table = $field['SOURCE_TABLE'];
	$DBFields->values_field = $field['VALUES_FIELD'];
	$DBFields->codes_field = $field['CODES_FIELD'];
//	echo $DBFields->getSourceData();
//var_dump($DBFields->getSourceData());
	$source_array = $DBFields->getSourceData();
	$field['SELECT_OPTIONS'] = str_replace("\n","\r",str_replace("\r\n","\r",$field['SELECT_OPTIONS']));
	$field['SELECT_OPTION_CODES'] = str_replace("\n","\r",str_replace("\r\n","\r",$field['SELECT_OPTION_CODES']));
	if($field['USE_SOURCE'] == 'Y')
	{
		$select_options = explode("\r",$source_array['values']);
		$select_option_codes = explode("\r",$source_array['codes']);
	}
	else
	{
		$select_options = explode("\r",$field['SELECT_OPTIONS']);
		$select_option_codes = explode("\r",$field['SELECT_OPTION_CODES']);
	}
//	var_dump($select_options);
//	var_dump($value[$column]);
	
	if(count($select_options))
	{
//		foreach($select_options as $index=>$option)
//			$options[$option] = $option;
		foreach($select_options as $index=>$option)
		{
			$options[$option] = $option;
			if($_Config['use_field_codes'] == true && !empty($select_option_codes[$index]))
				$options[$option] .= ' ['.$select_option_codes[$index].']';
		}
	}
//var_dump($options);
	$addition = 'style="max-width:250;"';
	return SelectInput($value[$column],$html_field_title.'['.$column.']',$name,$options,_('N/A'),$addition,$div);
}

function _makeCheckboxInput($column,$name)
{	global $value,$field,$_SYSTEM,$html_field_title;

	if(($_REQUEST['student_id']=='new' || $_REQUEST['user_id']=='new' || $_REQUEST['new_school']=='true') && $field['DEFAULT_SELECTION'])
	{
		$value[$column] = $field['DEFAULT_SELECTION'];
		$div = false;
	}
	elseif(($_REQUEST['student_id']=='new' || $_REQUEST['user_id']=='new' || $_REQUEST['new_school']=='true') || $_SYSTEM['other_info_blank']==true || $_REQUEST['new_incident']==true)
		$div = false;
	else
		$div = true;

	return CheckboxInput($value[$column],$html_field_title.'['.$column.']',$name,'',(!$div?true:false));
}

function _makeTextareaInput($column,$name)
{	global $value,$field,$_SYSTEM,$html_field_title;
 

	if(($_REQUEST['student_id']=='new' || $_REQUEST['user_id']=='new' || $_REQUEST['new_school']=='true') && $field['DEFAULT_SELECTION'])
	{
		$value[$column] = $field['DEFAULT_SELECTION'];
		$div = false;
	}
	elseif(($_REQUEST['student_id']=='new' || $_REQUEST['user_id']=='new') || $_SYSTEM['other_info_blank']==true || $_REQUEST['new_school']== true || $_REQUEST['new_incident']==true)
		$div = false;
	else
		$div = 'force';

	 return TextareaInput($value[$column],$html_field_title.'['.$column.']',$name,'',$div);
}

function _makeMultipleInput($column,$name)
{	global $value,$field,$_SYSTEM,$html_field_title,$_Config,$DBFields;

	if(($_REQUEST['student_id']=='new' || $_REQUEST['user_id']=='new' || $_REQUEST['new_school']=='true') && $field['DEFAULT_SELECTION'])
	{
		$value[$column] = '||'.$field['DEFAULT_SELECTION'].'||';
		$div = false;
	}
	elseif(($_REQUEST['student_id']=='new' || $_REQUEST['new_school']=='true' || $_REQUEST['user_id']=='new') || $_SYSTEM['other_info_blank']==true || $_REQUEST['new_incident']==true)
		$div = false;
	else
		$div = 'force';

	$DBFields->source_table = $field['SOURCE_TABLE'];
	$DBFields->values_field = $field['VALUES_FIELD'];
	$DBFields->codes_field = $field['CODES_FIELD'];
	$source_array = $DBFields->getSourceData();
	$field['SELECT_OPTIONS'] = str_replace("\n","\r",str_replace("\r\n","\r",$field['SELECT_OPTIONS']));
	$field['SELECT_OPTION_CODES'] = str_replace("\n","\r",str_replace("\r\n","\r",$field['SELECT_OPTION_CODES']));
	if($field['USE_SOURCE'] == 'Y')
	{
		$select_options = explode("\r",$source_array['values']);
		$select_option_codes = explode("\r",$source_array['codes']);
	}
	else
	{
		$select_options = explode("\r",$field['SELECT_OPTIONS']);
		$select_option_codes = explode("\r",$field['SELECT_OPTION_CODES']);
	}
/*
	$field['SELECT_OPTIONS'] = str_replace("\n","\r",str_replace("\r\n","\r",$field['SELECT_OPTIONS']));
	$select_options = explode("\r",$field['SELECT_OPTIONS']);
	$field['SELECT_OPTION_CODES'] = str_replace("\n","\r",str_replace("\r\n","\r",$field['SELECT_OPTION_CODES']));
	$select_option_codes = explode("\r",$field['SELECT_OPTION_CODES']);
*/
	if(count($select_options))
	{
		foreach($select_options as $index=>$option)
		{
			$options[$option] = $option;
			if($_Config['use_field_codes'] == true && !empty($select_option_codes[$index]))
				$options[$option] .= ' ['.$select_option_codes[$index].']';
		}
	}
/*	if(count($select_options))
	{
		foreach($select_options as $option)
			$options[$option] = $option;
	}
*/

	return SelectMultipleInput($value[$column],$html_field_title.'['.$column.']',$name,$options,null,null,$div);
}

function _makeSelectOneInput($column,$name)
{	global $value,$field,$_SYSTEM,$html_field_title,$_Config,$DBFields;

	if(($_REQUEST['student_id']=='new' || $_REQUEST['user_id']=='new' || $_REQUEST['new_school']=='true') && $field['DEFAULT_SELECTION'])
	{
		$value[$column] = $field['DEFAULT_SELECTION'];
		$div = false;
	}
	elseif(($_REQUEST['student_id']=='new' || $_REQUEST['new_school']=='true' || $_REQUEST['user_id']=='new') || $_SYSTEM['other_info_blank']==true || $_REQUEST['new_incident']==true)
		$div = false;
	else
		$div = 'force';

	$DBFields->source_table = $field['SOURCE_TABLE'];
	$DBFields->values_field = $field['VALUES_FIELD'];
	$DBFields->codes_field = $field['CODES_FIELD'];
	$source_array = $DBFields->getSourceData();
	$field['SELECT_OPTIONS'] = str_replace("\n","\r",str_replace("\r\n","\r",$field['SELECT_OPTIONS']));
	$field['SELECT_OPTION_CODES'] = str_replace("\n","\r",str_replace("\r\n","\r",$field['SELECT_OPTION_CODES']));
	if($field['USE_SOURCE'] == 'Y')
	{
		$select_options = explode("\r",$source_array['values']);
		$select_option_codes = explode("\r",$source_array['codes']);
	}
	else
	{
		$select_options = explode("\r",$field['SELECT_OPTIONS']);
		$select_option_codes = explode("\r",$field['SELECT_OPTION_CODES']);
	}
/*
	$field['SELECT_OPTIONS'] = str_replace("\n","\r",str_replace("\r\n","\r",$field['SELECT_OPTIONS']));
	$select_options = explode("\r",$field['SELECT_OPTIONS']);
	$field['SELECT_OPTION_CODES'] = str_replace("\n","\r",str_replace("\r\n","\r",$field['SELECT_OPTION_CODES']));
	$select_option_codes = explode("\r",$field['SELECT_OPTION_CODES']);
*/
	if(count($select_options))
	{
		foreach($select_options as $index=>$option)
		{
			$options[$option] = $option;
			if($_Config['use_field_codes'] == true && !empty($select_option_codes[$index]))
				$options[$option] .= ' ['.$select_option_codes[$index].']';
		}
	}
/*
	if(count($select_options))
	{
		foreach($select_options as $option)
			$options[$option] = $option;
	}
*/
	return SelectOneInput($value[$column],$html_field_title.'['.$column.']',$name,$options,null,null,$div);
}

function _makeFileInput($column,$name)
{	global $value,$field,$_SYSTEM,$staticpath,$lang,$IntegralPathWeb;
        if(AllowEdit() && !$_REQUEST['CREATEPDF'])
	{
            $Rand = rand();
            $path = (substr($staticpath,-1)=='/')? substr($staticpath,0,-1) : $staticpath;
            $path = substr($path,strrpos($path,'/')+1).'/';
            $path = ($_REQUEST['servicemod'] == 'applicants_online')?$IntegralPathWeb:'';
            if(is_numeric($value[$column]))
                $fileID=db_request(db_execute("SELECT LOCATION,TITLE,FILESIZE FROM PROGRAM_UPLOADED_FILES WHERE ID='".$value[$column]."'"));

            $UploadForm .= "<input type=hidden name=MAX_FILE_SIZE value=10000000>";
            $UploadForm .= "<input type=hidden name=Upload_file_field_id value='$column'>";
            $UploadForm .= "<form enctype='multipart/form-data' method='POST' name='form'>";
            $UploadForm .= "<input type='file' class='input' name='fileToUpload$Rand' size='45' id='fileToUpload$Rand'>";
            $UploadForm .= "<button onclick=\\\"return ajaxFileUpload('$column','$Rand','$path','$lang');\\\" id='buttonUpload$Rand' name='buttonUpload$Rand' class='button'>"._('Upload')."</button>";
            $UploadForm .= "<input type='hidden' id='inputstudents[$column]' name='students[$column]' value=''>";
            $UploadForm .= "</form>";
            echo "<script>var tmp_form$Rand= \"$UploadForm\";</script>";
            unset($UploadForm);
            $UploadForm =  "<div id='FileInput$Rand' style='display:none;'>";
            if(!$fileID)
                $UploadForm .= "<input type='hidden' id='inputstudents[$column]' name='students[$column]' value=''>";
            $UploadForm .= "</div>";
            $UploadForm .= "<span id='afterUP$Rand' style='display:block;' name='afterUP$Rand'>";
            if($fileID)
            {
                $UploadForm .= "<a href=\"JavaScript:Delete_file('$column','$value[$column]','$Rand','$path','$lang');\">"._('Delete this file')."</a>";
                $UploadForm .= "<a href=\"".$path."index.php?download=".$value[$column]."\"><img width=\"15\" vspace=\"0\" border=\"0\" src=\"images/file_download_button.gif\" class=\"GridOutput_fieldint\">&nbsp;"._('Download ').$fileID[1]['TITLE']."</a>";
                $UploadForm .= "<input type='hidden' id='inputstudents[$column]' name='students[$column]' value='$value[$column]'>";
            }
            $UploadForm .= "</span>";
            if(!$fileID)
                $upload_style = "block";
            else
                $upload_style = "none";
//                $UploadForm .= "<div style=\"display:$upload_style;\" onclick=\"FileInputShow('$Rand')\" id=\"InputButton$Rand\">".button('file_upload',_('file'),_('Upload '))."</div>";
                $UploadForm .= "<div style=\"display:$upload_style;\" onclick=\"ShowedForms[$Rand] = '#FileInput'+$Rand;jQuery('#FileInput'+$Rand).html(tmp_form$Rand).show();jQuery('#InputButton'+$Rand).hide();\" id=\"InputButton$Rand\">".button('file_upload',null,_('Upload file'))."</div>";
            $UploadForm .= "<div id=\"upload_status$Rand\"></div>";
            return $UploadForm;
        }
        elseif(!$value[$column])
            return '-';
}

// MEDICAL ----
function _makeDate($value,$column='MEDICAL_DATE')
{	global $THIS_array,$table;

	if(!$THIS_array['ID'])
		$THIS_array['ID'] = 'new';

	$div = true;
	if($column!='MEDICAL_DATE' && $THIS_array['ID']=='new' && !$value)
	{
		$value = get_db_date();
		$div = false;
	}

	return DateInput($value,'values['.$table.']['.$THIS_array['ID'].']['.$column.']','',$div);
}

function _makeComments($value,$column)
{	global $THIS_array,$table;

	if(!$THIS_array['ID'])
		$THIS_array['ID'] = 'new';

	return TextInput($value,'values['.$table.']['.$THIS_array['ID'].']['.$column.']');
}

// LOG FIELDS
function _makeLogDate($value,$column)
{	global $THIS_array,$table,$html_field_title, $_SYSTEM;

	if(!$THIS_array['ID'])
		$THIS_array['ID'] = 'new';

	$dateValue = db_request(db_execute("SELECT SELECT_OPTIONS FROM LISTING_FIELDS_SETTINGS
						WHERE STUDENT_ID = '".$table."' AND HTML_FIELD_TITLE = '".$html_field_title."' AND FIELD_NAME = '".$column."'"));
	$div = true;
//	var_dump($dateValue);
	//$allow_navalue = null;
	$allow_navalue = true;
	if($THIS_array['ID']=='new' && !$value)
	{
		if($dateValue[1]['SELECT_OPTIONS'])
		{
			switch ($dateValue[1]['SELECT_OPTIONS'])
			{
				case 'yesterday':
					$value = '-1 day';
				break;
				case 'tomorrow':
					$value = '+1 day';
				break;
				case 'blank':
					$value = '';
					//$allow_navalue = true;
				break;
				case 'today':
				default:
					//$value = $value;
					$value = 'now';
				break;
			}
		}
		//$value = get_db_date();
		$div = false;
	}
	elseif($THIS_array['ID']!='new')
		$div = 'force';

	return DateInput($value,'log_values['.$table.']['.$THIS_array['ID'].']['.$column.']','',$div,$allow_navalue,'postgres',true);
}

function _makeLogText($value,$column)
{	global $THIS_array,$table;

	if(!$THIS_array['ID'] || $THIS_array['ID']=='new')
	{
		$THIS_array['ID'] = 'new';
		$div = false;
	}
	else
		$div = 'force';
        if($_SESSION['SIZE'][$column]<>'')
             $addition = ' size='.intval($_SESSION['SIZE'][$column]);
	

	return TextInput($value,'log_values['.$table.']['.$THIS_array['ID'].']['.$column.']','',$addition,$div);
}
function _makeNumText($value,$column)
{	global $THIS_array,$table,$_SESSION;

	if(!$THIS_array['ID'] || $THIS_array['ID']=='new')
	{
		$THIS_array['ID'] = 'new';
		$div = false;
	}
	else
		$div = 'force';
        if($_SESSION['SIZE'][$column]<>'')
             $addition = ' size='.intval($_SESSION['SIZE'][$column]);
	/*if($column=='numeric')
		$addition = 'size=4 maxlength=10';*/
        $addition .= " onkeyup=\"return check_numeric(this);\" ";
                
	return TextInput($value,'log_values['.$table.']['.$THIS_array['ID'].']['.$column.']','',$addition,$div);
}

function _makeLogNumToText($value,$column)
{	global $THIS_array,$table;

	if(!$THIS_array['ID'] || $THIS_array['ID']=='new')
	{
		$THIS_array['ID'] = 'new';
		$div = false;
	}
	else
		$div = 'force';
        if(is_numeric($value))
        {
            $NumToText = new NumToText();
            $value = $NumToText->Convert(intval($value));
        }
	if($column=='numeric')
		$addition = 'size=4 maxlength=10';

	return TextInput($value,'log_values['.$table.']['.$THIS_array['ID'].']['.$column.']','',$addition,$div);
}
/*
function get_log_count($field_name,$student_id,$student_field_id)
{
    global $log_table,$log_student_id_field,$student_field_id_field;
    $count = db_request(db_execute("SELECT ID,$field_name FROM ".$log_table." WHERE ".$log_student_id_field."='".$student_id."' AND ".$student_field_id_field."='".$student_field_id."' ORDER BY ID ASC"));
    $sum=0;
    foreach($count as $index=>$data)
    {
        if(is_numeric($data[$field_name]))
            $sum += $data[$field_name];
    }
    return $sum;
}*/
function _makeCountText($value,$column)
{	global $THIS_array,$table,$_SESSION,$sum_log;

	if(!$THIS_array['ID'])
		$THIS_array['ID'] = 'new';
        $_SESSION['COUNT'][$column] += $value;
        $sum_log[$column] += $value;
/*        ob_start();
//            var_dump($_SESSION['COUNT'][$column]);
//            var_dump($column);
    	    echo 'val===>'.$value;
    	    var_dump($sum_log);
        $header = @ob_get_contents();
        ob_end_clean();*/
        if($_SESSION['SIZE'][$table][$column]<>'')
             $addition = ' size='.intval($_SESSION['SIZE'][$table][$column]);
        
        $addition .= " onkeyup=\"return check_numeric(this);\" ";

	return  TextInput($value,'log_values['.$table.']['.$THIS_array['ID'].']['.$column.']','',$addition);
}
function _makeLogCheckbox($value,$column)
{	global $THIS_array,$table;

	if(!$THIS_array['ID'] || $THIS_array['ID']=='new')
		$THIS_array['ID'] = 'new';

	if($THIS_array['ID']=='new')
		$new = true;
	else
		$new = false;

	return CheckboxInput($value,'log_values['.$table.']['.$THIS_array['ID'].']['.$column.']','','',$new);
}

function _makeLogSelect($value,$column)
{	global $THIS_array,$table,$log_options,$log_options2;
	if(!$THIS_array['ID'] || $THIS_array['ID']=='new')
	{
		$THIS_array['ID'] = 'new';
		$div = false;
	}
	else
		$div = 'force';

	//if($column=='LOG_SELECT')
		$lo = str_replace("\n","\r",str_replace("\r\n","\r",$log_options[$column]));
	//else
		//$lo = str_replace("\n","\r",str_replace("\r\n","\r",$log_options2));
	$lo = explode("\r",$lo);
	foreach($lo as $option)
		$options[$option] = $option;
        if($_SESSION['SIZE'][$table][$column]<>'')
             $addition = ' style="width:'.(intval($_SESSION['SIZE'][$table][$column])*20).'px;" ';
            //$addition = ' width="'.(intval($_SESSION['SIZE'][$table][$column])).'" ';
        
	return SelectInput($value,'log_values['.$table.']['.$THIS_array['ID'].']['.$column.']','',$options,'',$addition,$div);
}

function _makeLogTextArea($value,$column)
{	global $THIS_array,$table,$textarea_extra;

	if(!$THIS_array['ID'] || $THIS_array['ID']=='new')
	{
		$THIS_array['ID'] = 'new';
		$div = false;
	}
	else
		$div = 'force';

	return TextAreaInput($value,'log_values['.$table.']['.$THIS_array['ID'].']['.$column.']','',$textarea_extra,$div);
}

function _makeTimeInput($value,$column)
{	global $THIS_array,$table;

	if(!$THIS_array['ID'] || $THIS_array['ID']=='new')
	{
		$THIS_array['ID'] = 'new';
		$div = false;
	}
	else
		$div = 'force';

	return TimeInput($value,'log_values['.$table.']['.$THIS_array['ID'].']['.$column.']','',$addition,$div);
}


// ENROLLMENT
function _makeStartInput($value,$column)
{	global $THIS_array,$add_codes, $DBType;

	if($THIS_array['ID'])
		$id = $THIS_array['ID'];
	elseif(($_REQUEST['student_id']=='new' || $_REQUEST['user_id']=='new'))
	{
		$id = 'new';
		if($DBType == 'mssql')
		  $default = db_request(db_execute("SELECT ".db_to_char('min(SCHOOL_DATE)', 'yyyy-mm-dd')." AS START_DATE FROM SCHOOL_CALENDAR_DAYS WHERE SCHOOL_YEAR='".get_school_year()."' AND SCHOOL_ID='".get_school_id()."'"));
		else
		  $default = db_request(db_execute("SELECT min(SCHOOL_DATE) AS START_DATE FROM SCHOOL_CALENDAR_DAYS WHERE SCHOOL_YEAR='".get_school_year()."' AND SCHOOL_ID='".get_school_id()."'"));
		$default = $default[1]['START_DATE'];
		  if(!$default || get_db_date('postgres')>$default)
			  $default = get_db_date();
		$value = $default;
	}
	else
	{
//		$add = '<TD>'.button('add').'</TD>';
		$id = 'new';
	}

//	if(!$add_codes)
	$add_codes = array();
	{
		$school_years = get_school_year();
		if(get_student_id())
		{
			if($_REQUEST['historical'] == 'Y')
			{
//				$school_years_array = db_request(db_execute("SELECT SCHOOL_YEAR FROM STUDENT_REGISTRATION WHERE STUDENT_ID = ".get_student_id().""));
//				$school_years = '';
//				foreach($school_years_array as $year)
//				{
//					$school_years .= "".$year['SCHOOL_YEAR'].",";
//				}
//				$school_years = substr($school_years,0,-1);
//				var_dump($THIS_array['SCHOOL_YEAR']);
				$school_years = $THIS_array['SCHOOL_YEAR'];
			}
		}
		$options_array = db_request(db_execute("SELECT ID,TITLE AS TITLE FROM STUDENT_REGISTRATION_SETTINGS WHERE SCHOOL_YEAR IN (".$school_years.") AND TYPE='In'"));
		if($options_array)
		{
			foreach($options_array as $option)
				$add_codes[$option['ID']] = $option['TITLE'];
		}
	}

	if(($_REQUEST['student_id']=='new' || $_REQUEST['user_id']=='new'))
		$div = false;
	else
		$div = true;

	return '<TABLE class=GridOutput_fieldint><TR>'.$add.'<TD>'.DateInput($value,'values[STUDENT_REGISTRATION]['.$id.']['.$column.']','',$div,true).'</TD><TD> - </TD><TD>'.SelectInput($THIS_array['ENROLLMENT_CODE'],'values[STUDENT_REGISTRATION]['.$id.'][ENROLLMENT_CODE]','',$add_codes).'</TD></TR></TABLE>';
}

function _makeEndInput($value,$column)
{	global $THIS_array,$drop_codes;

	if($THIS_array['ID'])
		$id = $THIS_array['ID'];
	else
		$id = 'new';
	$drop_codes = array();
//	if(!$drop_codes)
	{
		$school_years = get_school_year();
		if(get_student_id())
		{
			if($_REQUEST['historical'] == 'Y')
			{
				$school_years = $THIS_array['SCHOOL_YEAR'];
			}
//			$school_years_array = db_request(db_execute("SELECT SCHOOL_YEAR FROM STUDENT_REGISTRATION WHERE STUDENT_ID = ".get_student_id().""));
//			$school_years = '';
//			foreach($school_years_array as $year)
//			{
//				$school_years .= "".$year['SCHOOL_YEAR'].",";
//			}
//			$school_years = substr($school_years,0,-1);
		}
		$options_array = db_request(db_execute("SELECT ID,TITLE AS TITLE FROM STUDENT_REGISTRATION_SETTINGS WHERE SCHOOL_YEAR IN (".$school_years.") AND TYPE='Out'"));

		if($options_array)
		{
			foreach($options_array as $option)
				$drop_codes[$option['ID']] = $option['TITLE'];
		}
	}

	return '<TABLE class=GridOutput_fieldint><TR><TD>'.DateInput($value,'values[STUDENT_REGISTRATION]['.$id.']['.$column.']',null,'force').'</TD><TD> - </TD><TD>'.SelectInput($THIS_array['DROP_CODE'],'values[STUDENT_REGISTRATION]['.$id.'][DROP_CODE]','',$drop_codes,null,null,'force').'</TD></TR></TABLE>';
}

function _makeSchoolInput($value,$column)
{	global $THIS_array,$schools;

	// Since school cant be changed
	$id = 'new';

	if(!$schools)
		$schools = db_request(db_execute("SELECT ID,TITLE FROM SCHOOLS"));

	foreach($schools as $school)
		$options[$school['ID']] = $school['TITLE'];

	if($_REQUEST['student_id']!='new')
		return SelectInput(get_school_id(),'values[STUDENT_REGISTRATION]['.$id.'][SCHOOL_ID]','',$options,'','',false);
	else
		return get_school(get_school_id());
}

// enrollment state functions
function _make_state_CheckBoxInput($value,$name)
{
    global $THIS_array,$_SYSTEM;

	$id = $THIS_array['ID'];

	return CheckBoxInput($value,'values[STUDENT_REGISTRATION]['.$id.']['.$name.']','','',$new,'<IMG SRC=images/check.gif height=14>','<IMG SRC=images/x_small.gif>','force');
}

function _make_state_SelectInput($value, $column)
{
    global $THIS_array, $_SYSTEM;

    $id = $THIS_array['ID'];

    return SelectInput($value, 'values[STUDENT_REGISTRATION]['.$id.']['.$column.']', '', $_SYSTEM['config']['select_options']['STUDENT_REGISTRATION'][$column], false, '', 'force', null, false);
}

function _make_state_TextInput($value, $column)
{
    global $THIS_array, $_SYSTEM;

    $id = $THIS_array['ID'];

    if($_SYSTEM['config']['max_length']['STUDENT_REGISTRATION'][$column])
	$options = ' maxlength='.$_SYSTEM['config']['max_length']['STUDENT_REGISTRATION'][$column].' size='.$_SYSTEM['config']['max_length']['STUDENT_REGISTRATION'][$column];
    else
	$options = '';
    return TextInput($value, 'values[STUDENT_REGISTRATION]['.$id.']['.$column.']','',$options,'force');
}
function _make_state_DateInput($value, $column)
{
    global $THIS_array, $_SYSTEM;

    $id = $THIS_array['ID'];

    return DateInput($value,'values[STUDENT_REGISTRATION]['.$id.']['.$column.']',null,'force');
//    return DateInput($value, 'values[STUDENT_REGISTRATION]['.$id.']['.$column.']','',$options,'force');
}

function _fromadding($value, $column)
{
    global $THIS_array, $_SYSTEM;
    if($value=='online')
        return _('Online');
    elseif($value=='byoperator')
        return _('Operator');
}
?>