var WEBROOT = "/";
var PRIVATE_WEBROOT = "/private/";
var CLIENTLAUNCH_PROTOCOL ="mncstart://";
var PLUGIN_PROTOCOL = "mnc://";
var PLUGIN_ARGUMENT_SEPARATOR = '|';
var OPERATIONS_PAGE = 'operation.aspx'
var KEEPALIVE_URL = 'keepalive.aspx';
var KEEPALIVE_INTERVAL = 180; //Three minutes

function launchPlugin(pluginId, arguments) {	
	var pluginLaunchString;
	pluginLaunchString = PLUGIN_PROTOCOL + pluginId;
	
	//Parse the arguments 
	if (arguments)	{
		for (var i=0;i<arguments.length ;i++ ) 	{
			pluginLaunchString +=  PLUGIN_ARGUMENT_SEPARATOR + (arguments[i]).toString();
		}
	}
	location.href = pluginLaunchString;
}

function startClientApplication() {
	location.href=CLIENTLAUNCH_PROTOCOL;
}

function redirectTo(url, timeout) {
	if (timeout == null) timeout = 0;
	setTimeout("location.href='" +  url + "';",timeout);
}

function redirectToOperationPage(timeout) {
	redirectTo(PRIVATE_WEBROOT + OPERATIONS_PAGE,timeout);
}

function PasswordsManager() {
	if (appTested) {
		if (appLoaded)	{
			launchPlugin('PasswordsManager');
			redirectToOperationPage();
		}
	}
	else	{
		setTimeout("PasswordsManager();",200);
	}
}

function ChangeServiceVisibility()	{
	if (appTested)	{
		if (appLoaded)	{
			updateServiceVisibility(true);
		}
	}
	else	{
		setTimeout("ChangeServiceVisibility();",100);
	}
}
function ShowUpdateNeeded(message) {	
	launchPlugin('Update');						
}

function CheckApplication()		{
	appStartedImg.src = 'http://localhost:5050/CheckRunningState?' + Date();
}

function KeepSessionAlive() {
	var url = PRIVATE_WEBROOT + KEEPALIVE_URL;
	//Ajax request to keep session alive
	var ran = Math.round(Math.random()*10000);
	new Ajax.Request(url, {
		method: 'get',
		parameters: 'r=' + ran,
		onSuccess: function (transport) {},
		onFailure: function (transport) {}
	});		
}

var KeepAliveExecuter= new PeriodicalExecuter(KeepSessionAlive,KEEPALIVE_INTERVAL);
