<?php
function ec($table="isdn"){
	if (isset($_GET['gr'])){
		$ba=new beroAri();
		$query=$ba->select("select config from ".$table." where gr='".$_GET['gr']."' group by gr");
		while  ($entry = $ba->fetch_array($query)) {
			$config=$entry['config'];
		}
		if ($config!=""){
			$configs=split(',',$config);
			foreach ($configs as $value){
				$key=split('=',$value);
				if ($key[0]=="ec" && $key[1]==1) return "<input type='checkbox' id='ec' name='ec' value='1' checked>";
				elseif ($key[0]=="ec" && $key[1]==0) return "<input type='checkbox' id='ec' name='ec' value='1'>";
			}
			return "<input type='checkbox' name='ec' id='ec' value='1' checked>";
		}else{
			return "<input type='checkbox' name='ec' id='ec' value='1' checked>";
		}
	}else{
		return "<input type='checkbox' name='ec' id='ec' value='1' checked>";
	}
}

function eao(){
	if (isset($_GET['gr'])){
		$ba=new beroAri();
		$query=$ba->select("select config from isdn where gr='".$_GET['gr']."' group by gr");
		while  ($entry = $ba->fetch_array($query)) {
			$config=$entry['config'];
		}
		if ($config!=""){
			$configs=split(',',$config);
			foreach ($configs as $value){
				$key=split('=',$value);
				if ($key[0]=="eao" && $key[1]==1) return "<input type='checkbox' id='eao' name='eao' value='1' checked>";
				elseif ($key[0]=="eao" && $key[1]==0) return "<input type='checkbox' id='eao' name='eao' value='1'>";
			}
			return "<input type='checkbox' name='eao' id='eao' value='1'>";
		}else{
			return "<input type='checkbox' name='eao' id='eao' value='1'>";
		}
	}else{
		return "<input type='checkbox' name='eao' id='eao' value='1'>";
	}
}

function ignorep8(){
	if (isset($_GET['gr'])){
		$ba=new beroAri();
		$query=$ba->select("select config from isdn where gr='".$_GET['gr']."' group by gr");
		while  ($entry = $ba->fetch_array($query)) {
			$config=$entry['config'];
		}
		if ($config!=""){
			$configs=split(',',$config);
			foreach ($configs as $value){
				$key=split('=',$value);
				if ($key[0]=="ignorep8" && $key[1]==1) return "<input type='checkbox' id='ignorep8' name='ignorep8' value='1' checked>";
				elseif ($key[0]=="ignorep8" && $key[1]==0) return "<input type='checkbox' id='ignorep8' name='ignorep8' value='1'>";
			}
			return "<input type='checkbox' name='ignorep8' id='ignorep8' value='1' checked>";
		}else{
			return "<input type='checkbox' name='ignorep8' id='ignorep8' value='1' checked>";
		}
	}else{
		return "<input type='checkbox' name='ignorep8' id='ignorep8' value='1' checked>";
	}
}

function gen_ring_eao(){
	if (isset($_GET['gr'])){
		$ba=new beroAri();
		$query=$ba->select("select config from isdn where gr='".$_GET['gr']."' group by gr");
		while  ($entry = $ba->fetch_array($query)) {
			$config=$entry['config'];
		}
		if ($config!=""){
			$configs=split(',',$config);
			foreach ($configs as $value){
				$key=split('=',$value);
				if ($key[0]=="gen_ring_eao" && $key[1]==1) return "<input type='checkbox' id='gen_ring_eao' name='gen_ring_eao' value='1' checked>";
				elseif ($key[0]=="gen_ring_eao" && $key[1]==0) return "<input type='checkbox' id='gen_ring_eao' name='gen_ring_eao' value='1'>";
			}
			return "<input type='checkbox' name='gen_ring_eao' id='gen_ring_eao' value='1'>";
		}else{
			return "<input type='checkbox' name='gen_ring_eao' id='gen_ring_eao' value='1'>";
		}
	}else{
		return "<input type='checkbox' name='gen_ring_eao' id='gen_ring_eao' value='1'>";
	}
}

function cd(){
	if (isset($_GET['gr'])){
		$ba=new beroAri();
		$query=$ba->select("select config from isdn where gr='".$_GET['gr']."' group by gr");
		while  ($entry = $ba->fetch_array($query)) {
			$config=$entry['config'];
		}
		if ($config!=""){
			$configs=split(',',$config);
			foreach ($configs as $value){
				$key=split('=',$value);
				if ($key[0]=="cd" && $key[1]==1) return "<input type='checkbox' id='cd' name='cd' value='1' checked>";
				elseif ($key[0]=="cd" && $key[1]==0) return "<input type='checkbox' id='cd' name='cd' value='1'>";
			}
			return "<input type='checkbox' name='cd' id='cd' value='1'>";
		}else{
			return "<input type='checkbox' name='cd' id='cd' value='1'>";
		}
	}else{
		return "<input type='checkbox' name='cd' id='cd' value='1'>";
	}
}

function ec_tail($table="isdn"){
	if (isset($_GET['gr'])){
		$ba=new beroAri();
		$query=$ba->select("select config from ".$table." where gr='".$_GET['gr']."' group by gr");
		while  ($entry = $ba->fetch_array($query)) {
			$config=$entry['config'];
		}
		if ($config!=""){
			$configs=split(',',$config);
			foreach ($configs as $value){
				$key=split('=',$value);
				if ($key[0]=="ectl") return "<input type='text' name='ectl' id='ectl' value='".stripslashes($key[1])."' size='7'>";
			}
		}
		return "<input type='text' name='ectl' id='ectl' value='15' size='7'>";
	}else{
		return "<input type='text' name='ectl' id='ectl' value='15' size='7'>";
	}
}

function featurecodes(){
	if (isset($_GET['gr'])){
		$ba=new beroAri();
		$query=$ba->select("select config from isdn where gr='".$_GET['gr']."' group by gr");
		while  ($entry = $ba->fetch_array($query)) {
			$config=$entry['config'];
		}
		if ($config!=""){
			$configs=split(',',$config);
			foreach ($configs as $value){
				$key=split('=',$value);
				if ($key[0]=="featurecodes") return "<input type='text' name='featurecodes' id='featurecodes' value='".stripslashes($key[1])."' size='15'>";
			}
		}
		return "<input type='text' name='featurecodes' id='featurecodes' value='' size='15'>";
	}else{
		return "<input type='text' name='featurecodes' id='featurecodes' value='' size='15'>";
	}
}


function clir_on_oad($table){
		if (isset($_GET['gr'])){
		$ba=new beroAri();
		$query=$ba->select("select config from ".$table." where gr='".$_GET['gr']."' group by gr");
		while  ($entry = $ba->fetch_array($query)) {
			$config=$entry['config'];
		}
		if ($config!=""){
			$configs=split(',',$config);
			foreach ($configs as $value){
				$key=split('=',$value);
				if ($key[0]=="clir_on_oad") return "<input type='text' name='clir_on_oad' id='clir_on_oad' value='".stripslashes($key[1])."' size='7'>";
			}
		}
		return "<input type='text' name='clir_on_oad' id='clir_on_oad' size='7'>";
	}else{
		return "<input type='text' name='clir_on_oad' id='clir_on_oad' size='7'>";
	}
}


function ie_on_sip(){
	if (isset($_GET['id'])){
		$ba=new beroAri();
		$query=$ba->select("select config from sip where id='".$_GET['id']."'");
		while  ($entry = $ba->fetch_array($query)) {
			$config=$entry['config'];
		}
		if ($config!=""){
			$configs=split(',',$config);
			foreach ($configs as $value){
				$key=split('=',$value);
				if ($key[0]=="ie_on_sip" && $key[1]==1) return "<input type='checkbox' name='ie_on_sip' id='ie_on_sip' value='1' checked>";
				elseif ($key[0]=="ie_on_sip" && $key[1]==0) return "<input type='checkbox' name='ie_on_sip' id='ie_on_sip' value='1'>";
			}
			return "<input type='checkbox' name='ie_on_sip' id='ie_on_sip' value='1' >";
		}else{
			return "<input type='checkbox' name='ie_on_sip' id='ie_on_sip' value='1' >";
		}
	}else{
		return "<input type='checkbox' name='ie_on_sip' id='ie_on_sip' value='1' >";
	}
}

function allow_sip_183_without_sdp(){
	if (isset($_GET['id'])){
		$ba=new beroAri();
		$query=$ba->select("select config from sip where id='".$_GET['id']."'");
		while  ($entry = $ba->fetch_array($query)) {
			$config=$entry['config'];
		}
		if ($config!=""){
			$configs=split(',',$config);
			foreach ($configs as $value){
				$key=split('=',$value);
				if ($key[0]=="allow_sip_183_without_sdp" && $key[1]==1) return "<input type='checkbox' name='allow_sip_183_without_sdp' id='allow_sip_183_without_sdp' value='1' checked>";
				elseif ($key[0]=="allow_sip_183_without_sdp" && $key[1]==0) return "<input type='checkbox' name='allow_sip_183_without_sdp' id='allow_sip_183_without_sdp' value='1'>";
			}
			return "<input type='checkbox' name='allow_sip_183_without_sdp' id='allow_sip_183_without_sdp' value='1' checked>";
		}else{
			return "<input type='checkbox' name='allow_sip_183_without_sdp' id='allow_sip_183_without_sdp' value='1' checked>";
		}
	}else{
		return "<input type='checkbox' name='allow_sip_183_without_sdp' id='allow_sip_183_without_sdp' value='1' checked>";
	}
}

function wait_for_cancel(){
	if (isset($_GET['id'])){
		$ba=new beroAri();
		$query=$ba->select("select config from sip where id='".$_GET['id']."'");
		while  ($entry = $ba->fetch_array($query)) {
			$config=$entry['config'];
		}
		if ($config!=""){
			$configs=split(',',$config);
			foreach ($configs as $value){
				$key=split('=',$value);
				if ($key[0]=="wait_for_cancel" && $key[1]==1) return "<input type='checkbox' name='wait_for_cancel' id='wait_for_cancel' value='1' checked>";
				elseif ($key[0]=="wait_for_cancel" && $key[1]==0) return "<input type='checkbox' name='wait_for_cancel' id='wait_for_cancel' value='1'>";
			}
			return "<input type='checkbox' name='wait_for_cancel' id='wait_for_cancel' value='1'>";
		}else{
			return "<input type='checkbox' name='wait_for_cancel' id='wait_for_cancel' value='1'>";
		}
	}else{
		return "<input type='checkbox' name='wait_for_cancel' id='wait_for_cancel' value='1'>";
	}
}

function ea(){
	if (isset($_GET['id'])){
		$ba=new beroAri();
		$query=$ba->select("select config from sip where id='".$_GET['id']."'");
		while  ($entry = $ba->fetch_array($query)) {
			$config=$entry['config'];
		}
		if ($config!=""){
			$configs=split(',',$config);
			foreach ($configs as $value){
				$key=split('=',$value);
				if ($key[0]=="ea" && $key[1]==1) return "<input type='checkbox' name='ea' id='ea' value='1' checked>";
				elseif ($key[0]=="ea" && $key[1]==0) return "<input type='checkbox' name='ea' id='ea' value='1'>";
			}
			return "<input type='checkbox' name='ea' id='ea' value='1' checked>";
		}else{
			return "<input type='checkbox' name='ea' id='ea' value='1' checked>";
		}
	}else{
		return "<input type='checkbox' name='ea' id='ea' value='1' checked>";
	}
}

function t38(){
	if (isset($_GET['id'])){
		$ba=new beroAri();
		$query=$ba->select("select config from sip where id='".$_GET['id']."'");
		while  ($entry = $ba->fetch_array($query)) {
			$config=$entry['config'];
		}
		if ($config!=""){
			$configs=split(',',$config);
			foreach ($configs as $value){
				$key=split('=',$value);
				if ($key[0]=="t38" && $key[1]==1) return "<input type='checkbox' name='t38' id='t38' value='1' checked>";
				elseif ($key[0]=="t38" && $key[1]==0) return "<input type='checkbox' name='t38' id='t38' value='1'>";
			}
			return "<input type='checkbox' name='t38' id='t38' value='1' checked>";
		}else{
			return "<input type='checkbox' name='t38' id='t38' value='1' checked>";
		}
	}else{
		return "<input type='checkbox' name='t38' id='t38' value='1' checked>";
	}
}

function dtmfmode(){
	$sel="<select name='dtmfmode' id='dtmfmode'>
				<option value='info'>info</option>
				<option value='inband'>inband</option>
				<option value='rfc2833' selected>rfc2833</option>
			</select>";
	if (isset($_GET['id'])){
		$ba=new beroAri();
		$query=$ba->select("select config from sip where id='".$_GET['id']."'");
		while  ($entry = $ba->fetch_array($query)) {
			$config=$entry['config'];
		}
		if ($config!=""){
			$configs=split(',',$config);
			foreach ($configs as $value){
				$key=split('=',$value);
				if ($key[0]=="dtmfmode"){
					$sel="<select name='dtmfmode' id='dtmfmode'>";
					($key[1]=="info") ? $sel.="<option value='info' selected>info</option>" : $sel.="<option value='info'>info</option>";
					($key[1]=="inband") ? $sel.="<option value='inband' selected>inband</option>" : $sel.="<option value='inband'>inband</option>";
					($key[1]=="rfc2833") ? $sel.="<option value='rfc2833' selected>rfc2833</option>" : $sel.="<option value='rfc2833'>rfc2833</option>";
					$sel.="</select>";
					return $sel;
				}
			}
		}
		return $sel;
	}else{
		return $sel;
	}
}

function failover_timeout(){
	$sel="<select name='failover_timeout' id='failover_timeout'>";
				for ($i=0;$i<=60;$i++){
					if ($i==0)  $sel.="<option value='$i' selected>$i</option>";
					else $sel.="<option value='$i'>$i</option>";
				}
			"</select>";
	if (isset($_GET['id'])){
		$ba=new beroAri();
		$query=$ba->select("select config from sip where id='".$_GET['id']."'");
		while  ($entry = $ba->fetch_array($query)) {
			$config=$entry['config'];
		}
		if ($config!=""){
			$configs=split(',',$config);
			foreach ($configs as $value){
				$key=split('=',$value);
				if ($key[0]=="failover_timeout"){
					$sel="<select name='failover_timeout' id='failover_timeout'>";
					for ($i=0;$i<=60;$i++){
						if ($i==$key[1])  $sel.="<option value='$i' selected>$i</option>";
						else $sel.="<option value='$i'>$i</option>";
					}
					$sel.="</select>";
					return $sel;
				}
			}
		}
		return $sel;
	}

	return $sel;

}

function failover_proxy(){
	$ba=new beroAri();
	$select="<select name='failover_proxy' id='failover_proxy'>";
	if ($_GET['id']==""){
		$select.="<option value=''>&nbsp;</option>";
		$query=$ba->select("SELECT name from sip");
			while ($entry=$ba->fetch_array($query)){
				$select.="<option value='".$entry['name']."'>".$entry['name']."</option>";
			}
	}else{
		$query=$ba->select("select config from sip where id='".$_GET['id']."'");
		while  ($entry = $ba->fetch_array($query)) {
			$config=$entry['config'];
		}
		$failover_proxy="";
		if ($config!=""){
			$configs=split(',',$config);
			foreach ($configs as $value){
				$key=split('=',$value);
				if ($key[0]=="failover_proxy"){
					$failover_proxy=$key[1];
				}
			}
		}
		$select.="<option value=''>&nbsp;</option>";
		$query=$ba->select("SELECT id, name from sip");
			while ($entry=$ba->fetch_array($query)){
				if ($entry['id']!=$_GET['id']){
					if ($entry['name']==$failover_proxy){
						$select.="<option value='".$entry['name']."' selected>".$entry['name']."</option>";
					}else{
						$select.="<option value='".$entry['name']."'>".$entry['name']."</option>";
					}
				}
			}
	}
	$select.="</select>";
	return $select;

}

function s2i_cpt(){
	$ba=new beroAri();
	$select="<select name='s2i_cpt' id='s2i_cpt'>";
	if ($_GET['id']==""){
		$select.="<option value=''>&nbsp;</option>";
		$query=$ba->select("SELECT name from callprogress where direction='SIP-ISDN'");
			while ($entry=$ba->fetch_array($query)){
				$select.="<option value='".$entry['name']."'>".$entry['name']."</option>";
			}
	}else{
		$query=$ba->select("select config from sip where id='".$_GET['id']."'");
		while  ($entry = $ba->fetch_array($query)) {
			$config=$entry['config'];
		}
		$s2i_cpt="";
		if ($config!=""){
			$configs=split(',',$config);
			foreach ($configs as $value){
				$key=split('=',$value);
				if ($key[0]=="s2i_cpt"){
					$s2i_cpt=$key[1];
				}
			}
		}
		$select.="<option value=''>&nbsp;</option>";
		$query=$ba->select("SELECT name from callprogress where direction='SIP-ISDN'");
			while ($entry=$ba->fetch_array($query)){
					if ($entry['name']==$s2i_cpt){
						$select.="<option value='".$entry['name']."' selected>".$entry['name']."</option>";
					}else{
						$select.="<option value='".$entry['name']."'>".$entry['name']."</option>";
					}
			}
	}
	$select.="</select>";
	return $select;

}

function i2s_cpt(){
	$ba=new beroAri();
	$select="<select name='i2s_cpt' id='i2s_cpt'>";
	if ($_GET['id']==""){
		$select.="<option value=''>&nbsp;</option>";
		$query=$ba->select("SELECT name from callprogress where direction='ISDN-SIP'");
			while ($entry=$ba->fetch_array($query)){
				$select.="<option value='".$entry['name']."'>".$entry['name']."</option>";
			}
	}else{
		$query=$ba->select("select config from sip where id='".$_GET['id']."'");
		while  ($entry = $ba->fetch_array($query)) {
			$config=$entry['config'];
		}
		$i2s_cpt="";
		if ($config!=""){
			$configs=split(',',$config);
			foreach ($configs as $value){
				$key=split('=',$value);
				if ($key[0]=="i2s_cpt"){
					$i2s_cpt=$key[1];
				}
			}
		}
		$select.="<option value=''>&nbsp;</option>";
		$query=$ba->select("SELECT name from callprogress where direction='ISDN-SIP'");
			while ($entry=$ba->fetch_array($query)){
					if ($entry['name']==$i2s_cpt){
						$select.="<option value='".$entry['name']."' selected>".$entry['name']."</option>";
					}else{
						$select.="<option value='".$entry['name']."'>".$entry['name']."</option>";
					}
			}
	}
	$select.="</select>";
	return $select;

}

function caller_id_mapping_sip($name){
	$sel="<select name='".$name."' id='".$name."' ".
		"onchange='if (this.selectedIndex==6) document.getElementById(\"".$name."_manual\").style.display=\"\"; ".
		"else document.getElementById(\"".$name."_manual\").style.display=\"none\";'>";
		($name=="sip_from_user_setting" || $name=="sip_from_display_setting") ? $sel.="<option value='\${new_source}' selected>new_source</option>" : $sel.="<option value='\${new_source}'>new_source</option>";
		$sel.="	<option value='\${oad}'>oad</option>";
		($name=="sip_p_asserted_identity_setting") ? $sel.="<option value='\${oad2}' selected>oad2</option> " : $sel.="<option value='\${oad2}'>oad2</option>";
		$sel.=" <option value='\${qsigname}'>qsigname</option>
				<option value='\${account_username}'>account_username</option>
				<option value=''>none</option>
				<option value='manual'>manual</option>
			</select><br>
			<input type='text' name='".$name."_manual' id='".$name."_manual' style='display:none;'>";
	if (isset($_GET['id'])){
		$ba=new beroAri();
		$query=$ba->select("select config from sip where id='".$_GET['id']."'");
		while  ($entry = $ba->fetch_array($query)) {
			$config=$entry['config'];
		}
		if ($config!=""){
			$configs=split(',',$config);
			foreach ($configs as $value){
				$key=split('=',$value);
				if ($key[0]==$name){
					$sel="<select name='".$name."' id='".$name."' ".
						"onchange='if (this.selectedIndex==6) document.getElementById(\"".$name."_manual\").style.display=\"\"; ".
						"else document.getElementById(\"".$name."_manual\").style.display=\"none\";'>";
					($key[1]=="\${new_source}") ? $sel.="<option value='\${new_source}' selected>new_source</option>" : $sel.="<option value='\${new_source}'>new_source</option>";
					($key[1]=="\${oad}") ? $sel.="<option value='\${oad}' selected>oad</option>" : $sel.="<option value='\${oad}'>oad</option>";
					($key[1]=="\${oad2}") ? $sel.="<option value='\${oad2}' selected>oad2</option>" : $sel.="<option value='\${oad2}'>oad2</option>";
					($key[1]=="\${qsigname}") ? $sel.="<option value='\${qsigname}' selected>qsigname</option>" : $sel.="<option value='\${qsigname}'>qsigname</option>";
					($key[1]=="\${account_username}") ? $sel.="<option value='\${account_username}' selected>account_username</option>" : $sel.="<option value='\${account_username}'>account_username</option>";
					($key[1]=="") ? $sel.="<option value='' selected>none</option>" : $sel.="<option value=''>none</option>";
					($key[1]!="\${qsigname}" && $key[1]!="\${account_username}" && $key[1]!="\${new_source}" && $key[1]!="\${oad2}" && $key[1]!="\${oad}" && $key[1]!="") ? $sel.="<option value='manual' selected>manual</option>" : $sel.="<option value='manual'>manual</option>";
					$sel.="</select><br>";
					($key[1]!="\${qsigname}" && $key[1]!="\${account_username}" && $key[1]!="\${new_source}" && $key[1]!="\${oad2}" && $key[1]!="\${oad}" && $key[1]!="") ? $sel.="<input type='text' name='".$name."_manual' id='".$name."_manual' value='".$key['1']."'>" : $sel.="<input type='text' name='".$name."_manual' id='".$name."_manual' style='display:none;'>";
					return $sel;
				}
			}
		}
		return $sel;
	}else{
		return $sel;
	}
}

function from_sip_src_setting(){
	$sel="<select name='from_sip_src_setting' id='from_sip_src_setting'>
			<option value='from_user' selected>from_user</option>
			<option value='from_display'>from_display</option>
			<option value='pai_all'>pai_all</option>
			<option value='pai_user'>pai_user</option>
			<option value='pai_display'>pai_display</option>
			<option value='ppi_all'>ppi_all</option>
			<option value='ppi_user'>ppi_user</option>
			<option value='ppi_display'>ppi_display</option>
			<option value='rpi_all'>rpi_all</option>
			<option value='rpi_user'>rpi_user</option>
			<option value='rpi_display'>rpi_display</option>
		</select>";
	if (isset($_GET['id'])){
		$ba=new beroAri();
		$query=$ba->select("select config from sip where id='".$_GET['id']."'");
		while  ($entry = $ba->fetch_array($query)) {
			$config=$entry['config'];
		}

		if ($config!=""){
			$configs=split(',',$config);
			foreach ($configs as $value){
				$key=split('=',$value);
				if ($key[0]=='from_sip_src_setting'){
					$sel="<select name='from_sip_src_setting' id='from_sip_src_setting'>";
					($key[1]=="from_user") ? $sel.="<option value='from_user' selected>from_user</option>" : $sel.="<option value='from_user'>from_user</option>";
					($key[1]=="from_display") ? $sel.="<option value='from_display' selected>from_display</option>" : $sel.="<option value='from_display'>from_display</option>";
					($key[1]=="pai_all") ? $sel.="<option value='pai_all' selected>pai_all</option>" : $sel.="<option value='pai_all'>pai_all</option>";
					($key[1]=="pai_user") ? $sel.="<option value='pai_user' selected>pai_user</option>" : $sel.="<option value='pai_user'>pai_user</option>";
					($key[1]=="pai_display") ? $sel.="<option value='pai_display' selected>pai_display</option>" : $sel.="<option value='pai_display'>pai_display</option>";
					($key[1]=="ppi_all") ? $sel.="<option value='ppi_all' selected>ppi_all</option>" : $sel.="<option value='ppi_all'>ppi_all</option>";
					($key[1]=="ppi_user") ? $sel.="<option value='ppi_user' selected>ppi_user</option>" : $sel.="<option value='ppi_user'>ppi_user</option>";
					($key[1]=="ppi_display") ? $sel.="<option value='ppi_display' selected>ppi_display</option>" : $sel.="<option value='ppi_display'>ppi_display</option>";
					($key[1]=="rpi_all") ? $sel.="<option value='rpi_all' selected>rpi_all</option>" : $sel.="<option value='rpi_all'>rpi_all</option>";
					($key[1]=="rpi_user") ? $sel.="<option value='rpi_user' selected>rpi_user</option>" : $sel.="<option value='rpi_user'>rpi_user</option>";
					($key[1]=="rpi_display") ? $sel.="<option value='rpi_display' selected>rpi_display</option>" : $sel.="<option value='rpi_display'>rpi_display</option>";
					$sel.="</select>";
					return $sel;
				}
			}
		}
		return $sel;
	}else{
		return $sel;
	}
}


function caller_id_mapping_pstn($name,$table='isdn'){
	$sel="<select name='".$name."' id='".$name."' ".
		"onchange='if (this.selectedIndex==13) document.getElementById(\"".$name."_manual\").style.display=\"\"; ".
		"else document.getElementById(\"".$name."_manual\").style.display=\"none\";'>";
	($name=="isdn_oad_setting" || $name=="analog_oad_setting") ? $sel.="<option value='\${new_source}' selected>new_source</option>" : $sel.="<option value='\${new_source}'>new_source</option>";
		$sel.="	<option value='\${from_user}'>from_user</option>
				<option value='\${from_display}'>from_display</option>
				<option value='\${pai_all}'>pai_all</option>
				<option value='\${pai_user}'>pai_user</option>
				<option value='\${pai_display}'>pai_display</option>
				<option value='\${ppi_all}'>ppi_all</option>
				<option value='\${ppi_user}'>ppi_user</option>
				<option value='\${ppi_display}'>ppi_display</option>
				<option value='\${rpi_all}'>rpi_all</option>
				<option value='\${rpi_user}'>rpi_user</option>
				<option value='\${rpi_display}'>rpi_display</option>";
				($name=="isdn_oad2_setting" || $name=="isdn_qsigname_setting" || $name=="isdn_redirected_nr_setting" || $name=="analog_display_setting")? $sel.="<option value='' selected>none</option>" : $sel.="<option value=''>none</option>";
		$sel.="<option value='manual'>manual</option>";
		$sel.="</select><br>";
		$sel.="<input type='text' name='".$name."_manual' id='".$name."_manual' style='display:none;'>";
	if (isset($_GET['gr'])){
		$ba=new beroAri();
		$query=$ba->select("select config from ".$table." where gr='".$_GET['gr']."' group by gr");
		while  ($entry = $ba->fetch_array($query)) {
			$config=$entry['config'];
		}
		if ($config!=""){
			$configs=split(',',$config);
			foreach ($configs as $value){
				$key=split('=',$value);
				if ($key[0]==$name){
					$sel="<select name='".$name."' id='".$name."' ".
						"onchange='if (this.selectedIndex==13) document.getElementById(\"".$name."_manual\").style.display=\"\"; ".
						"else document.getElementById(\"".$name."_manual\").style.display=\"none\";'>";
					($key[1]=="\${new_source}") ? $sel.="<option value='\${new_source}' selected>new_source</option>" : $sel.="<option value='\${new_source}'>new_source</option>";
					($key[1]=="\${from_user}") ? $sel.="<option value='\${from_user}' selected>from_user</option>" : $sel.="<option value='\${from_user}'>from_user</option>";
					($key[1]=="\${from_display}") ? $sel.="<option value='\${from_display}' selected>from_display</option>" : $sel.="<option value='\${from_display}'>from_display</option>";
					($key[1]=="\${pai_all}") ? $sel.="<option value='\${pai_all}' selected>pai_all</option>" : $sel.="<option value='\${pai_all}'>pai_all</option>";
					($key[1]=="\${pai_user}") ? $sel.="<option value='\${pai_user}' selected>pai_user</option>" : $sel.="<option value='\${pai_user}'>pai_user</option>";
					($key[1]=="\${pai_display}") ? $sel.="<option value='\${pai_display}' selected>pai_display</option>" : $sel.="<option value='\${pai_display}'>pai_display</option>";
					($key[1]=="\${ppi_all}") ? $sel.="<option value='\${ppi_all}' selected>ppi_all</option>" : $sel.="<option value='\${ppi_all}'>ppi_all</option>";
					($key[1]=="\${ppi_user}") ? $sel.="<option value='\${ppi_user}' selected>ppi_user</option>" : $sel.="<option value='\${ppi_user}'>ppi_user</option>";
					($key[1]=="\${ppi_display}") ? $sel.="<option value='\${ppi_display}' selected>ppi_display</option>" : $sel.="<option value='\${ppi_display}'>ppi_display</option>";
					($key[1]=="\${rpi_all}") ? $sel.="<option value='\${rpi_all}' selected>rpi_all</option>" : $sel.="<option value='\${rpi_all}'>rpi_all</option>";
					($key[1]=="\${rpi_user}") ? $sel.="<option value='\${rpi_user}' selected>rpi_user</option>" : $sel.="<option value='\${rpi_user}'>rpi_user</option>";
					($key[1]=="\${rpi_display}") ? $sel.="<option value='\${rpi_display}' selected>rpi_display</option>" : $sel.="<option value='\${rpi_display}'>rpi_display</option>";
					($key[1]=="") ? $sel.="<option value='' selected>none</option>" : $sel.="<option value=''>none</option>";
					($key[1]!="\${from_user}" && $key[1]!="\${from_display}" && $key[1]!="\${new_source}" && $key[1]!="\${pai_all}" && $key[1]!="\${pai_user}"
						&& $key[1]!="\${pai_display}" && $key[1]!="\${ppi_all}" && $key[1]!="\${ppi_user}" && $key[1]!="\${ppi_display}" && $key[1]!="\${rpi_all}" && $key[1]!="\${rpi_user}" && $key[1]!="\${rpi_display}" && $key[1]!="") ? $sel.="<option value='manual' selected>manual</option>" : $sel.="<option value='manual'>manual</option>";
					$sel.="</select><br>";
					($key[1]!="\${from_user}" && $key[1]!="\${from_display}" && $key[1]!="\${new_source}" && $key[1]!="\${pai_all}" && $key[1]!="\${pai_user}"
						&& $key[1]!="\${pai_display}" && $key[1]!="\${ppi_all}" && $key[1]!="\${ppi_user}" && $key[1]!="\${ppi_display}" && $key[1]!="\${rpi_all}" && $key[1]!="\${rpi_user}" && $key[1]!="\${rpi_display}" && $key[1]!="") ? $sel.="<input type='text' name='".$name."_manual' id='".$name."_manual' value='".$key['1']."'>" : $sel.="<input type='text' name='".$name."_manual' id='".$name."_manual' style='display:none;'>";
					return $sel;
				}
			}
		}
		return $sel;
	}else{
		return $sel;
	}
}

function from_pstn_src_setting($table){
		$sel="<select name='from_pstn_src_setting_".$table."' id='from_pstn_src_setting_".$table."'>";
		if ($table=="isdn")
				$sel.="<option value='oad' selected>oad</option>
				<option value='oad2'>oad2</option>
				<option value='qsigname'>qsigname</option>
				<option value='redirected_nr'>redirected_nr</option>";
		elseif ($table=="analog")
				$sel.="<option value='clip' selected>clip</option>
				<option value='cnip'>cnip</option>";
			$sel.="</select>";
	if (isset($_GET['gr'])){
		$ba=new beroAri();
		$query=$ba->select("select config from ".$table." where gr='".$_GET['gr']."' group by gr");
		while  ($entry = $ba->fetch_array($query)) {
			$config=$entry['config'];
		}
		if ($config!=""){
			$configs=split(',',$config);
			foreach ($configs as $value){
				$key=split('=',$value);
				if ($key[0]=="from_pstn_src_setting"){
					$sel="<select name='from_pstn_src_setting_".$table."' id='from_pstn_src_setting_".$table."'>";
					if ($table=="isdn"){
						($key[1]=="oad") ? $sel.="<option value='oad' selected>oad</option>" : $sel.="<option value='oad'>oad</option>";
						($key[1]=="oad2") ? $sel.="<option value='oad2' selected>oad2</option>" : $sel.="<option value='oad2'>oad2</option>";
						($key[1]=="qsigname") ? $sel.="<option value='qsigname' selected>qsigname</option>" : $sel.="<option value='qsigname'>qsigname</option>";
						($key[1]=="redirected_nr") ? $sel.="<option value='redirected_nr' selected>redirected_nr</option>" : $sel.="<option value='redirected_nr'>redirected_nr</option>";
					}elseif ($table=="analog"){
						($key[1]=="clip") ? $sel.="<option value='clip' selected>clip</option>" : $sel.="<option value='clip'>clip</option>";
						($key[1]=="cnip") ? $sel.="<option value='cnip' selected>cnip</option>" : $sel.="<option value='cnip'>cnip</option>";
					}
					$sel.="</select>";
					return $sel;
				}
			}
		}
		return $sel;
	}else{
		return $sel;
	}


}


// ++++++++CODECS+++++++++++++
function ges_codecs(){
	$select="";
	if (isset($_GET['id'])){
		$ba=new beroAri();
		$query=$ba->select("select config from sip where id='".$_GET['id']."'");
		while  ($entry = $ba->fetch_array($query)) {
			$config=$entry['config'];
		}
	}
	if ($config!=""){
		$control=false;
		$configs=split(',',$config);
		foreach ($configs as $value){
			$key=split('=',$value);
			if ($key[0]=="codecs"){
				$cod=split(':',$key[1]);
				$select="<select multiple name='codecs[]' id='sel' size='5'>";
				foreach ($cod as $codec){
						$select .= "<option value='".$codec."'>".$codec."</option>";
				}
				$select .= "</select>";
				$control=true;
			}
		}
		if ($control==false){
			$select="<select multiple name='codecs[]' id='sel' size='5'>";
			$select .= "</select>";
		}
	}else{
		$select="<select multiple name='codecs[]' id='sel' size='5'>";
		$select .= "<option value='pcma'>pcma</option>";
		$select .= "</select>";
	}

	return $select;
}

function new_codecs(){

	$config="";
	if (isset($_GET['id'])){
		$ba=new beroAri();
		$query=$ba->select("select config from sip where id='".$_GET['id']."'");
		while  ($entry = $ba->fetch_array($query)) {
			$config=$entry['config'];
		}
	}

	if ($config!=""){
		$all_codecs = Array ("pcma", "pcmu", "gsm", "g729", "g723", "g726-32");
		$new_codecs=$all_codecs;
		$configs=split(',',$config);
		foreach ($configs as $value){
			$key=split('=',$value);
			if ($key[0]=="codecs"){
				$new_codecs=array();
				$cod=split(':',$key[1]);
				for ($i=0;$i<count($all_codecs);$i++){
					$b=true;
					for ($j=0;$j<count($cod);$j++){
						if ($cod[$j]==$all_codecs[$i]) $b=false;
					}

					if ($b==true){
						$new_count=count($new_codecs);
						$new_codecs[$new_count]=$all_codecs[$i];
					}
				}
			}
		}

	}else{
		$all_codecs = Array ("pcmu", "gsm", "g729", "g723", "g726-32");
		$select="<select multiple name='new_codecs[]' id='sel1' size='5'>";
		for ($i=0;$i<count($all_codecs);$i++){
			$select .= "<option value='".$all_codecs[$i]."'>".$all_codecs[$i]."</option>";
		}
		return $select;
	}
	$select="<select multiple name='new_codecs[]' id='sel1' size='5'>";
	for ($i=0;$i<count($new_codecs);$i++){
		$select .= "<option value='".$new_codecs[$i]."'>".$new_codecs[$i]."</option>";
	}
	$select .= "</select>";
	return $select;


}

function codecs(){

			return "<table>
			<tr>
			<td></td>
			<td>".DIALPLAN_ALLOWED_CODECS_DIS."</td>
			<td></td>
			<td>".DIALPLAN_AVAILABLE_CODECS_DIS."</td>
			</tr>
			<tr>
			<td><input type='button' value='Up' id='upB' onclick='up()' onmouseover=\"Tip('".CODECS_UP."')\" onmouseout='UnTip()'></td>
			<td rowspan=2>".ges_codecs()."</td>
			<td><input type='button' value='<- Add' id='addB' onclick='move(\"sel1\",\"sel\")' onmouseover=\"Tip('".CODECS_ADD."')\" onmouseout='UnTip()'></td>
			<td rowspan=2>".new_codecs()."</td>
			</tr>
			<tr>
			<td><input type='button' value='Down' id='downB' onclick='down()' onmouseover=\"Tip('".CODECS_DOWN."')\" onmouseout='UnTip()'></td>
			<td><input type='button' value='Remove ->' id='removeB' onclick='move(\"sel\",\"sel1\")' onmouseover=\"Tip('".CODECS_REMOVE."')\" onmouseout='UnTip()'></td>
			</tr>
			</table>";
}


function dialplans($name){
	$sel="<select name='".$name."' id='".$name."'>
				<option value='0' selected>unknown</option>
				<option value='1'>international</option>
				<option value='2'>national</option>
				<option value='4'>subscriber</option>
			</select>";
	if (isset($_GET['gr'])){
		$ba=new beroAri();
		$query=$ba->select("select config from isdn where gr='".$_GET['gr']."' group by gr");
		while  ($entry = $ba->fetch_array($query)) {
			$config=$entry['config'];
		}
		if ($config!=""){
			$configs=split(',',$config);
			foreach ($configs as $value){
				$key=split('=',$value);
				if ($key[0]==$name){
					$sel= "<select name='".$name."' id='".$name."'>";
					($key[1]==0) ? $sel.="<option value='0' selected>unknown</option>" : $sel.="<option value='0'>unknown</option>";
					($key[1]==1) ? $sel.="<option value='1' selected>international</option>" : $sel.="<option value='1'>international</option>";
					($key[1]==2) ? $sel.="<option value='2' selected>national</option>" : $sel.="<option value='2'>national</option>";
					($key[1]==4) ? $sel.="<option value='4' selected>subscriber</option>" : $sel.="<option value='4'>subscriber</option>";
					$sel.="</select>";
					return $sel;
				}
			}
		}
		return $sel;
	}else{
		return $sel;
	}
}
function prefixes($name,$default){
	if (isset($_GET['gr'])){
		$ba=new beroAri();
		$query=$ba->select("select config from isdn where gr='".$_GET['gr']."' group by gr");
		while  ($entry = $ba->fetch_array($query)) {
			$config=$entry['config'];
		}
		if ($config!=""){
			$configs=split(',',$config);
			foreach ($configs as $value){
				$key=split('=',$value);
				if ($key[0]==$name) return "<input type='text' name='".$name."' id='".$name."' value='".stripslashes($key[1])."' size='7'>";
			}
		}
		return "<input type='text' name='".$name."' id='".$name."' value='".$default."' size='7'>";
	}else{
		return "<input type='text' name='".$name."' id='".$name."' value='".$default."' size='7'>";
	}
}
function screen(){
	if (isset($_GET['gr'])){
		$ba=new beroAri();
		$query=$ba->select("select config from isdn where gr='".$_GET['gr']."' group by gr");
		while  ($entry = $ba->fetch_array($query)) {
			$config=$entry['config'];
		}
		if ($config!=""){
			$configs=split(',',$config);
			foreach ($configs as $value){
				$key=split('=',$value);
				if ($key[0]=="screen" && $key[1]==1) return "<input type='checkbox' id='screen' name='screen' value='1' checked>";
				elseif ($key[0]=="screen" && $key[1]==0) return "<input type='checkbox' id='screen' name='screen' value='1'>";
			}
			return "<input type='checkbox' id='screen' name='screen' value='1'>";
		}else{
			return "<input type='checkbox' id='screen' name='screen' value='1'>";
		}
	}else{
		return "<input type='checkbox' id='screen' name='screen' value='1'>";
	}
}
function pres(){
	if (isset($_GET['gr'])){
		$ba=new beroAri();
		$query=$ba->select("select config from isdn where gr='".$_GET['gr']."' group by gr");
		while  ($entry = $ba->fetch_array($query)) {
			$config=$entry['config'];
		}
		if ($config!=""){
			$configs=split(',',$config);
			foreach ($configs as $value){
				$key=split('=',$value);
				if ($key[0]=="pres" && $key[1]==1) return "<input type='checkbox' name='pres' id='pres' value='1' checked>";
				elseif ($key[0]=="pres" && $key[1]==0) return "<input type='checkbox' name='pres' id='pres' value='1'>";
			}
			return "<input type='checkbox' name='pres' id='pres' value='1'>";
		}else{
			return "<input type='checkbox' name='pres' id='pres' value='1'>";
		}
	}else{
		return "<input type='checkbox' name='pres' id='pres' value='1'>";
	}
}

function bearer_cap(){
	if (isset($_GET['gr'])){
		$ba=new beroAri();
		$query=$ba->select("select config from isdn where gr='".$_GET['gr']."' group by gr");
		while  ($entry = $ba->fetch_array($query)) {
			$config=$entry['config'];
		}
		if ($config!=""){
			$configs=split(',',$config);
			foreach ($configs as $value){
				$key=split('=',$value);
				if ($key[0]=="bearer_cap"){
					 if ($key[1]=="SPEECH") return "<select name='bearer_cap' id='bearer_cap'>
											<option value='SPEECH' selected>SPEECH</option>
											<option value='AUDIO_3_1K'>AUDIO_3_1K</option>
											<option value='AUDIO_7K'>AUDIO_7K</option>
											<option value='VIDEO'>VIDEO</option>
											<option value='DIGITAL_UNRESTRICTED'>DIGITAL_UNRESTRICTED</option>
											<option value='DIGITAL_RESTRICTED'>DIGITAL_RESTRICTED</option>
											<option value='DIGITAL_UNRESTRICTED_TONES'>DIGITAL_UNRESTRICTED_TONES</option>
										</select>";
					 if ($key[1]=="AUDIO_3_1K") return "<select name='bearer_cap' id='bearer_cap'>
											<option value='SPEECH'>SPEECH</option>
											<option value='AUDIO_3_1K' selected>AUDIO_3_1K</option>
											<option value='AUDIO_7K'>AUDIO_7K</option>
											<option value='VIDEO'>VIDEO</option>
											<option value='DIGITAL_UNRESTRICTED'>DIGITAL_UNRESTRICTED</option>
											<option value='DIGITAL_RESTRICTED'>DIGITAL_RESTRICTED</option>
											<option value='DIGITAL_UNRESTRICTED_TONES'>DIGITAL_UNRESTRICTED_TONES</option>
										</select>";
					 if ($key[1]=="AUDIO_7K") return "<select name='bearer_cap' id='bearer_cap'>
											<option value='SPEECH'>SPEECH</option>
											<option value='AUDIO_3_1K'>AUDIO_3_1K</option>
											<option value='AUDIO_7K' selected>AUDIO_7K</option>
											<option value='VIDEO'>VIDEO</option>
											<option value='DIGITAL_UNRESTRICTED'>DIGITAL_UNRESTRICTED</option>
											<option value='DIGITAL_RESTRICTED'>DIGITAL_RESTRICTED</option>
											<option value='DIGITAL_UNRESTRICTED_TONES'>DIGITAL_UNRESTRICTED_TONES</option>
										</select>";
					 if ($key[1]=="VIDEO") return "<select name='bearer_cap' id='bearer_cap'>
											<option value='SPEECH'>SPEECH</option>
											<option value='AUDIO_3_1K'>AUDIO_3_1K</option>
											<option value='AUDIO_7K'>AUDIO_7K</option>
											<option value='VIDEO' selected>VIDEO</option>
											<option value='DIGITAL_UNRESTRICTED'>DIGITAL_UNRESTRICTED</option>
											<option value='DIGITAL_RESTRICTED'>DIGITAL_RESTRICTED</option>
											<option value='DIGITAL_UNRESTRICTED_TONES'>DIGITAL_UNRESTRICTED_TONES</option>
										</select>";
					 if ($key[1]=="DIGITAL_UNRESTRICTED") return "<select name='bearer_cap' id='bearer_cap'>
											<option value='SPEECH'>SPEECH</option>
											<option value='AUDIO_3_1K'>AUDIO_3_1K</option>
											<option value='AUDIO_7K'>AUDIO_7K</option>
											<option value='VIDEO'>VIDEO</option>
											<option value='DIGITAL_UNRESTRICTED' selected>DIGITAL_UNRESTRICTED</option>
											<option value='DIGITAL_RESTRICTED'>DIGITAL_RESTRICTED</option>
											<option value='DIGITAL_UNRESTRICTED_TONES'>DIGITAL_UNRESTRICTED_TONES</option>
										</select>";
					if ($key[1]=="DIGITAL_RESTRICTED") return "<select name='bearer_cap' id='bearer_cap'>
											<option value='SPEECH'>SPEECH</option>
											<option value='AUDIO_3_1K'>AUDIO_3_1K</option>
											<option value='AUDIO_7K'>AUDIO_7K</option>
											<option value='VIDEO'>VIDEO</option>
											<option value='DIGITAL_UNRESTRICTED'>DIGITAL_UNRESTRICTED</option>
											<option value='DIGITAL_RESTRICTED' selected>DIGITAL_RESTRICTED</option>
											<option value='DIGITAL_UNRESTRICTED_TONES'>DIGITAL_UNRESTRICTED_TONES</option>
										</select>";
					if ($key[1]=="DIGITAL_UNRESTRICTED_TONES") return "<select name='bearer_cap' id='bearer_cap'>
											<option value='SPEECH'>SPEECH</option>
											<option value='AUDIO_3_1K'>AUDIO_3_1K</option>
											<option value='AUDIO_7K'>AUDIO_7K</option>
											<option value='VIDEO'>VIDEO</option>
											<option value='DIGITAL_UNRESTRICTED'>DIGITAL_UNRESTRICTED</option>
											<option value='DIGITAL_RESTRICTED'>DIGITAL_RESTRICTED</option>
											<option value='DIGITAL_UNRESTRICTED_TONES' selected>DIGITAL_UNRESTRICTED_TONES</option>
										</select>";
			}
		}
	}
		return "<select name='bearer_cap' id='bearer_cap'>
				<option value='SPEECH' selected>SPEECH</option>
				<option value='AUDIO_3_1K'>AUDIO_3_1K</option>
				<option value='AUDIO_7K'>AUDIO_7K</option>
				<option value='VIDEO'>VIDEO</option>
				<option value='DIGITAL_UNRESTRICTED'>DIGITAL_UNRESTRICTED</option>
				<option value='DIGITAL_RESTRICTED'>DIGITAL_RESTRICTED</option>
				<option value='DIGITAL_UNRESTRICTED_TONES'>DIGITAL_UNRESTRICTED_TONES</option>
			</select>";
	}else{
		return "<select name='bearer_cap' id='bearer_cap'>
					<option value='SPEECH' selected>SPEECH</option>
					<option value='AUDIO_3_1K'>AUDIO_3_1K</option>
					<option value='AUDIO_7K'>AUDIO_7K</option>
					<option value='VIDEO'>VIDEO</option>
					<option value='DIGITAL_UNRESTRICTED'>DIGITAL_UNRESTRICTED</option>
					<option value='DIGITAL_RESTRICTED'>DIGITAL_RESTRICTED</option>
					<option value='DIGITAL_UNRESTRICTED_TONES'>DIGITAL_UNRESTRICTED_TONES</option>
				</select>";
	}
}
?>
