/*
Classes:
	CUserSettingsScreen
*/

  function lnk_bilnk()
    {
    
    document.getElementById('SignInhelp').className='lnk_color';
    
    }
    

function CreateRemoveClickFunc(id)
{
	return function () { if (confirm(Lang.ConfirmDeleteAccount)) { RemoveAccountHandle(id); } return false; }
}

function CreateAccountClickFunc(id)
{
	return function () {
		SetHistoryHandler(
			{
				ScreenId: SCREEN_USER_SETTINGS,
				SelectIdAcct: id,
				Entity: PART_ACCOUNT_PROPERTIES,
				NewMode: false
			}
		);
		return false;
	}
}

function CUserSettingsScreen(skinName)
{
	this.Id = SCREEN_USER_SETTINGS;
	this.isBuilded = false;
	this.hasCopyright = true;

	this._skinName = skinName;
	this._addAccountClass = 'wm_hide';
	this._allowChangeSettings = true;

	this._idAcct = -1;
	
	this.Settings = null;
	this.NewSettings = this.Settings;

	this.Accounts = new CAccounts();
	this.AccountProperties = [];
	this.Signatures = [];

	this._settings = null;
	
	this._mainContainer = null;
	this._nav = null;
	this._cont = null;
	this._addAccountTbl = null;

	this._contactsSettingsDiv = null;
	this._accountsSettingsDiv = null;
	this._EditProfileDiv= null;
	this._commonSettingsDiv = null;

	this._menuCommonImg = null;
	this._menuAccountsImg = null;
	this._menuContactsImg = null;
	
	this._commonSettingsObj = new CCommonSettingsScreenPart(skinName);
	this._accountPropertiesObj = new CAccountPropertiesScreenPart();
	this._filtersObj = new CFiltersScreenPart();
	this._signatureObj = new CSignatureScreenPart();
	this._manageFoldersObj = new CManageFoldersScreenPart(skinName);
	this._accountsListObj = new CAccountsListScreenPart(skinName, this._manageFoldersObj);
	this._contactsSettingsObj = new CContactsSettingsScreenPart();
	this._currPart = this._commonSettingsObj;

	this._manageFoldersSwitcher = null;
	this._signatureSwitcher = null;
	this._filtersSwitcher = null;
	this._propertiesSwitcher = null;

	this.SwitcherMode = 0;
	this._newMode = false;

	this._defAccountCount = 1;
}

CUserSettingsScreen.prototype = {
	PlaceData: function(Data)
	{
		if (Data) {
			var Type = Data.Type;
			switch (Type) {
				case TYPE_ACCOUNTS_LIST:
					if (this.Accounts.Count > 0 && Data.Count > this.Accounts.Count)
					{
						WebMail.ShowReport(Lang.ReportAccountCreatedSuccessfuly);
					}
					this.Accounts = Data;
					this._defAccountCount = this._accountsListObj.SetAccounts(Data);
					this._accountPropertiesObj.SetDefAccountCount(this._defAccountCount);
					if (this._accountsListObj.shown)
						this.ShowSettingsSwitcher(this.SwitcherMode);
					if (Data.HasAccount(this._idAcct) && !this._newMode) {
						this.ChangeAccountId(this._idAcct, true);
					} else {
						var flag = !(-1 == this._idAcct);
						if (-1 != Data.LastId)
						{
							this.ChangeAccountId(Data.LastId, true);
						}
						else
						{
							this.ChangeAccountId(Data.CurrId, true);
						}
					}
					break;
				case TYPE_FOLDERS_LIST:
					this._filtersObj.FillFolders(Data.Folders);
					this._manageFoldersObj.UpdateFolders(Data);
					break;
				case TYPE_USER_SETTINGS:
					this.Settings = Data;
					this._commonSettingsObj.SetSettings(Data);
					break;
				case TYPE_ACCOUNT_PROPERTIES:
					this._newMode = false;
					this.AccountProperties[Data.Id] = Data;
					this._accountPropertiesObj.SetAccountProperties(this.AccountProperties[Data.Id]);
					this.ChangeAccountId(Data.Id, false);
					this._manageFoldersObj.UpdateProtocol(Data.MailProtocol);
					break;
				case TYPE_FILTERS:
					this._filtersObj.SetFilters(Data.Items);
					break;
				case TYPE_X_SPAM:
					this._filtersObj.SetXSpam(Data);
					break;
				case TYPE_SIGNATURE:
					this.Signatures[Data.IdAcct] = Data;
					if (this._idAcct == Data.IdAcct)
						this._signatureObj.SetSignature(Data);
					break;
				case TYPE_CONTACTS_SETTINGS:
					this._contactsSettingsObj.UpdateSettings(Data);
					break;
			}//switch
		}
	},
	
	UpdateAccountProperties: function ()
	{
		var isDirectMode = this._accountPropertiesObj.GetNewAccountProperties();
		var acctProp = this._accountPropertiesObj.AccountProperties;

		if (acctProp.DefAcct != this.AccountProperties[acctProp.Id].DefAcct)
		{
			if (acctProp.DefAcct == true)
			{
				this._defAccountCount++;
			}
			else
			{
				this._defAccountCount--;
			}
			this._accountPropertiesObj.SetDefAccountCount(this._defAccountCount);
		}

		this.AccountProperties[acctProp.Id] = acctProp;
		return {IsDirectMode: isDirectMode, AcctId: acctProp.Id};
	},

	GetNewSettings: function ()
	{
		this.Settings = this._commonSettingsObj.GetNewSettings();
		return this.Settings;
	},
	
	GetNewSignature: function ()
	{
		var signature =  this._signatureObj.GetNewSignature();
		this.Signatures[signature.IdAcct] = signature;
		return signature;
	},
	
	DesignModeOn: function ()
	{
		this._signatureObj.DesignModeOn();
	},
	
	SetHtmlEditorField: function (heField)
	{
		this._signatureObj.SetHtmlEditorField(heField);
	},
	
	ClickBody: function(ev)
	{
		this._signatureObj.ClickBody();
	},

	ResizeBody: function(mode)
	{
		if (this.isBuilded) {
			if (this._signatureObj.shown) this._signatureObj.ReplaceHtmlEditorField();
		}
	},
	
	ShowCommonSettings: function ()
	{
		this._commonSettingsDiv.className = 'wm_selected_settings_item';
		if (this._allowChangeSettings) {
			this._accountsSettingsDiv.className = '';
		} else {
			this._accountsSettingsDiv.className = 'wm_hide';
		}
		this._contactsSettingsDiv.className = '';
		this._settingsSwitcher.className = 'wm_hide';
		this._accountsListObj.Hide();
		this._addAccountTbl.className = 'wm_hide';
		if (this._currPart != this._commonSettingsObj) {
			this._currPart.Hide();
			this._currPart = this._commonSettingsObj;
		}
		this._currPart.Show();
	},
	
	ChangeAccountId: function (id, showProp)
	{
		if (-1 != id)
		{
			this._idAcct = id;
			if (this.AccountProperties[id])
			{
				this._accountsListObj.ChangeIdAcct(id);
				if (showProp)
				{
					this._accountPropertiesObj.SetAccountProperties(this.AccountProperties[id]);
				}
				if (this.Signatures[id])
					this._signatureObj.SetSignature(this.Signatures[id]);
			}
			else
			{
				if (showProp)
				{
					GetHandler(TYPE_ACCOUNT_PROPERTIES, { IdAcct: id }, [], '');
				}
			}
			if (this._manageFoldersObj.shown)
			{
				this._manageFoldersObj.Show(id, this._settings);
			}
		}
	},
	
	ShowAccounts: function ()
	{
		if (this._allowChangeSettings) {
			this._commonSettingsDiv.className = '';
			this._accountsSettingsDiv.className = 'wm_selected_settings_item';
			this._contactsSettingsDiv.className = '';
			if (this._currPart != this._accountPropertiesObj) {
				this._currPart.Hide();
			}
			this._currPart = this._accountPropertiesObj;
			this._accountsListObj.Show(this._idAcct);
			this._addAccountTbl.className = this._addAccountClass;
			if (this._newMode) {
				this._settingsSwitcher.className = 'wm_hide';
				this._accountPropertiesObj.SetAccountProperties(new CAccountProperties());
			} else {
				if (this.AccountProperties[this._idAcct])
				{
					this._accountPropertiesObj.SetAccountProperties(this.AccountProperties[this._idAcct]);
				}
				else if (-1 != this._idAcct)
				{
					//GetHandler(TYPE_ACCOUNT_PROPERTIES, { IdAcct: this._idAcct }, [], '');
				}
				this.ShowSettingsSwitcher(0);
			}
			this._accountPropertiesObj.Show(this._settings);
		} else {
			this.ShowCommonSettings();
		}
	},//ShowAccounts
	
	ShowFilters: function ()
	{
		if (this._allowChangeSettings) {
			this._commonSettingsDiv.className = '';
			this._accountsSettingsDiv.className = 'wm_selected_settings_item';
			this._contactsSettingsDiv.className = '';
			if (this._currPart != this._filtersObj) {
				this._currPart.Hide();
				this._currPart = this._filtersObj;
				this._accountsListObj.Show(this._idAcct);
				this._addAccountTbl.className = this._addAccountClass;
				this.ShowSettingsSwitcher(1);
				this._currPart.Show(this._idAcct);
			}
		} else {
			this.ShowCommonSettings();
		}
	},//ShowFilters

	ShowSignature: function ()
	{
		if (this._allowChangeSettings) {
			this._commonSettingsDiv.className = '';
			this._accountsSettingsDiv.className = 'wm_selected_settings_item';
			this._contactsSettingsDiv.className = '';
			this._accountsListObj.Show(this._idAcct);
			this._addAccountTbl.className = this._addAccountClass;
			if (this._currPart != this._signatureObj) {
				this._currPart.Hide();
				this._currPart = this._signatureObj;
				this.ShowSettingsSwitcher(2);
				this._currPart.Show(this._idAcct, this._settings);
			}
		} else {
			this.ShowCommonSettings();
		}
	},//ShowSignature
	
	ShowManageFolders: function ()
	{
		if (this._allowChangeSettings) {
			this._commonSettingsDiv.className = '';
			this._accountsSettingsDiv.className = 'wm_selected_settings_item';
			this._contactsSettingsDiv.className = '';
			this._accountsListObj.Show(this._idAcct);
			this._addAccountTbl.className = this._addAccountClass;
			if (this._currPart != this._manageFoldersObj) {
				this._currPart.Hide();
				this._currPart = this._manageFoldersObj;
			}
			this.ShowSettingsSwitcher(3);
			this._currPart.Show(this._idAcct, this._settings);
			Screens[SCREEN_USER_SETTINGS].ShowHandler = '';
		} else {
			this.ShowCommonSettings();
		}
	},
	
	ShowContactsSettings: function ()
	{
		this._accountsListObj.Hide();
		this._addAccountTbl.className = 'wm_hide';
		this._commonSettingsDiv.className = '';
		if (this._allowChangeSettings) {
			this._accountsSettingsDiv.className = '';
		} else {
			this._accountsSettingsDiv.className = 'wm_hide';
		}
		this._contactsSettingsDiv.className = 'wm_selected_settings_item';
		this._settingsSwitcher.className = 'wm_hide';
		if (this._currPart != this._contactsSettingsObj) {
			this._currPart.Hide();
			this._currPart = this._contactsSettingsObj;
		}
		this._currPart.Show();
	},

	ShowSettingsSwitcher: function(mode)
	{
		this.SwitcherMode = mode;
		var obj = this;
		
		this._settingsSwitcher.className = 'wm_settings_accounts_info';

		var div = this._manageFoldersSwitcher;
		div.innerHTML = '';
		if (mode == 3) {
			div.className = 'wm_settings_switcher_select_item';
			div.innerHTML = Lang.ManageFolders;
		} else {
			div.className = 'wm_settings_switcher_item';
			var a = CreateChildWithAttrs(div, 'a', [['href', '#']]);
			a.innerHTML = Lang.ManageFolders;
			a.onclick = function () {
				SetHistoryHandler(
					{
						ScreenId: SCREEN_USER_SETTINGS,
						SelectIdAcct: obj._idAcct,
						Entity: PART_MANAGE_FOLDERS,
						NewMode: false
					}
				);
				return false;
			}
		}

		div = this._signatureSwitcher;
		div.innerHTML = '';
		if (mode == 2) {
			div.className = 'wm_settings_switcher_select_item';
			div.innerHTML = Lang.Signature;
		} else {
			div.className = 'wm_settings_switcher_item';
			var a = CreateChildWithAttrs(div, 'a', [['href', '#']]);
			a.innerHTML = Lang.Signature;
			a.onclick = function () {
				SetHistoryHandler(
					{
						ScreenId: SCREEN_USER_SETTINGS,
						SelectIdAcct: obj._idAcct,
						Entity: PART_SIGNATURE,
						NewMode: false
					}
				);
				return false;
			}
		}

		div = this._filtersSwitcher;
		div.innerHTML = '';
		if (mode == 1) {
			div.className = 'wm_settings_switcher_select_item';
			div.innerHTML = Lang.Filters;
		} else {
			div.className = 'wm_settings_switcher_item';
			var a = CreateChildWithAttrs(div, 'a', [['href', '#']]);
			a.innerHTML = Lang.Filters;
			a.onclick = function () {
				SetHistoryHandler(
					{
						ScreenId: SCREEN_USER_SETTINGS,
						SelectIdAcct: obj._idAcct,
						Entity: PART_FILTERS,
						NewMode: false
					}
				);
				return false;
			}
		}

		div = this._propertiesSwitcher;
		div.innerHTML = '';
		if (mode == 0) {
		//NetGainsTEchnologies
			div.className = 'wm_settings_switcher_select_item';
			//div.className = 'wm_hide';
			div.innerHTML = Lang.Properties;
		} else {
			div.className = 'wm_settings_switcher_item';
			//div.className = 'wm_hide';
			var a = CreateChildWithAttrs(div, 'a', [['href', '#']]);
			a.innerHTML = Lang.Properties;
			a.onclick = function () {
				SetHistoryHandler(
					{
						ScreenId: SCREEN_USER_SETTINGS,
						SelectIdAcct: obj._idAcct,
						Entity: PART_ACCOUNT_PROPERTIES,
						NewMode: false
					}
				);
				return false;
			}
		}
	},//ShowSettingsSwitcher

	Show: function (settings, historyArgs)
	{
		this.ParseSettings(settings);
		if (this.isBuilded) {
			this._mainContainer.className = '';
			if (null != historyArgs) {
				this.RestoreFromHistory(historyArgs);
			} else {
				this.ShowCommonSettings();
			}
		}
		if (this._accountsListObj.shown)
			this._addAccountTbl.className = this._addAccountClass;
		if (-1 != this._idAcct)
		{
			GetHandler(TYPE_FOLDERS_LIST, { IdAcct: this._idAcct, Sync: -1 }, [], '');
		}
	},
	
	RestoreFromHistory: function (historyArgs)
	{
		if ('undefined' != historyArgs.SelectIdAcct && null != historyArgs.SelectIdAcct &&
		 -1 != historyArgs.SelectIdAcct && historyArgs.SelectIdAcct != this._idAcct) {
			var showProp = false;
			if (PART_ACCOUNT_PROPERTIES == historyArgs.Entity) {
				showProp = true;
			}
			this.ChangeAccountId(historyArgs.SelectIdAcct, showProp);
		}
		switch (historyArgs.Entity) {
			case PART_COMMON_SETTINGS:
				this.ShowCommonSettings();
			break;
			case PART_ACCOUNT_PROPERTIES:
				this._newMode = historyArgs.NewMode;
				this.ShowAccounts();
			break;
			case PART_FILTERS:
				this.ShowFilters();
			break;
			case PART_SIGNATURE:
				this.ShowSignature();
			break;
			case PART_MANAGE_FOLDERS:
				this.ShowManageFolders();
			break;
			case PART_CONTACTS_SETTINGS:
				this.ShowContactsSettings();
			break;
		}
	},
	
	ParseSettings: function (settings)
	{
		if (settings.AllowAddAccount)
			this._addAccountClass = 'wm_settings_add_account_button';
		else
			this._addAccountClass = 'wm_hide';
		this._allowChangeSettings = settings.AllowChangeSettings;
		if (!this._allowChangeSettings) {
			this._accountsSettingsDiv.className = 'wm_hide';
		}
		this.ChangeSkin(settings.DefSkin);
		this._settings = settings;
	},

	ChangeSkin: function (newSkin)
	{
		if (this._skinName != newSkin) {
			this._skinName = newSkin;
			if (this.isBuilded) {
				this._menuCommonImg.src = 'skins/' + this._skinName + '/settings/menu_common.gif';
				this._menuAccountsImg.src = 'skins/' + this._skinName + '/settings/menu_accounts.gif';
				this._menuContactsImg.src = 'skins/' + this._skinName + '/settings/menu_contacts.gif';
				this._manageFoldersObj.ChangeSkin(newSkin);
			}
		}
	},
	
	Hide: function()
	{
		if (this.isBuilded) {
			this._commonSettingsObj.Hide();
			this._accountsListObj.Hide();
			this._contactsSettingsObj.Hide();
			this._accountPropertiesObj.Hide();
			this._filtersObj.Hide();
			this._signatureObj.Hide();
			this._manageFoldersObj.Hide();
			this._settingsSwitcher.className = 'wm_hide';
			this._mainContainer.className = 'wm_hide';
			this._addAccountTbl.className = 'wm_hide';
		}
	},
	
	Build: function(container, accountsBar, popupMenus, settings)
	{
	//debugger
	
		this._mainContainer = CreateChild(container, 'div');
		this._mainContainer.className = 'wm_hide';
		var tbl = CreateChild(this._mainContainer, 'table');
		tbl.className = 'wm_settings';
		var tr = tbl.insertRow(0);
		var td = tr.insertCell(0);
		td.className = 'wm_settings_nav';
		this._nav = td;
		var div = CreateChild(td, 'div');
		div.className = 'wm_selected_settings_item';
		var nobr = CreateChild(div, 'nobr');
		var img = CreateChildWithAttrs(nobr, 'img', [['src', 'skins/' + this._skinName + '/settings/menu_common.gif']]);
		this._menuCommonImg = img;
		var a = CreateChildWithAttrs(nobr, 'a', [['href', '#']]);
		a.onclick = function () {
			SetHistoryHandler(
				{
					ScreenId: SCREEN_USER_SETTINGS,
					SelectIdAcct: obj._idAcct,
					Entity: PART_COMMON_SETTINGS,
					NewMode: false
				}
			);
			return false;
		}
		a.innerHTML = Lang.Common;
		WebMail.LangChanger.Register('innerHTML', a, 'Common', '');
		this._commonSettingsDiv = div;
		div = CreateChild(td, 'div');
		nobr = CreateChild(div, 'nobr');
		img = CreateChildWithAttrs(nobr, 'img', [['src', 'skins/' + this._skinName + '/settings/menu_accounts.gif']]);
		this._menuAccountsImg = img;
		a = CreateChildWithAttrs(nobr, 'a', [['href', '#']]);
		var obj = this;
		a.onclick = function () {
			SetHistoryHandler(
				{
					ScreenId: SCREEN_USER_SETTINGS,
					SelectIdAcct: obj._idAcct,
					Entity: PART_ACCOUNT_PROPERTIES,
					NewMode: false
				}
			);
			return false;
		}
		a.innerHTML = Lang.EmailAccounts;
		WebMail.LangChanger.Register('innerHTML', a, 'EmailAccounts', '');
		this._accountsSettingsDiv = div;
		div = CreateChild(td, 'div');
		nobr = CreateChild(div, 'nobr');
		img = CreateChildWithAttrs(nobr, 'img', [['src', 'skins/' + this._skinName + '/settings/menu_contacts.gif']]);
		this._menuContactsImg = img;
		a = CreateChildWithAttrs(nobr, 'a', [['href', '#']]);
		a.onclick = function () {
			SetHistoryHandler(
				{
					ScreenId: SCREEN_USER_SETTINGS,
					SelectIdAcct: obj._idAcct,
					Entity: PART_CONTACTS_SETTINGS,
					NewMode: false
				}
			);
			return false;
		}
		a.innerHTML ='Change Password';
		//a.innerHTML = Lang.Contacts;
		WebMail.LangChanger.Register('innerHTML', a, 'Contacts', '');
		this._contactsSettingsDiv = div;
		
		//NetGiansTechnologies
//		
//		div = CreateChild(td, 'div');
//	nobr = CreateChild(div, 'nobr');
//		img = CreateChildWithAttrs(nobr, 'img', [['src', 'skins/' + this._skinName + '/settings/menu_accounts.gif']]);
//	//	this._menuAccountsImg = img;
//		a = CreateChildWithAttrs(nobr, 'a', [['href', '#']]);
//	var obj = this;
//	a.onclick = function ()
//		 {
//		SetHistoryHandler(
//				{
//				
//				ScreenId: SCREEN_USER_SETTINGS,
//					SelectIdAcct: obj._idAcct,
//					Entity: PART_ACCOUNT_PROPERTIES,
//				NewMode: false
//			}
//		);
//			//return false;
//		}
//		a.innerHTML = 'Edit Profile';
//		//WebMail.LangChanger.Register('innerHTML', a, 'EmailAccounts', '');
//		//this._accountsSettingsDiv = div;
//		//this._EditProfileDiv=div;
		//End NetGians
		
		
		

		td = tr.insertCell(1);
		td.className = 'wm_settings_cont';
		this._cont = td;

		this._commonSettingsObj.Build(td);

		this._accountsListObj.Build(td);

		var tbl_ = CreateChild(td, 'table');
		tbl_.className = 'wm_hide';
		this._addAccountTbl = tbl_;
		var tr_ = tbl_.insertRow(0);
		var td_ = tr_.insertCell(0);
		// NetGainsTechnologies
		var inp = CreateChildWithAttrs(td_, 'input', [['class', 'wm_hide'], ['type', 'button'], ['value', Lang.AddNewAccount]]);
		WebMail.LangChanger.Register('value', inp, 'AddNewAccount', '');
		inp.onclick = function () {
			SetHistoryHandler(
				{
					ScreenId: SCREEN_USER_SETTINGS,
					SelectIdAcct: obj._idAcct,
					Entity: PART_ACCOUNT_PROPERTIES,
					NewMode: true
				}
			);
		}
		if (WebMail._isDemo)
		{
			inp.onclick = function () { WebMail.ShowReport(DemoWarning); }
		}

		var div = CreateChild(td, 'div');
		div.className = 'wm_hide';
		var div_ = CreateChild(div, 'div');
		div_.className = 'wm_settings_switcher_indent';
		this._manageFoldersSwitcher = CreateChild(div, 'div');
		this._signatureSwitcher = CreateChild(div, 'div');
		this._filtersSwitcher = CreateChild(div, 'div');
		this._propertiesSwitcher = CreateChild(div, 'div');
		this._settingsSwitcher = div;
		var obj = this;
		this._accountPropertiesObj.Build(td, obj);
		this._filtersObj.Build(td);
		this._signatureObj.Build(td);
		this._manageFoldersObj.Build(td);

		this._contactsSettingsObj.Build(td);

		this.isBuilded = true;
	}//Build
}
