function AuthForm(authDivName, config){
	this.authDivName = authDivName;
	this.authFormDiv = Ext.get(this.authDivName);
	
	defConfig = {
		wrongEnterColor: 'FF0000',
		wrongEnterAnim: {
			attr: 'background-color',
			duration: 1
		},
		authBtnText: 'Войти',
		authBtnCls: 'loginBtn',
		regPageLink: ''
	}
	
	Ext.apply(this, config, defConfig);
	
	defRequestConfig = {
		scope: this,
		elScope: this.loginDiv,
		params: {},
		success: this.loginSuccessHandler,
		failure: failureHandler
	}
	
	this.loginRequestConfig = {};
	this.loginRequestConfig = Ext.apply(this.loginRequestConfig, config.loginRequestConfig, defRequestConfig);
	
	defLogoutRequestConfig = {}
	defLogoutRequestConfig = Ext.apply(defLogoutRequestConfig, defRequestConfig);
	defLogoutRequestConfig.elScope = this.userInfoDiv;
	defLogoutRequestConfig.success = this.logoutSuccessHandler;
	
	this.logoutRequestConfig = {};
	this.logoutRequestConfig = Ext.apply(this.logoutRequestConfig, config.logoutRequestConfig, defLogoutRequestConfig);
		
	this.curTip = null;
	
	delete config;
}

//Показать слой для входа
AuthForm.prototype.showAuthDiv = function(){
	if (!this.loginDiv) {
		this.loginDiv = Ext.DomHelper.append(this.authFormDiv, {
			id: 'loginDiv',
			cls: 'authDiv authForm'
		}, true);
		Ext.DomHelper.append(this.loginDiv, {
			html:['<label>Логин</label><input id="login" type="text"><br>','<label>Пароль</label><input id="pass" type="password">'] .join('')
		});
		this.loginField = Ext.get('login');
		this.passField = Ext.get('pass');
		
		this.authBtn = new Ext.Button({
			text: this.authBtnText,
			cls: this.authBtnCls,
			handler: this.loginBtnHandler,
			scope: this
		});
		
		this.authBtn.render(this.loginDiv);
		
		Ext.DomHelper.append(this.loginDiv, {
			html:['<div style="text-align:right"><a ext:targEl="content" href="',this.regPageLink,'" id="reg">Регистрация</a><div>'] .join('')
		});
		
		this.loginDiv.addKeyListener(13, this.loginBtnHandler, this);
		
		this.authFormDiv.setVisible(true);
		//this.authFormDiv.addKeyListener(13, function() {alert(1)});
	}
}

//Удалить слой для входа
AuthForm.prototype.removeAuthDiv = function(){
	if (this.loginDiv) {
		this.loginDiv.remove();
		delete this.loginDiv;
	}
}

//Показать слой информации о пользователе
AuthForm.prototype.showUserInfoDiv = function(userInfo){
	if (!this.userInfoDiv) {
		this.userInfoDiv = Ext.DomHelper.append(this.authFormDiv, {
			id: 'userInfoDiv',
			cls: 'authDiv userInfo'
		}, true);
		
		Ext.DomHelper.append(this.userInfoDiv, {
			html:['<label>Здравствуйте, ',userInfo.name,'!</label><br>','<label>Ваш баланс: <strong id="userBalance">',userInfo.balance,'</strong> тенге</label><br>','<a href="#" id="addBalance">Пополнить счёт</a><br>','<a href="#" id="logout" onclick="return false;">Выход</a>'] .join('')
		});
		
		this.logoutLink = Ext.get('logout');
		this.logoutLink.on('click', function(){
			this.userInfoDiv.request(this.logoutRequestConfig);
		}, this);
		
		this.addBalanceLink = Ext.get('addBalance');
		this.addBalanceLink.on('click', addBalance, this, {
			preventDefault: true
		});
		
		this.authFormDiv.setVisible(true);
	}
}

//Удалить слой информации о пользователе
AuthForm.prototype.removeUserInfoDiv = function(){
	if (this.userInfoDiv) {
		this.userInfoDiv.remove();
		delete this.userInfoDiv;
	}
}

//Проверка поля на заполненность
AuthForm.prototype.checkField = function(field, msg){
	if (field.dom.value == '') {
		field.highlight(this.wrongEnterColor, this.wrongEnterAnim);
		
		this.showTip(field, msg);
		return false;
	}
	
	return true;
}

//Показ подсказки
AuthForm.prototype.showTip = function(el, msg, showTime){
	el.focus();
	
	if (this.curTip) 
		this.curTip.destroy()
	
	this.curTip = new Ext.Tip({
		html: msg
	});
	
	this.curTip.showAt([el.getX() + el.getWidth() * 2 / 3, el.getY() + el.getHeight() / 2]);
	showTime = showTime || 2000;
	this.curTip.hide.defer(showTime, this.curTip);
}

//Обработчик нажатия кнопки входа
AuthForm.prototype.loginBtnHandler = function(){
	if (!this.checkField(this.loginField, 'Укажите Ваш логин')) 
		return;
	
	if (!this.checkField(this.passField, 'Введите Ваш пароль')) 
		return;
	
	this.loginRequestConfig.params.login = this.loginField.dom.value;
	this.loginRequestConfig.params.pass = this.passField.dom.value;
	
	this.loginDiv.request(this.loginRequestConfig);
}

AuthForm.prototype.authAnim = function(userInfo){
	updateContentDiv()
	
	this.authFormDiv.pause(.1).slideOut('t', {
		callback: function(){
			this.removeAuthDiv();
			this.showUserInfoDiv(userInfo);
			this.authFormDiv.slideIn();
		},
		scope: this
	});
}

//Обработчик успешного обращения к серверу при авторизации
AuthForm.prototype.loginSuccessHandler = function(response){
	response = Ext.decode(response.responseText);
	
	if (response.login == true) {
		//успешный вход
		this.authAnim(response.userInfo);
	}
	else 
		if (response.bann > 0) {
			//Забанен
			showErrorMsg({
				msg: 'Превышено количество допустимых попыток входа.<br>Попробуйте позднее',
				animEl: this.authFormDiv
			});
		}
		else {
			this.loginDiv.highlight(this.wrongEnterColor, {
				duration: 2
			});
			this.loginField.focus();
			
			if (response.msg) {
				//Есть сообщение от сервера
				this.showTip(this.loginDiv, response.msg, 4000);
			}
		}
}

//Обработчик успешного обращения к серверу при завершении сессии (выходе)
AuthForm.prototype.logoutSuccessHandler = function(){
	updateContentDiv()
	this.authFormDiv.pause(.1).slideOut('t', {
		callback: function(){
			this.removeUserInfoDiv();
			this.showAuthDiv();
			this.authFormDiv.slideIn();
		},
		scope: this
	});
}