/*
 * Functions:
 *  Init
 *  TryLoginHandler
 *  LoginErrorHandler
 *  LoginHandler
 *  SetCheckingAccountHandler
 *  SetStateTextHandler
 *  SetCheckingFolderHandler
 *  SetRetrievingMessageHandler
 *  SetDeletingMessageHandler
 *  EndCheckMailHandler
 *  CheckEndCheckMailHandler
 * Classes:
 *  CLoginScreen
 *  CTip
 *  CLoginDemoLangClass
 */

var WebMail;
var LoginScreen, LoginDemoLangClass;
var infoObj, infoMessage;
var Browser;
var NetLoader;
var checkMail;

var timerlen = 50;
var slideAniLen = 500;

var timerID = [];
var startTime = [];
var obj = [];
var endHeight = [];
var moving = [];
var dir = [];

/* class.webmail-parts.js can not load because it override functions to work with CheckMail */
var WebMailScripts = [
	'js/class.webmail.js',
	'js/class.html-editor.js',
	'js/class.xml-parsers.js',
	'js/class.screens-parts.js',
	'js/screen.messages-list.js',
	'js/screen.view-message.js',
	'js/screen.messages-list-view.js',
	'js/screen.new-message.js',
	'js/class.variable-table.js',
	'js/screen.user-settings.js',
	'js/screen.common-settings.js',
	'js/screen.accounts-settings.js',
	'js/screen.account-properties.js',
	'js/inc.calendar-settings.js',
	'js/screen.contacts.js',
	'js/screen.view-contact.js'
];
var ScriptToLoadIndex = 0;
var ScriptLoader = new CScriptLoader();

function LoadWebMailScript()
{
	if (ScriptToLoadIndex >= WebMailScripts.length) {
		return;
	}
	ScriptLoader.Load([WebMailScripts[ScriptToLoadIndex]], LoadWebMailScript);
	ScriptToLoadIndex++;
}

function LoginErrorHandler()
{
	infoObj.Hide();
	LoginScreen.ShowError(this.ErrorDesc);
}

function LoginHandler()
{
	var XmlDoc, RootElement, ErrorTag, ErrorDesc, Objects, iCount, i, j, attr, hashParts, jCount, part, infoDiv;
	infoObj.Hide();
	XmlDoc = this.responseXML;
	if (XmlDoc && XmlDoc.documentElement && typeof(XmlDoc) == 'object' && typeof(XmlDoc.documentElement) == 'object') {
		RootElement = XmlDoc.documentElement;
		if (RootElement && RootElement.tagName == 'webmail') {
			ErrorTag = RootElement.getElementsByTagName('error')[0];
			if (ErrorTag) {
				ErrorDesc = ErrorTag.childNodes[0].nodeValue;
				LoginScreen.ShowError((ErrorDesc && ErrorDesc.length > 0) ? ErrorDesc : Lang.ErrorWithoutDesc);
			} else {
				Objects = RootElement.childNodes;
				if (Objects.length == 0) {
					LoginScreen.ShowError(Lang.ErrorEmptyXmlPacket);
				} else {
					iCount = Objects.length;
					for (i = iCount - 1; i >= 0; i--) {
						if (Objects[i].tagName == 'login') {
							hash = '';
							id = -1;
							attr = Objects[i].getAttribute('id_acct');
							if (attr) {
								id = attr - 0;
							}
							hashParts = Objects[i].childNodes;
							jCount = hashParts.length;
							for (j = jCount - 1; j >= 0; j--) {
								part = hashParts[j].childNodes;
								if (part.length > 0 && hashParts[j].tagName == 'hash') {
									hash = part[0].nodeValue;
								}
							}
							if (id != -1 && hash != '') {
								CreateCookie('awm_autologin_data', hash, 14);
								CreateCookie('awm_autologin_id', id, 14);
							}
							checkMail.Start();
							/*for demo*/
							infoDiv = document.getElementById('demo_info');
							if (infoDiv) {
								infoDiv.className = 'wm_hide';
							}
							/*end for demo*/
						}
					}
				}
			}//if (ErrorTag)
		}
		else {
			LoginScreen.ShowError(Lang.ErrorParsing + '<br/>Error code 2.<br/>' + Lang.ResponseText + '<br/>' + this.responseText);
		}//if (RootElement)
	}
	else {
		LoginScreen.ShowError(Lang.ErrorParsing + '<br/>Error code 1.<br/>' + Lang.ResponseText + '<br/>' + this.responseText);
	}//if (XmlDoc)
}

function TryLoginHandler()
{
	infoMessage.innerHTML = Lang.Loading;
	infoObj.Show();
	infoObj.Resize();
	NetLoader.LoadXMLDoc(ActionUrl, 'xml=' + encodeURIComponent(this.Xml), LoginHandler, LoginErrorHandler);
}

function SetCheckingAccountHandler(accountName)
{
	LoginScreen.Hide();
	checkMail.SetAccount(accountName);
}

function SetStateTextHandler(text)
{
	checkMail.SetText(text);
}

function SetCheckingFolderHandler(folder, count)
{
	checkMail.SetFolder(folder, count);
}

function SetRetrievingMessageHandler(number)
{
	checkMail.SetMsgNumber(number);
}

function SetDeletingMessageHandler(number)
{
	checkMail.DeleteMsg(number);
}

function SetUpdatedFolders() {}

function EndCheckMailHandler(error)
{
	document.location = (error == 'session_error') ? LoginUrl + '?error=1' : WebMailUrl;
}

function CheckEndCheckMailHandler() {
	if (checkMail.started) {
		document.location = WebMailUrl;
	}
}


function CTip()
{
	var tr, td;
	this._container = CreateChild(document.body, 'table');
	this._container.className = 'wm_hide';
	tr = this._container.insertRow(0);
	td = tr.insertCell(0);
	CreateChildWithAttrs(td, 'div', [['class', 'wm_tip_arrow']]);
	CreateChildWithAttrs(td, 'div', [['class', 'wm_tip_icon']]);
	this._message = CreateChildWithAttrs(td, 'div', [['class', 'wm_tip_message']]);
	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';
		if (window.RTL) {
			this._container.style.right = (GetWidth() - bounds.Left + 6) + 'px';
		} else {
			this._container.style.left = (bounds.Left + bounds.Width + 6) + 'px';
		}
	},
	
	Show: function (text, element, base)
	{
		this.SetMessageText(text);
		this.SetCoord(element);
		this._base = base;
		this._container.className = 'wm_tip';
	},
	
	Hide: function (base)
	{
		if (this._base == base || base == '') {
			this._container.className = 'wm_hide';
		}
	}
};

function CLoginScreen(submitHandler)
{
	this.isBuilded = true;
	this.Tip = new CTip();
    this.onSubmit = submitHandler;
	this._langsIsShown = false;
	this.LangChanger = null;

	this._container = document.getElementById('login_screen');
	this._loginErrorCont = document.getElementById('login_error');
	this._loginErrorMess = document.getElementById('login_error_message');
	
	this._mode = (AdvancedLogin == 1) ? 'advanced' : '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 = document.getElementById('domain');
	this._password = document.getElementById('password');
	this._incServer = document.getElementById('inc_server');
	this._incPort = document.getElementById('inc_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._language = document.getElementById('language');
	this._body = document.getElementById('mbody');
	this._langs_collection = document.getElementById("langs_collection");
	this._langs_selected = document.getElementById("langs_selected");
	this.Init();
	this.MakeView();
}

CLoginScreen.prototype = {
	Init: function ()
	{
		var obj = this;
		
		/* email */
		this._email.onfocus = function () {
			this.className = 'wm_input_focus';
			obj.EmailFocus();
		};
		this._email.onkeypress = function (ev) {
			if (!isEnter(ev)) {
				obj.Tip.Hide('email');
			}
		};
		/* login */
		this._login.onfocus = function () {
			this.className = 'wm_input_focus';
			obj.LoginFocus();
		};
		this._login.onkeypress = function (ev) {
			if (!isEnter(ev)) {
				obj.Tip.Hide('login');
			}
		};
		
		/* password */
		this._password.onfocus = function () {
			this.className = 'wm_input_focus wm_password_input';
			obj.PasswordFocus();
		};
		this._password.onkeypress = function (ev) {
			if (!isEnter(ev)) {
				obj.Tip.Hide('password');
			}
		};

		this._body.onclick = function (event) { 
			obj.ShowLangs(event);
		};

		this.AjaxInit();

		if (this._incServer == null) { 
			return;
		}
		
		/* incoming mail */
		this._incServer.onkeypress = function (ev) {
			if (!isEnter(ev)) {
				obj.Tip.Hide('inc_server');
			}
		};
		this._incPort.onkeypress = function (ev) {
			if (!isEnter(ev)) {
				obj.Tip.Hide('inc_port');
			}
		};
		this._incProtocol.onchange = function () {
			obj._incPort.value = (this.value == IMAP4_PROTOCOL) ? IMAP4_PORT : POP3_PORT;
		};
		/* ougoing mail */
		this._outServer.onkeypress = function (ev) {
			if (!isEnter(ev)) {
				obj.Tip.Hide('out_server');
			}
		};
		this._outPort.onkeypress = function (ev) {
			if (!isEnter(ev)) {
				obj.Tip.Hide('out_port');
			}
		};
		
		return true;
	},

	ShowLangs: function (e) {
		if (this._langs_collection) {
			e = e ? e : window.event;
			var tgt = window.event ? window.event.srcElement : e.target;
			if (tgt && tgt.parentNode && tgt.parentNode.id == 'langs_selected') {
				if (this._langsIsShown) {
					this._langs_collection.style.display = "none";
					this._langsIsShown = false;
				} else {
					this._langs_collection.style.display = "block";
					this._langsIsShown = true;
				}
			} else {
				this._langs_collection.style.display = "none";
				this._langsIsShown = false;
			}
		}
		return false;
	},

	ChangeLang: function (object) {
		var obj, isRtl;
		if (null == this._language) {
			return;
		}
		obj = this;
		if (object && object.name && object.name.length > 4 && object.name.substr(0, 4) == 'lng_') {
			isRtl = IsRtlLanguage(object.name.substr(4));
			if (window.RTL && !isRtl || !window.RTL && isRtl) {
				document.location = LoginUrl + '?lang=' + object.name.substr(4);
			} else {
				this._language.value = object.name.substr(4);
				document.getElementById('langs_selected').innerHTML = '<span>' + object.innerHTML + '</span><font>&nbsp;</font><span class="wm_login_lang_switcher">&nbsp;</span>';
				CreateCookie('awm_defLang', object.name.substr(4), 635);
				ScriptLoader.Load([LanguageUrl + '?v=' + WmVersion + '&lang=' + object.name.substr(4)], function()
				{
				    obj.ChangeLangProcess.call(obj);
				});
			}
		}
	},

	ChangeLangProcess: function () {
		var obj = this;
		ScriptLoader.Load(['js/_defines_lang.js'], function () {
			obj.LangChanger.Go();
		});
		if (obj._loginModeSwitcher) {
			obj._loginModeSwitcher.innerHTML = (obj._mode == 'standard') ? Lang.AdvancedLogin : Lang.StandardLogin;
		}
	},

	InitLangs: function () {
		var langObj = document.getElementById('lang_LoginInfo');
		this.LangChanger.Register('innerHTML', langObj, 'LANG_LoginInfo', '');

		langObj = document.getElementById('lang_Email');
		this.LangChanger.Register('innerHTML', langObj, 'LANG_Email', ':');

		langObj = document.getElementById('lang_Login');
		this.LangChanger.Register('innerHTML', langObj, 'LANG_Login', ':');

		langObj = document.getElementById('lang_Password');
		this.LangChanger.Register('innerHTML', langObj, 'LANG_Password', ':');

		langObj = document.getElementById('lang_IncServer');
		this.LangChanger.Register('innerHTML', langObj, 'LANG_IncServer', ':');

		langObj = document.getElementById('lang_IncPort');
		this.LangChanger.Register('innerHTML', langObj, 'LANG_IncPort', ':');

		langObj = document.getElementById('lang_OutServer');
		this.LangChanger.Register('innerHTML', langObj, 'LANG_OutServer', ':');

		langObj = document.getElementById('lang_OutPort');
		this.LangChanger.Register('innerHTML', langObj, 'LANG_OutPort', ':');

		langObj = document.getElementById('lang_UseSmtpAuth');
		this.LangChanger.Register('innerHTML', langObj, 'LANG_UseSmtpAuth', '');

		langObj = document.getElementById('lang_SignMe');
		this.LangChanger.Register('innerHTML', langObj, 'LANG_SignMe', '');

		langObj = document.getElementById('submit');
		this.LangChanger.Register('value', langObj, 'LANG_Enter', '');
	},
	
	MakeView: function ()
	{
		var isAdvancedMode = this._mode == 'advanced' || AdvancedLogin == '1';
		if (isAdvancedMode) {
			this._emailCont.className = '';
			this._email.tabIndex = 1;
			this._loginCont.className = '';
			this._login.tabIndex = 2;
			this._email.focus();
		}
		else if (HideLoginMode >= 20) {
			this.Tip.Hide('email');
			this._emailCont.className = 'wm_hide';
			this._email.tabIndex = -1;
			this._loginCont.className = '';
			this._login.tabIndex = 2;
			this._login.focus();
		}
		else if (HideLoginMode >= 10) {
			this.Tip.Hide('login');
			this._emailCont.className = '';
			this._email.tabIndex = 1;
			this._loginCont.className = 'wm_hide';
			this._login.tabIndex = -1;
			this._email.focus();
		} else {
			this._email.focus();
		}
		if (isAdvancedMode || HideLoginMode != 21 && HideLoginMode != 23) {
			this._login.style.width = '224px';
			this._domain.innerHTML = '';
		} else {
			this._login.style.width = '150px';
			this._domain.innerHTML = '&nbsp;@' + DomainOptional;
		}
	},
	
	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 ()
	{
		var val, isAdvancedMode, vEmail, vLogin, vPassword, vIncServer, vIncPort, vOutServer, vOutPort;
		this.Tip.Hide('');
		val = new CValidate();
		isAdvancedMode = this._mode == 'advanced' || AdvancedLogin == '1';
		/* email */
		vEmail = Trim(this._email.value);
		if (val.IsEmpty(vEmail) && (isAdvancedMode || HideLoginMode < 20)) {
			this.Tip.Show(Lang.WarningEmailBlank, this._email, 'email');
			return false;
		}
		if (!val.IsCorrectEmail(vEmail) && (isAdvancedMode || HideLoginMode < 20)) {
			this.Tip.Show(Lang.WarningCorrectEmail, this._email, 'email');
			return false;
		}
		/* login */
		vLogin = Trim(this._login.value);
		if (val.IsEmpty(vLogin) && (isAdvancedMode || HideLoginMode != 10 && HideLoginMode != 11)) {
			this.Tip.Show(Lang.WarningLoginBlank, this._login, 'login');
			return false;
		}
		/* password */
		vPassword = Trim(this._password.value);
		if (val.IsEmpty(vPassword)) {
			this.Tip.Show(Lang.WarningPassBlank, this._password, 'password');
			return false;
		}
		if (this._incServer == null) {
			return true;
		}
		
		/* incoming mail */
		vIncServer = Trim(this._incServer.value);
		if (val.IsEmpty(vIncServer) && (isAdvancedMode)) {
			this.Tip.Show(Lang.WarningIncServerBlank, this._incPort, 'inc_server');
			return false;
		}
		if (!val.IsCorrectServerName(vIncServer) && (isAdvancedMode)) {
			this.Tip.Show(Lang.WarningCorrectIncServer, this._incPort, 'inc_server');
			return false;
		}
		vIncPort = Trim(this._incPort.value);
		if (val.IsEmpty(vIncPort) && (isAdvancedMode)) {
			this.Tip.Show(Lang.WarningIncPortBlank, this._incPort, 'inc_port');
			return false;
		}
		else if (!val.IsPort(vIncPort) && (isAdvancedMode)) {
			this.Tip.Show(Lang.WarningIncPortNumber + '<br />' + Lang.DefaultIncPortNumber, this._incPort, 'inc_port');
			return false;
		}
		/* outgoing mail */
		vOutServer = Trim(this._outServer.value);
		if (val.IsEmpty(vOutServer) && (isAdvancedMode)) {
			this.Tip.Show(Lang.WarningOutServerBlank, this._outPort, 'out_server');
			return false;
		}
		if (!val.IsCorrectServerName(vOutServer) && (isAdvancedMode)) {
			this.Tip.Show(Lang.WarningCorrectSMTPServer, this._outPort, 'out_server');
			return false;
		}
		vOutPort = Trim(this._outPort.value);
		if (val.IsEmpty(vOutPort) && (isAdvancedMode)) {
			this.Tip.Show(Lang.WarningOutPortBlank, this._outPort, 'out_port');
			return false;
		}
		if (!val.IsPort(vOutPort) && (isAdvancedMode)) {
			this.Tip.Show(Lang.WarningOutPortNumber + '<br />' + Lang.DefaultOutPortNumber, this._outPort, 'out_port');
			return false;
		}
		return true;
	},
	
	SendLoginForm: function ()
	{
		if (!this.CheckLoginForm()) {
			return;
		}
		var incServer, incPort, incProtocol, outServer, outPort, outAuth, signMe, xml, lang, getRequestParams;
		incServer = 'localhost';
		incPort = '110';
		incProtocol = '0';
		outServer = 'localhost';
		outPort = '25';
		outAuth = '0';
		signMe = this._signMe.checked ? '1' : '0';
		if (this._incServer != null) {
			incServer = this._incServer.value;
			incPort = this._incPort.value;
			incProtocol = this._incProtocol.value;
			outServer = this._outServer.value;
			outPort = this._outPort.value;
			outAuth = this._smtpAuth.checked ? '1' : '0';
		}
		this.HideError();
		this.AdvancedLogin = (this._mode == 'advanced') ? '1' : '0';
		xml = '<param name="action" value="login" /><param name="request" value="" />';
		xml += '<param name="email">' + GetCData(this._email.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_inc_host">' + GetCData(incServer) + '</param>';
		xml += '<param name="mail_inc_port" value="' + incPort + '"/>';
		xml += '<param name="mail_protocol" value="' + incProtocol + '"/>';
		xml += '<param name="mail_out_host">' + GetCData(outServer) + '</param>';
		xml += '<param name="mail_out_port" value="' + outPort + '"/>';
		xml += '<param name="mail_out_auth" value="' + outAuth + '"/>';
		xml += '<param name="sign_me" value="' + signMe + '"/>';
		if (this._language == null) {
			xml += '<param name="language">' + GetCData('') + '</param>';
		}
		else {
			lang = this._language.value;
			if (lang.length == 0) {
				getRequestParams = ParseGetParams();
				if (getRequestParams.lang && getRequestParams.lang.length > 0) {
					lang = getRequestParams.lang;
				}
			}
			xml += '<param name="language">' + GetCData(lang) + '</param>';
		}
		xml += '<param name="advanced_login" value="' + this.AdvancedLogin + '"/>';
		this.Xml = '<?xml version="1.0" encoding="utf-8"?><webmail>' + xml + '</webmail>';
		if (Browser.IE) {
			this._email.blur();
			this._login.blur();
			this._password.blur();
			if (this._incServer != null) {
				this._incServer.blur();
				this._incPort.blur();
				this._outServer.blur();
				this._outPort.blur();
			}
		}
		this.onSubmit.call(this);
	},
	
	ChangeMode: function ()
	{
		if (this._incServer == null) {
			return;
		}
		this.Tip.Hide('');
		if (this._mode == 'standard') {
			this._mode = 'advanced';
			this._incProtocol.className = 'wm_advanced_input';
			this._loginModeSwitcher.innerHTML = Lang.StandardLogin;
		}
		else {
			this._mode = 'standard';
			this._incProtocol.className = 'wm_hide';
			this._loginModeSwitcher.innerHTML = Lang.AdvancedLogin;
		}
		this.MakeView();
	},
	
	SlideIt: function (objname)
	{
		if (moving[objname]) {
			return;
		}
		moving[objname] = true;
		dir[objname] = (dir[objname] == 'down') ? 'up' : 'down';
		obj[objname] = document.getElementById(objname);
		endHeight[objname] = parseInt(obj[objname].style.height, 10);
		startTime[objname] = (new Date()).getTime();
		if (dir[objname] == 'down') {
			obj[objname].style.height = '1px';
		}
		obj[objname].style.display = 'block';
		if (dir[objname] == 'down') {
			this.ChangeMode();
		}
		timerID[objname] = setInterval('LoginScreen.SlideTick(\'' + objname + '\')', timerlen);
	},
	
	SlideTick: function (objname) {
		var elapsed, d;
		elapsed = (new Date()).getTime() - startTime[objname];
		if (elapsed > slideAniLen) {
			clearInterval(timerID[objname]);
			if (dir[objname] == 'up') {
				obj[objname].style.display = 'none';
				this.ChangeMode();
			}
			obj[objname].style.height = endHeight[objname] + 'px';
			delete(moving[objname]);
			delete(timerID[objname]);
			delete(startTime[objname]);
			delete(endHeight[objname]);
			delete(obj[objname]);
		} else {
			d = Math.round(elapsed / slideAniLen * endHeight[objname]);
			if (dir[objname] == 'up') {
				d = endHeight[objname] - d;
			} else {
				d = endHeight[objname] * Math.sin((d / endHeight[objname]) * Math.PI / 2);
			}
			obj[objname].style.height = d + 'px';
		}
	},
	
	AjaxInit: function ()
	{
		var obj, submit, ahrefs, name, rname, i;
		obj = this;
		if (AllowAdvancedLogin) {
			this._loginModeSwitcher = document.getElementById('login_mode_switcher');
			this._loginModeSwitcher.href = '#';
			dir.advanced_fields = (AdvancedLogin == 0) ? 'up' : 'down';
			this._loginModeSwitcher.onclick = function () {
				obj.SlideIt('advanced_fields');
				return false;
			};
		}
		this.LoginForm.onsubmit = function () {
			return false; 
		};
		submit = document.getElementById('submit');
		submit.onclick = function () { 
			obj.SendLoginForm(); 
		};
		if (NeedToSubmit) {
			this.SendLoginForm();
		}

		this.LangChanger = new CLanguageChanger();
		this.InitLangs();

		if (this._langs_collection) {
			ahrefs = this._langs_collection.getElementsByTagName('A');
			name = rname = '';
			for (i = 0; i < ahrefs.length; i++) {
				name = ahrefs[i].getAttribute('name');
				if (name.length > 4 && name.substr(0, 4) == 'lng_') {
					rname = name.substr(4);
					ahrefs[i].onclick = function () {
						obj.ChangeLang(this);
						return false;
					};
				}
			}
		}
	},

	ShowError: function (errorDesc)
	{
		this._loginErrorCont.className = 'wm_login_error';
		this._loginErrorMess.innerHTML = errorDesc;
	},

	HideError: function ()
	{
		this._loginErrorCont.className = 'wm_hide';
	},
	
	Show: function ()
	{
		this._container.className = '';
	},
	
	Hide: function ()
	{
		this._container.className = 'wm_hide';
	}
};

function CLoginDemoLangClass()
{
	this.currentLang = '';
	this.AddClassName = 'active';
	this.ContOne = document.getElementById('langDemoTop');
	this.ContTwo = document.getElementById('langDemoBottom');
}

CLoginDemoLangClass.prototype = {
	CheckLang: function (name)
	{
		if (this.currentLang == name) {
			return;
		}
		
		childsOne = (this.ContOne) ? this.ContOne.getElementsByTagName('a') : [];
		childsTwo = (this.ContTwo) ? this.ContTwo.getElementsByTagName('a') : [];
		
		for (i = 0; i < childsOne.length; i++) {
			this.uncheckNode(childsOne.item(i));
		}
		for (i = 0; i < childsTwo.length; i++) {
			this.uncheckNode(childsTwo.item(i));
		}
		for (i = 0; i < childsOne.length; i++) {
			this.initNode(childsOne.item(i), name);
		}
		for (i = 0; i < childsTwo.length; i++) {
			this.initNode(childsTwo.item(i), name);
		}
	},
	
	initNode: function (aNode, name)
	{
		if (aNode && aNode.name == name){
			aNode.className = aNode.className + ' active';
			this.currentLang = aNode.name;
		}
	},
	
	uncheckNode: function (aNode)
	{
		if (aNode) {
			aNode.className = aNode.className.replace(/ active/g, '');
		}
	}
};

function Init()
{
	var transport, infoElem, errorCont, errorMess;
	Browser = new CBrowser();
	
	NetLoader = new CNetLoader();
	transport = NetLoader.GetTransport();
	
	if (transport) {
		checkMail = new CCheckMail(1);
	
	    infoElem = document.getElementById('info');
	    infoMessage = document.getElementById('info_message');
	    infoObj = new CInformation(infoElem, 'wm_information');
	    setTimeout('LoadWebMailScript();', 3000);
	
		LoginScreen = new CLoginScreen(TryLoginHandler);
		LoginDemoLangClass = new CLoginDemoLangClass();
	}
	else {
		/*for lite
		errorCont = document.getElementById('login_error');
		errorMess = document.getElementById('login_error_message');
		errorCont.className = 'wm_login_error';
		errorMess.innerHTML = 'Sorry, this web browser is not supported.<br/>' + 
			'We recommend to use one of the following browsers:<br/>' +
			'<a href="http://www.microsoft.com/windows/internet-explorer/default.aspx">Internet Explorer 7</a>, ' +
			'<a href="http://www.firefox.com/">Mozilla Firefox 2</a>, ' +
			'<a href="http://www.apple.com/safari/download/">Safari 2</a>, ' +
			'<a href="http://www.opera.com/">Opera 9</a> ' +
			'or newer versions of these browsers.';
		end for lite*//*for pro*/
		document.location = PdaUrl;
		/*end for pro*/
	}
	
	if (window.defaultInit) {
		window.defaultInit();
	}
}

if (typeof window.JSFileLoaded != 'undefined') {
	JSFileLoaded();
}
