/**
 * support for HTA applications
 * 
 * this is meant to be called from HTAs or their clients
 * NEVER USE THIS FILE DIRECTLY, CALL IT VIA CACHED OTE WRAPPER
 * or USE the js_cache/htasupport.js version
 * [th]dummy string[/th] to register this file to OTE
 **/
var htasHost='{$ote.host}';
//{literal}
   
function htasGetSessionId(app) {
    var cmdline=app.commandLine;
    
    var cmdarr=cmdline.split(' ');
    var len=cmdarr.length;
    if (len>0) {
        return(cmdarr[len-1]);
    }
    return '';
}



// this is the emulation function of window.open() for HTA
// when possible, it will just start another HTA subapp instead of calling the new window
// if not, it will at least fix the session ID

function windowOpen(url, target, flags, replace) {
	if (target && typeof(target) == 'string' && target.substr(0,1) != '_') {
		var frm = window.top.frames;
		
		for (i = 0; i < frm.length; i++) {
			if (!frm(i).name)
				continue;
			//alert('HTAdebug: using window.open for frame '+frm(i).name);
			if (frm(i).name == target)
				return(window.open(url, target, flags, true));
		}
	}
	
	// add session id to urls if session available
	if (window['htas_sid'] != null && url.indexOf('OTYS_BACKOFFICE') == -1) {
		if (url.indexOf('?') == -1) {
			url = url +'?'+ htas_sid;
		}
		else {
			url = url +'&'+ htas_sid;
		}
	}
	// convert local urls to internet urls (backoffice)
	if (url.indexOf('http://') != 0) {
		url = 'http://'+ htasHost + url;
	}
	
	if (!window.top.htaLoadAndRun || typeof(window.top.htaLoadAndRun) != 'function') {
		alert('windowOpen(): HTA error: Unable to find htaLoadAndRun() in window.top!\n'+ window.top.location);
		
		return(0);
	}
	
	// parse flags to get requested width and height
	var width = false;
	var height = false;
	
	if (flags && typeof(flags) == 'string') {
		var m = flags.match(/width\=(\d+)/);
		
		if (m)
			width = m[1];
		
		m = flags.match(/height\=(\d+)/);
		
		if (m)
			height = m[1];
	}
	
	
	if (target == '_blank' || target == '') {
		target = 'otysw'+ Math.floor(Math.random() * 100000 + 1);
		htname = target;
	}
	else {
		htname = 'otys'+ target;
	}
	
	var hturl='/adm/hta/toolbar/htagen.php?lang=' + 'nl' + '&appname='+ target +'&url='+ encodeURIComponent(url);
	//var hturl=url;
	if (width) hturl = hturl +'&winw='+ width +'&winh='+ height;
	
	try {
		window.top.htaTerminate(target);
	}
	catch (e) {
		//alert('HTA warning: Exception occured while calling htaTerminate('+target+')\n'+e.message+'\n'+e.name);
		//return(0);
	}
	
	if (window['htas_session_id'] == null && window['htas_sid'] != null) {
		htas_session_id = htas_sid;
	}
	
	try {
		window.top.htaLoadAndRun(hturl, htname, htas_session_id);
	}
	catch (e) {
		alert('windowOpen(): HTA error: Exception occured while calling htaLoadAndRun()\n'+ e.message +'\n'+ e.name);
		return(0);
	}
	
	return 1;
}


function htaRunApplication(url, file, appname) {
	//alert('debug: RUN '+file+' '+appname);
	// add session id to urls if session available
	
	// first, we try to kill the application with the same appname
	if (appname == undefined || appname == '_blank' || appname == '') {
		appname = 'otysw'+ Math.floor(Math.random() * 100000 + 1);
	}
	
	try {
		window.top.htaTerminate(appname);
	}
	catch (e) {
		//alert('HTA error: Exception occured while calling htaTerminate()');
		//alert(e.message);
	}
	
	if (window['htas_sid'] != null && url.indexOf('OTYS_BACKOFFICE') == -1) {
		if (url.indexOf('?') == -1) {
			url = url +'?'+ htas_sid;
		}
		else {
			url = url +'&'+ htas_sid;
		}
	}
	// convert local urls to internet urls (backoffice)
	if (url.indexOf('http://') != 0) {
		url = 'http://'+ htasHost + url;
	}
	
	var hturl = '/adm/hta/toolbar/htagen.php?lang=' + 'nl' + '&appname='+ appname +'&url='+ encodeURIComponent(url);
	//var hturl=url;
	
	
	if (window['htas_session_id'] == null && window['htas_sid'] != null) {
		htas_session_id = htas_sid;
	}
	
	try {
		window.top.htaLoadAndRun(hturl, file, htas_session_id);
	}
	catch(e) {
		alert('htaRunApplication(): HTA error: Exception occured while calling htaLoadAndRun()');
		//alert(e.message);
	}
}

function launchToolbar() {
     try {
      var hturl='http://'+htasHost+'/adm/hta/toolbar/toolbargen.php';
      window.top.htaLoadAndRun(hturl, 'OTYS', htas_session_id);
    } catch (e) {
        alert('launchToolBar(): Exception occured while calling htaLoadAndRun()');
        alert(e.message);
    }
}

//{/literal}
{* JMa: if there's a multi-position toggle ("agenda:reminders/tasks/calendar"), don't run the next one right now, in case the user wants another. Wait a bit (if another toggle comes in during the wait, scrap the original command and start waiting for the new one), then run it. The wait is function-specific - e.g. delay_run(refresh) and delay_run(foobar) don't affect each other.
Although it may seem this will slow down the users, adding the timeout to their actions, it actually speeds up the experience - the browser is not waiting for heavy pages which it won't use.
As a side effect, the server doesn't generate those unneeded pages -> lower load.
 *}
//{literal}
var delayed_run_hta = new Array();
var delayed_run_time = 1500; // default delay
function delay_run(fn) {
	delay_cancel(fn);
	delayed_run_hta[fn] = window.setTimeout(fn,delayed_run_time,arguments[0],arguments[1],arguments[2],arguments[3],arguments[4],arguments[5],arguments[6],arguments[7],arguments[8],arguments[9]);
}

function delay_cancel(fn) {
	if (delayed_run_hta[fn] != null) {
		window.clearTimeout(delayed_run_hta[fn]);
	}
}


//{/literal}