﻿//Copyright WebMail Wou3, Inc. 2007-2010
Type.registerNamespace('WebMail2');
WebMail2.Toolbar=function
(
	DummyButton,
	RichTextButton,
	PlainTextButton,
	FolderSelect,
	ToolbarState,
	liSendID,
	liNewID,
	liRefreshID,
	liViewID,
	ViewButtonID,       //The view/edit button is shared.  The ViewButtonID is the  unique ID for the ViewButton 
	liSpellID,
	liSaveID,
	liSaveDraftContinueID,
	liPrintID,
	liComposeFormatID,
	liReplyToID,
	liMarkID,
	liMoveID,
	liDeleteID,
	liCloseID,
	liSpamID,
	SpamButtonID,
	ViewButtonText,     //ViewButtonText is the text value for the button when the button is used for viewing a message.
	EditButtonText,     //EditButtonText is the text value for the button when the button is used for editing a message.
	ViewButtonTitle,		//View Button's Tool Tip
	EditButtonTitle,		//Edit Button's Tool Tip
	SpamButtonText,
	NotSpamButtonText,
	SpamButtonTitle,		//View Button's Tool Tip
	NotSpamButtonTitle,		//Edit Button's Tool Tip
	SpamButtonVisibleClass,
	NotSpamButtonVisibleClass,
	SpamButtonNotVisibleClass
) 
{ 
	this._DummyButton=DummyButton;
	this._RichTextButton=RichTextButton;
	this._PlainTextButton=PlainTextButton;
	this._FolderSelect=FolderSelect;
	
	this.liSend=document.getElementById(liSendID);
	this.liNew=document.getElementById(liNewID);
	this.liRefresh=document.getElementById(liRefreshID);
	this.liView=document.getElementById(liViewID);
	this.liSpell=document.getElementById(liSpellID);
	this.liSave=document.getElementById(liSaveID);
	this.liSaveDraftContinue=document.getElementById(liSaveDraftContinueID);
	this.liPrint=document.getElementById(liPrintID);
	this.liComposeFormat=document.getElementById(liComposeFormatID);
	this.liReplyTo=document.getElementById(liReplyToID);
	this.liMark=document.getElementById(liMarkID);
	this.liMove=document.getElementById(liMoveID);
	this.liDelete=document.getElementById(liDeleteID);
	this.liClose=document.getElementById(liCloseID);
	this.ViewButton=document.getElementById(ViewButtonID);
	
	this.liSpam=null;
	if(liSpamID.length>0)
		this.liSpam=document.getElementById(liSpamID);
	
	this.SpamButton=null;
	if(SpamButtonID.length>0)
		this.SpamButton=document.getElementById(SpamButtonID);
	
	this._ToolbarState=ToolbarState;

	this._ViewButtonText=ViewButtonText;
	this._EditButtonText=EditButtonText;
	this._ViewButtonTitle=ViewButtonTitle;
	this._EditButtonTitle=EditButtonTitle;
	
	this._SpamButtonText=SpamButtonText;
	this._NotSpamButtonText=NotSpamButtonText;

	this._SpamButtonVisibleClass=SpamButtonVisibleClass;
	this._NotSpamButtonVisibleClass=NotSpamButtonVisibleClass;
	this._SpamButtonNotVisibleClass=SpamButtonNotVisibleClass;
	
	this._SpamButtonTitle=SpamButtonTitle;
	this._NotSpamButtonTitle=NotSpamButtonTitle;

	this._dropDownFolder=null;
	
	//every time a server method is called that is waiting for the callback results, 
	//this.disable() will set  this._enable=false.  When false, none of the action buttons
	//will work.  this.enable() will set this._enable=true
	/*the following are reference objects to the controls using the toolbar*/
	this._enable=true;
	this._Compose=null;
	this._ConfirmCompose=null;
	this._View=null;
	this._MessageList=null;
	this._ContactList=null;
	this._Contact=null;
	this._NewDistList=null;
	this._EditDistList=null;
}
WebMail2.Toolbar.prototype=
{
/***************************************************************************************************/
/* Set Toolbar Associations */

resetReferences:function()
{
	this._enable=true;
	this._Compose=null;
	this._ConfirmCompose=null;
	this._View=null;
	this._MessageList=null;
	this._ContactList=null;
	this._Contact=null;
	this._NewDistList=null;
	this._EditDistList=null;
},

HasViewReference:function() {return ((this._View!=null) && (this._enable));},
get_View:function() {return this._View;},
set_View:function(value) 
{
	this.resetReferences();
	this._View=value;
},

HasContactList:function() {return ((this._ContactList!=null) && (this._enable));},
get_ContactList:function() {return this._ContactList;},
set_ContactList:function(value)
{
	this.resetReferences();
	this._ContactList=value;
},

HasContact:function() {return ((this._Contact!=null) && (this._enable));},
get_Contact:function(){return this._Contact;},
set_Contact:function(value)
{
	this.resetReferences();
	this._Contact=value;
},

HasMessageList:function() {return ((this._MessageList!=null) && (this._enable));},
get_MessageList:function() {return  this._MessageList;},
set_MessageList:function(value) 
{
	this.resetReferences();
	this._MessageList=value;
},

HasEditDistList:function() {return ((this._EditDistList!=null) && (this._enable));},
get_EditDistList:function() {return  this._EditDistList;},
set_EditDistList:function(value) 
{
	this.resetReferences();
	this._EditDistList=value;
},

HasComposeReference:function() {return ((this._Compose!=null) && (this._enable));},
get_Compose:function() {return this._Compose;},
set_Compose:function(value) 
{   
	this.resetReferences();
	this._Compose=value;
},
HasConfirmComposeReference:function() {return ((this._ConfirmCompose!=null) && (this._enable));},
get_ConfirmCompose:function() {return this._ConfirmCompose;},
set_ConfirmCompose:function(value) 
{   
	this.resetReferences();
	this._ConfirmCompose=value;
},
/***************************************************************************************************/

get_IsHTML:function() 
{
	return (getObjectInnerHTML(this._DummyButton)==getObjectInnerHTML(this._RichTextButton));
},
set_IsHTML:function() 
{
	if (this._Compose!=null) 
	{
		//set the message body 
		this.set_EmailFormatButton(true);
		this._Compose.setMessageBodyFormat( true, true );
	}
},
set_IsPlain:function() 
{
	if (this._Compose!=null)
	{
		this.set_EmailFormatButton(false);
		//set the message body 
		this._Compose.setMessageBodyFormat( false, true );
	}
},
set_EmailFormatButton:function(IsHTML)
{
	if(this.liComposeFormat!=null)
	{
		if(IsHTML) 
		{
			if(this.liComposeFormat.className!=VALUE_CSS_FORMAT_RICH_TEXT)
			{
				try{this.liComposeFormat.className=VALUE_CSS_FORMAT_RICH_TEXT;}
				catch(ex)
				{this.liComposeFormat.setAttribute("class", VALUE_CSS_FORMAT_RICH_TEXT);}
			}
			if(getObjectInnerHTML(this._DummyButton)!=getObjectInnerHTML(this._RichTextButton))
			{
				//set the toolbar top level button' innerHTML to "Rich Text" which is the Rich Text linkButtons innerHTML 
				setObjectInnerHTML(this._DummyButton, getObjectInnerHTML(this._RichTextButton));
			}
		}
		else
		{
			if(this.liComposeFormat.className!=VALUE_CSS_FORMAT_PLAIN_TEXT)
			{
				try{this.liComposeFormat.className=VALUE_CSS_FORMAT_PLAIN_TEXT;}
				catch(ex)
				{this.liComposeFormat.setAttribute("class", VALUE_CSS_FORMAT_PLAIN_TEXT);}
			}
			if(getObjectInnerHTML(this._DummyButton)!=getObjectInnerHTML(this._PlainTextButton))
			{
				//set the toolbar top level button' innerHTML to "Rich Text" which is the Rich Text linkButtons innerHTML 
				setObjectInnerHTML(this._DummyButton, getObjectInnerHTML(this._PlainTextButton));
			}
		}
	}
},
get_SelectedFolder:function()
{
	var selectOption=document.getElementById(this._DestinationFolder).selectedIndex;
	if(selectOption==null)
		return "";
	return document.getElementsByTagName("option")[selectOption].value;
},
////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//Action methods.  Methods in this section, will execute code on their related controls
//i.e. the view control, the compose control, the message list control, etc.
////////////////////////////////////////////////////////////////////////////////////////////////////////////////
save:function()
{
	try{page.resetLastClicked();}catch(e){}
	if(this.HasContact())
		this.get_Contact().save();
	else if(this.HasEditDistList())
		this.get_EditDistList().save();
},
saveDraft:function()
{
	try{page.resetLastClicked();}catch(e){}
	if(this.HasComposeReference())
		this.get_Compose().saveMessage(false, false);
},
checkSpelling:function()
{   
	try{page.resetLastClicked();}catch(e){}
	if(this.HasComposeReference())
	{
		this.get_Compose().checkSpelling();
	}
},
closeItem:function() 
{   
	try{page.resetLastClicked();}catch(e){}
	if(this.HasComposeReference())
	{
		if (confirm("Unsaved changes will be permanently lost.  Are you sure you want to close this draft?"))
			this.get_Compose().closeMessage();
	}
	else if(this.HasViewReference())
	{this.get_View().closeMessage();}
	else if(this.HasContact())
	{this.get_Contact().closeContact();}
	else if(this.HasConfirmComposeReference())
	{this.get_ConfirmCompose().closeConfirm();}
	else if(this.HasEditDistList())
	{this.get_EditDistList().cancel();}
	return false;
},
deleteItem:function()
{
	try{page.resetLastClicked();}catch(e){}
	if(this.HasComposeReference())
		this.get_Compose().deleteMessage();
	else if(this.HasViewReference())
		this.get_View().deleteMessage();
	else if(this.HasMessageList())
		this.get_MessageList().deleteItems();
	else if(this.HasContactList())
		this.get_ContactList().deleteItems();
	else if(this.HasContact())
		this.get_Contact().deleteContact();
	else if(this.HasEditDistList())
		this.get_EditDistList().deleteList();
	return false;
},
move:function()
{
	try{page.resetLastClicked();}catch(e){}
	if(this._dropDownFolder==null)
		this._dropDownFolder=document.getElementById(this._FolderSelect);
	var folderName;
	if((this._dropDownFolder==null) || (this._dropDownFolder.selectedIndex <= 0))
	{
		return;
	}
	else
	{
		folderName=this._dropDownFolder.options[this._dropDownFolder.selectedIndex].value;
	}
	if(this.HasViewReference())
		this.get_View().moveMessage(folderName);
	else if(this.HasMessageList())
		this.get_MessageList().moveMessages(folderName);
	this._dropDownFolder.selectedIndex=0;
},
reply:function()
{
	try{page.resetLastClicked();}catch(e){}
	if(this.HasViewReference())
		return this.get_View().reply();
	else if(this.HasMessageList())
		this.get_MessageList().reply();
		
},
replyToAll:function()
{
	try{page.resetLastClicked();}catch(e){}
	if(this.HasViewReference())
		return this.get_View().replyToAll();
	else if(this.HasMessageList())
		this.get_MessageList().replyToAll();
},
forward:function()
{
	try{page.resetLastClicked();}catch(e){}
	if(this.HasViewReference())
		return this.get_View().forward();
	else if(this.HasMessageList())
		this.get_MessageList().forward();
},
send:function()
{
	try{page.resetLastClicked();}catch(e){}
	if(this.HasComposeReference())
		return this.get_Compose().sendMessage();
},
refresh:function()
{
	try{page.resetLastClicked();}catch(e){}
	if(this.HasMessageList())
		this.get_MessageList().refresh(false);
	else if(this.HasContactList())
		this.get_ContactList().refresh(false);
},
view:function()
{
	try{page.resetLastClicked();}catch(e){}
	if(this.HasMessageList())
		this.get_MessageList().view();
	else if(this.HasContactList())
		this.get_ContactList().view();
	
},
create:function()
{
	try{page.resetLastClicked();}catch(e){}
	if(this.HasContactList())
		this.get_ContactList().create();
	else if(this.HasContact())
		this.get_Contact().create();
	else if(this.HasEditDistList())
		this.get_EditDistList().create();
},
print:function()
{
	try{page.resetLastClicked();}catch(e){}
	if(this.HasViewReference())
		this.get_View().printMessage();
	else if(this.HasComposeReference())
		this.get_Compose().printMessage();
},
markAsRead:function(isRead) 
{
	try{page.resetLastClicked();}catch(e){}
	if(this.HasMessageList())
	{
		this.get_MessageList().markAsRead(isRead);
	}
	
	return false;
},
markAsSpam:function() 
{
	try{page.resetLastClicked();}catch(e){}
	if(this.HasViewReference())
	{
		this.get_View().markAsSpam();
	}
	else if(this.HasMessageList())
	{
		this.get_MessageList().markAsSpam();
	}
	
	return false;
},
validateAddresses:function() 
{
	try{page.resetLastClicked();}catch(e){}
	if(this.HasComposeReference())
	{
		this.get_Compose().validateAddresses();
	}
	
	return false;
},
//////////////////////////////////////////////////////////////////////////////////////////////////
//Changes the visible buttons and/or the state of the buttons for toolbars associated with
//viewMessage controls or messageList controls
//////////////////////////////////////////////////////////////////////////////////////////////////
updateToolbar:function()
{
	if(this.HasMessageList())
	{
		switch(page.get_FolderPath())
		{
			case FOLDER_DRAFTS:
				this.liClose.style.display='none';
				this.liComposeFormat.style.display='none';
				this.liDelete.style.display='list-item';
				this.liMark.style.display='none';
				this.liMove.style.display='none';
				this.liNew.style.display='none';
				this.liPrint.style.display='none';
				this.liRefresh.style.display='list-item';
				this.liReplyTo.style.display='none';
				this.liSave.style.display='none';
				this.liSaveDraftContinue.style.display='none';
				this.liSend.style.display='none';
				this.liSpell.style.display='none';
				
				this.liView.style.display='list-item';
				this.ViewButton.innerHTML=this._EditButtonText;
				this.ViewButton.setAttribute('title', this._EditButtonTitle);
				
				this.liSpam.className=this._SpamButtonNotVisibleClass;
				break;
			case FOLDER_SENT:
				this.liClose.style.display='none';
				this.liComposeFormat.style.display='none';
				this.liDelete.style.display='list-item';
				this.liMark.style.display='none';
				this.liMove.style.display='list-item';
				this.liNew.style.display='none';
				this.liPrint.style.display='none';
				this.liRefresh.style.display='list-item';
				this.liReplyTo.style.display='list-item';
				this.liSave.style.display='none';
				this.liSaveDraftContinue.style.display='none';
				this.liSend.style.display='none';
				this.liSpell.style.display='none';
				
				this.liView.style.display='list-item';
				this.ViewButton.innerHTML=this._ViewButtonText;
				this.ViewButton.setAttribute('title', this._ViewButtonTitle);
				
				this.liSpam.className=this._SpamButtonNotVisibleClass;
				break;
			case FOLDER_SPAM:
				this.liClose.style.display='none';
				this.liComposeFormat.style.display='none';
				this.liDelete.style.display='list-item';
				this.liMark.style.display='list-item';
				this.liMove.style.display='none';
				this.liNew.style.display='none';
				this.liPrint.style.display='none';
				this.liRefresh.style.display='list-item';
				this.liReplyTo.style.display='none';
				this.liSave.style.display='none';
				this.liSaveDraftContinue.style.display='none';
				this.liSend.style.display='none';
				this.liSpell.style.display='none';
				
				this.liView.style.display='list-item';
				this.ViewButton.innerHTML=this._ViewButtonText;
				this.ViewButton.setAttribute('title', this._ViewButtonTitle);
				
				this.liSpam.className=this._NotSpamButtonVisibleClass;
				this.SpamButton.innerHTML=this._NotSpamButtonText;
				this.SpamButton.setAttribute('title',this._NotSpamButtonTitle)
				break;
			default:
				this.liClose.style.display='none';
				this.liComposeFormat.style.display='none';
				this.liDelete.style.display='list-item';
				this.liMark.style.display='list-item';
				this.liMove.style.display='list-item';
				this.liNew.style.display='none';
				this.liPrint.style.display='none';
				this.liRefresh.style.display='list-item';
				this.liReplyTo.style.display='list-item';
				this.liSave.style.display='none';
				this.liSaveDraftContinue.style.display='none';
				this.liSend.style.display='none';
				this.liSpell.style.display='none';

				this.liView.style.display='list-item';
				this.ViewButton.innerHTML=this._ViewButtonText;
				this.ViewButton.setAttribute('title', this._ViewButtonTitle);
				
				if(this.liSpam!=null)
					this.liSpam.className=this._SpamButtonVisibleClass;
				
				if(this.SpamButton!=null)
				{
					this.SpamButton.innerHTML=this._SpamButtonText;
					this.SpamButton.setAttribute('title',this._SpamButtonTitle)
				}
				break;
		}
		
	}
	else if(this.HasViewReference())
	{	
		//if the message is from the drafts or sent folder, hide the spam button
		if(this.get_View().get_FileName().toLowerCase().indexOf(FOLDER_DRAFTS.toLowerCase())==0)
		{
			this.liSpam.className=this._SpamButtonNotVisibleClass;
		}
		else if(this.get_View().get_FileName().toLowerCase().indexOf(FOLDER_SENT.toLowerCase())==0)
		{
			this.liSpam.className=this._SpamButtonNotVisibleClass;
		}
		//if the message source is from the spam folder, set the spam button settings to "mark as not spam"
		else if(this.get_View().get_FileName().toLowerCase().substring(0,FOLDER_SPAM.length)==FOLDER_SPAM.toLowerCase())
		{
			this.liSpam.className=this._NotSpamButtonVisibleClass;
			this.SpamButton.innerHTML=this._NotSpamButtonText;
			this.SpamButton.setAttribute('title',this._NotSpamButtonTitle);
		}
		//if the message source is not from the spam folder, set the spam button settings to "mark as not spam"
		else
		{
			this.liSpam.className=this._SpamButtonVisibleClass;
			this.SpamButton.innerHTML=this._SpamButtonText;
			this.SpamButton.setAttribute('title',this._SpamButtonTitle);
		}
	}
},

////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//Enable/Disable the toolbar
//these methods are used 
////////////////////////////////////////////////////////////////////////////////////////////////////////////////
disable:function(){this._enable=false;},
enable:function(){this._enable=true;}
////////////////////////////////////////////////////////////////////////////////////////////////////////////////
}
WebMail2.Toolbar.registerClass("WebMail2.Toolbar");

