<?php

class Wizard {

	function title0($ba){
	$query=$ba->select("select distinct type from isdn where lif='0' union select distinct type from analog where lif='0' union select distinct type from gsm where lif='0' group by type");
	if (sqlite_num_rows($query)==0){
	$title="<b>Li0()</b>";
	}else{
	while ($entry=$ba->fetch_array($query)){
	$title="<b>Li0(".$entry['type'].")</b>";
	}
	}
	return $title;
}
function ports0($ba,$port_type){
	$tab="<table border='0'>";
	$query=$ba->select("select gr,port,type from isdn where lif='0' and port_type='".$port_type."' union select gr,port,type from analog where lif='0' and port_type='".$port_type."' union select gr,port,type from gsm where lif='0' and port_type='".$port_type."'");
	while ($entry=$ba->fetch_array($query)){
		$type=split("-",$entry['type']);
		if ($entry['gr']==$gr){
		$tab .= "<tr><td>Port ".$entry['port']."<input type='checkbox' name='ports[]' checked value='".$entry['port']."'></td></tr>";
		}elseif ($entry['gr']=='NULL'){
		$tab .= "<tr><td>Port ".$entry['port']."<input type='checkbox' name='ports[]' value='".$entry['port']."'></td></tr>";
		}else{
		$tab .= "<tr><td>Port ".$entry['port']."<input type='checkbox' name='ports[]' checked disabled='true' value='".$entry['port']."'></td></tr>";
		}
	}
	$tab .= "</table>";
	return $tab;
}

function title1($ba){
	$query=$ba->select("select distinct type from isdn where lif='1' union select distinct type from analog where lif='1' union select distinct type from gsm where lif='1' group by type");
	if (sqlite_num_rows($query)==0){
	$title="<b>Li1()</b>";
	}else{
	while ($entry=$ba->fetch_array($query)){
	$title="<b>Li1(".$entry['type'].")</b>";
	}
	}
	return $title;
}

function ports1($ba,$port_type){
	$tab="<table border='0'>";
	$query=$ba->select("select gr,port, type from isdn where lif='1' and port_type='".$port_type."' union select gr,port, type from analog where lif='1' and port_type='".$port_type."' union select gr,port, type from gsm where lif='1' and port_type='".$port_type."'");
	while ($entry=$ba->fetch_array($query)){
		$type=split("-",$entry['type']);
		if ($entry['gr']==$gr){
		$tab .= "<tr><td>Port ".$entry['port']."<input type='checkbox' name='ports[]' checked value='".$entry['port']."'></td></tr>";
		}elseif ($entry['gr']=='NULL'){
		$tab .= "<tr><td>Port ".$entry['port']."<input type='checkbox' name='ports[]' value='".$entry['port']."'></td></tr>";
		}else{
		$tab .= "<tr><td>Port ".$entry['port']."<input type='checkbox' name='ports[]' checked disabled='true' value='".$entry['port']."'></td></tr>";
		}
	}
	$tab .= "</table>";
	return $tab;
}


function display(){
$ba=new beroAri();
//Wizard language
if ($ba->get("lang")=="de"){
	require_once('./includes/lang/de.php');
}else{
	require_once('./includes/lang/en.php');
}
if ($_GET['res']=="yes"){
	unlink("/usr/conf/ari.db");

	echo "<script>window.location.href='./index.php';</script>";
}
echo '
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<link rel="icon" href="./includes/images/favicon.ico" type="image/x-icon">
<script type="text/javascript" language="javascript" src="./includes/validation_modul.js"></script>
<TITLE><?php echo WIZARD_TITLE_DIS; ?></TITLE>
<link rel="stylesheet" href="./includes/template_css.css" type="text/css">
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
</head>
<body>
<script type="text/javascript" src="./includes/notes/wz_tooltip.js"></script>
<center>';




echo "<br>
<table align=\"center\" bgcolor=\"#f0f0ec\" border=\"0\" cellpadding=\"0\" cellspacing=\"0\" width=\"450\">
	<colgroup>
		<col width=\"39\"></col>
	</colgroup>
	<tbody>
		<tr>
			<td colspan=\"2\"><img src=\"./includes/images/bg_top.png\"></td>
		</tr>
		<tr>
			<td background=\"./includes/images/bg_left.png\" style='background-repeat:repeat-y;' width=\"30\"><img src=\"./includes/images/trans.gif\" height=\"10\" width=\"39\"></td>
			<td><p align='left'><img src=\"./includes/images/berofixlogo.png\"/></p><hr size= \"1\" noshade><p><center><font size='3'>".WIZARD_TITLE_DIS."</font></p><br><br>
			<h2>".WIZARD_INFO_DIS."</h2><br/>";

if (!isset($_POST['sc']) && !isset($_POST['sip_name']) && !isset($_POST['group']) && !isset($_POST['tk'])){
	echo "<form action='' method='POST' class='extensions_form' name='scenario' >
			<table border='0' width='90%'>
				<tr>
					<th align='left' colspan='2'>
						".WIZARD_SCENARIOS_DIS.":
					</th>
				</tr>
				<tr valign='middle'>
					<td width='85' nowrap>
						<input type='radio' name='sc' value='gw' /> ".WIZARD_GATEWAY_DIS."
					</td>
					<td>
						<img src=\"./includes/images/gateway.png\" />
					</td>
				</tr>
				<tr>
					<td colsnan='2'>&nbsp;</td>
				</tr>
				<tr valign='middle'>
					<td width='85' nowrap>
						<input type='radio' name='sc' value='tk' /> ".WIZARD_PBX_DIS."
					</td>
					<td>
						<img src=\"./includes/images/pbx.png\"/>
					</td>
				</tr>
				<tr valign='middle'>
					<td width='85' nowrap>
						<input type='radio' name='sc' value='tk-admin' /> ".WIZARD_TK_DIS."
					</td>
					<td>
						<img src=\"./includes/images/Test_sz.png\"/>
					</td>
				</tr>
			        <tr>
					<td align='right' colspan='2'>
						<a onclick='javascript:window.location.href=\"index.php?m=wizard&res=yes\";' onmouseover=\"Tip('" . CANCEL . "')\" onmouseout='UnTip()'>
							<img src='".WIZARD_IMAGE_CANCEL_DIS."' onmouseover=\"this.src='".WIZARD_IMAGE_CANCEL_HOVER_DIS."';\" onmouseout=\"this.src='".WIZARD_IMAGE_CANCEL_DIS."';\"/>
						</a>
						<a onclick='javascript:document.forms.scenario.submit();' onmouseover=\"Tip('" . NEXT . "')\" onmouseout='UnTip()'>
							<img src='".WIZARD_IMAGE_NEXT_DIS."' onmouseover=\"this.src='".WIZARD_IMAGE_NEXT_HOVER_DIS."';\" onmouseout=\"this.src='".WIZARD_IMAGE_NEXT_DIS."';\"/>
						</a>
					</td>
				</tr>
			</table>
		</form>";
}

if (isset($_POST['sc']) && $_POST['sc']!="tk-admin"){
	echo "<form class='extensions_form' name='sip' method='POST'>
			<table border='0' width='90%'>
				<tr>
					<th colspan='2'>".WIZARD_SIP_CONFIGURATION_DIS."</th>
				</tr>
				<tr>
					<th colspan='2'>&nbsp;</th>
				</tr>
				<tr>
					<td align='right'><b>".SIP_NAME_DIS.":</b>&nbsp;</td>
					<td><input name='sip_name' type='text'  onmouseover=\"Tip('".TRUNKS_SIP_NAME."')\" onmouseout='UnTip()'></td>
				</tr>
				<tr>
					<td align='right'><b>".SIP_SERVER_ADDRESS_DIS.":</b>&nbsp;</td>
					<td><input name='server_address' type='text' onmouseover=\"Tip('".TRUNKS_SIP_SERVER_ADDRESS."')\" onmouseout='UnTip()'></td>
				</tr>
				<tr>
					<td align='right'><b>".SIP_USER_DIS.":</b>&nbsp;</td>
					<td><input name='user' type='text' onmouseover=\"Tip('".TRUNKS_SIP_USER."')\" onmouseout='UnTip()'></td>
				</tr>
				<tr>
					<td align='right'><b>".SIP_SECRET_DIS.":</b>&nbsp;</td>
					<td><input name='secret' type='text' onmouseover=\"Tip('".TRUNKS_SIP_SECRET."')\" onmouseout='UnTip()'></td>
				</tr>
				<tr>
					<td colspan='2'>
						<input type='hidden' name='scen' value='".$_POST['sc']."'/>&nbsp;
					</td>
				</tr>
				<tr>
	    			<td align='right' colspan='2'>
	    				<a onclick='javascript:window.location.href=\"index.php?m=wizard&res=yes\";' onmouseover=\"Tip('" . CANCEL . "')\" onmouseout='UnTip()'>
				 			<img src='".WIZARD_IMAGE_CANCEL_DIS."' onmouseover=\"this.src='".WIZARD_IMAGE_CANCEL_HOVER_DIS."';\" onmouseout=\"this.src='".WIZARD_IMAGE_CANCEL_DIS."';\"/>
				 		</a>
	    				<a onclick='javascript:if (is_not_empty(document.forms.sip.sip_name.value,\"name\") && is_not_empty(document.forms.sip.server_address.value, \"Server Address\") && is_not_empty(document.forms.sip.name.value,\"Name\")) document.forms.sip.submit();' onmouseover=\"Tip('" . NEXT . "')\" onmouseout='UnTip()'>
				 			<img src='".WIZARD_IMAGE_NEXT_DIS."' onmouseover=\"this.src='".WIZARD_IMAGE_NEXT_HOVER_DIS."';\" onmouseout=\"this.src='".WIZARD_IMAGE_NEXT_DIS."';\"/>
				 		</a>
	    			</td>
	    		</tr>
			</table>
		</form>";
}

if (isset($_POST['sip_name'])){
	$name=$_POST['sip_name'];
	$ser_addr=$_POST['server_address'];
	$user=$_POST['user'];
	$secret=$_POST['secret'];
	$type=$_POST['type'];
	$default=$_POST['default'];
	if ($default=="") $default=0;
	$externip=$_POST['externip'];
	$query_new = $ba->insert_("INSERT INTO sip (name, server_address, user, secret, type)
								VALUES ('$name', '$ser_addr', '$user', '$secret', '0')");
	$query=$ba->select("select option from activate where id='activate'");
	while($entry = $ba->fetch_array($query)){
		if ($entry['option'] <= 3) $query =  $ba->update("UPDATE activate SET option=3 where id='activate'");
	}
}

//TK Anlage (PBX): PSTN Eintraege speichern und Uebersicht am Ende anzeigen
if (isset($_POST['group']) && $_POST['scen']=="tk"){
	$query_comp=$ba->select("SELECT DISTINCT gr FROM isdn WHERE gr!='NULL' union SELECT DISTINCT gr FROM analog WHERE gr!='NULL' union SELECT DISTINCT gr FROM gsm WHERE gr!='NULL' group by gr");
	while ($entry = $ba->fetch_array($query_comp)) {
		if ($_POST['group']==$entry['gr']){
			$fehler=1;
			echo "<font color='red'>".WIZARD_INVALID_GROUP_NAME_DIS."</font>";
		}
	}

	if ($fehler==0){
		//Isdn Eintrag
		$port_type=$_POST['port_type'];
		$gr=$_POST['group'];
		$ports=$_POST['ports'];
		$new_ports=$_POST['new_ports'];
		$chansel='standard';
		$tones='[de]';
		$odt=3;
		$odt_empty_dad=15;
		$port="";
		$query = $ba->select('SELECT DISTINCT gr FROM isdn WHERE gr!="NULL" union SELECT DISTINCT gr FROM analog WHERE gr!="NULL" union SELECT DISTINCT gr FROM gsm WHERE gr!="NULL"');
		if (sqlite_num_rows($query)==0)	$ntte="te";
		else $ntte="nt";

		if ($port_type=="FXS" || $port_type=="FXO"){
			for ($i=0;$i<count($ports);$i++){
				$port = $ports[$i];
				$query_new = $ba->update("UPDATE analog SET gr='$gr' WHERE port='$port'");
			}
			$new_port="";
			for ($i=0;$i<count($new_ports);$i++){
				$new_port= $new_ports[$i];
				$query_new = $ba->update("UPDATE analog SET gr='NULL', config='' WHERE port='$new_port'");
			}
		}elseif ($port_type=="GSM"){
			for ($i=0;$i<count($ports);$i++){
				$port = $ports[$i];
				$query_new = $ba->update("UPDATE gsm SET gr='$gr' WHERE port='$port'");
			}
			$new_port="";
			for ($i=0;$i<count($new_ports);$i++){
				$new_port= $new_ports[$i];
				$query_new = $ba->update("UPDATE gsm SET gr='NULL', config='' WHERE port='$new_port'");
			}
		}else{
			for ($i=0;$i<count($ports);$i++){
				$port = $ports[$i];
				$query_new = $ba->update("UPDATE isdn SET gr='$gr', odt='$odt', ntteSW='$ntte', ntteHW='$ntte', odt_empty_dad='$odt_empty_dad', chansel='$chansel', tones='$tones' WHERE port='$port'");
			}
			$new_port="";
			for ($i=0;$i<count($new_ports);$i++){
				$new_port= $new_ports[$i];
				$query_new = $ba->update("UPDATE isdn SET gr='NULL', config='', odt='3', odt_empty_dad='15', chansel='standard', tones='[de]' WHERE port='$new_port'");
			}
		}

		$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'");
		}
	}

	$query = $ba->select('SELECT DISTINCT gr FROM isdn WHERE gr!="NULL" union SELECT DISTINCT gr FROM analog WHERE gr!="NULL" union SELECT DISTINCT gr FROM gsm WHERE gr!="NULL"');
	if (sqlite_num_rows($query)==2)	{
		//Dialplan Eintraege

		#Sip->pstn
		$fromDirection="sip";
		$query = $ba->select('SELECT DISTINCT gr,port_type FROM isdn WHERE gr!="NULL" union SELECT DISTINCT gr,port_type FROM analog WHERE gr!="NULL" union SELECT DISTINCT gr,port_type FROM gsm WHERE gr!="NULL"');
		$entry=@$ba->fetch_array($query);
		if ($entry['port_type']=="FXS" || $entry['port_type']=="FXO") $toDirection1="analog";
		elseif ($entry['port_type']=="GSM") $toDirection1="gsm";
		else $toDirection1="isdn";
		$entry=@$ba->fetch_array($query);
		if ($entry['port_type']=="FXS" || $entry['port_type']=="FXO") $toDirection2="analog";
		elseif ($entry['port_type']=="GSM") $toDirection2="gsm";
		else $toDirection2="isdn";
		$query=$ba->select("select name from sip");
		$fromid="p:".@sqlite_fetch_single($query);
		$query = $ba->select('SELECT DISTINCT gr FROM isdn WHERE gr!="NULL" union SELECT DISTINCT gr FROM analog WHERE gr!="NULL" union SELECT DISTINCT gr FROM gsm WHERE gr!="NULL"');
		$entry=@$ba->fetch_array($query);
		$toid1="g:".$entry['gr'];
		$entry=@$ba->fetch_array($query);
		$toid2="g:".$entry['gr'];

		$dest1="0(.*)";
		$dest2="1(.*)";
		$newdest="\\\\1";
		$src="(.*)";
		$newsrc="\\\\1";
		$query_new = $ba->insert_("INSERT INTO dialplan (fromDirection, toDirection, fromId, toId, dest, newDest, src, newSrc) VALUES ('$fromDirection','$toDirection1','$fromid','$toid1', '$dest1', '$newdest', '$src', '$newsrc')");
		$query_new = $ba->insert_("INSERT INTO dialplan (fromDirection, toDirection, fromId, toId, dest, newDest, src, newSrc) VALUES ('$fromDirection','$toDirection2','$fromid','$toid2', '$dest2', '$newdest', '$src', '$newsrc')");

		#pstn->Sip
		$query = $ba->select('SELECT DISTINCT gr,port_type FROM isdn WHERE gr!="NULL" union SELECT DISTINCT gr,port_type FROM analog WHERE gr!="NULL" union SELECT DISTINCT gr,port_type FROM gsm WHERE gr!="NULL"');
		$entry=@$ba->fetch_array($query);
		if ($entry['port_type']=="FXS" || $entry['port_type']=="FXO") $fromDirection1="analog";
		elseif ($entry['port_type']=="GSM") $fromDirection1="gsm";
		else $fromDirection1="isdn";
		$entry=@$ba->fetch_array($query);
		if ($entry['port_type']=="FXS" || $entry['port_type']=="FXO") $fromDirection2="analog";
		elseif ($entry['port_type']=="GSM") $fromDirection2="gsm";
		else $fromDirection2="isdn";
		$toDirection="sip";
		$dest="(.*)";
		$newdest1="0\\\\1";
		$newdest2="1\\\\0";
		$query_new = $ba->insert_("INSERT INTO dialplan (fromDirection, toDirection, fromId, toId, dest, newDest, src, newSrc) VALUES ('$fromDirection1','$toDirection', '$toid1', '$fromid', '$dest', '$newdest1', '$src', '$newsrc')");
		$query_new = $ba->insert_("INSERT INTO dialplan (fromDirection, toDirection, fromId, toId, dest, newDest, src, newSrc) VALUES ('$fromDirection2','$toDirection', '$toid2', '$fromid', '$dest', '$newdest2', '$src', '$newsrc')");

		echo "<table border='0' align='left' width='100%'>
				<tr>
					<td><b>".WIZARD_SIP_ENTRY_DIS.":</b></td>
				</tr>
				<tr>
					<td>";
		$query = $ba->select('SELECT * FROM sip ORDER BY id asc');
		while ($entry = $ba->fetch_array($query)) {
			echo SIP_NAME_DIS.": ".$entry['name']."<br/>";
			echo SIP_SERVER_ADDRESS_DIS.": ".$entry['server_address']."<br/>";
			echo SIP_USER_DIS.": ".$entry['user']."<br/>";
			echo SIP_SECRET_DIS.": ".$entry['secret']."<br/>";
			echo SIP_REGISTER_DIS.": ".$entry['type']."<br/>";
		}

		echo "</td>
			</tr>
			<tr>
				<td>&nbsp;</td>
			</tr>
			<tr>
				<td><b>".WIZARD_PSTN_ENTRIES_DIS.":</b></td>
			</tr>
			<tr>
				<td>";
		$query = $ba->select('SELECT DISTINCT gr,port_type FROM isdn WHERE gr!="NULL" union SELECT DISTINCT gr,port_type FROM analog WHERE gr!="NULL" union SELECT DISTINCT gr,port_type FROM gsm WHERE gr!="NULL"');
		while ($entry = $ba->fetch_array($query)) {
			if ($entry['port_type']=="FXS" || $entry['port_type']=="FXO") $query_port = $ba->select('SELECT port FROM analog WHERE gr="'.$entry['gr'].'" ORDER BY port asc');
			elseif ($entry['port_type']=="GSM") $query_port = $ba->select('SELECT port FROM gsm WHERE gr="'.$entry['gr'].'" ORDER BY port asc');
			else $query_port = $ba->select('SELECT port FROM isdn WHERE gr="'.$entry['gr'].'" ORDER BY port asc');
			$ports="";
			while ($entryp = $ba->fetch_array($query_port)) {
				if($ports!=""){
					$ports.=",".$entryp['port'];
				}else{
					$ports.=$entryp['port'];
				}
			}
			echo PSTN_NAME_DIS.": ".$entry['gr']."<br/>";
			echo PSTN_PORTS_DIS.": ".$ports."<br/></td></tr><tr><td>&nbsp;</td></tr><tr><td>";
		}

		echo "</td>
			</tr>
			<tr>
				<td>&nbsp;</td>
			</tr>
			<tr>
				<td><b>".WIZARD_DIALPLAN_ENTRIES_DIS.":</b></td>
			</tr>
			<tr>
				<td>";
		$query = $ba->select(' SELECT * from dialplan ORDER BY id asc');
		while ($entry = $ba->fetch_array($query)) {
			echo DIALPLAN_FROM_DIRECTION_DIS.": ".$entry['fromDirection']."<br/>";
			echo DIALPLAN_TO_DIRECTION_DIS.": ".$entry['toDirection']."<br/>";
			echo DIALPLAN_FROM_ID_DIS.": ".$entry['fromId']."<br/>";
			echo DIALPLAN_TO_ID_DIS.": ".$entry['toId']."<br/>";
			echo DIALPLAN_DESTINATION_DIS.": ".stripslashes($entry['dest'])."<br/>";
			echo DIALPLAN_NEW_DESTINATION_DIS.": ".stripslashes($entry['newDest'])."<br/>";
			echo DIALPLAN_SOURCE_DIS.": ".stripslashes($entry['src'])."<br/>";
			echo DIALPLAN_NEW_SOURCE_DIS.": ".stripslashes($entry['newSrc'])."<br/></td></tr><tr><td>&nbsp;</td></tr><tr><td>";
		}
		echo "</td></tr></table>
			<p align='right'>
				<a onclick='javascript:window.location.href=\"index.php?m=wizard&res=yes\";' onmouseover=\"Tip('" . CANCEL . "')\" onmouseout='UnTip()'>
					<img src='".WIZARD_IMAGE_CANCEL_DIS."' onmouseover=\"this.src='".WIZARD_IMAGE_CANCEL_HOVER_DIS."';\" onmouseout=\"this.src='".WIZARD_IMAGE_CANCEL_DIS."';\"/>
				</a>
				<a onclick='javascript:window.location.href=\"./includes/create_files.php?m=state\";' onmouseover=\"Tip('" . FINISH . "')\" onmouseout='UnTip()'>
					<img src='".WIZARD_IMAGE_FINISH_DIS."' onmouseover=\"this.src='".WIZARD_IMAGE_FINISH_HOVER_DIS."';\" onmouseout=\"this.src='".WIZARD_IMAGE_FINISH_DIS."';\"/>
				</a>
			</p>";
	}
}
//pstn Network Group Configuration (TK Anlage)
if (isset($_POST['sip_name']) && $_POST['scen']=="tk"){
	$query=$ba->select("select distinct port_type from isdn union select distinct port_type from analog union select distinct port_type from gsm");
	while ($entry=$ba->fetch_array($query)){
		echo "<input type='radio' value='".$entry['port_type']."' name='port_types'
				onclick='for (var i=0;i<document.forms.length;i++) document.forms[i].style.display=\"none\"; document.getElementById(\"pstn_".$entry['port_type']."\").style.display=\"\";'>"
				.$entry['port_type']."<br/>
				";
	}
	$query_=$ba->select("select distinct port_type from isdn union select distinct port_type from analog union select distinct port_type from gsm");
	while ($entry_=$ba->fetch_array($query_)){
	echo "<form class='extensions_form' style='display:none;' name='pstn_".$entry_['port_type']."' id='pstn_".$entry_['port_type']."' method='POST'>
			<table border=0 align='center'>
				<tr>
					<th colspan='2'>".WIZARD_PSTN_NETWORK_GROUP_DIS."</th>
				</tr>
				<tr>
					<th colspan='2'>&nbsp;</th>
				</tr>
				<tr>
					<td><b>".PSTN_NAME_DIS.":</b>&nbsp;</td>
					<td>&nbsp;<input type='text' name='group' onmouseover=\"Tip('".ISDN_GROUP_NAME."');\" onmouseout='UnTip();'></td>
				</tr>
				<tr>
					<td valign='top'><b>".PSTN_PORTS_DIS.":</b>&nbsp;</td>
				<td>
					<table>
						<tr>
							<td valign='top' colspan='2'>
								<table border='1' style='border-collapse:collapse;' width='100%' onmouseover=\"Tip('".ISDN_PORTS_TABLE."');\" onmouseout='UnTip();'>
									<tr>
										<td width='65'>".title0($ba)."</td>
										<td width='65'>".title1($ba)."</td>
									</tr>
									<tr>
										<td valign='top'>".ports0($ba,$entry_['port_type'])."</td>
										<td valign='top'>".ports1($ba,$entry_['port_type'])."</td>
									</tr>
								</table>
							</td>
						</tr>
					</table>
				</td>
				</tr>
				<tr>
					<td colspan='2'>
						<input type='hidden' name='scen' value='".$_POST['scen']."'/>&nbsp;
						<input type='hidden' name='port_type' value='".$entry_['port_type']."'/>&nbsp;
						<input type='hidden' name='entry' value='1'/>
					</td>
				</tr>
	    	</table>
	    	<p align='right'>
				<a onclick='javascript:window.location.href=\"index.php?m=wizard&res=yes\";' onmouseover=\"Tip('" . CANCEL . "')\" onmouseout='UnTip()'>
					<img src='".WIZARD_IMAGE_CANCEL_DIS."' onmouseover=\"this.src='".WIZARD_IMAGE_CANCEL_HOVER_DIS."';\" onmouseout=\"this.src='".WIZARD_IMAGE_CANCEL_DIS."';\"/>
	 			</a>
				<a onclick='javascript:if (is_not_empty(document.forms.pstn_".$entry_['port_type'].".group.value,\"group name\")) document.forms.pstn_".$entry_['port_type'].".submit();' onmouseover=\"Tip('" . NEXT . "')\" onmouseout='UnTip()'>
		 			<img src='".WIZARD_IMAGE_NEXT_DIS."' onmouseover=\"this.src='".WIZARD_IMAGE_NEXT_HOVER_DIS."';\" onmouseout=\"this.src='".WIZARD_IMAGE_NEXT_DIS."';\"/>
		 		</a>
	 		</p>
		</form>";
	}
}


//pstn PBX GROUP Configuration
if (($_POST['scen']=="tk" && $_POST['entry']=="1") || ($_POST['scen']=="tk" && $fehler==1)){
	$query=$ba->select("select distinct port_type from isdn union select distinct port_type from analog union select distinct port_type from gsm");
	while ($entry=$ba->fetch_array($query)){
		echo "<input type='radio' value='".$entry['port_type']."' name='port_types'
				onclick='for (var i=0;i<document.forms.length;i++) document.forms[i].style.display=\"none\"; document.getElementById(\"pstn_".$entry['port_type']."\").style.display=\"\";'>"
				.$entry['port_type']."<br/>
				";
	}
	$query_=$ba->select("select distinct port_type from isdn union select distinct port_type from analog union select distinct port_type from gsm");
	while ($entry_=$ba->fetch_array($query_)){
	echo "<form class='extensions_form' style='display:none;' name='pstn_".$entry_['port_type']."' id='pstn_".$entry_['port_type']."' method='POST'>
			<table border=0 align='center'>
				<tr>
					<th colspan='2'>".WIZARD_PSTN_PBX_GROUP_DIS."</th>
				</tr>
				<tr>
					<th colspan='2'>&nbsp;</th>
				</tr>
				<tr>
					<td><b>".PSTN_NAME_DIS.":</b>&nbsp;</td>
					<td>&nbsp;<input type='text' name='group' onmouseover=\"Tip('".ISDN_GROUP_NAME."');\" onmouseout='UnTip();'></td>
				</tr>
				<tr>
					<td valign='top'><b>".PSTN_PORTS_DIS.":</b>&nbsp;</td>
				<td>
					<table>
						<tr>
							<td valign='top' colspan='2'>
								<table border='1' style='border-collapse:collapse;' width='100%' onmouseover=\"Tip('".ISDN_PORTS_TABLE."');\" onmouseout='UnTip();'>
									<tr>
										<td width='65'>".title0($ba)."</td>
										<td width='65'>".title1($ba)."</td>
									</tr>
									<tr>
										<td valign='top'>".ports0($ba,$entry_['port_type'])."</td>
										<td valign='top'>".ports1($ba,$entry_['port_type'])."</td>
									</tr>
								</table>
							</td>
						</tr>
					</table>
				</td>
				</tr>
				<tr>
					<td colspan='2'>
						<input type='hidden' name='scen' value='".$_POST['scen']."'/>&nbsp;
						<input type='hidden' name='port_type' value='".$entry_['port_type']."'/>
					</td>
				</tr>
	    	</table>
	    	<p align='right'>
				<a onclick='javascript:window.location.href=\"index.php?m=wizard&res=yes\";' onmouseover=\"Tip('" . CANCEL . "')\" onmouseout='UnTip()'>
					<img src='".WIZARD_IMAGE_CANCEL_DIS."' onmouseover=\"this.src='".WIZARD_IMAGE_CANCEL_HOVER_DIS."';\" onmouseout=\"this.src='".WIZARD_IMAGE_CANCEL_DIS."';\"/>
	 			</a>
				<a onclick='javascript:if (is_not_empty(document.forms.pstn_".$entry_['port_type'].".group.value,\"group name\")) document.forms.pstn_".$entry_['port_type'].".submit();' onmouseover=\"Tip('" . NEXT . "')\" onmouseout='UnTip()'>
		 			<img src='".WIZARD_IMAGE_NEXT_DIS."' onmouseover=\"this.src='".WIZARD_IMAGE_NEXT_HOVER_DIS."';\" onmouseout=\"this.src='".WIZARD_IMAGE_NEXT_DIS."';\"/>
		 		</a>
	 		</p>
		</form>";
	}
}

//TK-Anlage (beroAdmin)
if ($_POST['sc']=="tk-admin"){

	echo "<form class='extensions_form' name='tk_admin' id='tk_admin' method='POST'>
			<input type='hidden' name='scen' value='".$_POST['sc']."'/>
			<input type='hidden' name='tk' value='admin'/>";
	echo "<table border=0 align='center'>
				<tr>
					<th>".WIZARD_TK_DIS."</th>
				</tr>";
	$query=$ba->select("select distinct port_type from isdn union select distinct port_type from analog union select distinct port_type from gsm group by port_type");
	while ($entry=$ba->fetch_array($query)){
		if ($entry['port_type']!="FXS" && $entry['port_type']!="FXO" && $entry['port_type']!="GSM"){
			echo "<tr><td><b>".$entry['port_type']."-INTERN</b></td><td><b>".$entry['port_type']."-EXTERN</b></td></tr>";
			$queryp=$ba->select("select gr,port,type from isdn where port_type='".$entry['port_type']."' union select gr,port,type from analog where port_type='".$entry['port_type']."' union select gr,port,type from gsm where port_type='".$entry['port_type']."' order by port");
			while ($entryp=$ba->fetch_array($queryp)){
				echo "<tr><td><input type='checkbox' name='".$entry['port_type']."-ports-intern[]' id='intern-".$entryp['port']."' value='".$entryp['port']."'
							onclick=\"if (this.checked==true) document.getElementById('extern-".$entryp['port']."').disabled=true;
									else document.getElementById('extern-".$entryp['port']."').disabled=false;\">".$entryp['port']."</td>
						<td><input type='checkbox' name='".$entry['port_type']."-ports-extern[]' id='extern-".$entryp['port']."' value='".$entryp['port']."'
						onclick=\"if (this.checked==true) document.getElementById('intern-".$entryp['port']."').disabled=true;
									else document.getElementById('intern-".$entryp['port']."').disabled=false;\">".$entryp['port']."</td></tr>";
			}
			if ($entry['port_type']=="BRI"){
				echo "<tr><td colspan=2><b>".WIZZARD_BRI_EXTERN_PROTOCOL_DIS.": </b><select name='ptp_extern' onmouseover=\"Tip('".ISDNHW_PTP."')\" onmouseout='UnTip()'>
					<option value=1>PTP</option>
					<option value=0>PMP</option>
					</select></td></tr>";
			}
			echo "<tr><td colspan=2>&nbsp;</td></tr>";
		}
	}
	echo "</table>
			<p align='right'>
				<a onclick='javascript:window.location.href=\"index.php?m=wizard&res=yes\";' onmouseover=\"Tip('" . CANCEL . "')\" onmouseout='UnTip()'>
					<img src='".WIZARD_IMAGE_CANCEL_DIS."' onmouseover=\"this.src='".WIZARD_IMAGE_CANCEL_HOVER_DIS."';\" onmouseout=\"this.src='".WIZARD_IMAGE_CANCEL_DIS."';\"/>
	 			</a>
				<a onclick='javascript:document.forms.tk_admin.submit();' onmouseover=\"Tip('" . FINISH . "')\" onmouseout='UnTip()'>
		 			<img src='".WIZARD_IMAGE_FINISH_DIS."' onmouseover=\"this.src='".WIZARD_IMAGE_FINISH_HOVER_DIS."';\" onmouseout=\"this.src='".WIZARD_IMAGE_FINISH_DIS."';\"/>
		 		</a>
	 		</p>
	 	</form>";
}
//TK-Anlage (beroAdmin) speichern
if ($_POST['tk']=="admin" && $_POST['scen']=="tk-admin"){

	//Bei BRI werden die externen ports in einer einzigen gruppe zusammengefasst, externe Ports sind automatisch TE PTP
	if (isset($_POST['BRI-ports-extern'])){
		for ($i=0;$i<count($_POST['BRI-ports-extern']);$i++)
			$ba->update("UPDATE isdn set gr='EXTERN-BRI', ptp='".$_POST['ptp_extern']."', ntteHW='te', ntteSW='te' where port='".$_POST['BRI-ports-extern'][$i]."';");
	}

	//Bei BRI werden die internen ports zu einzelnen Gruppen, interne ports sind automatisch nt und pmp
	if (isset($_POST['BRI-ports-intern'])){
		$j=1;
		for ($i=0;$i<count($_POST['BRI-ports-intern']);$i++)
			$ba->update("UPDATE isdn set gr='INTERN-BRI-".$j++."', ptp='0', ntteHW='nt', ntteSW='nt' where port='".$_POST['BRI-ports-intern'][$i]."';");
	}


	if (isset($_POST['PRI-ports-extern'])){
		for ($i=0;$i<count($_POST['PRI-ports-extern']);$i++)
		$ba->update("UPDATE isdn set gr='EXTERN-PRI', ptp='1', ntteHW='te', ntteSW='te' where port='".$_POST['PRI-ports-extern'][$i]."';");
	}

	//Bei PRI werden auch alle internen Ports zu einer einzigen Gruppe zusammengefasst und sind NT PTP
	if (isset($_POST['PRI-ports-intern'])){
		for ($i=0;$i<count($_POST['PRI-ports-intern']);$i++)
		$ba->update("UPDATE isdn set gr='INTERN-PRI', ptp='1', ntteHW='nt', ntteSW='nt' where port='".$_POST['PRI-ports-intern'][$i]."';");
	}

	//Bei GSM werden alle Ports zu einer externen Gruppe zusammengefasst
	$ba->update("UPDATE gsm set gr='EXTERN-GSM' WHERE port_type='GSM'");

	/*
	 * Bei Analog ist FXS automatisch intern und FXO automatisch extern
	 */

	//Bei FXO werden alle Ports zu einer externen Gruppe zusammengefasst
	$ba->update("UPDATE ANALOG set gr='EXTERN-FXO' WHERE port_type='FXO'");

	//Bei FXS sind alle internen Ports immer einzelne Gruppen
	$queryp=$ba->select("select port from analog where port_type='FXS' order by port");
	$i=1;
	while ($entryp=$ba->fetch_array($queryp)){
		$ba->update("UPDATE ANALOG set gr='INTERN-FXS-".$i++."' WHERE port='".$entryp['port']."'");
	}

	//Dialplan Einträge erzeugen
	$priority=array(0,9,8,7,6,5,4,3,2,1);
	$i=0;
	/*Externe Gruppen und die Gruppe INTERN-PRI erhalten Präfixe von 0,9,8,7,6.. runterzählend.
	 *d.h. angenommen es gibt 2 externe Ports (1x BRI und 1xFXO):
	 *destination=0(.*) -> BRI Gruppe Extern
	 *destination=9(.*) -> FXO Gruppe Extern
	 */
	if (sqlite_num_rows($ba->select("select * from isdn where gr='EXTERN-PRI'"))>0){
		$query=$ba->select("select distinct gr from isdn where gr LIKE 'INTERN%'");
		while ($entry=$ba->fetch_array($query))
		$ba->insert_("insert into dialplan (fromDirection, toDirection,fromId, toId, dest, newDest, src, newSrc, isdnid, posId, config) VALUES ('isdn', 'isdn', 'g:".$entry['gr']."', 'g:EXTERN-PRI', '".$priority[$i]."(.*)', '\\\\1', '(.*)', '\\\\1', '".uniqid("ISDNZUISDN")."','".(sqlite_fetch_single($ba->select("select max(posId) from dialplan"))+1)."','ie_on_sip=1,ec=0,t38=0,unknownprefix=,internationalprefix=,nationalprefix=,privateprefix=,localprefix=')");

		$query=$ba->select("select distinct gr from analog where gr LIKE 'INTERN%'");
		while ($entry=$ba->fetch_array($query))
		$ba->insert_("insert into dialplan (fromDirection, toDirection,fromId, toId, dest, newDest, src, newSrc, isdnid, posId) VALUES ('analog', 'isdn', 'g:".$entry['gr']."', 'g:EXTERN-PRI', '".$priority[$i]."(.*)', '\\\\1', '(.*)', '\\\\1', '".uniqid("ISDNZUISDN")."','".(sqlite_fetch_single($ba->select("select max(posId) from dialplan"))+1)."')");

		$i++;
	}
	if (sqlite_num_rows($ba->select("select * from isdn where gr='EXTERN-BRI'"))>0){
		$query=$ba->select("select distinct gr from isdn where gr LIKE 'INTERN%'");
		while ($entry=$ba->fetch_array($query))
		$ba->insert_("insert into dialplan (fromDirection, toDirection,fromId, toId, dest, newDest, src, newSrc, isdnid, posId, config) VALUES ('isdn', 'isdn', 'g:".$entry['gr']."', 'g:EXTERN-BRI', '".$priority[$i]."(.*)', '\\\\1', '(.*)', '\\\\1', '".uniqid("ISDNZUISDN")."','".(sqlite_fetch_single($ba->select("select max(posId) from dialplan"))+1)."','ie_on_sip=1,ec=0,t38=0,unknownprefix=,internationalprefix=,nationalprefix=,privateprefix=,localprefix=')");

		$query=$ba->select("select distinct gr from analog where gr LIKE 'INTERN%'");
		while ($entry=$ba->fetch_array($query))
		$ba->insert_("insert into dialplan (fromDirection, toDirection,fromId, toId, dest, newDest, src, newSrc, isdnid, posId) VALUES ('analog', 'isdn', 'g:".$entry['gr']."', 'g:EXTERN-BRI', '".$priority[$i]."(.*)', '\\\\1', '(.*)', '\\\\1', '".uniqid("ISDNZUISDN")."','".(sqlite_fetch_single($ba->select("select max(posId) from dialplan"))+1)."')");

		$i++;
	}
	if (sqlite_num_rows($ba->select("select * from analog where gr='EXTERN-FXO'"))>0){
		$query=$ba->select("select distinct gr from isdn where gr LIKE 'INTERN%'");
		while ($entry=$ba->fetch_array($query))
		$ba->insert_("insert into dialplan (fromDirection, toDirection,fromId, toId, dest, newDest, src, newSrc, isdnid, posId) VALUES ('isdn', 'analog', 'g:".$entry['gr']."', 'g:EXTERN-FXO', '".$priority[$i]."(.*)', '\\\\1', '(.*)', '\\\\1', '".uniqid("ISDNZUISDN")."','".(sqlite_fetch_single($ba->select("select max(posId) from dialplan"))+1)."')");

		$query=$ba->select("select distinct gr from analog where gr LIKE 'INTERN%'");
		while ($entry=$ba->fetch_array($query))
		$ba->insert_("insert into dialplan (fromDirection, toDirection,fromId, toId, dest, newDest, src, newSrc, isdnid, posId) VALUES ('analog', 'analog', 'g:".$entry['gr']."', 'g:EXTERN-FXO', '".$priority[$i]."(.*)', '\\\\1', '(.*)', '\\\\1', '".uniqid("ISDNZUISDN")."','".(sqlite_fetch_single($ba->select("select max(posId) from dialplan"))+1)."')");

		$i++;
	}
	if (sqlite_num_rows($ba->select("select * from gsm where gr='EXTERN-GSM'"))>0){
		$query=$ba->select("select distinct gr from isdn where gr LIKE 'INTERN%'");
		while ($entry=$ba->fetch_array($query))
		$ba->insert_("insert into dialplan (fromDirection, toDirection,fromId, toId, dest, newDest, src, newSrc, isdnid, posId) VALUES ('isdn', 'gsm', 'g:".$entry['gr']."', 'g:EXTERN-GSM', '".$priority[$i]."(.*)', '\\\\1', '(.*)', '\\\\1', '".uniqid("ISDNZUISDN")."','".(sqlite_fetch_single($ba->select("select max(posId) from dialplan"))+1)."')");

		$query=$ba->select("select distinct gr from analog where gr LIKE 'INTERN%'");
		while ($entry=$ba->fetch_array($query))
		$ba->insert_("insert into dialplan (fromDirection, toDirection,fromId, toId, dest, newDest, src, newSrc, isdnid, posId) VALUES ('analog', 'gsm', 'g:".$entry['gr']."', 'g:EXTERN-GSM', '".$priority[$i]."(.*)', '\\\\1', '(.*)', '\\\\1', '".uniqid("ISDNZUISDN")."','".(sqlite_fetch_single($ba->select("select max(posId) from dialplan"))+1)."')");

		$i++;
	}
	if (sqlite_num_rows($ba->select("select * from isdn where gr='INTERN-PRI'"))>0){
		$query=$ba->select("select distinct gr from isdn where gr LIKE 'INTERN%' and gr!='INTERN-PRI'");
		while ($entry=$ba->fetch_array($query))
		$ba->insert_("insert into dialplan (fromDirection, toDirection,fromId, toId, dest, newDest, src, newSrc, isdnid, posId, config) VALUES ('isdn', 'isdn', 'g:".$entry['gr']."', 'g:INTERN-PRI', '".$priority[$i]."(.*)', '\\\\1', '(.*)', '\\\\1', '".uniqid("ISDNZUISDN")."','".(sqlite_fetch_single($ba->select("select max(posId) from dialplan"))+1)."','ie_on_sip=1,ec=0,t38=0,unknownprefix=,internationalprefix=,nationalprefix=,privateprefix=,localprefix=')");

		$query=$ba->select("select distinct gr from analog where gr LIKE 'INTERN%'");
		while ($entry=$ba->fetch_array($query))
		$ba->insert_("insert into dialplan (fromDirection, toDirection,fromId, toId, dest, newDest, src, newSrc, isdnid, posId) VALUES ('analog', 'isdn', 'g:".$entry['gr']."', 'g:INTERN-PRI', '".$priority[$i]."(.*)', '\\\\1', '(.*)', '\\\\1', '".uniqid("ISDNZUISDN")."','".(sqlite_fetch_single($ba->select("select max(posId) from dialplan"))+1)."')");

		$i++;
	}

	$offset=array(101,201);
	$i=0;

	/* Jeder BRI Port erhält nun 2 Durchwahlen
	 * destinatination=(101) -> ISDN Port 1
	 * destinatination=(102) -> ISDN Port 1
	 * destinatination=(103) -> ISDN Port 2
	 * destinatination=(104) -> ISDN Port 2
	 */
	$query=$ba->select("select distinct gr from isdn where gr like 'INTERN%' and port_type='BRI'");
	while ($entry=$ba->fetch_array($query)){
		$dest1=$offset[$i]++;
		$dest2=$offset[$i]++;
		$q=$ba->select("select distinct gr from isdn where gr!='".$entry['gr']."' and gr!='NULL'");
		while ($ent=$ba->fetch_array($q)){
			$ba->insert_("insert into dialplan (fromDirection, toDirection,fromId, toId, dest, newDest, src, newSrc, isdnid, posId, config) VALUES ('isdn', 'isdn', 'g:".$ent['gr']."', 'g:".$entry['gr']."', '(".$dest1.")', '\\\\1', '(.*)', '\\\\1', '".uniqid("ISDNZUISDN")."','".(sqlite_fetch_single($ba->select("select max(posId) from dialplan"))+1)."','ie_on_sip=1,ec=0,t38=0,unknownprefix=,internationalprefix=,nationalprefix=,privateprefix=,localprefix=')");
			$ba->insert_("insert into dialplan (fromDirection, toDirection,fromId, toId, dest, newDest, src, newSrc, isdnid, posId, config) VALUES ('isdn', 'isdn', 'g:".$ent['gr']."', 'g:".$entry['gr']."', '(".$dest2.")', '\\\\1', '(.*)', '\\\\1', '".uniqid("ISDNZUISDN")."','".(sqlite_fetch_single($ba->select("select max(posId) from dialplan"))+1)."','ie_on_sip=1,ec=0,t38=0,unknownprefix=,internationalprefix=,nationalprefix=,privateprefix=,localprefix=')");
		}
		$q=$ba->select("select distinct gr from analog where gr!='".$entry['gr']."' and gr!='NULL'");
		while ($ent=$ba->fetch_array($q)){
			$ba->insert_("insert into dialplan (fromDirection, toDirection,fromId, toId, dest, newDest, src, newSrc, isdnid, posId) VALUES ('analog', 'isdn', 'g:".$ent['gr']."', 'g:".$entry['gr']."', '(".$dest1.")', '\\\\1', '(.*)', '\\\\1', '".uniqid("ISDNZUISDN")."','".(sqlite_fetch_single($ba->select("select max(posId) from dialplan"))+1)."')");
			$ba->insert_("insert into dialplan (fromDirection, toDirection,fromId, toId, dest, newDest, src, newSrc, isdnid, posId) VALUES ('analog', 'isdn', 'g:".$ent['gr']."', 'g:".$entry['gr']."', '(".$dest2.")', '\\\\1', '(.*)', '\\\\1', '".uniqid("ISDNZUISDN")."','".(sqlite_fetch_single($ba->select("select max(posId) from dialplan"))+1)."')");
		}
	}

	if (sqlite_num_rows($query)>0) $i++;
	/*Jeder FXS Port erhält 1 Durchwahl
	 * destinatination=(201) -> FXS Port 1
	 */
	$query=$ba->select("select distinct gr from analog where gr like 'INTERN%' and port_type='FXS'");
	while ($entry=$ba->fetch_array($query)){
		$dest=$offset[$i]++;
		$q=$ba->select("select  distinct gr from isdn where gr!='".$entry['gr']."' and gr!='NULL'");
		while ($ent=$ba->fetch_array($q))
		$ba->insert_("insert into dialplan (fromDirection, toDirection,fromId, toId, dest, newDest, src, newSrc, isdnid, posId) VALUES ('isdn', 'analog', 'g:".$ent['gr']."', 'g:".$entry['gr']."', '(".$dest.")', '\\\\1', '(.*)', '\\\\1', '".uniqid("ISDNZUISDN")."','".(sqlite_fetch_single($ba->select("select max(posId) from dialplan"))+1)."')");

		$q=$ba->select("select distinct gr from analog where gr!='".$entry['gr']."' and gr!='NULL'");
		while ($ent=$ba->fetch_array($q))
		$ba->insert_("insert into dialplan (fromDirection, toDirection,fromId, toId, dest, newDest, src, newSrc, isdnid, posId) VALUES ('analog', 'analog', 'g:".$ent['gr']."', 'g:".$entry['gr']."', '(".$dest.")', '\\\\1', '(.*)', '\\\\1', '".uniqid("ISDNZUISDN")."','".(sqlite_fetch_single($ba->select("select max(posId) from dialplan"))+1)."')");

	}
	$query=$ba->select("select option from activate where id='activate'");
	while($entry = $ba->fetch_array($query)){
		if ($entry['option'] <= 3) $query =  $ba->update("UPDATE activate SET option=3 where id='activate'");
	}
	echo "<script>window.location.href=\"./includes/create_files.php?m=state\";</script>";

}
//pstn Network Group Configuration Gateway
if (isset($_POST['sip_name']) && $_POST['scen']=="gw"){

	echo "<table border=0 align='center'>
				<tr>
					<th>".WIZARD_PSTN_NETWORK_GROUP_DIS."</th>
				</tr>
			</table>
			";
	$query=$ba->select("select distinct port_type from isdn union select distinct port_type from analog union select distinct port_type from gsm group by port_type");
	while ($entry=$ba->fetch_array($query)){
		echo "<input type='radio' value='".$entry['port_type']."' name='port_types'
				onclick='for (var i=0;i<document.forms.length;i++) document.forms[i].style.display=\"none\"; document.getElementById(\"pstn_".$entry['port_type']."\").style.display=\"\";'>"
				.$entry['port_type']."<br/>
				";
	}
	$query_=$ba->select("select distinct port_type from isdn union select distinct port_type from analog union select distinct port_type from gsm group by port_type");
	while ($entry_=$ba->fetch_array($query_)){
	echo "<form class='extensions_form' style='display:none;' name='pstn_".$entry_['port_type']."' id='pstn_".$entry_['port_type']."' method='POST'>

			<table border=0 align='center'>
				<tr>
					<td><b>".PSTN_NAME_DIS.":</b>&nbsp;</td>
					<td>&nbsp;<input type='text' name='group' onmouseover=\"Tip('".ISDN_GROUP_NAME."');\" onmouseout='UnTip();'></td>
				</tr>
				<tr>
					<td valign='top'><b>".PSTN_PORTS_DIS.":</b>&nbsp;</td>
				<td>
					<table>
						<tr>
							<td valign='top' colspan='2'>
								<table border='1' style='border-collapse:collapse;' width='100%' onmouseover=\"Tip('".ISDN_PORTS_TABLE."');\" onmouseout='UnTip();'>
									<tr>
										<td width='65'>".title0($ba)."</td>
										<td width='65'>".title1($ba)."</td>
									</tr>
									<tr>
										<td valign='top'>".ports0($ba,$entry_['port_type'])."</td>
										<td valign='top'>".ports1($ba,$entry_['port_type'])."</td>
									</tr>
								</table>
							</td>
						</tr>
					</table>
				</td>
				</tr>";
				$query=$ba->select("select type from isdn group by lif");
				while ($entry=$ba->fetch_array($query)){
					if ($entry['type']=="bf4S0" || $entry['type']=="bf2S02FXS"){
						echo "<tr>
							<td><b>".HARDWARE_PROTOCOL_DIS.":</b></td>
							<td>
								<select name='ptp' onmouseover=\"Tip('".ISDNHW_PTP."')\" onmouseout='UnTip()'>
									<option value=1>PTP</option>
									<option value=0>PMP</option>
								</select>
							</td>
						</tr>";
						break;
					}
				}
				echo "<tr>
					<td colspan='2'>
						<input type='hidden' name='scen' value='".$_POST['scen']."'/>&nbsp;
						<input type='hidden' name='port_type' value='".$entry_['port_type']."'/>&nbsp;
					</td>
				</tr>
	    	</table>
	    	<p align='right'>
				<a onclick='javascript:window.location.href=\"index.php?m=wizard&res=yes\";' onmouseover=\"Tip('" . CANCEL . "')\" onmouseout='UnTip()'>
					<img src='".WIZARD_IMAGE_CANCEL_DIS."' onmouseover=\"this.src='".WIZARD_IMAGE_CANCEL_HOVER_DIS."';\" onmouseout=\"this.src='".WIZARD_IMAGE_CANCEL_DIS."';\"/>
	 			</a>
				<a onclick='javascript:if (is_not_empty(document.forms.pstn_".$entry_['port_type'].".group.value,\"group name\")) document.forms.pstn_".$entry_['port_type'].".submit();' onmouseover=\"Tip('" . NEXT . "')\" onmouseout='UnTip()'>
		 			<img src='".WIZARD_IMAGE_NEXT_DIS."' onmouseover=\"this.src='".WIZARD_IMAGE_NEXT_HOVER_DIS."';\" onmouseout=\"this.src='".WIZARD_IMAGE_NEXT_DIS."';\"/>
		 		</a>
	 		</p>
		</form>";
	}
}

//pstn Eintrag speichern (Gateway) und Uebersicht anzeigen
if (isset($_POST['group']) && $_POST['scen']=="gw"){
	//PSTN Eintrag
	$port_type=$_POST['port_type'];
	$gr=$_POST['group'];
	$ports=$_POST['ports'];
	$new_ports=$_POST['new_ports'];
	$chansel='standard';
	$tones='[de]';
	$odt=3;
	$odt_empty_dad=15;
	$ptp=$_POST['ptp'];
	$port="";
	if ($port_type=="FXS" || $port_type=="FXO"){
		for ($i=0;$i<count($ports);$i++){
			$port = $ports[$i];
			$query_new = $ba->update("UPDATE analog SET gr='$gr' WHERE port='$port'");
		}
		$new_port="";
		for ($i=0;$i<count($new_ports);$i++){
			$new_port= $new_ports[$i];
			$query_new = $ba->update("UPDATE isdn SET gr='NULL', config='' WHERE port='$new_port'");
		}
	}elseif ($port_type=="GSM"){
		for ($i=0;$i<count($ports);$i++){
			$port = $ports[$i];
			$query_new = $ba->update("UPDATE gsm SET gr='$gr' WHERE port='$port'");
		}
		$new_port="";
		for ($i=0;$i<count($new_ports);$i++){
			$new_port= $new_ports[$i];
			$query_new = $ba->update("UPDATE gsm SET gr='NULL', config='' WHERE port='$new_port'");
		}
	}else{
		$query_new = $ba->update("UPDATE isdn SET ptp='$ptp' where type='bf4S0' or type='bf2S02FXS'");
		for ($i=0;$i<count($ports);$i++){
			$port = $ports[$i];
			$query_new = $ba->update("UPDATE isdn SET gr='$gr', odt='$odt', odt_empty_dad='$odt_empty_dad', chansel='$chansel', tones='$tones' WHERE port='$port'");
		}
		$new_port="";
		for ($i=0;$i<count($new_ports);$i++){
			$new_port= $new_ports[$i];
			$query_new = $ba->update("UPDATE isdn SET gr='NULL', config='', odt='3', odt_empty_dad='15', chansel='standard', tones='[de]' WHERE port='$new_port'");
		}
	}
	$query=$ba->select("select option from activate where id='activate'");
	while($entry = $ba->fetch_array($query)){
		if ($entry['option'] <= 3) $query =  $ba->update("UPDATE activate SET option=3 where id='activate'");
	}


	//Dialplan Eintraege

	#Sip->pstn
	$fromDirection="sip";
	if ($port_type=="FXS" || $port_type=="FXO")	$toDirection="analog";
	elseif ($port_type=="GSM")	$toDirection="gsm";
	else $toDirection="isdn";
	$query=$ba->select("select name from sip");
	$fromid="p:".@sqlite_fetch_single($query);
	$toid="g:".$gr;
	$dest="(.*)";
	$newdest="\\\\1";
	$src="(.*)";
	$newsrc="\\\\1";
	$query_new = $ba->insert_("INSERT INTO dialplan (fromDirection, toDirection, fromId, toId, dest, newDest, src, newSrc) VALUES ('$fromDirection', '$toDirection', '$fromid','$toid', '$dest', '$newdest', '$src', '$newsrc')");
	#pstn->Sip
	if ($port_type=="FXS" || $port_type=="FXO")	$fromDirection="analog";
	elseif ($port_type=="GSM")	$fromDirection="gsm";
	else $fromDirection="isdn";
	$toDirection="sip";
	$query_new = $ba->insert_("INSERT INTO dialplan (fromDirection, toDirection, fromId, toId, dest, newDest, src, newSrc) VALUES ('$fromDirection', '$toDirection', '$toid', '$fromid', '$dest', '$newdest', '$src', '$newsrc')");


	echo "<table border='0' align='left' width='100%'>
			<tr>
				<td><b>".WIZARD_SIP_ENTRY_DIS.":</b></td>
		  	</tr>
		  	<tr>
		  		<td>";
	$query = $ba->select('SELECT * FROM sip ORDER BY id asc');
	while ($entry = $ba->fetch_array($query)) {
		echo SIP_NAME_DIS.": ".$entry['name']."<br/>";
		echo SIP_SERVER_ADDRESS_DIS.": ".$entry['server_address']."<br/>";
		echo SIP_USER_DIS.": ".$entry['user']."<br/>";
		echo SIP_SECRET_DIS.": ".$entry['secret']."<br/>";
		echo SIP_REGISTER_DIS.": ".$entry['type']."<br/>";
	}

	echo "</td>
		</tr>
		<tr>
			<td>&nbsp;</td>
		</tr>
		<tr>
			<td><b>".WIZARD_PSTN_ENTRY_DIS.":</b></td>
		</tr>
		<tr>
			<td>";
	$query = $ba->select('SELECT DISTINCT gr FROM isdn WHERE gr!="NULL" union SELECT DISTINCT gr FROM analog WHERE gr!="NULL" union SELECT DISTINCT gr FROM gsm WHERE gr!="NULL"');
	while ($entry = $ba->fetch_array($query)) {
		if ($port_type=="FXS" || $port_type=="FXO") $query_port = $ba->select('SELECT port FROM analog WHERE gr="'.$entry['gr'].'" ORDER BY port asc');
		elseif ($port_type=="GSM") $query_port = $ba->select('SELECT port FROM gsm WHERE gr="'.$entry['gr'].'" ORDER BY port asc');
		else $query_port = $ba->select('SELECT port FROM isdn WHERE gr="'.$entry['gr'].'" ORDER BY port asc');
		$ports="";
		while ($entryp = $ba->fetch_array($query_port)) {
			if($ports!=""){
				$ports.=",".$entryp['port'];
			}else{
				$ports.=$entryp['port'];
			}
		}
		echo PSTN_NAME_DIS.": ".$entry['gr']."<br/>";
		echo PSTN_PORTS_DIS.": ".$ports."<br/>";
	}

	echo "</td>
		</tr>
		<tr>
			<td>&nbsp;</td>
		</tr>
		<tr>
			<td><b>".WIZARD_DIALPLAN_ENTRIES_DIS.":</b></td>
		</tr>
		<tr>
			<td>";
	$query = $ba->select(' SELECT * from dialplan ORDER BY id asc');
	while ($entry = $ba->fetch_array($query)) {
		echo DIALPLAN_FROM_DIRECTION_DIS.": ".$entry['fromDirection']."<br/>";
		echo DIALPLAN_TO_DIRECTION_DIS.": ".$entry['toDirection']."<br/>";
		echo DIALPLAN_FROM_ID_DIS.": ".$entry['fromId']."<br/>";
		echo DIALPLAN_TO_ID_DIS.": ".$entry['toId']."<br/>";
		echo DIALPLAN_DESTINATION_DIS.": ".stripslashes($entry['dest'])."<br/>";
		echo DIALPLAN_NEW_DESTINATION_DIS.": ".stripslashes($entry['newDest'])."<br/>";
		echo DIALPLAN_SOURCE_DIS.": ".stripslashes($entry['src'])."<br/>";
		echo DIALPLAN_NEW_SOURCE_DIS.": ".stripslashes($entry['newSrc'])."<br/></td></tr><tr><td>&nbsp;</td></tr><tr><td>";
	}
	echo "</td></tr></table>
		<p align='right'>
			<a onclick='javascript:window.location.href=\"index.php?m=wizard&res=yes\";' onmouseover=\"Tip('" . CANCEL . "')\" onmouseout='UnTip()'>
				<img src='".WIZARD_IMAGE_CANCEL_DIS."' onmouseover=\"this.src='".WIZARD_IMAGE_CANCEL_HOVER_DIS."';\" onmouseout=\"this.src='".WIZARD_IMAGE_CANCEL_DIS."';\"/>
			</a>
			<a onclick='javascript:window.location.href=\"./includes/create_files.php?m=state\";' onmouseover=\"Tip('" . FINISH . "')\" onmouseout='UnTip()'>
	 			<img src='".WIZARD_IMAGE_FINISH_DIS."' onmouseover=\"this.src='".WIZARD_IMAGE_FINISH_HOVER_DIS."';\" onmouseout=\"this.src='".WIZARD_IMAGE_FINISH_DIS."';\"/>
	 		</a>
		</p>";

}
print "<hr size= \"1\" noshade>";
print "<br><a href=\"http://www.beronet.com\" target=\"_blank\" onfocus=\"if (this.blur()) { this.blur(); }\"><img src=\"./includes/images/beroLogo.gif\" align=\"right\" border=\"0\"></a>";
print "</center></td></tr>
<tr>
<td colspan=\"2\" style=\"background-repeat:no-repeat\" background=\"./includes/images/bg_bottom.png\"><img src=\"./includes/images/trans.gif\" border=\"0\" height=\"39\" width=\"10\"><div style=\"color: white;\" align=\"right\">&nbsp;&nbsp;&nbsp;</div>
</td>
</tr>
</table>
</body>
</html>";
}
}
?>
