/*------------------------------------------------------------------------------------
------------------------------------------------------------------------------------*/
function rxcLoginManager() {
	this.cfComponent = cfComponent;
	this.cfComponent();
	
	this.url_logout = '/rxc/logoutUserJSON';
	this.url_login = '/rxc/loginUserJSON';
	
	
	this.ajaxRequest = new cfJsonAjax('GET');
	this.ajaxRequest.addListener(this,'AjaxRequestDataAvailable','dataAvailable');
	
	this.enabled = true;
}

p = CF.registerPrototype('rxcLoginManager','cfComponent');


p.errorBox = function () {
	return CF.fkt.getElementById('login_errorbox_root');
}

p.errorTextBox = function () {
	return CF.fkt.getElementById('login_errorbox');
}


p.formvalues = function () {
	var result = Array();
	result['_auth']=CF.fkt.getElementById('login_auth').value;
	result['_auth_type']=CF.fkt.getElementById('login_authtype').value;
	result['_pin']=CF.fkt.getElementById('login_pin').value;
	return result;
}


p.setErrorText = function (text) {
	var error_box = new cfLayer(this.errorBox());
	var error_textbox = new cfDivControl(this.errorTextBox());
	if (text) 
		error_textbox.setHTML(text);
		
	error_box.setFloat(true);
	error_box.setDisplayType('block');
	
	if (text)
		error_box.show(365,350)
	else
		error_box.hide();
}

p.clearError = function () {
	this.setErrorText();
}


p.doLogin = function () {
	if (this.enabled) {
		this.setEnabled(false);
		var values = this.formvalues();
		this.clearPinField();
		values['_action']='dologin';
		values['_id']='rxcLoginManager_login';
		
		/* 
			BUG: Versenden mit der Methode POST dauert sehr lange!!??
		*/
		
		/* send request with getvars */
		this.ajaxRequest.request(this.url_login,values,null);

	}
}


p.doLogout = function () {
	var values = Array();
	values['_action']='dologout';
	values['_id']='rxcLoginManager_logout';
	
	/* send request with getvars */
	this.ajaxRequest.request(this.url_logout,values,null);
}


p.clearPinField = function () {
	CF.fkt.getElementById('login_pin').value = '';
}


p.FieldKeyPress = function (event) {
	this.clearError();
	if (event.keyCode == 13) {
		this.doLogin();
	}
}


p.doAfterLogin = function (redirecturl) {
	/* SUCCESS - reload page */
	// kein Reload, da postdaten vorhanden sein können
	// CF.window.reload();
	if (redirecturl)
		CF.window.goto(redirecturl);
	else
		CF.window.reloadUrl();
}


p.AjaxRequestDataAvailable = function (sender) {
	
	/* -----------------------------------------------------------*/	
	/* LOGIN Response */
	if (sender.header['id']=='rxcLoginManager_login') {
		if (sender.header['result']=='E') {
			/* ERROR - show error message */
			this.setErrorText(sender.header['errortext']);
			this.setEnabled(true);
		} else {
			this.doAfterLogin(sender.header['redirecturl']);
		}
		
	/* -----------------------------------------------------------*/	
	/* LOGOUT Response */
	} else { /* rxcLoginManager_logout */ 
		if (sender.header['result']=='E') {
			/* ERROR - show error message */
			alert('Beim Abmelden ist ein Fehler aufgetreten. ' + sender.header['errortext']);
		} else {
			this.doAfterLogin(sender.header['redirecturl']);
		}
	}
}

p.setEnabled = function (state) {
	this.enabled = state;
	var control; 
	if (control=CF.fkt.getElementById('login_auth'))
		control.disabled = (state)?false:true;
	if (control=CF.fkt.getElementById('login_authtype'))
		control.disabled = (state)?false:true;
	if (control=CF.fkt.getElementById('login_pin'))
		control.disabled = (state)?false:true;
}


CF.loginManager = new rxcLoginManager();



/*------------------------------------------------------------------------------------
------------------------------------------------------------------------------------*/
function rxcMainLoginManager() {
	this.rxcLoginManager = rxcLoginManager;
	this.rxcLoginManager();
}

p = CF.registerPrototype('rxcMainLoginManager','rxcLoginManager');



p.errorBox = function () {
	return CF.fkt.getElementById('mainlogin_errorbox_root');
}

p.errorTextBox = function () {
	return CF.fkt.getElementById('mainlogin_errorbox');
}



p.setErrorText = function (text) {
	var error_box = new cfLayer(this.errorBox());
	var error_textbox = new cfDivControl(this.errorTextBox());
	if (text) 
		error_textbox.setHTML(text);
		
	error_box.setFloat(true);
	error_box.setDisplayType('block');
	
	if (text)
		error_box.show(17,this.errorBox().style.height.replace(/px/,""))
	else
		error_box.hide();
}


p.formvalues = function () {
	var result = Array();
	result['_auth']=CF.fkt.getElementById('mainlogin_auth').value;
	result['_auth_type']=CF.fkt.getElementById('login_dropdown').value;
	result['_pin']=CF.fkt.getElementById('mainlogin_pin').value;
	return result;
}


p.clearPinField = function () {
	CF.fkt.getElementById('mainlogin_pin').value = '';
}

p.setEnabled = function (state) {
	this.enabled = state;
	var control; 
	if (control=CF.fkt.getElementById('mainlogin_auth'))
		control.disabled = (state)?false:true;
	if (control=CF.fkt.getElementById('login_dropdown'))
		control.disabled = (state)?false:true;
	if (control=CF.fkt.getElementById('mainlogin_pin'))
		control.disabled = (state)?false:true;
}



CF.mainLoginManager = new rxcMainLoginManager();






/*------------------------------------------------------------------------------------
------------------------------------------------------------------------------------*/
function rxcPopupManager() {
	this.cfComponent = cfComponent;
	this.cfComponent();
	
	this.popups = Array();
	
}

p = CF.registerPrototype('rxcPopupManager','cfComponent');


p.popup = function (id,url,name,width,height) {
	if (!this.isOpen(id)) {
		this.popups[id] = new rxcPopup(id);
		this.popups[id].open(url,name,width,height);
	} else {
		var window = this.popups[id];
		window.goto(url);
		window.toFront();
	}
}

p.close = function (id) {
	if (this.isOpen(id)) {
		this.popups[id].close();
		this.popups[id] = null;
	}
}

p.isOpen = function (id) {
	return (this.popups[id] && (this.popups[id].isOpen()))?true:false;
}

p.window = function (id) {
	return this.popups[id];
}


CF.popupManager = new rxcPopupManager();


/*------------------------------------------------------------------------------------
------------------------------------------------------------------------------------*/
function rxcPopup(id) {
	this.cfComponent = cfComponent;
	this.cfComponent();
	
	this.id = id;
	this.window = null;
	
}

p = CF.registerPrototype('rxcPopup','cfComponent');



p.open = function (url,name,width,height) {
	this.window = null;
	name = (name)?name:'rxcpopup';
	width = (width)?width:700;
	height = (height)?height:700;
	var args = 'width='+width+',height='+height+',scrollbars';
	this.window = CF.window.window.open(url,name,args)
}


p.close = function () {
	if (this.isOpen()) {
		this.window.close();
		this.window = null;
	}
}

p.goto = function (url) {
	if (this.isOpen()) 
		this.window.location.href = url;
}

p.isOpen = function () {
	return (this.window && (!this.window.closed))?true:false;
}


p.window = function () {
	return this.window;
}

p.toFront = function () {
	if (this.isOpen())
		this.window.focus();
}

