<?php
@session_start();
if (!isset($_SESSION['beroari_time'])){
	 echo "<script>this.window.close();</script>";
}elseif((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();
	chdir("..");

	include "./includes/variables.php";
	include "./misc/database.php";
	require_once "./includes/isgwtelnet.php";
	$ba= new beroAri();
	require_once("./includes/lang.php");

	if ($_POST['id_upd']){
	
		$direction=$_POST['call_direction'];
		$id=$_POST['id_upd'];
		$sip_response_message="";
		if ($direction=="SIP-ISGW"){
			$to_cause=$_POST['to_cause_isgw'];
			$from_cause=$_POST['from_cause_sip'];
			$sip_response_message=$_POST['sip_response_message'];
		}elseif ($direction=="SIP-ISDN"){
			$to_cause=$_POST['to_cause_isdn'];
			$from_cause=$_POST['from_cause_sip'];
			$sip_response_message=$_POST['sip_response_message'];
		}elseif ($direction=="ISDN-ISGW"){
			$to_cause=$_POST['to_cause_isgw'];
			$from_cause=$_POST['from_cause_isdn'];
		}elseif ($direction=="ISDN-SIP"){
			$to_cause=$_POST['to_cause_sip'];
			$from_cause=$_POST['from_cause_isdn'];
		}
		if ($from_cause==""){
			echo "<script>alert('".ISGW_CAUSES_TO_CAUSE_MESS_DIS."'); window.history.back(); </script>";
			return;
		}
		if ($to_cause==""){
			echo "<script>alert('".ISGW_CAUSES_FROM_CAUSE_MESS_DIS."'); window.history.back(); </script>";
			return;
		}
		$query_new = $ba->update("UPDATE isgw_causes SET to_cause='$to_cause', from_cause='$from_cause',
						 direction='$direction', sip_response_message='$sip_response_message' WHERE id='$id'");
	
		$query=$ba->select("select option from activate where id='activate'");
		while($entry = $ba->fetch_array($query)){
			if ($entry['option'] <= 2) $query =  $ba->update("UPDATE activate SET option=2 where id='activate'");
		}
		if (!$ba->is_error()){
			echo "<script>window.opener.location='../index.php?m=causes'</script>";
			echo "<script>this.window.close();</script>";

		}else{
			echo "<script>alert(".$ba->error().");</script>";
		}
	}else{
		$direction=$_POST['call_direction'];
		$sip_response_message="";
		if ($direction=="SIP-ISGW"){
			$to_cause=$_POST['to_cause_isgw'];
			$from_cause=$_POST['from_cause_sip'];
			$sip_response_message=$_POST['sip_response_message'];
		}elseif ($direction=="SIP-ISDN"){
			$to_cause=$_POST['to_cause_isdn'];
			$from_cause=$_POST['from_cause_sip'];
			$sip_response_message=$_POST['sip_response_message'];
		}elseif ($direction=="ISDN-ISGW"){
			$to_cause=$_POST['to_cause_isgw'];
			$from_cause=$_POST['from_cause_isdn'];
		}elseif ($direction=="ISDN-SIP"){
			$to_cause=$_POST['to_cause_sip'];
			$from_cause=$_POST['from_cause_isdn'];
		}
		if ($from_cause==""){
			echo "<script>alert('".ISGW_CAUSES_TO_CAUSE_MESS_DIS."'); window.history.back(); </script>";
			return;
		}
		if ($to_cause==""){
			echo "<script>alert('".ISGW_CAUSES_FROM_CAUSE_MESS_DIS."'); window.history.back(); </script>";
			return;
		}
		$query_new = $ba->insert_("INSERT INTO isgw_causes (direction, to_cause, from_cause, sip_response_message) 
								VALUES ('$direction','$to_cause', '$from_cause', '$sip_response_message')");
			$query=$ba->select("select option from activate where id='activate'");
			while($entry = $ba->fetch_array($query)){
				if ($entry['option'] <= 2) $query =  $ba->update("UPDATE activate SET option=2 where id='activate'");
			}
			if (!$ba->is_error()){
				echo "<script>window.opener.location='../index.php?m=causes'</script>";
				echo "<script>this.window.close();</script>";

			}else{
				echo "<script>alert(".$ba->error().");</script>";
			}

	}
}


?>
