			Twilio.Device.setup("<?php echo $token; ?>");
			
			Twilio.Device.ready(function (device) {
				$("#log").text("Ready");
			});

			Twilio.Device.error(function (error) {
				$('#log').css('color','red');
				$("#log").text("Error: " + error.message);
			});

			Twilio.Device.connect(function (conn) {
				$('#log').css('color','green');
				$("#log").text("Successfully established call");
			});

			Twilio.Device.disconnect(function (conn) {
				$('#log').css('color','green');
				$("#log").text("Call ended");
			});

			Twilio.Device.incoming(function (conn) {
				$('#log').css('color','green');
				$("#log").text("Incoming connection from " + conn.parameters.From + " To "+ conn.parameters.To);
		        $('#myAudio').trigger('click');
				/*  Aaccept the incoming connection and start two-way audio */
				$("#Answer").click(function() {
					conn.accept();
				});
			});
	  
		   /* Twilio.Device.incoming(function (connection) {
				if (confirm('Accept incoming call from ' + connection.parameters.From + '?')){
					connection.accept();
				} else {
					connection.reject();
				}
			}); */

			function call() {
				/* Get the phone number to connect the call to */
				params = {"tocall": $("#number").val()};
				Twilio.Device.connect(params);
				$('#log').css('color','green');
			}

			function hangup() {
				Twilio.Device.disconnectAll();
				//connection.disconnect()
				$('#log').css('color','red');
			}
			
			function isNumber(evt) {
				evt = (evt) ? evt : window.event;
				var charCode = (evt.which) ? evt.which : evt.keyCode;
				if (charCode > 43 && charCode > 31 && (charCode < 48 || charCode > 57)) {
					return false;
				}
				return true;
			}
			function remove(){
				$('#number').val(
					function(index, value){
						return value.substr(0, value.length - 1);
				})
			}
			$(document).ready(function () {
				$('.num').click(function () {
					var num = $(this);
					var text = $.trim(num.find('.txt').clone().children().remove().end().text());
					var telNumber = $('#number');
					$(telNumber).val(telNumber.val() + text);
				});
             });