/*
auth.js
*/

//Класс обработки пары логин-пароль
function AUTH(IsError){
	if(IsError!=undefined)
		this.IsError=IsError;
}

//Переменные и методы класса Auth
with(AUTH){
	prototype.IsError=false;
	prototype.LoginField=null;
	//Флаг: установлено ли поле логина в первоначальное положение
	prototype.IsLoginDefault=true;
	prototype.PasswordField=null;
	prototype.PasswordSpan=null;
	//Флаг: установлено ли поле пароля в первоначальное положение
	prototype.IsPasswordDefault=true;
	prototype.AuthForm=null;
	
	prototype.isMSIE=false;
	prototype.textElm;
	prototype.passwordElm;
	
	//Метод присваивания полям их идентикифакторы
	prototype.Initialize=function(){
		this.LoginField=document.getElementById('auth_login');
		this.AuthForm=document.getElementById('auth');
		
		//События для поля логина
		if(this.LoginField){
			//фокус
			this.LoginField.onclick=LoginFieldFocus;
			this.LoginField.onfocus=LoginFieldFocus;
			//Удаление фокуса
			this.LoginField.onchange=LoginFieldBlur;
			this.LoginField.onblur=LoginFieldBlur;
		}

		this.InitializePassword();
	}
	
	//Метод присваивания полю пароля идентификатора
	prototype.InitializePassword=function(){
		this.PasswordField=document.getElementById('auth_password');
		this.PasswordSpan=document.getElementById('password_span');
		
		//События для поля пароля
		if(this.PasswordField && this.PasswordSpan){
			//Фокус
			this.PasswordField.onclick=PasswordFieldFocus;
			this.PasswordField.onfocus=PasswordFieldFocus;
			//Удаление фокуса
			this.PasswordField.onchange=PasswordFieldBlur;
			this.PasswordField.onblur=PasswordFieldBlur;
		}
	}
	
	//Метод определения правильности идентификации обоих полей
	prototype.IsExist=function(){
		if(!this.LoginField || !this.PasswordField){
			//Если включено сообщение об ошибке, тогда выводим сообщение
			if(this.IsError)
				alert('Не найдены HTML-идентификаторы полей логина и пароля.');
			return false;
		}
		return true;
	}
	
	//Метод первоначальных установок свойств полей
	prototype.OnLoad=function(){
		this.Initialize();
		if(!this.IsExist)
			return;
		//Для MSIE
		this.isMSIE=/*@cc_on!@*/false;

		this.SetLoginDefault();
		this.SetPasswordDefault();
		this.LoginField.disabled=false;
		this.PasswordField.disabled=false;
	}
	
	//Установка первоначальных значений полю логина
	prototype.SetLoginDefault=function(){
		this.LoginField.style.color='#c0c0c0';
		this.LoginField.value='E-mail';
		this.IsLoginDefault=true;
	}
	prototype.UnsetLoginDefault=function(){
		this.LoginField.value='';
		this.LoginField.style.color='black';
		this.IsLoginDefault=false;
	}
	
	//Установка первоначальных значений полю пароля
	prototype.SetPasswordDefault=function(){
		if(this.isMSIE){
			this.PasswordSpan.innerHTML='<input id="auth_password" type="text" tabindex="2" name="password" />';
			this.InitializePassword();
		}
		else{
			this.PasswordField.type='text';
		}
		this.PasswordField.style.color='#c0c0c0';
		this.PasswordField.value='Пароль';
		this.IsPasswordDefault=true;
	}
	prototype.UnsetPasswordDefault=function(){
		if(this.isMSIE){
			this.PasswordSpan.innerHTML='<input id="auth_password" type="password" tabindex="2" name="password" />';
			this.InitializePassword();
			this.PasswordField.select();
			this.PasswordField.focus();
		}
		else {
			this.PasswordField.type='password';
		}
		this.PasswordField.style.color='black';
		this.PasswordField.value='';
		this.IsPasswordDefault=false;
	}
}

Auth=new AUTH();

//Установка серого цвета для поля логин и пароль, а для пароля, кроме того, установка поля в режим текста
window.onload=function(){
	if(AuthError)
		alert(AuthError);
	Auth.OnLoad();
}

//Обработка клика на поле логина
function LoginFieldFocus(){
	//Если поле установлено в положение первоначальное
	if(Auth.IsLoginDefault)
		Auth.UnsetLoginDefault();
}

//Обработка изменения поля логина
function LoginFieldBlur(){
	//Если поле не в первоначальном режиме и при этом оно пустое, тогда устанавливаем первоначальный
	if(!Auth.IsLoginDefault && !Auth.LoginField.value)
		Auth.SetLoginDefault();
}

//Обработка клика на поле пароля
function PasswordFieldFocus(){
	//Если поле установлено в положение первоначальное
	if(Auth.IsPasswordDefault)
		Auth.UnsetPasswordDefault();
}

//Обработка изменения поля пароля
function PasswordFieldBlur(){
	//Если поле не в первоначальном режиме и при этом оно пустое, тогда устанавливаем первоначальный
	if(!Auth.IsPasswordDefault && !Auth.PasswordField.value)
		Auth.SetPasswordDefault();
}

