<?php
echo GetBaseJavaFunc();

if($_REQUEST['values'] && $_POST['values'])
{
	foreach($_REQUEST['values'] as $id=>$columns)
	{
		if($id!='new')
		{
			$sql = "UPDATE INTERMEDIATE_REPORT SET ";

			foreach($columns as $column=>$value)
			{
				$sql .= $column."='".str_replace("\'","''",$value)."',";
			}
			$sql = substr($sql,0,-1) . " WHERE ID='$id'";
			db_execute($sql);
		}
		else
		{
			$sql = "INSERT INTO INTERMEDIATE_REPORT ";

			$fields = 'ID,SCHOOL_ID,';
			$values = db_seq_nextval('SCHOOL_CLASSLEVELS_SEQ').",'".get_school_id()."',";

			$go = 0;
			foreach($columns as $column=>$value)
			{
				if($value)
				{
					$fields .= $column.',';
					$values .= "'".str_replace("\'","''",$value)."',";
					$go = true;
				}
			}
			$sql .= '(' . substr($fields,0,-1) . ') values(' . substr($values,0,-1) . ')';

			if($go)
				db_execute($sql);
		}
	}
}

DisplayHeader(".:".ProgramTitle().":.");

if($_REQUEST['servicefunction']=='remove')
{
	if(DeletePrompt(_('this class')))
	{
		db_execute("DELETE FROM INTERMEDIATE_REPORT WHERE ID='$_REQUEST[id]'");
		unset($_REQUEST['servicefunction']);
		unset($_SESSION['_REQUEST_vars']['servicefunction']);
	}
}

if($_REQUEST['servicefunction']!='remove')
{
	$sql = "SELECT ID,TITLE,SHORT_NAME,GPAVALUE FROM INTERMEDIATE_REPORT WHERE SCHOOL_ID='".get_school_id()."' ORDER BY ID";
	$QI = db_execute($sql);
	$grades_array = db_request($QI,array('TITLE'=>'makeTextInput','SHORT_NAME'=>'makeTextInput','GPAVALUE'=>'makeTextInput'));

	$columns = array('TITLE'=>_('Title'),'SHORT_NAME'=>_('Short Name'),'GPAVALUE'=>_('GPA Value'));
	$link['add']['html'] = array('TITLE'=>makeTextInput('','TITLE'),'SHORT_NAME'=>makeTextInput('','SHORT_NAME'),'GPAVALUE'=>makeTextInput('','GPAVALUE'));
	$link['remove']['link'] = "modloader.php?servicemod=$_REQUEST[servicemod]&servicefunction=remove";
	$link['remove']['variables'] = array('id'=>'ID');

	echo "<FORM action=modloader.php?servicemod=$_REQUEST[servicemod]&servicefunction=update method=POST>";
	DisplayHeader('','<INPUT type=submit value='._('Save').'>');
	Gridoutput($grades_array,$columns,_('Eligibility Codes'),_('Amount of Eligibility Codes'),$link);
	echo '<CENTER><INPUT type=submit value='._('Save').'></CENTER>';
	echo '</FORM>';
}

function makeTextInput($value,$name)
{	global $THIS_array;

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

	if($name!='TITLE')
		$addition = 'size=5 maxlength=4';
	if($name=='GPAVALUE')
		$addition = ' class=numeric ';

	return $comment.TextInput($value,'values['.$id.']['.$name.']','',$addition,$div);
}

?>