<?php
@session_start();
if((isset($_SESSION['beroari_time']))&&(($_SESSION['beroari_time']+1200)<time())){
	@session_unset();
	@session_destroy();
	echo "<script>window.opener.location='../index.php'</script>";
	echo "<script>this.window.close();</script>";
}else{
	$_SESSION['beroari_time']=time();
	
	$id=$_GET['posid'];
	$new_id=0;
	chdir("..");
	include './includes/variables.php';
	include './misc/database.php';
	$ba = new beroAri();
	$query=$ba->select("SELECT * from dialplan order by posId asc");
	
	while(sqlite_has_more($query)){
		$tmp=sqlite_fetch_array($query);
		if ($tmp['posId']==$id){
			$tmp=sqlite_fetch_array($query);
			$new_id=$tmp['posId'];
			break;
		}
	}
		if ($new_id>0){
			$query=$ba->update("UPDATE dialplan SET posId=999999 where posId=".$new_id);
			$query=$ba->update("UPDATE dialplan SET posId=".$new_id." where posId=".$id);
			$query=$ba->update("UPDATE dialplan SET posId=".$id." where posId=999999");
			
			$div=floor(($new_id-1)/$ba->get('span'));
			$start=$div*$ba->get('span');
		
			$query=$ba->select("select option from activate where id='activate'");
			while($entry = $ba->fetch_array($query)){
				if ($entry['option'] <= 1) $query =  $ba->update("UPDATE activate SET option=1 where id='activate'");
			}
		}
	
	if (isset($_GET['start'])){
		$page=$_GET['start'];
		echo "<script>window.location='../index.php?m=dialplan&start=" . $page . "'</script>";
	}else{
		echo "<script>window.location='../index.php?m=dialplan'</script>";
	}
}
?>
