<?php
/* $Id$ */
/*

	freeswitch_gateways_edit.php
	Copyright (C) 2008 Mark J Crane
	All rights reserved.

	FreeSWITCH (TM)
	http://www.freeswitch.org/

	Redistribution and use in source and binary forms, with or without
	modification, are permitted provided that the following conditions are met:

	1. Redistributions of source code must retain the above copyright notice,
	   this list of conditions and the following disclaimer.

	2. Redistributions in binary form must reproduce the above copyright
	   notice, this list of conditions and the following disclaimer in the
	   documentation and/or other materials provided with the distribution.

	THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
	INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
	AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
	AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
	OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
	SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
	INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
	CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
	ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
	POSSIBILITY OF SUCH DAMAGE.
*/

require("guiconfig.inc");
require("/usr/local/pkg/freeswitch.inc");


$a_gateways 	   = &$config['installedpackages']['freeswitchgateways']['config'];

$id = $_GET['id'];
if (isset($_POST['id'])) {
	$id = $_POST['id'];
}

if (isset($id) && $a_gateways[$id]) {

	$pconfig['gatewayid'] = $a_gateways[$id]['gatewayid'];
	$gatewayid = $a_gateways[$id]['gatewayid'];
	$pconfig['gateway'] = $a_gateways[$id]['gateway'];
	$pconfig['username'] = $a_gateways[$id]['username'];	
	$pconfig['auth-username'] = $a_gateways[$id]['auth-username'];
	$pconfig['password'] = $a_gateways[$id]['password'];
	$pconfig['realm'] = $a_gateways[$id]['realm'];
	$pconfig['from-user'] = $a_gateways[$id]['from-user'];
	$pconfig['from-domain'] = $a_gateways[$id]['from-domain'];
	$pconfig['proxy'] = $a_gateways[$id]['proxy'];
	$pconfig['expire-seconds'] = $a_gateways[$id]['expire-seconds'];
	$pconfig['register'] = $a_gateways[$id]['register'];
	$pconfig['register-transport'] = $a_gateways[$id]['register-transport'];
	$pconfig['retry-seconds'] = $a_gateways[$id]['retry-seconds'];
	$pconfig['extension'] = $a_gateways[$id]['extension'];
	$pconfig['ping'] = $a_gateways[$id]['ping'];
	$pconfig['caller-id-in-from'] = $a_gateways[$id]['caller-id-in-from'];
	$pconfig['supress-cng'] = $a_gateways[$id]['supress-cng'];

	$pconfig['effective_caller_id_name'] = $a_gateways[$id]['effective_caller_id_name'];
	$pconfig['effective_caller_id_number'] = $a_gateways[$id]['effective_caller_id_number'];
	$pconfig['outbound_caller_id_name'] = $a_gateways[$id]['outbound_caller_id_name'];
	$pconfig['outbound_caller_id_number'] = $a_gateways[$id]['outbound_caller_id_number'];
	
	$pconfig['context'] = $a_gateways[$id]['context'];
	$pconfig['enabled'] = $a_gateways[$id]['enabled'];
	$pconfig['description'] = $a_gateways[$id]['description'];
}

if ($_POST) {

	unset($input_errors);
	$pconfig = $_POST;

	if (!$input_errors) {
	
		$effective_caller_id_number = $_POST['effective_caller_id_number'];
		$outbound_caller_id_number =  $_POST['outbound_caller_id_number'];		
		$replace_array = array("(", ")", " ", "-");
		$effective_caller_id_number = str_replace($replace_array, "", $effective_caller_id_number);
		$outbound_caller_id_number = str_replace($replace_array, "", $outbound_caller_id_number);
		
		$ent = array();
		if (strlen($_POST['ivrid']) > 0) {
		  $ent['gatewayid'] = $_POST['ivrid'];
		}
		else {
			$ent['gatewayid'] = guid();
		}
		$ent['gateway'] = $_POST['gateway'];
		$ent['username'] = $_POST['username'];
		$ent['auth-username'] = $_POST['auth-username'];		
		$ent['password'] = $_POST['password'];
		$ent['realm'] = $_POST['realm'];
		$ent['from-user'] = $_POST['from-user'];
		$ent['from-domain'] = $_POST['from-domain'];
		$ent['proxy'] = $_POST['proxy'];
		$ent['expire-seconds'] = $_POST['expire-seconds'];
		$ent['register'] = $_POST['register'];
		$ent['register-transport'] = $_POST['register-transport'];
		$ent['retry-seconds'] = $_POST['retry-seconds'];
		$ent['extension'] = $_POST['extension'];
		$ent['ping'] = $_POST['ping'];		
		$ent['caller-id-in-from'] = $_POST['caller-id-in-from'];
		$ent['supress-cng'] = $_POST['supress-cng'];
		
		$ent['effective_caller_id_name'] = $_POST['effective_caller_id_name'];
		$ent['effective_caller_id_number'] = $effective_caller_id_number;
		$ent['outbound_caller_id_name'] = $_POST['outbound_caller_id_name'];
		$ent['outbound_caller_id_number'] = $outbound_caller_id_number;

		$ent['context'] = $_POST['context'];
		$ent['enabled'] = $_POST['enabled'];
		$ent['description'] = $_POST['description'];
		
		if (isset($id) && $a_gateways[$id]) {
		  	//update
      		$a_gateways[$id] = $ent;
		}
		else {
		  	//add	
			$a_gateways[] = $ent;
		}


		if (strlen(trim($_POST['dialplan_expression']))> 0) {
		
		    $gatewayid = $_POST['gatewayid'];		
		    $gateway = $_POST['gateway'];
		    $context = $_POST['context'];

	        $default_area_code 				= &$config['installedpackages']['freeswitchsettings']['config'][0]['default_area_code'];
	        $a_dialplan_includes          	= &$config['installedpackages']['freeswitchdialplanincludes']['config'];
	        $a_dialplan_include_details 	= &$config['installedpackages']['freeswitchdialplanincludedetails']['config'];
  		

    		$tmp_array = split("\\\n", $_POST['dialplan_expression']);

		    foreach($tmp_array as $dialplan_expression) {
		
	            $dialplan_expression = trim($dialplan_expression);
	            if (strlen($dialplan_expression)>0) {
	
	                switch ($dialplan_expression) {
	                case "^(\d{7})$":
	                    $action_data = "sofia/gateway/".$gateway."/1".$default_area_code."\$1";
	                    $label = "7 digits";
	                    $abbrv = "7d";
	                    break;
	                case "^(\d{10})$":
	                    $action_data = "sofia/gateway/".$gateway."/1\$1";
	                    $label = "10 digits";
	                    $abbrv = "10d";
	                    break;
	                case "^(\d{11})$":
	                    $action_data = "sofia/gateway/".$gateway."/\$1";
	                    $label = "11 digits";
	                    $abbrv = "11d";
	                    break;
	                case "^(311)$":
	                    $action_data = "sofia/gateway/".$gateway."/\$1";
	                    $label = "311";
	                    $abbrv = "311";
	                    break;
	                case "^(411)$":
	                    $action_data = "sofia/gateway/".$gateway."/\$1";
	                    $label = "411";
	                    $abbrv = "411";
	                    break;
	                case "^(911)$":
	                    $action_data = "sofia/gateway/".$gateway."/\$1";
	                    $label = "911";
	                    $abbrv = "911";
	                    break;
	                case "^9(\d{3})$":
	                    $action_data = "sofia/gateway/".$gateway."/1".$default_area_code."\$1";
	                    $label = "dial 9, 3 digits";
	                    $abbrv = "9.3d";
	                    break;
	                case "^9(\d{4})$":
	                    $action_data = "sofia/gateway/".$gateway."/1".$default_area_code."\$1";
	                    $label = "dial 9, 4 digits";
	                    $abbrv = "9.4d";
	                    break;	
	                case "^9(\d{7})$":
	                    $action_data = "sofia/gateway/".$gateway."/1".$default_area_code."\$1";
	                    $label = "dial 9, 7 digits";
	                    $abbrv = "9.7d";
	                    break;
	                case "^9(\d{10})$":
	                    $action_data = "sofia/gateway/".$gateway."/\$1";
	                    $label = "dial 9, 10 digits";
	                    $abbrv = "9.10d";
	                    break;
	                case "^9(\d{11})$":
	                    $action_data = "sofia/gateway/".$gateway."/\$1";
	                    $label = "dial 9, 11 digits";
	                    $abbrv = "9.11d";
	                    break;
	                case "^1?(8(00|55|66|77|88)[2-9]\d{6})$":
	                    $action_data = "sofia/gateway/".$gateway."/\$1";
	                    $label = "toll free";
	                    $abbrv = "tollfree";
	                    break;
	                default:
	                    $action_data = "sofia/gateway/".$gateway."/\$1";
	                    $label = $dialplan_expression;
	                    $abbrv = $dialplan_expression;
	                }
	
	                $dialplanincludeid = guid();
	                $ent['dialplanincludeid'] = $dialplanincludeid;
					$ent['extensionname'] = $gateway.".".$abbrv;
					$ent['order'] = '9002'; //if update use the existing order number and extension name and desc
					$ent['context'] = $context;
					$ent['enabled'] = 'true';			
					$ent['descr'] = $label.' '.$gateway;
					$ent['opt1name'] = 'gatewayid';
					$ent['opt1value'] = $gatewayid;
					$a_dialplan_includes[] = $ent;
					unset($ent);
	            		
	                $ent = array();
	                $ent['dialplanincludeid'] = $dialplanincludeid;
	                $ent['tag'] = 'condition'; //condition, action, antiaction
	                $ent['fieldtype'] = 'destination_number';
	                $ent['fielddata'] = $dialplan_expression;
					$ent['fieldorder'] = '000';
					$a_dialplan_include_details[] = $ent;
					unset($ent);

					if (strlen($effective_caller_id_name) > 0) {
						$ent = array();
						$ent['dialplanincludeid'] = $dialplanincludeid;
						$ent['tag'] = 'action'; //condition, action, antiaction
						$ent['fieldtype'] = 'set';
						$ent['fielddata'] = 'effective_caller_id_name='.$effective_caller_id_name;
						$ent['fieldorder'] = '001';
						$a_dialplan_include_details[] = $ent;
						unset($ent);
					}
					if (strlen($effective_caller_id_number) > 0) {
						$ent = array();
						$ent['dialplanincludeid'] = $dialplanincludeid;
						$ent['tag'] = 'action'; //condition, action, antiaction
						$ent['fieldtype'] = 'set';
						$ent['fielddata'] = 'effective_caller_id_number='.$effective_caller_id_number;
						$ent['fieldorder'] = '001';
						$a_dialplan_include_details[] = $ent;
						unset($ent);
					}
					if (strlen($outbound_caller_id_name) > 0) {
						$ent = array();
						$ent['dialplanincludeid'] = $dialplanincludeid;
						$ent['tag'] = 'action'; //condition, action, antiaction
						$ent['fieldtype'] = 'set';
						$ent['fielddata'] = 'outbound_caller_id_name='.$outbound_caller_id_name;
						$ent['fieldorder'] = '001';
						$a_dialplan_include_details[] = $ent;
						unset($ent);
					}
					if (strlen($outbound_caller_id_number) > 0) {
						$ent = array();
						$ent['dialplanincludeid'] = $dialplanincludeid;
						$ent['tag'] = 'action'; //condition, action, antiaction
						$ent['fieldtype'] = 'set';
						$ent['fielddata'] = 'outbound_caller_id_number='.$outbound_caller_id_number;
						$ent['fieldorder'] = '001';
						$a_dialplan_include_details[] = $ent;
						unset($ent);
					}
					
	                $ent = array();
	                $ent['dialplanincludeid'] = $dialplanincludeid;
	                $ent['tag'] = 'action'; //condition, action, antiaction
	                $ent['fieldtype'] = 'bridge';
	                $ent['fielddata'] = $action_data;
					$ent['fieldorder'] = '001';
					$a_dialplan_include_details[] = $ent;
					unset($ent);
					
					unset($label);
					unset($abbrv);
					unset($dialplan_expression);
					unset($action_data);
	            } //if strlen
        	} //end for each		
		}
	
		write_config();
		sync_package_freeswitch_gateways();	
		sync_package_freeswitch_dialplan_includes();		
		
		header("Location: freeswitch_gateways.php");
		exit;
	}
}

include("head.inc");

?>


<script type="text/javascript" language="JavaScript">

function enable_change(enable_over) {
	var endis;
	endis = !(document.iform.enable.checked || enable_over);
	document.iform.range_from.disabled = endis;
	document.iform.range_to.disabled = endis;
}

function show_advanced_config() {
	document.getElementById("showadvancedbox").innerHTML='';
	aodiv = document.getElementById('showadvanced');
	aodiv.style.display = "block";
}

</script>


<body link="#0000CC" vlink="#0000CC" alink="#0000CC">
<?php include("fbegin.inc"); ?>
<p class="pgtitle">FreeSWITCH: Gateways: Edit</p>
<?php if ($input_errors) print_input_errors($input_errors); ?>


<div id="mainlevel">
<table width="100%" border="0" cellpadding="0" cellspacing="0">
<tr><td class="tabnavtbl">
<?php

display_top_tabs(build_menu());
 	
?>
</td></tr>
</table>
<table width="100%" border="0" cellpadding="0" cellspacing="0">
   <tr>
     <td class="tabcont" >


  	<table width="100%" border="0" cellpadding="6" cellspacing="0">
      <tr>
        <td><p><span class="vexpl"><span class="red"><strong>Gateway Setup<br>
            </strong></span>
            The 'SIP Provider Examples' from the FreeSWITCH wiki can be used as reference to get started. <br />
            <a href='http://wiki.freeswitch.org/wiki/SIP_Provider_Examples' target='_blank'>http://wiki.freeswitch.org/wiki/SIP_Provider_Examples</a>
            </p></td>
      </tr>
    </table>
    <br />

        <form action="freeswitch_gateways_edit.php" method="post" name="iform" id="iform">
            <table width="100%" border="0" cellpadding="6" cellspacing="0">
            <tr>
              <td width="22%" valign="top" class="vncellreq">Gateway</td>
              <td width="78%" class="vtable">
                <input name="gateway" type="text" class="formfld" id="gateway" size="40" value="<?=htmlspecialchars($pconfig['gateway']);?>">
                <br><span class="vexpl">Enter the gateway name here.<br></span>
              </td>
            </tr>
            <tr>
              <td width="22%" valign="top" class="vncellreq">Username</td>
              <td width="78%" class="vtable">
                <input name="username" type="text" class="formfld" id="username" size="40" value="<?=htmlspecialchars($pconfig['username']);?>">
                <br><span class="vexpl">Enter the username here.<br></span>
              </td>
            </tr>
            <tr>
              <td width="22%" valign="top" class="vncellreq">Password</td>
              <td width="78%" class="vtable">
                <input name="password" type="password" class="formfld" id="password" size="40" value="<?=htmlspecialchars($pconfig['password']);?>">
                <br><span class="vexpl">Enter the password here.<br></span>
              </td>
            </tr>
            <tr>
              <td width="22%" valign="top" class="vncellreq">From-user</td>
              <td width="78%" class="vtable">
                <input name="from-user" type="text" class="formfld" id="from-user" size="40" value="<?=htmlspecialchars($pconfig['from-user']);?>">
                <br><span class="vexpl">Enter the from-user here.<br></span>
              </td>
            </tr>
            <tr>
              <td width="22%" valign="top" class="vncellreq">From-domain</td>
              <td width="78%" class="vtable">
                <input name="from-domain" type="text" class="formfld" id="from-domain" size="40" value="<?=htmlspecialchars($pconfig['from-domain']);?>">
                <br><span class="vexpl">Enter the from-domain here.<br></span>
              </td>
            </tr>
            <tr>
              <td width="22%" valign="top" class="vncellreq">Proxy</td>
              <td width="78%" class="vtable">
                <input name="proxy" type="text" class="formfld" id="proxy" size="40" value="<?=htmlspecialchars($pconfig['proxy']);?>">
                <br><span class="vexpl">Enter the proxy here.<br></span>
              </td>
            </tr>
            <tr>
              <td width="22%" valign="top" class="vncellreq">Realm</td>
              <td width="78%" class="vtable">
                <input name="realm" type="text" class="formfld" id="realm" size="40" value="<?=htmlspecialchars($pconfig['realm']);?>">
                <br><span class="vexpl">Enter the realm here.<br></span>
              </td>
            </tr>
            <tr>
              <td width="22%" valign="top" class="vncellreq">Expire-seconds</td>
              <td width="78%" class="vtable">
                <input name="expire-seconds" type="text" class="formfld" id="expire-seconds" size="40" value="<?=htmlspecialchars($pconfig['expire-seconds']);?>">
                <br><span class="vexpl">Enter the expire-seconds here. Example: 600<br></span>
              </td>
            </tr>
            <tr>
              <td width="22%" valign="top" class="vncellreq">Register</td>
              <td width="78%" class="vtable">
                <?php
                echo "              <select name='register' class='formfld'>\n";
                echo "                <option></option>\n";
                switch (htmlspecialchars($pconfig['register'])) {
                case "true":
                    echo "              <option value='true' selected='yes'>true</option>\n";
                    echo "              <option value='false'>false</option>\n";
                    break;
                case "false":
                    echo "              <option value='true'>true</option>\n";
                    echo "              <option value='false' selected='yes'>false</option>\n";

                    break;
                default:
                    echo "              <option value='true' selected='yes'>true</option>\n";
                    echo "              <option value='false'>false</option>\n";
                }
                echo "              </select>\n";
                ?>
                Choose whether to register.
              </td>
            </tr>				

            <tr>
              <td width="22%" valign="top" class="vncellreq">Retry-seconds</td>
              <td width="78%" class="vtable">
                <input name="retry-seconds" type="text" class="formfld" id="retry-seconds" size="40" value="<?=htmlspecialchars($pconfig['retry-seconds']);?>">
                <br> <span class="vexpl">Enter the retry_seconds here. Example: 30<br></span>
              </td>
            </tr>

            <tr>
              <td width="22%" valign="top" class="vncell">Effective Caller ID Name</td>
              <td width="78%" class="vtable">
                <input name="effective_caller_id_name" type="text" class="formfld" id="effective_caller_id_name" size="40" value="<?=htmlspecialchars($pconfig['effective_caller_id_name']);?>">
                <br> <span class="vexpl">Enter the effective caller ID name here.<br></span>
              </td>
            </tr>
            <tr>
              <td width="22%" valign="top" class="vncell">Effective Caller ID Number</td>
              <td width="78%" class="vtable">
                <input name="effective_caller_id_number" type="text" class="formfld" id="effective_caller_id_number" size="40" value="<?=htmlspecialchars($pconfig['effective_caller_id_number']);?>">
                <br> <span class="vexpl">Enter the effective caller ID number here.<br></span>
              </td>
            </tr>
            <tr>
              <td width="22%" valign="top" class="vncell">Outbound Caller ID Name</td>
              <td width="78%" class="vtable">
                <input name="outbound_caller_id_name" type="text" class="formfld" id="outbound_caller_id_name" size="40" value="<?=htmlspecialchars($pconfig['outbound_caller_id_name']);?>">
                <br> <span class="vexpl">Enter the outbound	caller ID name here.<br></span>
              </td>
            </tr>
            <tr>
              <td width="22%" valign="top" class="vncell">Outbound Caller ID Number</td>
              <td width="78%" class="vtable">
                <input name="outbound_caller_id_number" type="text" class="formfld" id="outbound_caller_id_number" size="40" value="<?=htmlspecialchars($pconfig['outbound_caller_id_number']);?>">
                <br> <span class="vexpl">Enter the outbound caller ID number here.<br></span>
              </td>
            </tr>
			
            <tr>
              <td width="22%" valign="top" class="vncellreq">Context</td>
              <td width="78%" class="vtable">
                <input name="context" type="text" class="formfld" id="context" size="40" value="<?=htmlspecialchars($pconfig['context']);?>">
                <br> <span class="vexpl">Enter the context here. Example: public<br></span>
              </td>
            </tr>
            </table>
      			<div id="showadvancedbox">
      			      <table width="100%" border="0" cellpadding="6" cellspacing="0">
                  <tr>
                    <td width="22%" valign="top" class="vncell">Show Advanced</td>
                    <td width="78%" class="vtable">
                      <input type="button" onClick="show_advanced_config()" value="Advanced"></input></a>
                    </td>
                  </tr>
                  </table>
      			</div>
      			<div id="showadvanced" style="display:none">
                  <table width="100%" border="0" cellpadding="6" cellspacing="0">
				  <tr>
                    <td width="22%" valign="top" class="vncell">Auth-username</td>
                    <td width="78%" class="vtable">
                      <input name="auth-username" type="text" class="formfld" id="auth-username" size="40" value="<?=htmlspecialchars($pconfig['auth-username']);?>">
                      <br> <span class="vexpl">Enter the auth-username here.<br></span>
                    </td>
                  </tr>
                  <tr>
                    <td width="22%" valign="top" class="vncell">Register-transport</td>
                    <td width="78%" class="vtable">
                      <?php
                      echo "              <select name='register-transport' class='formfld'>\n";
                      echo "                <option></option>\n";
                      switch (htmlspecialchars($pconfig['register-transport'])) {
                      case "udp":
                          echo "              <option value='udp' selected='yes'>udp</option>\n";
                          echo "              <option value='tcp'>tcp</option>\n";
						  echo "              <option value='tls'>tls</option>\n";						  
                          break;
                      case "tcp":
                          echo "              <option value='udp'>udp</option>\n";
                          echo "              <option value='tcp' selected='yes'>tcp</option>\n";
						  echo "              <option value='tls'>tls</option>\n";						  
                      case "tls":
                          echo "              <option value='udp'>udp</option>\n";
                          echo "              <option value='tcp'>tcp</option>\n";
						  echo "              <option value='tls' selected='yes'>tls</option>\n";
                          break;
                      default:
                          echo "              <option value='udp'>udp</option>\n";
                          echo "              <option value='tcp'>tcp</option>\n";
						  echo "              <option value='tls'>tls</option>\n";
                      }
                      echo "              </select>\n";
                      ?>
                      Choose whether to register-transport.
                    </td>
                  </tr>
                  <tr>
                    <td width="22%" valign="top" class="vncell">Extension</td>
                    <td width="78%" class="vtable">
                      <input name="extension" type="text" class="formfld" id="extension" size="40" value="<?=htmlspecialchars($pconfig['extension']);?>">
                      <br> <span class="vexpl">Enter the extension here.<br></span>
                    </td>
                  </tr>
                  <tr>
                    <td width="22%" valign="top" class="vncell">Ping</td>
                    <td width="78%" class="vtable">
                      <input name="ping" type="text" class="formfld" id="ping" size="40" value="<?=htmlspecialchars($pconfig['ping']);?>">
                      <br> <span class="vexpl">Enter the ping interval here in seconds.<br></span>
                    </td>
                  </tr>
                  <tr>
                    <td width="22%" valign="top" class="vncell">Caller-id-in-from</td>
                    <td width="78%" class="vtable">
                      <?php
                      echo "              <select name='caller-id-in-from' class='formfld'>\n";
                      echo "                <option></option>\n";
                      switch (htmlspecialchars($pconfig['caller-id-in-from'])) {
                      case "true":
                          echo "              <option value='true' selected='yes'>true</option>\n";
                          echo "              <option value='false'>false</option>\n";
                          break;
                      case "false":
                          echo "              <option value='true'>true</option>\n";
                          echo "              <option value='false' selected='yes'>false</option>\n";

                          break;
                      default:
                          echo "              <option value='true'>true</option>\n";
                          echo "              <option value='false'>false</option>\n";
                      }
                      echo "              </select>\n";
                      ?>
                    </td>
                  </tr>
                  <tr>
                    <td width="22%" valign="top" class="vncell">Supress-cng</td>
                    <td width="78%" class="vtable">
                      <?php
                      echo "              <select name='supress-cng' class='formfld'>\n";
                      echo "                <option></option>\n";
                      switch (htmlspecialchars($pconfig['supress-cng'])) {
                      case "true":
                          echo "              <option value='true' selected='yes'>true</option>\n";
                          echo "              <option value='false'>false</option>\n";
                          break;
                      case "false":
                          echo "              <option value='true'>true</option>\n";
                          echo "              <option value='false' selected='yes'>false</option>\n";

                          break;
                      default:
                          echo "              <option value='true'>true</option>\n";
                          echo "              <option value='false'>false</option>\n";
                      }
                      echo "              </select>\n";
                      ?>
                    </td>
                  </tr>
                  </table>

      			</div>

            <table width="100%" border="0" cellpadding="6" cellspacing="0">
            <tr>
              <td width="22%" valign="top" class="vncellreq">Enabled</td>
              <td width="78%" class="vtable">
                <?php
                echo "              <select name='enabled' class='formfld'>\n";
                echo "                <option></option>\n";
                switch (htmlspecialchars($pconfig['enabled'])) {
                case "true":
                    echo "              <option value='true' selected='yes'>true</option>\n";
                    echo "              <option value='false'>false</option>\n";
                    break;
                case "false":
                    echo "              <option value='true'>true</option>\n";
                    echo "              <option value='false' selected='yes'>false</option>\n";

                    break;
                default:
                    echo "              <option value='true' selected='yes'>true</option>\n";
                    echo "              <option value='false'>false</option>\n";
                }
                echo "              </select>\n";
                ?>
              </td>
            </tr>

            <tr>
              <td width="22%" valign="top" class="vncell">Dialplan Expression</td>
              <td width="78%" class="vtable">
              <?php
                echo "<textarea name=\"dialplan_expression\" id=\"dialplan_expression\" cols=\"30\" rows=\"4\" wrap=\"off\"></textarea>\n";
              ?>
                <br>
                <select name='dialplan_expression_select' id='dialplan_expression_select' onchange="document.getElementById('dialplan_expression').value += document.getElementById('dialplan_expression_select').value + '\n';" class='formfld'>
                <option></option>
                <option value='^(\d{7})$'>7 digits local</option>
                <option value='^(\d{10})$'>10 digits long distance</option>
                <option value='^(\d{11})$'>11 digits long distance</option>
                <option value='^011(.*)$'>011 International</option>
                <option value='^311$'>311 information</option>
                <option value='^411$'>411 information</option>
                <option value='^911$'>911 emergency</option>
                <option value='^1?(8(00|55|66|77|88)[2-9]\d{6})$'>toll free</option>
                <option value='^9(\d{3})$'>Dial 9 then 3 digits</option>
                <option value='^9(\d{4})$'>Dial 9 then 4 digits</option>
                <option value='^9(\d{7})$'>Dial 9 then 7 digits</option>
                <option value='^9(\d{10})$'>Dial 9 then 10 digits</option>
                <option value='^9(\d{11})$'>Dial 9 then 11 digits</option>
                </select>
                <span class="vexpl">
                <br />
                Shortcut to create the outbound dialplan entries for this Gateway. The entries are saved to and edited from the 'Dialplan' tab.
                </span></td>
            </tr>

            <tr>
              <td width="22%" valign="top" class="vncell">Gateway Description</td>
              <td width="78%" class="vtable">
                <input name="description" type="text" class="formfld" id="description" size="40" value="<?=htmlspecialchars($pconfig['description']);?>">
                <br> <span class="vexpl">Enter the description of the gateway here.</span></td>
            </tr>
            <tr>
              <td width="22%" valign="top">&nbsp;</td>
              <td width="78%">
                <input name="Submit" type="submit" class="formbtn" value="Save"> <input class="formbtn" type="button" value="Cancel" onclick="history.back()">
                <?php if (isset($id) && $a_gateways[$id]): ?>
                  <input name="id" type="hidden" value="<?=$id;?>">
                  <input name="gatewayid" type="hidden" value="<?=htmlspecialchars($pconfig['gatewayid']);?>">
                <?php endif; ?>
              </td>
            </tr>
            </table>
        </form>

      <br>
      <br>
      <br>
      <br>
      <br>
      <br>

     </td>
   </tr>
</table>

</div>

<?php include("fend.inc"); ?>
</body>
</html>