<?php 

session_start();

$script_path1 = 'cat /sys/class/gpio/gpio45/value';
$is_dual_channel = shell_exec($script_path1);

$script_path2 = 'cat /sys/class/gpio/gpio23/value';
$is_dual_channel2 = shell_exec($script_path2);

$timezones = array('Africa/Accra','Africa/Addis_Ababa','Africa/Algiers','Africa/Asmara','Africa/Bamako','Africa/Bangui','Africa/Banjul','Africa/Blantyre','Africa/Brazzaville','Africa/Bujumbura','Africa/Cairo','Africa/Casablanca','Africa/Ceuta','Africa/Conakry','Africa/Dakar','Africa/Dar_es_Salaam','Africa/Djibouti','Africa/Douala','Africa/El_Aaiun','Africa/Freetown','Africa/Gaborone','Africa/Harare','Africa/Johannesburg','Africa/Juba','Africa/Kampala','Africa/Khartoum','Africa/Kigali','Africa/Kinshasa','Africa/Lagos','Africa/Libreville','Africa/Lome','Africa/Luanda','Africa/Lubumbashi','Africa/Lusaka','Africa/Malabo','Africa/Maputo','Africa/Maseru','Africa/Mbabane','Africa/Mogadishu','Africa/Monrovia','Africa/Nairobi','Africa/Ndjamena','Africa/Niamey','Africa/Nouakchott','Africa/Ouagadougou','Africa/Porto-Novo','Africa/Sao_Tome','Africa/Tripoli','Africa/Tunis','Africa/Windhoek','America/Adak','America/Anchorage','America/Anguilla','America/Antigua','America/Araguaina','America/Argentina/Buenos_Aires','America/Argentina/Catamarca','America/Argentina/Cordoba','America/Argentina/Jujuy','America/Argentina/La_Rioja','America/Argentina/Mendoza','America/Argentina/Rio_Gallegos','America/Argentina/Salta','America/Argentina/San_Juan','America/Argentina/San_Luis','America/Argentina/Tucuman','America/Argentina/Ushuaia','America/Aruba','America/Asuncion','America/Atikokan','America/Bahia','America/Bahia_Banderas','America/Barbados','America/Belem','America/Belize','America/Blanc-Sablon','America/Boa_Vista','America/Bogota','America/Boise','America/Cambridge_Bay','America/Campo_Grande','America/Cancun','America/Caracas','America/Cayenne','America/Cayman','America/Chicago','America/Chihuahua','America/Costa_Rica','America/Creston','America/Cuiaba','America/Curacao','America/Danmarkshavn','America/Dawson','America/Dawson_Creek','America/Denver','America/Detroit','America/Dominica','America/Edmonton','America/Eirunepe','America/El_Salvador','America/Fortaleza','America/Glace_Bay','America/Godthab','America/Goose_Bay','America/Grand_Turk','America/Grenada','America/Guadeloupe','America/Guatemala','America/Guayaquil','America/Guyana','America/Halifax','America/Havana','America/Hermosillo','America/Indiana/Indianapolis','America/Indiana/Knox','America/Indiana/Marengo','America/Indiana/Petersburg','America/Indiana/Tell_City','America/Indiana/Valparaiso','America/Indiana/Vevay','America/Indiana/Vincennes','America/Indiana/Winamac','America/Inuvik','America/Iqaluit','America/Jamaica','America/Juneau','America/Kentucky/Louisville','America/Kentucky/Monticello','America/Kralendijk','America/La_Paz','America/Lima','America/Los_Angeles','America/Lower_Princes','America/Maceio','America/Managua','America/Manaus','America/Marigot','America/Martinique','America/Matamoros','America/Mazatlan','America/Menominee','America/Merida','America/Metlakatla','America/Mexico_City','America/Miquelon','America/Moncton','America/Monterrey','America/Montevideo','America/Montreal','America/Montserrat','America/Nassau','America/New_York','America/Nipigon','America/Nome','America/Noronha','America/North_Dakota/Beulah','America/North_Dakota/Center','America/North_Dakota/New_Salem','America/Ojinaga','America/Panama','America/Pangnirtung','America/Paramaribo','America/Phoenix','America/Port_of_Spain','America/Port-au-Prince','America/Porto_Velho','America/Puerto_Rico','America/Rainy_River','America/Rankin_Inlet','America/Recife','America/Regina','America/Resolute','America/Rio_Branco','America/Santa_Isabel','America/Santarem','America/Santiago','America/Santo_Domingo','America/Sao_Paulo','America/Scoresbysund','America/Shiprock','America/Sitka','America/St_Barthelemy','America/St_Johns','America/St_Kitts','America/St_Lucia','America/St_Thomas','America/St_Vincent','America/Swift_Current','America/Tegucigalpa','America/Thule','America/Thunder_Bay','America/Tijuana','America/Toronto','America/Tortola','America/Vancouver','America/Whitehorse','America/Winnipeg','America/Yakutat','America/Yellowknife','Antarctica/Casey','Antarctica/Davis','Antarctica/DumontDUrville','Antarctica/Macquarie','Antarctica/Mawson','Antarctica/McMurdo','Antarctica/Palmer','Antarctica/Rothera','Antarctica/South_Pole','Antarctica/Syowa','Antarctica/Troll','Antarctica/Vostok','Arctic/Longyearbyen','Asia/Aden','Asia/Almaty','Asia/Amman','Asia/Anadyr','Asia/Aqtau','Asia/Aqtobe','Asia/Ashgabat','Asia/Baghdad','Asia/Bahrain','Asia/Baku','Asia/Bangkok','Asia/Beirut','Asia/Bishkek','Asia/Brunei','Asia/Choibalsan','Asia/Chongqing','Asia/Colombo','Asia/Damascus','Asia/Dhaka','Asia/Dili','Asia/Dubai','Asia/Dushanbe','Asia/Gaza','Asia/Harbin','Asia/Hebron','Asia/Ho_Chi_Minh','Asia/Hong_Kong','Asia/Hovd','Asia/Irkutsk','Asia/Jakarta','Asia/Jayapura','Asia/Jerusalem','Asia/Kabul','Asia/Kamchatka','Asia/Karachi','Asia/Kashgar','Asia/Kathmandu','Asia/Khandyga','Asia/Kolkata','Asia/Krasnoyarsk','Asia/Kuala_Lumpur','Asia/Kuching','Asia/Kuwait','Asia/Macau','Asia/Magadan','Asia/Makassar','Asia/Manila','Asia/Muscat','Asia/Nicosia','Asia/Novokuznetsk','Asia/Novosibirsk','Asia/Omsk','Asia/Oral','Asia/Phnom_Penh','Asia/Pontianak','Asia/Pyongyang','Asia/Qatar','Asia/Qyzylorda','Asia/Rangoon','Asia/Riyadh','Asia/Sakhalin','Asia/Samarkand','Asia/Seoul','Asia/Shanghai','Asia/Singapore','Asia/Taipei','Asia/Tashkent','Asia/Tbilisi','Asia/Tehran','Asia/Thimphu','Asia/Tokyo','Asia/Ulaanbaatar','Asia/Urumqi','Asia/Ust-Nera','Asia/Vientiane','Asia/Vladivostok','Asia/Yakutsk','Asia/Yekaterinburg','Asia/Yerevan','Atlantic/Azores','Atlantic/Bermuda','Atlantic/Canary','Atlantic/Cape_Verde','Atlantic/Faroe','Atlantic/Madeira','Atlantic/Reykjavik','Atlantic/South_Georgia','Atlantic/St_Helena','Atlantic/Stanley','Australia/Adelaide','Australia/Brisbane','Australia/Broken_Hill','Australia/Currie','Australia/Darwin','Australia/Eucla','Australia/Hobart','Australia/Lindeman','Australia/Lord_Howe','Australia/Melbourne','Australia/Perth','Australia/Sydney','Europe/Amsterdam','Europe/Andorra','Europe/Athens','Europe/Belgrade','Europe/Berlin','Europe/Bratislava','Europe/Brussels','Europe/Bucharest','Europe/Budapest','Europe/Busingen','Europe/Chisinau','Europe/Copenhagen','Europe/Dublin','Europe/Gibraltar','Europe/Guernsey','Europe/Helsinki','Europe/Isle_of_Man','Europe/Istanbul','Europe/Jersey','Europe/Kaliningrad','Europe/Kiev','Europe/Lisbon','Europe/Ljubljana','Europe/London','Europe/Luxembourg','Europe/Madrid','Europe/Malta','Europe/Mariehamn','Europe/Minsk','Europe/Monaco','Europe/Moscow','Europe/Oslo','Europe/Paris','Europe/Podgorica','Europe/Prague','Europe/Riga','Europe/Rome','Europe/Samara','Europe/San_Marino','Europe/Sarajevo','Europe/Simferopol','Europe/Skopje','Europe/Sofia','Europe/Stockholm','Europe/Tallinn','Europe/Tirane','Europe/Uzhgorod','Europe/Vaduz','Europe/Vatican','Europe/Vienna','Europe/Vilnius','Europe/Volgograd','Europe/Warsaw','Europe/Zagreb','Europe/Zaporozhye','Europe/Zurich','Indian/Antananarivo','Indian/Chagos','Indian/Christmas','Indian/Cocos','Indian/Comoro','Indian/Kerguelen','Indian/Mahe','Indian/Maldives','Indian/Mauritius','Indian/Mayotte','Indian/Reunion','Pacific/Apia','Pacific/Auckland','Pacific/Chatham','Pacific/Chuuk','Pacific/Easter','Pacific/Efate','Pacific/Enderbury','Pacific/Fakaofo','Pacific/Fiji','Pacific/Funafuti','Pacific/Galapagos','Pacific/Gambier','Pacific/Guadalcanal','Pacific/Guam','Pacific/Honolulu','Pacific/Johnston','Pacific/Kiritimati','Pacific/Kosrae','Pacific/Kwajalein','Pacific/Majuro','Pacific/Marquesas','Pacific/Midway','Pacific/Nauru','Pacific/Niue','Pacific/Norfolk','Pacific/Noumea','Pacific/Pago_Pago','Pacific/Palau','Pacific/Pitcairn','Pacific/Pohnpei','Pacific/Port_Moresby','Pacific/Rarotonga','Pacific/Saipan','Pacific/Tahiti','Pacific/Tarawa','Pacific/Tongatapu','Pacific/Wake','Pacific/Wallis');

// load in the dsp json data
$data_file = '';
$json_type = '';
if($_SESSION['device_id']=='nq-mix'){
	$data_file = file_get_contents("/etc/nyquist/mixer.json");
	$json_type = 'mixer';
}
if($_SESSION['device_id']=='nq-nqpaxxx'){
	$data_file = file_get_contents("/etc/nyquist/pama.json");
	$json_type = 'nqpaxxx';
}
if($_SESSION['device_id']=='nq-amp2c'){
	$data_file = file_get_contents("/etc/nyquist/amp2c.json");
	$json_type = 'ampg1_2c';
}
if($_SESSION['device_id']=='nq-amp4c'){
	$data_file = file_get_contents("/etc/nyquist/amp4c.json");
	$json_type = 'ampg1_4c';
}
if($_SESSION['device_id']=='nq-amp2c-g2'){
	$data_file = file_get_contents("/etc/nyquist/ampg2_2c.json");
	$json_type = 'ampg2_2c';
}
if($_SESSION['device_id']=='nq-amp4c-g2'){
	$data_file = file_get_contents("/etc/nyquist/ampg2_4c.json");
	$json_type = 'ampg2_4c';
}
if($_SESSION['device_id']=='nq-edp01'){
	$data_file = file_get_contents("/etc/nyquist/edp01.json");
	$json_type = 'edp01';
}

$message = '';
$message2 = '';
$message3 = '';
$message4 = '';
$message5 = '';

if(!$_SESSION['login']){
	header("Location: login.php");
}

if ($_SERVER["REQUEST_METHOD"] == "POST") {
	
	if (isset($_POST['save_configuration_settings'])) {
			
		$errorArray = array();
        $message = '';
        $device_type = $_POST["device_type"];
        $device_name = $_POST["device_name"];
        $web_username = $_POST["web_username"];
        $web_password = $_POST["web_password"];
        $timezone = $_POST["timezone"];
        $ptt_type = $_POST["ptt_type"];
        $ptt_type_name = $ptt_type;
        $ptt_output = $_POST["ptt_output"];
        $ptt_output_name = $ptt_output; 
        $speaker_load_impedance = $_POST["speaker_load_impedance"];
        $speaker_load_impedance_name = ucfirst($speaker_load_impedance);
        $speaker_output_power = $_POST["speaker_output_power"];
        $speaker_output_power_name = $speaker_output_power;
		$output_power = $_POST["output_power"];
		$talkback_gain = $_POST["talkback_gain"];
		
		$load_impedance1 = $_POST["load_impedance1"];
        $load_impedance_name1 = ucfirst($load_impedance1);
        $output_power1 = $_POST["output_power1"];
        $output_power_name1 = $output_power1;
		
		$load_impedance2 = $_POST["load_impedance2"];
        $load_impedance_name2 = ucfirst($load_impedance2);
        $output_power2 = $_POST["output_power2"];
        $output_power_name2 = $output_power2;
		
		$load_impedance3 = $_POST["load_impedance3"];
        $load_impedance_name3 = ucfirst($load_impedance3);
        $output_power3 = $_POST["output_power3"];
        $output_power_name3 = $output_power3;
		
		$load_impedance4 = $_POST["load_impedance4"];
        $load_impedance_name4 = ucfirst($load_impedance4);
        $output_power4 = $_POST["output_power4"];
        $output_power_name4 = $output_power4;
		
		$sip_enabled = $_POST["sip_enabled"];
		$sip_enabled_name = $_POST["sip_enabled_name"];
		
		if($_SESSION['device_id']=='nq-edp01'){
			$sip_enabled = 1;
		}
		
		if(isset($_POST["line_in_priority"])){
			$line_in_priority = $_POST["line_in_priority"];
		} else {
			$line_in_priority = '0';
		}
		
		$account_reg_uri = $_POST["account_reg_uri"];
		$account_id = $_POST["account_id"];
		$local_port = $_POST["local_port"];
		$account_username = $_POST["account_username"];
		$account_password = $_POST["account_password"];
		$account_id_1 = $_POST["account_id_1"];
		$local_port_1 = $local_port;
		$account_username_1 = $_POST["account_username_1"];
		$account_password_1 = $_POST["account_password_1"];
		$intercom_cut_level_1 = $_POST["intercom_cut_level_1"];
		$account_id_2 = $_POST["account_id_2"];
		$local_port_2 = $local_port;
		$account_username_2 = $_POST["account_username_2"];
		$account_password_2 = $_POST["account_password_2"];
		$intercom_cut_level_2 = $_POST["intercom_cut_level_2"];
		$account_id_3 = $_POST["account_id_3"];
		$local_port_3 = $local_port;
		$account_username_3 = $_POST["account_username_3"];
		$account_password_3 = $_POST["account_password_3"];
		$intercom_cut_level_3 = $_POST["intercom_cut_level_3"];
		$account_id_4 = $_POST["account_id_4"];
		$local_port_4 = $local_port;
		$account_username_4 = $_POST["account_username_4"];
		$account_password_4 = $_POST["account_password_4"];
		$intercom_cut_level_4 = $_POST["intercom_cut_level_4"];
		
		$external_relay_trigger = $_POST["external_relay_trigger_old"];
		$external_relay_trigger_1 = $_POST["external_relay_trigger_1_old"];
		$external_relay_trigger_2 = $_POST["external_relay_trigger_2_old"];
		
		$dial_extension = $_POST["dial_extension"];
		$intercom_cut_level = $_POST["intercom_cut_level"];
		$port_type = $_POST["port_type"];
			
		if($sip_enabled=='1' && $account_reg_uri==''){
			$errorArray[] = "A SIP Server Address is required when Enable SIP Calls is set to Yes.";
		}
		
        if (empty($_POST["web_username"])) {
            $errorArray[] = "Web username is required.";
        }
        if ($_POST["web_password"] != $_POST["web_password_confirm"]) {
            $errorArray[] = "Web passwords do not match.";
        }

        if(count($errorArray)>0){
            // show error messages
            $message = '<div class="isa_error"><i class="fa fa-times-circle"></i><div class="message_content">';
            foreach($errorArray as $error){
                $message .= $error . '<br>';
            }
            $message .= '</div></div>';
        } else {

            // edit standalone.config
            // read the current file
            $standalone = fopen("/etc/nyquist/standalone.config", "r");
            $config_file_array = array();
            if(!$standalone){
              // show error message
              $message = '<div class="isa_error"><i class="fa fa-times-circle"></i><div class="message_content">';
              $message .= 'There was a problem reading the Standalone Configuration file.';
              $message .= '</div></div>';
             } else { 
                // go through the config file one line at a time
                while(!feof($standalone)){
                    $line_text = fgets($standalone);
                    array_push($config_file_array,$line_text);
                }
                fclose($config_file);
            }

            // search through rows and insert changes
            $edited_array = array();
            foreach($config_file_array as $key => $row){

                if(strpos($row, 'DEVICE_NAME =') !== FALSE){
                    $new_row = 'DEVICE_NAME = '. $device_name. '' . PHP_EOL;
                    array_push($edited_array, $new_row);
                } else if(strpos($row, 'WEB_USERNAME =') !== FALSE){
                    $new_row = 'WEB_USERNAME = '. $web_username . '' . PHP_EOL;
                    array_push($edited_array, $new_row);
                } else if(strpos($row, 'WEB_PASSWORD =') !== FALSE && $web_password!=''){
                    $new_row = 'WEB_PASSWORD = '. $web_password . '' . PHP_EOL;
                    array_push($edited_array, $new_row);
                } else if(strpos($row, 'TIMEZONE =') !== FALSE){
                    $new_row = 'TIMEZONE = '. $timezone . '' . PHP_EOL;
                    array_push($edited_array, $new_row);
                } else if(strpos($row, 'PTT_TYPE =') !== FALSE){
                    $new_row = 'PTT_TYPE = '. $ptt_type . '' . PHP_EOL;
                    array_push($edited_array, $new_row);
                } else if(strpos($row, 'PTT_OUTPUT =') !== FALSE){
                    $new_row = 'PTT_OUTPUT = '. $ptt_output . '' . PHP_EOL;
                    array_push($edited_array, $new_row);
                } else if(strpos($row, 'SPEAKER_LOAD_IMPEDANCE =') !== FALSE){
                    $new_row = 'SPEAKER_LOAD_IMPEDANCE = '. $speaker_load_impedance . '' . PHP_EOL;
                    array_push($edited_array, $new_row);
                } else if(strpos($row, 'SPEAKER_OUTPUT_POWER =') !== FALSE){
                    $new_row = 'SPEAKER_OUTPUT_POWER = '. $speaker_output_power . '' . PHP_EOL;
                    array_push($edited_array, $new_row);
				} else if(strpos($row, 'OUTPUT_POWER =') !== FALSE && isset($_POST["output_power"])){
                    $new_row = 'OUTPUT_POWER = '. $output_power . '' . PHP_EOL;
                    array_push($edited_array, $new_row);
				} else if(strpos($row, 'TALKBACK_GAIN =') !== FALSE && isset($_POST["talkback_gain"])){
                    $new_row = 'TALKBACK_GAIN = '. $talkback_gain . '' . PHP_EOL;
                    array_push($edited_array, $new_row);
				} else if(strpos($row, 'LOAD_IMPEDANCE_1 =') !== FALSE && isset($_POST["load_impedance1"])){
                    $new_row = 'LOAD_IMPEDANCE_1 = '. $load_impedance1 . '' . PHP_EOL;
                    array_push($edited_array, $new_row);
                } else if(strpos($row, 'OUTPUT_POWER_1 =') !== FALSE && isset($_POST["output_power1"])){
                    $new_row = 'OUTPUT_POWER_1 = '. $output_power1 . '' . PHP_EOL;
                    array_push($edited_array, $new_row);
				} else if(strpos($row, 'LOAD_IMPEDANCE_2 =') !== FALSE && isset($_POST["load_impedance2"])){
                    $new_row = 'LOAD_IMPEDANCE_2 = '. $load_impedance2 . '' . PHP_EOL;
                    array_push($edited_array, $new_row);
				} else if(strpos($row, 'LOAD_IMPEDANCE_2 =') !== FALSE && !isset($_POST["load_impedance2"])){
                    $new_row = 'LOAD_IMPEDANCE_2 = ' . '' . PHP_EOL;
                    array_push($edited_array, $new_row);
                } else if(strpos($row, 'OUTPUT_POWER_2 =') !== FALSE && isset($_POST["output_power2"])){
                    $new_row = 'OUTPUT_POWER_2 = '. $output_power2 . '' . PHP_EOL;
                    array_push($edited_array, $new_row);
				 } else if(strpos($row, 'OUTPUT_POWER_2 =') !== FALSE && !isset($_POST["output_power2"])){
                    $new_row = 'OUTPUT_POWER_2 = ' . '' . PHP_EOL;
                    array_push($edited_array, $new_row);
				} else if(strpos($row, 'LOAD_IMPEDANCE_3 =') !== FALSE && isset($_POST["load_impedance3"])){
                    $new_row = 'LOAD_IMPEDANCE_3 = '. $load_impedance3 . '' . PHP_EOL;
                    array_push($edited_array, $new_row);
                } else if(strpos($row, 'OUTPUT_POWER_3 =') !== FALSE && isset($_POST["output_power3"])){
                    $new_row = 'OUTPUT_POWER_3 = '. $output_power3 . '' . PHP_EOL;
                    array_push($edited_array, $new_row);
				} else if(strpos($row, 'LOAD_IMPEDANCE_4 =') !== FALSE && isset($_POST["load_impedance4"])){
                    $new_row = 'LOAD_IMPEDANCE_4 = '. $load_impedance4 . '' . PHP_EOL;
                    array_push($edited_array, $new_row);
				} else if(strpos($row, 'LOAD_IMPEDANCE_4 =') !== FALSE && !isset($_POST["load_impedance4"])){
                    $new_row = 'LOAD_IMPEDANCE_4 = ' . '' . PHP_EOL;
                    array_push($edited_array, $new_row);
                } else if(strpos($row, 'OUTPUT_POWER_4 =') !== FALSE && isset($_POST["output_power4"])){
                    $new_row = 'OUTPUT_POWER_4 = '. $output_power4 . '' . PHP_EOL;
                    array_push($edited_array, $new_row);
				} else if(strpos($row, 'OUTPUT_POWER_4 =') !== FALSE && !isset($_POST["output_power4"])){
                    $new_row = 'OUTPUT_POWER_4 = ' . '' . PHP_EOL;
                    array_push($edited_array, $new_row);
				} else if(strpos($row, 'SIP_ENABLED =') !== FALSE){
                    $new_row = 'SIP_ENABLED = '. $sip_enabled . '' . PHP_EOL;
                    array_push($edited_array, $new_row);
				} else if(strpos($row, 'ACCOUNT_REG_URI =') !== FALSE && isset($_POST["account_reg_uri"])){
                    $new_row = 'ACCOUNT_REG_URI = sip:'. $account_reg_uri . '' . PHP_EOL;
                    array_push($edited_array, $new_row);
				} else if(strpos($row, 'ACCOUNT_ID =') !== FALSE && isset($_POST["account_id"])){
					if($account_id!=''){
						 $new_row = 'ACCOUNT_ID = sip:'. $account_id . '@' . $account_reg_uri . '' . PHP_EOL;
					} else {
						 $new_row = 'ACCOUNT_ID =' . '' . PHP_EOL;
					}
                    array_push($edited_array, $new_row);
				} else if(strpos($row, 'LOCAL_PORT =') !== FALSE && isset($_POST["local_port"])){
                    $new_row = 'LOCAL_PORT = '. $local_port . '' . PHP_EOL;
					 array_push($edited_array, $new_row);
				} else if(strpos($row, 'ACCOUNT_USERNAME =') !== FALSE && isset($_POST["account_username"])){
                    $new_row = 'ACCOUNT_USERNAME = '. $account_username . '' . PHP_EOL;
                    array_push($edited_array, $new_row);
				} else if(strpos($row, 'ACCOUNT_PASSWORD =') !== FALSE && isset($_POST["account_password"])){
                    $new_row = 'ACCOUNT_PASSWORD = '. $account_password . '' . PHP_EOL;
                    array_push($edited_array, $new_row);
				} else if(strpos($row, 'ACCOUNT_ID_1 =') !== FALSE && isset($_POST["account_id_1"])){
					if($account_id_1!=''){
						 $new_row = 'ACCOUNT_ID_1 = sip:'. $account_id_1 . '@' . $account_reg_uri . '' . PHP_EOL;
					} else {
						 $new_row = 'ACCOUNT_ID_1 =' . '' . PHP_EOL;
					}
                    array_push($edited_array, $new_row);
				} else if(strpos($row, 'LOCAL_PORT_1 =') !== FALSE && isset($_POST["local_port"])){
                    $new_row = 'LOCAL_PORT_1 = '. $local_port_1 . '' . PHP_EOL;
					 array_push($edited_array, $new_row);
				} else if(strpos($row, 'ACCOUNT_USERNAME_1 =') !== FALSE && isset($_POST["account_username_1"])){
                    $new_row = 'ACCOUNT_USERNAME_1 = '. $account_username_1 . '' . PHP_EOL;
                    array_push($edited_array, $new_row);
				} else if(strpos($row, 'ACCOUNT_PASSWORD_1 =') !== FALSE && isset($_POST["account_password_1"])){
                    $new_row = 'ACCOUNT_PASSWORD_1 = '. $account_password_1 . '' . PHP_EOL;
                    array_push($edited_array, $new_row);
				} else if(strpos($row, 'INTERCOM_VOLUME_1 =') !== FALSE && isset($_POST["intercom_cut_level_1"])){
                    $new_row = 'INTERCOM_VOLUME_1 = '. $intercom_cut_level_1 . '' . PHP_EOL;
                    array_push($edited_array, $new_row);	
				} else if(strpos($row, 'ACCOUNT_ID_2 =') !== FALSE && isset($_POST["account_id_2"])){
					if($account_id_2!=''){
						 $new_row = 'ACCOUNT_ID_2 = sip:'. $account_id_2 . '@' . $account_reg_uri . '' . PHP_EOL;
					} else {
						 $new_row = 'ACCOUNT_ID_2 =' . '' . PHP_EOL;
					}
                    array_push($edited_array, $new_row);
				} else if(strpos($row, 'ACCOUNT_ID_2 =') !== FALSE && !isset($_POST["account_id_2"]) && $sip_enabled==1){
					$new_row = 'ACCOUNT_ID_2 =' . '' . PHP_EOL;
					array_push($edited_array, $new_row);
				} else if(strpos($row, 'LOCAL_PORT_2 =') !== FALSE && isset($_POST["local_port"]) && $is_dual_channel==1){
                    $new_row = 'LOCAL_PORT_2 = '. $local_port_2 . '' . PHP_EOL;
                    array_push($edited_array, $new_row);
				} else if(strpos($row, 'LOCAL_PORT_2 =') !== FALSE && $is_dual_channel==0){
                    $new_row = 'LOCAL_PORT_2 = ' . '' . PHP_EOL;
                    array_push($edited_array, $new_row);
				} else if(strpos($row, 'ACCOUNT_USERNAME_2 =') !== FALSE && isset($_POST["account_username_2"])){
                    $new_row = 'ACCOUNT_USERNAME_2 = '. $account_username_2 . '' . PHP_EOL;
                    array_push($edited_array, $new_row);
				} else if(strpos($row, 'ACCOUNT_USERNAME_2 =') !== FALSE && !isset($_POST["account_username_2"]) && $sip_enabled==1){
                    $new_row = 'ACCOUNT_USERNAME_2 = ' . '' . PHP_EOL;
                    array_push($edited_array, $new_row);
				} else if(strpos($row, 'ACCOUNT_PASSWORD_2 =') !== FALSE && isset($_POST["account_password_2"])){
                    $new_row = 'ACCOUNT_PASSWORD_2 = '. $account_password_2 . '' . PHP_EOL;
                    array_push($edited_array, $new_row);
				} else if(strpos($row, 'ACCOUNT_PASSWORD_2 =') !== FALSE && !isset($_POST["account_password_2"]) && $sip_enabled==1){
                    $new_row = 'ACCOUNT_PASSWORD_2 = ' . '' . PHP_EOL;
                    array_push($edited_array, $new_row);
				} else if(strpos($row, 'INTERCOM_VOLUME_2 =') !== FALSE && isset($_POST["intercom_cut_level_2"])){
                    $new_row = 'INTERCOM_VOLUME_2 = '. $intercom_cut_level_2 . '' . PHP_EOL;
                    array_push($edited_array, $new_row);
				} else if(strpos($row, 'ACCOUNT_ID_3 =') !== FALSE && isset($_POST["account_id_3"])){
					if($account_id_3!=''){
						 $new_row = 'ACCOUNT_ID_3 = sip:'. $account_id_3 . '@' . $account_reg_uri . '' . PHP_EOL;
					} else {
						 $new_row = 'ACCOUNT_ID_3 =' . '' . PHP_EOL;
					}
                    array_push($edited_array, $new_row);
				} else if(strpos($row, 'LOCAL_PORT_3 =') !== FALSE && isset($_POST["local_port"])){
                    $new_row = 'LOCAL_PORT_3 = '. $local_port_3 . '' . PHP_EOL;
                    array_push($edited_array, $new_row);
				} else if(strpos($row, 'ACCOUNT_USERNAME_3 =') !== FALSE && isset($_POST["account_username_3"])){
                    $new_row = 'ACCOUNT_USERNAME_3 = '. $account_username_3 . '' . PHP_EOL;
                    array_push($edited_array, $new_row);
				} else if(strpos($row, 'ACCOUNT_PASSWORD_3 =') !== FALSE && isset($_POST["account_password_3"])){
                    $new_row = 'ACCOUNT_PASSWORD_3 = '. $account_password_3 . '' . PHP_EOL;
                    array_push($edited_array, $new_row);
				} else if(strpos($row, 'INTERCOM_VOLUME_3 =') !== FALSE && isset($_POST["intercom_cut_level_3"])){
                    $new_row = 'INTERCOM_VOLUME_3 = '. $intercom_cut_level_3 . '' . PHP_EOL;
                    array_push($edited_array, $new_row);
				} else if(strpos($row, 'ACCOUNT_ID_4 =') !== FALSE && isset($_POST["account_id_4"])){
					if($account_id_4!=''){
						 $new_row = 'ACCOUNT_ID_4 = sip:'. $account_id_4 . '@' . $account_reg_uri . '' . PHP_EOL;
					} else {
						 $new_row = 'ACCOUNT_ID_4 =' . '' . PHP_EOL;
					}
                    array_push($edited_array, $new_row);
				} else if(strpos($row, 'ACCOUNT_ID_4 =') !== FALSE && !isset($_POST["account_id_4"]) && $sip_enabled==1){
					$new_row = 'ACCOUNT_ID_4 =' . '' . PHP_EOL;
					array_push($edited_array, $new_row);
				} else if(strpos($row, 'LOCAL_PORT_4 =') !== FALSE && isset($_POST["local_port"]) && $is_dual_channel2==1){
                    $new_row = 'LOCAL_PORT_4 = '. $local_port_4 . '' . PHP_EOL;
                    array_push($edited_array, $new_row);
				} else if(strpos($row, 'LOCAL_PORT_4 =') !== FALSE && $is_dual_channel2==0){
                    $new_row = 'LOCAL_PORT_4 = ' . '' . PHP_EOL;
                    array_push($edited_array, $new_row);
				} else if(strpos($row, 'ACCOUNT_USERNAME_4 =') !== FALSE && isset($_POST["account_username_4"])){
                    $new_row = 'ACCOUNT_USERNAME_4 = '. $account_username_4 . '' . PHP_EOL;
                    array_push($edited_array, $new_row);
				} else if(strpos($row, 'ACCOUNT_USERNAME_4 =') !== FALSE && !isset($_POST["account_username_4"]) && $sip_enabled==1){
                    $new_row = 'ACCOUNT_USERNAME_4 = ' . '' . PHP_EOL;
                    array_push($edited_array, $new_row);
				} else if(strpos($row, 'ACCOUNT_PASSWORD_4 =') !== FALSE && isset($_POST["account_password_4"])){
                    $new_row = 'ACCOUNT_PASSWORD_4 = '. $account_password_4 . '' . PHP_EOL;
                    array_push($edited_array, $new_row);
				} else if(strpos($row, 'ACCOUNT_PASSWORD_4 =') !== FALSE && !isset($_POST["account_password_4"]) && $sip_enabled==1){
                    $new_row = 'ACCOUNT_PASSWORD_4 = ' . '' . PHP_EOL;
                    array_push($edited_array, $new_row);
				} else if(strpos($row, 'INTERCOM_VOLUME_4 =') !== FALSE && isset($_POST["intercom_cut_level_4"])){
                    $new_row = 'INTERCOM_VOLUME_4 = '. $intercom_cut_level_4 . '' . PHP_EOL;
                    array_push($edited_array, $new_row);
				} else if(strpos($row, 'LINE_IN_PRIORITY =') !== FALSE){
                    $new_row = 'LINE_IN_PRIORITY = '. $line_in_priority . '' . PHP_EOL;
                    array_push($edited_array, $new_row);
				} else if(strpos($row, 'DIAL_EXTENSION =') !== FALSE){
                    $new_row = 'DIAL_EXTENSION = '. $dial_extension . '' . PHP_EOL;
                    array_push($edited_array, $new_row);
				} else if(strpos($row, 'PORT_TYPE =') !== FALSE){
                    $new_row = 'PORT_TYPE = '. $port_type . '' . PHP_EOL;
                    array_push($edited_array, $new_row);
				} else if(strpos($row, 'INTERCOM_VOLUME =') !== FALSE && isset($_POST["intercom_cut_level"])){
                    $new_row = 'INTERCOM_VOLUME = '. $intercom_cut_level . '' . PHP_EOL;
                    array_push($edited_array, $new_row);
				} else if(strpos($row, 'EXTERNAL_RELAY_TRIGGER =') !== FALSE){
                    $new_row = 'EXTERNAL_RELAY_TRIGGER = '. $external_relay_trigger . '' . PHP_EOL;
                    array_push($edited_array, $new_row);
				} else if(strpos($row, 'EXTERNAL_RELAY_TRIGGER_1 =') !== FALSE){
                    $new_row = 'EXTERNAL_RELAY_TRIGGER_1 = '. $external_relay_trigger_1 . '' . PHP_EOL;
                    array_push($edited_array, $new_row);
				} else if(strpos($row, 'EXTERNAL_RELAY_TRIGGER_2 =') !== FALSE){
                    $new_row = 'EXTERNAL_RELAY_TRIGGER_2 = '. $external_relay_trigger_2 . '' . PHP_EOL;
                    array_push($edited_array, $new_row);
                } else { 
                    array_push($edited_array, $row);
                }
            }

            // create new file
            $new_config_file = '';
            foreach($edited_array as $row){
                $new_config_file .= $row;
            }

            // write the file
            $config_file = fopen("/etc/nyquist/standalone.config", "w");

            if(!$config_file){
                // show error message
                $message = '<div class="isa_error"><i class="fa fa-times-circle"></i><div class="message_content">';
                $message .= 'Save failed. There was a problem writing to the Standalone Configuration file.';
                $message .= '</div></div>';

            } else {
                // complete write
                fwrite($config_file, $new_config_file);
                fclose($config_file);	

                $script_path = 'sudo /usr/local/bin/update_configuration';
                $run_script = shell_exec($script_path);

                // show success message
                $message = '<div class="isa_success"><i class="fa fa-check"></i><div class="message_content">';
                $message .='Configuration settings saved.<br>';
                $message .= '</div></div>';
            }
        } 
	}
	
	if (isset($_POST['save_multicast_addresses'])) {
		
		$dataArray = $_POST['dataArray']; 
			
		$multicasts = json_decode($dataArray);
		
		// edit standalone.config
        // read the current file
        $standalone = fopen("/etc/nyquist/standalone.config", "r");
        $config_file_array = array();
        if(!$standalone){
          // show error message
          $message2 = '<div class="isa_error"><i class="fa fa-times-circle"></i><div class="message_content">';
          $message2 .= 'There was a problem reading the Standalone Configuration file.';
          $message2 .= '</div></div>';
         } else { 
            // go through the config file one line at a time
            while(!feof($standalone)){
                $line_text = fgets($standalone);
                array_push($config_file_array,$line_text);
            }
            fclose($config_file);
        }

		// search through rows and insert changes
        $edited_array = array();
        foreach($config_file_array as $key => $row){

            if(strpos($row, 'MULTICAST_LIST_') !== FALSE){
                // remove current multicasts
            } else { 
                array_push($edited_array, $row);
            }
        }
		foreach($multicasts as $multicast){
			// MULTICAST_LIST_1 = 239.1.1.10:6100[G722]{1,2} OR optional 239.1.1.10:6100[G722]{1,2}<-20>Description text.
			if($multicast[0]=='' || $multicast[1]=='' || $multicast[2]=='' || $multicast[3]=='' || $multicast[4]==''){
				// don't save if any fields are empty
			} else {
				
				$multicast6 = str_replace( array( '[', ']', '{' , '}', '<', '>', ',', '|', ':' ), ' ', $multicast[6]); // remove special characters
				$row = 'MULTICAST_LIST_'.$multicast[0].' = '.$multicast[1].':'.$multicast[2].'['.$multicast[3].']{'.$multicast[4].'}<'.$multicast[5].'>'.$multicast6. '' . PHP_EOL;
				array_push($edited_array, $row);
			}
		}

        // create new file
        $new_config_file = '';
        foreach($edited_array as $row){
            $new_config_file .= $row;
        }

        // write the file
        $config_file = fopen("/etc/nyquist/standalone.config", "w");

        if(!$config_file){
            // show error message
            $message2 = '<div class="isa_error"><i class="fa fa-times-circle"></i><div class="message_content">';
            $message2 .= 'Save failed. There was a problem writing to the Standalone Configuration file.';
            $message2 .= '</div></div>';

        } else {
            // complete write
            fwrite($config_file, $new_config_file);
            fclose($config_file);	

            $script_path = 'sudo /usr/local/bin/update_configuration';
            $run_script = shell_exec($script_path);

            // show success message
            $message2 = '<div class="isa_success"><i class="fa fa-check"></i><div class="message_content">';
            $message2 .='Multicast Addresses (Receive) saved.<br>';
            $message2 .= '</div></div>';
        }
	}
	
	if (isset($_POST['save_multicast_addresses2'])) {
		
		$dataArray2 = $_POST['dataArray2']; 
			
		$audio_casts = json_decode($dataArray2);
		
		// edit standalone.config
        // read the current file
        $standalone = fopen("/etc/nyquist/standalone.config", "r");
        $config_file_array = array();
        if(!$standalone){
          // show error message
          $message3 = '<div class="isa_error"><i class="fa fa-times-circle"></i><div class="message_content">';
          $message3 .= 'There was a problem reading the Standalone Configuration file.';
          $message3 .= '</div></div>';
         } else { 
            // go through the config file one line at a time
            while(!feof($standalone)){
                $line_text = fgets($standalone);
                array_push($config_file_array,$line_text);
            }
            fclose($config_file);
        }

		// search through rows and insert changes
        $edited_array = array();
        foreach($config_file_array as $key => $row){

            if(strpos($row, 'LINE_STREAM') !== FALSE){
                // remove current multicasts
            } else { 
                array_push($edited_array, $row);
            }
        }
		
		$audio_cast_errors = 0;
		foreach($audio_casts as $audio_cast){
			if($audio_cast[0]==1){
                $row = 'LINE_STREAM_ENABLE_'.$audio_cast[0].' = '.strtolower($audio_cast[6]).''.PHP_EOL;
                array_push($edited_array, $row);
            } 
            if($audio_cast[0]==2){
                $row = 'LINE_STREAM_ENABLE_'.$audio_cast[0].' = '.strtolower($audio_cast[6]).''.PHP_EOL;
                array_push($edited_array, $row);
            } 
		}
		foreach($audio_casts as $audio_cast){
			// LINE_STREAM_1 = 239.1.1.10:6100[G722]{1,2}<-20>
			if($audio_cast[1]=='' || $audio_cast[2]==''){
				$row = 'LINE_STREAM_'.$audio_cast[0].' = '.PHP_EOL;
				array_push($edited_array, $row);
				$audio_cast_errors++;
			} else {
				$row = 'LINE_STREAM_'.$audio_cast[0].' = '.$audio_cast[1].':'.$audio_cast[2].'['.$audio_cast[3].']{'.$audio_cast[4].'}'.$audio_cast[5].''.PHP_EOL;
				array_push($edited_array, $row);
			}
		}
	
        // create new file
        $new_config_file = '';
        foreach($edited_array as $row){
            $new_config_file .= $row;
        }

        // write the file
        $config_file = fopen("/etc/nyquist/standalone.config", "w");

        if(!$config_file){
            // show error message
            $message3 = '<div class="isa_error"><i class="fa fa-times-circle"></i><div class="message_content">';
            $message3 .= 'Save failed. There was a problem writing to the Standalone Configuration file.';
            $message3 .= '</div></div>';
        } else {
            // complete write
            fwrite($config_file, $new_config_file);
            fclose($config_file);	

            $script_path = 'sudo /usr/local/bin/update_configuration';
            $run_script = shell_exec($script_path);
			
			if($audio_cast_errors>0){
				// show error message
				$message3 = '<div class="isa_error"><i class="fa fa-times-circle"></i><div class="message_content">';
				$message3 .= 'Rows with Empty data not saved.';
				$message3 .= '</div></div>';
				
			} else {
				// show success message
				$message3 = '<div class="isa_success"><i class="fa fa-check"></i><div class="message_content">';
            	$message3 .='Multicast Addresses (Transmit / Audiocast) saved.<br>';
				$message3 .= '</div></div>';
			}
        }
	}
	
	if (isset($_POST['save_unicast_stream'])) {
		
		$unicast_stream_enable_data = $_POST["unicast_stream_enable_data"];
		$unicast_stream_source_data = $_POST["unicast_stream_source_data"];
		$unicast_stream_address_data = $_POST["unicast_stream_address_data"];
		$unicast_stream_port_data = $_POST["unicast_stream_port_data"];
		$unicast_stream_codec_data = $_POST["unicast_stream_codec_data"];
		$unicast_stream_description_data = $_POST["unicast_stream_description_data"];
		
		if(count($errorArray)>0){
            // show error messages
            $message = '<div class="isa_error"><i class="fa fa-times-circle"></i><div class="message_content">';
            foreach($errorArray as $error){
                $message .= $error . '<br>';
            }
            $message .= '</div></div>';
        } else {

            // edit standalone.config
            // read the current file
            $standalone = fopen("/etc/nyquist/standalone.config", "r");
            $config_file_array = array();
            if(!$standalone){
              // show error message
              $message = '<div class="isa_error"><i class="fa fa-times-circle"></i><div class="message_content">';
              $message .= 'There was a problem reading the Standalone Configuration file.';
              $message .= '</div></div>';
             } else { 
                // go through the config file one line at a time
                while(!feof($standalone)){
                    $line_text = fgets($standalone);
                    array_push($config_file_array,$line_text);
                }
                fclose($config_file);
            }

            // search through rows and insert changes
            $edited_array = array();
            foreach($config_file_array as $key => $row){

                if(strpos($row, 'UNICAST_STREAM_ENABLE =') !== FALSE){
                    $new_row = 'UNICAST_STREAM_ENABLE = '. strtolower($unicast_stream_enable_data). '' . PHP_EOL;
                    array_push($edited_array, $new_row);
                } else if(strpos($row, 'UNICAST_STREAM_SOURCE =') !== FALSE){
                    $new_row = 'UNICAST_STREAM_SOURCE = '. $unicast_stream_source_data . '' . PHP_EOL;
                    array_push($edited_array, $new_row);
				 } else if(strpos($row, 'UNICAST_STREAM_ADDRESS =') !== FALSE){
                    $new_row = 'UNICAST_STREAM_ADDRESS = '. $unicast_stream_address_data . '' . PHP_EOL;
                    array_push($edited_array, $new_row);
				 } else if(strpos($row, 'UNICAST_STREAM_PORT =') !== FALSE){
                    $new_row = 'UNICAST_STREAM_PORT = '. $unicast_stream_port_data . '' . PHP_EOL;
                    array_push($edited_array, $new_row);
				 } else if(strpos($row, 'UNICAST_STREAM_CODEC =') !== FALSE){
                    $new_row = 'UNICAST_STREAM_CODEC = '. $unicast_stream_codec_data . '' . PHP_EOL;
                    array_push($edited_array, $new_row);
				} else if(strpos($row, 'UNICAST_STREAM_DESCRIPTION =') !== FALSE){
                    $new_row = 'UNICAST_STREAM_DESCRIPTION = '. $unicast_stream_description_data . '' . PHP_EOL;
                    array_push($edited_array, $new_row);
                } else { 
                    array_push($edited_array, $row);
                }
            }

            // create new file
            $new_config_file = '';
            foreach($edited_array as $row){
                $new_config_file .= $row;
            }

            // write the file
            $config_file = fopen("/etc/nyquist/standalone.config", "w");

            if(!$config_file){
                // show error message
                $message4 = '<div class="isa_error"><i class="fa fa-times-circle"></i><div class="message_content">';
                $message4 .= 'Save failed. There was a problem writing to the Standalone Configuration file.';
                $message4 .= '</div></div>';

            } else {
                // complete write
                fwrite($config_file, $new_config_file);
                fclose($config_file);	

                $script_path = 'sudo /usr/local/bin/update_configuration';
                $run_script = shell_exec($script_path);

                // show success message
                $message4 = '<div class="isa_success"><i class="fa fa-check"></i><div class="message_content">';
                $message4 .='Unicast Address (Transmit / Audiocast) saved.<br>';
                $message4 .= '</div></div>';
            }
        }
	}
	if (isset($_POST['save_unicast_receive'])) {
		
		$unicast_receive_enable_data = $_POST["unicast_receive_enable_data"];
		$unicast_receive_port_data = $_POST["unicast_receive_port_data"];
		$unicast_receive_line_output_data = $_POST["unicast_receive_line_output_data"];
		$unicast_receive_codec_data = $_POST["unicast_receive_codec_data"];
		$unicast_receive_cut_data = $_POST["unicast_receive_cut_data"];
		$unicast_receive_description_data = $_POST["unicast_receive_description_data"];
		
		if($unicast_receive_line_output_data=='1,2'){
			$unicast_receive_line_output_data = 'Both';
		}
		
		if(count($errorArray)>0){
            // show error messages
            $message = '<div class="isa_error"><i class="fa fa-times-circle"></i><div class="message_content">';
            foreach($errorArray as $error){
                $message .= $error . '<br>';
            }
            $message .= '</div></div>';
        } else {

            // edit standalone.config
            // read the current file
            $standalone = fopen("/etc/nyquist/standalone.config", "r");
            $config_file_array = array();
            if(!$standalone){
              // show error message
              $message = '<div class="isa_error"><i class="fa fa-times-circle"></i><div class="message_content">';
              $message .= 'There was a problem reading the Standalone Configuration file.';
              $message .= '</div></div>';
             } else { 
                // go through the config file one line at a time
                while(!feof($standalone)){
                    $line_text = fgets($standalone);
                    array_push($config_file_array,$line_text);
                }
                fclose($config_file);
            }

            // search through rows and insert changes
            $edited_array = array();
            foreach($config_file_array as $key => $row){

                if(strpos($row, 'UNICAST_RECEIVE_ENABLE =') !== FALSE){
                    $new_row = 'UNICAST_RECEIVE_ENABLE = '. strtolower($unicast_receive_enable_data). '' . PHP_EOL;
                    array_push($edited_array, $new_row);
				} else if(strpos($row, 'UNICAST_RECEIVE_PORT =') !== FALSE){
                    $new_row = 'UNICAST_RECEIVE_PORT = '. $unicast_receive_port_data . '' . PHP_EOL;
                    array_push($edited_array, $new_row);
                } else if(strpos($row, 'UNICAST_RECEIVE_LINE_OUTPUT =') !== FALSE){
                    $new_row = 'UNICAST_RECEIVE_LINE_OUTPUT = '. $unicast_receive_line_output_data . '' . PHP_EOL;
                    array_push($edited_array, $new_row);
				 } else if(strpos($row, 'UNICAST_RECEIVE_CODEC =') !== FALSE){
                    $new_row = 'UNICAST_RECEIVE_CODEC = '. $unicast_receive_codec_data . '' . PHP_EOL;
                    array_push($edited_array, $new_row);
				} else if(strpos($row, 'UNICAST_RECEIVE_CUT =') !== FALSE){
                    $new_row = 'UNICAST_RECEIVE_CUT = '. $unicast_receive_cut_data . '' . PHP_EOL;
                    array_push($edited_array, $new_row);
				} else if(strpos($row, 'UNICAST_RECEIVE_DESCRIPTION =') !== FALSE){
                    $new_row = 'UNICAST_RECEIVE_DESCRIPTION = '. $unicast_receive_description_data . '' . PHP_EOL;
                    array_push($edited_array, $new_row);
                } else { 
                    array_push($edited_array, $row);
                }
            }

            // create new file
            $new_config_file = '';
            foreach($edited_array as $row){
                $new_config_file .= $row;
            }

            // write the file
            $config_file = fopen("/etc/nyquist/standalone.config", "w");

            if(!$config_file){
                // show error message
                $message5 = '<div class="isa_error"><i class="fa fa-times-circle"></i><div class="message_content">';
                $message5 .= 'Save failed. There was a problem writing to the Standalone Configuration file.';
                $message5 .= '</div></div>';

            } else {
                // complete write
                fwrite($config_file, $new_config_file);
                fclose($config_file);	

                $script_path = 'sudo /usr/local/bin/update_configuration';
                $run_script = shell_exec($script_path);

                // show success message
                $message5 = '<div class="isa_success"><i class="fa fa-check"></i><div class="message_content">';
                $message5 .='Unicast Port (Receive) saved.<br>';
                $message5 .= '</div></div>';
            }
        }
	}
} 

// Load Page Data

$device_type = '';
$device_name = '';
$web_username = '';
$web_password = '';
$timezone = '';
$ptt_type = '';
$ptt_type_name = '';
$ptt_output = '';
$ptt_output_name = '';
$speaker_load_impedance = '';
$speaker_load_impedance_name = '';
$speaker_output_power = '';
$speaker_output_power_name = '';
$output_power = '';
$talkback_gain = '';
$load_impedance1 = '';
$load_impedance1_name = '';
$output_power1 = '';
$output_power_name1 = '';
$load_impedance2 = '';
$load_impedance2_name = '';
$output_power2 = '';
$output_power_name2 = '';
$load_impedance3 = '';
$load_impedance3_name = '';
$output_power3 = '';
$output_power_name3 = '';
$load_impedance4 = '';
$load_impedance4_name = '';
$output_power4 = '';
$output_power_name4 = '';
$multicast_html = '';
$multicasts = array();
$audio_cast_html = '';
$audio_casts = array();
$dataArrayString = '';
$sip_enabled = '';
$sip_enabled_name = '';
$account_reg_uri = '';
$account_id = '';
$local_port = '';
$account_username = '';
$account_password = '';
$account_id_1 = '';
$local_port_1 = '';
$account_username_1 = '';
$account_password_1 = '';
$intercom_cut_level_1 = '';
$account_id_2 = '';
$local_port_2 = '';
$account_username_2 = '';
$account_password_2 = '';
$intercom_cut_level_2 = '';
$account_id_3 = '';
$local_port_3 = '';
$account_username_3 = '';
$account_password_3 = '';
$intercom_cut_level_3 = '';
$account_id_4 = '';
$local_port_4 = '';
$account_username_4 = '';
$account_password_4 = '';
$intercom_cut_level_4 = '';
$line_in_priority = '';
$dial_extension = '';
$port_type = '';
$intercom_cut_level = '';
$external_relay_trigger = '';
$external_relay_trigger_1 = '';
$external_relay_trigger_2 = '';
$line_stream_enable_1 = '';
$line_stream_enable_2 = '';

$unicast_stream_enable = '';
$unicast_stream_source = '';
$unicast_stream_address = '';
$unicast_stream_port = '';
$unicast_stream_codec = '';
$unicast_stream_description = '';
$unicast_stream_html = '';

$unicast_receive_enable = '';
$unicast_receive_port = '';
$unicast_receive_line_output = '';
$unicast_receive_codec = '';
$unicast_receive_cut = '';
$unicast_receive_description = '';
$unicast_receive_html = '';
$audio_cast_enable_total = 0;

  // get standalone.config data
  $standalone = fopen("/etc/nyquist/standalone.config", "r");

  if(!$standalone){
      // show error message
      $message = '<div class="isa_error"><i class="fa fa-times-circle"></i><div class="message_content">';
      $message .= 'There was a problem reading the Standalone Configuration file.';
      $message .= '</div></div>';
  } else {
        while(!feof($standalone)) {

          $line_text = fgets($standalone);

          if(strpos($line_text, 'DEVICE_TYPE =') !== FALSE){
              $device_type = str_replace("DEVICE_TYPE =","",$line_text);
              $device_type = trim($device_type);
          }
          if(strpos($line_text, 'DEVICE_NAME =') !== FALSE){
              $device_name = str_replace("DEVICE_NAME =","",$line_text);
              $device_name = trim($device_name);
          }
          if(strpos($line_text, 'WEB_USERNAME =') !== FALSE){
              $web_username = str_replace("WEB_USERNAME =","",$line_text);
              $web_username = trim($web_username);
          }
          if(strpos($line_text, 'TIMEZONE =') !== FALSE){
              $timezone = str_replace("TIMEZONE =","",$line_text);
              $timezone = trim($timezone);
          }
          if(strpos($line_text, 'PTT_TYPE =') !== FALSE){
              $ptt_type = str_replace("PTT_TYPE =","",$line_text);
              $ptt_type = trim($ptt_type);
			  if($ptt_type!=''){
				  $ptt_type_name = $ptt_type;
			  } else {
				  $ptt_type_name = 'None';
			  }
          }
          if(strpos($line_text, 'PTT_OUTPUT =') !== FALSE){
              $ptt_output = str_replace("PTT_OUTPUT =","",$line_text);
              $ptt_output = trim($ptt_output);
			  if($ptt_output !=''){
				  $ptt_output_name = $ptt_output;
			  } else {
				  $ptt_output_name = 'Disabled';
			  }
          }
          if(strpos($line_text, 'SPEAKER_LOAD_IMPEDANCE =') !== FALSE){
              $speaker_load_impedance = str_replace("SPEAKER_LOAD_IMPEDANCE =","",$line_text);
              $speaker_load_impedance = trim($speaker_load_impedance);
              $speaker_load_impedance_name = ucfirst($speaker_load_impedance);
          }
          if(strpos($line_text, 'SPEAKER_OUTPUT_POWER =') !== FALSE){
              $speaker_output_power = str_replace("SPEAKER_OUTPUT_POWER =","",$line_text);
              $speaker_output_power = trim($speaker_output_power);
              $speaker_output_power_name = $speaker_output_power;
          }
		  if(strpos($line_text, 'OUTPUT_POWER =') !== FALSE){
              $output_power = str_replace("OUTPUT_POWER =","",$line_text);
              $output_power = trim($output_power);
          }
		  if(strpos($line_text, 'TALKBACK_GAIN =') !== FALSE){
              $talkback_gain = str_replace("TALKBACK_GAIN =","",$line_text);
              $talkback_gain = trim($talkback_gain);
          }
		  if(strpos($line_text, 'LOAD_IMPEDANCE_1 =') !== FALSE){
              $load_impedance1 = str_replace("LOAD_IMPEDANCE_1 =","",$line_text);
              $load_impedance1 = trim($load_impedance1);
              $load_impedance_name1 = ucfirst($load_impedance1);
          }
          if(strpos($line_text, 'OUTPUT_POWER_1 =') !== FALSE){
              $output_power1 = str_replace("OUTPUT_POWER_1 =","",$line_text);
              $output_power1 = trim($output_power1);
              $output_power_name1 = $output_power1;
          }
		  if(strpos($line_text, 'LOAD_IMPEDANCE_2 =') !== FALSE){
              $load_impedance2 = str_replace("LOAD_IMPEDANCE_2 =","",$line_text);
              $load_impedance2 = trim($load_impedance2);
              $load_impedance_name2 = ucfirst($load_impedance2);
			  if($load_impedance2==''){
				  $load_impedance2 = 'low';
				  $load_impedance_name2 = 'Low';
			  }
          }
          if(strpos($line_text, 'OUTPUT_POWER_2 =') !== FALSE){
              $output_power2 = str_replace("OUTPUT_POWER_2 =","",$line_text);
              $output_power2 = trim($output_power2);
              $output_power_name2 = $output_power2;
			  if($output_power2==''){
				  $output_power2 = '0';
				  $output_power_name2 = $output_power2;
			  }
          }
		  if(strpos($line_text, 'LOAD_IMPEDANCE_3 =') !== FALSE){
              $load_impedance3 = str_replace("LOAD_IMPEDANCE_3 =","",$line_text);
              $load_impedance3 = trim($load_impedance3);
              $load_impedance_name3 = ucfirst($load_impedance3);
          }
          if(strpos($line_text, 'OUTPUT_POWER_3 =') !== FALSE){
              $output_power3 = str_replace("OUTPUT_POWER_3 =","",$line_text);
              $output_power3 = trim($output_power3);
              $output_power_name3 = $output_power3;
          }
		  if(strpos($line_text, 'LOAD_IMPEDANCE_4 =') !== FALSE){
              $load_impedance4 = str_replace("LOAD_IMPEDANCE_4 =","",$line_text);
              $load_impedance4 = trim($load_impedance4);
              $load_impedance_name4 = ucfirst($load_impedance4);
			  if($load_impedance4==''){
				  $load_impedance4 = 'low';
				  $load_impedance_name4 = 'Low';
			  }
          }
          if(strpos($line_text, 'OUTPUT_POWER_4 =') !== FALSE){
              $output_power4 = str_replace("OUTPUT_POWER_4 =","",$line_text);
              $output_power4 = trim($output_power4);
              $output_power_name4 = $output_power4;
			   if($output_power4==''){
				  $output_power4 = '0';
				  $output_power_name4 = $output_power4;
			  }
          }	
          if(strpos($line_text, 'MULTICAST_LIST') !== FALSE){
            $multicast_list = str_replace("MULTICAST_LIST_","",$line_text);
            $multicast_list = str_replace(",","|",$multicast_list);
            $multicast_list = str_replace("=",",",$multicast_list);
            $multicast_list = str_replace(":",",",$multicast_list);
            $multicast_list = str_replace("[",",",$multicast_list);
            $multicast_list = str_replace("]","",$multicast_list);
            $multicast_list = str_replace("{",",",$multicast_list);
            $multicast_list = str_replace("}",",",$multicast_list);
            $multicast_list = trim($multicast_list);
            $multicast_row_array = explode(",", $multicast_list);
			  
			$last_two = $multicast_row_array[5];
			$last_two = str_replace("<","",$last_two);
			$last_two = str_replace(">",",",$last_two);
			$last_two_array = explode(",", $last_two);
			$mulicast_slider = $last_two_array[0];
			$mulicast_slider = trim($mulicast_slider);
			if($mulicast_slider==''){
				$mulicast_slider = '-20';
			}
			$multicast_description = $last_two_array[1];
			$multicast_description = trim($multicast_description);
			  
            array_push($multicasts, ["order"=>$multicast_row_array[0], "multicast_ip_address"=>$multicast_row_array[1],"multicast_port"=>$multicast_row_array[2],"multicast_encoding"=>$multicast_row_array[3],"multicast_audio_ports"=>str_replace("|",",",$multicast_row_array[4]), "multicast_slider"=>$mulicast_slider, "multicast_description"=>$multicast_description]);
        }	
		if(strpos($line_text, 'LINE_STREAM_ENABLE_1 =') !== FALSE){
            $line_stream_enable_1 = str_replace("LINE_STREAM_ENABLE_1 =","",$line_text);
            $line_stream_enable_1 = trim($line_stream_enable_1);
			$line_stream_enable_1 = ucfirst($line_stream_enable_1);
			if($line_stream_enable_1=='Yes'){
				$audio_cast_enable_total++;
			}
        }
		if(strpos($line_text, 'LINE_STREAM_ENABLE_2 =') !== FALSE){
            $line_stream_enable_2 = str_replace("LINE_STREAM_ENABLE_2 =","",$line_text);
            $line_stream_enable_2 = trim($line_stream_enable_2);
			$line_stream_enable_2 = ucfirst($line_stream_enable_2);
			if($line_stream_enable_2=='Yes'){
				$audio_cast_enable_total++;
			}
        }
		if(strpos($line_text, 'LINE_STREAM_1 ') !== FALSE || strpos($line_text, 'LINE_STREAM_2 ') !== FALSE){
			$audio_cast = str_replace("LINE_STREAM_","",$line_text);
            $audio_cast = str_replace(",","|",$audio_cast);
            $audio_cast = str_replace("=",",",$audio_cast);
            $audio_cast = str_replace(":",",",$audio_cast);
            $audio_cast = str_replace("[",",",$audio_cast);
            $audio_cast = str_replace("]","",$audio_cast);
            $audio_cast = str_replace("{",",",$audio_cast);
            $audio_cast = str_replace("}",",",$audio_cast);
            $audio_cast = trim($audio_cast);
            $audio_cast_row_array = explode(",", $audio_cast);
			
			$audio_cast_description = $audio_cast_row_array[5];
			
			$audio_cast_encoding = $audio_cast_row_array[3];
			if($audio_cast_encoding==''){
				$audio_cast_encoding = 'OPUS';
			}
			$audio_cast_audio_ports = str_replace("|",",",$audio_cast_row_array[4]);
			if($audio_cast_audio_ports==''){
				$audio_cast_audio_ports = $audio_cast_row_array[0];
			}
			$audio_cast_enable = '';
			if($audio_cast_row_array[0]==1){
				$audio_cast_enable = $line_stream_enable_1;
			} else {
				$audio_cast_enable = $line_stream_enable_2;
			}
			array_push($audio_casts, ["order"=>$audio_cast_row_array[0], "audio_cast_ip_address"=>$audio_cast_row_array[1],"audio_cast_port"=>$audio_cast_row_array[2],"audio_cast_encoding"=>$audio_cast_encoding,"audio_cast_audio_ports"=>$audio_cast_audio_ports, "audio_cast_description"=>$audio_cast_description,"audio_cast_enable"=>$audio_cast_enable]);
		}
			
         if(strpos($line_text, 'SIP_ENABLED =') !== FALSE){
            $sip_enabled = str_replace("SIP_ENABLED =","",$line_text);
            $sip_enabled = trim($sip_enabled);
           if($sip_enabled == '1'){
                $sip_enabled_name = 'Yes';
           } else {
               $sip_enabled_name = 'No';
           }
        }
		if(strpos($line_text, 'ACCOUNT_REG_URI =') !== FALSE){
            $account_reg_uri = str_replace("ACCOUNT_REG_URI =","",$line_text);
            $account_reg_uri = trim($account_reg_uri);
			$account_reg_uri = str_replace("sip:","",$account_reg_uri);
        }
		if(strpos($line_text, 'ACCOUNT_ID =') !== FALSE){
            $account_id = str_replace("ACCOUNT_ID =","",$line_text);
            $account_id = trim($account_id);
			$account_id = str_replace("sip:","",$account_id);
			$account_idParts = explode("@", $account_id);
			$account_id = $account_idParts[0];
        }
		if(strpos($line_text, 'LOCAL_PORT =') !== FALSE){
            $local_port = str_replace("LOCAL_PORT =","",$line_text);
            $local_port = trim($local_port);
        }
		if(strpos($line_text, 'ACCOUNT_USERNAME =') !== FALSE){
            $account_username = str_replace("ACCOUNT_USERNAME =","",$line_text);
            $account_username = trim($account_username);
        }	
		if(strpos($line_text, 'ACCOUNT_PASSWORD =') !== FALSE){
            $account_password = str_replace("ACCOUNT_PASSWORD =","",$line_text);
            $account_password = trim($account_password);
        }
		if(strpos($line_text, 'ACCOUNT_ID_1 =') !== FALSE){
            $account_id_1 = str_replace("ACCOUNT_ID_1 =","",$line_text);
            $account_id_1 = trim($account_id_1);
			$account_id_1 = str_replace("sip:","",$account_id_1);
			$account_id_1Parts = explode("@", $account_id_1);
			$account_id_1 = $account_id_1Parts[0];
        }
		if(strpos($line_text, 'LOCAL_PORT_1 =') !== FALSE){
            $local_port_1 = str_replace("LOCAL_PORT_1 =","",$line_text);
            $local_port_1 = trim($local_port_1);
			$local_port = $local_port_1;
        }
		if(strpos($line_text, 'ACCOUNT_USERNAME_1 =') !== FALSE){
            $account_username_1 = str_replace("ACCOUNT_USERNAME_1 =","",$line_text);
            $account_username_1 = trim($account_username_1);
        }	
		if(strpos($line_text, 'ACCOUNT_PASSWORD_1 =') !== FALSE){
            $account_password_1 = str_replace("ACCOUNT_PASSWORD_1 =","",$line_text);
            $account_password_1 = trim($account_password_1);
        }	
		if(strpos($line_text, 'INTERCOM_VOLUME_1 =') !== FALSE){
            $intercom_cut_level_1 = str_replace("INTERCOM_VOLUME_1 =","",$line_text);
            $intercom_cut_level_1 = trim($intercom_cut_level_1);
        }
		if(strpos($line_text, 'ACCOUNT_ID_2 =') !== FALSE){
            $account_id_2 = str_replace("ACCOUNT_ID_2 =","",$line_text);
            $account_id_2 = trim($account_id_2);
			$account_id_2 = str_replace("sip:","",$account_id_2);
			$account_id_2Parts = explode("@", $account_id_2);
			$account_id_2 = $account_id_2Parts[0];
        }
		if(strpos($line_text, 'LOCAL_PORT_2 =') !== FALSE){
            $local_port_2 = str_replace("LOCAL_PORT_2 =","",$line_text);
            $local_port_2 = trim($local_port_2);
        }
		if(strpos($line_text, 'ACCOUNT_USERNAME_2 =') !== FALSE){
            $account_username_2 = str_replace("ACCOUNT_USERNAME_2 =","",$line_text);
            $account_username_2 = trim($account_username_2);
        }	
		if(strpos($line_text, 'ACCOUNT_PASSWORD_2 =') !== FALSE){
            $account_password_2 = str_replace("ACCOUNT_PASSWORD_2 =","",$line_text);
            $account_password_2 = trim($account_password_2);
        }	
		if(strpos($line_text, 'INTERCOM_VOLUME_2 =') !== FALSE){
            $intercom_cut_level_2 = str_replace("INTERCOM_VOLUME_2 =","",$line_text);
            $intercom_cut_level_2 = trim($intercom_cut_level_2);
        }
		if(strpos($line_text, 'ACCOUNT_ID_3 =') !== FALSE){
            $account_id_3 = str_replace("ACCOUNT_ID_3 =","",$line_text);
            $account_id_3 = trim($account_id_3);
			$account_id_3 = str_replace("sip:","",$account_id_3);
			$account_id_3Parts = explode("@", $account_id_3);
			$account_id_3 = $account_id_3Parts[0];
        }
		if(strpos($line_text, 'LOCAL_PORT_3 =') !== FALSE){
            $local_port_3 = str_replace("LOCAL_PORT_3 =","",$line_text);
            $local_port_3 = trim($local_port_3);
        }
		if(strpos($line_text, 'ACCOUNT_USERNAME_3 =') !== FALSE){
            $account_username_3 = str_replace("ACCOUNT_USERNAME_3 =","",$line_text);
            $account_username_3 = trim($account_username_3);
        }	
		if(strpos($line_text, 'ACCOUNT_PASSWORD_3 =') !== FALSE){
            $account_password_3 = str_replace("ACCOUNT_PASSWORD_3 =","",$line_text);
            $account_password_3 = trim($account_password_3);
        }	
		if(strpos($line_text, 'INTERCOM_VOLUME_3 =') !== FALSE){
            $intercom_cut_level_3 = str_replace("INTERCOM_VOLUME_3 =","",$line_text);
            $intercom_cut_level_3 = trim($intercom_cut_level_3);
        }	
		if(strpos($line_text, 'ACCOUNT_ID_4 =') !== FALSE){
            $account_id_4 = str_replace("ACCOUNT_ID_4 =","",$line_text);
            $account_id_4 = trim($account_id_4);
			$account_id_4 = str_replace("sip:","",$account_id_4);
			$account_id_4Parts = explode("@", $account_id_4);
			$account_id_4 = $account_id_4Parts[0];
        }
		if(strpos($line_text, 'LOCAL_PORT_4 =') !== FALSE){
            $local_port_4 = str_replace("LOCAL_PORT_4 =","",$line_text);
            $local_port_4 = trim($local_port_4);
        }
		if(strpos($line_text, 'ACCOUNT_USERNAME_4 =') !== FALSE){
            $account_username_4 = str_replace("ACCOUNT_USERNAME_4 =","",$line_text);
            $account_username_4 = trim($account_username_4);
        }	
		if(strpos($line_text, 'ACCOUNT_PASSWORD_4 =') !== FALSE){
            $account_password_4 = str_replace("ACCOUNT_PASSWORD_4 =","",$line_text);
            $account_password_4 = trim($account_password_4);
        }
		if(strpos($line_text, 'INTERCOM_VOLUME_4 =') !== FALSE){
            $intercom_cut_level_4 = str_replace("INTERCOM_VOLUME_4 =","",$line_text);
            $intercom_cut_level_4 = trim($intercom_cut_level_4);
        }
		if(strpos($line_text, 'LINE_IN_PRIORITY =') !== FALSE){
            $line_in_priority = str_replace("LINE_IN_PRIORITY =","",$line_text);
            $line_in_priority = trim($line_in_priority);
        }
		if(strpos($line_text, 'DIAL_EXTENSION =') !== FALSE){
            $dial_extension = str_replace("DIAL_EXTENSION =","",$line_text);
            $dial_extension = trim($dial_extension);
        }
		if(strpos($line_text, 'PORT_TYPE =') !== FALSE){
            $port_type = str_replace("PORT_TYPE =","",$line_text);
            $port_type = trim($port_type);
        }
		if(strpos($line_text, 'INTERCOM_VOLUME =') !== FALSE){
            $intercom_cut_level = str_replace("INTERCOM_VOLUME =","",$line_text);
            $intercom_cut_level = trim($intercom_cut_level);
			if($intercom_cut_level==''){
				$intercom_cut_level = -20;
			}
        }
		if(strpos($line_text, 'EXTERNAL_RELAY_TRIGGER =') !== FALSE){
            $external_relay_trigger = str_replace("EXTERNAL_RELAY_TRIGGER =","",$line_text);
            $external_relay_trigger = trim($external_relay_trigger);
        }
		if(strpos($line_text, 'EXTERNAL_RELAY_TRIGGER_1 =') !== FALSE){
            $external_relay_trigger_1 = str_replace("EXTERNAL_RELAY_TRIGGER_1 =","",$line_text);
            $external_relay_trigger_1 = trim($external_relay_trigger_1);
        }
		if(strpos($line_text, 'EXTERNAL_RELAY_TRIGGER_2 =') !== FALSE){
            $external_relay_trigger_2 = str_replace("EXTERNAL_RELAY_TRIGGER_2 =","",$line_text);
            $external_relay_trigger_2 = trim($external_relay_trigger_2);
        }
		if(strpos($line_text, 'UNICAST_STREAM_ENABLE =') !== FALSE){
            $unicast_stream_enable = str_replace("UNICAST_STREAM_ENABLE =","",$line_text);
            $unicast_stream_enable = trim($unicast_stream_enable);
			$unicast_stream_enable = ucfirst($unicast_stream_enable);
        }
		if(strpos($line_text, 'UNICAST_STREAM_SOURCE =') !== FALSE){
            $unicast_stream_source = str_replace("UNICAST_STREAM_SOURCE =","",$line_text);
			$unicast_stream_source= trim($unicast_stream_source);
        }
		if(strpos($line_text, 'UNICAST_STREAM_ADDRESS =') !== FALSE){
            $unicast_stream_address = str_replace("UNICAST_STREAM_ADDRESS =","",$line_text);
			$unicast_stream_address= trim($unicast_stream_address);
        }
		if(strpos($line_text, 'UNICAST_STREAM_PORT =') !== FALSE){
            $unicast_stream_port = str_replace("UNICAST_STREAM_PORT =","",$line_text);
			$unicast_stream_port= trim($unicast_stream_port);
        }
		if(strpos($line_text, 'UNICAST_STREAM_CODEC =') !== FALSE){
            $unicast_stream_codec = str_replace("UNICAST_STREAM_CODEC =","",$line_text);
			$unicast_stream_codec= trim($unicast_stream_codec);
        }	
		if(strpos($line_text, 'UNICAST_STREAM_DESCRIPTION =') !== FALSE){
            $unicast_stream_description = str_replace("UNICAST_STREAM_DESCRIPTION =","",$line_text);
			$unicast_stream_description= trim($unicast_stream_description);
        }	
		if(strpos($line_text, 'UNICAST_RECEIVE_ENABLE =') !== FALSE){
            $unicast_receive_enable = str_replace("UNICAST_RECEIVE_ENABLE =","",$line_text);
            $unicast_receive_enable = trim($unicast_receive_enable);
			$unicast_receive_enable = ucfirst($unicast_receive_enable);
        }
		if(strpos($line_text, 'UNICAST_RECEIVE_PORT =') !== FALSE){
            $unicast_receive_port = str_replace("UNICAST_RECEIVE_PORT =","",$line_text);
			$unicast_receive_port= trim($unicast_receive_port);
        }
		if(strpos($line_text, 'UNICAST_RECEIVE_LINE_OUTPUT =') !== FALSE){
            $unicast_receive_line_output = str_replace("UNICAST_RECEIVE_LINE_OUTPUT =","",$line_text);
			$unicast_receive_line_output= trim($unicast_receive_line_output);
			if($unicast_receive_line_output=='Both'){
				$unicast_receive_line_output = '1,2';
			}
        }
		if(strpos($line_text, 'UNICAST_RECEIVE_CODEC =') !== FALSE){
            $unicast_receive_codec = str_replace("UNICAST_RECEIVE_CODEC =","",$line_text);
			$unicast_receive_codec= trim($unicast_receive_codec);
        }
		if(strpos($line_text, 'UNICAST_RECEIVE_CUT =') !== FALSE){
            $unicast_receive_cut = str_replace("UNICAST_RECEIVE_CUT =","",$line_text);
			$unicast_receive_cut= trim($unicast_receive_cut);
        }
		if(strpos($line_text, 'UNICAST_RECEIVE_DESCRIPTION =') !== FALSE){
            $unicast_receive_description = str_replace("UNICAST_RECEIVE_DESCRIPTION =","",$line_text);
			$unicast_receive_description= trim($unicast_receive_description);
        }
      }

      $dataArrayString = '[]';
	  	  
      if(count($multicasts)>0){

          $keys = array_column($multicasts, 'order');

          array_multisort($keys, SORT_ASC, SORT_NUMERIC, $multicasts);

           $dataArrayString = '['; // start the array

          foreach($multicasts as $key => $multicast){
              $order = trim($multicast['order']);
              $multicast_ip_address = trim($multicast['multicast_ip_address']);
              $multicast_port = trim($multicast['multicast_port']);
              $multicast_encoding = trim($multicast['multicast_encoding']);
              $multicast_audio_ports = trim($multicast['multicast_audio_ports']);
              $multicast_audio_ports_string =str_replace(",","<br>",$multicast_audio_ports);
			  $multicast_slider = trim($multicast['multicast_slider']);
			  $multicast_description = trim($multicast['multicast_description']);
			  
			  if($_SESSION['device_id']=='nq-mix'){ 
					$multicast_audio_ports_string =str_replace("1","Line Out",$multicast_audio_ports_string);
			 }
			 if($_SESSION['device_id']=='nq-s810v2'){ 
					$multicast_audio_ports_string =str_replace("1","Speaker",$multicast_audio_ports_string);
			 }
			 if($_SESSION['device_id']=='nq-nqpaxxx'){ 
					$multicast_audio_ports_string =str_replace("1","Line Out",$multicast_audio_ports_string);
				    $multicast_audio_ports_string =str_replace("2","Speaker Out",$multicast_audio_ports_string);
			 }
             if($_SESSION['device_id']=='nq-amp2c' || $_SESSION['device_id']=='nq-amp2c-g2'){
					$multicast_audio_ports_string =str_replace("1","Channel A",$multicast_audio_ports_string);
				    $multicast_audio_ports_string =str_replace("2","Channel B",$multicast_audio_ports_string);
			  }
			  if($_SESSION['device_id']=='nq-amp4c' || $_SESSION['device_id']=='nq-amp4c-g2'){
				  	$multicast_audio_ports_string =str_replace("1","Channel A",$multicast_audio_ports_string);
				    $multicast_audio_ports_string =str_replace("2","Channel B",$multicast_audio_ports_string);
			  		$multicast_audio_ports_string =str_replace("3","Channel C",$multicast_audio_ports_string);
				  	$multicast_audio_ports_string =str_replace("4","Channel D",$multicast_audio_ports_string);
			  }

              $dataArrayString .= '[\''.$order.'\',\''.$multicast_ip_address.'\',\''.$multicast_port.'\',\''.$multicast_encoding.'\',\''.$multicast_audio_ports.'\',\''.$multicast_slider.'\',\''.$multicast_description.'\',\''.$order.'\'],'; // add each array item
			  
			  if($multicast_encoding=='G711u'){
				  $multicast_encoding='G711 u-law';
			  }
			  if($multicast_encoding=='G711a'){
				  $multicast_encoding='G711 a-law';
			  }
			  
              $multicast_html .= '
              <tr id="row'.$order.'">
			  	  <td><i class="fa fa-arrows"></i></td>
                  <td><button class="btn btn-xs btn-danger" type="button" onClick="deleteRow('.$order.');"><i class="fa fa-trash"></i></button></td>
                  <td><a href="#" id="multicast_ip_address'.$order.'">'.$multicast_ip_address.'</a></td>
                  <td><a href="#" id="multicast_port'.$order.'">'.$multicast_port.'</a></td>
                  <td><a href="#" id="multicast_encoding'.$order.'">'.$multicast_encoding.'</a></td>
                  <td><a href="#" id="multicast_audio_ports'.$order.'">'.$multicast_audio_ports_string .'</a></td>
				  <td><a href="#" id="multicast_slider'.$order.'">'.$multicast_slider .'</a></td>
				  <td><a href="#" id="multicast_description'.$order.'">'.$multicast_description .'</a></td>
              </tr>';
          }

          $dataArrayString = substr($dataArrayString, 0, -1); // remove last comma
          $dataArrayString .= ']'; // end the array
      }
	  
	  $dataArrayString2 = '[]';
	  	  	  
      if(count($audio_casts)>0){

          $keys = array_column($audio_casts, 'order');

          array_multisort($keys, SORT_ASC, $audio_casts);

           $dataArrayString2 = '['; // start the array

          foreach($audio_casts as $key => $audio_cast){
              $order = trim($audio_cast['order']);
              $audio_cast_ip_address = trim($audio_cast['audio_cast_ip_address']);
			  if($audio_cast_ip_address==''){
				  if($key==0){
					  $audio_cast_ip_address = '239.1.1.1';
				  } else {
					  $audio_cast_ip_address = '239.1.1.2';
				  }
			  }
              $audio_cast_port = trim($audio_cast['audio_cast_port']);
			  if($audio_cast_port==''){
				  if($key==0){
					  $audio_cast_port = '6000';
				  } else {
					  $audio_cast_port = '6002';
				  }
			  }
              $audio_cast_encoding = trim($audio_cast['audio_cast_encoding']);
              $audio_cast_audio_ports = trim($audio_cast['audio_cast_audio_ports']);
              $audio_cast_audio_ports_string =str_replace(",","<br>",$audio_cast_audio_ports);
			  $audio_cast_description = trim($audio_cast['audio_cast_description']);
			  $audio_cast_enable = trim($audio_cast['audio_cast_enable']);
			 
              $dataArrayString2 .= '[\''.$order.'\',\''.$audio_cast_ip_address.'\',\''.$audio_cast_port.'\',\''.$audio_cast_encoding.'\',\''.$audio_cast_audio_ports.'\',\''.$audio_cast_description.'\',\''.$audio_cast_enable.'\',\''.$order.'\'],'; // add each array item
			  
			  if($audio_cast_encoding==''){
				  $audio_cast_encoding = 'OPUS';
			  }
			  if($audio_cast_audio_ports_string==''){
				  $audio_cast_audio_ports_string = $order;
			  }
			   if($audio_cast_encoding=='G711u'){
				  $audio_cast_encoding='G711 u-law';
			  }
			  if($audio_cast_encoding=='G711a'){
				  $audio_cast_encoding='G711 a-law';
			  }
			  
			  if($unicast_stream_enable=='Yes'){
				   $audio_cast_html .= '
                  <tr id="cast_row'.$order.'">
                      <td>'.$audio_cast_enable.'</td>
                      <td>'.$audio_cast_ip_address.'</td>
                      <td>'.$audio_cast_port.'</td>
                      <td>'.$audio_cast_encoding.'</td>
                      <td>'.$audio_cast_audio_ports_string .'</td>
                      <td>'.$audio_cast_description .'</td>
                  </tr>';
				  
			  } else {
				   $audio_cast_html .= '
                  <tr id="cast_row'.$order.'">
                      <td><a href="#" id="audio_cast_enable'.$order.'">'.$audio_cast_enable.'</a></td>
                      <td><a href="#" id="audio_cast_ip_address'.$order.'">'.$audio_cast_ip_address.'</a></td>
                      <td><a href="#" id="audio_cast_port'.$order.'">'.$audio_cast_port.'</a></td>
                      <td><a href="#" id="audio_cast_encoding'.$order.'">'.$audio_cast_encoding.'</a></td>
                      <td><a href="#" id="audio_cast_audio_ports'.$order.'">'.$audio_cast_audio_ports_string .'</a></td>
                      <td><a href="#" id="audio_cast_description'.$order.'">'.$audio_cast_description .'</a></td>
                  </tr>';
			  }
          }

          $dataArrayString2 = substr($dataArrayString2, 0, -1); // remove last comma
          $dataArrayString2 .= ']'; // end the array
      }

    fclose($standalone);
	  
	if($unicast_receive_enable=='Yes' || $audio_cast_enable_total>0){
          $unicast_stream_html .= ' 
          <tr>
      		<td>'.$unicast_stream_enable.'</td>
			<td>'.$unicast_stream_address.'</td>
        	<td>'.$unicast_stream_port.'</td>
        	<td>'.$unicast_stream_codec.'</td>
        	<td>'.$unicast_stream_source.'</td>
			<td>'.$unicast_stream_description.'</td>
         </tr>';
	} else {
		$unicast_stream_html .= ' 
          <tr>
      		<td><a href="#" id="unicast_stream_enable">'.$unicast_stream_enable.'</a></td>
			<td><a href="#" id="unicast_stream_address">'.$unicast_stream_address.'</a></td>
        	<td><a href="#" id="unicast_stream_port">'.$unicast_stream_port.'</a></td>
        	<td><a href="#" id="unicast_stream_codec">'.$unicast_stream_codec.'</a></td>
        	<td><a href="#" id="unicast_stream_source">'.$unicast_stream_source.'</a></td>
			<td><a href="#" id="unicast_stream_description">'.$unicast_stream_description.'</a></td>
		</tr>';
	}
	  
	if($unicast_stream_enable=='Yes'){
		  $unicast_receive_html .= ' 
          <tr>
            <td>'.$unicast_receive_enable.'</td>
            <td>'.$unicast_receive_port.'</td>
            <td>'.$unicast_receive_codec.'</td>
            <td>'.$unicast_receive_line_output.'</td>
            <td>'.$unicast_receive_cut.'</td>
            <td>'.$unicast_receive_description.'</td>
          </tr>';
	} else {
		$unicast_receive_html .= ' 
          <tr>
            <td><a href="#" id="unicast_receive_enable">'.$unicast_receive_enable.'</a></td>
            <td><a href="#" id="unicast_receive_port">'.$unicast_receive_port.'</td>
            <td><a href="#" id="unicast_receive_codec">'.$unicast_receive_codec.'</a></td>
            <td><a href="#" id="unicast_receive_line_output">'.$unicast_receive_line_output.'</a></td>
            <td><a href="#" id="unicast_receive_cut">'.$unicast_receive_cut.'</a></td>
            <td><a href="#" id="unicast_receive_description">'.$unicast_receive_description.'</a></td>
          </tr>';
	}
}
?>
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="description" content="">
<meta name="author" content="Bogen Communications LLC">
<meta name="copyright" content="Bogen Communications LLC">
<meta http-equiv="Content-Security-Policy" content="upgrade-insecure-requests" />
<link rel="shortcut icon" href="">
<link rel="stylesheet" href="/css/styles.css">
<link rel="stylesheet" href="/css/bootstrap.min.css">
<link rel="stylesheet" href="/css/font-awesome/css/font-awesome.min.css">
<link rel="stylesheet" href="/css/select2.min.css" />
<link rel="stylesheet" href="/css/bootstrap-editable.css" >
<link rel="stylesheet" href="/css/bootstrap-toggle.min.css" >
<link rel="icon" type="image/png" href="/favicon.png" />
<title><?php echo $_SESSION['device_type']; ?> (<?php echo $_SESSION['device_name']; ?>) - Configuration Settings</title>
	
<style>
input[type="range"]{
    padding: 0px;
}
</style>
</head>

<body>
<input name="json_type" id="json_type" type="hidden" value="<?php echo $json_type; ?>">
<input name="device_id" id="device_id" type="hidden" value="<?php echo $_SESSION['device_id']; ?>">
<input name="IsDualChannel" id="IsDualChannel" type="hidden" value="<?php echo $is_dual_channel;?>">
<input name="IsDualChannel2" id="IsDualChannel2" type="hidden" value="<?php echo $is_dual_channel2;?>">
<input name="sorting_active" id="sorting_active" type="hidden" value="0">
<input name="sorting_active2" id="sorting_active2" type="hidden" value="0">
<div class="container">

<?php include 'navbar.php';?>

<h4><strong><i class="fa fa-cogs"></i> Configuration Settings</strong>&nbsp;&nbsp;<a href="/help/<?php echo $_SESSION['device_id']; ?>/#t=<?php echo $_SESSION['help_configuration']; ?>" type="button" class="btn btn-xs btn-primary" target="_blank"><i class="fa fa-question-circle"></i></a></h4>
	
<div id="status"></div>
<form id="form1" name="form1" action="configuration_s.php" method="post">
<div id="message_container"><?php echo $message; ?></div>
<table id="config_table" class="table-condensed">
<tr>
<td style="width: 250px;"><strong>Device Type:</strong></td>
<td><input type="text" class="form-control" name="device_type" placeholder="" style="width:350px;" value="<?php echo $device_type; ?>" readonly></td>
<td>&nbsp;</td>
<td>&nbsp;</td>
</tr>
<tr>
<td><strong>Device Name:</strong></td>
<td><input type="text" class="form-control" name="device_name" placeholder="" style="width:200px;" value="<?php echo $device_name; ?>"></td>
<td>&nbsp;</td>
<td>&nbsp;</td>
</tr>
<tr>
<td><strong>Web Username:</strong></td>
<td><input type="text" class="form-control" name="web_username" placeholder="" style="width:200px;" value="<?php echo $web_username; ?>"></td>
<td>&nbsp;</td>
<td>&nbsp;</td>
</tr>
<tr>
<td><strong>Web Password:</strong></td>
<td><input type="password" class="form-control" name="web_password" placeholder="" style="width:200px;" value=""></td>
<td>&nbsp;</td>
<td>&nbsp;</td>
</tr>
<tr>
<td><strong>Web Confirm Password:</strong></td>
<td><input type="password" class="form-control" name="web_password_confirm" placeholder="" style="width:200px;" value=""></td>
<td>&nbsp;</td>
<td>&nbsp;</td>
</tr>
<tr>
<td><strong>Time Zone:</strong></td>
<td>
	<select name="timezone" id="timezone" style="width: 60%">
   		<?php $oldheader = ''; ?>
  		<?php $newheader = ''; ?>
		<option value=""></option>
   		<?php foreach($timezones as $data){ ?>
   			<?php $thirdname = ''; ?>
    		<?php $countryArray = explode('/', $data);?>
			<?php $newheader = $countryArray[0]; ?>
    		<?php if(is_array($countryArray) && count($countryArray)>2){ $thirdname = '/'. str_replace("_"," ", $countryArray[2]); }?>
    		<?php $timezonename = str_replace("_"," ", $countryArray[1]). '' . $thirdname; ?>
    		<?php if($newheader != $oldheader){ ?>
    			 <optgroup label="<?php echo $newheader; ?>">
        	<?php } ?>
            	<?php if($timezone == $data){ ?>
                	<option value="<?php echo $data; ?>" selected><?php echo $timezonename; ?></option>
            	<?php } else { ?>
            		<option value="<?php echo $data; ?>"><?php echo $timezonename; ?></option>
                <?php } ?>
             <?php $oldheader = $countryArray[0]; ?>
       <?php } ?>
    </select>
</td>
<td>&nbsp;</td>
<td>&nbsp;</td>
</tr>
<?php if(!in_array($_SESSION['device_id'], ['nq-edp01','nq-s810v2','nq-ga10p','nq-ga10pv','nq-ga20p2','nq-ga400p','nq-ser20p2'])){ // start hide for EDP01, VoIP G2, nq-ga10p, nq-ga10pv, nq-ga20p2, nq-ga400p, nq-ser20p2 ?>
<tr>
<td><strong>Prioritize Line Input:</strong></td>
<td><input id="line_in_priority" name="line_in_priority" class="form-control" data-on="Yes" data-off="No" data-toggle="toggle" data-size="small" type="checkbox" value="<?php echo $line_in_priority;?>"></td>
<td>&nbsp;</td>
<td>&nbsp;</td>
</tr>
<?php } // end hide for EDP01, VoIP G2, nq-ga400p, nq-ga10p, nq-ga10pv, nq-ga400p ?>
<?php if($_SESSION['device_id']=='nq-mix'){ // start nq-mix ?>
<tr>
<td><strong>PTT Output:</strong></td>
<td>
<input name="ptt_output" id="ptt_output" type="hidden" value="<?php echo $ptt_output; ?>">
<input name="ptt_output_name" id="ptt_output_name" type="hidden" value="<?php echo $ptt_output_name; ?>">
<div class="btn-group ptt-output-btn">
  <button type="button" class="btn btn-default dropdown-toggle" data-toggle="dropdown">
    <span class="selection"><?php echo $ptt_output_name; ?></span>&nbsp;<span class="caret"></span>
  </button>
  <ul class="dropdown-menu" role="menu">  
	  <li><a href="javascript:void(0)" class="ptt-output-action" data-value="Disabled">Disabled</a></li>
	  <li><a href="javascript:void(0)" class="ptt-output-action" data-value="Line Out">Line Out</a></li>
  </ul>
</div>
</td>
<td></td>
</tr>
<?php } // end nq-mix ?>	
	
<?php if($_SESSION['device_id']=='nq-nqpaxxx'){ // start nq-nqpaxxx ?>
<tr>
<td><strong>PTT Output:</strong></td>
<td>
<input name="ptt_output" id="ptt_output" type="hidden" value="<?php echo $ptt_output; ?>">
<input name="ptt_output_name" id="ptt_output_name" type="hidden" value="<?php echo $ptt_output_name; ?>">
<div class="btn-group ptt-output-btn">
  <button type="button" class="btn btn-default dropdown-toggle" data-toggle="dropdown">
    <span class="selection"><?php echo $ptt_output_name; ?></span>&nbsp;<span class="caret"></span>
  </button>
  <ul class="dropdown-menu" role="menu">  
	  <li><a href="javascript:void(0)" class="ptt-output-action" data-value="Disabled">Disabled</a></li>
	  <li><a href="javascript:void(0)" class="ptt-output-action" data-value="Line-Out">Line-Out</a></li>
	  <li><a href="javascript:void(0)" class="ptt-output-action" data-value="Speaker-Out">Speaker-Out</a></li>
	  <li><a href="javascript:void(0)" class="ptt-output-action" data-value="Both">Both</a></li>
  </ul>
</div>
</td>
<td></td>
</tr>
<tr>
<td><strong>Speaker Load Impedance:</strong></td>
<td>
<input name="speaker_load_impedance" id="speaker_load_impedance" type="hidden" value="<?php echo $speaker_load_impedance; ?>">
<input name="speaker_load_impedance_name" id="speaker_load_impedance_name" type="hidden" value="<?php echo $speaker_load_impedance_name; ?>">
<div class="btn-group speaker-load-impedance-btn">
  <button type="button" class="btn btn-default dropdown-toggle" data-toggle="dropdown">
    <span class="selection"><?php echo $speaker_load_impedance_name; ?></span>&nbsp;<span class="caret"></span>
  </button>
  <ul class="dropdown-menu" role="menu">  
	  <li><a href="javascript:void(0)" class="speaker-load-impedance-action" data-value="low">Low</a></li>
	  <li><a href="javascript:void(0)" class="speaker-load-impedance-action" data-value="high">High</a></li>
  </ul>
</div>
</td>
<td></td>
</tr>	
<tr>
<td><strong>Speaker Output Power:</strong></td>
<td>
<input name="speaker_output_power" id="speaker_output_power" type="hidden" value="<?php echo $speaker_output_power; ?>">
<input name="speaker_output_power_name" id="speaker_output_power_name" type="hidden" value="<?php echo $speaker_output_power_name; ?>">
<div class="btn-group dropup speaker-output-power-btn">
  <button type="button" class="btn btn-default dropdown-toggle" data-toggle="dropdown">
    <span class="selection"><?php echo $speaker_output_power_name; ?></span>&nbsp;<span class="caret"></span>
  </button>
  <ul class="dropdown-menu" role="menu">  
	  <li><a href="javascript:void(0)" class="speaker-output-power-action" data-value="6">6</a></li>
	  <li><a href="javascript:void(0)" class="speaker-output-power-action" data-value="5">5</a></li>
	  <li><a href="javascript:void(0)" class="speaker-output-power-action" data-value="4">4</a></li>
	  <li><a href="javascript:void(0)" class="speaker-output-power-action" data-value="3">3</a></li>
	  <li><a href="javascript:void(0)" class="speaker-output-power-action" data-value="2">2</a></li>
	  <li><a href="javascript:void(0)" class="speaker-output-power-action" data-value="1">1</a></li>
	  <li><a href="javascript:void(0)" class="speaker-output-power-action" data-value="0">0</a></li>
	  <li><a href="javascript:void(0)" class="speaker-output-power-action" data-value="-1">-1</a></li>
	  <li><a href="javascript:void(0)" class="speaker-output-power-action" data-value="-2">-2</a></li>
	  <li><a href="javascript:void(0)" class="speaker-output-power-action" data-value="-3">-3</a></li>
	  <li><a href="javascript:void(0)" class="speaker-output-power-action" data-value="-4">-4</a></li>
	  <li><a href="javascript:void(0)" class="speaker-output-power-action" data-value="-5">-5</a></li>
	  <li><a href="javascript:void(0)" class="speaker-output-power-action" data-value="-6">-6</a></li>
  </ul>
</div>
</td>
<td></td>
</tr>
<?php }  // end nq-nqpaxxx ?>	

<?php if(in_array($_SESSION['device_id'], ['nq-amp2c','nq-amp4c','nq-amp2c-g2','nq-amp4c-g2'])){ // start nq-amp2c, nq-amp4c, nq-amp2c-g2 nq-amp4c-g2 ?>
<?php 
	$channel_name1 = 'Channel A';	
	$channel_name3 = 'Channel C';
	if($is_dual_channel==0){
			$channel_name1 = 'Channel A/B';
	}
	if($is_dual_channel2==0){
		$channel_name3 = 'Channel C/D';
	}
?>
<tr>
<td><strong><?php echo $channel_name1;?> Load Impedance:</strong></td>
<td>
<input name="load_impedance1" id="load_impedance1" type="hidden" value="<?php echo $load_impedance1; ?>">
<input name="load_impedance_name1" id="load_impedance_name1" type="hidden" value="<?php echo $load_impedance_name1; ?>">
<div class="btn-group load-impedance1-btn">
  <button type="button" class="btn btn-default dropdown-toggle" data-toggle="dropdown">
    <span class="selection"><?php echo $load_impedance_name1; ?></span>&nbsp;<span class="caret"></span>
  </button>
  <ul class="dropdown-menu" role="menu">  
	  <li><a href="javascript:void(0)" class="load-impedance1-action" data-value="low">Low</a></li>
	  <li><a href="javascript:void(0)" class="load-impedance1-action" data-value="high">High</a></li>
  </ul>
</div>
</td>
<td></td>
</tr>	
<tr>
<td><strong><?php echo $channel_name1;?> Output Power:</strong></td>
<td>
<input name="output_power1" id="output_power1" type="hidden" value="<?php echo $output_power1; ?>">
<input name="output_power_name1" id="output_power_name1" type="hidden" value="<?php echo $output_power_name1; ?>">
<div class="btn-group dropup output-power1-btn">
  <button type="button" class="btn btn-default dropdown-toggle" data-toggle="dropdown">
    <span class="selection"><?php echo $output_power_name1; ?></span>&nbsp;<span class="caret"></span>
  </button>
  <ul class="dropdown-menu" role="menu">  
	  <li><a href="javascript:void(0)" class="output-power1-action" data-value="6">6</a></li>
	  <li><a href="javascript:void(0)" class="output-power1-action" data-value="5">5</a></li>
	  <li><a href="javascript:void(0)" class="output-power1-action" data-value="4">4</a></li>
	  <li><a href="javascript:void(0)" class="output-power1-action" data-value="3">3</a></li>
	  <li><a href="javascript:void(0)" class="output-power1-action" data-value="2">2</a></li>
	  <li><a href="javascript:void(0)" class="output-power1-action" data-value="1">1</a></li>
	  <li><a href="javascript:void(0)" class="output-power1-action" data-value="0">0</a></li>
	  <li><a href="javascript:void(0)" class="output-power1-action" data-value="-1">-1</a></li>
	  <li><a href="javascript:void(0)" class="output-power1-action" data-value="-2">-2</a></li>
	  <li><a href="javascript:void(0)" class="output-power1-action" data-value="-3">-3</a></li>
	  <li><a href="javascript:void(0)" class="output-power1-action" data-value="-4">-4</a></li>
	  <li><a href="javascript:void(0)" class="output-power1-action" data-value="-5">-5</a></li>
	  <li><a href="javascript:void(0)" class="output-power1-action" data-value="-6">-6</a></li>
  </ul>
</div>
</td>
<td></td>
</tr>
<?php if($is_dual_channel==1){ // start is_dual_channel ?>
<tr>
<td><strong>Channel B Load Impedance:</strong></td>
<td>
<input name="load_impedance2" id="load_impedance2" type="hidden" value="<?php echo $load_impedance2; ?>">
<input name="load_impedance_name2" id="load_impedance_name2" type="hidden" value="<?php echo $load_impedance_name2; ?>">
<div class="btn-group load-impedance2-btn">
  <button type="button" class="btn btn-default dropdown-toggle" data-toggle="dropdown">
    <span class="selection"><?php echo $load_impedance_name2; ?></span>&nbsp;<span class="caret"></span>
  </button>
  <ul class="dropdown-menu" role="menu">  
	  <li><a href="javascript:void(0)" class="load-impedance2-action" data-value="low">Low</a></li>
	  <li><a href="javascript:void(0)" class="load-impedance2-action" data-value="high">High</a></li>
  </ul>
</div>
</td>
<td></td>
</tr>
<tr>
<td><strong>Channel B Output Power:</strong></td>
<td>
<input name="output_power2" id="output_power2" type="hidden" value="<?php echo $output_power2; ?>">
<input name="output_power_name2" id="output_power_name2" type="hidden" value="<?php echo $output_power_name2; ?>">
<div class="btn-group dropup output-power2-btn">
  <button type="button" class="btn btn-default dropdown-toggle" data-toggle="dropdown">
    <span class="selection"><?php echo $output_power_name2; ?></span>&nbsp;<span class="caret"></span>
  </button>
  <ul class="dropdown-menu" role="menu">  
	  <li><a href="javascript:void(0)" class="output-power2-action" data-value="6">6</a></li>
	  <li><a href="javascript:void(0)" class="output-power2-action" data-value="5">5</a></li>
	  <li><a href="javascript:void(0)" class="output-power2-action" data-value="4">4</a></li>
	  <li><a href="javascript:void(0)" class="output-power2-action" data-value="3">3</a></li>
	  <li><a href="javascript:void(0)" class="output-power2-action" data-value="2">2</a></li>
	  <li><a href="javascript:void(0)" class="output-power2-action" data-value="1">1</a></li>
	  <li><a href="javascript:void(0)" class="output-power2-action" data-value="0">0</a></li>
	  <li><a href="javascript:void(0)" class="output-power2-action" data-value="-1">-1</a></li>
	  <li><a href="javascript:void(0)" class="output-power2-action" data-value="-2">-2</a></li>
	  <li><a href="javascript:void(0)" class="output-power2-action" data-value="-3">-3</a></li>
	  <li><a href="javascript:void(0)" class="output-power2-action" data-value="-4">-4</a></li>
	  <li><a href="javascript:void(0)" class="output-power2-action" data-value="-5">-5</a></li>
	  <li><a href="javascript:void(0)" class="output-power2-action" data-value="-6">-6</a></li>
  </ul>
</div>
</td>
<td></td>
</tr>
<?php } // end is_dual_channel ?>
<?php } // end nq-amp2c, nq-amp4c, nq-amp2c-g2, nq-amp4c-g2 ?>
	
<?php if($_SESSION['device_id']=='nq-amp4c' || $_SESSION['device_id']=='nq-amp4c-g2'){ // start nq-amp4c nq-amp4c-g2 ?>
<tr>
<td><strong><?php echo $channel_name3;?> Load Impedance:</strong></td>
<td>
<input name="load_impedance3" id="load_impedance3" type="hidden" value="<?php echo $load_impedance3; ?>">
<input name="load_impedance_name3" id="load_impedance_name3" type="hidden" value="<?php echo $load_impedance_name3; ?>">
<div class="btn-group load-impedance3-btn">
  <button type="button" class="btn btn-default dropdown-toggle" data-toggle="dropdown">
    <span class="selection"><?php echo $load_impedance_name3; ?></span>&nbsp;<span class="caret"></span>
  </button>
  <ul class="dropdown-menu" role="menu">  
	  <li><a href="javascript:void(0)" class="load-impedance3-action" data-value="low">Low</a></li>
	  <li><a href="javascript:void(0)" class="load-impedance3-action" data-value="high">High</a></li>
  </ul>
</div>
</td>
<td></td>
</tr>	
<tr>
<td><strong><?php echo $channel_name3;?> Output Power:</strong></td>
<td>
<input name="output_power3" id="output_power3" type="hidden" value="<?php echo $output_power3; ?>">
<input name="output_power_name3" id="output_power_name3" type="hidden" value="<?php echo $output_power_name3; ?>">
<div class="btn-group dropup output-power3-btn">
  <button type="button" class="btn btn-default dropdown-toggle" data-toggle="dropdown">
    <span class="selection"><?php echo $output_power_name3; ?></span>&nbsp;<span class="caret"></span>
  </button>
  <ul class="dropdown-menu" role="menu">  
	  <li><a href="javascript:void(0)" class="output-power3-action" data-value="6">6</a></li>
	  <li><a href="javascript:void(0)" class="output-power3-action" data-value="5">5</a></li>
	  <li><a href="javascript:void(0)" class="output-power3-action" data-value="4">4</a></li>
	  <li><a href="javascript:void(0)" class="output-power3-action" data-value="3">3</a></li>
	  <li><a href="javascript:void(0)" class="output-power3-action" data-value="2">2</a></li>
	  <li><a href="javascript:void(0)" class="output-power3-action" data-value="1">1</a></li>
	  <li><a href="javascript:void(0)" class="output-power3-action" data-value="0">0</a></li>
	  <li><a href="javascript:void(0)" class="output-power3-action" data-value="-1">-1</a></li>
	  <li><a href="javascript:void(0)" class="output-power3-action" data-value="-2">-2</a></li>
	  <li><a href="javascript:void(0)" class="output-power3-action" data-value="-3">-3</a></li>
	  <li><a href="javascript:void(0)" class="output-power3-action" data-value="-4">-4</a></li>
	  <li><a href="javascript:void(0)" class="output-power3-action" data-value="-5">-5</a></li>
	  <li><a href="javascript:void(0)" class="output-power3-action" data-value="-6">-6</a></li>
  </ul>
</div>
</td>
<td></td>
</tr>
<?php if($is_dual_channel2==1){ // start is_dual_channel2 ?>
<tr>
<td><strong>Channel D Load Impedance:</strong></td>
<td>
<input name="load_impedance4" id="load_impedance4" type="hidden" value="<?php echo $load_impedance4; ?>">
<input name="load_impedance_name4" id="load_impedance_name4" type="hidden" value="<?php echo $load_impedance_name4; ?>">
<div class="btn-group load-impedance4-btn">
  <button type="button" class="btn btn-default dropdown-toggle" data-toggle="dropdown">
    <span class="selection"><?php echo $load_impedance_name4; ?></span>&nbsp;<span class="caret"></span>
  </button>
  <ul class="dropdown-menu" role="menu">  
	  <li><a href="javascript:void(0)" class="load-impedance4-action" data-value="low">Low</a></li>
	  <li><a href="javascript:void(0)" class="load-impedance4-action" data-value="high">High</a></li>
  </ul>
</div>
</td>
<td></td>
</tr>	
<tr>
<td><strong>Channel D Output Power:</strong></td>
<td>
<input name="output_power4" id="output_power4" type="hidden" value="<?php echo $output_power4; ?>">
<input name="output_power_name4" id="output_power_name4" type="hidden" value="<?php echo $output_power_name4; ?>">
<div class="btn-group dropup output-power4-btn">
  <button type="button" class="btn btn-default dropdown-toggle" data-toggle="dropdown">
    <span class="selection"><?php echo $output_power_name4; ?></span>&nbsp;<span class="caret"></span>
  </button>
  <ul class="dropdown-menu" role="menu">  
	  <li><a href="javascript:void(0)" class="output-power4-action" data-value="6">6</a></li>
	  <li><a href="javascript:void(0)" class="output-power4-action" data-value="5">5</a></li>
	  <li><a href="javascript:void(0)" class="output-power4-action" data-value="4">4</a></li>
	  <li><a href="javascript:void(0)" class="output-power4-action" data-value="3">3</a></li>
	  <li><a href="javascript:void(0)" class="output-power4-action" data-value="2">2</a></li>
	  <li><a href="javascript:void(0)" class="output-power4-action" data-value="1">1</a></li>
	  <li><a href="javascript:void(0)" class="output-power4-action" data-value="0">0</a></li>
	  <li><a href="javascript:void(0)" class="output-power4-action" data-value="-1">-1</a></li>
	  <li><a href="javascript:void(0)" class="output-power4-action" data-value="-2">-2</a></li>
	  <li><a href="javascript:void(0)" class="output-power4-action" data-value="-3">-3</a></li>
	  <li><a href="javascript:void(0)" class="output-power4-action" data-value="-4">-4</a></li>
	  <li><a href="javascript:void(0)" class="output-power4-action" data-value="-5">-5</a></li>
	  <li><a href="javascript:void(0)" class="output-power4-action" data-value="-6">-6</a></li>
  </ul>
</div>
</td>
<td></td>
</tr>
<?php } // end is_dual_channel2 ?>
<?php } // end nq-amp4c, nq-amp4c-g2 ?>
	
<?php if(in_array($_SESSION['device_id'], ['nq-ga10p','nq-ga10pv','nq-s810v2','nq-ga20p2','nq-ga40p3','nq-ser20p2'])){ // start nq-ga10p, nq-ga10pv, nq-s810v2, nq-ga20p2, nq-ga40p3, nq-ser20p2 ?>
	<tr id="row_output_power">
      <td><strong>Output Power (Watts):</strong></td>
      <td>
		   <select name="output_power" id="output_power" class="form-control" style="width: 60%;">
	  			<option value="1/8" <?php if($output_power == '1/8'){ echo 'selected="selected"';}?>>1/8</option>
			   	<option value="1/4" <?php if($output_power == '1/4'){ echo 'selected="selected"';}?>>1/4</option>
			   	<option value="1/2" <?php if($output_power == '1/2'){ echo 'selected="selected"';}?>>1/2</option>
			  	<option value="1" <?php if($output_power == '1'){ echo 'selected="selected"';}?>>1</option>
			  	<option value="2" <?php if($output_power == '2'){ echo 'selected="selected"';}?>>2</option>
			  	<option value="4" <?php if($output_power == '4'){ echo 'selected="selected"';}?>>4</option>
				<option value="8" <?php if($output_power == '8'){ echo 'selected="selected"';}?>>8</option>
			   <?php if($_SESSION['device_id']=='nq-ga20p2' || $_SESSION['device_id']=='nq-ga40p3'  || $_SESSION['device_id']=='nq-ser20p2'){ // 16 watt added for GA20P2, GA40P3, SER20P2 ?> 
			   		<option value="16" <?php if($output_power == '16'){ echo 'selected="selected"';}?>>16</option>
			   <?php } ?>
			   <?php if($_SESSION['device_id']=='nq-ga40p3'){ // 32 watt added for GA40P3 ?> 
			   		<option value="32" <?php if($output_power == '32'){ echo 'selected="selected"';}?>>32</option>
			   <?php } ?>
			</select>
		</td>
      <td>&nbsp;</td>
      <td>&nbsp;</td>
	</tr>
	<?php } // end nq-ga10p, nq-ga10pv, nq-s810v2, nq-ga20p2, nq-ser20p2 ?>
	
<?php if(in_array($_SESSION['device_id'], ['nq-mix','nq-nqpaxxx', 'nq-amp2c', 'nq-amp4c', 'nq-amp2c-g2','nq-amp4c-g2','nq-s810v2','nq-ga400p','nq-ga40p3','nq-ga20p2','nq-ga10p','nq-ga10pv','nq-ser20p2'])){ // start nq-nqpaxxx, nq-amp2c, nq-amp4c, nq-amp2c-g2, nq-amp4c-g2, nq-s810v2, nq-ga400p, nq-ga40p3, nq-ga10p, nq-ga10pv, nq-ser20p2 ?>
<tr>
<td><strong>Enable SIP Calls:</strong></td>
<td>
<input name="sip_enabled" id="sip_enabled" type="hidden" value="<?php echo $sip_enabled; ?>">
<input name="sip_enabled_name" id="sip_enabled_name" type="hidden" value="<?php echo $sip_enabled_name; ?>">
<div class="btn-group sip-enabled-btn">
  <button type="button" class="btn btn-default dropdown-toggle" data-toggle="dropdown">
    <span class="selection"><?php echo $sip_enabled_name; ?></span>&nbsp;<span class="caret"></span>
  </button>
  <ul class="dropdown-menu" role="menu">  
	  <li><a href="javascript:void(0)" class="sip-enabled-action" data-value="0">No</a></li>
	  <li><a href="javascript:void(0)" class="sip-enabled-action" data-value="1">Yes</a></li>
  </ul>
</div>
</td>
<td></td>
</tr>
<?php }  // end nq-nqpaxxx, nq-amp2c, nq-amp4c, nq-amp2c-g2, nq-amp4c-g2 ?>
	
<?php if($_SESSION['device_id']=='nq-ga10p' || $_SESSION['device_id']=='nq-ga10pv' || $_SESSION['device_id']=='nq-s810v2' || $_SESSION['device_id']=='nq-ser20p2'){ // start EXTERNAL_RELAY_TRIGGER for nq-ga10p, nq-ga10pv, nq-s810v2, nq-ser20p2  ?>
	<tr>
    <td><strong>External Relay Trigger:</strong></td>
	<input name="external_relay_trigger_old" id="external_relay_trigger_old" type="hidden" value="<?php echo $external_relay_trigger; ?>">
    <td><input id="external_relay_trigger" name="external_relay_trigger" class="form-control" data-on="Enabled" data-off="Disabled" data-toggle="toggle" data-size="small" type="checkbox" value="<?php echo $external_relay_trigger;?>"></td>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
    </tr>
		
<?php } // end EXTERNAL_RELAY_TRIGGER for nq-ga10p, nq-ga10pv, nq-s810v2?>
	
<?php if($_SESSION['device_id']=='nq-ga400p'){ // start EXTERNAL_RELAY_TRIGGER_1, EXTERNAL_RELAY_TRIGGER_2 for nq-ga400p ?>
	
	<tr>
    <td><strong>External Relay Trigger 1:</strong></td>
	<input name="external_relay_trigger_1_old" id="external_relay_trigger_1_old" type="hidden" value="<?php echo $external_relay_trigger_1; ?>">
    <td><input id="external_relay_trigger_1" name="external_relay_trigger_1" class="form-control" data-on="Enabled" data-off="Disabled" data-toggle="toggle" data-size="small" type="checkbox" value="<?php echo $external_relay_trigger_1;?>"></td>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
    </tr>
	<tr>
    <td><strong>External Relay Trigger 2:</strong></td>
		<input name="external_relay_trigger_2_old" id="external_relay_trigger_2_old" type="hidden" value="<?php echo $external_relay_trigger_2; ?>">
    <td><input id="external_relay_trigger_2" name="external_relay_trigger_2" class="form-control" data-on="Enabled" data-off="Disabled" data-toggle="toggle" data-size="small" type="checkbox" value="<?php echo $external_relay_trigger_2;?>"></td>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
    </tr>
		
<?php } // end EXTERNAL_RELAY_TRIGGER_1, EXTERNAL_RELAY_TRIGGER_2 for nq-ga400p ?>
	
<?php 
	$channel_name1 = '';
	$channel_name2 = '';
	$channel_name3 = '';
	$channel_name4 = '';
	if($_SESSION['device_id']=='nq-mix'){
		$channel_name1 = 'Line Out';
	}
	if($_SESSION['device_id']=='nq-ga400p'){
		$channel_name1 = 'Line Output 1';
		$channel_name2 = 'Line Output 2';
	}
	if($_SESSION['device_id']=='nq-nqpaxxx'){
		$channel_name1 = 'Line Out';
		$channel_name2 = 'Speaker Out';
	}
	if($_SESSION['device_id']=='nq-amp2c' || $_SESSION['device_id']=='nq-amp2c-g2'){
		$channel_name1 = 'Channel A';
		$channel_name2 = 'Channel B';
		if($is_dual_channel==0){
			$channel_name1 = 'Channel A/B';
		}
	}
	if($_SESSION['device_id']=='nq-amp4c' || $_SESSION['device_id']=='nq-amp4c-g2'){
		$channel_name1 = 'Channel A';
		$channel_name2 = 'Channel B';
		$channel_name3 = 'Channel C';
		$channel_name4 = 'Channel D';
		if($is_dual_channel==0){
			$channel_name1 = 'Channel A/B';
		}
		if($is_dual_channel2==0){
			$channel_name3 = 'Channel C/D';
		}
	}
?>
<?php if($sip_enabled == '1'){ // start sip_enabled ?>
	
	<?php if(in_array($_SESSION['device_id'], ['nq-mix','nq-nqpaxxx', 'nq-amp2c', 'nq-amp4c', 'nq-amp2c-g2','nq-amp4c-g2','nq-edp01','nq-s810v2','nq-ga400p','nq-ga40p3','nq-ga20p2','nq-ga10p','nq-ga10pv','nq-ser20p2'])){ // start nq-mix, nq-nqpaxxx, nq-amp2c, nq-amp4c, nq-amp2c-g2, nq-amp4c-g2, nq-edp01, nq-s810v2, nq-ga400p, nq-ga40p3, nq-ga20p2, nq-ga10p, nq-ga10pv, nq-ser20p2 ?>
	<tr id="row_account_reg_uri">
    <td><strong>SIP Server Address:</strong></td>
    <td><input type="text" class="form-control" name="account_reg_uri" placeholder="" style="width:200px;" value="<?php echo $account_reg_uri; ?>"></td>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
    </tr>
	
	<tr id="row_local_port">
    <td><strong>SIP Network Port</strong></td>
    <td><input type="text" class="form-control" name="local_port" placeholder="" style="width:200px;" value="<?php echo $local_port; ?>"></td>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
    </tr>
	
	<?php if($_SESSION['device_id']!='nq-edp01'){ // start not for edp-01 ?>
	<tr id="row_codecs_read_only">
    <td><strong>SIP Codecs:</strong></td>
    <td>
		 <select multiple="multiple" name="codecs_read_only[]" id="codecs_read_only" class="form-control" style="width:200px;" disabled>
   				<option value="G722" selected="selected">G722</option>
			 <option value="ulaw" selected="selected">ulaw</option>
			 <option value="alaw" selected="selected">alaw</option>
		</select>
	</td>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
    </tr>	
	<?php } // end not for edp-01  ?>
	<?php } ?>
	
	<?php if(in_array($_SESSION['device_id'], ['nq-mix','nq-edp01','nq-s810v2','nq-ga40p3','nq-ga20p2','nq-ga10p','nq-ga10pv','nq-ser20p2'])){ // start nq-mix, nq-edp01, nq-s810v2, nq-ga40p3, nq-ga20p2, nq-ga10p, nq-ga10pv, nq-ser20p2 ?>
	
	<tr id="row_account_id_1">
    <td><strong>SIP Extension<?php if($channel_name1!=''){ echo ' ('.$channel_name1.'):'; } else { echo ':';}?></strong></td>
    <td><input type="text" class="form-control" name="account_id" placeholder="" style="width:200px;" value="<?php echo $account_id; ?>"></td>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
    </tr>
	<tr id="row_account_username_1">
    <td><strong>SIP Username<?php if($channel_name1!=''){ echo ' ('.$channel_name1.'):'; } else { echo ':';}?></strong></td>
    <td><input type="text" class="form-control" name="account_username" placeholder="" style="width:200px;" value="<?php echo $account_username; ?>"></td>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
    </tr>
	<tr id="row_account_password_1">
    <td><strong>SIP Password<?php if($channel_name1!=''){ echo ' ('.$channel_name1.'):'; } else { echo ':';}?></strong></td>
    <td><input type="text" class="form-control" name="account_password" placeholder="" style="width:200px;" value="<?php echo $account_password; ?>"></td>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
    </tr>
	
	<?php if(!in_array($_SESSION['device_id'], ['nq-edp01','nq-s810v2','nq-ga10p','nq-ga10pv'])){ // start not including  nq-edp01, nq-s810v2, nq-ga10p, nq-ga10pv ?>
	<tr id="row_intercom_cut_level">
      <td style="min-width:200px"><strong>Cut Level<?php if($channel_name1!=''){ echo ' ('.$channel_name1.'):'; } else { echo ':';}?> <span id="intercomCutLabel"><?php echo $intercom_cut_level; ?></span> dB</strong></td>
      <td><input id="intercom_cut_level" name="intercom_cut_level" type="range" min="-42" max="0" step="1" value="<?php echo $intercom_cut_level; ?>" style="width:200px;" onChange="intercomCutChanged(this.value)" oninput="updateIntercomCutLabel(this.value);"></td>
      <td>&nbsp;</td>
      <td>&nbsp;</td>
	</tr>
	<?php } // end not including  nq-edp01, nq-s810v2, nq-ga10p, nq-ga10pv  ?>
	
	<?php if(in_array($_SESSION['device_id'], ['nq-ga10p','nq-ga10pv'])){ // start nq-ga10p, nq-ga10pv ?>
	<tr id="row_talkback_gain">
      <td style="min-width:200px"><strong>Talkback Gain: <span id="talkbackGainLabel"><?php echo $talkback_gain; ?></span> dB</strong></td>
      <td><input id="talkback_gain" name="talkback_gain" type="range" min="-12" max="20" step="1" value="<?php echo $talkback_gain; ?>" style="width:200px;" onChange="talkbackGainChanged(this.value)" oninput="updateTalkbackGainLabel(this.value);"></td>
      <td>&nbsp;</td>
      <td>&nbsp;</td>
	</tr>
	<?php } // end nq-ga10p, nq-ga10pv  ?>


	<?php } // end nq-mix, nq-edp01, nq-s810v2, nq-ga40p3, nq-ga20p2, nq-ga10p, nq-ga10pv, nq-ser20p2 ?>
	
	<?php if(in_array($_SESSION['device_id'], ['nq-s810v2','nq-ga10p','nq-ga10pv'])){ // start nq-s810v2, nq-ga10p, nq-ga10pv ?>
	<tr id="row_port_type">
      <td><strong>Type:</strong></td>
      <td>
		   <select name="port_type" id="port_type" class="form-control" style="width: 250px">
	  			<option value="Speaker-Only" <?php if($port_type == 'Speaker-Only'){ echo 'selected="selected"';}?>>VoIP Speaker Only</option>
			   	<option value="Digital-Call-Switch-With-Speaker" <?php if($port_type == 'Digital-Call-Switch-With-Speaker'){ echo 'selected="selected"';}?>>Digital Call Switch & Speaker</option>
			</select>
		</td>
      <td>&nbsp;</td>
      <td>&nbsp;</td>
	</tr>
	<?php } // end nq-s810v2, nq-ga10p, nq-ga10pv ?>
	
	<?php if(in_array($_SESSION['device_id'], ['nq-s810v2','nq-edp01','nq-ga10p','nq-ga10pv'])){ // start nq-s810v2, nq-edp01, nq-ga10p, nq-ga10pv ?>
	 </tr>
      <tr id="row_dial_extension">
      <td><strong>Dial Extension:</strong></td>
      <td><input type="text" class="form-control" id="dial_extension" name="dial_extension" placeholder="" style="width:200px;" value="<?php echo $dial_extension; ?>"></td>
      <td>&nbsp;</td>
      <td>&nbsp;</td>
    </tr>
	<?php } // end nq-s810v2, nq-edp01, nq-ga10p, nq-ga10pv ?>
	
	<?php if(in_array($_SESSION['device_id'], ['nq-nqpaxxx', 'nq-amp2c','nq-amp4c','nq-amp2c-g2','nq-amp4c-g2','nq-ga400p'])){ // start nq-nqpaxxx, nq-amp2c, nq-amp4c, nq-amp2c-g2, nq-amp4c-g2, nq-ga400p ?>
	<tr id="row_account_id_1">
    <td><strong>SIP Extension (<?php echo $channel_name1; ?>):</strong></td>
    <td><input type="text" class="form-control" name="account_id_1" placeholder="" style="width:200px;" value="<?php echo $account_id_1; ?>"></td>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
    </tr>
	<tr id="row_account_username_1">
    <td><strong>SIP Username (<?php echo $channel_name1; ?>):</strong></td>
    <td><input type="text" class="form-control" name="account_username_1" placeholder="" style="width:200px;" value="<?php echo $account_username_1; ?>"></td>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
    </tr>
	<tr id="row_account_password_1">
    <td><strong>SIP Password (<?php echo $channel_name1; ?>):</strong></td>
    <td><input type="text" class="form-control" name="account_password_1" placeholder="" style="width:200px;" value="<?php echo $account_password_1; ?>"></td>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
    </tr>
	<tr id="row_intercom_cut_level_1">
      <td style="min-width:200px"><strong>Cut Level (<?php echo $channel_name1; ?>): <span id="intercomCutLabel_1"><?php echo $intercom_cut_level_1; ?></span> dB</strong></td>
      <td><input id="intercom_cut_level_1" name="intercom_cut_level_1" type="range" min="-42" max="0" step="1" value="<?php echo $intercom_cut_level_1; ?>" style="width:200px;" onChange="intercomCutChanged_1(this.value)" oninput="updateIntercomCutLabel_1(this.value);"></td>
      <td>&nbsp;</td>
      <td>&nbsp;</td>
	</tr>
	<?php if($_SESSION['device_id']=='nq-nqpaxxx' || $_SESSION['device_id']=='nq-ga400p' || ((in_array($_SESSION['device_id'], ['nq-amp2c','nq-amp4c','nq-amp2c-g2','nq-amp4c-g2'])) && $is_dual_channel==1)){ // start nq-nqpaxxx or nq-ga400p or nq-amp2c and is_dual_channel, nq-amp4c and is_dual_channel, nq-amp2c-g2 and is_dual_channel, nq-amp4c-g2 and is_dual_channel ?>
	
	<tr id="row_account_id_2">
    <td><strong>SIP Extension (<?php echo $channel_name2; ?>):</strong></td>
    <td><input type="text" class="form-control" name="account_id_2" placeholder="" style="width:200px;" value="<?php echo $account_id_2; ?>"></td>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
	<tr id="row_account_username_2">
    <td><strong>SIP Username (<?php echo $channel_name2; ?>):</strong></td>
    <td><input type="text" class="form-control" name="account_username_2" placeholder="" style="width:200px;" value="<?php echo $account_username_2; ?>"></td>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
    </tr>
	<tr id="row_account_password_2">
    <td><strong>SIP Password (<?php echo $channel_name2; ?>):</strong></td>
    <td><input type="text" class="form-control" name="account_password_2" placeholder="" style="width:200px;" value="<?php echo $account_password_2; ?>"></td>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
    </tr>
	<tr id="row_intercom_cut_level_2">
      <td style="min-width:200px"><strong>Cut Level (<?php echo $channel_name2; ?>): <span id="intercomCutLabel_2"><?php echo $intercom_cut_level_2; ?></span> dB</strong></td>
      <td><input id="intercom_cut_level_2" name="intercom_cut_level_2" type="range" min="-42" max="0" step="1" value="<?php echo $intercom_cut_level_2; ?>" style="width:200px;" onChange="intercomCutChanged_2(this.value)" oninput="updateIntercomCutLabel_2(this.value);"></td>
      <td>&nbsp;</td>
      <td>&nbsp;</td>
	</tr>
	<?php } // end start nq-nqpaxxx or nq-amp2c and is_dual_channel, nq-amp4c and is_dual_channel, nq-amp2c-g2 and is_dual_channel,  nq-amp4c-g2 and is_dual_channel ?>
		
	<?php if($_SESSION['device_id']=='nq-amp4c' || $_SESSION['device_id']=='nq-amp4c-g2'){ // start nq-amp4c nq-amp4c-g2 ?>
	
	<tr id="row_account_id_3">
    <td><strong>SIP Extension (<?php echo $channel_name3; ?>):</strong></td>
    <td><input type="text" class="form-control" name="account_id_3" placeholder="" style="width:200px;" value="<?php echo $account_id_3; ?>"></td>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
    </tr>
	<tr id="row_account_username_3">
    <td><strong>SIP Username (<?php echo $channel_name3; ?>):</strong></td>
    <td><input type="text" class="form-control" name="account_username_3" placeholder="" style="width:200px;" value="<?php echo $account_username_3; ?>"></td>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
    </tr>
	<tr id="row_account_password_3">
    <td><strong>SIP Password (<?php echo $channel_name3; ?>):</strong></td>
    <td><input type="text" class="form-control" name="account_password_3" placeholder="" style="width:200px;" value="<?php echo $account_password_3; ?>"></td>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
    </tr>
	<tr id="row_intercom_cut_level_3">
      <td style="min-width:200px"><strong>Cut Level (<?php echo $channel_name3; ?>): <span id="intercomCutLabel_3"><?php echo $intercom_cut_level_3; ?></span> dB</strong></td>
      <td><input id="intercom_cut_level_3" name="intercom_cut_level_3" type="range" min="-42" max="0" step="1" value="<?php echo $intercom_cut_level_3; ?>" style="width:200px;" onChange="intercomCutChanged_3(this.value)" oninput="updateIntercomCutLabel_3(this.value);"></td>
      <td>&nbsp;</td>
      <td>&nbsp;</td>
	</tr>
	<?php } // end nq-amp4c  nq-amp4c-g2 ?>
	
	
	<?php if( ($_SESSION['device_id']=='nq-amp4c' || $_SESSION['device_id']=='nq-amp4c-g2') && $is_dual_channel2==1){ // start nq-amp4c and is_dual_channel2, start nq-amp4c-g2 and is_dual_channel2 ?>
	
	<tr id="row_account_id_4">
    <td><strong>SIP Extension (<?php echo $channel_name4; ?>):</strong></td>
    <td><input type="text" class="form-control" name="account_id_4" placeholder="" style="width:200px;" value="<?php echo $account_id_4; ?>"></td>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
    </tr>
	<tr id="row_account_username_4">
    <td><strong>SIP Username (<?php echo $channel_name4; ?>):</strong></td>
    <td><input type="text" class="form-control" name="account_username_4" placeholder="" style="width:200px;" value="<?php echo $account_username_4; ?>"></td>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
    </tr>
	<tr id="row_account_password_4">
    <td><strong>SIP Password (<?php echo $channel_name4; ?>):</strong></td>
    <td><input type="text" class="form-control" name="account_password_4" placeholder="" style="width:200px;" value="<?php echo $account_password_4; ?>"></td>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
    </tr>
	<tr id="row_intercom_cut_level_4">
      <td style="min-width:200px"><strong>Cut Level (<?php echo $channel_name4; ?>): <span id="intercomCutLabel_4"><?php echo $intercom_cut_level_4; ?></span> dB</strong></td>
      <td><input id="intercom_cut_level_4" name="intercom_cut_level_4" type="range" min="-42" max="0" step="1" value="<?php echo $intercom_cut_level_4; ?>" style="width:200px;" onChange="intercomCutChanged_4(this.value)" oninput="updateIntercomCutLabel_4(this.value);"></td>
      <td>&nbsp;</td>
      <td>&nbsp;</td>
	</tr>
	<?php } // end nq-amp4c and is_dual_channel2, nq-amp4c-g2 and is_dual_channel2?>
	
	
	<?php }  // end nq-nqpaxxx, nq-amp2c, nq-amp4c, nq-amp2c-g2, nq-amp4c-g2  ?>
	
	<?php if(in_array($_SESSION['device_id'], ['nq-edp01','nq-s810v2','nq-ga10p','nq-ga10pv'])){ // start nq-edp01, nq-s810v2, nq-ga10pv, nq-ga10pv ?>
	 <tr id="intercom_cut">
      <td style="min-width:200px"><strong>Intercom Cut Level: <span id="intercomCutLabel"><?php echo $intercom_cut_level; ?></span> dB</strong></td>
      <td><input id="intercom_cut_level" name="intercom_cut_level" type="range" min="-42" max="0" step="1" value="<?php echo $intercom_cut_level; ?>" style="width:200px;" onChange="intercomCutChanged(this.value)" oninput="updateIntercomCutLabel(this.value);"></td>
      <td>&nbsp;</td>
      <td>&nbsp;</td>
	</tr>
	<?php } // end nq-edp01,nq-s810v2,nq-ga10p,nq-ga10pv ?>
<?php } // end sip_enabled ?>	
</table><br>
<button id="save_configuration_settings" name="save_configuration_settings" class="btn btn-xs btn-success" type="submit" onClick="show_processing();"><i class="fa fa-floppy-o"></i>&nbsp;&nbsp;Save Configuration Settings</button>
<br><br>
</form>
	
<?php if($_SESSION['device_id']=='nq-ga400p'){ // start show for GA400P ?>
	
<div class="panel panel-default"><!-- start panel -->
  <div class="panel-heading"><h4><strong><i class="fa fa-long-arrow-right"></i> Unicast Address (Transmit / Audiocast)</strong></h4></div>
  <div class="panel-body">
      <div id="status4"></div>
      <form id="form4" name="form4" action="configuration_s.php" method="post">

      <input name="unicast_stream_enable_data" id="unicast_stream_enable_data" type="hidden" value="<?php echo $unicast_stream_enable; ?>">	
      <input name="unicast_stream_address_data" id="unicast_stream_address_data" type="hidden" value="<?php echo $unicast_stream_address; ?>">	
      <input name="unicast_stream_port_data" id="unicast_stream_port_data" type="hidden" value="<?php echo $unicast_stream_port; ?>">	
      <input name="unicast_stream_codec_data" id="unicast_stream_codec_data" type="hidden" value="<?php echo $unicast_stream_codec; ?>">	
      <input name="unicast_stream_source_data" id="unicast_stream_source_data" type="hidden" value="<?php echo $unicast_stream_source; ?>">
      <input name="unicast_stream_description_data" id="unicast_stream_description_data" type="hidden" value="<?php echo $unicast_stream_description; ?>">

      <div id="message_container4"><?php echo $message4; ?></div>
      <table id="unicast_stream" class="table table-striped">
      <thead>
          <th><strong>Enabled</strong></th>
          <th><strong>Unicast IP Address</strong></th>
          <th><strong>Unicast Port Number</strong></th>
          <th><strong>Codec</strong></th>
          <th><strong>Line Input</strong></th>
          <th><strong>Description</strong></th>
      </thead>
      <tbody>
      <?php echo $unicast_stream_html;?>
      </tbody>
      </table>
      <?php if($unicast_receive_enable=='No' && $audio_cast_enable_total==0){ ?> 
      <button id="save_unicast_stream" name="save_unicast_stream" class="btn btn-xs btn-success" type="submit" onClick="show_processing4();"><i class="fa fa-floppy-o"></i>&nbsp;&nbsp;Save Unicast Address (Transmit / Audiocast)</button>
      <?php } else if($audio_cast_enable_total>0){ ?>
     	 <strong>Note:</strong> Editing Unicast Address (Transmit / Audiocast) is Disabled because Multicast Addresses (Transmit / Audiocast) is Enabled.
      <?php } else { ?>
		  <strong>Note:</strong> Editing Unicast Address (Transmit / Audiocast) is Disabled because Unicast Port (Receive) is Enabled.
	   <?php } ?>
      </form>
  </div>
</div>	<!-- end panel -->
	
<div class="panel panel-default"><!-- start panel -->
  <div class="panel-heading"><h4><strong><i class="fa fa-long-arrow-right"></i> Unicast Port (Receive)</strong></h4></div>
  <div class="panel-body">	
      <div id="status5"></div>
      <form id="form5" name="form5" action="configuration_s.php" method="post">

      <input name="unicast_receive_enable_data" id="unicast_receive_enable_data" type="hidden" value="<?php echo $unicast_receive_enable; ?>">	
      <input name="unicast_receive_port_data" id="unicast_receive_port_data" type="hidden" value="<?php echo $unicast_receive_port; ?>">	
      <input name="unicast_receive_codec_data" id="unicast_receive_codec_data" type="hidden" value="<?php echo $unicast_receive_codec; ?>">	
      <input name="unicast_receive_line_output_data" id="unicast_receive_line_output_data" type="hidden" value="<?php echo $unicast_receive_line_output; ?>">	
      <input name="unicast_receive_cut_data" id="unicast_receive_cut_data" type="hidden" value="<?php echo $unicast_receive_cut; ?>">	
      <input name="unicast_receive_description_data" id="unicast_receive_description_data" type="hidden" value="<?php echo $unicast_receive_description; ?>">	

      <div id="message_container5"><?php echo $message5; ?></div>
      <table id="unicast_receive" class="table table-striped">
      <thead>
          <th><strong>Enabled</strong></th>
          <th><strong>Unicast Port Number</strong></th>
          <th><strong>Codec</strong></th>
          <th><strong>Channels</strong></th>
          <th><strong>Cut Level (dB)</strong></th>
          <th><strong>Description</strong></th>
      </thead>
      <tbody>
      <?php echo $unicast_receive_html;?>
      </tbody>
      </table>
      <?php if($unicast_stream_enable=='No'){ ?> 
      <strong>Note:</strong> The following codecs are supported for unicast: G722 and OPUS.
      <br><br>
      <button id="save_unicast_receive" name="save_unicast_receive" class="btn btn-xs btn-success" type="submit" onClick="show_processing5();"><i class="fa fa-floppy-o"></i>&nbsp;&nbsp;Save Unicast Port (Receive)</button>
      <?php } else { ?>
      <strong>Note:</strong> Editing Unicast Port (Receive) is Disabled because Unicast Address (Transmit / Audiocast) is Enabled.
      <?php } ?>
      </form>
  </div>
</div><!-- end panel -->
	
<div class="panel panel-default"><!-- start panel -->
  <div class="panel-heading"><h4><strong><i class="fa fa-sitemap"></i> Multicast Addresses (Transmit / Audiocast)</strong></h4></div>
  <div class="panel-body"> 	
      <div id="status3"></div>
      <form id="form3" name="form3" action="configuration_s.php" method="post">
      <div id="message_container3"><?php echo $message3; ?></div>
      <input type="hidden" name="dataArray2" id="dataArray2" value="">
      <table id="audio_casts" class="table table-striped">
      <thead>
          <th><strong>Enabled</strong></th>
          <th><strong>Multicast IP Address</strong></th>
          <th><strong>Multicast Port Number</strong></th>
          <th><strong>Codec</strong></th>
          <th><strong>Line Input</strong></th>
          <th><strong>Description</strong></th>
      </thead>
      <tbody id="list_items2">
      <?php echo $audio_cast_html;?>
      </tbody>
      </table>
		 <?php if($unicast_stream_enable=='No'){ ?>
      <button id="save_multicast_addresses2" name="save_multicast_addresses2" class="btn btn-xs btn-success" type="submit" onClick="show_processing3();"><i class="fa fa-floppy-o"></i>&nbsp;&nbsp;Save Multicast Addresses (Transmit / Audiocast)</button>
		  <?php } else { ?>
		   <strong>Note:</strong> Editing Multicast Addresses (Transmit / Audiocast) is Disabled because Unicast Address (Transmit / Audiocast) is Enabled.
		  <?php } ?>
      </form>
  </div>
</div><!-- end panel -->
<?php } // end show for GA400P ?>	
	
<div class="panel panel-default"><!-- start panel -->
  <div class="panel-heading"><h4><strong><i class="fa fa-sitemap"></i> Multicast Addresses (Receive)</strong>&nbsp;&nbsp;<button class="btn btn-xs btn-success" type="button" onClick="addRow();"><i class="fa fa-plus"></i></button></h4>	</div>	
  <div class="panel-body">
      
      <div id="status2"></div>
      <form id="form2" name="form2" action="configuration_s.php" method="post">
      <div id="message_container2"><?php echo $message2; ?></div>
      <input type="hidden" name="dataArray" id="dataArray" value="">
      <strong>Sorting:</strong> <input id="sorting_mode" name="sorting_mode" class="form-control" data-on="Enabled" data-off="Disabled" data-toggle="toggle" data-size="mini" type="checkbox" value="1">	
      <table id="multicasts" class="table table-striped">
      <thead>
          <th></th>
          <th></th>
          <th><strong>Multicast IP Address</strong></th>
          <th><strong>Multicast Port Number</strong></th>
          <th><strong>Codec</strong></th>
          <th><strong>Channels</strong></th>
          <th><strong>Cut Level (dB)</strong></th>
          <th><strong>Description</strong></th>
      </thead>
      <tbody id="list_items">
      <?php echo $multicast_html;?>
      </tbody>
      </table>
      <strong>Note:</strong> The following codecs are supported for multicast: G711 u-law, G711 a-law, <?php if($_SESSION['device_id']=='nq-edp01'){  echo 'and G722.'; } else { echo 'G722, and OPUS.'; } ?><br>
      <br>
      <button id="save_multicast_addresses" name="save_multicast_addresses" class="btn btn-xs btn-success" type="submit" onClick="show_processing2();"><i class="fa fa-floppy-o"></i>&nbsp;&nbsp;Save Multicast Addresses (Receive)</button>
      </form>
  </div>
</div>	<!-- end panel -->
		
</div>	
<br><br><br><br>
<script src="/js/jquery.min.js"></script>
<script src="/js/bootstrap.min.js"></script>
<script src="/js/bootstrap-toggle.min.js"></script>
<script src="/js/select2.min.js"></script>
<script src="/js/bootstrap-editable.js"></script> 
<script src="/js/Sortable.min.js"></script>
<script src="/js/bootstrap-growl.min.js"></script>
</body>
</html>
<script>
var dataArray = [];
var dataArray2 = [];
$(document).ready(function() { // start ready function
		
	dataArray  = <?php echo $dataArrayString;?>;
	dataArray2  = <?php echo $dataArrayString2;?>;

	document.getElementById("dataArray").value = JSON.stringify(dataArray);
	
	<?php if($_SESSION['device_id']=='nq-ga400p'){ // start  for ga400p ?>
		document.getElementById("dataArray2").value = JSON.stringify(dataArray2);
	<?php } // end for ga400p  ?>
	
	var device_id = '<?php echo $_SESSION['device_id']; ?>';

	// only start the web socket for these devices
	if(['nq-mix','nq-amp2c','nq-amp4c','nq-s810v2','nq-ga20p2','nq-nqpaxxx','nq-amp2c','nq-amp4c','nq-amp2c-g2','nq-amp4c-g2','nq-edp01','nq-ser20p2'].indexOf(device_id)>-1){ 
		init_socket(); 
	}
		
	if(['nq-s810v2','nq-edp01','nq-ga10p','nq-ga10pv','nq-ga20p2','nq-ga400p','nq-ser20p2'].indexOf(device_id)>-1){
		// do nothing, line_in_priority is hidden
	} else {
		if(document.getElementById("line_in_priority").value=='1'){
            $('#line_in_priority').bootstrapToggle('on');
        } else {
            $('#line_in_priority').bootstrapToggle('off');
        }
	}
	if(device_id=='nq-ga10p' || device_id=='nq-ga10pv' || device_id=='nq-s810v2' || device_id=='nq-ser20p2'){
		if(document.getElementById("external_relay_trigger").value=='ENABLED'){
            $('#external_relay_trigger').bootstrapToggle('on');
        } else {
            $('#external_relay_trigger').bootstrapToggle('off');
        }
		var port_type = '<?php echo $port_type; ?>';
		var sip_enabled = '<?php echo $sip_enabled; ?>';
		if(sip_enabled=='1'){
		    if(port_type=='Speaker-Only'){
            document.getElementById('dial_extension').value = '';
            document.getElementById('dial_extension').readOnly = true;
            } else {
               document.getElementById('dial_extension').readOnly = false;  
            }
		}
	}
	if(device_id=='nq-ga400p'){
		if(document.getElementById("external_relay_trigger_1").value=='ENABLED'){
            $('#external_relay_trigger_1').bootstrapToggle('on');
        } else {
            $('#external_relay_trigger_1').bootstrapToggle('off');
        }
		if(document.getElementById("external_relay_trigger_2").value=='ENABLED'){
            $('#external_relay_trigger_2').bootstrapToggle('on');
        } else {
            $('#external_relay_trigger_2').bootstrapToggle('off');
        }
	}
}); // end ready function
	
var socket;
	
// timezone
$("#timezone").select2({
      placeholder: "Select a time zone",
})
//  clear message after 10 seconds
setTimeout(function(){ 
	document.getElementById("message_container").innerHTML = '';
}, 10000);
setTimeout(function(){ 
	document.getElementById("message_container2").innerHTML = '';
}, 10000);
<?php if($_SESSION['device_id']=='nq-ga400p'){ // start show for ga400p ?>
setTimeout(function(){ 
	document.getElementById("message_container3").innerHTML = '';
}, 10000);
setTimeout(function(){ 
	document.getElementById("message_container4").innerHTML = '';
}, 10000);
setTimeout(function(){ 
	document.getElementById("message_container5").innerHTML = '';
}, 10000);
<?php } // end show for ga400p 1 ?>
// show_processing
function show_processing(){
	document.getElementById("status").innerHTML = '<i class="fa fa-refresh fa-spin fa-fw"></i> <strong>Processing ... </strong>';
}
// show_processing2
function show_processing2(){
	document.getElementById("status2").innerHTML = '<i class="fa fa-refresh fa-spin fa-fw"></i> <strong>Processing ... </strong>';
}
// show_processing3
function show_processing3(){
	document.getElementById("status3").innerHTML = '<i class="fa fa-refresh fa-spin fa-fw"></i> <strong>Processing ... </strong>';
}
function show_processing4(){
	document.getElementById("status4").innerHTML = '<i class="fa fa-refresh fa-spin fa-fw"></i> <strong>Processing ... </strong>';
}
function show_processing5(){
	document.getElementById("status5").innerHTML = '<i class="fa fa-refresh fa-spin fa-fw"></i> <strong>Processing ... </strong>';
}
// ptt_type drop down
$(".ptt-type-action").click(function(){
    $(this).parents(".ptt-type-btn").find('.selection').text($(this).text());
    $(this).parents(".ptt-type-btn").find('.selection').val($(this).data('value'));
    document.getElementById("ptt_type").value = $(this).data('value');
    document.getElementById("ptt_type_name").value = $(this).text();
    $('.open').removeClass('open');
    return false;
});
// ptt_output drop down
$(".ptt-output-action").click(function(){
    $(this).parents(".ptt-output-btn").find('.selection').text($(this).text());
    $(this).parents(".ptt-output-btn").find('.selection').val($(this).data('value'));
    document.getElementById("ptt_output").value = $(this).data('value');
    document.getElementById("ptt_output_name").value = $(this).text();
    $('.open').removeClass('open');
    return false;
});
// output_power drop down
$(".output-power-action").click(function(){
    $(this).parents(".output-power-btn").find('.selection').text($(this).text());
    $(this).parents(".output-power-btn").find('.selection').val($(this).data('value'));
    document.getElementById("output_power").value = $(this).data('value');
    $('.open').removeClass('open');
    return false;
});
// speaker_output_power drop down
$(".speaker-output-power-action").click(function(){
    $(this).parents(".speaker-output-power-btn").find('.selection').text($(this).text());
    $(this).parents(".speaker-output-power-btn").find('.selection').val($(this).data('value'));
    document.getElementById("speaker_output_power").value = $(this).data('value');
    document.getElementById("speaker_output_power_name").value = $(this).text();
    $('.open').removeClass('open');
    return false;
});
// output_power1 drop down
$(".output-power1-action").click(function(){
    $(this).parents(".output-power1-btn").find('.selection').text($(this).text());
    $(this).parents(".output-power1-btn").find('.selection').val($(this).data('value'));
    document.getElementById("output_power1").value = $(this).data('value');
    document.getElementById("output_power_name1").value = $(this).text();
    $('.open').removeClass('open');
    return false;
});
// output_power2 drop down
$(".output-power2-action").click(function(){
    $(this).parents(".output-power2-btn").find('.selection').text($(this).text());
    $(this).parents(".output-power2-btn").find('.selection').val($(this).data('value'));
    document.getElementById("output_power2").value = $(this).data('value');
    document.getElementById("output_power_name2").value = $(this).text();
    $('.open').removeClass('open');
    return false;
});
// output_power3 drop down
$(".output-power3-action").click(function(){
    $(this).parents(".output-power3-btn").find('.selection').text($(this).text());
    $(this).parents(".output-power3-btn").find('.selection').val($(this).data('value'));
    document.getElementById("output_power3").value = $(this).data('value');
    document.getElementById("output_power_name3").value = $(this).text();
    $('.open').removeClass('open');
    return false;
});
// output_power4 drop down
$(".output-power4-action").click(function(){
    $(this).parents(".output-power4-btn").find('.selection').text($(this).text());
    $(this).parents(".output-power4-btn").find('.selection').val($(this).data('value'));
    document.getElementById("output_power4").value = $(this).data('value');
    document.getElementById("output_power_name4").value = $(this).text();
    $('.open').removeClass('open');
    return false;
});
// speaker_load_impedance drop down
$(".speaker-load-impedance-action").click(function(){
    $(this).parents(".speaker-load-impedance-btn").find('.selection').text($(this).text());
    $(this).parents(".speaker-load-impedance-btn").find('.selection').val($(this).data('value'));
    document.getElementById("speaker_load_impedance").value = $(this).data('value');
    document.getElementById("speaker_load_impedance_name").value = $(this).text();
    $('.open').removeClass('open');
    return false;
});
// load_impedance1 drop down
$(".load-impedance1-action").click(function(){
    $(this).parents(".load-impedance1-btn").find('.selection').text($(this).text());
    $(this).parents(".load-impedance1-btn").find('.selection').val($(this).data('value'));
    document.getElementById("load_impedance1").value = $(this).data('value');
    document.getElementById("load_impedance_name1").value = $(this).text();
    $('.open').removeClass('open');
    return false;
});
// load_impedance2 drop down
$(".load-impedance2-action").click(function(){
    $(this).parents(".load-impedance2-btn").find('.selection').text($(this).text());
    $(this).parents(".load-impedance2-btn").find('.selection').val($(this).data('value'));
    document.getElementById("load_impedance2").value = $(this).data('value');
    document.getElementById("load_impedance_name2").value = $(this).text();
    $('.open').removeClass('open');
    return false;
});
// load_impedance3 drop down
$(".load-impedance3-action").click(function(){
    $(this).parents(".load-impedance3-btn").find('.selection').text($(this).text());
    $(this).parents(".load-impedance3-btn").find('.selection').val($(this).data('value'));
    document.getElementById("load_impedance3").value = $(this).data('value');
    document.getElementById("load_impedance_name3").value = $(this).text();
    $('.open').removeClass('open');
    return false;
});
// load_impedance4 drop down
$(".load-impedance4-action").click(function(){
    $(this).parents(".load-impedance4-btn").find('.selection').text($(this).text());
    $(this).parents(".load-impedance4-btn").find('.selection').val($(this).data('value'));
    document.getElementById("load_impedance4").value = $(this).data('value');
    document.getElementById("load_impedance_name4").value = $(this).text();
    $('.open').removeClass('open');
    return false;
});
// sip_enable drop down
$(".sip-enabled-action").click(function(){
	var sip_enabled_before = document.getElementById("sip_enabled").value;
    $(this).parents(".sip-enabled-btn").find('.selection').text($(this).text());
    $(this).parents(".sip-enabled-btn").find('.selection').val($(this).data('value'));
    document.getElementById("sip_enabled").value = $(this).data('value');
    document.getElementById("sip_enabled_name").value = $(this).text();	
	if($(this).data('value')=='1'){
		if(sip_enabled_before!='1'){
		    add_sip_rows();
		 }
	} else {
	   remove_sip_rows();
	}
	$('.open').removeClass('open');
    return false;
});
function remove_sip_rows(){
	$("#row_codecs_read_only").remove();
	$("#row_account_reg_uri").remove();
	$("#row_local_port").remove();
	$("#row_account_id").remove();
	$("#row_account_username").remove();
	$("#row_account_password").remove();
	$("#row_intercom_cut_level").remove();
	$("#row_account_id_1").remove();
	$("#row_account_username_1").remove();
	$("#row_account_password_1").remove();
	$("#row_intercom_cut_level_1").remove();
	$("#row_account_id_2").remove();
	$("#row_account_username_2").remove();
	$("#row_account_password_2").remove();	
	$("#row_intercom_cut_level_2").remove();
	$("#row_account_id_3").remove();
	$("#row_account_username_3").remove();
	$("#row_account_password_3").remove();	
	$("#row_intercom_cut_level_3").remove();
	$("#row_account_id_4").remove();
	$("#row_account_username_4").remove();
	$("#row_account_password_4").remove();
	$("#row_intercom_cut_level_4").remove();
	$("#intercom_cut").remove();
	$("#row_talkback_gain").remove();
	$("#row_dial_extension").remove();
	$("#row_port_type").remove();
}
function add_sip_rows(){
	
	var device_id = document.getElementById('device_id').value;
	var IsDualChannel = document.getElementById('IsDualChannel').value;
	var IsDualChannel2 = document.getElementById('IsDualChannel2').value;
	
	var channel_name1 = '';
	var channel_name2 = '';
	var channel_name3 = '';
	var channel_name4 = '';
	if(device_id=='nq-mix'){
		channel_name1 = ' (Line Out)';
	}
	if(device_id=='nq-ga400p'){
		channel_name1 = 'Line Output 1';
		channel_name2 = 'Line Output 2';
	}
	if(device_id=='nq-nqpaxxx'){
		channel_name1 = 'Line Out';
		channel_name2 = 'Speaker Out';
	}
	if(device_id=='nq-amp2c' || device_id=='nq-amp2c-g2'){
		channel_name1 = 'Channel A';
		channel_name2 = 'Channel B';
		if(IsDualChannel==0){
			channel_name1 = 'Channel A/B';
		} else {
			channel_name1 = 'Channel A';
		}
	}
		if(device_id=='nq-amp4c' || device_id=='nq-amp4c-g2'){
		channel_name1 = 'Channel A';
		channel_name2 = 'Channel B';
		channel_name3 = 'Channel C';
		channel_name4 = 'Channel D';
		if(IsDualChannel==0){
			channel_name1 = 'Channel A/B';
		}
		if(IsDualChannel2==0){
			channel_name3 = 'Channel C/D';
		}
	}		
	var table = document.getElementById('config_table');
	var row = table.insertRow();
	row.id = 'row_account_reg_uri';
	var cell1 = row.insertCell(0);
	var cell2 = row.insertCell(1);
	var cell3 = row.insertCell(2);
	var cell4 = row.insertCell(3);
	
	cell1.innerHTML = '<strong>SIP Server Address:</strong>';
	cell2.innerHTML = '<input type="text" class="form-control" name="account_reg_uri" placeholder="" style="width:200px;" value="<?php echo $account_reg_uri; ?>">';
	cell3.innerHTML = '&nbsp;';
	cell4.innerHTML = '&nbsp;';
	
	var row = table.insertRow();
	row.id = 'row_local_port';
	var cell1 = row.insertCell(0);
	var cell2 = row.insertCell(1);
	var cell3 = row.insertCell(2);
	var cell4 = row.insertCell(3);
	
	cell1.innerHTML = '<strong>SIP Network Port:</strong>';
	cell2.innerHTML = '<input type="text" class="form-control" name="local_port" placeholder="" style="width:200px;" value="<?php echo $local_port; ?>">';
	cell3.innerHTML = '&nbsp;';
	cell4.innerHTML = '&nbsp;';
	
	var row = table.insertRow();
	row.id = 'row_codecs_read_only';
	var cell1 = row.insertCell(0);
	var cell2 = row.insertCell(1);
	var cell3 = row.insertCell(2);
	var cell4 = row.insertCell(3);
	
	cell1.innerHTML = '<strong>SIP Codecs:</strong>';
	cell2.innerHTML = '<select multiple="multiple" name="codecs_read_only[]" id="codecs_read_only" class="form-control" style="width:200px;" disabled><option value="G722" selected="selected">G722</option><option value="ulaw" selected="selected">ulaw</option><option value="alaw" selected="selected">alaw</option></select>';
	cell3.innerHTML = '&nbsp;';
	cell4.innerHTML = '&nbsp;';
	
	// codecs_read_only
    $('#codecs_read_only').select2({
        placeholder: 'Add codecs ...',
        tokenSeparators: [',', ' '],
        closeOnSelect: false
    });
	
	if(device_id=='nq-mix' || device_id=='nq-s810v2' || device_id=='nq-ga40p3' || device_id=='nq-ga20p2' || device_id=='nq-ga10p' || device_id=='nq-ga10pv' || device_id=='nq-ser20p2'){ // start nq-mix, nq-s810v2, nq-ga40p3, nq-ga20p2, nq-ga10p, nq-ga10pv, nq-ser20p2
	
	var row = table.insertRow();
	row.id = 'row_account_id';
	var cell1 = row.insertCell(0);
	var cell2 = row.insertCell(1);
	var cell3 = row.insertCell(2);
	var cell4 = row.insertCell(3);
	
	cell1.innerHTML = '<strong>SIP Extension'+channel_name1+':</strong>';
	cell2.innerHTML = '<input type="text" class="form-control" name="account_id" placeholder="" style="width:200px;" value="<?php echo $account_id; ?>">';
	cell3.innerHTML = '&nbsp;';
	cell4.innerHTML = '&nbsp;';
		
	var row = table.insertRow();
	row.id = 'row_account_username';
	var cell1 = row.insertCell(0);
	var cell2 = row.insertCell(1);
	var cell3 = row.insertCell(2);
	var cell4 = row.insertCell(3);
	
	cell1.innerHTML = '<strong>SIP Username'+channel_name1+':</strong>';
	cell2.innerHTML = '<input type="text" class="form-control" name="account_username" placeholder="" style="width:200px;" value="<?php echo $account_username; ?>">';
	cell3.innerHTML = '&nbsp;';
	cell4.innerHTML = '&nbsp;';
	
	var row = table.insertRow();
	row.id = 'row_account_password';
	var cell1 = row.insertCell(0);
	var cell2 = row.insertCell(1);
	var cell3 = row.insertCell(2);
	var cell4 = row.insertCell(3);
	
	cell1.innerHTML = '<strong>SIP Password'+channel_name1+':</strong>';
	cell2.innerHTML = '<input type="text" class="form-control" name="account_password" placeholder="" style="width:200px;" value="<?php echo $account_password; ?>">';
	cell3.innerHTML = '&nbsp;';
	cell4.innerHTML = '&nbsp;';
				
	if(device_id=='nq-mix' || device_id=='nq-ga40p3' || device_id=='nq-ga20p2' || device_id=='nq-ser20p2'){ // start nq-mix, nq-ga40p3, nq-ga20p2, nq-ser20p2
		
	var row = table.insertRow();
    row.id = 'row_intercom_cut_level';
    var cell1 = row.insertCell(0);
    var cell2 = row.insertCell(1);
    var cell3 = row.insertCell(2);
    var cell4 = row.insertCell(3);

    cell1.innerHTML = '<strong>Cut Level'+channel_name1+': <span id="intercomCutLabel"><?php echo $intercom_cut_level; ?></span> dB</strong>';
    cell2.innerHTML = '<input id="intercom_cut_level" name="intercom_cut_level" type="range" min="-42" max="0" step="1" value="<?php echo $intercom_cut_level; ?>" style="width:200px;" onChange="intercomCutChanged(this.value)" oninput="updateIntercomCutLabel(this.value);">';
    cell3.innerHTML = '&nbsp;';
    cell4.innerHTML = '&nbsp;';	
		
	} // end nq-mix, nq-ga40p3, nq-ga20p2, nq-ser20p2
		
	if(device_id=='nq-ga10p' || device_id=='nq-ga10pv'){ // start  nq-ga10p, nq-ga10pv
		
	var row = table.insertRow();
    row.id = 'row_talkback_gain';
    var cell1 = row.insertCell(0);
    var cell2 = row.insertCell(1);
    var cell3 = row.insertCell(2);
    var cell4 = row.insertCell(3);

    cell1.innerHTML = '<strong>Talkback Gain: <span id="talkbackGainLabel"><?php echo $talkback_gain; ?></span> dB</strong>';
    cell2.innerHTML = '<input id="talkback_gain" name="talkback_gain" type="range" min="-12" max="20" step="1" value="<?php echo $talkback_gain; ?>" style="width:200px;" onChange="talkbackGainChanged(this.value)" oninput="updateTalkbackGainLabel(this.value);">';
    cell3.innerHTML = '&nbsp;';
    cell4.innerHTML = '&nbsp;';	
		
	} // end  nq-ga10p, nq-ga10pv
				
	} // end nq-mix, nq-s810v2, nq-ga40p3, nq-ga20p2, nq-ga10p, nq-ga10pv, nq-ser20p2
	
	if(device_id=='nq-nqpaxxx' || device_id=='nq-ga400p' || (device_id=='nq-amp2c') || (device_id=='nq-amp2c-g2') || (device_id=='nq-amp4c') || (device_id=='nq-amp4c-g2')){ // start nq-nqpaxxx, nq-ga400p, nq-amp2c, nq-amp4c, nq-amp2c-g2, nq-amp4c-g2
	
	var row = table.insertRow();
	row.id = 'row_account_id_1';
	var cell1 = row.insertCell(0);
	var cell2 = row.insertCell(1);
	var cell3 = row.insertCell(2);
	var cell4 = row.insertCell(3);
	
	cell1.innerHTML = '<strong>SIP Extension ('+channel_name1+'):</strong>';
	cell2.innerHTML = '<input type="text" class="form-control" name="account_id_1" placeholder="" style="width:200px;" value="<?php echo $account_id_1; ?>">';
	cell3.innerHTML = '&nbsp;';
	cell4.innerHTML = '&nbsp;';
	
	var row = table.insertRow();
	row.id = 'row_account_username_1';
	var cell1 = row.insertCell(0);
	var cell2 = row.insertCell(1);
	var cell3 = row.insertCell(2);
	var cell4 = row.insertCell(3);
	
	cell1.innerHTML = '<strong>SIP Username ('+channel_name1+'):</strong>';
	cell2.innerHTML = '<input type="text" class="form-control" name="account_username_1" placeholder="" style="width:200px;" value="<?php echo $account_username_1; ?>">';
	cell3.innerHTML = '&nbsp;';
	cell4.innerHTML = '&nbsp;';
	
	var row = table.insertRow();
	row.id = 'row_account_password_1';
	var cell1 = row.insertCell(0);
	var cell2 = row.insertCell(1);
	var cell3 = row.insertCell(2);
	var cell4 = row.insertCell(3);
	
	cell1.innerHTML = '<strong>SIP Password ('+channel_name1+'):</strong>';
	cell2.innerHTML = '<input type="text" class="form-control" name="account_password_1" placeholder="" style="width:200px;" value="<?php echo $account_password_1; ?>">';
	cell3.innerHTML = '&nbsp;';
	cell4.innerHTML = '&nbsp;';
		
	if(device_id!='nq-s810v2'){ // start do not include nq-s810v2 
	
	var row = table.insertRow();
    row.id = 'row_intercom_cut_level_1';
    var cell1 = row.insertCell(0);
    var cell2 = row.insertCell(1);
    var cell3 = row.insertCell(2);
    var cell4 = row.insertCell(3);

    cell1.innerHTML = '<strong>Cut Level ('+channel_name1+'): <span id="intercomCutLabel_1"><?php echo $intercom_cut_level_1; ?></span> dB</strong>';
    cell2.innerHTML = '<input id="intercom_cut_level_1" name="intercom_cut_level_1" type="range" min="-42" max="0" step="1" value="<?php echo $intercom_cut_level_1; ?>" style="width:200px;" onChange="intercomCutChanged_1(this.value)" oninput="updateIntercomCutLabel_1(this.value);">';
    cell3.innerHTML = '&nbsp;';
    cell4.innerHTML = '&nbsp;';	
		
	} // end do not include nq-s810v2  
		
	} // end nq-nqpaxxx, nq-ga400p, nq-amp2c, nq-amp4c, nq-amp2c-g2, nq-amp4c-g2
	
	if(device_id=='nq-nqpaxxx' || device_id=='nq-ga400p' || (device_id=='nq-amp2c' && IsDualChannel==1) || (device_id=='nq-amp2c-g2' && IsDualChannel==1) || (device_id=='nq-amp4c' && IsDualChannel==1) || (device_id=='nq-amp4c-g2' && IsDualChannel==1)){ // start nq-nqpaxxx, nq-ga400p, nq-amp2c, nq-amp4c, nq-amp2c-g2, nq-amp4c-g2
		
	var row = table.insertRow();
	row.id = 'row_account_id_2';
	var cell1 = row.insertCell(0);
	var cell2 = row.insertCell(1);
	var cell3 = row.insertCell(2);
	var cell4 = row.insertCell(3);
	   
	cell1.innerHTML = '<strong>SIP Extension ('+channel_name2+'):</strong>';
	cell2.innerHTML = '<input type="text" class="form-control" name="account_id_2" placeholder="" style="width:200px;" value="<?php echo $account_id_2; ?>">';
	cell3.innerHTML = '&nbsp;';
	cell4.innerHTML = '&nbsp;';
		
	var row = table.insertRow();
	row.id = 'row_account_username_2';
	var cell1 = row.insertCell(0);
	var cell2 = row.insertCell(1);
	var cell3 = row.insertCell(2);
	var cell4 = row.insertCell(3);
	
	cell1.innerHTML = '<strong>SIP Username ('+channel_name2+'):</strong>';
	cell2.innerHTML = '<input type="text" class="form-control" name="account_username_2" placeholder="" style="width:200px;" value="<?php echo $account_username_2; ?>">';
	cell3.innerHTML = '&nbsp;';
	cell4.innerHTML = '&nbsp;';
	
	var row = table.insertRow();
	row.id = 'row_account_password_2';
	var cell1 = row.insertCell(0);
	var cell2 = row.insertCell(1);
	var cell3 = row.insertCell(2);
	var cell4 = row.insertCell(3);
	
	cell1.innerHTML = '<strong>SIP Password ('+channel_name2+'):</strong>';
	cell2.innerHTML = '<input type="text" class="form-control" name="account_password_2" placeholder="" style="width:200px;" value="<?php echo $account_password_2; ?>">';
	cell3.innerHTML = '&nbsp;';
	cell4.innerHTML = '&nbsp;';	
		
	var row = table.insertRow();
    row.id = 'row_intercom_cut_level_2';
    var cell1 = row.insertCell(0);
    var cell2 = row.insertCell(1);
    var cell3 = row.insertCell(2);
    var cell4 = row.insertCell(3);

    cell1.innerHTML = '<strong>Cut Level ('+channel_name2+'): <span id="intercomCutLabel_2"><?php echo $intercom_cut_level_2; ?></span> dB</strong>';
    cell2.innerHTML = '<input id="intercom_cut_level_2" name="intercom_cut_level_2" type="range" min="-42" max="0" step="1" value="<?php echo $intercom_cut_level_2; ?>" style="width:200px;" onChange="intercomCutChanged_2(this.value)" oninput="updateIntercomCutLabel_2(this.value);">';
    cell3.innerHTML = '&nbsp;';
    cell4.innerHTML = '&nbsp;';	
		
	} // end nq-nqpaxxx,  nq-ga400p, nq-amp2c, nq-amp4c, nq-amp2c-g2, nq-amp4c-g2
	
	if(device_id=='nq-amp4c' || device_id=='nq-amp4c-g2'){ // start nq-amp4c, nq-amp4c-g2
		
	var row = table.insertRow();
	row.id = 'row_account_id_3';
	var cell1 = row.insertCell(0);
	var cell2 = row.insertCell(1);
	var cell3 = row.insertCell(2);
	var cell4 = row.insertCell(3);
	   
	cell1.innerHTML = '<strong>SIP Extension ('+channel_name3+'):</strong>';
	cell2.innerHTML = '<input type="text" class="form-control" name="account_id_3" placeholder="" style="width:200px;" value="<?php echo $account_id_3; ?>">';
	cell3.innerHTML = '&nbsp;';
	cell4.innerHTML = '&nbsp;';
	
	var row = table.insertRow();
	row.id = 'row_account_username_3';
	var cell1 = row.insertCell(0);
	var cell2 = row.insertCell(1);
	var cell3 = row.insertCell(2);
	var cell4 = row.insertCell(3);
	
	cell1.innerHTML = '<strong>SIP Username ('+channel_name3+'):</strong>';
	cell2.innerHTML = '<input type="text" class="form-control" name="account_username_3" placeholder="" style="width:200px;" value="<?php echo $account_username_3; ?>">';
	cell3.innerHTML = '&nbsp;';
	cell4.innerHTML = '&nbsp;';
	
	var row = table.insertRow();
	row.id = 'row_account_password_3';
	var cell1 = row.insertCell(0);
	var cell2 = row.insertCell(1);
	var cell3 = row.insertCell(2);
	var cell4 = row.insertCell(3);
	
	cell1.innerHTML = '<strong>SIP Password ('+channel_name3+'):</strong>';
	cell2.innerHTML = '<input type="text" class="form-control" name="account_password_3" placeholder="" style="width:200px;" value="<?php echo $account_password_3; ?>">';
	cell3.innerHTML = '&nbsp;';
	cell4.innerHTML = '&nbsp;';	
		
	var row = table.insertRow();
    row.id = 'row_intercom_cut_level_3';
    var cell1 = row.insertCell(0);
    var cell2 = row.insertCell(1);
    var cell3 = row.insertCell(2);
    var cell4 = row.insertCell(3);

    cell1.innerHTML = '<strong>Cut Level ('+channel_name3+'): <span id="intercomCutLabel_3"><?php echo $intercom_cut_level_3; ?></span> dB</strong>';
    cell2.innerHTML = '<input id="intercom_cut_level_3" name="intercom_cut_level_3" type="range" min="-42" max="0" step="1" value="<?php echo $intercom_cut_level_3; ?>" style="width:200px;" onChange="intercomCutChanged_3(this.value)" oninput="updateIntercomCutLabel_3(this.value);">';
    cell3.innerHTML = '&nbsp;';
    cell4.innerHTML = '&nbsp;';	
		
	} // end nq-amp4c. nq-amp4c-g2
	
	if( (device_id=='nq-amp4c' && IsDualChannel2==1) || (device_id=='nq-amp4c-g2' && IsDualChannel2==1)){ // start nq-amp4c and IsDualChannel2, nq-amp4c-g2 and IsDualChannel2
	  
	var row = table.insertRow();
	row.id = 'row_account_id_4';
	var cell1 = row.insertCell(0);
	var cell2 = row.insertCell(1);
	var cell3 = row.insertCell(2);
	var cell4 = row.insertCell(3);
		
	cell1.innerHTML = '<strong>SIP Extension ('+channel_name4+'):</strong>';
	cell2.innerHTML = '<input type="text" class="form-control" name="account_id_4" placeholder="" style="width:200px;" value="<?php echo $account_id_4; ?>">';
	cell3.innerHTML = '&nbsp;';
	cell4.innerHTML = '&nbsp;';
	
	var row = table.insertRow();
	row.id = 'row_account_username_4';
	var cell1 = row.insertCell(0);
	var cell2 = row.insertCell(1);
	var cell3 = row.insertCell(2);
	var cell4 = row.insertCell(3);
	
	cell1.innerHTML = '<strong>SIP Username ('+channel_name4+'):</strong>';
	cell2.innerHTML = '<input type="text" class="form-control" name="account_username_4" placeholder="" style="width:200px;" value="<?php echo $account_username_4; ?>">';
	cell3.innerHTML = '&nbsp;';
	cell4.innerHTML = '&nbsp;';
	
	var row = table.insertRow();
	row.id = 'row_account_password_4';
	var cell1 = row.insertCell(0);
	var cell2 = row.insertCell(1);
	var cell3 = row.insertCell(2);
	var cell4 = row.insertCell(3);
	
	cell1.innerHTML = '<strong>SIP Password ('+channel_name4+'):</strong>';
	cell2.innerHTML = '<input type="text" class="form-control" name="account_password_4" placeholder="" style="width:200px;" value="<?php echo $account_password_4; ?>">';
	cell3.innerHTML = '&nbsp;';
	cell4.innerHTML = '&nbsp;';	
		
	var row = table.insertRow();
    row.id = 'row_intercom_cut_level_4';
    var cell1 = row.insertCell(0);
    var cell2 = row.insertCell(1);
    var cell3 = row.insertCell(2);
    var cell4 = row.insertCell(3);

    cell1.innerHTML = '<strong>Cut Level ('+channel_name4+'): <span id="intercomCutLabel_4"><?php echo $intercom_cut_level_4; ?></span> dB</strong>';
    cell2.innerHTML = '<input id="intercom_cut_level_4" name="intercom_cut_level_4" type="range" min="-42" max="0" step="1" value="<?php echo $intercom_cut_level_4; ?>" style="width:200px;" onChange="intercomCutChanged_4(this.value)" oninput="updateIntercomCutLabel_4(this.value);">';
    cell3.innerHTML = '&nbsp;';
    cell4.innerHTML = '&nbsp;';	
		
	} // end nq-amp4c and IsDualChannel2, nq-amp4c-g2 and IsDualChannel2
	
	if(device_id=='nq-edp01' || device_id=='nq-s810v2' || device_id=='nq-ga10p'){ // nq-edp01, nq-s810v2, ga-10p/v
		
		var row = table.insertRow();
        row.id = 'row_port_type';
        var cell1 = row.insertCell(0);
        var cell2 = row.insertCell(1);
        var cell3 = row.insertCell(2);
        var cell4 = row.insertCell(3);

        cell1.innerHTML = '<strong>Type:</strong>';
        cell2.innerHTML = '<select name="port_type" id="port_type" class="form-control" style="width: 250px"><option value="Speaker-Only" <?php if($port_type == 'Speaker-Only'){ echo 'selected="selected"';}?>>VoIP Speaker Only</option><option value="Digital-Call-Switch-With-Speaker" <?php if($port_type == 'Digital-Call-Switch-With-Speaker'){ echo 'selected="selected"';}?>>Digital Call Switch & Speaker</option></select>';
        cell3.innerHTML = '&nbsp;';
        cell4.innerHTML = '&nbsp;';	
		
		// select2 port_type
        $('#port_type').select2({	
            minimumResultsForSearch: -1
        });
        $("#port_type").on("select2:select", function (e) { 
            var selected = e.params.data.id;
            if(selected=='Speaker-Only'){
                document.getElementById('dial_extension').value = '';
                document.getElementById('dial_extension').readOnly = true;
            } else {
               document.getElementById('dial_extension').readOnly = false;
            } 
        });

		var row = table.insertRow();
        row.id = 'row_dial_extension';
        var cell1 = row.insertCell(0);
        var cell2 = row.insertCell(1);
        var cell3 = row.insertCell(2);
        var cell4 = row.insertCell(3);

        cell1.innerHTML = '<strong>Dial Extension:</strong>';
        cell2.innerHTML = '<td><input type="text" class="form-control" id="dial_extension" name="dial_extension" placeholder="" style="width:200px;" value="<?php echo $dial_extension; ?>"></td>';
        cell3.innerHTML = '&nbsp;';
        cell4.innerHTML = '&nbsp;';	
		
		var port_type = document.getElementById('port_type').value;
		
        if(port_type=='Speaker-Only'){
            document.getElementById('dial_extension').value = '';
            document.getElementById('dial_extension').readOnly = true;
        } else {
           document.getElementById('dial_extension').readOnly = false;  
        }
	   
		var row = table.insertRow();
        row.id = 'intercom_cut';
        var cell1 = row.insertCell(0);
        var cell2 = row.insertCell(1);
        var cell3 = row.insertCell(2);
        var cell4 = row.insertCell(3);

        cell1.innerHTML = '<strong>Intercom Cut Level: <span id="intercomCutLabel"><?php echo $intercom_cut_level; ?></span> dB</strong>';
        cell2.innerHTML = '<input id="intercom_cut_level" name="intercom_cut_level" type="range" min="-42" max="0" step="1" value="<?php echo $intercom_cut_level; ?>" style="width:200px;" onChange="intercomCutChanged(this.value)" oninput="updateIntercomCutLabel(this.value);">';
        cell3.innerHTML = '&nbsp;';
        cell4.innerHTML = '&nbsp;';	
	}
}
function addRow(){
	var next = dataArray.length+1;	
	if(next>24){
	   // send growl notification
        $.bootstrapGrowl("The maximum allowable number of multicast addresses is 24.", {
          ele: 'body', // which element to append to
          type: 'danger', // (null, 'info', 'error', 'success')
          offset: {from: 'top', amount: 50}, // 'top', or 'bottom'
          align: 'right', // ('left', 'right', or 'center')
          width: 350, // (integer, or 'auto')
          delay: 10000,
          allow_dismiss: true,
          stackup_spacing: 10 // spacing between consecutively stacked growls.
        });
	} else {
		var next_port = getNextPort();
        dataArray.push([next,'239.1.1.1',next_port,'G711u','1','-20','',next]);
        var table = document.getElementById('multicasts').getElementsByTagName('tbody')[0];
        var row = table.insertRow();
        row.id = 'row'+next;
        var cell1 = row.insertCell(0);
        var cell2 = row.insertCell(1);
        var cell3 = row.insertCell(2);
        var cell4 = row.insertCell(3);
        var cell5 = row.insertCell(4);
        var cell6 = row.insertCell(5);
        var cell7 = row.insertCell(6);
		var cell8 = row.insertCell(7);

        cell1.innerHTML = '<i class="fa fa-arrows"></i>';
        cell2.innerHTML = '<button class="btn btn-xs btn-danger" type="button" onClick="deleteRow('+next+');"><i class="fa fa-trash"></i></button>';
        cell3.innerHTML = '<a href="#" id="multicast_ip_address'+next+'">239.1.1.1</a>';
        cell4.innerHTML = '<a href="#" id="multicast_port'+next+'">'+next_port+'</a>';
        cell5.innerHTML = '<a href="#" id="multicast_encoding'+next+'">G711 u-law</a>';
        <?php if($_SESSION['device_id']=='nq-mix' || $_SESSION['device_id']=='nq-nqpaxxx'){ ?>
            cell6.innerHTML = '<a href="#" id="multicast_audio_ports'+next+'">Line Out</a>';		
        <?php } ?>
        <?php if($_SESSION['device_id']=='nq-amp2c' || $_SESSION['device_id']=='nq-amp2c-g2' || $_SESSION['device_id']=='nq-amp4c' || $_SESSION['device_id']=='nq-amp4c-g2'){ ?>
            cell6.innerHTML = '<a href="#" id="multicast_audio_ports'+next+'">Channel A</a>';
        <?php } ?>
        <?php if($_SESSION['device_id']=='nq-s810v2'){ ?>
            cell6.innerHTML = '<a href="#" id="multicast_audio_ports'+next+'">Speaker</a>';
        <?php } ?>
        <?php if($_SESSION['device_id']=='nq-ga400p'){ ?>
            cell6.innerHTML = '<a href="#" id="multicast_audio_ports'+next+'">1</a>';
        <?php } ?>
		 <?php if($_SESSION['device_id']=='nq-ga40p3'){ ?>
            cell6.innerHTML = '<a href="#" id="multicast_audio_ports'+next+'">1</a>';
        <?php } ?>
		<?php if($_SESSION['device_id']=='nq-ga20p2'){ ?>
            cell6.innerHTML = '<a href="#" id="multicast_audio_ports'+next+'">1</a>';
        <?php } ?>
		<?php if($_SESSION['device_id']=='nq-ga10p'){ ?>
            cell6.innerHTML = '<a href="#" id="multicast_audio_ports'+next+'">1</a>';
        <?php } ?>
		 <?php if($_SESSION['device_id']=='nq-ga10pv'){ ?>
            cell6.innerHTML = '<a href="#" id="multicast_audio_ports'+next+'">1</a>';
        <?php } ?>
		 <?php if($_SESSION['device_id']=='nq-edp01'){ ?>
            cell6.innerHTML = '<a href="#" id="multicast_audio_ports'+next+'">1</a>';
        <?php } ?>
		<?php if($_SESSION['device_id']=='nq-ser20p2'){ ?>
            cell6.innerHTML = '<a href="#" id="multicast_audio_ports'+next+'">1</a>';
        <?php } ?>
        cell7.innerHTML = '<a href="#" id="multicast_slider'+next+'">-20</a>';
		cell8.innerHTML = '<a href="#" id="multicast_description'+next+'"></a>';

        document.getElementById('dataArray').value = JSON.stringify(dataArray);

            $('#multicast_ip_address'+next).editable({
                type: 'text',
                url: '',
                value: '239.1.1.1',
                title: 'Enter IP Address',
                placeholder: 'IP Address required',
                success: function(response, newValue){
                    // update
                    for(var i=0; i<dataArray.length; i++){  
                        if(dataArray[i][7]==next){
                                dataArray[i][1] = newValue;
                        }
                    }
                    document.getElementById('dataArray').value = JSON.stringify(dataArray);
                },
                validate: function(value){
                    if(value==''){
                       return 'Enter IP Address';
                    } else if (/^239\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$/.test(value)){
                    } else {
                        return 'Valid range is 239.0.0.0 to 239.255.255.255';
                    }
                },
            });

        $('#multicast_port'+next).editable({
                type: 'text',
                url: '',
                value: next_port,
                title: 'Enter Port Number',
                success: function(response, newValue){
                    // update
                    for(var i=0; i<dataArray.length; i++){  
                        if(dataArray[i][7]==next){
                                dataArray[i][2] = parseInt(newValue);
                        }
                    }
                    document.getElementById('dataArray').value = JSON.stringify(dataArray);
                },
                validate: function(value){
                     if($.trim(value) == ''){
                         return 'Multicast Port is required';
                     } else if ($.isNumeric(value) == ''){
                         return 'Only numbers allowed';
                     } else if (value<1026 || value > 65535){
                         return 'Port range is 1026 to 65535';	
                    } else if(value % 2 != 0){ // if not even
                        return 'Port must be an even number';	  
                    } else if(checkForPort(next,value)==1){
                         return 'Multicast Port already used';
                    } else {

                    }
                },
            });
            $('#multicast_encoding'+next).editable({ 
                type: 'select',
                url: '',
                value: 'G711u',
                title: 'Select Codec',
                success: function(response, newValue){
                    // update
                    for(var i=0; i<dataArray.length; i++){  
                        if(dataArray[i][7]==next){
                                dataArray[i][3] = newValue;
                        }
                    }
                    document.getElementById('dataArray').value = JSON.stringify(dataArray);
                },
                source: [
                      {value: 'G711u', text: 'G711 u-law'},
                      {value: 'G711a', text: 'G711 a-law'},
                      {value: 'G722', text: 'G722'},
					<?php if($_SESSION['device_id']!='nq-edp01'){ // remove for nq-edp01 ?>
                      {value: 'OPUS', text: 'OPUS'}
					<?php } ?>
                   ],
            });
            $('#multicast_audio_ports'+next).editable({
                type: 'checklist',
                url: '',
                source: 'array',
                value: [1],
                title: 'Select Channels',
                success: function(response, newValue){
                    // update
                    for(var i=0; i<dataArray.length; i++){  
                        if(dataArray[i][7]==next){
                                dataArray[i][4] = newValue.toString();
                        }
                    }
                    document.getElementById('dataArray').value = JSON.stringify(dataArray);
                },
                validate: function(newValue){
                     if($.trim(newValue) == ''){
                         return 'Channel is required';
                    }
                },
                source: [
                     <?php if($_SESSION['device_id']=='nq-mix'){ ?>
                        {value: 1, text: 'Line Out'},
                     <?php } ?>
                     <?php if($_SESSION['device_id']=='nq-s810v2'){ ?>
                        {value: 1, text: 'Speaker'},
                     <?php } ?>
                    <?php if($_SESSION['device_id']=='nq-nqpaxxx'){ ?>
                        {value: 1, text: 'Line Out'},
                        {value: 2, text: 'Speaker Out'},
                     <?php } ?>
                     <?php if($_SESSION['device_id']=='nq-amp2c' || $_SESSION['device_id']=='nq-amp2c-g2'){ ?>
                        {value: 1, text: 'Channel A'},
                        <?php if($is_dual_channel==1){ ?>
                            {value: 2, text: 'Channel B'},
                        <?php } ?>
                    <?php } ?>
                    <?php if($_SESSION['device_id']=='nq-amp4c' || $_SESSION['device_id']=='nq-amp4c-g2'){ ?>
                        {value: 1, text: 'Channel A'},
                        <?php if($is_dual_channel==1){ ?>
                            {value: 2, text: 'Channel B'},
                        <?php } ?>
                        {value: 3, text: 'Channel C'},
                        <?php if($is_dual_channel2==1){ ?>
                            {value: 4, text: 'Channel D'},
                        <?php } ?>
                    <?php } ?>
					<?php if($_SESSION['device_id']=='nq-ga400p'){ ?>
                        {value: 1, text: '1'},
                        {value: 2, text: '2'},
                     <?php } ?>
					<?php if($_SESSION['device_id']=='nq-ga40p3'){ ?>
                        {value: 1, text: '1'},
                     <?php } ?>
					<?php if($_SESSION['device_id']=='nq-ga20p2'){ ?>
                        {value: 1, text: '1'},
                     <?php } ?>
					<?php if($_SESSION['device_id']=='nq-ga10p'){ ?>
                        {value: 1, text: '1'},
                     <?php } ?>
					<?php if($_SESSION['device_id']=='nq-ga10pv'){ ?>
                        {value: 1, text: '1'},
                     <?php } ?>
					<?php if($_SESSION['device_id']=='nq-ser20p2'){ ?>
                        {value: 1, text: '1'},
                     <?php } ?>
               ]
            });
            $('#multicast_slider'+next).editable({
                type: 'range',
                url: '',
                value: -20,
                title: 'Select Cut Level (dB)',
                min: -70,
                max: 0,
                step: 1,
                tpl: '<input type="range" id="multicast_slider'+next+'" name="multicast_slider'+next+'" min="-70" max="0" step="1" value="-20" oninput="updateSliderLabel(\'slider_label'+next+'\',this.value)"><span id="slider_label'+next+'">-20</span>',
                success: function(response, newValue){
                    // update
                    for(var i=0; i<dataArray.length; i++){  
                        if(dataArray[i][7]==next){
                                dataArray[i][5] = newValue.toString();
                        }
                    }
                    document.getElementById('dataArray').value = JSON.stringify(dataArray);
                },
            });
			$('#multicast_description'+next).editable({
                type: 'text',
                url: '',
                value: '',
                title: 'Enter Description',
				tpl: '<input type="text" id="multicast_description'+next+'" name="multicast_description'+next+'" value="" maxlength="30">',
                success: function(response, newValue){
                    // update
                    for(var i=0; i<dataArray.length; i++){  
                        if(dataArray[i][7]==next){
                            dataArray[i][6] = newValue.toString();
                        }
                    }
                    document.getElementById('dataArray').value = JSON.stringify(dataArray);
                },
				validate: function(value){
                    if(value.length>30){
                       return 'Max length is 30.';
                    } 
                },
			});

            document.getElementById("multicast_port"+next).addEventListener('click', function () {
                if(document.getElementById("sorting_active").value==1){
                    $('#sorting_mode').bootstrapToggle('toggle');
                 }
            });
            document.getElementById("multicast_ip_address"+next).addEventListener('click', function () {
                if(document.getElementById("sorting_active").value==1){
                    $('#sorting_mode').bootstrapToggle('toggle');
                 }
            });
            document.getElementById("multicast_encoding"+next).addEventListener('click', function () {
                if(document.getElementById("sorting_active").value==1){
                    $('#sorting_mode').bootstrapToggle('toggle');
                 }
            });
            document.getElementById("multicast_audio_ports"+next).addEventListener('click', function () {
                if(document.getElementById("sorting_active").value==1){
                    $('#sorting_mode').bootstrapToggle('toggle');
                 }
            });
            document.getElementById("multicast_slider"+next).addEventListener('click', function (evt) {
                //console.log(evt.srcElement.innerHTML);
                document.getElementById("slider_label"+next).innerHTML = evt.srcElement.innerHTML;
                if(document.getElementById("sorting_active").value==1){
                    $('#sorting_mode').bootstrapToggle('toggle');
                 }
            });
			document.getElementById("multicast_description"+next).addEventListener('click', function () {
                if(document.getElementById("sorting_active").value==1){
                    $('#sorting_mode').bootstrapToggle('toggle');
                 }
            });
	}	
}
function deleteRow(id){
   $("#row" + id).remove();
	// remove dataArray data
	for(var row=0; row<dataArray.length; row++){
		if(id==dataArray[row][7]){
			dataArray.splice(row,1);
		}
	}
	// update the order numbers in the array
    for (i=0;i<dataArray.length;i++) {
        dataArray[i][0] = (i+1).toString();
    }
	document.getElementById('dataArray').value = JSON.stringify(dataArray);
}
// add x-editable functions
<?php foreach($multicasts as $multicast){ ?>
	<?php $order = trim($multicast['order']); ?> 
	<?php $multicast_ip_address = trim($multicast['multicast_ip_address']); ?> 
	<?php $multicast_port = trim($multicast['multicast_port']); ?> 
	<?php $multicast_encoding = trim($multicast['multicast_encoding']); ?> 
	<?php $multicast_audio_ports = trim($multicast['multicast_audio_ports']); 
		  $multicast_audio_ports_string = '['.$multicast_audio_ports .']';
	?>
	<?php $multicast_slider = trim($multicast['multicast_slider']); ?> 
	<?php $multicast_description = trim($multicast['multicast_description']); ?> 
	var multicast_audio_ports_array =  '<?php echo $multicast_audio_ports_string; ?>';
	
	$('#multicast_ip_address<?php echo $order;?>').editable({
			type: 'text',
			url: '',
			value: '<?php echo $multicast_ip_address;?>',
			title: 'Enter IP Address',
			placeholder: 'IP Address required',
			success: function(response, newValue){
				// update
				for(var i=0; i<dataArray.length; i++){  
                    if(dataArray[i][7]=='<?php echo $order;?>'){
                        	dataArray[i][1] = newValue;
                    }
                }	   
				document.getElementById('dataArray').value = JSON.stringify(dataArray);
			},
			validate: function(value){
				if(value==''){
				   return 'Enter IP Address';
				} else if (/^239\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$/.test(value)){
				} else {
                  return 'Valid range is 239.0.0.0 to 239.255.255.255';
                }
			},
		});
	
	$('#multicast_port<?php echo $order;?>').editable({
			type: 'text',
			url: '',
			value: '<?php echo $multicast_port;?>',
			title: 'Enter Port Number',
			success: function(response, newValue){
				// update
				for(var i=0; i<dataArray.length; i++){  
                    if(dataArray[i][7]=='<?php echo $order;?>'){
                        	dataArray[i][2] = newValue;
                    }
                }
				document.getElementById('dataArray').value = JSON.stringify(dataArray);
			},
			validate: function(value){
               if($.trim(value) == ''){
                   return 'Multicast Port is required';
               } else if ($.isNumeric(value) == ''){
                   return 'Only numbers allowed';
               } else if (value<1026 || value > 65535){
                   return 'Port range is 1026 to 65535';	
               } else if(value % 2 != 0){ // if not even
                  return 'Port must be an even number';	  
               } else if(checkForPort(<?php echo $order;?>,parseInt(value))==1){
                   return 'Multicast Port already used';
               } else {
				    
               }
			},
		});
		$('#multicast_encoding<?php echo $order;?>').editable({ 
			type: 'select',
			url: '',
			value: '<?php echo $multicast_encoding;?>',
			title: 'Select Codec',
			success: function(response, newValue){		
				// update
				for(var i=0; i<dataArray.length; i++){  
                    if(dataArray[i][7]=='<?php echo $order;?>'){
                        	dataArray[i][3] = newValue;
                    }
                }
				document.getElementById('dataArray').value = JSON.stringify(dataArray);
			},
			source: [
				  {value: 'G711u', text: 'G711 u-law'},
				  {value: 'G711a', text: 'G711 a-law'},
				  {value: 'G722', text: 'G722'},
				<?php if($_SESSION['device_id']!='nq-edp01'){ // remove for nq-edp01 ?>
				  {value: 'OPUS', text: 'OPUS'}
				<?php } ?>
			   ],
		});
		$('#multicast_audio_ports<?php echo $order;?>').editable({
			type: 'checklist',
			url: '',
			source: 'array',
			value: multicast_audio_ports_array,
			title: 'Select Channels',
			success: function(response, newValue){
				// update
				for(var i=0; i<dataArray.length; i++){  
                    if(dataArray[i][7]=='<?php echo $order;?>'){
                        	dataArray[i][4] = newValue.toString();
                    }
                }
				document.getElementById('dataArray').value = JSON.stringify(dataArray);
			},
			validate: function(newValue){
                 if($.trim(newValue) == ''){
                     return 'Channel is required';
                }
			},
        	source: [
				 <?php if($_SESSION['device_id']=='nq-mix'){ ?>
					{value: 1, text: 'Line Out'},
				 <?php } ?>
				<?php if($_SESSION['device_id']=='nq-s810v2'){ ?>
					{value: 1, text: 'Speaker'},
				 <?php } ?>
				 <?php if($_SESSION['device_id']=='nq-nqpaxxx'){ ?>
					{value: 1, text: 'Line Out'},
					{value: 2, text: 'Speaker Out'},
				 <?php } ?>
              	 <?php if($_SESSION['device_id']=='nq-amp2c' || $_SESSION['device_id']=='nq-amp2c-g2'){ ?>
					{value: 1, text: 'Channel A'},
					<?php if($is_dual_channel==1){ ?>
						{value: 2, text: 'Channel B'},
					<?php } ?>
			  	<?php } ?>
			  	<?php if($_SESSION['device_id']=='nq-amp4c' || $_SESSION['device_id']=='nq-amp4c-g2'){ ?>
					{value: 1, text: 'Channel A'},
					<?php if($is_dual_channel==1){ ?>
						{value: 2, text: 'Channel B'},
					<?php } ?>
					{value: 3, text: 'Channel C'},
					<?php if($is_dual_channel2==1){ ?>
             	 		{value: 4, text: 'Channel D'},
					<?php } ?>
			  	<?php } ?>
				 <?php if($_SESSION['device_id']=='nq-ga400p'){ ?>
					{value: 1, text: '1'},
					{value: 2, text: '2'},
				 <?php } ?>
				 <?php if($_SESSION['device_id']=='nq-ga40p3'){ ?>
					{value: 1, text: '1'},
				 <?php } ?>
				 <?php if($_SESSION['device_id']=='nq-ga20p2'){ ?>
					{value: 1, text: '1'},
				 <?php } ?>
				 <?php if($_SESSION['device_id']=='nq-ga10p'){ ?>
                    {value: 1, text: '1'},
                 <?php } ?>
				 <?php if($_SESSION['device_id']=='nq-ga10pv'){ ?>
                    {value: 1, text: '1'},
                 <?php } ?>
				<?php if($_SESSION['device_id']=='nq-ser20p2'){ ?>
					{value: 1, text: '1'},
				 <?php } ?>
           ]
		});
		$('#multicast_slider<?php echo $order;?>').editable({
          	type: 'range',
			url: '',
			value: '<?php echo $multicast_slider;?>',
			title: 'Select Cut Level (dB)',
			min: -70,
			max: 0,
			step: 1,
			tpl: '<input type="range" id="multicast_slider<?php echo $order;?>" name="multicast_slider<?php echo $order;?>" min="-70" max="0" step="1" value="<?php echo $multicast_slider;?>" oninput="updateSliderLabel(\'slider_label<?php echo $order;?>\',this.value)"><span id="slider_label<?php echo $order;?>"><?php echo $multicast_slider;?></span>',
			success: function(response, newValue){
				// update
				for(var i=0; i<dataArray.length; i++){  
                    if(dataArray[i][7]=='<?php echo $order;?>'){
                        	dataArray[i][5] = newValue.toString();
                    }
                }
				document.getElementById('dataArray').value = JSON.stringify(dataArray);
			},
		});
		$('#multicast_description<?php echo $order;?>').editable({
			type: 'text',
			url: '',
			value: '<?php echo $multicast_description;?>',
			title: 'Enter Description',
			tpl: '<input type="text" id="multicast_description<?php echo $order;?>" name="multicast_description<?php echo $order;?>" value="<?php echo $multicast_description;?>" maxlength="30">',
			success: function(response, newValue){
				// update
				for(var i=0; i<dataArray.length; i++){  
                    if(dataArray[i][7]=='<?php echo $order;?>'){
                        dataArray[i][6] = newValue.toString();
                    }
                }
				document.getElementById('dataArray').value = JSON.stringify(dataArray);
			},
		});
	
		document.getElementById("multicast_port<?php echo $order;?>").addEventListener('click', function () {
			if(document.getElementById("sorting_active").value==1){
				$('#sorting_mode').bootstrapToggle('toggle');
			 }
        });
		document.getElementById("multicast_ip_address<?php echo $order;?>").addEventListener('click', function () {
			if(document.getElementById("sorting_active").value==1){
				$('#sorting_mode').bootstrapToggle('toggle');
			 }
        });
		document.getElementById("multicast_encoding<?php echo $order;?>").addEventListener('click', function () {
			if(document.getElementById("sorting_active").value==1){
				$('#sorting_mode').bootstrapToggle('toggle');
			 }
        });
		document.getElementById("multicast_audio_ports<?php echo $order;?>").addEventListener('click', function () {
			if(document.getElementById("sorting_active").value==1){
				$('#sorting_mode').bootstrapToggle('toggle');
			 }
        });
        document.getElementById("multicast_slider<?php echo $order;?>").addEventListener('click', function (evt) {
			//console.log(evt.srcElement.innerHTML);
			document.getElementById("slider_label<?php echo $order;?>").innerHTML = evt.srcElement.innerHTML;
			if(document.getElementById("sorting_active").value==1){
				$('#sorting_mode').bootstrapToggle('toggle');
			 }
        });
		document.getElementById("multicast_description<?php echo $order;?>").addEventListener('click', function () {
			if(document.getElementById("sorting_active").value==1){
				$('#sorting_mode').bootstrapToggle('toggle');
			 }
        });
<?php } ?>
	
// Start GA400P Audio Cast
function addRow2(){
	var next = dataArray2.length+1;
	if(next>24){
	   // send growl notification
        $.bootstrapGrowl("The maximum allowable number of multicast addresses is 24.", {
          ele: 'body', // which element to append to
          type: 'danger', // (null, 'info', 'error', 'success')
          offset: {from: 'top', amount: 50}, // 'top', or 'bottom'
          align: 'right', // ('left', 'right', or 'center')
          width: 350, // (integer, or 'auto')
          delay: 10000,
          allow_dismiss: true,
          stackup_spacing: 10 // spacing between consecutively stacked growls.
        });
	} else {
		var next_port = getNextPort2();
        dataArray2.push([next,'239.1.1.1',next_port,'OPUS','1','-20',next]);
        var table = document.getElementById('audio_casts').getElementsByTagName('tbody')[0];
        var row = table.insertRow();
        row.id = 'row'+next;
        var cell1 = row.insertCell(0);
        var cell2 = row.insertCell(1);
        var cell3 = row.insertCell(2);
        var cell4 = row.insertCell(3);
        var cell5 = row.insertCell(4);
        var cell6 = row.insertCell(5);
        var cell7 = row.insertCell(6);

        cell1.innerHTML = '<i class="fa fa-arrows"></i>';
        cell2.innerHTML = '<button class="btn btn-xs btn-danger" type="button" onClick="deleteRow2('+next+');"><i class="fa fa-trash"></i></button>';
        cell3.innerHTML = '<a href="#" id="audio_cast_ip_address'+next+'">239.1.1.1</a>';
        cell4.innerHTML = '<a href="#" id="audio_cast_port'+next+'">'+next_port+'</a>';
        cell5.innerHTML = '<a href="#" id="audio_cast_encoding'+next+'">OPUS</a>';
        cell6.innerHTML = '<a href="#" id="audio_cast_audio_ports'+next+'">1</a>';
        cell7.innerHTML = '<a href="#" id="audio_cast_description'+next+'">-20</a>';

        document.getElementById('dataArray2').value = JSON.stringify(dataArray2);

            $('#audio_cast_ip_address'+next).editable({
                type: 'text',
                url: '',
                value: '239.1.1.1',
                title: 'Enter IP Address',
                placeholder: 'IP Address required',
                success: function(response, newValue){
                    // update
                    for(var i=0; i<dataArray2.length; i++){  
                        if(dataArray2[i][6]==next){
                                dataArray2[i][1] = newValue;
                        }
                    }
                    document.getElementById('dataArray2').value = JSON.stringify(dataArray2);
                },
                validate: function(value){
                    if(value==''){
                       return 'Enter IP Address';
                    } else if (/^239\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$/.test(value)){
                    } else {
                        return 'Valid range is 239.0.0.0 to 239.255.255.255';
                    }
                },
            });

        $('#audio_cast_port'+next).editable({
                type: 'text',
                url: '',
                value: next_port,
                title: 'Enter Port Number',
                success: function(response, newValue){
                    // update
                    for(var i=0; i<dataArray2.length; i++){  
                        if(dataArray2[i][6]==next){
                                dataArray2[i][2] = parseInt(newValue);
                        }
                    }
                    document.getElementById('dataArray2').value = JSON.stringify(dataArray2);
                },
                validate: function(value){
                     if($.trim(value) == ''){
                         return 'Multicast Port is required';
                     } else if ($.isNumeric(value) == ''){
                         return 'Only numbers allowed';
                     } else if (value<1026 || value > 65535){
                         return 'Port range is 1026 to 65535';	
                    } else if(value % 2 != 0){ // if not even
                        return 'Port must be an even number';	  
                    } else if(checkForPort2(next,value)==1){
                         return 'Multicast Port already used';
                    } else {

                    }
                },
            });
            $('#audio_cast_encoding'+next).editable({ 
                type: 'select',
                url: '',
                value: 'OPUS',
                title: 'Select Codec',
				validate: function(value){
					if($.trim(value) == ''){
						return 'Codec is required';
               		}
				},
                success: function(response, newValue){
                    // update
                    for(var i=0; i<dataArray2.length; i++){  
                        if(dataArray2[i][6]==next){
                                dataArray2[i][3] = newValue;
                        }
                    }
                    document.getElementById('dataArray2').value = JSON.stringify(dataArray2);
                },
                source: [
				  	{value: 'G722', text: 'G722'},
				  	{value: 'OPUS', text: 'OPUS'}
                   ],
            });
            $('#audio_cast_audio_ports'+next).editable({
				type: 'select',
                url: '',
                source: 'array',
                value: [1],
                title: 'Select Channels',
                success: function(response, newValue){
                    // update
                    for(var i=0; i<dataArray2.length; i++){  
                        if(dataArray2[i][6]==next){
                                dataArray2[i][4] = newValue.toString();
                        }
                    }
                    document.getElementById('dataArray2').value = JSON.stringify(dataArray2);
                },
                validate: function(newValue){
                     if($.trim(newValue) == ''){
                         return 'Channel is required';
                    }
                },
                source: [
                        {value: 1, text: '1'},
                        {value: 2, text: '2'},
               ]
            });
			$('#audio_cast_description'+next).editable({
                type: 'text',
                url: '',
                value: '',
                title: 'Enter Description',
				tpl: '<input type="text" id="audio_cast_description'+next+'" name="audio_cast_description'+next+'" value="" maxlength="30">',
                success: function(response, newValue){
                    // update
                    for(var i=0; i<dataArray2.length; i++){  
                        if(dataArray2[i][6]==next){
                            dataArray2[i][5] = newValue.toString();
                        }
                    }
                    document.getElementById('dataArray2').value = JSON.stringify(dataArray2);
                },
			});
	}	
}
function deleteRow2(id){
   $("#row" + id).remove();
	// remove dataArray data
	for(var row=0; row<dataArray2.length; row++){
		if(id==dataArray2[row][6]){
			dataArray2.splice(row,1);
		}
	}
	// update the order numbers in the array
    for (i=0;i<dataArray2.length;i++) {
        dataArray2[i][0] = (i+1).toString();
    }
	document.getElementById('dataArray2').value = JSON.stringify(dataArray2);
}
<?php foreach($audio_casts as $audio_cast){ ?>
	<?php $order = trim($audio_cast['order']); ?> 
	<?php $audio_cast_ip_address = trim($audio_cast['audio_cast_ip_address']); ?> 
	<?php $audio_cast_port = trim($audio_cast['audio_cast_port']); ?> 
	<?php $audio_cast_encoding = trim($audio_cast['audio_cast_encoding']); ?> 
	<?php $audio_cast_audio_ports = trim($audio_cast['audio_cast_audio_ports']); 
		  $audio_cast_audio_ports_string = '['.$audio_cast_audio_ports .']';
	?>
	<?php $audio_cast_description = trim($audio_cast['audio_cast_description']); ?> 
	<?php $audio_cast_enable = trim($audio_cast['audio_cast_enable']); ?>
	var audio_cast_audio_ports_array =  '<?php echo $audio_cast_audio_ports_string; ?>';
	
	$('#audio_cast_ip_address<?php echo $order;?>').editable({
			type: 'text',
			url: '',
			value: '<?php echo $audio_cast_ip_address;?>',
			title: 'Enter IP Address',
			placeholder: 'IP Address required',
			success: function(response, newValue){
				// update
				for(var i=0; i<dataArray2.length; i++){  
                    if(dataArray2[i][7]=='<?php echo $order;?>'){
                        	dataArray2[i][1] = newValue;
                    }
                }	   
				document.getElementById('dataArray2').value = JSON.stringify(dataArray2);
			},
			validate: function(value){
				if(value==''){
				   return 'Enter IP Address';
				} else if (/^239\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$/.test(value)){
                } else {
                   return 'Valid range is 239.0.0.0 to 239.255.255.255';
                }
			},
		});
	
	$('#audio_cast_port<?php echo $order;?>').editable({
			type: 'text',
			url: '',
			value: '<?php echo $audio_cast_port;?>',
			title: 'Enter Port Number',
			success: function(response, newValue){
				// update
				for(var i=0; i<dataArray2.length; i++){  
                    if(dataArray2[i][7]=='<?php echo $order;?>'){
                        	dataArray2[i][2] = newValue;
                    }
                }
				document.getElementById('dataArray2').value = JSON.stringify(dataArray2);
			},
			validate: function(value){
               if($.trim(value) == ''){
                   return 'Multicast Port is required';
               } else if ($.isNumeric(value) == ''){
                   return 'Only numbers allowed';
               } else if (value<1026 || value > 65535){
                   return 'Port range is 1026 to 65535';	
               } else if(value % 2 != 0){ // if not even
                  return 'Port must be an even number';	  
               } else if(checkForPort2(<?php echo $order;?>,parseInt(value))==1){
                   return 'Multicast Port already used';
               } else {
				    
               }
			},
		});
		$('#audio_cast_encoding<?php echo $order;?>').editable({ 
			type: 'select',
			url: '',
			value: '<?php if($audio_cast_encoding==''){echo 'OPUS'; } else { echo $audio_cast_encoding; } ?>',
			title: 'Select Codec',
			validate: function(value){
				if($.trim(value) == ''){
                   return 'Codec is required';
               }
			},
			success: function(response, newValue){		
				// update
				for(var i=0; i<dataArray2.length; i++){  
                    if(dataArray2[i][7]=='<?php echo $order;?>'){
                        	dataArray2[i][3] = newValue;
                    }
                }
				document.getElementById('dataArray2').value = JSON.stringify(dataArray2);
			},
			source: [
				  {value: 'G722', text: 'G722'},
				  {value: 'OPUS', text: 'OPUS'}
			   ],
		});
		$('#audio_cast_audio_ports<?php echo $order;?>').editable({
			type: 'select',
			url: '',
			source: 'array',
			value: <?php echo $order;?>,
			title: 'Select Channels',
			success: function(response, newValue){
				// update
				for(var i=0; i<dataArray2.length; i++){  
                    if(dataArray2[i][7]=='<?php echo $order;?>'){
                        	dataArray2[i][4] = newValue.toString();
                    }
                }
				document.getElementById('dataArray2').value = JSON.stringify(dataArray2);
			},
			validate: function(newValue){
                 if($.trim(newValue) == ''){
                     return 'Channel is required';
                }
			},
        	source: [
				<?php if($order==1){?>
					{value: 1, text: '1'},
				<?php } ?>
				<?php if($order==2){?>
					{value: 2, text: '2'},
				<?php } ?>
           ]
		});
		$('#audio_cast_description<?php echo $order;?>').editable({
			type: 'text',
			url: '',
			value: '<?php echo $audio_cast_description;?>',
			title: 'Enter Description',
			tpl: '<input type="text" id="audio_cast_description<?php echo $order;?>" name="audio_cast_description<?php echo $order;?>" value="<?php echo $audio_cast_description;?>" maxlength="30">',
			success: function(response, newValue){
				// update
				for(var i=0; i<dataArray2.length; i++){  
                    if(dataArray2[i][7]=='<?php echo $order;?>'){
                        dataArray2[i][5] = newValue.toString();
                    }
                }
				document.getElementById('dataArray2').value = JSON.stringify(dataArray2);
			},
		});
		$('#audio_cast_enable<?php echo $order;?>').editable({ 
			type: 'select',
			url: '',
			value: '<?php echo $audio_cast_enable; ?>',
			title: 'Select Enabled',
			validate: function(value){
				if($.trim(value) == ''){
                   return 'Enabled is required';
               }
			},
			success: function(response, newValue){		
				// update
				for(var i=0; i<dataArray2.length; i++){  
                    if(dataArray2[i][7]=='<?php echo $order;?>'){
                       dataArray2[i][6] = newValue;
                    }
                }
				document.getElementById('dataArray2').value = JSON.stringify(dataArray2);
			},
			source: [
				  {value: 'Yes', text: 'Yes'},
				  {value: 'No', text: 'No'},
			   ],
		});
<?php } ?>	
// End GA400P Audio Cast
	
// Start GA400P Unicast Transmit
$('#unicast_stream_enable').editable({ 
    type: 'select',
    url: '',
    value: '<?php echo $unicast_stream_enable; ?>',
    title: 'Select Enabled',
    validate: function(value){
        if($.trim(value) == ''){
           return 'Enabled is required';
       }
    },
    success: function(response, newValue){		
        // update
    	document.getElementById('unicast_stream_enable_data').value = newValue;
    },
    source: [
          {value: 'Yes', text: 'Yes'},
          {value: 'No', text: 'No'},
       ],
});

$('#unicast_stream_address').editable({
    type: 'text',
    url: '',
    value: '<?php echo $unicast_stream_address;?>',
    title: 'Enter IP Address',
    placeholder: 'IP Address required',
    success: function(response, newValue){
        // update
        document.getElementById('unicast_stream_address_data').value = newValue;
    },
    validate: function(value){
        if(value==''){
           return 'Enter IP Address';
        } else if (/^(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$/.test(value)){
        } else {
           return 'Valid IP Address is required.';
        }
    },
});

$('#unicast_stream_port').editable({
    type: 'text',
    url: '',
    value: '<?php echo $unicast_stream_port;?>',
    title: 'Enter Port Number',
    success: function(response, newValue){
        // update
        document.getElementById('unicast_stream_port_data').value = newValue;
    },
    validate: function(value){
       if($.trim(value) == ''){
           return 'Unicast Port is required';
       } else if ($.isNumeric(value) == ''){
           return 'Only numbers allowed';
       } else if (value<1026 || value > 65535){
           return 'Port range is 1026 to 65535';	
       } else if(value % 2 != 0){ // if not even
          return 'Port must be an even number';
       } else {

       }
    },
});
$('#unicast_stream_codec').editable({ 
    type: 'select',
    url: '',
    value: '<?php if($audio_cast_encoding==''){echo 'OPUS'; } else { echo $unicast_stream_codec; } ?>',
    title: 'Select Codec',
    validate: function(value){
        if($.trim(value) == ''){
           return 'Codec is required';
       }
    },
    success: function(response, newValue){		
        // update
        document.getElementById('unicast_stream_codec_data').value = newValue;
    },
    source: [
          {value: 'G722', text: 'G722'},
          {value: 'OPUS', text: 'OPUS'}
       ],
});
$('#unicast_stream_source').editable({
    type: 'checklist',
    url: '',
    source: 'array',
    value: '<?php echo $unicast_stream_source;?>',
    title: 'Select Channels',
    success: function(response, newValue){
        // update
         document.getElementById('unicast_stream_source_data').value = newValue;
    },
    validate: function(newValue){
        if($.trim(newValue) == ''){
			return 'Channel is required';
        }
		if($.trim(newValue) == '1,2'){
			return 'Choose Channel 1 or 2, not both';
        }
    },
    source: [
            {value: 1, text: '1'},
			{value: 2, text: '2'},
   ]
});
$('#unicast_stream_description').editable({
    type: 'text',
    url: '',
    value: '<?php echo $unicast_stream_description;?>',
    title: 'Enter Description',
    tpl: '<input type="text" id="unicast_stream_description" name="unicast_stream_description" value="" maxlength="30">',
    success: function(response, newValue){
        // update
        document.getElementById('unicast_stream_description_data').value = newValue;
    },
    validate: function(value){
        if(value.length>30){
           return 'Max length is 30.';
        } 
    },
});
// End GA400P Unicast Transmit
	
// Start GA400P Unicast Receive
$('#unicast_receive_enable').editable({ 
    type: 'select',
    url: '',
    value: '<?php echo $unicast_receive_enable; ?>',
    title: 'Select Enabled',
    validate: function(value){
        if($.trim(value) == ''){
           return 'Enabled is required';
       }
    },
    success: function(response, newValue){		
        // update
    	document.getElementById('unicast_receive_enable_data').value = newValue;
    },
    source: [
          {value: 'Yes', text: 'Yes'},
          {value: 'No', text: 'No'},
       ],
});

$('#unicast_receive_port').editable({
    type: 'text',
    url: '',
    value: '<?php echo $unicast_receive_port;?>',
    title: 'Enter Port Number',
    success: function(response, newValue){
        // update
        document.getElementById('unicast_receive_port_data').value = newValue;
    },
    validate: function(value){
       if($.trim(value) == ''){
           return 'Unicast Port is required';
       } else if ($.isNumeric(value) == ''){
           return 'Only numbers allowed';
       } else if (value<1026 || value > 65535){
           return 'Port range is 1026 to 65535';	
       } else if(value % 2 != 0){ // if not even
          return 'Port must be an even number';
       } else {

       }
    },
});
$('#unicast_receive_codec').editable({ 
    type: 'select',
    url: '',
    value: '<?php if($audio_cast_encoding==''){echo 'OPUS'; } else { echo $unicast_receive_codec; } ?>',
    title: 'Select Codec',
    validate: function(value){
        if($.trim(value) == ''){
           return 'Codec is required';
       }
    },
    success: function(response, newValue){		
        // update
        document.getElementById('unicast_receive_codec_data').value = newValue;
    },
    source: [
          {value: 'G722', text: 'G722'},
          {value: 'OPUS', text: 'OPUS'}
       ],
});
$('#unicast_receive_line_output').editable({
    type: 'checklist',
    url: '',
    source: 'array',
    value: '<?php echo $unicast_receive_line_output;?>', // [1]
    title: 'Select Channels',
    success: function(response, newValue){
        // update
         document.getElementById('unicast_receive_line_output_data').value = newValue;
    },
    validate: function(newValue){
         if($.trim(newValue) == ''){
             return 'Channel is required';
        }
    },
    source: [
            {value: 1, text: '1'},
			{value: 2, text: '2'},
   ]
});
$('#unicast_receive_cut').editable({
    type: 'range',
    url: '',
    value: '<?php echo $unicast_receive_cut;?>',
    title: 'Select Cut Level (dB)',
    min: -42,
    max: 0,
    step: 1,
    tpl: '<input type="range" id="unicast_receive_cut" name="unicast_receive_cut" min="-42" max="0" step="1" value="<?php echo $unicast_receive_cut;?>" oninput="updateSliderLabel3(\'slider_label\',this.value)"><span id="slider_label"><?php echo $unicast_receive_cut;?></span>',
    success: function(response, newValue){
        // update
       document.getElementById('unicast_receive_cut_data').value = newValue;
    },
});
$('#unicast_receive_description').editable({
    type: 'text',
    url: '',
    value: '<?php echo $unicast_receive_description;?>',
    title: 'Enter Description',
    tpl: '<input type="text" id="unicast_receive_description" name="unicast_receive_description" value="" maxlength="30">',
    success: function(response, newValue){
        // update
        document.getElementById('unicast_receive_description_data').value = newValue;
    },
    validate: function(value){
        if(value.length>30){
           return 'Max length is 30.';
        } 
    },
});
// End GA400P Unicast Receive


function updateSliderLabel(id,value){
	//console.log(id,value);
	document.getElementById(id).innerHTML = value;
}
function updateSliderLabel2(id,value){
	//console.log(id,value);
	document.getElementById(id).innerHTML = value;
}
function updateSliderLabel3(id,value){
	//console.log(id,value);
	document.getElementById(id).innerHTML = value;
}
// checkForPort
function checkForPort(id,port){
  var status = 0;
  for(var i=0; i<dataArray.length; i++){  
	  if(dataArray[i][2]==parseInt(port)){
          if(dataArray[i][5]!=id){
             status = 1;
		  }
	  }
  }
  return status;
}
// checkForPort2
function checkForPort2(id,port){
  var status = 0;
  for(var i=0; i<dataArray2.length; i++){  
	  if(dataArray2[i][2]==parseInt(port)){
          if(dataArray2[i][5]!=id){
             status = 1;
		  }
	  }
  }
  return status;
}
// getNextPort
function getNextPort(){
  var port = 5998;
  for(var i=0; i<dataArray.length; i++){
	  if(dataArray[i][2]>port){
          port = dataArray[i][2];
	  }
  }
  return parseInt(port)+2;
}
// getNextPort2
function getNextPort2(){
  var port = 5998;
  for(var i=0; i<dataArray2.length; i++){
	  if(dataArray2[i][2]>port){
          port = dataArray2[i][2];
	  }
  }
  return parseInt(port)+2;
}
	
// sortable	
	var sortable;
	/*
var sortable = Sortable.create(list_items, {
    onUpdate: function (evt) {
		// move the changed row in the array
		var new_index = evt.newIndex;
        var old_index = evt.oldIndex;
        var element = dataArray[old_index];
		dataArray.splice(old_index, 1);
    	dataArray.splice(new_index, 0, element);
		
		// update the order numbers in the array
		for (var i=0;i<dataArray.length;i++) {
			dataArray[i][0] = (i+1).toString();
		}
		document.getElementById('dataArray').value = JSON.stringify(dataArray);
    },
});
*/
	
// web socket
function init_socket() {
	var host = "wss://" + location.hostname + '/ws';
	
	try {
		socket = new WebSocket(host);
		//console.log('WebSocket - status ' + socket.readyState);
		socket.onopen    = function(msg) { 
			
			//console.log("Welcome - status "+this.readyState); 
			start_amp_status();
		};	
		
		socket.onmessage = function(msg) {
			
			var obj = JSON.parse(msg.data);
			var obj2;
			var json_type = document.getElementById('json_type').value;
			var IsDualChannel = document.getElementById('IsDualChannel').value;
			var IsDualChannel2 = document.getElementById('IsDualChannel2').value;
			
			if(json_type=='mixer'){
			   obj2 = obj.mixer;
			}
			if(json_type=='nqpaxxx'){
			   obj2 = obj.nqpaxxx;
			}
			if(json_type=='ampg1_2c'){
			   obj2 = obj.ampg1_2c;
			}
			if(json_type=='ampg1_4c'){
			   obj2 = obj.ampg1_4c;
			}
			if(json_type=='ampg2_2c'){
			   obj2 = obj.ampg2_2c;
			}
			if(json_type=='ampg2_4c'){
			   obj2 = obj.ampg2_4c;
			}
				
			// console.log(msg.data);
			// console.log(obj);
			for(var key1 in obj) {
				//console.log(key1);
			  for(var key2 in obj[key1]) {
				  //console.log(key2);  
				for(var key3 in obj[key1][key2]) { // start key2, key3
					// console.log(key1,key2,key3);
					
						if(key2=='AmpAB' && key3=='IsDualChannel'){
						 var IsDualChannel = obj2.AmpAB.IsDualChannel;	
						 var before1 =  document.getElementById('IsDualChannel').value;
						 var before2 =  document.getElementById('IsDualChannel2').value;
						 var after1 = 0;
						 var after2 = 0;
						 if(IsDualChannel){
							  document.getElementById('IsDualChannel').value = 1;	
							  after1 = 1;
						  } else {
							  document.getElementById('IsDualChannel').value = 0;
							  after1 = 0;
						  }
							if(before1!=after1){
							   window.location.href = '/configuration_s.php';
							 }
						  }
					  
					  if(key2=='AmpCD' && key3=='IsDualChannel'){
						 var IsDualChannel2 = obj2.AmpCD.IsDualChannel;	
						   if(IsDualChannel2){
							  document.getElementById('IsDualChannel2').value = 1;	
							  after2 = 1;
						  } else {
							  document.getElementById('IsDualChannel2').value = 0;
							  after2 = 0;
						  }
							if(before2!=after2){
							   window.location.href = '/configuration_s.php';
							 }
						  }
					} // end of key2, key3
			  }
			}
		};
		
		socket.onclose  = function(msg) { 
			//console.log(msg);
			
			set_disabled_view();
		};
		
		socket.onerror  = function(msg) { 
			//console.log(msg);
			
			set_disabled_view();
		};
	}
	catch(ex){ 
		console.log(ex); 
	}
}
	
function start_amp_status(){
	var json_type = document.getElementById('json_type').value;
	var msg;
	if(json_type=='nqpaxxx'){
		msg = '{ "Interval": "async", "'+json_type+'": { "InProtection": null } }';
	}
	if(json_type=='ampg1_2c' || json_type=='ampg2_2c'){
		msg = '{ "Interval": "async", "'+json_type+'": { "AmpAB":{ "IsDualChannel": null, "InProtection": null }, "Out1": { "Clip": null }, "Out2": { "Clip": null } } }';
	}
	if(json_type=='ampg1_4c' || json_type=='ampg2_4c'){
		msg = '{ "Interval": "async", "'+json_type+'": { "AmpAB":{ "IsDualChannel": null, "InProtection": null }, "Out1": { "Clip": null }, "Out2": { "Clip": null }, "AmpCD":{ "IsDualChannel": null, "InProtection": null }, "Out3": { "Clip": null }, "Out4": { "Clip": null } } }';
	}
	socket.send(msg);
}
	
// codecs_read_only
$('#codecs_read_only').select2({
	placeholder: 'Add codecs ...',
	tokenSeparators: [',', ' '],
	closeOnSelect: false
});
// select2 output_power
$('#output_power').select2({	
});
// select2 port_type
$('#port_type').select2({	
	minimumResultsForSearch: -1
});
$("#port_type").on("select2:select", function (e) { 
	var selected = e.params.data.id;
	if(selected=='Speaker-Only'){
	    document.getElementById('dial_extension').value = '';
		document.getElementById('dial_extension').readOnly = true;
	} else {
	   document.getElementById('dial_extension').readOnly = false;
	} 
});
// line_in_priority 
$(function() {
	$('#line_in_priority').bootstrapToggle();
});
$(function() {
  	$('#line_in_priority').change(function() {
	  if(document.getElementById("line_in_priority").checked){
		 document.getElementById('line_in_priority').value = '1';
	  } else {
		 document.getElementById('line_in_priority').value = '0';
	  }
    })
});
// external_relay_trigger
$(function() {
	$('#external_relay_trigger').bootstrapToggle();
});
$(function() {
  	$('#external_relay_trigger').change(function() {
	  if(document.getElementById("external_relay_trigger").checked){
		 document.getElementById('external_relay_trigger_old').value = 'ENABLED';
	  } else {
		 document.getElementById('external_relay_trigger_old').value = 'DISABLED';
	  }
    })
});
// external_relay_trigger_1 
$(function() {
	$('#external_relay_trigger_1').bootstrapToggle();
});
$(function() {
  	$('#external_relay_trigger_1').change(function() {
	  if(document.getElementById("external_relay_trigger_1").checked){
		 document.getElementById('external_relay_trigger_1_old').value = 'ENABLED';
	  } else {
		 document.getElementById('external_relay_trigger_1_old').value = 'DISABLED';
	  }
    })
});
// external_relay_trigger_2 
$(function() {
	$('#external_relay_trigger_2').bootstrapToggle();
});
$(function() {
  	$('#external_relay_trigger_2').change(function() {
	  if(document.getElementById("external_relay_trigger_2").checked){
		 document.getElementById('external_relay_trigger_2_old').value = 'ENABLED';
	  } else {
		 document.getElementById('external_relay_trigger_2_old').value = 'DISABLED';
	  }
    })
});
function intercomCutChanged(value){
	document.getElementById('intercom_cut_level').value = value;
}
function intercomCutChanged_1(value){
	document.getElementById('intercom_cut_level_1').value = value;
}	
function intercomCutChanged_2(value){
	document.getElementById('intercom_cut_level_2').value = value;
}	
function intercomCutChanged_3(value){
	document.getElementById('intercom_cut_level_3').value = value;
}	
function intercomCutChanged_4(value){
	document.getElementById('intercom_cut_level_4').value = value;
}
function talkbackGainChanged(value){
	document.getElementById('talkback_gain').value = value;
}
function updateIntercomCutLabel(value){	
	document.getElementById('intercomCutLabel').innerHTML = value;
}
function updateIntercomCutLabel_1(value){	
	document.getElementById('intercomCutLabel_1').innerHTML = value;
}
function updateIntercomCutLabel_2(value){	
	document.getElementById('intercomCutLabel_2').innerHTML = value;
}
function updateIntercomCutLabel_3(value){	
	document.getElementById('intercomCutLabel_3').innerHTML = value;
}
function updateIntercomCutLabel_4(value){	
	document.getElementById('intercomCutLabel_4').innerHTML = value;
}
function updateTalkbackGainLabel(value){	
	document.getElementById('talkbackGainLabel').innerHTML = value;
}
// sorting_mode 
$(function() {
    $('#sorting_mode').bootstrapToggle();
});
$(function() {
  $('#sorting_mode').change(function() {
	    if(document.getElementById("sorting_mode").checked){
		 	document.getElementById('sorting_active').value = '1';
			
			sortable = Sortable.create(list_items, {
            onUpdate: function (evt) {
                // move the changed row in the array
                var new_index = evt.newIndex;
                var old_index = evt.oldIndex;
                var element = dataArray[old_index];
                dataArray.splice(old_index, 1);
                dataArray.splice(new_index, 0, element);

                // update the order numbers in the array
                for (var i=0;i<dataArray.length;i++) {
                    dataArray[i][0] = (i+1).toString();
                }
                document.getElementById('dataArray').value = JSON.stringify(dataArray);
            },
        });
			
	  } else {
		 document.getElementById('sorting_active').value = '0';
		  sortable.destroy();
	  }
    })
});
</script>
