var serveraddress_input = null; // serveraddress input field
var sipusername_input = null; // sip account username input field
var username_input = null; // sip account username input field
var password_input = null; // sip account password input field
var destination_input = null; // destination number or SIP URI input field
var td_started = false;
function InitializeTextInputs()
{
if (isNull(serveraddress_input))
{
serveraddress_input = document.getElementById('serveraddress');
sipusername_input = document.getElementById('sipusername');
username_input = document.getElementById('username');
password_input = document.getElementById('password');
destination_input = document.getElementById('destinationnr');
}
}
function Start() // function called on "Register" button click to start the webphone
{
td_started = true;
InitializeTextInputs();
SaveSettings();
DisplayStatus('EVENT, Initializing...');
webphone_api.start();
// populate advanced settings form
PopulateSettings();
}
// Wait until the webphone is loaded, before calling any API functions
// if automatic start is required, then webphone_api.start() should be called "onLoaded" event like this:
// webphone_api.onAppStateChange(function (state)
// {
// if (state === 'loaded')
// {
// webphone_api.start();
// }
// });
webphone_api.onAppStateChange(function (state)
{
if (state === 'loaded')
{
InitializeTextInputs();
PopulateSettings();
var autostart = webphone_api.getparameter('autostart');
if(1 == 1 || autostart.length < 1)
{
webphone_api.setparameter('autostart',0); //precent auto-start by default
}
var serveraddress = webphone_api.getparameter('serveraddress');
if (serveraddress.length < 1) { serveraddress = webphone_api.getparameter('serveraddress_user'); } // only for demo
var sipusername = webphone_api.getparameter('sipusername');
var password = webphone_api.getparameter('password');
var destination = webphone_api.getparameter('destination');
if (serveraddress.length > 0) { serveraddress_input.value = serveraddress; }
if (sipusername.length > 0) { sipusername_input.value = sipusername; }
if (password.length > 0) { password_input.value = password; }
if (destination.length > 0) { destination_input.value = destination; }
var rcolumn = document.getElementById('right_column');
var btnadv = document.getElementById('btn_advanced_sett');
if (isNull(rcolumn) || isNull(btnadv)) { return; }
if (rcolumn.style.display === 'none')
{
btnadv.innerHTML = 'Show more settings';
}else
{
btnadv.innerHTML = 'Hide Other Settings';
}
}
});
/** Initiate call to a number, sip username or SIP URI.*/
function Call()
{
InitializeTextInputs();
if(!td_started)
{
Start();
}
var destnr = destination_input.value;
if (isNull(destnr) || (Trim(destnr)).length < 1)
{
DisplayStatus('ERROR Invalid destination number');
return;
}
webphone_api.setparameter('destination', destnr, false);
webphone_api.call(Trim(destnr));
}
/** Disconnect current call(s).*/
function Hangup()
{
webphone_api.hangup();
}
/** Connect incoming call*/
function Accept()
{
webphone_api.accept();
}
/** Disconnect incoming call.*/
function Reject(reason)
{
webphone_api.reject(null, reason);
}
/** This callback function will be called on every call state change.
* --PARAMETERS --
* status: can have following values: callSetup, callRinging, callConnected, callDisconnected
* direction: 1 (outgoing), 2 (incoming)
* peername: is the other party username
* peerdisplayname: is the other party display name if any
* line: the status refers to this line*/
webphone_api.onCallStateChange(function (status, direction, peername, peerdisplayname, line)
{
if (status === 'setup')
{
ringingNumber = peername;
// if it's an incoming call, then display popup with Accept/Reject buttons
if (direction === 2)
{
AcceptRejectPopup(ringingNumber);
}
}
else if (status === 'disconnected')
{
// close AcceptReseject popup on call finished, if it's still open
if (!isNull(armodal))
{
armodal.close();
armodal = null;
}
}
});
/** display / hide chat form*/
function ShowHideChat()
{
if ($('#chat_box').is(':visible')) { $('#chat_box').hide(); } else { $('#chat_box').show(); }
}
/** Send a chat message. (SIP MESSAGE method after RFC 3428)*/
function SendChat()
{
InitializeTextInputs();
var msgF = document.getElementById('message');
var to = destination_input.value;
var msg = msgF.value;
if (isNull(to) || (Trim(to)).length < 1)
{
DisplayStatus('ERROR, Invalid chat destination number');
destination_input.focus();
return;
}
if (isNull(msg) || (Trim(msg)).length < 1)
{
DisplayStatus('ERROR, Enter chat message to be sent');
msgF.focus();
return;
}
msgF.value = '';
// Displays messages in chat form window
AddMessageToHistory('Me', msg);
webphone_api.sendchat(to, msg);
}
/** Receive incoming messages*/
webphone_api.onChat(function (from, msg, line)
{
InitializeTextInputs();
if (isNull(from) || isNull(msg)) { return; }
var currdest = destination_input.value;
if (isNull(currdest) || (Trim(currdest)).length < 1)
{
destination_input.value = from;
}else
{
if (Trim(currdest) !== from)
{
AddMessageToHistory('', '
###############################
');
destination_input.value = from;
}
}
// Displays messages in chat form window
AddMessageToHistory(from, msg);
});
/** Displays sent/received messages in chat form window*/
function AddMessageToHistory(to, message)
{
var sentmsgF = document.getElementById('msg_list');
var msgconttent = sentmsgF.innerHTML;
if (isNull(msgconttent)) { msgconttent = ''; }
var item = '';
if (!isNull(to) && to.length > 0)
{
item = '' + to + ':
' + message + '
' + GetDateForMessage() + '
'; }else { item = '' + message + '
'; } msgconttent = msgconttent + item + '