document.write('');
var call_button_text = ''; // text showed on call button
var hangup_button_text = ''; // text showed on hangup button
var call_button_color = ''; // call button color
var hangup_button_color = ''; // hangup button color
var status_text_color = ''; // color of displayed status messages
var button_width = '';
var started = 0; // 0=not started, 1=register not received(wait longer before making the call), 2=register received, so we can make call
var btn_c2k = null;
var text_label = null;
var text_status = null;
var isincall = false;
var isfirstcall = true; // wait before placing the first call
var calltonr = '';
function Start()
{
started = 1;
webphone_api.start();
webphone_api.getEvents(function (evt)
{
ProcessNotifications(evt);
});
webphone_api.GetDisplayableNotifications(function (msg)
{
DisplayStatus(msg);
});
}
var calltimer = null;
function Click2Call()
{
var serveraddress = webphone_api.parameters['serveraddress'];
var username = webphone_api.parameters['username'];
var callto = webphone_api.parameters['callto'];
// don't request server, user, password if it's started from and set from URL
if (isNull(calltonr) || calltonr.length < 1)
{
if (isNull(serveraddress) || serveraddress.length < 3) { alert('VoIP serveraddress is not set.\n It can be set in "click2call_example.html"'); return; }
if (isNull(username) || username.length < 3) { alert('VoIP accounts username is not set.\n It can be set in "click2call_example.html"'); return; }
if (isNull(callto) || callto.length < 3) { alert('Destination number (callto) is not set.\n It can be set in "click2call_example.html"'); return; }
}
if (started < 1)
{
Start();
//put status to started after 10 seconds, even if register was not received
setTimeout(function ()
{
started = 2;
}, 10000);
}
if (!isNull(calltimer)) { clearTimeout(calltimer); }
calltimer = null;
calltimer = setTimeout(function ()
{
if (started < 2)
{
Click2Call();
return;
}
var destnr = webphone_api.parameters['callto'];
if (!isincall) // make a call
{
var timeout = 1;
if (isfirstcall)
{
isfirstcall = false;
timeout = 800;
}
setTimeout(function ()
{
webphone_api.call(Trim(destnr));
}, timeout);
}else
{
webphone_api.hangup();
text_label.innerHTML = call_button_text;
btn_c2k.style.background = call_button_color;
}
}, 250);
}
var loglevel = null;
function DisplayStatus(msg_orig)
{
try{
var msg = msg_orig;
if (isNull(text_status))
{
text_status = document.getElementById('status');
if (isNull(text_status))
{
PutToDebugLog(2, 'ERROR, cannot get status element');
return;
}
}
if (isNull(loglevel))
{
loglevel = 1;
try{ loglevel = parseInt(webphone_api.parameters['loglevel'], 10); } catch(err) { }
}
if (isNull(msg) || msg.length < 1) { msg = ' '; }
if (msg.indexOf('ERROR') >= 0)
{
msg = msg.replace('ERROR', '');
msg = Trim(msg);
if (msg.indexOf(',') === 0)
{
msg = msg.replace(',', '');
msg = Trim(msg);
}
msg = '' + msg + '';
}
else if (msg.indexOf('WARNING') >= 0)
{
msg = msg.replace('WARNING', '');
msg = Trim(msg);
if (msg.indexOf(',') === 0)
{
msg = msg.replace(',', '');
msg = Trim(msg);
}
msg = '' + msg + '';
}
else if (msg.indexOf('STATUS') >= 0 || msg.indexOf('EVENT') >= 0)
{
msg = msg.replace('STATUS', '');
msg = msg.replace('EVENT', '');
msg = Trim(msg);
if (msg.indexOf(',') === 0)
{
msg = msg.replace(',', '');
msg = Trim(msg);
}
}
if ((msg.toLowerCase()).indexOf('registered') >= 0 || (msg.toLowerCase()).indexOf('authenticated successfully') >= 0)
{
started = 2;
}
text_status.innerHTML = msg;
if (loglevel > 1)
{
PutToDebugLog(2, 'NOT: ' + msg_orig);
}
} catch(err) { PutToDebugLogException(2, 'live_demo: DisplayStatus', err); }
}
// Populate settings
window.onload = function ()
{
btn_c2k = document.getElementById('btn_c2k');
text_label = document.getElementById('text_label');
if (!isNull(btn_c2k) && !isNull(text_label))
{
text_label.innerHTML = call_button_text;
btn_c2k.style.background = call_button_color;
if (button_width > 0)
{
btn_c2k.style.width = button_width + 'px';
}
}
text_status = document.getElementById('status');
if (!isNull(text_status))
{
text_status.style.color = status_text_color;
}
// handle call from url
var url = window.location.href;
var pos = url.indexOf('?');
if (pos > 0)
{
url = url.substring(pos + 1, url.length);
pos = url.indexOf('wp_callto=');
if (pos > 0)
{
url = url.substring(pos + 10);
pos = url.indexOf('&');
if (pos < 0) { pos = url.length; }
calltonr = url.substring(0, pos);
}
}
if (!isNull(calltonr) && calltonr.length > 0)
{
Start();
}
};
var ringingNumber = '';
function ProcessNotifications(not)
{
try{
if (isNull(not) || not.length < 1) { return; }
not = Trim(not);
var type = '';
var line = '';
var notifyword = '';
//STATUS,1,Ringing,8888,9999,2,8888
var pos = not.indexOf(',');
if (pos > 0)
{
type = Trim(not.substring(0, pos));
not = Trim(not.substring(pos + 1));
}
// get line
pos = not.indexOf(',');
if (pos > 0)
{
line = Trim(not.substring(0, pos));
notifyword = Trim(not.substring(pos + 1));
}else
{
notifyword = not;
}
var notifywordcontent = '';
// clear junk of notifyword's end
pos = notifyword.indexOf(',');
if (pos > 0)
{
notifywordcontent = Trim(notifyword.substring(pos + 1));
notifyword = Trim(notifyword.substring(0, pos));
}
// handle incoming call
if (type === 'STATUS')
{
if (notifyword === 'Ringing')
{
text_label.innerHTML = hangup_button_text;
btn_c2k.style.background = hangup_button_color;
isincall = true;
}
if (notifyword === 'Call Finished' || notifyword === 'Finished')
{
isincall = false;
text_label.innerHTML = call_button_text;
btn_c2k.style.background = call_button_color;
}
}
} catch(err) { PutToDebugLogException(2, 'live_demo: ProcessNotifications', err); }
}
// ------------------ helper functions
function isNull (variable)
{
try{
if (typeof (variable) === 'undefined' || variable === null)
{
return true;
}else
{
return false;
}
} catch(err) { PutToDebugLogException(2, "common: isNull", err); }
return true;
}
function Trim(str)
{
try{
if (isNull(str) || str.lenght < 1) { return ''; }
str = str.toString();
return str.replace(/^\s+|\s+$/g, '');
} catch(err) { PutToDebugLogException(2, "common: Trim", err); }
return str;
}
function PutToDebugLogException(level, msg, err) { PutToDebugLog(level, 'ERROR,' + msg + ' ' + err); }
function PutToDebugLog(level, msg)
{
if (msg.indexOf("ERROR") > -1)
{
try { console.error(msg); } catch (e1) { }
}else
{
try { console.log(msg); } catch (e2) { }
}
}