<?php

include_once("config.php");

if (isset($_SESSION["user_id"]) && isset($_SESSION["pin"])) { //LOG IN!
	if ($_GET['mode'] == 'add') {
		$id = urlencode(stripslashes($_SESSION["user_id"]));
		$pin = urlencode(stripslashes($_SESSION["pin"]));
		$sugarlevel = urlencode(stripslashes($_POST['add_sugarlevel']));
		$unitofmeasure = urlencode(stripslashes($_POST['add_unitofmeasure']));
		$measuredby = urlencode(stripslashes($_POST['add_measured_by']));
		$readingdate = urlencode(stripslashes($_POST['add_date']));
				
		$params = "id=".$id;
		$params .= "&pin=" . $pin;
		$params .= "&sugarlevel=".$sugarlevel;
		$params .= "&unitofmeasure=".$unitofmeasure;
		$params .= "&measuredby=".$measuredby;
		$params .= "&readingdate=".$readingdate;

		$results = new xml("amjayaddbs", $params, true);
		if (isset($results->error)) {
			$_SESSION['Err'] = $results->error[0]->row[0]->row[0]->error;
			$template = new template();
		} else {
			$template = new template();
		}
		$template->go302(format_bloodsugar_url());
	} elseif ($_GET['mode'] == 'edit') {
		$template = new bloodsugar_page("template_bloodsugar.html", $_GET['rowid']);
		$template->show();
	} elseif ($_GET['mode'] == 'update') {
		$id = urlencode(stripslashes($_SESSION["user_id"]));
		$pin = urlencode(stripslashes($_SESSION["pin"]));
		$sugarlevel = urlencode(stripslashes($_POST['edit_sugarlevel']));
		$unitofmeasure = urlencode(stripslashes($_POST['edit_unitofmeasure']));
		$measuredby = urlencode(stripslashes($_POST['edit_measured_by']));
		$readingdate = urlencode(stripslashes($_POST['edit_date']));
		$bsid = urlencode(stripslashes($_GET['rowid']));
				
		$params = "id=".$id;
		$params .= "&pin=" . $pin;
		$params .= "&sugarlevel=".$sugarlevel;
		$params .= "&unitofmeasure=".$unitofmeasure;
		$params .= "&measuredby=".$measuredby;
		$params .= "&readingdate=".$readingdate;
		$params .= "&bsid=".$bsid;
		
		$results = new xml("amjayeditbs", $params, true);
		if (isset($results->error)) {
			$_SESSION['Err'] = $results->error[0]->row[0]->row[0]->error;
			$template = new template();
		} else {
			$template = new template();
		}
		$template->go302(format_bloodsugar_url());
	} elseif ($_GET['mode'] == 'del') {
		$id = urlencode(stripslashes($_SESSION["user_id"]));
		$pin = urlencode(stripslashes($_SESSION["pin"]));
		$bsid = urlencode(stripslashes($_GET['rowid']));

		$params = "id=".$id;
		$params .= "&pin=" . $pin;
		$params .= "&bsid=".$bsid;
		
		$results = new xml("amjaydelbs", $params, true);
		if (isset($results->error)) {
			$_SESSION['Err'] = $results->error[0]->row[0]->row[0]->error;
			$template = new template();
		} else {
			$template = new template();
		}
		$template->go302(format_bloodsugar_url());
	} else {
		$template = new bloodsugar_page("template_bloodsugar.html");
		$template->show();
	}
} else {
	$template = new template();
	$template->go302(format_login_url(format_bloodsugar_url()));
}

?>