<?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"]));
		$systolic = urlencode(stripslashes($_POST['add_systolic']));
		$diastolic = urlencode(stripslashes($_POST['add_diastolic']));
		$measuredby = urlencode(stripslashes($_POST['add_measured_by']));
		$readingdate = urlencode(stripslashes($_POST['add_date']));
		$pulse = urlencode(stripslashes($_POST['add_pulse']));
				
		$params = "id=".$id;
		$params .= "&pin=" . $pin;
		$params .= "&systolic=".$systolic;
		$params .= "&diastolic=".$diastolic;
		$params .= "&measuredby=".$measuredby;
		$params .= "&readingdate=".$readingdate;
		$params .= "&pulse=".$pulse;

		$results = new xml("amjayaddbp", $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_bloodpressure_url());
	} elseif ($_GET['mode'] == 'edit') {
		$template = new bloodpressure_page("template_bloodpressure.html", $_GET['rowid']);
		$template->show();
	} elseif ($_GET['mode'] == 'update') {
		$id = urlencode(stripslashes($_SESSION["user_id"]));
		$pin = urlencode(stripslashes($_SESSION["pin"]));
		$systolic = urlencode(stripslashes($_POST['edit_systolic']));
		$diastolic = urlencode(stripslashes($_POST['edit_diastolic']));
		$measuredby = urlencode(stripslashes($_POST['edit_measured_by']));
		$readingdate = urlencode(stripslashes($_POST['edit_date']));
		$pulse = urlencode(stripslashes($_POST['edit_pulse']));
		$bpid = urlencode(stripslashes($_GET['rowid']));
				
		$params = "id=".$id;
		$params .= "&pin=" . $pin;
		$params .= "&systolic=".$systolic;
		$params .= "&diastolic=".$diastolic;
		$params .= "&measuredby=".$measuredby;
		$params .= "&readingdate=".$readingdate;
		$params .= "&pulse=".$pulse;
		$params .= "&bpid=".$bpid;
		
		$results = new xml("amjayeditbp", $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_bloodpressure_url());
	} elseif ($_GET['mode'] == 'del') {
		$id = urlencode(stripslashes($_SESSION["user_id"]));
		$pin = urlencode(stripslashes($_SESSION["pin"]));
		$bpid = urlencode(stripslashes($_GET['rowid']));

		$params = "id=".$id;
		$params .= "&pin=" . $pin;
		$params .= "&bpid=".$bpid;
		
		$results = new xml("amjaydelbp", $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_bloodpressure_url());
	} else {
		$template = new bloodpressure_page("template_bloodpressure.html");
		$template->show();
	}
} else {
	$template = new template();
	$template->go302(format_login_url(format_bloodpressure_url()));
}

?>