

	if((typeof AFL_JS == 'undefined') || (typeof AFL_JS.Common == 'undefined'))
		throw("AFL_JS.VOIPIax requires the AFL JavaScript framework");
		
	var VOIP = AFL_JS.VOIPIax = new Object();
	
	VOIP.Settings = {
		AppletId : 'phonefromhere',
		ClassNames : {
			On: 'VOIPButton On',
			Out: 'VOIPButton',
			Over: 'VOIPButton Over'
		},
		Debug : 0,
		ButtonMap : {
			'VOIPButtonOne' 		: 1, 
			'VOIPButtonTwo' 		: 2, 
			'VOIPButtonThree' 		: 3,
			'VOIPButtonFour' 		: 4,
			'VOIPButtonFive' 		: 5,
			'VOIPButtonSix' 		: 6,
			'VOIPButtonSeven' 		: 7,
			'VOIPButtonEight' 		: 8,
			'VOIPButtonNine' 		: 9,
			'VOIPButtonZero' 		: 0,
			'VOIPButtonStar' 		: '*',
			'VOIPButtonHash' 		: '#', 
			'VOIPButtonConnected' 	: null,
			'VOIPButtonConnecting'	: null,
			'VOIPButtonDisconnected': null
		}
	}
	
	VOIP.Storage = {
		LoadingTimeoutId : null,
		Buttons : {},
		Calling : 0,
		Connecting : 0,
		ConnectingTries : 0 
	}
	
	VOIP.AssignButtons = function() {
		$H(VOIP.Settings.ButtonMap).each(function(aValues){
			VOIP.Storage.Buttons[aValues[0]] = $(aValues[0]);
		});
	}
	
	VOIP.Initialize = function(iDDI, sCallerName, sCallerId, sDomain, sWebApp, bAttach) {
		VOIP.AssignButtons();
		VOIP.Debug('Buttons assigned');
		if(!bAttach) {
			VOIP.Applet.Build(iDDI, sCallerName, sCallerId, sDomain, sWebApp);
			VOIP.Debug('Built on the page');
		}
		if(bAttach) {
			VOIP.Applet.Attach(iDDI, sCallerName, sCallerId, sDomain, sWebApp);
			VOIP.Debug('Built and attched');
		}
	}
	
	VOIP.Debug = function(sMessage) {
		if(!VOIP.Settings.Debug) return;
		try {
			console.log(sMessage);
		} catch(error) {
			
		}
	}
	
	VOIP.SetDebug = function(bDebug) {
		VOIP.Settings.Debug = bDebug;
	}
	
	VOIP.SynchStatus = function(bFirstRun) {
		if(bFirstRun) {
			VOIP.Storage.ConnectingTries = 0;
		}
		var oPhone = VOIP.Applet.getApplet();
		if(oPhone) {
			oStatus = oPhone.getCallStatus().evalJSON();
			if(VOIP.Storage.Connecting) {
				if(!oStatus.answered) {
					VOIP.Storage.ConnectingTries++;
				}
				if(VOIP.Storage.ConnectingTries > 5) {
					VOIP.Actions.HangUp();
					AnimatedOverlay.ShowById('popup_voip_error');
					return;
				}
			}
		}
		setTimeout(function() {
			VOIP.SynchStatus();
		}, 3000); 
	}
	
	VOIP.Actions = {}
	
	VOIP.Actions.Dial = function() {
		VOIP.Debug('Preparing to dial');
		var oPhone = VOIP.Applet.getApplet();
		if(oPhone) {
			oPhone.dial(VOIP.Settings.Extension);
			VOIP.Console.StatusConnecting();
			VOIP.SynchStatus(1);
			VOIP.Debug('Dialing DDI: '+VOIP.Settings.Extension);
		}
	}
	
	VOIP.Actions.HangUp = function() {
		VOIP.Debug('Preparing for HangUp ...');
		var oPhone = VOIP.Applet.getApplet();
		if(oPhone) {
			oPhone.hangup();
			VOIP.Console.StatusDisconnected();
			VOIP.Debug('HangingUp... done');
		}
	}
	
	VOIP.Actions.DTMF = function(iKey) {
		VOIP.Debug("DTMF recieved - "+iKey);
		var oPhone = VOIP.Applet.getApplet();
		if(oPhone) {
			oPhone.sendDTMF(iKey);
			VOIP.Debug("DTMF executed - "+iKey);
		}
	}
		
	VOIP.Callback = {}
	
	var VOIPCallbackStatus = VOIP.Callback.Status = function(iCode, sMessage, sJSONString) {
		VOIP.Debug('Status callback called, CODE: '+iCode+', Status: '+sMessage);
		switch(parseInt(iCode)) {
			case 1:
				if(!VOIP.Storage.Calling) {
					VOIP.Debug('Status Disconnected NOT called as VOIP.Storage.Calling == false');
					return;
				}
				VOIP.Console.StatusDisconnected();
				VOIP.Debug('Status Disconnected called');
				break;
			case 2:
				if(VOIP.Storage.Calling) {
					VOIP.Debug('Status Connected NOT called as VOIP.Storage.Calling == true');
					return;
				}
				VOIP.Console.StatusConnected();
				VOIP.Debug('Status Connected called');
				break;
			case 3:
				VOIP.Console.StatusConnecting();
				VOIP.Debug('Status Connecting called');
				break;
		}
	}
	
	var VOIPCallbackDTMF = VOIP.Callback.DTMF = function(DTMF) {
		VOIP.Debug('DTMF callback called');
	}
	var VOIPCallbackText = VOIP.Callback.Text = function(Text) {
		VOIP.Debug('Text callback called');
	}
	
	VOIP.Console = {};
	
	VOIP.Console.GetButton = function(sButtonId) {
		return VOIP.Storage.Buttons[sButtonId];
	}
	
	VOIP.Console.GetButtonValue = function(sButtonId) {
		return VOIP.Settings.ButtonMap[sButtonId];
	}
	
	VOIP.Console.Click = function(sButtonId) {
		VOIP.Actions.DTMF(VOIP.Console.GetButtonValue(sButtonId));
		VOIP.Console.GetButton(sButtonId).className = VOIP.Settings.ClassNames.On;
	}
	
	VOIP.Console.Over = function(sButtonId) {
		VOIP.Console.GetButton(sButtonId).className = VOIP.Settings.ClassNames.Over;
	}
	
	VOIP.Console.Out = function(sButtonId) {
		VOIP.Console.GetButton(sButtonId).className = VOIP.Settings.ClassNames.Out;
	}
	
	VOIP.Console.Dial = function(sButtonId) {
		VOIP.Actions.Dial();
	}
	
	VOIP.Console.HangUp = function(sButtonId) {
		VOIP.Actions.HangUp();
	}
	
		
	VOIP.Console.StatusConnected = function() {
		VOIP.Storage.Calling = 1;
		Element.hide(VOIP.Console.GetButton('VOIPButtonDisconnected'));
		Element.hide(VOIP.Console.GetButton('VOIPButtonConnecting'));
		Element.show(VOIP.Console.GetButton('VOIPButtonConnected'));
		VOIP.Debug('StatusConnected executed');
	}
	
	VOIP.Console.StatusConnecting = function() {
		VOIP.Storage.Connecting = 1;
		Element.hide(VOIP.Console.GetButton('VOIPButtonDisconnected'));
		Element.hide(VOIP.Console.GetButton('VOIPButtonConnected'));
		Element.show(VOIP.Console.GetButton('VOIPButtonConnecting'));
		VOIP.Debug('StatusConnecting executed');
	}
	
	VOIP.Console.StatusDisconnected = function() {
		VOIP.Storage.Calling = 0;
		VOIP.Storage.Connecting = 0;
		Element.hide(VOIP.Console.GetButton('VOIPButtonConnecting'));
		Element.hide(VOIP.Console.GetButton('VOIPButtonConnected'));
		Element.show(VOIP.Console.GetButton('VOIPButtonDisconnected'));	
		VOIP.Debug('StatusDisconnected executed');
	}
	
	VOIP.Applet = {}
	
	VOIP.Applet.getApplet = function() {
		return $(VOIP.Settings.AppletId);
	}

	VOIP.Applet.Build = function(iDDI, sCallerName, sCallerId, sDomain, sWebApp) {
		VOIP.Debug('Initialising: ' +sCallerName + ', '  + iDDI);
		VOIP.Settings.Extension = iDDI;
		
		var sJARLocation = sDomain + '/assets/java/pfh.jar';
		var sAuthLocation = sDomain + '/' + sWebApp + '/auth.php';
	
		document.write('<applet');
  		document.write('   code="com.phonefromhere.softphone.Phonefromhere"');
		document.write('   archive="' + sJARLocation + '" ');
		document.write('   id=' + VOIP.Settings.AppletId + ' ');
		document.write('   name="phonefromhere" ');
		document.write('   height="1"');
		document.write('   width="1"');
		document.write('   hspace="0"');
		document.write('   vspace="0"');
		document.write('   align="middle"');
		document.write('   mayscript="true" >');
		document.write('      <param name="debug" value="' + VOIP.Settings.Debug + '"/>');
		document.write('      <param name="authuri" value="' + sWebApp + '"/>');
		document.write('      <param name="dialno" value ="' + iDDI + '"/>');
		document.write('      <param name="autostart" value ="false"/>');
		document.write('      <param name="callerid" value ="' + sCallerId + '"/>');
		document.write('      <param name="callername" value ="' + sCallerName + '"/>');
		document.write('      <param name="statusCallback" value="phonefromherestatus"/>');
		document.write('      <param name="dtmfCallback" value="phonefromhereGotDtmf"/>');
		document.write('      <param name="textCallback" value="phonefromhereGotText"/>');
		document.write('      <param name="doEncryption" value="true"/>');
		document.write('      <param name="doEC" value="true"/>');
		document.write('      <param name="technology" value="com.phonefromhere.softphone.iax.DigiumPhone"/>');
		document.write('      <param name="mayscript"/>');
		document.write('  </applet>');
	}
	
	VOIP.Applet.Attach = function(iDDI, sCallerName, sCallerId, sDomain, sWebApp) {

		VOIP.Debug('Initialising: ' +sCallerName + ', '  + iDDI);
		VOIP.Settings.Extension = iDDI;
		
		var sJARLocation = sDomain + '/assets/java/pfh.jar';
		var sAuthLocation = sDomain + '/' + sWebApp + '/auth.php';
	
		oApplet = new Element('applet', {
			'code'			: "com.phonefromhere.softphone.Phonefromhere",
			'archive'		: sJARLocation,
			'id'			: VOIP.Settings.AppletId,
			'name'			: "phonefromhere",
			'height'		: "1",
			'width'			: "1",
			'hspace'		: "0",
			'vspace'		: "0",
			'align'			: "middle",
			'mayscript'		: "true"
		});
		oApplet.appendChild(new Element('param', {
			'name'	: "debug",
			'value'	: VOIP.Settings.Debug
		}));
		oApplet.appendChild(new Element('param', {
			'name'	: "authuri",
			'value'	: sWebApp
		}));
		oApplet.appendChild(new Element('param', {
			'name'	: "dialno",
			'value'	: iDDI
		}));
		oApplet.appendChild(new Element('param', {
			'name'	: "autostart",
			'value'	: "false"
		}));
		oApplet.appendChild(new Element('param', {
			'name'	: "callerid",
			'value'	: sCallerId
		}));
		oApplet.appendChild(new Element('param', {
			'name'	: "callername",
			'value'	: sCallerName
		}));
		oApplet.appendChild(new Element('param', {
			'name'	: "statusCallback",
			'value'	: "VOIPCallbackStatus"
		}));
		oApplet.appendChild(new Element('param', {
			'name'	: "dtmfCallback",
			'value'	: "VOIPCallbackDTMF"
		}));
		oApplet.appendChild(new Element('param', {
			'name'	: "textCallback",
			'value'	: "VOIPCallbackText"
		}));
		oApplet.appendChild(new Element('param', {
			'name'	: "doEncryption",
			'value'	: "true"
		}));
		oApplet.appendChild(new Element('param', {
			'name'	: "doEC",
			'value'	: "true"
		}));
		oApplet.appendChild(new Element('param', {
			'name'	: "technology",
			'value'	: "com.phonefromhere.softphone.iax.DigiumPhone"
		}));
		oApplet.appendChild(new Element('param', {
			'name'	: "mayscript"
		}));
		
		$('VoipInterface').appendChild(oApplet);
		
		return;

	}
