Ext.ns('iPack');
iPack.Menu = Ext.extend(Ext.Toolbar, {
	constructor : function(pTarget, pNLS, pLang) {
		this.target = pTarget;
		this.Lang = pLang;
		this.cfg = {
			items: [/*{
	            text: pNLS.tr('TOPINCLUDE', 'Info'),
	            iconCls: 'info',
	            handler: function(bn, clickEvent){
					this.redirectForm('index.html');
				},
	            id: 'idinfo',
	            scope: this
	        },*/{
	            text: pNLS.tr('TOPINCLUDE', 'Aufnahmeantrag'),
	            iconCls: 'aufnahmeantrag',
	            handler: function(bn, clickEvent){
					this.redirectForm('wohnheimaufnahmeantrag.html');
				},
	            id: 'idaufnahmeantrag',
	            scope: this
	        }
/*	        {
	            text: pNLS.tr('TOPINCLUDE', 'Privatzimmer'),
	            iconCls: 'privatzimmer',
	            handler: function(bn, clickEvent){
					this.redirectForm('privatzimmer.html');
				},
	            id: 'idprivatzimmer',
	            scope: this
	        },
	        {
	            text: pNLS.tr('TOPINCLUDE', 'Privatzimmer Aufnahme'),
	            iconCls: 'privatzimmeraufnahme',
	            handler: function(bn, clickEvent){
					this.redirectForm('privatzimmeraufnahme.html');
				},
	            id: 'idprivatzimmeraufnahme',
	            scope: this
	        },
	        {
	        	xtype: 'tbseparator',
	        	cls: 'tbsep'
	        },
	        {
	            text: pNLS.tr('TOPINCLUDE', 'Verträge'),
	            iconCls: 'vertraege',
	            handler: function(bn, clickEvent){
					this.redirectForm('vertraege.html');
				},
	            id: 'idvertraege',
	            scope: this
	        },
	        {
	            text: pNLS.tr('TOPINCLUDE', 'Stammdaten ändern'),
	            iconCls: 'stammdaten',
	            handler: function(bn, clickEvent){
					this.redirectForm('stammdaten.html');
				},
	            id: 'idstammdaten',
	            scope: this
	        },
	        {
	            text: pNLS.tr('TOPINCLUDE', 'Mitteilung senden'),
	            iconCls: 'mitteilung',
	            handler: function(bn, clickEvent){
					this.redirectForm('mitteilung.html');
				},
	            id: 'idmittteilung',
	            scope: this
	        },
	        {
	            text: pNLS.tr('TOPINCLUDE', 'IB senden'),
	            iconCls: 'imsenden',
	            handler: function(bn, clickEvent){
					this.redirectForm('imsenden.html');
				},
	            id: 'idimsenden',
	            scope: this
	        },
	        {
	            text: pNLS.tr('TOPINCLUDE', 'Übersicht Anfragen'),
	            iconCls: 'anfrageuebersicht',
	            handler: function(bn, clickEvent){
					this.redirectForm('anfrageuebersicht.html');
				},
	            id: 'idanfrageuebersicht',
	            scope: this
	        }*/]
		};

		var logindata = Ext.util.Cookies.get('iPackLoginData');
		if (Ext.isString(logindata) && logindata.length != 0){
			var arr = logindata.split('|||')[0].split('|');
			this.cfg.items[this.cfg.items.length] = {xtype: 'tbfill'};
			this.cfg.items[this.cfg.items.length] = {
					xtype: 'tbtext',
					cls: 'x-panel-header',
					text: pNLS.tr('TOPINCLUDE', 'Sie sind angemeldet als:') + ' ' + arr[2] + ' ' + arr[1]
					};
			this.cfg.items[this.cfg.items.length] = {
		        text: pNLS.tr('TOPINCLUDE', 'Abmelden'),
		        iconCls: 'abmelden',
		        handler: function(bn, clickEvent){
					Ext.util.Cookies.set('iPackLoginData', '');
					Ext.util.Cookies.clear('iPackLoginData');
					this.redirectForm('index.html');
				},
		        id: 'idabmelden',
		        scope: this
		    };
		};

		iPack.Menu.superclass.constructor.call(this, this.cfg);
		if (Ext.isString(pTarget) && pTarget != "")
			this.render(pTarget);
	},
	redirectForm: function(pFormName){
		var p = document.location.pathname.replace(/(.*\/)([^\/]+.html)/, "$1");
		// evtl. vorhandenen \ entfernen
		p = p.replace(/(.*)\/$/, "$1");
		document.location.assign(p + '/' + pFormName);
	}
});

// language-ID aus der aktuellen URL ermitteln
function getLangID(){
	var lang_id = Number(Ext.util.Cookies.get('iPackLangID'));
	return lang_id > 0 ? lang_id : 1;
}

function checkURLLangID(){
	var args = Ext.urlDecode(window.location.search.substring(1).toLowerCase());
	if (args !== undefined && args.lang_id !== undefined && Ext.isNumber(Number(args.lang_id))){
		Ext.util.Cookies.set('iPackLangID', args.lang_id);
	} else if (args !== undefined && args.l !== undefined && Ext.isNumber(Number(args.l))){
		args.l++;
		Ext.util.Cookies.set('iPackLangID', args.l);
	} else {
		switch (window.location.pathname.substr(1, 2)) {
			case 'en':
				Ext.util.Cookies.set('iPackLangID', 2);
				break;
			case 'fr':
				Ext.util.Cookies.set('iPackLangID', 3);
				break;
// 			case 'es':
// 				Ext.util.Cookies.set('iPackLangID', 4);
// 				break;
			default:
				Ext.util.Cookies.set('iPackLangID', 1);
		};
	}
}

function getUrlParam(pParam){
	var args = Ext.urlDecode(window.location.search.substring(1));
	return (args === undefined || args[pParam] === undefined) ? "" : args[pParam];
}

// server base directory (restless-base) aus der URL ermitteln
function getRestlessBase(){
	return window.location.protocol + "//www.studentenwerk-muenchen.de/ipack/server/";
	return window.location.protocol + "//" + window.location.host + window.location.pathname + '/../server/';
}

function getLoginID(){
	var logindata = Ext.util.Cookies.get('iPackLoginData');
	var res = -1;
	if (logindata != '' && logindata != null){
		var p = logindata.split(/\|\|\|/);
		if (p.length == 2){
			var p2= p[0].split(/\|/);
			if (p2.length > 3)
				res = p2[0];
		}
	}
	return res;
}

function checkUserLoggedIn(){
	if (getLoginID() <=0){
		var p = document.location.pathname.replace(/(.*)\/([^\/]+.html)/, "$1|$2").split(/\|/);
		var redirect = p[0] + '/anmeldung.html?retpage=' + p[1];
		Ext.util.Cookies.set('iPackLoginData', '');
		Ext.util.Cookies.clear('iPackLoginData');
		document.location.assign(redirect);
		return false;
	}
	else
		return true;
}

function checkAdminLoggedIn(){
	if (getLoginID() != 0){
		var p = document.location.pathname.replace(/(.*)\/([^\/]+.html)/, "$1|$2").split(/\|/);
		var redirect = p[0] + '/anmeldung.html?admin=1&retpage=' + p[1];
		Ext.util.Cookies.set('iPackLoginData', '');
		Ext.util.Cookies.clear('iPackLoginData');
		document.location.assign(redirect);
		return false;
	}
	else
		return true;
}

function htmlspecialchars(str) {
	if (typeof(str) == "string"){
		str = str.replace(/&/g, "&amp;"); /* must do &amp; first */
		str = str.replace(/"/g, "&quot;");
		str = str.replace(/'/g, "&#039;");
		str = str.replace(/</g, "&lt;");
		str = str.replace(/>/g, "&gt;");
	}
	return str;
}

function createpanels(ptarget, pnlssubjects, pcreatefunct){
	Ext.QuickTips.init();
	checkURLLangID();
	var lang_id = getLangID();
	MyLib.doJsonRequest(null, {
		url : getRestlessBase() + 'nlsstrings/' + lang_id
				+ '/get?FORMS=' + 'ALLGEMEIN|TOPINCLUDE|' + pnlssubjects,
		method : 'GET',
		handlesuccess : function(pcfgdata) {
			if (pcfgdata.error == '') {
				var nlsdata = pcfgdata.data;
				nlsdata.tr =  function(pName, pVal){
					pName = pName.toUpperCase();
					if (Ext.isDefined(pVal) && Ext.isDefined(nlsdata[lang_id]) &&
						Ext.isDefined(nlsdata[lang_id][pName]) && Ext.isDefined(nlsdata[lang_id][pName][pVal]) &&
						nlsdata[lang_id][pName][pVal] != ''){
						return nlsdata[lang_id][pName][pVal];
					}
					else
						return pVal;
				};
				if (Ext.fly('ipacknavigator') != null)
					new iPack.Menu('ipacknavigator', nlsdata, lang_id);
				if (Ext.fly('langcombo') != null)
					new MyLib.LangComboBox('langcombo');
				if (Ext.fly(ptarget) != null)
					pcreatefunct(ptarget, nlsdata, lang_id);
			} else {
				Ext.MessageBox.alert('Fehler beim Laden der NLS-Informationen (' + pcfgdata.error + ')');
			}
		}
	});
}

iPack.Captcha = Ext.extend(Ext.BoxComponent, {
  constructor: function(config){
    var cfg = {
        autoEl: {
          tag:'img',
          id: 'activateCodeImg',
          title : 'Klicken, um neues Bild zu erhalten',
          src: String.format("{0}captcha/getimage?width={1}&height={2}", getRestlessBase(), config.width, config.height)
        },
        listeners : {
            'render': function (){
                      var curr = Ext.get(this.autoEl.id);
                      curr.on('click',
                              function (){
                                var curr = Ext.get(this.autoEl.id);
                                curr.slideOut('b', {callback: function(){
                                    curr.dom.src = this.autoEl.src + '&_dc=' + Math.random();
                                    curr.slideIn('t');
                                }, scope: this},this);
                              },
                              this);
                      },
             scope: this}
    };
    Ext.apply(cfg, config);

    iPack.Captcha.superclass.constructor.call(this, cfg);
  }
});
Ext.reg('x_captcha', iPack.Captcha);

