<?php
session_start();
ini_set('max_execution_time', 300000);
include("include/setsession.php");
include("include/functions.php");
require_once("include/connect.inc");
mysqli_query($link, "SET character_set_results=utf8");

$txt="0";
$id = 0;
$enabled = 0;
$email_1 = "";
$email_2 = "";

//echo '*'.$_POST['active'].'*';

if (isset($_POST['id']) && $_POST['id']!='') 				$id = $_POST['id'];
if (isset($_POST['active']) && $_POST['active']!='') 		$enabled = $_POST['active'];
if (isset($_POST['email_1']) && $_POST['email_1']!='')		$email_1 = $_POST['email_1'];
if (isset($_POST['email_2']) && $_POST['email_2']!='')		$email_2 = $_POST['email_2'];

if($id==0) {
	$id=1;
	$sql="SELECT MAX(id) AS maxid FROM ".$s_prefix."_config";
	$val = mysqli_query($link, $sql) or die('Query failed: ' . mysqli_error($link));
	if($res = mysqli_fetch_array($val, MYSQLI_ASSOC)) {
		$id=($res['maxid']+1);
	}
	
	$sql="INSERT INTO ".$s_prefix."_config (id, email_1, email_2, enabled) VALUE (".$id.", '".$email_1."', '".$email_2."', ".$enabled.")";
	$res = mysqli_query($link, $sql) or die('Query failed: ' . mysqli_error($link));
} else {
	$sql="UPDATE ".$s_prefix."_config SET email_1='".$email_1."', email_2='".$email_2."', enabled='".$enabled."' WHERE id=".$id;
	$res = mysqli_query($link, $sql) or die('Query failed: ' . mysqli_error($link));
}

header("Location: index.php?sz=2&go=showSetupAlarm&sv=1");
?>