﻿//Copyright WebMail Wou3, Inc. 2007-2010
Type.registerNamespace('WebMail2');
WebMail2.View=function
(
	From,ReplyTo,To,Cc,Bcc,
	Subject,DateDiv,HTMLBodyDiv, 
	FileNameIndex, ToolBarObject,
	ListAttachmentDivID,ShowHiddenAttachmentsID,DefaultAttachmentListHTML,AttachmentPath
)
{ 
	var baseParams=new Array
	(
		From,ReplyTo,To,Cc,Bcc,
		Subject,null,HTMLBodyDiv,
		FileNameIndex,ToolBarObject,
		ListAttachmentDivID,ShowHiddenAttachmentsID,DefaultAttachmentListHTML
	);
	WebMail2.View.initializeBase(this,baseParams);
	
	if(this._ToolBarObject!=null)
		this._ToolBarObject.set_View(this);

	this._DateDiv = DateDiv;
	this._PrintPreview=null;
	this._ForceHTML=false;
	//HTML Div reference for displaying the HTML Body of a message
	this._HTMLBodyDiv=null;
	this._AttachmentPath=AttachmentPath;
	
	try
	{
		if(page.pageName().toLowerCase() == "default.aspx")
			page.addViewControl(this.get_FileNameIndex(),this);
	}
	catch(e){}
}

WebMail2.View.prototype=
{
showMessage:function()
{
	this._ForceHTML=false;
	showLoading('');
	if((this.get_FileNameIndex() >= 0) && (g_objViewFileNames[this.get_FileNameIndex()].value.length>0) )
	{
		g_objSelectedTabIndex.value=this.get_FileNameIndex() + page.ViewFilesFirstTab;
		//public static object GetMessage(string FolderPath, string FileName, int FileNameIndex, bool ForcePlainText)
		PageMethods.GetMessage(page.get_FolderPath(), g_objViewFileNames[this.get_FileNameIndex()].value, this.get_FileNameIndex(), false, this.onShowMessageComplete, this.onShowMessageError)
	}
	else
	{ 
		//make the message list update again.
		listObject.refresh(true);
		page.setTabVisible(1,false);
			
		page.set_ErrorMessage('Could not find the selected file');
		return;
	}   
},
onShowMessageComplete:function(emailMessage) 
{
	if(emailMessage==null)
		return;
	
	var ctlView;
	switch(Number(emailMessage.FileNameIndex))
	{
		case (0):
		case (1):
		case (2):
			ctlView=page.get_ViewControl(Number(emailMessage.FileNameIndex));
			break;
		default:
			//this will close any pending view updates
			cV0.onShowMessageError('Could not determine the FileViewIndex');
			return;
	}    


	try
	{
		if(emailMessage.FileFound==false)
		{
			page.set_ErrorMessage(emailMessage.ErrorMessage);
						
			ctlView.closeMessage();
			
			foldersArray.get_FolderByPath(page.get_FolderPath()).UpdateFolder(true, false, true, 5);
			return;
		}
		
		
		ctlView.set_FileName(emailMessage.WebmailPath + "\\" + emailMessage.FileName);
		ctlView.set_To(emailMessage.To);
		ctlView.set_ReplyTo(emailMessage.ReplyTo);
		ctlView.set_From(emailMessage.From);
		ctlView.set_Cc(emailMessage.Cc);
		ctlView.set_Bcc(emailMessage.Bcc);
		ctlView.set_Subject(emailMessage.Subject);
		ctlView.set_Date(emailMessage.ReceivedDate);
		ctlView.set_AttachmentList(emailMessage.Attachments,emailMessage.HasHiddenAttachments);
		ctlView.set_HasHiddenAttachments(emailMessage.HasHiddenAttachments);

		page.setTabVisible(ctlView.get_TabIndex(),true);
		if(emailMessage.Subject=="")
			page.setTabText(ctlView.get_TabIndex(),'[No Subject]');
		else
			page.setTabText(ctlView.get_TabIndex(),emailMessage.Subject);
		page.setSelectedTabIndex(ctlView.get_TabIndex());

		ctlView._ToolBarObject.updateToolbar();
		
		if(emailMessage.IsHTML)
		{
			if(emailMessage.HTMLBody.length>0)
			{
				if(!ctlView.set_MessageBody(emailMessage.HTMLBody))
				{
					if(ctlView._ForceHTML==false)
					{
						ctlView._ForceHTML=true;
						
						PageMethods.GetMessage(
							page.get_FolderPath(),
							g_objViewFileNames[ctlView.get_FileNameIndex()].value,
							ctlView.get_FileNameIndex(),
							true,
							ctlView.onShowMessageComplete,
							ctlView.onShowMessageError);
						
						return;
					}
					else
					{
						page.set_ErrorMessage('The message body for this email can not be displayed.  Contact technical support');
					}
				}
			}
			else
				ctlView.set_MessageBody("[The message has no body]");
		}
		else
		{
			if(emailMessage.PlainTextBody.length>0)
				ctlView.set_MessageBody(emailMessage.PlainTextBody);
			else
				ctlView.set_MessageBody("[The message has no body]");
		}
	}
	catch(e)
	{
		displayMessage(e);
	}
	finally
	{
		hideLoading();
		ctlView=null;
		if(emailMessage.FolderIndexHeader!=null)
			foldersArray.set_FolderIndexHeader(emailMessage.FolderIndexHeader);
	}
},

onShowMessageError:function(error) 
{
	listObject.refresh(true);
	onError(error);
	page.set_ErrorMessage("Unable to retrieve the email message");
	hideLoading();
},
showHiddenAttachments:function()
{
	//Dictionary<string, object> GetAttachmentsList(string FolderPath, string FileName, int FileNameIndex, string AttachmentPath, bool ShowHiddenAttachments)
	PageMethods.GetAttachmentsList
	(
		page.get_FolderPath(), g_objViewFileNames[this.get_FileNameIndex()].value, this.get_FileNameIndex(), 
		this._AttachmentPath, true,
		this.onUpdateAttachmentsListSuccess, this.onUpdateAttachmentsListError
	);
},
onUpdateAttachmentsListSuccess:function(results)
{
	var ctlView=page.get_ViewControl(Number(results.FileNameIndex));
	
	if(ctlView==null)
		return;
	
	ctlView.set_AttachmentList(results.Attachments,results.HasHiddenAttachments);
},
onUpdateAttachmentsListError:function(results)
{
	
},
set_HasHiddenAttachments:function(hasHidden)
{
	if(this._ShowHiddenAttachments==null)
	{
		try{this._ShowHiddenAttachments=document.getElementById(this._ShowHiddenAttachmentsID);}
		catch(e){}
		if(this._ShowHiddenAttachments==null)
			return;
	}
	if(hasHidden==1)
		this._ShowHiddenAttachments.style.display='block';
	else
		this._ShowHiddenAttachments.style.display='none';
},
closePrint:function()
{
	if(this._PrintPreview!=null)
	{
		try{this._PrintPreview.close();}
		catch(e){}
		finally{this._PrintPreview=null;}
	}
},
closeMessage:function()
{
	page.setTabVisible(this.get_TabIndex(),false);
	page.setTabText(this.get_TabIndex(),"");
	
	this.set_To('');
	this.set_ReplyTo('');
	this.set_From('');
	this.set_Cc('');
	this.set_Bcc('');
	this.set_Subject('');
	this.set_Date('');
	this.set_AttachmentList(null,0);
	this.set_MessageBody('-');
	g_objViewFileNames[this.get_FileNameIndex()].value="";
	return false;
},
reply:function()
{
	var editMessageIndex=page.addEditFileName(g_objViewFileNames[this.get_FileNameIndex()].value);
	if(editMessageIndex<0)
		return false;

	g_objEditFileNames[editMessageIndex].value=g_objViewFileNames[this.get_FileNameIndex()].value;
	clientPageMethods.createReplyToMessage(g_objEditFileNames[editMessageIndex].value, editMessageIndex);
	
	this.closeMessage();
},
replyToAll:function()
{
	var editMessageIndex=page.addEditFileName(g_objViewFileNames[this.get_FileNameIndex()].value);
	if(editMessageIndex<0)
		return false;
		
	g_objEditFileNames[editMessageIndex].value=g_objViewFileNames[this.get_FileNameIndex()].value;
	clientPageMethods.createReplyToAllMessage(g_objEditFileNames[editMessageIndex].value, editMessageIndex);

	this.closeMessage();

},

forward:function()
{
	var editMessageIndex=page.addEditFileName(g_objViewFileNames[this.get_FileNameIndex()].value);
	if(editMessageIndex<0)
		return false;

	g_objEditFileNames[editMessageIndex].value=g_objViewFileNames[this.get_FileNameIndex()].value;
	clientPageMethods.createForwardMessage(g_objEditFileNames[editMessageIndex].value, editMessageIndex);
	this.closeMessage();
},

set_FileName:function(value){g_objViewFileNames[this.get_FileNameIndex()].value=value;},
get_FileName:function(){return g_objViewFileNames[this.get_FileNameIndex()].value;},
set_MessageBody:function(value)
{
	if(this._HTMLBodyDiv==null)
		this._HTMLBodyDiv=document.getElementById(this._HTMLBodyID);
	try
	{
		if(this._HTMLBodyDiv!=null)
		{
			this._HTMLBodyDiv.innerHTML=value;
			return true;
		}
	}
	catch(e)
	{}
	return false;
},
get_MessageBody:function()
{
	if(this._HTMLBodyDiv==null)
	{
		this._HTMLBodyDiv=document.getElementById(this._HTMLBodyID);
		if(this._HTMLBodyDiv!=null)
			return this._HTMLBodyDiv.innerHTML;
		else
			return "";
	}
	return this._HTMLBodyDiv.innerHTML;
},
set_Date:function(value){setObjectInnerHTML(this._DateDiv, value);},
get_Date:function(){return getObjectInnerHTML(this._DateDiv);},

moveMessage:function(destinationFolderPath) 
{
	var fileName=g_objViewFileNames[this.get_FileNameIndex()].value.toLowerCase().replace(destinationFolderPath.toLowerCase(),'');
	
	//if after removing the destinationFolderPath from the filepath, thre are no '\' left, the destionation path is the same
	//start checking at character [1] because the first character will stil be a '\'
	if(fileName.indexOf('\\',1)<0)
	{
		page.set_ErrorMessage('the destination and source folders can not be the same');
		return false;
	}
	
	clientPageMethods.moveMessage(g_objViewFileNames[this.get_FileNameIndex()].value, destinationFolderPath);
	
	this._LastSelected=null;
	
	this.closeMessage()
	return false;
},
printMessage:function() 
{
	var fileName = PARAM_ATTACHMENT_EMAIL_FILE_PATH + "=" + escape(this.get_FileName()) ;
	var attachmentsURL="ViewMessage.aspx?" + fileName;
	
	this._PrintPreview=
		window.open
		(
			attachmentsURL, 
			"Attachments",
			"personalbar=no, toolbar=no, location=no, directories=no, status=no, menubar=yes, scrollbars=yes, resizable=yes, copyhistory=no, fullscreen=no"
		);

	if(this._PrintPreview==null)
		page.set_ErrorMessage('Your browser has blocked popups.  You must enable popups for this web site in order to print.');
	else
		this._PrintPreview.focus();
	return false;
},
deleteMessage:function() 
{
	try
	{
		var fileName=this.get_FileName();
		clientPageMethods.deleteMessage(fileName, false);
		this.closeMessage();
	}
	catch(e)
	{displayMessage(e);}
	
	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_ViewControl(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.");
	onError(error);
	hideLoading();
},

//////////////////////////////////////////////////////////////////////////////////////////////////////
markAsSpam:function() 
{   
	//MarkMessageAsSpam(string SourceFilePath, string CurrentFolderPath, int? PageNo)
	PageMethods.MarkMessageAsSpam(this.get_FileName(), page.get_FolderPath(), g_objPageNo.value, this.setSpamComplete, this.setSpamError);
	
	this.closeMessage();
},
setSpamComplete:function(results)
{
	clientPageMethods.onReturnBuildReturnFolderObjects(results);
},
//if there is an error, display an error message and reload the message list
setSpamError:function(error)
{
	onError(error);
	page.set_ErrorMessage('Unable to set the messages\' spam setting.  If this problem persists, contact support.');
	try{listObject.refresh(false);}
	catch(e){}
}
//////////////////////////////////////////////////////////////////////////////////////////////////////


}
WebMail2.View.registerClass("WebMail2.View",WebMail2.MessageBase);

