/*
Classes:
	CLoginScreen
	CTip
*/

function CLoginScreen()
{
	this.isBuilded = true;
	this.Tip = new CTip();
	this._isAjax = false;

	this._container = document.getElementById("login_screen");
	this._loginError = document.getElementById("login_error");
	
	this._mode = 'standard';
	this._incoming = document.getElementById("incoming");
	this._incProtocol = document.getElementById("inc_protocol");
	this._outgoing = document.getElementById("outgoing");
	this._authentication = document.getElementById("authentication");
	this.LoginForm = document.getElementById("login_form");
	this._loginTable = document.getElementById("login_table");
	this._email = document.getElementById("email");
	this._emailCont = document.getElementById("email_cont");
	this._login = document.getElementById("login");
	this._loginCont = document.getElementById("login_cont");
	this._loginParent = document.getElementById("login_parent");
	this._domain = null;
	this._password = document.getElementById("password");
	this._incServer = document.getElementById("inc_server");
	this._incPort = document.getElementById("inc_port");
	var obj = this;
	this._incProtocol.onchange = function () {
		if (this.value == IMAP4_PROTOCOL)
			obj._incPort.value = IMAP4_PORT;
		else
			obj._incPort.value = POP3_PORT;
	};
	this._outServer = document.getElementById("out_server");
	this._outPort = document.getElementById("out_port");
	this._smtpAuth = document.getElementById("smtp_auth");
	this._signMe = document.getElementById("sign_me");
	this.LoginForm.onsubmit = function () {
		if (!obj.CheckLoginForm())
			return false;
	}
	this.MakeView();
}

CLoginScreen.prototype = {
	MakeView: function ()
	{
		if (this._mode == 'advanced' || AdvancedLogin == '1') {
			this._emailCont.className = '';
			this._email.tabIndex = 1;
			this._loginCont.className = '';
			this._login.tabIndex = 2;
		} else if (HideLoginMode >= 20) {
			this.Tip.Hide('email');
			this._emailCont.className = 'wm_hide';
			this._email.tabIndex = -1;
			this._loginCont.className = '';
			this._login.tabIndex = 2;
		} else if (HideLoginMode >= 10) {
			this.Tip.Hide('login');
			this._emailCont.className = '';
			this._email.tabIndex = 1;
			this._loginCont.className = 'wm_hide';
			this._login.tabIndex = -1;
		}
		if (this._mode == 'advanced' || (HideLoginMode != 21 && HideLoginMode != 23) || AdvancedLogin == '1') {
			if (this._domain != null) {
				this._loginCont.removeChild(this._domain);
				this._domain = null;
				this._loginParent.colSpan = 4;
				this._login.style.width = '220px';
				this._loginTable.className = 'wm_login wm_fixed';
			}
		} else {
			if (this._domain == null) {
				this._loginParent.colSpan = 2;
				this._domain = CreateChild(this._loginCont, 'td');
				this._domain.innerHTML = '@' + DomainOptional;
				this._domain.colSpan = 2;
				this._login.style.width = '150px';
				this._loginTable.className = 'wm_login';
			}
		}
	},
	
	EmailFocus: function ()
	{
		this._email.select();
	},

	LoginFocus: function ()
	{
		if (this._login.value.length == 0 && this._email.value.length != 0)
		{
			this._login.value = this._email.value;
		}
		this._login.select();
	},

	PasswordFocus: function ()
	{
		this._password.select();
	},

	CheckLoginForm: function()
	{
		this.Tip.Hide('');
		if (this._email.value.length == 0 && (this._mode == 'advanced' || HideLoginMode < 20))
		{
			this.Tip.SetMessageText(Lang.WarningEmailBlank);
			this.Tip.SetCoord(this._email);
			this.Tip.Show('email');
			return false;
		} else if (this._login.value.length == 0 && (this._mode == 'advanced' || HideLoginMode != 10 && HideLoginMode != 11)) {
			this.Tip.SetMessageText(Lang.WarningLoginBlank);
			this.Tip.SetCoord(this._login);
			this.Tip.Show('login');
			return false;
		}else if (this._mode == 'advanced' && (this._incServer.value.length == 0 ||
			this._incPort.value.length == 0 || this._outServer.value.length == 0 ||
			this._outPort.value.length == 0)){
				this.Tip.SetMessageText(Lang.WarningServerPortBlank);
				this.Tip.SetCoord(this._incPort);
				if (this._incServer.value.length == 0)
					this.Tip.Show('inc_server');
				else if (this._incPort.value.length == 0)
					this.Tip.Show('inc_port');
				else if (this._outServer.value.length == 0)
					this.Tip.Show('out_server');
				else
					this.Tip.Show('out_port');
				return false;
		}else if (this._mode == 'advanced') {
			var re = /^[0-9]{1,5}$/;
			if (!re.test(this._incPort.value)) {
				this.Tip.SetMessageText(Lang.WarningIncPortNumber + '<br />' + Lang.DefaultIncPortNumber);
				this.Tip.SetCoord(this._incPort);
				this.Tip.Show('inc_port');
				return false;
			} else if (!re.test(this._outPort.value)) {
				this.Tip.SetMessageText(Lang.WarningOutPortNumber + '<br />' + Lang.DefaultOutPortNumber);
				this.Tip.SetCoord(this._outPort);
				this.Tip.Show('out_port');
				return false;
			} else {
				return true;
			}
		} else {
			return true;
		}
	},
	
	sf: function ()
	{
		if (this._isAjax) {
			this.HideError();
			if (this._mode == 'advanced')
				this.AdvancedLogin = '1';
			else
				this.AdvancedLogin = '0';
			var xml = '<param name="action" value="login"/>';
			xml += '<param name="request" value=""/>';
			xml += '<param name="mail_protocol" value="' + this._incProtocol.value + '"/>';
			xml += '<param name="mail_inc_port" value="' + this._incPort.value + '"/>';
			xml += '<param name="mail_out_port" value="' + this._outPort.value + '"/>';
			if (this._smtpAuth.checked) {
				xml += '<param name="mail_out_auth" value="1"/>';
			} else {
				xml += '<param name="mail_out_auth" value="0"/>';
			}
			if (this._signMe.checked) {
				xml += '<param name="sign_me" value="1"/>';
			} else {
				xml += '<param name="sign_me" value="0"/>';
			}
			xml += '<param name="email">' + GetCData(this._email.value) + '</param>';
			xml += '<param name="mail_inc_host">' + GetCData(this._incServer.value) + '</param>';
			xml += '<param name="mail_inc_login">' + GetCData(this._login.value) + '</param>';
			xml += '<param name="mail_inc_pass">' + GetCData(this._password.value) + '</param>';
			xml += '<param name="mail_out_host">' + GetCData(this._outServer.value) + '</param>';
			if (this._mode == 'advanced') {
				xml += '<param name="advanced_login" value="1"/>';
			} else {
				xml += '<param name="advanced_login" value="0"/>';
			}
			this.Xml = '<?xml version="1.0" encoding="utf-8"?><webmail>' + xml + '</webmail>';
			if (Browser.IE) {
				this._email.blur();
				this._login.blur();
				this._password.blur();
				this._incServer.blur();
				this._incPort.blur();
				this._outServer.blur();
				this._outPort.blur();
			}
			this.onSubmit.call(this);
		} else {
			this.LoginForm.submit();
		}
	},
	
	AjaxInit: function (SubmitHandler)
	{
		if (AllowAdvancedLogin) {
			this._loginModeSwitcher = document.getElementById("login_mode_switcher");
			this._loginModeSwitcher.href = '#';
			var obj = this;
			this._loginModeSwitcher.onclick = function(){
				if (obj._mode == 'standard') {
					obj._mode = 'advanced';
					obj._incoming.className = '';
					obj._outgoing.className = '';
					obj._authentication.className = '';
					obj._incProtocol.className = 'wm_advanced_input';
					obj._loginModeSwitcher.innerHTML = Lang.StandardLogin;
					obj.MakeView();
				} else {
					obj._mode = 'standard';
					obj._incoming.className = 'wm_hide';
					obj._outgoing.className = 'wm_hide';
					obj._authentication.className = 'wm_hide';
					obj._incProtocol.className = 'wm_hide';
					obj._loginModeSwitcher.innerHTML = Lang.AdvancedLogin;
					obj.MakeView();
				}
				return false;
			}
		}
		this.onSubmit = SubmitHandler;
		this._isAjax = true;
		var obj = this;
		this.LoginForm.onsubmit = function () {
			return false;
		}
		var submit = document.getElementById("submit");
		submit.onclick = function() {
			if (obj.CheckLoginForm()){
				obj.sf();
			}
		}
	},
	
	ShowError: function (errorDesc)
	{
		this._loginError.className = 'wm_login_error';
		this._loginError.innerHTML = errorDesc;
	},

	HideError: function ()
	{
		this._loginError.className = 'wm_hide';
	},
	
	Show: function ()
	{
		this._container.className = '';
	},
	
	Hide: function ()
	{
		this._container.className = 'wm_hide';
	},
	
	ResizeBody: function ()
	{
	},
	
	ClickBody: function ()
	{
	},
	
	PlaceData: function ()
	{
	}
}

function CTip()
{
	this._container = CreateChild(document.body, 'table');
	this._container.className = 'wm_hide';
	var tr = this._container.insertRow(0);
	var td = tr.insertCell(0);
	td.className = 'wm_tip_arrow';
	this._message = tr.insertCell(1);
	this._message.className = 'wm_tip_info';
	this._base = '';
}

CTip.prototype = {
	SetMessageText: function(text)
	{
		this._message.innerHTML = text;
	},
	
	SetCoord: function(element)
	{
		var bounds = GetBounds(element);
		this._container.style.top = (bounds.Top + bounds.Height/2 - 16) + 'px';
		this._container.style.left = (bounds.Left + bounds.Width - 5) + 'px';
	},
	
	Show: function(base)
	{
		this._base = base;
		this._container.className = 'wm_tip';
	},
	
	Hide: function(base)
	{
		if (this._base == base || this._base == '')
			this._container.className = 'wm_hide';
	}
}