/*
 * Classes, prototypes:
 *  CBrowser
 *  CPopupMenu
 *  CPopupMenus
 *  CSearchForm
 *  CError
 *  CReport
 *  ReportPrototype
 *  CInformation
 *  CFadeEffect
 *  Validator
 *  CPageSwitcher
 * 
 *  CCheckMail
 * 
 *  CScriptLoader
 *  CNetLoader
 *  CDictionary
 *
 *  CLanguageChanger
 *  CTip
 */

function CBrowser()
{
	this.Init = function ()
	{
		var i, len;
		len = this.Profiles.length;
		for (i = 0; i < len; i++) {
			if (this.Profiles[i].Criterion) {
				this.Name = this.Profiles[i].Id;
				this.Version = this.Profiles[i].Version();
				this.Allowed = this.Version >= this.Profiles[i].AtLeast;
				break;
			}
		}
		this.IE = (this.Name == 'Microsoft Internet Explorer');
		this.Opera = (this.Name == 'Opera');
		this.Mozilla = (this.Name == 'Mozilla' || this.Name == 'Firefox' || this.Name == 'Netscape' || this.Name == 'Chrome');
		this.Safari = (this.Name == 'Safari');
		this.Chrome = (this.Name == 'Chrome');
		this.Gecko = (this.Opera || this.Mozilla);
	};

	this.Profiles = [
		{
			Id: 'Opera',
			Criterion: window.opera,
			AtLeast: 8,
			Version: function () {
				var start, end, r, start1, start2;
				r = navigator.userAgent;
				start1 = r.indexOf('Opera/');
				start2 = r.indexOf('Opera ');
				if (-1 == start1) {
					start = start2 + 6;
					end = r.length;
				} else {
					start = start1 + 6;
					end = r.indexOf(' ');
				}
				r = parseFloat(r.slice(start, end));
				return r;
			}
		},
		{
			Id: 'Chrome',
			Criterion:
			(
				(navigator.appCodeName.toLowerCase() == 'mozilla') &&
				(navigator.appName.toLowerCase() == 'netscape') &&
				(navigator.product.toLowerCase() == 'gecko') &&
				(navigator.userAgent.toLowerCase().indexOf('chrome') != -1)
			),
			AtLeast: 0,
			Version: function () {
				return parseFloat(navigator.userAgent.split('Chrome/').reverse().join('Chrome/'));
			}
		},
		{
			Id: 'Safari',
			Criterion:
			(
				(navigator.appCodeName.toLowerCase() == 'mozilla') &&
				(navigator.appName.toLowerCase() == 'netscape') &&
				(navigator.product.toLowerCase() == 'gecko') &&
				(navigator.userAgent.toLowerCase().indexOf('safari') != -1)
			),
			AtLeast: 1.2,
			Version: function () {
				var r = navigator.userAgent;
				return parseFloat(r.split('Version/').reverse().join(' '));
			}
		},
		{
			Id: 'Firefox',
			Criterion:
			(
				(navigator.appCodeName.toLowerCase() == 'mozilla') &&
				(navigator.appName.toLowerCase() == 'netscape') &&
				(navigator.product.toLowerCase() == 'gecko') &&
				((navigator.userAgent.toLowerCase().indexOf('firefox') != -1) ||
				(navigator.userAgent.toLowerCase().indexOf('iceweasel') != -1))
			),
			AtLeast: 1,
			Version: function () {
				var userAgent = navigator.userAgent.toLowerCase();
				if (userAgent.indexOf('firefox/') != -1) {
					return parseFloat(userAgent.split('firefox/').reverse().join('firefox/'));
				}
				if (userAgent.indexOf('iceweasel/') != -1) {
					return parseFloat(userAgent.split('iceweasel/').reverse().join('iceweasel/'));
				}
				return 0;
			}
		},
		{
			Id: 'Netscape',
			Criterion:
			(
				(navigator.appCodeName.toLowerCase() == 'mozilla') &&
				(navigator.appName.toLowerCase() == 'netscape') &&
				(navigator.product.toLowerCase() == 'gecko') &&
				(navigator.userAgent.toLowerCase().indexOf('netscape') != -1)
			),
			AtLeast: 7,
			Version: function () {
				var r = navigator.userAgent.split(' ').reverse().join(' ');
				r = parseFloat(r.slice(r.indexOf('/') + 1, r.indexOf(' ')));
				return r;
			}
		},
		{
			Id: 'Mozilla',
			Criterion:
			(
				(navigator.appCodeName.toLowerCase() == 'mozilla') &&
				(navigator.appName.toLowerCase() == 'netscape') &&
				(navigator.product.toLowerCase() == 'gecko') &&
				(navigator.userAgent.toLowerCase().indexOf('mozilla') != -1)
			),
			AtLeast: 1,
			Version: function () {
				var r = navigator.userAgent;
				return parseFloat(r.split('Firefox/').reverse().join('Firefox/'));
			}
		},
		{
			Id: 'Microsoft Internet Explorer',
			Criterion:
			(
				(navigator.appName.toLowerCase() == 'microsoft internet explorer') &&
				(navigator.appVersion.toLowerCase().indexOf('msie') !== 0) &&
				(navigator.userAgent.toLowerCase().indexOf('msie') !== 0) &&
				(!window.opera)
			),
			AtLeast: 5,
			Version: function () {
				var r = navigator.userAgent.toLowerCase();
				r = parseFloat(r.slice(r.indexOf('msie') + 4, r.indexOf(';', r.indexOf('msie') + 4)));
				return r;
			}
		}
	];

	this.Init();
}

function CPopupMenu(popup_menu, popup_control, menu_class, popup_move, popup_title, move_class, move_press_class, title_class, title_over_class)
{
	this.popup = popup_menu;
	this.control = popup_control;
	this.move = popup_move;
	this.title = popup_title;
	this.menu_class = menu_class;
	this.move_class = move_class;
	this.move_press_class = move_press_class;
	this.title_class = title_class;
	this.title_over_class = title_over_class;
	this.disable = false;
}

function CPopupMenus()
{
	this.items = [];
	this.isShown = 0;
}

CPopupMenus.prototype = {
	getLength: function ()
	{
		return this.items.length;
	},
	
	addItem: function (popup_menu)
	{
		this.items.push(popup_menu);
		this.hideItem(this.getLength() - 1);
	},
	
	showItem: function (item_id)
	{
		this.hideAllItems();
		var item = this.items[item_id];
		var bounds = GetBounds(item.move);
		if (!window.RTL) {
			item.popup.style.left = bounds.Left + 'px';
		}
		item.popup.style.top = bounds.Top + bounds.Height + 'px';

		item.popup.className = item.menu_class;
		if (item.title_class && item.title_class != '') {
			item.control.className = item.title_class;
			item.title.className = item.title_class + (item.disable ? ' wm_toolbar_item_disabled' : '');
		}
		if (item.move_press_class && item.move_press_class != '')
			item.move.className = item.move_press_class;
		var obj = this;
		item.control.onclick = function () {
			obj.hideItem(item_id);
		};
		var borders = 1;
		if (item.title_over_class != '') {
			item.control.onmouseover = function () {};
			item.control.onmouseout = function () {};
			item.title.onmouseover = function () {};
			item.title.onmouseout = function () {};
			borders = 2;
		}
		this.isShown = 2;
		item.popup.style.width = 'auto';
		var pOffsetWidth = item.popup.offsetWidth;
		var cOffsetWidth = item.control.offsetWidth;
		var tOffsetWidth = (item.control == item.title) ? 0 : item.title.offsetWidth;
		item.popup.style.width = (pOffsetWidth < (cOffsetWidth + tOffsetWidth - borders)) ?
			(cOffsetWidth + tOffsetWidth - borders) + 'px' : (pOffsetWidth + borders) + 'px';

		/* rtl */
		if (window.RTL) {
			item.popup.style.left = (bounds.Left + bounds.Width - item.popup.offsetWidth) + 'px';
		}

		item.popup.style.height = 'auto';
		var pOffsetHeight = item.popup.offsetHeight;
		var height = GetHeight();
		if (pOffsetHeight > height * 2 / 3) {
			item.popup.style.height = Math.round(height * 2 / 3) + 'px';
			item.popup.style.overflowY = 'auto';
		} else {
			item.popup.style.overflowY = 'hidden';
		}
	},
	
	hideItem: function (item_id)
	{
		var item = this.items[item_id];
		item.popup.className = 'wm_hide';
		if (item.move_class && item.move_class != '' && item.move.className != 'wm_hide')
			item.move.className = item.move_class;
		var obj = this;
		item.control.onclick = function () {
			obj.showItem(item_id);
		};
		if (item.title_over_class != '') {
			item.control.onmouseover = function () {
				item.title.className = item.title_over_class + (item.disable ? ' wm_toolbar_item_disabled' : ''); 
				item.control.className = item.title_over_class;
			};
			item.control.onmouseout = function () {
				item.title.className = item.title_class + (item.disable ? ' wm_toolbar_item_disabled' : ''); 
				item.control.className = item.title_class; 
			};
			item.title.onmouseover = function () {
				item.title.className = item.title_over_class + (item.disable ? ' wm_toolbar_item_disabled' : ''); 
			};
			item.title.onmouseout = function () {
				item.title.className = item.title_class + (item.disable ? ' wm_toolbar_item_disabled' : ''); 
			};
		}
	},
	
	hideAllItems: function ()
	{
		for (var i = this.getLength() - 1; i >= 0; i--) {
			this.hideItem(i);
		}
		this.isShown = 0;
	},
	
	checkShownItems: function ()
	{
		if (this.isShown == 1) {
			this.hideAllItems();
		}
		if (this.isShown == 2) {
			this.isShown = 1;
		}
	}
};

function CSearchForm(BigSearchForm, SmallSearchForm, downButton, upButton, bigFormId, bigLookFor, smallLookFor)
{
	this.form = BigSearchForm;
	this._bigFormId = bigFormId;
	this.smallForm = SmallSearchForm;
	this.downButton = downButton;
	this.upButton = upButton;
	this._bigLookFor = bigLookFor;
	this._smallLookFor = smallLookFor;
	this.isShown = 0;
	this.shown = false;
	this._searchIn = null;
}

CSearchForm.prototype = 
{
	Show: function ()
	{
		this.shown = true;
		this.isShown = 0;
		this.smallForm.className = 'wm_toolbar_search_item';
		var obj = this;
		this.downButton.onclick = function () {
			obj.ShowBigForm(); 
		};
		this.ShowDownButton();
		this.form.className = 'wm_hide';
		this.HideUpButton();
	},
	
	ShowDownButton: function ()
	{
		var obj = this;
		this.downButton.onmouseover = function () {
			obj.downButton.className = 'wm_toolbar_search_item_over';
			obj.smallForm.className = 'wm_toolbar_search_item_over';
		};
		this.downButton.onmouseout = function () {
			obj.downButton.className = 'wm_toolbar_search_item';
			obj.smallForm.className = 'wm_toolbar_search_item';
		};
		this.downButton.className = 'wm_toolbar_search_item';
	},
	
	HideDownButton: function ()
	{
		this.downButton.onmouseover = function () {};
		this.downButton.onmouseout = function () {};
		this.downButton.className = 'wm_hide';
	},
	
	ShowUpButton: function ()
	{
		var obj = this;
		this.upButton.onmouseover = function () {
			obj.upButton.className = 'wm_toolbar_search_item_over';
		};
		this.upButton.onmouseout = function () {
			obj.upButton.className = 'wm_toolbar_search_item';
		};
		this.upButton.className = 'wm_toolbar_search_item';
	},
	
	HideUpButton: function ()
	{
		this.upButton.onmouseover = function () {};
		this.upButton.onmouseout = function () {};
		this.upButton.className = 'wm_hide';
	},
	
	Hide: function ()
	{
		this.shown = false;
		this.smallForm.className = 'wm_hide';
		this.HideDownButton();
		this.form.className = 'wm_hide';
		this.HideUpButton();
	},
	
	SetSearchIn: function (searchIn)
	{
		this._searchIn = searchIn;
	},
	
	ShowBigForm: function ()
	{
		var bounds = GetBounds(this.smallForm);
		this.form.style.top = bounds.Top + 'px';
		if (window.RTL) {
		    this.form.style.left = bounds.Left + 'px';
		} else {
		    this.form.style.right = (GetWidth() - bounds.Left - bounds.Width) + 'px';
		}
		this.form.className = 'wm_search_form';
		this.smallForm.className = 'wm_hide';
		this.HideDownButton();
		this.ShowUpButton();
		this.isShown = 2;
		this._bigLookFor.value = this._smallLookFor.value;
		if (null !== this._searchIn) {
			this._searchIn.className = '';
		}
	},
	
	checkVisibility: function (ev, isM)
	{
		if (this.isShown == 1) {
			ev = ev ? ev : window.event;
			elem = (isM) ? ev.target : ev.srcElement;
			while (elem && elem.tagName != 'DIV') {
				if (elem.parentNode) { 
					elem = elem.parentNode;
				} else {
					break; 
				}
			}
			if (elem.id != this._bigFormId) {
				this.Show();
			}
		}
		if (this.isShown == 2) {
			this.isShown = 1;
		}
	}
};

/* for control placement and displaying of information block */
function CInformation(cont, cls)
{
	this._mainContainer = cont;
	this._containerClass = cls;
}

CInformation.prototype = {
	Show: function ()
	{
		this._mainContainer.className = this._containerClass;
	},
	
	Hide: function ()
	{
		this._mainContainer.className = 'wm_hide';
	},

	Resize: function ()
	{
		var cont, offsetWidth, width;
		cont = this._mainContainer;
		cont.style.right = 'auto';
		cont.style.width = 'auto';
		offsetWidth = cont.offsetWidth;
		width = GetWidth();
		if (offsetWidth >  0.4 * width) {
			cont.style.width = '40%';
		}
		offsetWidth = cont.offsetWidth;
		cont.style.top = this.GetScrollY() + 'px';
		cont.style.left = Math.round((width - offsetWidth) / 2) + 'px';
	},

	GetScrollY: function ()
	{
		var scrollY = 0;
		if (document.body && typeof document.body.scrollTop != 'undefined') {
			scrollY += document.body.scrollTop;
			if (scrollY === 0 && document.body.parentNode && typeof document.body.parentNode != 'undefined') {
				scrollY += document.body.parentNode.scrollTop;
			}
		} else if (typeof window.pageXOffset != 'undefined') {
			scrollY += window.pageYOffset;
		}
		return scrollY;
	}
};

function CError(name)
{
	this._name = name;
	this._containerObj = null;
	this._messageObj = null;
	this._controlObj = null;
	this._fadeObj = null;
	this._delay = 10000;

	this.Build = function ()
	{
		var tbl, tr, td, aDiv, infoDiv, bDiv, div, closeImageDiv, obj;

		tbl = CreateChildWithAttrs(document.body, 'table',
				[['class', 'wm_hide'],
				['cellpadding', '0'],
				['cellspacing', '0']]);

			tr = CreateChildWithAttrs(tbl, 'tr', [['style', 'position:relative;z-index:20']]);
			CreateChildWithAttrs(tr, 'td', [['class', 'wm_shadow'],
				['style', 'width:2px;font-size:1px;']]);

			td = CreateChild(tr, 'td');
			infoDiv = CreateChildWithAttrs(td, 'div', [['class', 'wm_info_message'], ['id', 'info_message']]);
			aDiv = CreateChildWithAttrs(td, 'div', [['class', 'a']]);
			aDiv.innerHTML = '&nbsp;';
			bDiv = CreateChildWithAttrs(td, 'div', [['class', 'b']]);
			bDiv.innerHTML = '&nbsp;';
			CreateChildWithAttrs(infoDiv, 'div', [['class', 'wm_info_image']]);
			closeImageDiv = CreateChildWithAttrs(infoDiv, 'div', [['class', 'wm_close_info_image wm_control']]);
			obj = this;
			closeImageDiv.onclick = function () {
				obj.Hide();
			};

			CreateChildWithAttrs(tr, 'td', [['class', 'wm_shadow'],
				['style', 'width:2px;font-size:1px;']]);

			tr = CreateChild(tbl, 'tr');
			td = CreateChildWithAttrs(tr, 'td', [['class', 'wm_shadow'],
				['colspan', '3'],
				['style', 'height:2px;background:none;']]);
			aDiv = CreateChildWithAttrs(td, 'div', [['class', 'a']]);
			aDiv.innerHTML = '&nbsp;';
			bDiv = CreateChildWithAttrs(td, 'div', [['class', 'b']]);
			bDiv.innerHTML = '&nbsp;';

			tr = CreateChildWithAttrs(tbl, 'tr', [['style', 'position:relative;z-index:19']]);
			td = CreateChildWithAttrs(tr, 'td', [['style', 'height:2px;'], ['colspan', '3']]);
			div = CreateChildWithAttrs(td, 'div', [['class', 'a wm_shadow'],
				['style', 'margin:0px 2px;height:2px; top:-4px; position:relative; border:0px;background:#555;']]);
			div.innerHTML = '&nbsp;';

			this._containerObj = tbl;
			this._messageObj = CreateChild(infoDiv, 'span');
			this._controlObj = new CInformation(tbl, 'wm_error_information');
	};
}

function CReport(name)
{
	this._name = name;
	this._containerObj = null;
	this._messageObj = null;
	this._controlObj = null;
	this._fadeObj = null;
	this._delay = 5000;

	this.Build = function ()
	{
		var tbl, tr, td, aDiv, infoDiv, div, bDiv;

		tbl = CreateChildWithAttrs(document.body, 'table',
				[['class', 'wm_hide'],
				['cellpadding', '0'],
				['cellspacing', '0']]);

			tr = CreateChildWithAttrs(tbl, 'tr', [['style', 'position:relative;z-index:20']]);
			CreateChildWithAttrs(tr, 'td', [['class', 'wm_shadow'],
				['style', 'width:2px;font-size:1px;']]);

		td = CreateChild(tr, 'td');
		infoDiv = CreateChildWithAttrs(td, 'div', [['class', 'wm_info_message'], ['id', 'info_message']]);
		aDiv = CreateChildWithAttrs(td, 'div', [['class', 'a']]);
		aDiv.innerHTML = '&nbsp;';
		bDiv = CreateChildWithAttrs(td, 'div', [['class', 'b']]);
		bDiv.innerHTML = '&nbsp;';
		CreateChildWithAttrs(tr, 'td', [['class', 'wm_shadow'],
			['style', 'width:2px;font-size:1px;']]);

		tr = CreateChild(tbl, 'tr');
		td = CreateChildWithAttrs(tr, 'td', [['class', 'wm_shadow'],
				['colspan', '3'],
				['style', 'height:2px;background:none;']]);
		aDiv = CreateChildWithAttrs(td, 'div', [['class', 'a']]);
		aDiv.innerHTML = '&nbsp;';
		bDiv = CreateChildWithAttrs(td, 'div', [['class', 'b']]);
		bDiv.innerHTML = '&nbsp;';

			tr = CreateChildWithAttrs(tbl, 'tr', [['style', 'position:relative;z-index:19']]);
			td = CreateChildWithAttrs(tr, 'td', [['style', 'height:2px;'], ['colspan', '3']]);
			div = CreateChildWithAttrs(td, 'div', [['class', 'a wm_shadow'],
				['style', 'margin:0px 2px;height:2px; top:-4px; position:relative; border:0px;background:#555;']]);
			div.innerHTML = '&nbsp;';

			this._containerObj = tbl;
			this._messageObj = CreateChild(infoDiv, 'span');
			this._controlObj = new CInformation(tbl, 'wm_report_information');
	};
}

function IsGoodIE()
{
	if (document.body.filters)
	{
		var marray = navigator.appVersion.match(/MSIE ([\d.]+);/);
		if (marray && marray.length > 1 && marray[1] >= 5.5) {
			return true;
		}
	}
	return false;
}

var ReportPrototype = 
{
	Show: function (msg, priorDelay)
	{
		this._messageObj.innerHTML = msg;
		this._controlObj.Show();
		this._controlObj.Resize();
		if (null !== this._fadeObj) {
			if (this._name) {
				var interval = this._fadeObj.Go(this._containerObj, (priorDelay) ? priorDelay : this._delay);
				setTimeout(this._name + '.Hide()', interval);
			}
		} else {
			if (this._name) {
				setTimeout(this._name + '.Hide()', (priorDelay) ? priorDelay : this._delay);
			}
		}
	},
	
	SetFade: function (fadeObj)
	{
		this._fadeObj = fadeObj;
	},
	
	Hide: function ()
	{
		this._controlObj.Hide();
		if (null !== this._fadeObj) {
			this._fadeObj.SetOpacity(1, IsGoodIE());
		}
	},
	
	Resize: function ()
	{
		this._controlObj.Resize();		
	}
};

CReport.prototype = ReportPrototype;
CError.prototype = ReportPrototype;


function CFadeEffect(name)
{
	this._name = name;
	this._elem = null;
}

CFadeEffect.prototype = 
{
	Go: function (elem, delay)
	{
		var i, interval, iCount, diff, isIE;
		this._elem = elem;
		interval = 50;
		iCount = 10;
		diff = 1 / iCount;
		isIE = IsGoodIE() ? 'true' : 'false';
		for (i = 0; i <= iCount; i++) {
			setTimeout(this._name + '.SetOpacity(' + (1 - diff * i) + ', ' + isIE + ')', delay + interval * i);
		}
		return delay + interval * iCount;
	},
	
	SetOpacity: function (opacity, isIE)
	{
		var elem, oAlpha;
		elem = this._elem;
		// Internet Exploder 5.5+
		if (isIE) {
			opacity *= 100;
			oAlpha = elem.filters['DXImageTransform.Microsoft.alpha'] || elem.filters.alpha;
			if (oAlpha) {
				oAlpha.opacity = opacity;
			} else {
				elem.style.filter += 'progid:DXImageTransform.Microsoft.Alpha(opacity=' + opacity + ')';
			}
		}
		else {
			elem.style.opacity = opacity;		// CSS3 compliant (Moz 1.7+, Safari 1.2+, Opera 9)
			elem.style.MozOpacity = opacity;	// Mozilla 1.6-, Firefox 0.8
			elem.style.KhtmlOpacity = opacity;	// Konqueror 3.1, Safari 1.1
		}
	}
};

var Validator = {
    IsEmpty: function (strValue)
    {
		return (strValue.replace(/\s+/g, '') == '');
    },
    
    HasEmailForbiddenSymbols: function (strValue)
    {
		return (strValue.match(/[^A-Z0-9\"!#\$%\^\{\}`~&'\+\-=_@\.]/i));
    },
    
    IsCorrectEmail: function (strValue)
    {
		return (strValue.match(/^[A-Z0-9\"!#\$%\^\{\}`~&'\+\-=_\.]+@[A-Z0-9\.\-]+$/i));
    },
    
    IsCorrectServerName: function (strValue)
    {
		return (!strValue.match(/[^A-Z0-9\.\-\:\/]/i));
    },
    
    IsPositiveNumber: function (intValue)
    {
        if (isNaN(intValue) || intValue <= 0 || Math.round(intValue) != intValue) {
            return false;
        }
        return true;
    },
    
    CorrectNumber: function (value, minValue, maxValue)
    {
        if (isNaN(value) || value <= minValue) {
            return minValue;
        }
        if (maxValue != undefined && value >= maxValue) {
			return maxValue;
		}
        return Math.round(value);
    },
    
    IsPort: function (intValue)
    {
		return (this.IsPositiveNumber(intValue) && intValue <= 65535);
    },
    
    HasSpecSymbols: function (strValue)
    {
		return (strValue.match(/["\/\\*?<>|:]/));
    },
    
    IsCorrectFileName: function (strValue)
    {
        if (!this.HasSpecSymbols(strValue)) {
			return !strValue.match(/^(CON|AUX|COM1|COM2|COM3|COM4|LPT1|LPT2|LPT3|PRN|NUL)$/i);
        }
        return false;
    },
    
    CorrectWebPage: function (strValue)
    {
        return strValue.replace(/^[\/;<=>\[\\#\?]+/g, '');
    },
    
    HasFileExtention: function (strValue, strExtension)
    {           
		return (strValue.substr(strValue.length - strExtension.length - 1, strExtension.length + 1).toLowerCase() == '.' + strExtension.toLowerCase());
    }
};

function CPageSwitcher(withoutBorders)
{
	this._mainCont = null;
	this._pagesCont = null;
	this._count = 0;
	this._perPage = 0;
	this.PagesCount = 0;
	this._withoutBorders = (withoutBorders) ? true : false;
	this._mainClassName = 'wm_inbox_page_switcher';
	this._locationObj = null;
	this._build();
}

CPageSwitcher.prototype = {
	Show: function (page, perPage, count, beginOnclick, endOnclick)
	{
		this.PagesCount = 0;
		if (page === 0) {
			this._mainCont.className = this._mainClassName;
		}
		else {
			this.Hide();
			this._count = count;
			this._perPage = perPage;
			if (count > perPage) {
				var strPages = '';
				var pagesCount = Math.ceil(count / perPage);
				this.PagesCount = pagesCount;
				var firstPage, lastPage;
				if (pagesCount > 4) {
					firstPage = page - 2;
					if (firstPage < 1) {
						firstPage = 1;
					}
					lastPage = firstPage + 4;
					if (lastPage > pagesCount) {
						lastPage = pagesCount;
						firstPage = lastPage - 4;
					}
				}
				else {
					firstPage = 1;
					lastPage = pagesCount;
				}
				if (firstPage != lastPage) {
					if (firstPage > 1) {
						var strFirstPage = '<a href="#" onclick="' + beginOnclick + '1' + endOnclick + ' return false;" class="wm_page_switcher_first"><div title="' + Lang.FirstPage + '"></div></a>';
						var strPrevPage = '<a href="#" onclick="' + beginOnclick + (firstPage - 1) + endOnclick + ' return false;" class="wm_page_switcher_prev"><div title="' + Lang.PreviousPage + '"></div></a>';
						if (window.RTL) {
							strPages = strPrevPage + strFirstPage + strPages;
						}
						else {
							strPages += strFirstPage + strPrevPage;
						}
					}
					for (var i = firstPage; i <= lastPage; i++) {
						if (page == i) {
							if (window.RTL) {
								strPages = '<font>' + i + '</font>' + strPages;
							}
							else {
								strPages += '<font>' + i + '</font>';
							}
						}
						else {
							if (window.RTL) {
								strPages = '<a href="#" onclick="' + beginOnclick + i + endOnclick + ' return false;">' + i + '</a>' + strPages;
							}
							else {
								strPages += '<a href="#" onclick="' + beginOnclick + i + endOnclick + ' return false;">' + i + '</a>';
							}
						}
					}
					if (pagesCount > lastPage) {
						var strNextPage = '<a href="#" onclick="' + beginOnclick + (lastPage + 1) + endOnclick + ' return false;" class="wm_page_switcher_next"><div title="' + Lang.NextPage + '"></div></a>';
						var strLastPage = '<a href="#" onclick="' + beginOnclick + pagesCount + endOnclick + ' return false;" class="wm_page_switcher_last"><div title="' + Lang.LastPage + '"></div></a>';
						if (window.RTL) {
							strPages = strLastPage + strNextPage + strPages;
						}
						else {
							strPages += strNextPage + strLastPage;
						}
					}
					this._mainCont.className = this._mainClassName;
					this._pagesCont.innerHTML = strPages;
				}
			} // if (count > perPage)
		}
	},
	
	GetLastPage: function (removeCount, perPage)
	{
		var count = this._count - removeCount;
		if (perPage) {
			this._perPage = perPage;
		}
		var page = Math.ceil(count / this._perPage);
		if (page < 1) {
			page = 1;
		}
		return page;
	},
	
	Hide: function ()
	{
		this._mainCont.className = 'wm_hide';
	},

	SetLocationObject: function (locationObj)
	{
		this._locationObj = locationObj;
	},
	
	Replace: function ()
	{
		if (this._locationObj == null) return;
		var oBounds = GetBounds(this._locationObj);
		var ps = this._mainCont;
		var indent = 13;
		if (this._withoutBorders) {
			ps.style.top = (oBounds.Top + indent) + 'px';
		}
		else {
			ps.style.top = (oBounds.Top - ps.offsetHeight) + 'px';
			indent = 18;
		}
		/* rtl */
		if (window.RTL) {
			ps.style.left = (oBounds.Left + indent) + 'px';
		}
		else {
			ps.style.left = (oBounds.Left + oBounds.Width - ps.offsetWidth - indent) + 'px';
		}
	},
	
	_build: function ()
	{
		if (this._withoutBorders) {
			var tbl = CreateChild(document.body, 'table');
			this._mainCont = tbl;
			tbl.className = 'wm_hide';
			var tr = tbl.insertRow(0);
			var td = tr.insertCell(0);
			this._pagesCont = td;
			td.className = 'wm_inbox_page_switcher_pages';
		}
		else {
			var tbl = CreateChild(document.body, 'table');
			this._mainCont = tbl;
			tbl.className = 'wm_hide';
			var tr = tbl.insertRow(0);
			var td = tr.insertCell(0);
			var div = CreateChild(td, 'div');
			div.className = 'wm_inbox_page_switcher_left';
			td = tr.insertCell(1);
			this._pagesCont = td;
			td.className = 'wm_inbox_page_switcher_pages wm_inbox_page_switcher_borders';
			td = tr.insertCell(2);
			div = CreateChild(td, 'div');
			div.className = 'wm_inbox_page_switcher_right';
		}
	}
};

var CHECK_MAIL_BY_CLICK = 0;
var CHECK_MAIL_AT_LOGIN = 1;

function CCheckMail(type)
{
	this.isBuilded = false;
	this._type = (type) ? type : CHECK_MAIL_BY_CLICK;
	this.started = false;
	
	this._url = CheckMailUrl;
	this._email = '';
	this._msgsCount = 0;
	this._preText = '';
	
	this._form = null;
	this._typeObj = null;
	
	this._mainContainer = null;
	this._infomation = null;
	this._message = null;
	this._progressBarUsed = null;
}

CCheckMail.prototype = {
	Start: function (hide)
	{
		hide = hide || false;
		if (this.started) {
			return;
	    }
		if (this.isBuilded) {
			if (!hide && this._type == CHECK_MAIL_BY_CLICK) {
				this._infomation.Show();
			}
		} else {
			this.Build(hide);
		}
		this._preText = '';
		if (!hide) {
			this.SetText(Lang.LoggingToServer);
			this.UpdateProgressBar(0);
		}
		this._msgsCount = 0;
		this._typeObj.value = (hide) ? 2 : this._type;
		this._form.action = this._url + '?param=' + Math.random();
		this._form.submit();
		this.started = true;
	},

	SetAccount: function (account)
	{
		this._email = account;
		this._mainContainer.className = 'wm_connection_information';
		this._preText = '<b>' + this._email + '</b><br/>';
	},

	SetFolder: function (folderName, msgsCount)
	{
		this.UpdateProgressBar(0);
		this._folderName = folderName;
		this._msgsCount = msgsCount;
		this._preText = '';
		if (this._email.length > 0) {
			this._preText += '<b>' + this._email + '</b><br/>';
		}
		this._preText += Lang.Folder + ' <b>' + this._folderName + '</b><br/>';
	},
	
	SetText: function (text)
	{
		this._message.innerHTML = this._preText + text;
		if (this._type == CHECK_MAIL_BY_CLICK) {
			this._infomation.Resize();
		}
	},
	
	DeleteMsg: function (msgNumber) {
		if (msgNumber == -1) {
			this.SetText(Lang.DeletingMessages);
		} else {
			this.SetText(Lang.DeletingMessage + ' #' + msgNumber + ' ' + Lang.Of + ' ' + this._msgsCount);
			this.UpdateProgressBar(msgNumber);
		}
	},
	
	SetMsgNumber: function (msgNumber)
	{
		if (msgNumber <= this._msgsCount) {
			this.SetText(Lang.RetrievingMessage + ' #' + msgNumber + ' ' + Lang.Of + ' ' + this._msgsCount);
		}
		this.UpdateProgressBar(msgNumber);
	},
	
	UpdateProgressBar: function (msgNumber)
	{
		if (this._msgsCount > 0) {
			var percent = Math.ceil((msgNumber - 1) * 100 / this._msgsCount);
			if (percent < 0) { 
				percent = 0; 
			} else if (percent > 100) {
				percent = 100;
			}
			this._progressBarUsed.style.width = percent + 'px';
		}
	},
	
	End: function ()
	{
		if (this._type == CHECK_MAIL_BY_CLICK) {
			this._infomation.Hide();
		}
		this.started = false;
	},
	
	Build: function (hide)
	{
		var frm, tbl, tr, td, div, aDiv, bDiv, infoDiv, divPB, parentCont, subDiv;

		/* iframe block building */
		CreateChildWithAttrs(document.body, 'iframe', [['id', 'CheckMailIframe'], ['name', 'CheckMailIframe'], ['src', EmptyHtmlUrl], ['class', 'wm_hide']]);
		frm = CreateChildWithAttrs(document.body, 'form', [['action', this._url], ['target', 'CheckMailIframe'], ['method', 'post'], ['id', 'CheckMailForm'], ['name', 'CheckMailForm'], ['class', 'wm_hide']]);
		this._typeObj = CreateChildWithAttrs(frm, 'input', [['name', 'Type'], ['value', this._type]]);
		this._form = frm;
		switch (this._type) {
		case CHECK_MAIL_BY_CLICK:
			tbl = CreateChildWithAttrs(document.body, 'table',
				[['class', 'wm_information wm_connection_information'],
				['style', 'right: auto; width: auto; top: 0px; left: 604px;'],
				['id', 'info_cont'],
				['cellpadding', '0'],
				['cellspacing', '0']]);

			tr = CreateChildWithAttrs(tbl, 'tr', [['style', 'position:relative;z-index:20']]);
			CreateChildWithAttrs(tr, 'td', [['class', 'wm_shadow'],
				['style', 'width:2px;font-size:1px;']]);

			td = CreateChild(tr, 'td');
			infoDiv = CreateChildWithAttrs(td, 'div', [['class', 'wm_info_message'], ['id', 'info_message']]);
			aDiv = CreateChildWithAttrs(td, 'div', [['class', 'a']]);
			aDiv.innerHTML = '&nbsp;';
			bDiv = CreateChildWithAttrs(td, 'div', [['class', 'b']]);
			bDiv.innerHTML = '&nbsp;';
			this._message = CreateChild(infoDiv, 'span');
			divPB = CreateChildWithAttrs(infoDiv, 'div', [['class', 'wm_progressbar']]);
			CreateChildWithAttrs(tr, 'td', [['class', 'wm_shadow'],
				['style', 'width:2px;font-size:1px;']]);

			tr = CreateChild(tbl, 'tr');

			td = CreateChildWithAttrs(tr, 'td', [['class', 'wm_shadow'],
				['colspan', '3'],
				['style', 'height:2px;background:none;']]);
			aDiv = CreateChildWithAttrs(td, 'div', [['class', 'a']]);
			aDiv.innerHTML = '&nbsp;';
			bDiv = CreateChildWithAttrs(td, 'div', [['class', 'b']]);
			bDiv.innerHTML = '&nbsp;';

			tr = CreateChildWithAttrs(tbl, 'tr', [['style', 'position:relative;z-index:19']]);
			td = CreateChildWithAttrs(tr, 'td', [['style', 'height:2px;'], ['colspan', '3']]);
			div = CreateChildWithAttrs(td, 'div', [['class', 'a wm_shadow'],
				['style', 'margin:0px 2px;height:2px; top:-4px; position:relative; border:0px;background:#555;']]);
			div.innerHTML = '&nbsp;';

			this._containerObj = tbl;
			
			this._progressBarUsed = CreateChildWithAttrs(divPB, 'div', [['class', 'wm_progressbar_used']]);
			this._infomation = new CInformation(tbl, 'wm_information wm_connection_information');
			
			break;
		case CHECK_MAIL_AT_LOGIN:
			parentCont = document.getElementById('content');
			tbl = CreateChild((parentCont) ? parentCont : document.body, 'table');
			tbl.className = 'wm_hide';
			this._mainContainer = tbl;
			tbl.style.marginTop = '30px';
			tr = tbl.insertRow(0);
			td = tr.insertCell(0);
			td.className = 'wm_connection_header';
			td.colSpan = '3';
			td.innerHTML = Lang.Connection;
			CreateChild(td, 'span');
			tr = tbl.insertRow(1);
			td = tr.insertCell(0);
			td.className = 'wm_connection_icon';
			td = tr.insertCell(1);
			td.className = 'wm_connection_message';
			td.align = 'center';
			this._message = td;
			td = tr.insertCell(2);
			td.className = 'wm_connection_empty';
			tr = tbl.insertRow(2);
			td = tr.insertCell(0);
			td.className = 'wm_connection_progressbar';
			td.colSpan = 3;
			div = CreateChildWithAttrs(td, 'div', [['align', 'center']]);
			subDiv = CreateChildWithAttrs(div, 'div', [['class', 'wm_progressbar']]);
			this._progressBarUsed = CreateChildWithAttrs(subDiv, 'div', [['class', 'wm_progressbar_used']]);
			break;
		}
		
		/* it's builded! */
		this.isBuilded = true;
		if (this._infomation && hide) {
			this._infomation.Hide();	
		}
	}
};

function CDictionary()
{
	this.count = 0;
	this.Obj = {};
}

CDictionary.prototype = {
	exists: function (sKey)
	{
		return (this.Obj[sKey]) ? true : false;
	},

	add: function (sKey, aVal)
	{
		var K = String(sKey);
		if (this.exists(K)) {
			return false;
		}
		this.Obj[K] = aVal;
		this.count++;
		return true;
	},

	remove: function (sKey)
	{
		var K = String(sKey);
		if (!this.exists(K)) {
			return false;
		}
		delete this.Obj[K];
		this.count--;
		return true;
	},

	removeAll: function ()
	{
		for (var key in this.Obj) {
			delete this.Obj[key];
		}
		this.count = 0;
	},

	values: function ()
	{
		var Arr, key;
		Arr = [];
		for (key in this.Obj) {
			Arr[Arr.length] = this.Obj[key];
		}
		return Arr;
	},

	keys: function ()
	{
		var Arr, key;
		Arr = [];
		for (key in this.Obj) {
			Arr[Arr.length] = key;
		}
		return Arr;
	},

	items: function ()
	{
		var Arr, A, key;
		Arr = [];
		for (key in this.Obj) {
			A = [key, this.Obj[key]];
			Arr[Arr.length] = A;
		}
		return Arr;
	},

	getVal: function (sKey)
	{
		var K = String(sKey);
		return this.Obj[K];
	},

	setVal: function (sKey, aVal)
	{
		var K = String(sKey);
		if (this.exists(K)) {
			this.Obj[K] = aVal;
		} else {
			this.add(K, aVal);
		}
	},

	setKey: function (sKey, sNewKey)
	{
		var K, Nk;
		K = String(sKey);
		Nk = String(sNewKey);
		if (this.exists(K)) {
			if (!this.exists(Nk)) {
				this.add(Nk, this.getVal(K));
				this.remove(K);
			}
		} else if (!this.exists(Nk)) {
			this.add(Nk, null);
		}
	}
};

function CScriptLoader()
{
	this.onLoad = null;
	this.loadedCount = 0;
	this.scriptsCount = 0;
	this._onLoad = null;
	this._scripts = new CDictionary();
}

CScriptLoader.prototype = {
	Load: function (urlArray, loadHandler)
	{
		this.onLoad = loadHandler;
		this.loadedCount = 0;
		this.scriptsCount = urlArray.length;
		if (this.scriptsCount == 0) {
			this.onLoad.call();
		}
		for (var i in urlArray) {
			this.LoadItem(urlArray[i], this.ScriptLoadHandler);
		}
	},
	
	ScriptLoadHandler: function ()
	{
		this.loadedCount++;
		if (this.loadedCount == this.scriptsCount) {
			this.onLoad.call();
		}
	},
	
	LoadItem: function (url, loadHandler)
	{
		var script, obj, HeadElements;
		this._onLoad = loadHandler;
		script = document.createElement('script');
		script.setAttribute('type', 'text/javascript');
		obj = this;
		if (Browser.IE) {
			script.onreadystatechange = function ()
			{
			    if (this.readyState == 'complete' || this.readyState == 'loaded') {
			        if (obj._scripts.exists(this.src)) obj._scripts.remove(this.src);
					obj._onLoad.call(obj);
				}
			};
		}
		else {
			script.onload = function () {
				obj._scripts.remove(this.src);
				obj._onLoad.call(obj);
			};
		}
		this._scripts.add(url, true);
		script.src = url;
		HeadElements = document.getElementsByTagName('head');
		HeadElements[0].appendChild(script);
	}
};

function CNetLoader()
{
	this.Url = null;
	this.onLoad = null;
	this.onError = null;
	this.responseXML = null;
	this.responseText = null;
	this.ErrorDesc = null;
	this.Request = null;
	this.Log = '';
}

CNetLoader.prototype = {
	GetTransport: function ()
	{
		var transport = null;
		if (window.XMLHttpRequest) {
			transport = new XMLHttpRequest();
		}
		else {
			if (window.ActiveXObject) {
				try {
					transport = new ActiveXObject('Msxml2.XMLHTTP');
				}
				catch (err) {
					try {
						transport = new ActiveXObject('Microsoft.XMLHTTP');
					}
					catch (err2) {
					}
				}
			}
		}
		return transport;
	},

	LoadXMLDoc: function (Url, PostParams, onLoad, onError)
	{
		var Request, obj;
		this.Url = Url;
		this.onLoad = onLoad;
		this.onError = onError;
		Request = this.GetTransport();
		if (Request) {
			try {
				Request.open('POST', this.Url, true);
				Request.setRequestHeader('Content-type', 'application/x-www-form-urlencoded');
				obj = this;
				Request.onreadystatechange = function () {
					obj.OnReadyState(Request); 
				};
				Request.send(PostParams);
			}
			catch (err) {
				this.ErrorDesc = Lang.ErrorRequestFailed;
				this.onError.call(this);
			}
		}
		else {
			this.ErrorDesc = Lang.ErrorAbsentXMLHttpRequest;
			this.onError.call(this);
		}
		this.Request = Request;
	},
	
	OnReadyState: function (Request)
	{
		var ReadyStateComplete, Ready, HttpStatus, HttpStatusText;
		ReadyStateComplete = 4;
		Ready = Request.readyState;
		if (Ready == ReadyStateComplete) {
			try {
				HttpStatus = (typeof Request.status != 'undefined') ? Request.status : 13030;
				HttpStatusText = (typeof Request.statusText != 'undefined') ? Request.statusText : 'empty status text';
			}
			catch (e) {
				// 13030 is the custom code to indicate the condition -- in Mozilla/FF --
				// when the o object's status and statusText properties are
				// unavailable, and a query attempt throws an exception.
				HttpStatus = 13030;
				HttpStatusText = 'empty status text';
			}
			if (HttpStatus == 200 || HttpStatus == 0) {
				if (HttpStatus == 0 && Request.getResponseHeader('Content-Type') == null) {
					return;
				}
				this.responseXML = Request.responseXML;
				this.responseText = Request.responseText;
				this.onLoad.call(this);
			} else if (HttpStatus != 13030) {
				this.ErrorDesc = Lang.ErrorConnectionFailed + ' (' + HttpStatus + ' - ' + HttpStatusText + ')\n' + Request.responseText;
				this.onError.call(this);
			}
		}
	},
	
	CheckRequest: function ()
	{
		if (null != this.Request) {
			this.Request.onreadystatechange = null;
			this.Request.abort();
		}
	}
};


var Logger = {
	_container: null,
	_initialized: false,
	
	_init: function ()
	{
		if (this._initialized == true) {
			return;
		}
		this._container = CreateChild(document.body, 'div');
		this._container.dir = 'ltr';
		with (this._container.style) {
			color = 'black';
			border = 'solid 2px black';
			background = 'white';
			width = '300px';
			height = '100px';
			bottom = '0px';
			right = '0px';
			position = 'absolute';
			zIndex = '10';
			textAlign = 'left';
			overflow = 'auto';
		}
		this._initialized = true;
	},
	
	_write: function (msg)
	{
		this._init();
		if (!this._initialized) return;
		this._container.innerHTML = this._container.innerHTML + msg;
	},
	
	Write: function (msg)
	{
		this._write(msg + '; ');
	},
	
	WriteLine: function (msg)
	{
		this._write(msg + '<br />');
	}
};

function CreateSessionSaver() {
	CreateChildWithAttrs(document.body, 'iframe', [['id', 'session_saver'], ['name', 'session_saver'], ['src', 'session-saver.php'], ['class', 'wm_hide']]);
}

function CLanguageChanger()
{
	this._innerHTML = Array();
	this._iCount = 0;
	this._value = Array();
	this._vCount = 0;
	this._title = Array();
	this._tCount = 0;
}

CLanguageChanger.prototype = {
	Register: function (type, obj, field, end, start, number)
	{
		if (!start) {
			start = '';
		}
		switch (type) {
		default:
		case 'innerHTML':
			if (!number) {
				number = this._iCount;
				this._iCount++;
			}
			this._innerHTML[number] = {Elem: obj, Field: field, End: end, Start: start};
			return number;
		case 'value':
			if (!number) {
				number = this._vCount;
				this._vCount++;
			}
			this._value[number] = {Elem: obj, Field: field, End: end, Start: start};
			return number;
		case 'title':
			if (!number) {
				number = this._tCount;
				this._tCount++;
			}
			this._title[number] = {Elem: obj, Field: field, End: end, Start: start};
			return number;
		}
	},

	Go: function ()
	{
		var i, obj, iCount;
		iCount = this._innerHTML.length;
		for (i = 0; i < iCount; i++) {
			obj = this._innerHTML[i];
			if (obj && obj.Elem) {
				obj.Elem.innerHTML = obj.Start + Lang[obj.Field] + obj.End;
			}
		}

		iCount = this._value.length;
		for (i = 0; i < iCount; i++) {
			obj = this._value[i];
			if (obj && obj.Elem) {
				obj.Elem.value = Lang[obj.Field] + obj.End;
			}
		}

		iCount = this._title.length;
		for (i = 0; i < iCount; i++) {
			obj = this._title[i];
			if (obj && obj.Elem) {
				obj.Elem.title = Lang[obj.Field] + obj.End;
			}
		}
	}
};


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';
		}
	}
};


if (typeof window.JSFileLoaded != 'undefined') {
	JSFileLoaded();
}

