<?php

function Widgets($item)
{	global $addition,$_SYSTEM;
//echo "widget itss are = ".$item;
	if(!is_array($addition['functions']))
		$addition['functions'] = array();

	if(get_user('PROFILE')!='parent' && get_user('PROFILE')!='student')
	{
		switch($item)
		{
			case 'all':
				if($_SYSTEM['user_search']!==true && substr($_REQUEST['servicemod'],0,9) != 'mod_apply')
				{
					if(AllowUse('mod_curriculum/Curriculum.php') || AllowUse('mod_curriculum/ScdStudReqProperties.php'))
					{
						$addition['search'] .= '<A onclick="switchMenu(\'scheduling_table\');" style="cursor:pointer;"><IMG SRC=images/arrow_right.gif id=scheduling_table_arrow> <B>'._('Scheduling').'</B></A><BR><TABLE border="0" cellspacing=0 cellpadding=4 class=EntireBox width=100% id=scheduling_table style="display:none;">';
						Widgets('section');
						Widgets('course');
						Widgets('request');
						$addition['search'] .= '</TABLE>';
					}
					
					if(AllowUse('mod_attendance/AttdStuSumAttendance.php') || AllowUse('mod_attendance/AttdByDays.php'))
					{
						$addition['search'] .= '<A onclick="switchMenu(\'absences_table\');" style="cursor:pointer;"><IMG SRC=images/arrow_right.gif id=absences_table_arrow> <B>'._('Absences').'</B></A><BR><TABLE cellspacing=0 cellpadding=4 class=EntireBox width=100% id=absences_table style="display:none;">';
						Widgets('absences');
						$addition['search'] .= '</TABLE>';
					}
					
					if(AllowUse('mod_grades/GrdPrintStudGrades.php') || AllowUse('mod_grades/GrdPrintStudentReportCards.php') || AllowUse('mod_grades/GrdStudentPrintGC.php'))
					{
						$addition['search'] .= '<A onclick="switchMenu(\'grades_table\');" style="cursor:pointer;"><IMG SRC=images/arrow_right.gif id=grades_table_arrow> <B>'._('Final Grades').'</B></A><BR><TABLE class=EntireBox width=100% cellspacing=0 cellpadding=4 id=grades_table style="display:none;">';
						Widgets('gpa');
						Widgets('average_grade');
						Widgets('class_rank');
						Widgets('letter_grade');
						$addition['search'] .= '</TABLE>';
					}
				
					if(AllowUse('mod_activity/ActStudMain.php') || AllowUse('mod_activity/ActStudAdd.php'))
					{
						$addition['search'] .= '<A onclick="switchMenu(\'activity_table\');" style="cursor:pointer;"><IMG SRC=images/arrow_right.gif id=activity_table_arrow> <B>'._('Activities').'</B></A><BR><TABLE cellspacing=0 cellpadding=4 class=EntireBox width=100% id=activity_table style="display:none;">';
//						Widgets('suitability');
						Widgets('activity');
						$addition['search'] .= '</TABLE>';
					}
					if(AllowUse('mod_evaluation/SuitStudReport.php') || AllowUse('mod_evaluation/SuitPostingIntrProgress.php'))
					{
						$addition['search'] .= '<A onclick="switchMenu(\'suitability_table\');" style="cursor:pointer;"><IMG SRC=images/arrow_right.gif id=suitability_table_arrow> <B>'._('Suitability').'</B></A><BR><TABLE cellspacing=0 cellpadding=4 class=EntireBox width=100% id=suitability_table style="display:none;">';
						Widgets('suitability');
						$addition['search'] .= '</TABLE>';
					}

					
					if(AllowUse('mod_discipline/DisListSubmittedReports.php'))
					{
						$addition['search'] .= '<A onclick="switchMenu(\'discipline_table\');" style="cursor:pointer;"><IMG SRC=images/arrow_right.gif id=discipline_table_arrow> <B>'._('Discipline').'</B></A><BR><TABLE cellspacing=0  cellpadding=4 class=EntireBox width=100% id=discipline_table style="display:none;">';
						Widgets('discipline');
						$addition['search'] .= '</TABLE>';
					}
	
					if(AllowUse('mod_billing/BilStudBalanceList.php') || AllowUse('mod_billing/BilStudFeesList.php') || AllowUse('mod_billing/BilStudPaysList.php'))
					{
						$addition['search'] .= '<A onclick="switchMenu(\'billing_table\');" style="cursor:pointer;"><IMG SRC=images/arrow_right.gif id=billing_table_arrow> <B>'._('Billing').'</B></A><BR><TABLE cellspacing=0  class=EntireBox width=100% id=billing_table style="display:none;">';
						Widgets('balance');
						$addition['search'] .= '</TABLE>';
					}
				}
			break;
//					$addition['search'] .= " <TR BGCOLOR=#".Preferences('HIGHLIGHT_BACKGROUND')."><td colspan=2 width=400 background=themes/".Preferences('HIGHLIGHT_FRAME')."/bottom_shadow.png height=20 width=100%> </td></tr>";

			case 'section':
				if($_REQUEST['w_course_section_id'])
				{
					$course = db_request(db_execute("SELECT c.TITLE AS COURSE_TITLE,cp.TITLE,cp.COURSE_ID,cp.COURSE_CREDIT_TITLE FROM COURSE_SECTIONS cp,COURSES c WHERE c.COURSE_ID=cp.COURSE_ID AND cp.COURSE_SECTION_ID='".$_REQUEST['w_course_section_id']."'"));
					if($_REQUEST['not_w_course_period']!='Y')
					{
						$addition['FROM'] .= ",STUDENT_SCHEDULE w_ss";
						$addition['WHERE'] .= " AND w_ss.STUDENT_ID=s.STUDENT_ID AND w_ss.SCHOOL_YEAR=ssm.SCHOOL_YEAR AND w_ss.SCHOOL_ID=ssm.SCHOOL_ID AND w_ss.COURSE_SECTION_ID='".$_REQUEST['w_course_section_id']."' AND ('".get_db_date()."' BETWEEN w_ss.START_DATE AND w_ss.END_DATE OR w_ss.END_DATE IS NULL)";
						$_SYSTEM['SearchTerms'] .= '<font color=gray><b>'._('Scheduled into Course Section').': </b></font>'.$course[1]['COURSE_TITLE'].': '.$course[1]['TITLE'].'<BR>';
					}
					else
					{
						$addition['WHERE'] .= " AND NOT EXISTS (SELECT '' FROM STUDENT_SCHEDULE sr WHERE sr.STUDENT_ID=ssm.STUDENT_ID AND sr.SCHOOL_YEAR=ssm.SCHOOL_YEAR AND sr.COURSE_SECTION_ID='".$_REQUEST['w_course_section_id']."') ";
						$_SYSTEM['SearchTerms'] .= '<font color=gray><b>'._('Not Scheduled Into').': </b></font>'.$course[1]['TITLE'].'<BR>';					
						// .' - '.$_REQUEST['request_course_credits']
					}
				}
				if(AllowUse('mod_curriculum/Curriculum.php'))
					$addition['search'] .= " <TR><TD>"._('Scheduled into Course Section')."</TD><TD><DIV id=\"section_div\"></DIV> <A HREF=# onclick=\"window.open('modloader.php?servicemod=mod_portal/CourseSectionSelector.php&useontop=false','','scrollbars=yes,resizable=yes,width=800,height=400');\"><SMALL><B>"._('Choose')."</B></SMALL></A></TD></TR>";
			break;

			case 'course':
				//заменил w_ss на ww_ss потому что был баг, при выборе в поиске и секции и курса
				if($_REQUEST['w_course_id'])
				{
					$course = db_request(db_execute("SELECT c.TITLE AS COURSE_TITLE,c.COURSE_ID FROM COURSES c WHERE c.COURSE_ID='".$_REQUEST['w_course_id']."'"));
					if($_REQUEST['not_w_course']!='Y')
					{
						$addition['FROM'] .= ",STUDENT_SCHEDULE ww_ss";
						$addition['WHERE'] .= " AND ww_ss.STUDENT_ID=s.STUDENT_ID AND ww_ss.SCHOOL_YEAR=ssm.SCHOOL_YEAR AND ww_ss.SCHOOL_ID=ssm.SCHOOL_ID AND ww_ss.COURSE_ID='".$course[1]['COURSE_ID']."' AND ('".get_db_date()."' BETWEEN ww_ss.START_DATE AND ww_ss.END_DATE OR ww_ss.END_DATE IS NULL)";
						$_SYSTEM['SearchTerms'] .= '<font color=gray><b>'._('Scheduled into Course').': </b></font>'.$course[1]['COURSE_TITLE'].'<BR>';
					}
					else
					{
						$addition['WHERE'] .= " AND NOT EXISTS (SELECT '' FROM STUDENT_SCHEDULE sr WHERE sr.STUDENT_ID=ssm.STUDENT_ID AND sr.SCHOOL_YEAR=ssm.SCHOOL_YEAR AND sr.COURSE_ID='".$_REQUEST['w_course_id']."') ";
						$_SYSTEM['SearchTerms'] .= '<font color=gray><b>'._('Not Scheduled Into').': </b></font>'.$course[1]['COURSE_TITLE'].'<BR>';
					}
				}
				if(AllowUse('mod_curriculum/Curriculum.php'))
					$addition['search'] .= "<TR><TD>"._('Scheduled into Course')."</TD><TD><DIV id=\"course_div\"></DIV> <A HREF=# onclick=\"window.open('modloader.php?servicemod=mod_portal/CourseSelector.php&useontop=false','','scrollbars=yes,resizable=yes,width=800,height=400');\"><SMALL><B>"._('Choose')."</B></SMALL></A></TD></TR>";
			break;
	
			case 'request':
				if($_REQUEST['request_course_id'] && $_REQUEST['request_course_credits'])
				{
					$course = db_request(db_execute("SELECT c.TITLE FROM COURSES c WHERE c.COURSE_ID='".$_REQUEST['request_course_id']."'"));
					if(!$_REQUEST['not_request_course'])
					{
						$addition['FROM'] .= ",STUDENT_SCHEDULE_REQUESTS sr";
						$addition['WHERE'] .= " AND sr.STUDENT_ID=s.STUDENT_ID AND sr.SCHOOL_YEAR=ssm.SCHOOL_YEAR AND sr.SCHOOL_ID=ssm.SCHOOL_ID AND sr.COURSE_ID='".$_REQUEST['request_course_id']."' AND sr.COURSE_CREDIT_TITLE='".$_REQUEST['request_course_credits']."'";
						$_SYSTEM['SearchTerms'] .= '<font color=gray><b>'._('Requested').': </b></font>'.$course[1]['TITLE'].'<BR>';
						// .' - '.$_REQUEST['request_course_credits']
					}
					else
					{
						$addition['WHERE'] .= " AND NOT EXISTS (SELECT '' FROM STUDENT_SCHEDULE_REQUESTS sr WHERE sr.STUDENT_ID=ssm.STUDENT_ID AND sr.SCHOOL_YEAR=ssm.SCHOOL_YEAR AND sr.COURSE_ID='".$_REQUEST['request_course_id']."' AND sr.COURSE_CREDIT_TITLE='".$_REQUEST['request_course_credits']."') ";
						$_SYSTEM['SearchTerms'] .= '<font color=gray><b>'._('Doesn\'t have Request').': </b></font>'.$course[1]['TITLE'].'<BR>';
						// .' - '.$_REQUEST['request_course_credits']
					}
				}
				if(AllowUse('mod_curriculum/ScdStudReqProperties.php'))
					$addition['search'] .= "<TR><TD >"._('Requested Course')."</TD><TD><DIV id=\"weight_div\"></DIV> <A HREF=# onclick='window.open(\"modloader.php?servicemod=mod_portal/STRequestSelector.php&useontop=false\",\"\",\"scrollbars=yes,resizable=yes,width=800,height=400\");'><SMALL><B>"._('Choose')."</B></SMALL></A></TD></TR>";
			break;
	
			case 'absences':
				if(is_numeric($_REQUEST['absences_low']) && is_numeric($_REQUEST['absences_high']))
				{
					if($_REQUEST['absences_low'] > $_REQUEST['absences_high'])
					{
						$temp = $_REQUEST['absences_high'];
						$_REQUEST['absences_high'] = $_REQUEST['absences_low'];
						$_REQUEST['absences_low'] = $temp;
					}
		
					if($_REQUEST['month_days_abs_begin'] && $_REQUEST['day_days_abs_begin'] && $_REQUEST['year_days_abs_begin'])
					{
						while(!VerifyDate($_REQUEST['days_abs_begin'] = $_REQUEST['year_days_abs_begin'].'-'.$_REQUEST['month_days_abs_begin'].'-'.$_REQUEST['day_days_abs_begin']))
							$_REQUEST['month_days_abs_begin']--;
						while(!VerifyDate($_REQUEST['days_abs_end'] = $_REQUEST['year_days_abs_end'].'-'.$_REQUEST['month_days_abs_end'].'-'.$_REQUEST['day_days_abs_end']))
							$_REQUEST['month_days_abs_end']--;
					}
					$abs_days_extra = " AND ad.SCHOOL_DATE BETWEEN '".$_REQUEST['days_abs_begin']."' AND '".$_REQUEST['days_abs_end']."' ";

					if($_REQUEST['daily_abs_all']=='Y')
					{
					}
					elseif($_REQUEST['daily_abs_code'])
					{
						foreach($_REQUEST['daily_abs_code'] as $code=>$y)
						{
							$daily_abs_code_extra .= " OR DAILY_CODE='".$code."'";
						}
						$daily_abs_code_extra = ' AND ('.substr($daily_abs_code_extra,3).')';
					}

					if($_REQUEST['absences_term']!='FY')
						$mp_extra = " AND ad.SCHOOL_TERM_ID IN (".get_child_school_term($_REQUEST['absences_term'],get_user_school_term()).")";
					if($_REQUEST['absences_low']==$_REQUEST['absences_high'])
						$addition['WHERE'] .= " AND (SELECT COALESCE((SUM(STATE_VALUE-1)*-1),0.0) AS STATE_VALUE FROM STUDENT_ATTENDANCE_DAYS ad WHERE ssm.STUDENT_ID=ad.STUDENT_ID AND ad.SCHOOL_YEAR=ssm.SCHOOL_YEAR ".$mp_extra.$abs_days_extra.$daily_abs_code_extra.") = '$_REQUEST[absences_low]'";
					else
						$addition['WHERE'] .= " AND (SELECT COALESCE((SUM(STATE_VALUE-1)*-1),0.0) AS STATE_VALUE FROM STUDENT_ATTENDANCE_DAYS ad WHERE ssm.STUDENT_ID=ad.STUDENT_ID AND ad.SCHOOL_YEAR=ssm.SCHOOL_YEAR ".$mp_extra.$abs_days_extra.$daily_abs_code_extra.") BETWEEN '$_REQUEST[absences_low]' AND '$_REQUEST[absences_high]'";

					switch($_REQUEST['absences_term'])
					{
						case 'FY':
							//$term = _('this school year to date');
							$term = ProperDate($_REQUEST['days_abs_begin'])._(' through ').ProperDate($_REQUEST['days_abs_end']);
						break;
						case 'SEM':
							$term = _('this semester to date');
						break;
						case 'QTR':
							$term = _('this school term to date');
						break;
					}
					$_SYSTEM['SearchTerms'] .= '<font color=gray><b>'._('Days Absent ').$term._(' From').': </b></font>'.$_REQUEST['absences_low']._(' To ').$_REQUEST['absences_high'].'<BR>';
				}
				if(is_numeric($_REQUEST['att_low']) && is_numeric($_REQUEST['att_high']))
				{
					if($_REQUEST['att_low'] > $_REQUEST['att_high'])
					{
						$temp = $_REQUEST['att_high'];
						$_REQUEST['att_high'] = $_REQUEST['att_low'];
						$_REQUEST['att_low'] = $temp;
					}
					
					if($_REQUEST['att_term']!='FY')
						$mp_extra = " AND ap.SCHOOL_TERM_ID IN (".get_child_school_term($_REQUEST['att_term'],get_user_school_term()).")";
					if($_REQUEST['att_low']==$_REQUEST['att_high'])
						$addition['WHERE'] .= " AND (SELECT COUNT(*) AS COUNT FROM STUDENT_ATTENDANCE_PERIODS ap WHERE ssm.STUDENT_ID=ap.STUDENT_ID AND ap.ATTENDANCE_CODE='".$_REQUEST['att_code']."' ".$mp_extra.") = '$_REQUEST[att_low]'";
					else
						$addition['WHERE'] .= " AND (SELECT COUNT(*) AS COUNT FROM STUDENT_ATTENDANCE_PERIODS ap WHERE ssm.STUDENT_ID=ap.STUDENT_ID AND ap.ATTENDANCE_CODE='".$_REQUEST['att_code']."' ".$mp_extra.") BETWEEN '$_REQUEST[att_low]' AND '$_REQUEST[att_high]'";
	
					switch($_REQUEST['att_term'])
					{
						case 'FY':
							$term = _('this school year to date');
						break;
						case 'SEM':
							$term = _('this semester to date');
						break;
						case 'QTR':
							$term = _('this school term to date');
						break;
					}
					$code_array = db_request(db_execute("SELECT TITLE FROM ATTENDANCE_CODES_SETTINGS WHERE ID='".$_REQUEST['att_code']."'"));
					$_SYSTEM['SearchTerms'] .= '<font color=gray><b>'._('Periods ').$code_array[1]['TITLE'].' '.$term._(' From').': </b></font>'.$_REQUEST['att_low']._(' To ').$_REQUEST['att_high'].'<BR>';
				}
				
				if(AllowUse('mod_attendance/AttdStuSumAttendance.php') || AllowUse('mod_attendance/AttdByDays.php'))
				{
					if($_REQUEST['search_servicefunction']=='search' || !$_REQUEST['search_servicefunction'] || $_SYSTEM['modules_search'])
					{
						$codes_array = db_request(db_execute("SELECT ID,TITLE,SHORT_NAME,IS_DAILY_CODE FROM ATTENDANCE_CODES_SETTINGS WHERE SCHOOL_YEAR='".get_school_year()."' AND SCHOOL_ID='".get_school_id()."'"));
						foreach($codes_array as $code)
							$code_select .= '<OPTION value='.$code['ID'].'>'.$code['TITLE'].'</option>';
					}

					/*$addition['search'] .= "<TR><TD colspan=2><div class='divdotted'></div> </td></tr>";*/
					$addition['search'] .= "<TR><TD align=center colspan=2><fieldset><legend>"._('Daily Absences').'</legend> ';
					if(is_countable($codes_array))
					{
						$addition['search'] .= '<INPUT type=radio name=daily_abs_all id=daily_abs_all onclick="checkAll(this.form,false,\'daily_abs_code\');" value=Y CHECKED><label for=daily_abs_all>'._('All').'</label> ';
						foreach($codes_array as $code)
						{
							if($code['IS_DAILY_CODE']=='Y')
								$addition['search'] .= '<INPUT type=checkbox name=daily_abs_code['.$code['SHORT_NAME'].'] onclick="if(this.checked==true) document.getElementById(\'daily_abs_all\').checked=false;" id="daily_abs_code['.$code['SHORT_NAME'].']" ><label for="daily_abs_code['.$code['SHORT_NAME'].']">'.$code['SHORT_NAME'].'</label> ';
						}
					}
					$abs_PD_options = array('short'=>true,'C'=>false);
					$addition['search'] .= '<font color=gray>'._('Timeframe').':</font> '.PrepareDate(date('Y-m-').'01','_days_abs_begin',null,$abs_PD_options).' - '.PrepareDate(get_db_date('postgres'),'_days_abs_end',null,$abs_PD_options).'</fieldset></TD></TR>';
					//$addition['search'] .= '<BR><INPUT type=radio name=absences_term value=FY checked><small>'._('YTD')."</small><INPUT type=radio name=absences_term value=SEM><small>".get_school_term(get_parent_school_term('SEM',get_user_school_term()),'SHORT_NAME')."</small><INPUT type=radio name=absences_term value=QTR><small>".get_school_term(get_user_school_term(),'SHORT_NAME')."</small>";
					$addition['search'] .= '<INPUT type=hidden name=absences_term value=FY></TD></TR>';
					$addition['search'] .= "<TR ><TD align=left colspan=2>"._('Amount of Days Absent from')." <INPUT type=text name=absences_low size=3 maxlength=5> "._('to')." <INPUT type=text name=absences_high size=3 maxlength=5></TD></TR>";
//echo get_user_school_term()."->".get_school_term(get_parent_school_term('SEM',get_user_school_term()),'SHORT_NAME');
//echo "|".get_parent_school_term('SEM',get_user_school_term());
					$addition['search'] .= "<TR><TD align=".ALIGN_LEFT."><b>"._('Select Attendance Reason').'</b><select name=att_code>'.$code_select.'</select>'.'<INPUT type=radio name=att_term value=FY checked>'._('FY').
							    "<INPUT type=radio name=att_term value=SEM>".get_school_term(get_parent_school_term('SEM',get_user_school_term()),'SHORT_NAME')."<INPUT type=radio name=att_term value=QTR>".get_school_term(get_user_school_term(),'SHORT_NAME')."</TD></TR><TR><TD><B>"._('Type School Periods')."</B>"._(' From ').
							    " <INPUT type=text name=att_low size=3 maxlength=5> "._('to')." <INPUT type=text name=att_high size=3 maxlength=5></TD></TR>";
				}
			break;
			
			case 'average_grade':
				if(is_numeric($_REQUEST['avg_grade_low']) && is_numeric($_REQUEST['avg_grade_high']))
				{
					if($_REQUEST['avg_grade_low'] > $_REQUEST['avg_grade_high'])
					{
						$temp = $_REQUEST['avg_grade_high'];
						$_REQUEST['avg_grade_high'] = $_REQUEST['avg_grade_low'];
						$_REQUEST['avg_grade_low'] = $temp;
					}
			
					$addition['WHERE'] = " AND (SELECT AVG(srcg.PERCENT_GRADE) FROM STUDENT_GRADES_CARD srcg WHERE srcg.STUDENT_ID=ssm.STUDENT_ID AND srcg.SCHOOL_TERM_ID='$_REQUEST[avg_grade_term]' AND srcg.PERCENT_GRADE!='-1') BETWEEN '$_REQUEST[avg_grade_low]' AND '$_REQUEST[avg_grade_high]' ";
			
					$_SYSTEM['SearchTerms'] .= '<font color=gray><b> '._('Average Grade').' '.get_school_term($_REQUEST['avg_grade_term']).' '._(' From:').' </b></font>'.$_REQUEST['avg_grade_low']._(' to ').$_REQUEST['avg_grade_high'].'<BR>';
				}

				if(AllowUse('mod_grades/GrdPrintStudGrades.php') || AllowUse('mod_grades/GrdPrintStudentReportCards.php'))
				{
					$qtr = get_user_school_term();
					$sem = get_parent_school_term('SEM',get_user_school_term());
					$fy = get_parent_school_term('FY',$sem);

					if(get_school_term($fy,'POST_START_DATE') || get_school_term($sem,'POST_START_DATE') || get_school_term($qtr,'POST_START_DATE'))
					{
						$addition['search'] .= "<TR><TD colspan=2><fieldset><legend><B>"._('Average Percent Grade')."</B></legend><table width=100%><TR><td>";
						$selected = false;
						if(get_school_term($fy,'POST_START_DATE'))
						{
							$addition['search'] .= '<INPUT type=radio name=avg_grade_term value='.$fy.' CHECKED>'.get_school_term($fy,'SHORT_NAME').'';
							$selected = true;
						}
						if(get_school_term($sem,'POST_START_DATE'))
						{
							$addition['search'] .= '<INPUT type=radio name=avg_grade_term value='.$sem.(!$selected?' CHECKED':'').'>'.get_school_term($sem,'SHORT_NAME').'';
							$selected = true;
						}
						if(get_school_term($qtr,'POST_START_DATE'))
						{
							$addition['search'] .= '<INPUT type=radio name=avg_grade_term value='.$qtr.(!$selected?' CHECKED':'').'>'.get_school_term($qtr,'SHORT_NAME').'';
							$selected = true;
						}

						$addition['search'] .= "</TD><TD align=right>"._('From')." <INPUT type=text name=avg_grade_low size=3 maxlength=5> "._('to')." <INPUT type=text name=avg_grade_high size=3 maxlength=5></TD></TR></table></fielset></TD></TR>";
					}
				}
			break;
	
			case 'gpa':
				if(is_numeric($_REQUEST['gpa_low']) && is_numeric($_REQUEST['gpa_high']))
				{
					if($_REQUEST['gpa_low'] > $_REQUEST['gpa_high'])
					{
						$temp = $_REQUEST['gpa_high'];
						$_REQUEST['gpa_high'] = $_REQUEST['gpa_low'];
						$_REQUEST['gpa_low'] = $temp;
					}
					if($_REQUEST['list_gpa'])
					{
						$addition['SELECT'] .= ',sgc.CUMULATIVE_WEIGHTED_GPA,sgc.CUMULATIVE_GPA';
						$addition['columns_after']['WEIGHTED_GPA'] = _('Weighted GPA');
						$addition['columns_after']['GPA'] = _('GPA');
					}
					if(strpos($addition['FROM'],'STUDENT_GPA_TOTALS sgc')===false)
					{
						$addition['FROM'] .= ",STUDENT_GPA_TOTALS sgc";
						$addition['WHERE'] .= " AND sgc.STUDENT_ID=s.STUDENT_ID AND sgc.SCHOOL_TERM_ID='".$_REQUEST['gpa_term']."'";
					}
					$addition['WHERE'] .= " AND sgc.".(($_REQUEST['weighted']=='Y')?'WEIGHTED_':'')."GPA BETWEEN '$_REQUEST[gpa_low]' AND '$_REQUEST[gpa_high]' AND sgc.SCHOOL_TERM_ID='".$_REQUEST['gpa_term']."'";
					$_SYSTEM['SearchTerms'] .= '<font color=gray><b>'.(($_REQUEST['gpa_weighted']=='Y')?_('Weighted').' ':'')._('GPA From:').' </b></font>'.$_REQUEST['gpa_low']._(' to ').$_REQUEST['gpa_high'].'<BR>';
				}
				if(AllowUse('mod_grades/GrdPrintStudGrades.php') || AllowUse('mod_grades/GrdPrintStudentReportCards.php') || AllowUse('mod_grades/GrdStudentPrintGC.php'))
				{
/*					$addition['search'] .= "<TR><TD colspan=2><div class='divdotted'></div> </td></tr>";*/
				        $addition['search'] .= "<TR><TD colspan=2><fieldset><legend><b>"._('GPA').'</b></legend><table width=100%><TR><td>';
					if(SystemPreferences('WEIGHTED_GPA')=='Y')
						$addition['search'] .= "<INPUT type=checkbox name=gpa_weighted value=Y>"._('Weighted')."";
					$addition['search'] .= "<INPUT type=radio name=gpa_term value=".get_parent_school_term('SEM',get_user_school_term())." CHECKED>".get_school_term(get_parent_school_term('SEM',get_user_school_term()),'SHORT_NAME')."<INPUT type=radio name=gpa_term value=".get_user_school_term().">".get_school_term(get_user_school_term(),'SHORT_NAME').
							    "</TD><TD align=right>  "._('From')." <INPUT type=text name=gpa_low size=3 maxlength=5> "._('to')." <INPUT type=text name=gpa_high size=3 maxlength=5></TD></TR></table></fielset></TD></TR>";
				}
			break;
	
			case 'class_rank':
				if(is_numeric($_REQUEST['class_rank_low']) && is_numeric($_REQUEST['class_rank_high']))
				{
					if($_REQUEST['class_rank_low'] > $_REQUEST['class_rank_high'])
					{
						$temp = $_REQUEST['class_rank_high'];
						$_REQUEST['class_rank_high'] = $_REQUEST['class_rank_low'];
						$_REQUEST['class_rank_low'] = $temp;
					}
					if(strpos($addition['FROM'],'STUDENT_GPA_TOTALS sgc')===false)
					{
						$addition['FROM'] .= ",STUDENT_GPA_TOTALS sgc";
						$addition['WHERE'] .= " AND sgc.STUDENT_ID=s.STUDENT_ID AND sgc.SCHOOL_TERM_ID='".$_REQUEST['class_rank_term']."'";
					}
					$addition['WHERE'] .= " AND sgc.CLASS_RANK BETWEEN '$_REQUEST[class_rank_low]' AND '$_REQUEST[class_rank_high]'";
					$_SYSTEM['SearchTerms'] .= '<font color=gray><b>'._('Student Rating in Class From:').' </b></font>'.$_REQUEST['class_rank_low']._(' to ').$_REQUEST['class_rank_high'].'<BR>';
				}
				if(AllowUse('mod_grades/GrdPrintStudGrades.php') || AllowUse('mod_grades/GrdPrintStudentReportCards.php') || AllowUse('mod_grades/GrdStudentPrintGC.php'))
				{
					$addition['search'] .= "<TR><TD colspan=2><fieldset><legend><B>"._('Student Rating in Class')."</B></legend><table width=100%><TR><td><INPUT type=radio name=class_rank_term value=".get_parent_school_term('SEM',get_user_school_term())." CHECKED>".get_school_term(get_parent_school_term('SEM',get_user_school_term()),'SHORT_NAME').
							    "<INPUT type=radio name=class_rank_term value=".get_user_school_term().">".get_school_term(get_user_school_term(),'SHORT_NAME')."";
					if(strlen($pros = get_child_school_term('PRO',get_user_school_term())))
					{
						$pros = explode(',',str_replace("'",'',$pros));
						foreach($pros as $pro)
							$addition['search'] .= "<INPUT type=radio name=class_rank_term value=".$pro.">".get_school_term($pro,'SHORT_NAME')."";
					}
					$addition['search'] .= "</TD><TD align=right>"._('From')." <INPUT type=text name=class_rank_low size=3 maxlength=5> "._('to')." <INPUT type=text name=class_rank_high size=3 maxlength=5></TD></TR></table></fielset></TD></TR>";
				}
			break;
			
			case 'letter_grade':
				if(is_array($_REQUEST['letter_grade']))
				{
					$_SYSTEM['SearchTerms'] .= '<font color=gray><b>'.($_REQUEST['letter_grade_exclude']=='Y'?_('Without'):_('With')).' '._('Report Card Grade in ').get_school_term($_REQUEST['letter_grade_term']).': </b></font>';
					foreach($_REQUEST['letter_grade'] as $grade=>$Y)
					{
						$letter_grades .= ",'$grade'";
						$_SYSTEM['SearchTerms'] .= $grade.', ';
					}
					$_SYSTEM['SearchTerms'] = substr($_SYSTEM['SearchTerms'],0,-2);
					
					if($_REQUEST['letter_grade_exclude']!='Y')
						$_SYSTEM['SearchTerms'] .= ' &nbsp; '.CheckboxOnClick('letter_grade_show_courses',_('Show Courses'));
					if($_REQUEST['letter_grade_show_courses'])
					{
						$addition['SELECT'] .= ',srcg_lg.COURSE_TITLE,srcg_lg.GRADE_TITLE';
						$addition['columns_before']['COURSE_TITLE'] = _('Course');
						$addition['columns_before']['GRADE_TITLE'] = _('Grade');
						$addition['FROM'] .= ',STUDENT_GRADES_CARD srcg_lg ';
						$addition['WHERE'] .= " AND srcg_lg.STUDENT_ID=ssm.STUDENT_ID AND srcg_lg.SCHOOL_YEAR=ssm.SCHOOL_YEAR AND srcg_lg.GRADE_TITLE IN (".substr($letter_grades,1).") AND srcg_lg.SCHOOL_TERM_ID='".$_REQUEST['letter_grade_term']."' ";
						$addition['singular'] = _('Course');
						$addition['plural'] = _('Courses');
					}
					else
						$addition['WHERE'] .= " AND ".($_REQUEST['letter_grade_exclude']=='Y'?'NOT ':'')."EXISTS (SELECT '' FROM STUDENT_GRADES_CARD sg3 WHERE sg3.STUDENT_ID=ssm.STUDENT_ID AND sg3.SCHOOL_YEAR=ssm.SCHOOL_YEAR AND sg3.GRADE_TITLE IN (".substr($letter_grades,1).") AND sg3.SCHOOL_TERM_ID='".$_REQUEST['letter_grade_term']."' )";
			///		$_SYSTEM['SearchTerms'] .= '<BR>';
				}
				if(AllowUse('mod_grades/GrdPrintStudGrades.php') || AllowUse('mod_grades/GrdPrintStudentReportCards.php') || AllowUse('mod_grades/GrdStudentPrintGC.php'))
				{
				/*	$addition['search'] .= "<TR><TD colspan=2><div class='divdotted'></div> </td></tr>";*/
/*�������� ������*/                     $addition['search'] .= "<TR ><TD style='width: 40%;' align=center><B>"._('Grade')."</B></TD><TD style='width: 100%;'>"._('Exclude Grades')."<INPUT type=checkbox name=letter_grade_exclude id=letter_grade_exclude value=Y><label for='letter_grade_exclude'> "."</label></TD></TR><TR><TD colspan=2>";

					$sem = get_parent_school_term('SEM',get_user_school_term());
					$fy = get_parent_school_term('FY',$sem);
					$checked = ' CHECKED';
					if(get_school_term($fy,'POST_START_DATE'))
					{
						$addition['search'] .= "<label for='letter_grade_term".$fy."' style='white-space:nowrap;'><INPUT type=radio name=letter_grade_term id='letter_grade_term".$fy."' value=".$fy.$checked.">".get_school_term($fy,'SHORT_NAME')."</label> ";
						$checked = '';
						if(SystemPreferences('FY_EXAMS')=='Y')
							$addition['search'] .= "<label for='letter_grade_termE".$fy."' style='white-space:nowrap;'><INPUT type=radio name=letter_grade_term id='letter_grade_termE".$fy."' value=E".$fy.">".get_school_term('E'.$fy,'SHORT_NAME')."</label> ";
					}
					if(get_school_term($sem,'POST_START_DATE'))
					{
						$addition['search'] .= "<label for='letter_grade_term".$sem."' style='white-space:nowrap;'><INPUT type=radio name=letter_grade_term id='letter_grade_term".$sem."' value=".$sem.$checked.">".get_school_term($sem,'SHORT_NAME')."</label> ";
						$checked = '';
						if(SystemPreferences('FINAL_EXAMS')=='Y')
							$addition['search'] .= "<label for='letter_grade_termE".$sem."' style='white-space:nowrap;'><INPUT type=radio name=letter_grade_term id='letter_grade_termE".$sem."' value=E".$sem.">".get_school_term('E'.$sem,'SHORT_NAME')."</label> ";
					}
					if(get_school_term(get_user_school_term(),'POST_START_DATE'))
					{
						$addition['search'] .= "<label for='letter_grade_term".get_user_school_term()."' style='white-space:nowrap;'><INPUT type=radio name=letter_grade_term id='letter_grade_term".get_user_school_term()."' value=".get_user_school_term().$checked.">".get_school_term(get_user_school_term(),'SHORT_NAME')." ";
						$checked = '';
						if(SystemPreferences('QTR_EXAMS')=='Y')
							$addition['search'] .= "<label for='letter_grade_termE".get_user_school_term()."' style='white-space:nowrap;'><INPUT type=radio name=letter_grade_term id='letter_grade_termE".get_user_school_term()."' value=E".get_user_school_term().">".get_school_term('E'.get_user_school_term(),'SHORT_NAME')."</label> ";
					}

					if(strlen($pros = get_child_school_term('PRO',get_user_school_term())))
					{	
						$pros = explode(',',str_replace("'",'',$pros));
						foreach($pros as $pro)
							$addition['search'] .= "<label for='letter_grade_term".$pro."' style='white-space:nowrap;'><INPUT type=radio name=letter_grade_term id='letter_grade_term".$pro."' value=".$pro.">".get_school_term($pro,'SHORT_NAME')."</label> ";
					}
					$addition['search'] .= "</TD></TR><TR><TD colspan=2>";
					if($_REQUEST['search_servicefunction']=='search' || !$_REQUEST['search_servicefunction'] || $_SYSTEM['modules_search'])
						$letter_grades_array = db_request(db_execute("SELECT TITLE FROM GRADES_CARD_SETTINGS WHERE SCHOOL_ID='".get_school_id()."' AND SCHOOL_YEAR='".get_school_year()."' ORDER BY SCALE_ID,SORT_ORDER"));
					$i = 0;
					if(is_array($letter_grades_array))
					{
						foreach($letter_grades_array as $grade)
						{
							if($used_grades[$grade['TITLE']])
								continue;

					//		if($i%9==0)
					//			$addition['search'] .= '<BR>';
						
							$addition['search'] .= '<INPUT type=checkbox value=Y name=letter_grade['.$grade['TITLE'].']>'.$grade['TITLE'];
							$i++;
							$used_grades[$grade['TITLE']] = true;
						}
					}
					$addition['search'] .= '</TD></TR>';
				}
			break;
	
			case 'suitability':
				if($_REQUEST['unsuitable']=='Y')
				{
					$start_end_array = db_request(db_execute("SELECT TITLE,VALUE FROM SYSTEM_CONFIG WHERE SCHOOL_YEAR='".get_school_year()."' AND SCHOOL_ID='".get_school_id()."' AND PROGRAM='activity' AND TITLE IN ('START_DAY','END_DAY')"));
					if(count($start_end_array))
					{
						foreach($start_end_array as $value)
							$value['TITLE'] = $value['VALUE'];
					}
					
					switch(date('D'))
					{
						case 'Mon':
						$today = 1;
						break;
						case 'Tue':
						$today = 2;
						break;
						case 'Wed':
						$today = 3;
						break;
						case 'Thu':
						$today = 4;
						break;
						case 'Fri':
						$today = 5;
						break;
						case 'Sat':
						$today = 6;
						break;
						case 'Sun':
						$today = 7;
						break;
					}
	
					$start_date = strtoupper(date('d-M-y',time() - ($today-$START_DAY)*60*60*24));
					$end_date = strtoupper(date('d-M-y',time()));
					$addition['FROM'] .= ",STUDENT_REGISTRATION ssm";
					$addition['WHERE'] .= " AND (SELECT count(*) FROM STUDENT_SUITABILITY_GRADES e WHERE ssm.STUDENT_ID=e.STUDENT_ID AND e.SCHOOL_YEAR=ssm.SCHOOL_YEAR AND e.SCHOOL_DATE BETWEEN '$start_date' AND '$end_date' AND e.STUDENT_SUITABILITY_GRADES_CODE='FAILING') > '0'";
					$_SYSTEM['SearchTerms'] .= '<font color=gray><b>'._('Eligibility:').' </b></font>'._('Uneligible').'<BR>';
				}
//				echo "----".AllowUse('mod_evaluation/SuitIntermediateReport.php');
	//			if(AllowUse('mod_evaluation/SuitIntermediateReport.php'))
				if(AllowUse('mod_evaluation/SuitStudReport.php'))
					$addition['search'] .= "<TR><TD align=".ALIGN_RIGHT." width=150>"._('Uneligible')."</TD><TD><INPUT type=checkbox name=unsuitable value='Y'></TD></TR>";
			break;
			
			case 'activity':
				if($_REQUEST['activity_id'])
				{
					$addition['FROM'] .= ",STUDENT_ACTIVITIES_HISTORY sea";
					$addition['WHERE'] .= " AND sea.STUDENT_ID=s.STUDENT_ID AND sea.SCHOOL_YEAR=ssm.SCHOOL_YEAR AND sea.ACTIVITY_ID='".$_REQUEST['activity_id']."'";
					$activity = db_request(db_execute("SELECT TITLE FROM ACTIVITY_COURSES WHERE ID='".$_REQUEST['activity_id']."'"));
					$_SYSTEM['SearchTerms'] .= '<font color=gray><b>'._('Activity:').' </b></font>'.$activity[1]['TITLE'].'<BR>';
				}
				if($_REQUEST['search_servicefunction']=='search' || !$_REQUEST['search_servicefunction'] || $_REQUEST['search_servicefunction']=='list')
					$activities_array = db_request(db_execute("SELECT ID,TITLE FROM ACTIVITY_COURSES WHERE SCHOOL_ID='".get_school_id()."' AND SCHOOL_YEAR='".get_school_year()."'"));
				$select = "<SELECT name=activity_id><OPTION value=''>"._('N/A')."</OPTION>";

				if($activities_array && count($activities_array))
				{
					foreach($activities_array as $activity)
						$select .= "<OPTION value=$activity[ID]>$activity[TITLE]</OPTION>";
				}
				$select .= '</SELECT>';
				if(AllowUse('mod_activity/ActStudMain.php') || AllowUse('mod_activity/ActStudAdd.php'))
				$addition['search'] .= "<TR ><TD>"._('Select Activity Course')."</TD><TD>".$select."</TD></TR>";
			break;		
			
			case 'mailing_labels':
				if($_REQUEST['mailing_labels']=='Y')
				{
					$addition['SELECT'] .= ',sam.ADDRESS_ID,sam.ADDRESS_ID AS MAILING_LABEL';
					$addition['FROM'] = " LEFT OUTER JOIN STUDENTS_IN_ADDRESS sam ON (sam.STUDENT_ID=ssm.STUDENT_ID AND sam.MAILING='Y') ".$addition['FROM'];
					$addition['functions'] += array('MAILING_LABEL'=>'MailingLabel');
				}
				
				$addition['second_col'] .= '<TABLE width=100%><TR><TD><INPUT type=checkbox name=mailing_labels value=Y id=mailing_labels_checkbox><label for="mailing_labels_checkbox">'._('Mailing Labels').'</label></TD></TR></TABLE>';
			break;
			case 'line':				
				$addition['second_col'] .= '<TABLE width=100%><TR><TD><HR></TD></TR></TABLE>';
			break;
			case 'document_template':
				if(get_user('PROFILE')!='admin')
				{
					$default_templates_array = db_request(db_execute("SELECT ID,TITLE,DEFAULT_PROGRAM_FOR_TEACHERS AS DEFAULT_PROGRAM FROM LH_TEMPLATES WHERE (SCHOOL_ID='".get_school_id()."' OR SCHOOL_ID='0') AND DEFAULT_PROGRAM_FOR_TEACHERS='".$_REQUEST['servicemod']."' ORDER BY TITLE"));
					if($default_templates_array)
						$addition['second_col'] .= '<INPUT type=hidden name="_template_id" value="'.$default_templates_array[1]['ID'].'">';
				}

				if(get_user('PROFILE')=='admin' || !$default_templates_array)
				{
					$templates_array = db_request(db_execute("SELECT ID,TITLE,DEFAULT_PROGRAM FROM LH_TEMPLATES WHERE (SCHOOL_ID='".get_school_id()."' OR SCHOOL_ID='0') ORDER BY TITLE"));
					foreach($templates_array as $template)
					$options .= "<OPTION value=$template[ID]".($template['DEFAULT_PROGRAM']==$_REQUEST['servicemod']?' SELECTED':'').">".$template['TITLE'].'</OPTION>';
					$addition['second_col'] .= '<TABLE style="display:inline;"><TR><TD align='.ALIGN_LEFT.' width=150>'._('Letterhead Template').' </TD><TD><SELECT name=_template_id><OPTION value="">'._('None').'</OPTION>'.$options.'</SELECT></TD></TR></TABLE>';
				}
			break;
			
			case 'balance':
				if(is_numeric($_REQUEST['balance_low']) && is_numeric($_REQUEST['balance_high']))
				{
					if($_REQUEST['balance_low'] > $_REQUEST['balance_high'])
					{
						$temp = $_REQUEST['balance_high'];
						$_REQUEST['balance_high'] = $_REQUEST['balance_low'];
						$_REQUEST['balance_low'] = $temp;
					}
					$addition['WHERE'] .= " AND (COALESCE((SELECT SUM(f.AMOUNT) FROM BILLING_STUDENT_FEES f WHERE f.STUDENT_ID=ssm.STUDENT_ID AND f.SCHOOL_YEAR=ssm.SCHOOL_YEAR),0)-COALESCE((SELECT SUM(p.AMOUNT) FROM BILLING_STUDENT_PAYMENTS p WHERE p.STUDENT_ID=ssm.STUDENT_ID AND p.SCHOOL_YEAR=ssm.SCHOOL_YEAR),0)) BETWEEN '$_REQUEST[balance_low]' AND '$_REQUEST[balance_high]' ";
				}
				if(AllowUse('mod_billing/BilStudBalanceList.php') || AllowUse('mod_billing/BilStudFeesList.php') || AllowUse('mod_billing/BilStudPaysList.php'))
					$addition['search'] .= "<TR><TD align=".ALIGN_RIGHT." width=40%>"._('Billing Balance')."<BR></TD><TD >"._('From')." <INPUT type=text name=balance_low size=5 maxlength=10> "._('to')." <INPUT type=text name=balance_high size=5 maxlength=10></TD></TR>";
			break;
			
			case 'discipline':
				if(is_numeric($_REQUEST['referrals_low']) && is_numeric($_REQUEST['referrals_high']))
				{
					if($_REQUEST['referrals_low'] > $_REQUEST['referrals_high'])
					{
						$temp = $_REQUEST['referrals_low'];
						$_REQUEST['referrals_low'] = $_REQUEST['referrals_high'];
						$_REQUEST['referrals_low'] = $temp;
					}
		
					if($_REQUEST['referrals_low']==$_REQUEST['referrals_high'])
						$addition['WHERE'] .= " AND (SELECT COUNT(*) AS COUNT FROM STUDENT_DISCIPLINE_REPORTS d WHERE ssm.STUDENT_ID=d.STUDENT_ID AND d.SCHOOL_YEAR=ssm.SCHOOL_YEAR) = '$_REQUEST[referrals_low]'";
					else
						$addition['WHERE'] .= " AND (SELECT COUNT(*) AS COUNT FROM STUDENT_DISCIPLINE_REPORTS d WHERE ssm.STUDENT_ID=d.STUDENT_ID AND d.SCHOOL_YEAR=ssm.SCHOOL_YEAR) BETWEEN '$_REQUEST[referrals_low]' AND '$_REQUEST[referrals_high]'";

					$_SYSTEM['SearchTerms'] .= '<font color=gray><b>'._('Number of Incident Report received ')._(' from').': </b></font>'.$_REQUEST['referrals_low']._(' to ').$_REQUEST['referrals_high'].'<BR>';
				}
                               	$addition['search'] .= "<TR><TD colspan=2><div class='divdotted'></div> </td></tr>";
			        $addition['search'] .= "<TR><TD colspan=2 align=center><fieldset><legend>"._('Number of Incident Report recieved')."</legend><font color=gray>"._('From')." <INPUT type=text name=referrals_low size=3 maxlength=5> "._('to')." <INPUT type=text name=referrals_high size=3 maxlength=5><font color=gray></fieldset></TD></TR>";

				if(is_array($_REQUEST['discipline']))
				{
					foreach($_REQUEST['discipline'] as $key=>$value)
					{
						if(!$value)
							unset($_REQUEST['discipline'][$key]);
					}
				}
				if($_REQUEST['month_discipline_entry_begin'] && $_REQUEST['day_discipline_entry_begin'] && $_REQUEST['year_discipline_entry_begin'])
				{
					$_REQUEST['discipline_entry_begin'] = $_REQUEST['year_discipline_entry_begin'].'-'.$_REQUEST['month_discipline_entry_begin'].'-'.$_REQUEST['day_discipline_entry_begin'];
					if(!VerifyDate($_REQUEST['discipline_entry_begin']))
						unset($_REQUEST['discipline_entry_begin']);
					$_REQUEST['discipline_entry_end'] = $_REQUEST['year_discipline_entry_end'].'-'.$_REQUEST['month_discipline_entry_end'].'-'.$_REQUEST['day_discipline_entry_end'];
					if(!VerifyDate($_REQUEST['discipline_entry_end']))
						unset($_REQUEST['discipline_entry_end']);
				}
				if($_REQUEST['discipline_begin'] && $_REQUEST['discipline_end'])
				{
					foreach($_REQUEST['discipline_begin'] as $key=>$begin)
					{
						if($begin > $_REQUEST['discipline_end'][$key])
						{
							$temp = $_REQUEST['discipline_begin'][$key];
							$_REQUEST['discipline_begin'][$key] = $_REQUEST['discipline_end'][$key];
							$_REQUEST['discipline_end'][$key] = $temp;
						}
					}
				}
				if($_REQUEST['discipline_reporter'] || ($_REQUEST['discipline_entry_begin'] && $_REQUEST['discipline_entry_end']) || (!empty($_REQUEST['discipline'])))
				{
					$addition['WHERE'] .= ' AND dr.STUDENT_ID=ssm.STUDENT_ID AND dr.SCHOOL_YEAR=ssm.SCHOOL_YEAR AND dr.SCHOOL_ID=ssm.SCHOOL_ID ';
					$addition['FROM'] .= ',STUDENT_DISCIPLINE_REPORTS dr ';			
					if($_REQUEST['discipline_reporter'])
					{
						$addition['WHERE'] .= " AND dr.USER_ID='$_REQUEST[discipline_reporter]' ";
						$_SYSTEM['SearchTerms'] .= '<font color=gray><b>'._('Discipline Reporter').': </b></font>'.GetTeacher($_REQUEST['discipline_reporter']).'<BR>';
					}
					if($_REQUEST['discipline_entry_begin'] && $_REQUEST['discipline_entry_end'])
					{
						$addition['WHERE'] .= " AND dr.ENTRY_DATE BETWEEN '$_REQUEST[discipline_entry_begin]' AND '$_REQUEST[discipline_entry_end]' ";
						$_SYSTEM['SearchTerms'] .= '<font color=gray><b>'._('Submited Incident Report Date timeframe').': </b></font>'.ProperDate($_REQUEST['discipline_entry_begin']).'  '.ProperDate($_REQUEST['discipline_entry_end']).'<BR>';
					}
				}
				if($_REQUEST['search_servicefunction']=='search' || !$_REQUEST['search_servicefunction'])
				{
					$addition['search'] .= '<TR ><TD width=40% align=right>'._('Reporter').'</TD><TD>';
					$users_array = db_request(db_execute("SELECT USER_ID,FIRST_NAME,LAST_NAME,MIDDLE_NAME FROM USERS WHERE SCHOOLS LIKE '%,".get_school_id().",%' AND PROFILE IN ('admin','teacher') ORDER BY LAST_NAME,FIRST_NAME,MIDDLE_NAME"));
					$addition['search'] .= '<SELECT name=discipline_reporter><OPTION value="">'._('N/A').'</OPTION>';
					foreach($users_array as $user)
						$addition['search'] .= '<OPTION value='.$user['USER_ID'].'>'.$user['LAST_NAME'].', '.$user['FIRST_NAME'].' '.$user['MIDDLE_NAME'].'</OPTION>';
					$addition['search'] .= '</SELECT>';
					$addition['search'] .= '</TD></TR>';
	//$addition['search'] .= '<TR ><TD colspan=2 width=370 align=center><fieldset><legend>'._('Incident Date').'</legend><font color=gray>'._('Timeframe ends').' '.PrepareDate('','_discipline_entry_end',true,array('short'=>true))."</font></fieldset></TD></TR>";				
                $addition['search'] .= '<TR ><TD colspan=2 width=370 align=center><fieldset><legend>'._('Incident Date').'</legend><font color=gray>'._('Timeframe begins').' '.PrepareDate('','_discipline_entry_begin',true,array('short'=>true)).'<br>'._('Timeframe ends').' '.PrepareDate('','_discipline_entry_end',true,array('short'=>true))."</font></fieldset></TD></TR>";
				} 
				$categories_array = db_request(db_execute("SELECT f.ID,u.TITLE,f.DATA_TYPE,u.SELECT_OPTIONS FROM DISCIPLINE_REPORT_FIELDS f,DISCIPLINE_CUSTOM_FIELDS_ASG u WHERE u.DISCIPLINE_FIELD_ID=f.ID AND u.SCHOOL_YEAR='".get_school_year()."' AND u.SCHOOL_ID='".get_school_id()."' AND f.DATA_TYPE!='textarea'"));
				foreach($categories_array as $category)
				{
					if($category['DATA_TYPE']!='date')
					{

						$addition['search'] .= '<TR><TD align='.ALIGN_LEFT.' width=40%>'.$category['TITLE'].'</TD><TD>';
						switch($category['DATA_TYPE'])
						{
							case 'text':
								$addition['search'] .= '<INPUT type=text name=discipline['.$category['ID'].']>';
								if($_REQUEST['discipline'][$cateogory['ID']])
								{
									$addition['WHERE'] .= " AND dr.CATEGORY_".$category['ID']." LIKE '".$_REQUEST['discipline'][$cateogory['ID']]."%' ";
									$_SYSTEM['SearchTerms'] .= '<font color=gray><b>'.$category['TITLE'].': </b></font>'.$_REQUEST['discipline'][$cateogory['ID']].'<BR>';
								}
							break;
							case 'checkbox':
								$addition['search'] .= '<INPUT type=checkbox name=discipline['.$category['ID'].'] value=Y>';
								if($_REQUEST['discipline'][$cateogory['ID']])
								{
									$addition['WHERE'] .= " AND dr.CATEGORY_".$category['ID']." = 'Y' ";
									$_SYSTEM['SearchTerms'] .= '<font color=gray><b>'.$category['TITLE'].': </b></font>'.$_REQUEST['discipline'][$cateogory['ID']].'<BR>';
								}
							break;
							case 'numeric':
								$addition['search'] .= '<small>'._('Between').' </small><INPUT type=text name=discipline_begin['.$category['ID'].'] size=3 maxlength=11> & <INPUT type=text name=discipline_end['.$category['ID'].'] size=3 maxlength=11>';
								if($_REQUEST['discipline_begin'][$cateogory['ID']] && $_REQUEST['discipline_begin'][$cateogory['ID']])
								{
									$addition['WHERE'] .= " AND dr.CATEGORY_".$category['ID']." BETWEEN '".$_REQUEST['discipline_begin'][$cateogory['ID']]."' AND '".$_REQUEST['discipline_end'][$cateogory['ID']]."' ";
									$_SYSTEM['SearchTerms'] .= '<font color=gray><b>'.$category['TITLE']._(' between').': </b></font>'.$_REQUEST['discipline_begin'][$cateogory['ID']].' &amp; '.$_REQUEST['discipline_end'][$cateogory['ID']].'<BR>';
								}
							break;
							case 'multiple_checkbox':
							case 'multiple_radio':
							case 'select':
								$category['SELECT_OPTIONS'] = str_replace("\n","\r",str_replace("\r\n","\r",$category['SELECT_OPTIONS']));
								$category['SELECT_OPTIONS'] = explode("\r",$category['SELECT_OPTIONS']);
								//echo "<pre>";print_r($category['SELECT_OPTIONS']);die;
//								$addition['search'] .= '<SELECT name=discipline['.$category['ID'].']><OPTION value="">'._('N/A').'</OPTION>';
                        $addition['search'] .= '<SELECT name=discipline['.$category['ID'].']><OPTION value="">'._('N/A').'</OPTION>';

                        
                       // $addition['search']=db_request(db_execute("SELECT TITLE FROM DISCIPLINE_INCIDENT_FIELDs"));
                       
                      // print_r($addition['search']);
                        	//$category['SELECT_OPTIONS']=db_request(db_execute("SELECT id,TITLE FROM DISCIPLINE_INCIDENT_FIELDS"));
                        /*
                       echo "<pre>";
                        print_r($category['SELECT_OPTIONS']);
                        echo "</pre>";
                        */
                        		$addition['search'] .= '<option value="First Offence">First Offence</option><option value=""></option><option value="Second Offence">Second Offence</option><option value=""></option><option value="Third Offence">Third Offence</option>';
								foreach($category['SELECT_OPTIONS'] as $option)
									$addition['search'] .= '<OPTION value="'.$option.'">'.$option.'</OPTION>';
                       // $addition['search'] .= '<OPTION value="'.$option['id'].'">'.$option['TITLE'].'</OPTION>';

                        
                        //echo($option['id']);
                        
                        //die('nn');
                        
								$addition['search'] .= '</SELECT>';
								if(($category['DATA_TYPE']=='multiple_radio' || $category['DATA_TYPE']=='select') && $_REQUEST['discipline'][$category['ID']])
								{
									$addition['WHERE'] .= " AND dr.CATEGORY_".$category['ID']." = '".$_REQUEST['discipline'][$category['ID']]."' ";
									$_SYSTEM['SearchTerms'] .= '<font color=gray><b>'.$category['TITLE'].': </b></font>'.$_REQUEST['discipline'][$category['ID']].'<BR>';
								}
								elseif($category['DATA_TYPE']=='multiple_checkbox' && $_REQUEST['discipline'][$category['ID']])
								{
									$addition['WHERE'] .= " AND dr.CATEGORY_".$category['ID']." LIKE '%||".$_REQUEST['discipline'][$category['ID']]."||%' ";
									$_SYSTEM['SearchTerms'] .= '<font color=gray><b>'.$category['TITLE']._(' includes').': </b></font>'.$_REQUEST['discipline'][$category['ID']].'<BR>';
								}
							break;
						}
						$addition['search'] .= '</TD></TR>';
					}
				}
			break;
		}
	}
}
?>