/* 
History:

Ver	Date		Who	Did what
============================================================================
0.01	20.10.2010	ASI 	Created initial version
0.02  27.01.2011  ASI   Added AI Phone settings
                        Modified to set autohangup mode in SIP Phone
				profile
				Set to select always 20 seconds hangup time
				for the SIP phone profile
0.03  19.01.2012  ASI   Added -stream timeout setting for all profiles 
                        except Monitor Mode
0.04  31.05.2012  ASI   Added AEC
0.05  14.09.2012  ASI   Added Beep on call answer
0.06  17.09.2012  ASI   Added also beep on incoming call answer
0.07  08.11.2022  ASI   Added offhook call entry

============================================================================

*/ 

// Keep original values (must be global)
var profileHasChanged = parseInt(0,10);		// force numeric type
var initialCloseOnLevel;
var initialCloseOnTimeout;
var initialCallOnLevel;
var initialCallOnLevelId;
var initialLevelThreshold;
var initialPickupMode;
var initialPickHangUpTimeout;
var initialStreamTimeout;
var initialRebrcastPort;
var initialBGMPort;
var initialAudioInput;
var initialCallOnInput0;
var initialCallOnInput1;
var initialCallOnInput2;
var initialCallOnInput3;
var initialCallOnInput4;
var initialCallOnInput5;
var initialCallOnInput6;
var initialCallOnInput7;
var initialCallOnX8Input0;
var initialCallOnX8Input1;
var initialCallOnX8Input2;
var initialCallOnX8Input3;
var initialCallOnX8Input4;
var initialCallOnX8Input5;
var initialCallOnX8Input6;
var initialCallOnX8Input7;
var initialTalkMode;
var initialAIphoneEnable;
var initialAEC;
var initialBeepOnOutboundAnswer;
var initialBeepOnIncomingAnswer;
var initialBlindCallTransfer;
var initialOffhookCall;
var currentProfile;

/* Profile processing functions */

function PresetProfile(newProfile){
	profileHasChanged = profileHasChanged + 1; 

	// the default values
	// use parseInt to force numeric type
	var defaultCallOnLevel = parseInt (0,10);	// call on level disabled
	var defaultCallOnLevelId = '';			// no call on level id defined
	var defaultLevelThreshold = parseInt(1000,10);	// default - 1000
	var defaultCloseOnLevel = parseInt (0,10);	// close on level disabled
	var defaultAudioRebrcastPort = parseInt (0,10);	// Rebroadcast disabled
	var defaultBGMPort = parseInt (0,10);		// BGM disabled
	var defaultAudioInput = parseInt(130,10);	// Mic
	var defaultCallId = '';				// empty string for number/id to call
	var directPickupMode = parseInt (0,10);		// autoanswer pickup mode
	var notcallablePickupMode = parseInt (3,10);	// do not call this device
	var autohangupPickupMode = parseInt (2,10);	// autohang up on timeout if not answered
	var defaultPickHangUpTimeout=parseInt (20,10);  // 20 seconds default pik/hang up timeout
	var defaultCloseOnTimeout = parseInt (0,10);	// close on timeout disabled
	var defaultStreamTimeout = parseInt (0,10);      // stream timoeut disabled
	var maxCloseOnTimeout = parseInt (30,10);       // maximum close on timeout (30 seconds)
	var defaultTalkMode = parseInt (0,10);		// default talk mode: Full Duplex
	var defaultAIphoneEnable = parseInt (0,10);	// AI Phone not enabled
	var halfduplexTalkMode = parseInt (1,10);	// set talk mode: Half Duplex
	var defaultAEC = parseInt (0,10);               // default: AEC off
	var defaultBeepOnOutboundAnswer = parseInt (0,10);	// default: Beep off
	var defaultBeepOnIncomingAnswer = parseInt (0,10);	// default: Beep off
	var defaultBlindCallTransfer = parseInt (0,10);	// default: Blind call transfer disabled
	var defaultOffhookCall = parseInt (0,10);  // default: disable offhook/onhook handling for Ann60
	
	// we may select several profiles before clicking "Apply",
	// so, take care to restore the "original" values, and 
	// change only the relevant ones. Otherwise we may end up
	// with a mix of settings we do not want
	// if it is the first change (profileHasChanged = 0 or 1), we
	// have nothing to reset
	
	if ((currentProfile != newProfile) && (profileHasChanged > 1)) {
		RestoreInitialValues();
		profileHasChanged = parseInt(0,10);
	}
	
	switch (newProfile){
		case "0":	// Sip phone
			set_val('callonlevel', defaultCallOnLevel);
			set_val('callonlevelid', defaultCallOnLevelId);
			set_val('levelthreshold', defaultLevelThreshold);
			set_val('closeonlevel', defaultCloseOnLevel);
			set_val('pickupmode', autohangupPickupMode);
			set_val('pickhangtimeout', defaultPickHangUpTimeout);
			set_val('rebrcastport', defaultAudioRebrcastPort);
			set_val('bgmport', defaultBGMPort);
			set_val('bgmport', defaultBGMPort);
			set_val('talkmode', defaultTalkMode);
			set_val('aiphone_en', defaultAIphoneEnable);
			set_val('aec', defaultAEC);
			break;
		case "1":	// Doorstation 
			set_val('callonlevel', defaultCallOnLevel);
			set_val('callonlevelid', defaultCallOnLevelId);
			set_val('levelthreshold', defaultLevelThreshold);
			set_val('closeonlevel', defaultCloseOnLevel);
			set_val('closeontimeout', maxCloseOnTimeout);
			set_val('pickupmode', directPickupMode);
			set_val('rebrcastport', defaultAudioRebrcastPort);
			set_val('bgmport', defaultBGMPort);
			set_val('talkmode', halfduplexTalkMode);
			set_val('aiphone_en', defaultAIphoneEnable);
			set_val('aec', defaultAEC);
			set_val('call_transfer', defaultBlindCallTransfer);
			set_val('offhook_call', defaultOffhookCall);
			break;
		case "2":	// Paging station
			set_val('callonlevel', defaultCallOnLevel);
			set_val('callonlevelid', defaultCallOnLevelId);
			set_val('levelthreshold', defaultLevelThreshold);
			set_val('closeonlevel', defaultCloseOnLevel);
			set_val('closeontimeout', defaultCloseOnTimeout);
			set_val('pickupmode', directPickupMode);
			set_val('rebrcastport', defaultAudioRebrcastPort);
			set_val('talkmode', defaultTalkMode);
			set_val('aiphone_en', defaultAIphoneEnable);
			set_val('aec', defaultAEC);
			set_val('beep_out', defaultBeepOnOutboundAnswer);
			set_val('call_transfer', defaultBlindCallTransfer);
			set_val('offhook_call', defaultOffhookCall);
			// clear all call ids
			set_val('coi0', defaultCallId);
			set_val('coi1', defaultCallId);
			set_val('coi2', defaultCallId);
			set_val('coi3', defaultCallId);
			set_val('coi4', defaultCallId);
			set_val('coi5', defaultCallId);
			set_val('coi6', defaultCallId);
			set_val('coi7', defaultCallId);
			set_val('cox8i0', defaultCallId);
			set_val('cox8i1', defaultCallId);
			set_val('cox8i2', defaultCallId);
			set_val('cox8i3', defaultCallId);
			set_val('cox8i4', defaultCallId);
			set_val('cox8i5', defaultCallId);
			set_val('cox8i6', defaultCallId);
			set_val('cox8i7', defaultCallId);
			break;
		case "3":	// gateway
			set_val('callonlevel', defaultCallOnLevel);
			set_val('callonlevelid', defaultCallOnLevelId);
			set_val('levelthreshold', defaultLevelThreshold);
			set_val('closeonlevel', defaultCloseOnLevel);
			set_val('closeontimeout', defaultCloseOnTimeout);
			set_val('pickupmode', directPickupMode);
			set_val('bgmport', defaultBGMPort);
			set_val('talkmode', defaultTalkMode);
			set_val('aiphone_en', defaultAIphoneEnable);
			set_val('aec', defaultAEC);
			set_val('beep_out', defaultBeepOnOutboundAnswer);
			set_val('beep_in', defaultBeepOnIncomingAnswer);
			set_val('call_transfer', defaultBlindCallTransfer);
			set_val('offhook_call', defaultOffhookCall);
			// clear all call ids
			set_val('coi0', defaultCallId);
			set_val('coi1', defaultCallId);
			set_val('coi2', defaultCallId);
			set_val('coi3', defaultCallId);
			set_val('coi4', defaultCallId);
			set_val('coi5', defaultCallId);
			set_val('coi6', defaultCallId);
			set_val('coi7', defaultCallId);
			set_val('cox8i0', defaultCallId);
			set_val('cox8i1', defaultCallId);
			set_val('cox8i2', defaultCallId);
			set_val('cox8i3', defaultCallId);
			set_val('cox8i4', defaultCallId);
			set_val('cox8i5', defaultCallId);
			set_val('cox8i6', defaultCallId);
			set_val('cox8i7', defaultCallId);

			break;
		case "4":	// monitor (call on voice activity on the mic)
			set_val('pickupmode', notcallablePickupMode);
			set_val('bgmport', defaultBGMPort);
			set_val('rebrcastport', defaultAudioRebrcastPort);
			set_val('audioinput', defaultAudioInput);
			set_val('talkmode', defaultTalkMode);
			set_val('aiphone_en', defaultAIphoneEnable);
			set_val('streamtimeout',defaultStreamTimeout);
			set_val('aec', defaultAEC);
			set_val('beep_out', defaultBeepOnOutboundAnswer);
			set_val('beep_in', defaultBeepOnIncomingAnswer);
			set_val('call_transfer', defaultBlindCallTransfer);
			set_val('offhook_call', defaultOffhookCall);
			// clear all call ids
			set_val('coi0', defaultCallId);
			set_val('coi1', defaultCallId);
			set_val('coi2', defaultCallId);
			set_val('coi3', defaultCallId);
			set_val('coi4', defaultCallId);
			set_val('coi5', defaultCallId);
			set_val('coi6', defaultCallId);
			set_val('coi7', defaultCallId);
			set_val('cox8i0', defaultCallId);
			set_val('cox8i1', defaultCallId);
			set_val('cox8i2', defaultCallId);
			set_val('cox8i3', defaultCallId);
			set_val('cox8i4', defaultCallId);
			set_val('cox8i5', defaultCallId);
			set_val('cox8i6', defaultCallId);
			set_val('cox8i7', defaultCallId);

			break;
		default:
			alert ('Unknown profile '+ newProfile + ' used');
			break;
	}
}

function StoreInitialValues(){
		
	initialCloseOnLevel	= get_val('closeonlevel');
	initialCloseOnTimeout   = get_val('closeontimeout');
	initialCallOnLevel	= get_val('callonlevel');
	initialCallOnLevelId	= get_val('callonlevelid');
	initialLevelThreshold	= get_val('levelthreshold');
	initialPickupMode	= get_val('pickupmode');
	initialPickHangUpTimeout= get_val('pickhangtimeout');
	initialStreamTimeout    = get_val('streamtimeout');
	initialRebrcastPort	= get_val('rebrcastport');
	initialBGMPort		= get_val('bgmport');
	initialAudioInput	= get_val('audioinput');
	initialTalkMode         = get_val('talkmode');
	initialAIphoneEnable    = get_val('aiphone_en');
	initialCallOnInput0	= get_val('coi0');
	initialCallOnInput1	= get_val('coi1');
	initialCallOnInput2	= get_val('coi2');
	initialCallOnInput3	= get_val('coi3');
	initialCallOnInput4	= get_val('coi4');
	initialCallOnInput5	= get_val('coi5');
	initialCallOnInput6	= get_val('coi6');
	initialCallOnInput7	= get_val('coi7');
	initialCallOnX8Input0	= get_val('cox8i0');
	initialCallOnX8Input1	= get_val('cox8i1');
	initialCallOnX8Input2	= get_val('cox8i2');
	initialCallOnX8Input3	= get_val('cox8i3');
	initialCallOnX8Input4	= get_val('cox8i4');
	initialCallOnX8Input5	= get_val('cox8i5');
	initialCallOnX8Input6	= get_val('cox8i6');
	initialCallOnX8Input7	= get_val('cox8i7');
	initialAEC              = get_val('aec');
	initialBeepOnOutboundAnswer	= get_val('beep_out');
	initialBeepOnIncomingAnswer	= get_val('beep_in');
	initialBlindCallTransfer	= get_val('call_transfer');
	initialOffhookCall          = get_val('offhook_call');
	currentProfile		= get_val('profile');
}

function RestoreInitialValues(){
	set_val('closeonlevel', initialCloseOnLevel);
	set_val('closeontimeout', initialCloseOnTimeout);
	set_val('callonlevel', initialCallOnLevel);
	set_val('callonlevelid', initialCallOnLevelId);  
	set_val('levelthreshold', initialLevelThreshold);
	set_val('pickupmode', initialPickupMode);
	set_val('pickhangtimeout', initialPickHangUpTimeout);
	set_val('rebrcastport', initialRebrcastPort);
	set_val('bgmport', initialBGMPort);
	set_val('audioinput', initialAudioInput);
	set_val('talkmode', initialTalkMode);
	set_val('aiphone_en', initialAIphoneEnable);
	set_val('coi0', initialCallOnInput0);
	set_val('coi1', initialCallOnInput1);
	set_val('coi2', initialCallOnInput2);
	set_val('coi3', initialCallOnInput3);
	set_val('coi4', initialCallOnInput4);
	set_val('coi5', initialCallOnInput5);
	set_val('coi6', initialCallOnInput6);
	set_val('coi7', initialCallOnInput7);
	set_val('cox8i0', initialCallOnX8Input0);
	set_val('cox8i1', initialCallOnX8Input1);
	set_val('cox8i2', initialCallOnX8Input2);
	set_val('cox8i3', initialCallOnX8Input3);
	set_val('cox8i4', initialCallOnX8Input4);
	set_val('cox8i5', initialCallOnX8Input5);
	set_val('cox8i6', initialCallOnX8Input6);
	set_val('cox8i7', initialCallOnX8Input7);
	set_val('aec',initialAEC);
	set_val('beep_out', initialBeepOnOutboundAnswer);
	set_val('beep_in', initialBeepOnIncomingAnswer);
	set_val('call_transfer', initialBlindCallTransfer);
}
