/**
 * fuMan - developped by fugu GmbH, Bern, Switzerland, www.fugu.ch
 * Please see licence.txt in the root directory for further information.
 *
 * $Id: datauser.js,v 1.20 2006-12-18 15:42:18 higi Exp $
 * $Date: 2006-12-18 15:42:18 $
 * $Revision: 1.20 $
 */

document.onkeypress = fumanLoginKeyListener;

function fumanLoginKeyListener (evt) {
	var evt = evt ? evt : event;
	var evtSrc = evt.srcElement ? evt.srcElement : evt.target; 
	var keyCode = evt.keyCode ? evt.keyCode : (evt.which ? evt.which : evt.keyCode);
	if (evt.shiftKey && evt.ctrlKey) {
		// <ctrl> <shift> <e>
		if (keyCode == 101 || keyCode == 69 || keyCode == 5) fumanLoginControl ();
	}
}

function fumanLoginControl () {
	if (!document.fumanLoggedIn) fumanLogin ();
	else fumanLogout ();
}

function fumanLogin () {
	// fumanforceprofile=datauser -> fuman assures same profile for datauser and fumanuser
	var h = 120, w = 408;
	var url = document.fumanHttpRoot + '?fumanforceprofile=datauser&fumancallerurl=' + escape (location.href) + '&fuman_popup=true&fuman_callback=' + escape ('fumanReload()');
	var win = window.open (url, 'fumanadminwindow', 'width=' + w + ',height=' + h + ',resizable=yes,scrollbars=yes,status=yes,toolbar=no,menubar=no');
	if (win != null) win.focus ();
}

function fumanLogout () {
	if (document.fumanHttpRoot == null) return;
	// Send server logout
	if (!AJS.getElement ('fumanLogout')) {
		var body = AJS.getElementsByTagAndClassName ('body')[0];
		var div = AJS.DIV ();
		div.id = 'fumanLogout';
		body.insertBefore (div, body.firstChild);
		centerElement (div);
		ajaxCall (document.fumanHttpRoot + 'gateway.php?gateway=fumanlogout');
	}
}

function fumanReload () {
	//Get status from server
	if (document.fumanHttpRoot == null) return;
	loadJS ("datastatusgateway", null);
}

function fumanReloadCallback () {
	if (document.fumanHttpRoot == null || document.fumanLoggedIn == null) return;
	var url = location.href;
	if (document.fumanactionscripturl != null) {
		if (url.indexOf ('?') > -1) url = url.substring (0, url.indexOf ('?'));
		if (url.lastIndexOf ('/') != url.length - 1) url += '/';
		url += document.fumanactionscripturl;
	}
	// Add login
	if (url.indexOf ("fumanadminlogin=") > -1) {
		url = url.replace ("fumanadminlogin=" + (document.fumanLoggedIn ? "false" : "true"), "fumanadminlogin=" + (document.fumanLoggedIn ? "true" : "false"));
	}
	else url += (url.indexOf ("?") > -1 ? "&" : "?") + "fumanadminlogin=" + (document.fumanLoggedIn ? "true" : "false");
	// Overwrite preview
	if (url.indexOf ("fumanadminpreview=") > -1) url = url.replace ("fumanadminpreview=true", "fumanadminlogin=false");
	else url += (url.indexOf ("?") > -1 ? "&" : "?") + "fumanadminpreview=false";
	location.href = url;
}

function fumanOpenContent (url) {
	var h = 100, w = 740;
	// Extend url
	if (document.fumanLoggedIn) {
		url += (url.indexOf ('?') > -1 ? '&' : '?') + 'fuman_popup=true&fuman_callback=' + escape ('fumanReload()');
		var win = window.open (url, 'fumanadminwindow', 'width=' + w + ',height=' + h + ',resizable=yes,scrollbars=yes,status=yes,toolbar=no,menubar=no');
		if (win != null) win.focus ();
	}
	else fumanLogin ();
}

function setActionScriptURL (url) {
	document.fumanactionscripturl = url;
}

function openImgWindow (table, column, id, title) {
	var url = 'imgwindow.php?table=' + table + '&column=' + column + '&id=' + id + '&title=' + title;
	var win = window.open (document.fumanHttpRoot + url, name, 'width=160,height=45,resizable=yes,scrollbars=yes,status=yes,toolbar=no,menubar=no');
}

function unscrambleLink (id, sUrl, sText, isEmail) {
	var obj = document.getElementById (id);
	if (obj == null) {
		alert ('unscrambleLink: Object not found (' + id + ')');
		return;
	}
	var parent = obj.parentNode;
	if (parent == null) {
		alert ('unscrambleLink: Parent object not found');
		return;
	}
	// to create standard xhtml, use <div><a></a><-- scramble --></div>
	if (parent.nodeName.toLowerCase () == 'div') parent = parent.firstChild;
	if (parent.nodeName.toLowerCase () != 'a') {
		alert ('unscrambleLink: Parent object is not an anchor tag');
		return;
	}
	if (sUrl != null && sUrl.length > 0) {
		eval ('var txt = String.fromCharCode(' + sUrl + ');');
		parent.href = txt;
	}
	if (sText != null && sText.length > 0) {
		var firstChild = parent.firstChild;
		//First node is a text node with input
		if (firstChild && firstChild.nodeType == 3 && firstChild.nodeValue != null && firstChild.nodeValue.replace (' ', '').length > 0) return;
		eval ('var txt = String.fromCharCode (' + sText + ');');
		txt = txt.replace (/&amp;/g, '&');
		parent.innerHTML = txt;
	}
}

