﻿//Copyright WebMail Wou3, Inc. 2007-2010
Type.registerNamespace('WebMail2');
WebMail2.MessageList=function
(
	ToolbarObject, 
	SiblingValueControlIDs, SelectedValuesControlID,  
	UnSelectedColorBack, SelectedColorBack, SelectedValueAttribute,
	CheckBoxControlID, ListTabIndex, 
	PageSize, MessageListID, LoadingImageID,
	MessageFromID, SortFromImgID, SortSubjectImgID, SortDateImgID, SortSizeImgID
)
{ 
	var baseParams=new Array
	(
		MessageListID,
		SiblingValueControlIDs,
		SelectedValuesControlID,
		UnSelectedColorBack,
		SelectedColorBack,
		SelectedValueAttribute,
		CheckBoxControlID,
		ListTabIndex,
		ToolbarObject
	);
	WebMail2.MessageList.initializeBase(this,baseParams);
	
	if(this._ToolbarObject!=null)
		this._ToolbarObject.set_MessageList(this);
	
	this._PageSize=PageSize;
	this._LoadingImage=document.getElementById(LoadingImageID);
	this._MessageFromDiv=document.getElementById(MessageFromID);
	this._SortFromImg=document.getElementById(SortFromImgID);
	this._SortSubjectImg=document.getElementById(SortSubjectImgID);
	this._SortDateImg=document.getElementById(SortDateImgID);
	this._SortSizeImg=document.getElementById(SortSizeImgID);

}
WebMail2.MessageList.prototype=
{

//////////////////////////////////////////////////////////////////////////////////////////////////////
//sort is called when a user clicks on a list header 
//sortColumn is the column name to sort by
//newSortOrder==the sort order if this method is called.  
//if the current column is the sort column, newSortOrder is the opposite of the current sort order
//if the current column is not he sort column, newSortOrder is the default sort order for the column
sort:function(sortColumn,defaultSortOrder)
{
	hideListContextMenu();
	hideCalendarContextMenu();
	showLoading('');
	//the sort order will look at the current image selected for the column
	//if the image stype.display=='none', use the default sort order.
	//if it is not display=='none', 
	//  use the ascending order if 'icon_arrow_dn.gif' is displayed
	//  use the descending order if 'icon_arrow_up.gif is displayed
	//If the image is displayed, set the image in the opposite direction
	//if the image is not displayed, set it to the default direction
	switch(sortColumn)
	{
		case 'From':
		case 'To':
			//The From and To setting are not changing after the page is loading.
			//The Sent/Drafts folder should always sort on the To when from/to is selected
			//All others are sorted on 'From'
			switch(page.get_FolderPath())
			{
				case FOLDER_SENT:
				case FOLDER_DRAFTS:
					sortColumn="To";
					break;
				default:
					sortColumn="From";
					break;
			}
			
			if(this._SortFromImg.style.display!='none')
			{
				defaultSortOrder=(this._SortFromImg.src.toLowerCase().indexOf(this._ImgDown.toLowerCase())>=0);
				if(defaultSortOrder)
					this._SortFromImg.src=this._ImgDown;
			}
			else
			{
				if(defaultSortOrder)
					this._SortFromImg.src=this._ImgUp;
				else
					this._SortFromImg.src=this._ImgDown;
			}
			this._SortFromImg.style.display='inline';
			this._SortSubjectImg.style.display='none';
			this._SortDateImg.style.display='none';
			this._SortSizeImg.style.display='none';
			break;
		case 'Subject':
			if(this._SortSubjectImg.style.display!='none')
			{
				defaultSortOrder=(this._SortSubjectImg.src.toLowerCase().indexOf(this._ImgDown.toLowerCase())>=0);
				if(defaultSortOrder)
					this._SortSubjectImg.src=this._ImgDown;
			}
			else
			{
				if(defaultSortOrder)
					this._SortSubjectImg.src=this._ImgUp;
				else
					this._SortSubjectImg.src=this._ImgDown;
			}
			this._SortFromImg.style.display='none';
			this._SortSubjectImg.style.display='inline';
			this._SortDateImg.style.display='none';
			this._SortSizeImg.style.display='none';
			break;
		case 'DateReceived':
			if(this._SortDateImg.style.display!='none')
			{
				defaultSortOrder=(this._SortDateImg.src.toLowerCase().indexOf(this._ImgDown.toLowerCase())>=0);
				if(defaultSortOrder)
					this._SortDateImg.src=this._ImgDown;
			}
			else
			{
				if(defaultSortOrder)
					this._SortDateImg.src=this._ImgUp;
				else
					this._SortDateImg.src=this._ImgDown;
			}
			this._SortFromImg.style.display='none';
			this._SortSubjectImg.style.display='none';
			this._SortDateImg.style.display='inline';
			this._SortSizeImg.style.display='none';
			break;
		case 'Size':
			if(this._SortSizeImg.style.display!='none')
			{
				defaultSortOrder=(this._SortSizeImg.src.toLowerCase().indexOf(this._ImgDown.toLowerCase())>=0);
				if(defaultSortOrder)
					this._SortSizeImg.src=this._ImgDown;
			}
			else
			{
				if(defaultSortOrder)
					this._SortSizeImg.src=this._ImgUp;
				else
					this._SortSizeImg.src=this._ImgDown;
			}
			this._SortFromImg.style.display='none';
			this._SortSubjectImg.style.display='none';
			this._SortDateImg.style.display='none';
			this._SortSizeImg.style.display='inline';
			break;
	}
	//public static Dictionary<string, object> SetFolderSortOrder(string folderPath, string sortColumn, bool sortAscending, int? PageNo)
	PageMethods.SetFolderSortOrder(page.get_FolderPath(),sortColumn,true==defaultSortOrder,g_objPageNo.value,this.onReturnBuildReturnFolderObjects,this.onSortError);
},
onReturnBuildReturnFolderObjects:function(results)
{
	hideLoading();
	clientPageMethods.onReturnBuildReturnFolderObjects(results);
},
onSortError:function(errors)
{
	hideLoading();
	listObject.refresh(true);
},
//////////////////////////////////////////////////////////////////////////////////////////////////////

//////////////////////////////////////////////////////////////////////////////////////////////////////
//changes the style so the message list updates without updating the messageList from the server.
//////////////////////////////////////////////////////////////////////////////////////////////////////
markAsReadUpdateList:function(selectedItems,markMessagesAsRead)
{
	var messagesFoundAndMarkedAsRead = new Array();

	if(selectedItems.length>0)
	{
		//turns the selectedItems list into an array
		var anchorIds=selectedItems.split(FORMAT_ARRAY_ITEM_SEPERATOR);
		
		//loop through each selected item and get its actual value
		for(var i=0; i<anchorIds.length; i++)
		{
			if(anchorIds[i].length>0)
			{
				var objSelected=document.getElementById(anchorIds[i]);
				if(objSelected!=null)
				{
					//if the selected row's className=this._SelectedColorBack, then toggle the marked as read value and toggle the bold/non bold style
					var div=document.getElementById(objSelected.getAttribute(ATT_LINE_DIV_ID));
					if(div!=null)
					{
						if(div.className==this._SelectedColorBack)
						{
							Array.add(messagesFoundAndMarkedAsRead, objSelected.getAttribute(this._SelectedValueAttribute));
							if(markMessagesAsRead)
								div.style.fontWeight='';
							else
								div.style.fontWeight='bold';
						}
					}
				}
			}
		}
	}
	return messagesFoundAndMarkedAsRead;
},
markAsRead:function(markMessagesAsRead) 
{ 
	var selectedValues=this.markAsReadUpdateList(this._SelectedValuesControl.value,markMessagesAsRead);
	
	if((selectedValues.length>0) && (selectedValues[0].length==0))
		Array.removeAt(selectedValues,0);
	if((selectedValues.length>0) && (selectedValues[selectedValues.length-1].length==0))
		Array.removeAt(selectedValues,selectedValues.length-1);
		
	if(selectedValues.length>0)
	{
		if(selectedValues[0].length==0)
			Array.removeAt(selectedValues,0);
		if(selectedValues.length>0)
		{
		PageMethods.SetMessagesReadStatus(page.get_FolderPath(), g_objPageNo.value, selectedValues, markMessagesAsRead, this.onMarkAsReadComplete, this.onMarkAsReadError);
		}
	}
	else
	{
		page.set_ErrorMessage('you have not selected a message to mark');
		return;
	}
},
onMarkAsReadComplete:function(results)
{
	clientPageMethods.onReturnBuildReturnFolderObjects(results);
},
//if there is an error, display an error message and reload the message list
onMarkAsReadError:function(error)
{
	onError(error);
	page.set_ErrorMessage('unable to mark all of the messages as read.  Please try again.  If this problem persists, contact technical support');
	try{listObject.refresh(false);}
	catch(e){}
},
//////////////////////////////////////////////////////////////////////////////////////////////////////

//////////////////////////////////////////////////////////////////////////////////////////////////////
markAsSpam:function() 
{   
	var selectedValues=this.getSelectedValuesArray(false, true, FORMAT_ARRAY_ITEM_SEPERATOR);
		
	if((selectedValues.length>0) && (selectedValues[0].length==0))
		Array.removeAt(selectedValues,0);
	if((selectedValues.length>0) && (selectedValues[selectedValues.length-1].length==0))
		Array.removeAt(selectedValues,selectedValues.length-1);
		
	//Dictionary<string, object> MarkMessagesAsSpam(string FolderPath, string[] FileNames, int? PageNo)
	if(selectedValues.length>0)
		PageMethods.MarkMessagesAsSpam(page.get_FolderPath(),selectedValues, g_objPageNo.value, this.setSpamComplete, this.setSpamError);
	else
	{
		if(page.get_FolderPath().toLowerCase()==FOLDER_SPAM.toLowerCase())
			page.set_ErrorMessage('No messages selected to mark as not spam.');
		else
			page.set_ErrorMessage('No messages selected to mark as spam.');
		return;
	}
},
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){}
},
//////////////////////////////////////////////////////////////////////////////////////////////////////

//////////////////////////////////////////////////////////////////////////////////////////////////////
//Toolbar references
get_Toolbar:function() {return this._ToolbarObject;},
//////////////////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////////////////
reply:function()
{
	if(this.getLastSelected()==null)
	{
		page.set_ErrorMessage('Select the item to reply to');
		return;
	}
	
	try
	{
		var editMessageIndex=page.addEditFileName(page.get_FolderPath() + "\\" + this._LastSelected.getAttribute(this._SelectedValueAttribute));
		if(editMessageIndex<0)
			return false;
			
		g_objEditFileNames[editMessageIndex].value=page.get_FolderPath() + "\\" + this._LastSelected.getAttribute(this._SelectedValueAttribute);
		clientPageMethods.createReplyToMessage(g_objEditFileNames[editMessageIndex].value, editMessageIndex);
		
		this.markAsReadUpdateList(this._LastSelected.id,true);
	}
	catch(e){page.set_ErrorMessage('Reselect the item to reply to');}
	return false;
},
replyToAll:function()
{
	try
	{
		if(this.getLastSelected()==null)
		{
			page.set_ErrorMessage('Select the item to reply to');
			return;
		}
		var editMessageIndex=page.addEditFileName(page.get_FolderPath() + "\\" + this._LastSelected.getAttribute(this._SelectedValueAttribute));
		if(editMessageIndex<0)
			return false;
			
		g_objEditFileNames[editMessageIndex].value=page.get_FolderPath() + "\\" + this._LastSelected.getAttribute(this._SelectedValueAttribute);
		clientPageMethods.createReplyToAllMessage(g_objEditFileNames[editMessageIndex].value, editMessageIndex);
		
		this.markAsReadUpdateList(this._LastSelected.id,true);
	}
	catch(e){page.set_ErrorMessage('Reselect the item to reply to');}
	return false;        
},
forward:function()
{
	if(this.getLastSelected()==null)
	{
		page.set_ErrorMessage('Select the item to forward');
		return;
	}
	try
	{
		var editMessageIndex=page.addEditFileName(page.get_FolderPath() + "\\" + this._LastSelected.getAttribute(this._SelectedValueAttribute));
		if(editMessageIndex<0)
			return false;
			
		g_objEditFileNames[editMessageIndex].value=page.get_FolderPath() + "\\" + this._LastSelected.getAttribute(this._SelectedValueAttribute);
		clientPageMethods.createForwardMessage(g_objEditFileNames[editMessageIndex].value, editMessageIndex);
		   
		this.markAsReadUpdateList(this._LastSelected.id,true);
	}
	catch(e){page.set_ErrorMessage('Reselect the item to forward');}
	return false;
},
//////////////////////////////////////////////////////////////////////////////////////////////////////

//////////////////////////////////////////////////////////////////////////////////////////////////////
moveMessages:function(destinationFolderPath) 
{
	if( destinationFolderPath.toLowerCase()==page.get_FolderPath().toLowerCase() )
	{
		page.set_ErrorMessage('the destination and source folders can not be the same');
		return false;
	}
	
	var strMessages=this.getSelectedValues(true, true, FORMAT_ARRAY_ITEM_SEPERATOR);
	
	if((strMessages.length>0) && (destinationFolderPath.length>0))
	{
		clientPageMethods.moveMessages(page.get_FolderPath(), destinationFolderPath, strMessages);
		this.showLoading();
	}
	else if (strMessages.length==0)
		page.set_ErrorMessage('you have not selected any items to move');
	else if (destinationFolderPath.length==0)
		page.set_ErrorMessage('you have not selected a folder to move the items to');
		
	this._LastSelected=null;
	
	return false;
},
//////////////////////////////////////////////////////////////////////////////////////////////////////

//////////////////////////////////////////////////////////////////////////////////////////////////////
view:function()
{
	if(this.getLastSelected()!=null)
		this.doubleClick(this._LastSelected);
	else
		page.set_ErrorMessage('Select an item.');
},
//////////////////////////////////////////////////////////////////////////////////////////////////////
deleteItems:function() 
{
	var deletePermenant=false;
	switch(page.get_FolderPath())
	{
		case FOLDER_DRAFTS:
		case FOLDER_DELETED:
		case FOLDER_SPAM:
			deletePermenant=true;
			break;
	}
	var strMessages=this.getSelectedValues(true, true, FORMAT_ARRAY_ITEM_SEPERATOR);
	if(strMessages.length>0)
		clientPageMethods.deleteMessages( page.get_FolderPath(), strMessages, deletePermenant);
	else
		page.set_ErrorMessage('you have not selected any items to delete');
		
	this._LastSelected=null;    
},
//////////////////////////////////////////////////////////////////////////////////////////////////////

//////////////////////////////////////////////////////////////////////////////////////////////////////
doubleClick:function(objSelected)
{
	try{page.resetLastClicked();}catch(e){}
	
	this._LastSelected=objSelected;
	var fileName=objSelected.getAttribute(this._SelectedValueAttribute);
	
	if (document.selection && document.selection.empty) 
		document.selection.empty();
	
	if(fileName.length==0)
	{
		page.set_ErrorMessage('Unable to locate the requested e-mail.  The page will now refresh.');
		clientPageMethods.redirect("default.aspx",page.get_FolderPath());
		return;
	}
	
	if(page.get_FolderPath().toLowerCase()==FOLDER_DRAFTS.toLowerCase())
	{
		var fileNameIndex=page.addEditFileName(fileName);
		if(fileNameIndex>=0)
		{
			page.get_EditControl(fileNameIndex).loadDraft();
		}
		else
		{
			var i;
			for(i=0; i < g_objViewFileNames.length; i++)
			{
				if ( (g_objEditFileNames[i].value.length>0) &&  (g_objEditFileNames[i].value.toLowerCase()==fileName.toLowerCase()) )
					g_objSelectedTabIndex.value=i + page.EditFilesFirstTab;
			}
			page.setSelectedTabIndex(Number(g_objSelectedTabIndex.value));
		}
		return;
	}
	else
	{
		var updateViewIndex=-1;
	   try
	   {
			updateViewIndex=addViewFileName(fileName);
			if(updateViewIndex>=0)
			{
				document.getElementById(objSelected.getAttribute(ATT_LINE_DIV_ID)).style.fontWeight='';
				switch(updateViewIndex)
				{
					case (0):
					case (1):
					case (2):
						page.get_ViewControl(updateViewIndex).showMessage();
						break;
					default:
						page.set_ErrorMessage('Unable to open the selected message');
						page.setSelectedTabIndex(Number(g_objSelectedTabIndex.value));
						return;
				}
				this.selectCurrentOnly(objSelected);
			}
		}
		catch(e)
		{
			this.refresh(true);
			page.set_ErrorMessage('re-select the item to view');
			if(updateViewIndex>=0)
			{
				try{page.get_ViewControl(updateViewIndex).closeMessage();}
				catch(e){}
			}
			return;
		}
	}
	this.selectCurrentOnly(objSelected);
},
//////////////////////////////////////////////////////////////////////////////////////////////////////
refresh:function(hideWhileUpdating)
{
	this._LastSelected=null;
	foldersArray.UpdateFolderByPath(page.get_FolderPath(),true,false,false,15);

	if(hideWhileUpdating)
		this.hideMessageList();
	else
		this._LoadingImage.style.display='block';
},
//////////////////////////////////////////////////////////////////////////////////////////////////////
set_MessageListHTML:function(messageList,selectedValues)
{
	try
	{
		if(messageList==null)
			return;

		//results.MessageList
		//results.SiblingAnchorIds
		//results.SortAscending
		//results.SortColumn
		//results.PageNo
		//results.FolderPath
		//results.FolderName
		//results.FolderIndexHeader
		if(messageList.MessageListHTML!=null)
			this._ListDiv.innerHTML=messageList.MessageListHTML;
		else
			this._ListDiv.innerHTML="&nbsp;";
		g_objPageNo.value=messageList.PageNo;
		page.set_FolderPath(messageList.FolderPath);
		page.setTabText(1,messageList.FolderName);
		
		try
		{
			switch(page.get_FolderPath())
			{
				case FOLDER_SENT:
				case FOLDER_DRAFTS:
					this._MessageFromDiv.innerHTML="To";
					break;
				default:
					this._MessageFromDiv.innerHTML="From";
					break;
			}
		}
		catch(e){displayMessage(e)}
		
		try
		{
			//If this method was called, the message list is being updated
			//if the current box is the inbox, 
			//hide the "you have X new messages"
			if(page.get_FolderPath()==FOLDER_INBOX)
				page.set_newMessagesCount(-1);
		}
		catch(e){}
		
		var imgPath;
		if(true==messageList.SortAscending)
			imgPath=listObject._ImgUp;
		else
			imgPath=listObject._ImgDown;
			
		switch(messageList.SortColumn)
		{
			case 'To':
			case 'From':
				this._SortFromImg.src=imgPath;
				this._SortFromImg.style.display="inline-block";
				this._SortSubjectImg.style.display="none";
				this._SortDateImg.style.display="none";
				this._SortSizeImg.style.display="none";
				break;
			case 'Subject':
				this._SortSubjectImg.src=imgPath;
				this._SortSubjectImg.style.display="inline-block";
				this._SortFromImg.style.display="none";
				this._SortDateImg.style.display="none";
				this._SortSizeImg.style.display="none";
				break;
			case 'DateReceived':
				this._SortDateImg.src=imgPath;
				this._SortDateImg.style.display="inline-block";
				this._SortFromImg.style.display="none";
				this._SortSubjectImg.style.display="none";
				this._SortSizeImg.style.display="none";
				break;
			case 'Size':
				this._SortSizeImg.src=imgPath;
				this._SortSizeImg.style.display="inline-block";
				this._SortFromImg.style.display="none";
				this._SortSubjectImg.style.display="none";
				this._SortDateImg.style.display="none";
				break;
		}
		//clear the sibling controls before setting the checkbox
		//otherwise it will attempt to clear the controls it thinks are selected
		this.set_SibilingControls("");
		setCheckBoxValue(this._CheckBoxControlID,false);
		this.set_SibilingControls(messageList.SiblingAnchorIds);
		
		this.updateSelectedValuesPostCallback(selectedValues, messageList.SiblingAnchorIds);
		this._ToolbarObject.updateToolbar();
	}
	catch(e)
	{displayMessage(e);}
	
	this.unHideMessageList();
},
onMessageListError:function(error)
{
	listObject._LoadingImage.style.display='none';
	onError(error);
},
showLoading:function(){this._LoadingImage.style.display='block';},
hideMessageList:function()
{
	this._ListDiv.style.visibility='hidden';
	this._LoadingImage.style.display='block'; 
},
unHideMessageList:function()
{
	listObject._ListDiv.style.visibility='visible';
	listObject._LoadingImage.style.display='none'; 
},
messageListVisible:function()
{
	return (this._ListDiv.style.visibility=='visible' && this._LoadingImage.style.display=='none'); 
}
////////////////////////////////////////////////////////////////////////////////////////////////////

}
WebMail2.MessageList.registerClass("WebMail2.MessageList", WebMail2.ListBase);
