﻿//Copyright WebMail Wou3, Inc. 2007-2010
Type.registerNamespace('WebMail2');
WebMail2.Compose=function
(
MainDivCompose,ConfirmDiv,AddContactsDiv,From,ReplyTo,To,Cc,Bcc,Subject,
PlainTextBody,DivHtmlWrapperID, HTMLBody,
FileNameIndex,SaveCopyID,ToolBarObject,ConfirmToolBarObject,
ListAttachmentDivID,ShowHiddenAttachmentsID,DefaultAttachmentListHTML,
LinkShowCC,CcDiv,BccDiv,EnableRTF,SaveCopy,AutoSpellCheck
)
{
/*
EnableRTF=default display for new messages is RichText when EnableRTF==true. uses WebMailSettings.EnableRTF.
SaveCopy=the Save a copy checkbox Checked=SaveCopy. uses WebMailSettings.SaveMessage.
*/
var baseParams=new Array
(
	From,ReplyTo,To,Cc,Bcc,Subject,
	PlainTextBody,HTMLBody,
	FileNameIndex,ToolBarObject,
	ListAttachmentDivID,ShowHiddenAttachmentsID,DefaultAttachmentListHTML
);
WebMail2.Compose.initializeBase(this,baseParams);

this._MainDivCompose=MainDivCompose;
this._ConfirmDiv=ConfirmDiv;
this._AddContactsDiv=AddContactsDiv;
this._ConfirmToolBarObject=ConfirmToolBarObject;
this._SaveCopyObj=document.getElementById(SaveCopyID);
this._SaveCopy=SaveCopy;
this._ContactsPopup=null;
this._AttachmentWindow=null;
this._DivHtmlWrapperID=DivHtmlWrapperID;
this._SpellCheckerID=null;

if(this._ToolBarObject!=null)
	this._ToolBarObject.set_Compose(this);
if(this._ConfirmToolBarObject!=null)
	this._ConfirmToolBarObject.set_ConfirmCompose(this);
	
this._LinkShowCC=LinkShowCC;
this._CcDiv=CcDiv;
this._BccDiv=BccDiv;

this._EnableRTF=EnableRTF;
this._SaveCopy=SaveCopy;
page.addEditControl(this.get_FileNameIndex(),this);
this._FirstTabIndex=page.EditFilesFirstTab;
this._AutoSpellCheck=AutoSpellCheck;

this.RecipeintsCounted=0;
}

WebMail2.Compose.prototype=
{
set_showCC:function(show)
{
	set_ObjectIsDisplayed(this._LinkShowCC, !show);
	set_ObjectIsDisplayed(this._CcDiv, show);
	set_ObjectIsDisplayed(this._BccDiv, show);
},
get_showCC:function()
{
	return get_ObjectIsVisible(this._CcDiv) && get_ObjectIsVisible(this._BccDiv) && !get_ObjectIsVisible(this._LinkShowCC);
},
composeNewMessage:function()
{
	set_ObjectIsDisplayed(this._MainDivCompose,true)
	set_ObjectIsDisplayed(this._ConfirmDiv,false)
		
	if(this._EnableRTF)
	{   
		this.setMessageBodyFormat(true,false);
		this.set_PlainTextBody('');
		this.set_HTMLBody(g_objRTFSignature.value);
	}
	else
	{
		this.setMessageBodyFormat(false,false);
		this.set_PlainTextBody(g_objPlainSignature.value);
		this.set_HTMLBody('');
	}
	//sets the from address.  If the user has a display name, it will be included.  Otherwise just the email address
	this.set_From(g_objFrom.value);
	//set Save message after send
	this.set_SaveCopy(this._SaveCopy);

	//set reply to
	if(g_objReplyTo.value.length>0)
	{
		this.set_ReplyTo(g_objReplyTo.value);
	}

	page.setTabText(this.get_TabIndex(), "[New Message]");
	page.setTabVisible(this.get_TabIndex(), true);
	page.setSelectedTabIndex(this.get_TabIndex());

},
loadDraft:function()
{
	if((this.get_FileNameIndex()>=0) && (g_objEditFileNames[this.get_FileNameIndex()].value.length>0) )
	{
		try{showLoading('');}
		catch(e){}
		g_objSelectedTabIndex.value=this.get_FileNameIndex() + page.ViewFilesFirstTab;
		//public static Dictionary<string, object> GetDraft(string FileName, int FileNameIndex)
		PageMethods.GetDraft(g_objEditFileNames[this.get_FileNameIndex()].value, this.get_FileNameIndex(), this.loadDraftMessageCallback, onError);
	}
	else
	{ 
		listObject.refresh(true);
		page.setTabVisible(1,false);
			
		page.set_ErrorMessage('Could not load the selected draft.');
		return;
	}   
},
loadDraftMessageCallback:function(results)
{
	if(results==null)
	{
		page.set_ErrorMessage('Unable to load the draft.');
		return;
	}
	clientPageMethods.onReturnBuildReturnFolderObjects(results);
	if(isNaN(results.FileNameIndex))
	{
		//force the drat folder to rebuild
		foldersArray.UpdateFolderByPath(FOLDER_DRAFTS, true, true,false,5);
		page.set_ErrorMessage('Unable to load the draft.  Close this tab and try again.');
		return;
	}
	
	var ctlEdit=page.get_EditControl(results.FileNameIndex);
	
	if(ctlEdit==null)
	{
		page.set_ErrorMessage('Unable to load the draft.  Close this tab and try again.');
		return;
	}

	hideLoading();
	ctlEdit._ToolBarObject.enable();

	var message=null;
	
	var forceSaveMessage=(results.AutoDraft!=null) && (results.AutoDraft.FileFound);
		
	if((results.Draft!=null) && (results.AutoDraft!=null) && (results.Draft.FileFound) && (results.AutoDraft.FileFound))
	{
		if(confirm('There is an unsaved version of this email.  Do you want to load the unsaved changes? \r\n\r\nClick OK to load the unsaved version.'))
		{
			results.Draft.To=results.AutoDraft.To;
			results.Draft.Cc=results.AutoDraft.Cc;
			results.Draft.Bcc=results.AutoDraft.Bcc;
			results.Draft.From=results.AutoDraft.From;
			results.Draft.ReplyTo=results.AutoDraft.ReplyTo;
			results.Draft.Subject=results.AutoDraft.Subject;
			if(results.AutoDraft.IsHTML)
			{
				results.Draft.PlainTextBody="";
				results.Draft.HTMLBody=results.AutoDraft.HTMLBody;
				results.Draft.IsHTML=true;
			}
			else
			{
				results.Draft.PlainTextBody=results.AutoDraft.PlainTextBody;
				results.Draft.HTMLBody="";
				results.Draft.IsHTML=false;
			}
		}
		results.AutoDraft=null;
	}
	if((results.AutoDraft!=null) && (results.AutoDraft.FileFound))
	{
		message=results.AutoDraft;
	}
	else if((results.Draft!=null) && (results.Draft.FileFound))
	{
		message=results.Draft;
	}
	
	set_ObjectIsDisplayed(ctlEdit._MainDivCompose,true);
	set_ObjectIsDisplayed(ctlEdit._ConfirmDiv,false);

	ctlEdit.set_FileName(message.FileName);
	
	ctlEdit.setMessageBodyFormat(message.IsHTML,false);
	if (message.IsHTML)
	{
		ctlEdit.set_HTMLBody(message.HTMLBody);
		ctlEdit.set_PlainTextBody('');
	}
	else
	{
		ctlEdit.set_HTMLBody('');
		ctlEdit.set_PlainTextBody(message.PlainTextBody);
	}
	
	//sets the from address.  If the user has a display name, it will be included.  Otherwise just the email address
	ctlEdit.set_From(message.From);
	//set Save message after send
	ctlEdit.set_SaveCopy(ctlEdit._SaveCopy);
	//set reply to
	if(message.ReplyTo.length>0)
		ctlEdit.set_ReplyTo(message.ReplyTo);
	
	ctlEdit.set_To(message.To);

		
	//will set the parameter in set_showCC to true if the message has a cc or bcc value.
	ctlEdit.set_showCC(((message.Cc.length>0) || (message.Bcc.length>0)));
	ctlEdit.set_Cc(message.Cc);
	ctlEdit.set_Bcc(message.Bcc);
	
	ctlEdit.set_Subject(message.Subject);
	if(message.Subject=="")
		page.setTabText(ctlEdit.get_TabIndex(),'[No Subject]');
	else
		page.setTabText(ctlEdit.get_TabIndex(),message.Subject);
	ctlEdit.set_AttachmentList(message.Attachments,message.HasHiddenAttachments);
	
	page.setTabVisible(ctlEdit.get_TabIndex(), true);
	page.setSelectedTabIndex(ctlEdit.get_TabIndex());
	
	//now that the messages have been merged, resave them to get rid of the autodraft
	if(true==forceSaveMessage)
		ctlEdit.saveMessage(false,true);
},
setMessageBodyFormat:function(IsHTML, convertText) 
{
	if(this._ToolBarObject==null)
	{
		page.set_ErrorMessage('Can not change the mail format.  A reference to the toolbar was not found.');
		return;
	}
	//sets the toolbar display format i.e. plain text vs. rich text
	this._ToolBarObject.set_EmailFormatButton(IsHTML);
		
	var divHtmlWrapper=document.getElementById(this._DivHtmlWrapperID);
	var plainText=document.getElementById(this._PlainTextBodyID);
	if (IsHTML)
	{   
		if(convertText && (divHtmlWrapper.style.display=="none") && (this.get_PlainTextBody().length>0))
		{
			this.set_HTMLBody('')
			PageMethods.PlaintextToHtml(this.get_PlainTextBody(),this.get_FileNameIndex(),this.onConvertComplete);
		}
		divHtmlWrapper.style.display="block";
		plainText.style.display="none";
	}
	else
	{
		//when this method is called from the toolbar make sure the this._ToolBarObject.get_IsHTML() returns
		//true before the setMessageBodyFormat is called.  It could end in a circular call.
		if((true==convertText) && (plainText.style.display=="none") && (this.get_HTMLBody().length>0))
		{
			this.set_PlainTextBody('');
			PageMethods.HtmlToPlaintext(this.get_HTMLBody(),this.get_FileNameIndex(),this.onConvertComplete);
		}
		divHtmlWrapper.style.display="none";
		plainText.style.display="block";
	}
},
onConvertComplete:function(results)
{
	var ctlEdit;
	
	if(Number(results.FileNameIndex)==0)
		ctlEdit=cE0;
	else if(Number(results.FileNameIndex)==1)
		ctlEdit=cE1;
	else if(Number(results.FileNameIndex)==2)
		ctlEdit=cE2;
	
	ctlEdit.set_HTMLBody(results.HTMLBody);
	ctlEdit.set_PlainTextBody(results.PlainTextBody);
	
},
onPlaintextToHtmlError:function()
{page.set_ErrorMessage('Unable to convert the e-mail');},

get_PlainTextBody:function() 
{return getObjectValue(this._PlainTextBodyID);},
set_PlainTextBody:function(value) 
{setObjectValue(this._PlainTextBodyID, value);},
get_IsHTML:function()
{return get_ObjectIsDisplayed(this._DivHtmlWrapperID);},
get_HTMLBody:function() 
{
	try
	{
		var htmlText=iged_getById(this._HTMLBodyID);
		if(htmlText==null)
			return "";
		return htmlText.getText();
	}
	catch(e){return "";}
},
set_HTMLBody:function(value) 
{
	var htmlText=null;
	try{htmlText=iged_getById(this._HTMLBodyID);}
	catch(e) 
	{
		displayMessage(e);
		return false;
	}

	if(htmlText!=null)
	{
		htmlText.setText(value);
		return true;
	}
	
	return false;
},
get_MessageBody:function()
{
	var messageBody="";
	if(this.get_Toolbar()==null)
		return "";
	if(this.get_IsHTML())
		return this.get_HTMLBody();
	else
		return this.get_PlainTextBody();
},        
get_SaveCopy:function()
{
	if(this._SaveCopyObj==null)
		return false;
	return this._SaveCopyObj.checked;
},
set_SaveCopy:function(value)
{
	if(this._SaveCopyObj==null)
		return;
	this._SaveCopyObj.checked=(value==true);
},
//opens a new window for the user to set attachments for the current file.
ShowAttachmentWindow:function(autoSaveFirst)
{
try{
	if(this.get_FileName()==VALUE_NEWFILENAME_PLACEHOLDER)
	{
		this._ToolBarObject.disable();
		PageMethods.SaveMessage
		(
			this.get_FileNameIndex(), this.get_FileName(), true,   this.get_From(),  this.get_ReplyTo(), 
			this.get_To(), this.get_Cc(), this.get_Bcc(), this.get_Subject(),
			true, 
			this.get_MessageBody(),
			page.get_FolderPath(), g_objPageNo.value,
			true,              //supressmessages
			this.onSaveMessageCompleteForAttachments, 
			composeOnError
		);
		return;
	}
	else if(autoSaveFirst==true)
	{
		this.saveMessage(true,true);
	}
	//in case it is open when a new request happens
	var attachmentsURL="Attachments.aspx?" +   
		PARAM_ATTACHMENT_EMAIL_FILE_NAME + "=" + g_objEditFileNames[this.get_FileNameIndex()].value + "&" +
		PARAM_ATTACHMENT_DIV_ID + "=" + this._ListAttachment.id +  "&" +
		PARAM_ATTACHMENT_EMAIL_FILE_NAME_OBJECT_ID + "=" + g_objEditFileNames[this.get_FileNameIndex()].id ;
	this._AttachmentWindow=
		window.open
		(
			attachmentsURL, 
			"EditAttachments" + this.get_FileNameIndex(),
			"personalbar=no, toolbar=no, location=no, directories=no, status=yes, menubar=no, scrollbars=no, resizable=yes, copyhistory=no, width=600, height=300, left=50, top=50"
		);
	if(this._AttachmentWindow==null)
		page.set_ErrorMessage("Your browser has blocked popups.  You must enable popups for this web site in order to add attachments.  See your browser's help for direction to unblock popups.");
	else
		this._AttachmentWindow.focus();
	
	//force the folder to update when the attachment window closes.
	try
	{
		this._AttachmentWindow.onunload = this.UpdateAttachments();
	}
	catch(ex){}
}
catch(ex)
{displayMessage(ex);}

},
CloseAttachmentWindow:function()
{
	if(this._AttachmentWindow!=null)
	{
		try{this._AttachmentWindow.close();}
		catch(e){}
		finally{this._AttachmentWindow=null;}
	}
},
UpdateAttachments:function()
{
	//if the current folder is drafts, force the message list to update also
	if(page.get_FolderPath().toLowerCase()==FOLDER_DRAFTS.toLowerCase())
		foldersArray.UpdateFolderByPath(FOLDER_DRAFTS,false,true,true,5)
	else
		foldersArray.UpdateFolderByPath(FOLDER_DRAFTS,false,true,false,5)
},
onSaveMessageCompleteForAttachments:function(results)
{
	if(results==null)
	{
		//should not ever happen
		page.set_ErrorMessage('Unable to load the attachments window.  Please try again.');
		return;
	}
	var message=null;
	
	if(results.Draft!=null)
		message=results.Draft;
	else if(results.AutoDraft!=null)
		message=results.AutoDraft;
	var ctlEdit=page.get_EditControl(results.FileNameIndex);
	ctlEdit._ToolBarObject.enable();
	
	if(objectIsNull(ctlEdit) || objectIsNull(message))
	{
		//should not ever happen.  A message object should always have a FileNameIndex
		page.set_ErrorMessage('Unable to load the attachments window.  Please try again.');
		return;
	}

	ctlEdit.set_FileName(message.FileName);
	ctlEdit.ShowAttachmentWindow(false);
},
set_FileName:function(value)
{
	if(value.indexOf(VALUE_AUTO_EXTENSION)>=0)
	{
		value=value.substring(0, value.indexOf(VALUE_AUTO_EXTENSION));
	}
	g_objEditFileNames[this.get_FileNameIndex()].value=value;
},
get_FileName:function()
{return g_objEditFileNames[this.get_FileNameIndex()].value;},
//////////////////////////////////////////////////////////////////////////////////////////////////////
/*************   WebMethod definition  *********************
public static Dictionary<string, object> SaveMessage
(
	int FileNameIndex, string FileName, bool AutoSave, string From, string ReplyTo, 
	string To, string Cc, string Bcc, string Subject, 
	bool IsHTML, 
	string MessageBody, 
	string CurrentFolderPath, int? PageNo
)
*************************************************************/
//////////////////////////////////////////////////////////////////////////////////////////////////////
saveMessage:function(autoSave,supressMessages)
{
	this._ToolBarObject.disable();
	PageMethods.SaveMessage
	(
		this.get_FileNameIndex(), this.get_FileName(), autoSave,   this.get_From(),  this.get_ReplyTo(), 
		this.get_To(), this.get_Cc(), this.get_Bcc(), this.get_Subject(),
		this.get_IsHTML(), 
		this.get_MessageBody(),
		page.get_FolderPath(), g_objPageNo.value,
		supressMessages,
		this.onSaveMessageComplete, 
		composeOnError
	);
},
//////////////////////////////////////////////////////////////////////////////////////////////////////

/////////////////////////////////////////////////////////////////////////////////////////////////////////        
//savemessage callback
/////////////////////////////////////////////////////////////////////////////////////////////////////////        
onSaveMessageComplete:function (results) 
{   
	if(results==null)
	{
		//should not ever happen
		page.set_ErrorMessage('The message was not saved.');
		return;
	}

	var ctlEdit = page.get_EditControl(results.FileNameIndex);
	ctlEdit._ToolBarObject.enable();
	var message=null;
	
	clientPageMethods.onReturnBuildReturnFolderObjects(results);

	if(results.Draft!=null)
	{
		message=results.Draft;
	}
	else if(results.AutoDraft!=null)
	{
		message=results.AutoDraft;
	}
	else
	{
		//should not ever happen.  A message object should always be returned.
		if(results.ErrorMessage.length==0)
			page.set_ErrorMessage('The message was not saved.');
		return;
	}
	ctlEdit.set_FileName(message.FileName);

	
	if(ctlEdit.get_Subject()=="")
		page.setTabText(ctlEdit.get_TabIndex(),'[No Subject]');
	else
		page.setTabText(ctlEdit.get_TabIndex(),ctlEdit.get_Subject());
	
},
printMessage:function() 
{
	
	this._ToolBarObject.disable();
	PageMethods.SaveMessage
	(
		this.get_FileNameIndex(), this.get_FileName(), true,   this.get_From(),  this.get_ReplyTo(), 
		this.get_To(), this.get_Cc(), this.get_Bcc(), this.get_Subject(),
		this.get_IsHTML(), 
		this.get_MessageBody(),
		page.get_FolderPath(), g_objPageNo.value,
		true,              //supressMessages
		this.onSaveMessageCompleteForPreview, 
		composeOnError
	);
},
onSaveMessageCompleteForPreview:function(results)
{
	if(results==null)
	{
		//should not ever happen
		page.set_ErrorMessage('Unable to load the attachments window.  Please try again.');
		return;
	}
	var message=null;
	
	if(results.Draft!=null)
		message=results.Draft;
	else if(results.AutoDraft!=null)
		message=results.AutoDraft;
	var ctlEdit=page.get_EditControl(results.FileNameIndex);
	ctlEdit._ToolBarObject.enable();
	
	if(objectIsNull(ctlEdit) || objectIsNull(message))
	{
		//should not ever happen.  A message object should always have a FileNameIndex
		page.set_ErrorMessage('Unable to load the attachments window.  Please try again.');
		return;
	}
		
	ctlEdit.set_FileName(message.FileName);
	var fileName = PARAM_FOLDER_PATH + "=" + FOLDER_DRAFTS + "&" +  PARAM_ATTACHMENT_EMAIL_FILE_NAME + "=" + ctlEdit.get_FileName();
	var attachmentsURL="ViewMessage.aspx?" + fileName;
	
	ctlEdit._PrintPreview=
		window.open
		(
			attachmentsURL, 
			"PrintPreview",
			"personalbar=no, toolbar=no, location=no, directories=no, status=no, menubar=yes, scrollbars=yes, resizable=yes, copyhistory=no, fullscreen=no"
		);

	if(ctlEdit._PrintPreview==null)
		page.set_ErrorMessage('Your browser has blocked popups.  You must enable popups for this web site in order to print.');
	else
		ctlEdit._PrintPreview.focus();
},
clearMessage:function()
{
	this.CloseAttachmentWindow();
	if(page.HasTabControl())
	{
		page.get_TabControl().Tabs[this.get_TabIndex()].setVisible(false);
		page.get_TabControl().Tabs[this.get_TabIndex()].setText('');
	}
	
	set_ObjectIsDisplayed(this._MainDivCompose,true);
	set_ObjectIsDisplayed(this._ConfirmDiv,false);
			
	this.set_FileName("");
	
	this.setMessageBodyFormat(this._EnableRTF, false);
	this.set_HTMLBody('');
	this.set_PlainTextBody('');
	this.set_To('');
	this.set_From('');
	this.set_Cc('');
	this.set_Bcc('');
	this.set_Subject('');
	this.set_AttachmentList(null,0);
	
	//sets the from address.  If the user has a display name, it will be included.  Otherwise just the email address
	this.set_From(g_objFrom.value);
	//set Save message
	this.set_SaveCopy(this._SaveCopy);
	//set reply to
	if(g_objReplyTo.value.length>0)
		this.set_ReplyTo(g_objReplyTo.value);
	else
		this.set_ReplyTo('');
	this.set_showCC(false);
},

closeMessage:function()
{
	try
	{
		var fileName=this.get_FileName();
		if(fileName.length>0)
			clientPageMethods.deleteDraft(fileName, true);
		this.clearMessage();
	}
	catch(e)
	{{displayMessage(e);}}
	
	return false;
},

//////////////////////////////////////////////////////////////////////////////////////////////////////
//send message methods
//////////////////////////////////////////////////////////////////////////////////////////////////////
sendMessage:function()
{
	try
	{
		showLoading('1');
		this._ToolBarObject.disable();
		this.CloseAttachmentWindow();

		var messageBody="";
		if(this.get_IsHTML())
			messageBody=this.get_HTMLBody() + " ";
		else
			messageBody=this.get_PlainTextBody() + " ";
		/*
		public static Dictionary<string, object> SendMessage
		(
		string FileName, string From, string ReplyTo, string To, string Cc, 
		string Bcc, string Subject, bool IsHTML, string MessageBody, 
		bool SaveCopy, int FileNameIndex, 
		string CurrentFolderPath, int? PageNo
		)
		*/
		PageMethods.SendMessage
		(
			this.get_FileName(), this.get_From(), this.get_ReplyTo(), this.get_To(), this.get_Cc(), 
			this.get_Bcc(), this.get_Subject(), this.get_IsHTML(), messageBody, 
			this.get_SaveCopy(), this.get_FileNameIndex(),
			page.get_FolderPath(), g_objPageNo.value,
			this.onSendMessageComplete, composeOnError
		);
	}
	catch(e){displayMessage(e);}
},
onSendMessageComplete:function(results) 
{
	
	hideLoading();
	
	var ctlEdit = page.get_EditControl(results.FileNameIndex);
	try
	{
		if(results.Success!=true)
		{
			ctlEdit.set_To(results.To);
			ctlEdit.set_Cc(results.Cc);
			ctlEdit.set_Bcc(results.Bcc);
		}
		else
		{
			ctlEdit.clearMessage();
			//results.NewAddresses contains the recipients of the sent email that were not in the senders' contact list
			//results.NewAddresses contains the formateed divs to display.  
			if((results.NewAddresses!=null) &&  (results.NewAddressCount>=0))
			{
				ctlEdit.set_FileName(VALUE_CONFIRM_SEND_PLACEHOLDER);
				set_ObjectIsDisplayed(ctlEdit._MainDivCompose,false);
				set_ObjectIsDisplayed(ctlEdit._ConfirmDiv,true);
				setObjectInnerHTML(ctlEdit._AddContactsDiv, results.NewAddresses);
				ctlEdit.ResetRecipientCount(results.NewAddressCount);
				page.setSelectedTabIndex(ctlEdit.get_TabIndex());
				page.setTabText(ctlEdit.get_TabIndex(), "[Add Contacts]");
			}
		}
	}
	finally
	{
		ctlEdit._ToolBarObject.enable();
		
		clientPageMethods.onReturnBuildReturnFolderObjects(results);
	}
},

//only used after sending a message.
closeConfirm:function()
{
	try
	{this.clearMessage();}
	catch(e)
	{displayMessage(e);}
	return false;
},
//////////////////////////////////////////////////////////////////////////////////////////////////////
//end of send message methods
//////////////////////////////////////////////////////////////////////////////////////////////////////


//////////////////////////////////////////////////////////////////////////////////////////////////////
//Decrements the recipient count each time 
RecipientAdded:function()
{
	this.RecipeintsCounted=this.RecipeintsCounted-1;
	if(this.RecipeintsCounted<=0)
	{this.clearMessage();}
},
//When there are email recipients to be added after sending email messages, 
//Sets the count of emails to be added.  The count will be decremented each time an address is sent
ResetRecipientCount:function(NewCount)
{
	this.RecipeintsCounted=NewCount;
	if(NewCount<=0)
	{this.clearMessage();}
},
//////////////////////////////////////////////////////////////////////////////////////////////////////

//////////////////////////////////////////////////////////////////////////////////////////////////////
//delete current message
//////////////////////////////////////////////////////////////////////////////////////////////////////
deleteMessage:function() 
{
	try
	{
		var fileName=this.get_FileName();
			
		clientPageMethods.deleteDraft(fileName, false);
		this.clearMessage();
	}
	catch(e)
	{{displayMessage(e);}}
	
	return false;
},



//////////////////////////////////////////////////////////////////////////////////////////////////////
//Check Spelling Functions
//////////////////////////////////////////////////////////////////////////////////////////////////////
checkSpelling:function()
{
try{
	var checker=null;
	
	if(this.get_IsHTML()) 
		checker=ig_getWebControlById(page.get_SpellCheckXMLID());
	else
		checker=ig_getWebControlById(page.get_SpellCheckID());
		
	if (checker==null)
	{
		page.set_ErrorMessage('unable to load the spell checker.');
		return false;
	}
	this.saveMessage(true,true);
	
	if(this.get_IsHTML()) 
	{
		if(this.get_HTMLBody().length>0)
			checker.checkTextComponent(this._HTMLBodyID);
		else
			page.set_ErrorMessage('the e-mail has no text');
	}
	else
	{
		if(this.get_PlainTextBody().length>0)
			checker.checkTextComponent(this._PlainTextBodyID);
		else
			page.set_ErrorMessage('the e-mail has no text');
	}
}catch(ex){displayMessage(ex);}
return false;
},

//////////////////////////////////////////////////////////////////////////////////////////////////////
onremoveAttachmentComplete:function(results)
{
	page.set_Messages(results);

	var emailMessage;
	if(results.Draft!=null)
		emailMessage=results.Draft;
	else if(results.AutoDraft!=null)
		emailMessage=results.AutoDraft;
	else 
	{
		page.setErrorMessage("The attachment may not have been removed.  Save the draft, close this tab and reload the draft to verify the attachment ws revmoved.");
		return;
	}
	
	var ctl=page.get_EditControl(results.FileNameIndex);
	ctl.set_AttachmentList(emailMessage.Attachments,emailMessage.HasHiddenAttachments);
	clientPageMethods.onReturnBuildReturnFolderObjects(results);
	hideLoading();
},
onremoveAttachmentError:function(error)
{
	page.set_ErrorMessage("There was an error removing the attachment.");
	composeOnError(error);
},

//fieldName will be either "To", "Cc", or "Bcc"
//public static Dictionary<string, object> ValidateAddresses
//(int FileNameIndex, string FileName, string From, string ReplyTo, 
//string To, string Cc, string Bcc, string Subject, 
//bool IsHTML, string MessageBody, string CurrentFolderPath, int? PageNo)
validateAddresses:function()
{
	if( (this.get_To().trim().length==0) && (this.get_Cc().trim().length==0) && (this.get_Bcc().trim().length==0))
	{
		return false;
	}
	PageMethods.ValidateAddresses
		(
			this.get_FileNameIndex(), this.get_FileName(), this.get_From(),  this.get_ReplyTo(), 
			this.get_To(), this.get_Cc(), this.get_Bcc(), this.get_Subject(),
			this.get_IsHTML(), this.get_MessageBody(), page.get_FolderPath(), g_objPageNo.value,
			this.onValidateAddressesComplete, 
			onError
		);
},
onValidateAddressesComplete:function(results)
{
	var ctlEdit = page.get_EditControl(results.FileNameIndex);
	
	ctlEdit.onSaveMessageComplete(results)
	ctlEdit.set_To(results.AutoDraft.To);
	ctlEdit.set_Bcc(results.AutoDraft.Bcc);
	ctlEdit.set_Cc(results.AutoDraft.Cc);
},

showHiddenAttachments:function()
{
	//GetAttachmentsList(string FolderPath, string FileName, int FileNameIndex, bool ShowHiddenAttachments)
	PageMethods.GetAttachmentsList
	(
		FOLDER_DRAFTS, g_objViewFileNames[this.get_FileNameIndex()].value, this.get_FileNameIndex(), "", true, 
		this.onUpdateAttachmentsListSuccess, this.onUpdateAttachmentsListError
	);
},
onUpdateAttachmentsListSuccess:function(results)
{
	var ctlEdit = page.get_EditControl(results.FileNameIndex);
	
	if(ctlEdit==null)
		return;
	
	ctlEdit.set_AttachmentList(results.Attachments,results.HasHiddenAttachments);
},
onUpdateAttachmentsListError:function(results)
{
	
}

}
WebMail2.Compose.registerClass("WebMail2.Compose",WebMail2.MessageBase);

