﻿//Copyright WebMail Wou3, Inc. 2007-2010
Type.registerNamespace('WebMail2');
///////////////////////////////////////////////////////////////////////////////////////////////////////////
WebMail2.WebMethods=function(){}
WebMail2.WebMethods.prototype=
{
	///////////////////////////////////////////////////////////////////////////////////////////////////////////
	//callback methods 
	///////////////////////////////////////////////////////////////////////////////////////////////////////////
	onReturnBuildReturnFolderObjects:function(results) 
	{
		try{foldersArray.set_FolderIndexHeaders(results.FolderIndexHeaders);}catch(e){}
		var selectedValues=null;
		
		//gets the current list of selected messages and attempts to highlight the emails that are still in the
		//visible message list.  Removes all others from the selected list.
		try{selectedValues=listObject.getSelectedValues(false,false,FORMAT_ARRAY_ITEM_SEPERATOR);}catch(e){}
		try{listObject.set_MessageListHTML(results.MessageList, selectedValues);}
		catch(e){}
		
		
		try{page.set_Messages(results);}catch(e){}
		try{page.Nav.UpdateNavigateLabel();}catch(e){}
		try{page.updateQuota(results.QuotaInfo);}catch(e){}
	},
	///////////////////////////////////////////////////////////////////////////////////////////////////////////
	
	removeAttachmentFromLink:function(folderPath,fileName,ordinal,fileNameIndex,divUniqueID)
	{
		try{showLoading('');}
		catch(e){}
		try{document.getElementById(divUniqueID).style.display="none";}
		catch(e){}
		var ctl=null;
		if(folderPath.toLowerCase()==FOLDER_DRAFTS.toLowerCase())
			ctl=page.get_EditControl(fileNameIndex);
		else
			ctl=page.get_ViewControl(fileNameIndex);
		//Dictionary<string, object> RemoveAttachment(string FolderPath, string FileName, int AttachmentOrdinal, int FileNameIndex, string CurrentFolderPath, int? PageNo)
		PageMethods.RemoveAttachment(folderPath,fileName,ordinal,fileNameIndex,page.get_FolderPath(),g_objPageNo.value,ctl.onremoveAttachmentComplete,ctl.onremoveAttachmentError);
	},
	//////////////////////////////////////////////////////////////////////////////////////////////////////    
	writeErrorToEventLog:function(errorMessage)
	{PageMethods.WriteErrorToEventLog(errorMessage, doNothing,doNothing);
	},
	/////////////////////////////////////////////////////////////////////////////////////////////////////////

	redirect:function(url, folderPath)
	{
		showLoading('');

		if(foldersArray!=null)
			foldersArray.cancelPending();

		if(url.indexOf('/',0) != 0)
			url='/' + url;

		if(page.pageName().toLowerCase()=='default.aspx')
		{
			
			if(page.getTabVisible(page.EditFilesFirstTab))
				cE0.saveMessage(true,true);
			if(page.getTabVisible(page.EditFilesFirstTab+1))
				cE1.saveMessage(true,true);
			if(page.getTabVisible(page.EditFilesFirstTab+2))
				cE2.saveMessage(true,true);
		}
		else 
		{
			if(folderPath.length>0)
			{
				if(url.indexOf('?',0) > 0)
					url += '&hdnFolderPath=' + escape(folderPath);
				else
					url += '?hdnFolderPath=' + escape(folderPath);
			}
			if(g_objEditFileNames[0].value==VALUE_NEWFILENAME_PLACEHOLDER)
			{
				if(url.indexOf('?',0) > 0)
					url += '&hdnEditFileName0=' + g_objEditFileNames[0].value;
				else
					url += '?hdnEditFileName0=' + g_objEditFileNames[0].value;
				
			}
		}
		
		this.redirectWebMethod(url);

		return false;
	},
	//////////////////////////////////////////////////////////////////////////////////////////////////////    


	//////////////////////////////////////////////////////////////////////////////////////////////////////    
	//redirectWebMethod and callbacks
	//////////////////////////////////////////////////////////////////////////////////////////////////////    
	redirectWebMethod:function(url)
	{
		if(typeof PageMethods!='undefined')
			PageMethods.RedirectWait(url,this.onRedirectWaitComplete,this.onRedirectWaitError);
		else
			window.top.location=url;
	},
	
	onRedirectWaitComplete:function(results)
	{
		try {window.top.location=results;}
		catch(e) {window.top.location='/default.aspx';}
	},
	
	onRedirectWaitError:function()
	{window.top.location='/Default.aspx';},
	//////////////////////////////////////////////////////////////////////////////////////////////////////    

	/////////////////////////////////////////////////////////////////////////////////////////////////////////        
	/////////////////////////////////////////////////////////////////////////////////////////////////////////        
	//Generate a reply to message
	/////////////////////////////////////////////////////////////////////////////////////////////////////////        
	//WebMethod definition
	//CreateReplyToMessage(string SourceFilePath, int EditMessageTabIndex, bool ForcePlainText)
	//
	//Any page calling createReplyToMessage must implement it's own version of onMessageFromFileComplete
	/////////////////////////////////////////////////////////////////////////////////////////////////////////        
	createReplyToMessage:function(sourceFilePath, editMessageIndex)
	{   
		var ctlEdit=page.get_EditControl(editMessageIndex);
		if(ctlEdit==null)
		{
			page.set_ErrorMessage('There was an unexpected error creating the reply.  Please try again.  Index=' + editMessageIndex); 
			return false;
		}
		//public static Dictionary<string, object> CreateReplyToMessage(string SourceFilePath, int EditMessageIndex, bool ForcePlainText, string CurrentFolderPath, int? PageNo)
		if(page.get_FolderPath().toLowerCase()==FOLDER_DRAFTS.toLowerCase())
			PageMethods.CreateReplyToMessage(sourceFilePath, editMessageIndex, false, page.get_FolderPath(), g_objPageNo.value, ctlEdit.loadDraftMessageCallback, onError);
		else
			PageMethods.CreateReplyToMessage(sourceFilePath, editMessageIndex, false, null, null, ctlEdit.loadDraftMessageCallback, onError);

		ctlEdit._ToolBarObject.disable();
		showLoading('');
		return false;
	},
	/////////////////////////////////////////////////////////////////////////////////////////////////////////        
	//Generate a reply to all message
	/////////////////////////////////////////////////////////////////////////////////////////////////////////        
	createReplyToAllMessage:function(sourceFilePath, editMessageIndex)
	{   
		var ctlEdit=page.get_EditControl(editMessageIndex);
		if(ctlEdit==null)
		{
			page.set_ErrorMessage('There was an unexpected error creating the reply.  Please try again.  Index=' + editMessageIndex); 
			return false;
		}
		//public static Dictionary<string, object> CreateReplyToAllMessage(string SourceFilePath, int EditMessageIndex, bool ForcePlainText, string CurrentFolderPath, int? PageNo)
		if(page.get_FolderPath().toLowerCase()==FOLDER_DRAFTS.toLowerCase())
			PageMethods.CreateReplyToAllMessage(sourceFilePath, editMessageIndex, false, page.get_FolderPath(), g_objPageNo.value, ctlEdit.loadDraftMessageCallback, onError);
		else
			PageMethods.CreateReplyToAllMessage(sourceFilePath, editMessageIndex, false, null, null, ctlEdit.loadDraftMessageCallback, onError);
			
		ctlEdit._ToolBarObject.disable();
		showLoading('');
		return false;
	},
	/////////////////////////////////////////////////////////////////////////////////////////////////////////        
	/////////////////////////////////////////////////////////////////////////////////////////////////////////        
	//Generate a forward to message
	createForwardMessage:function(sourceFilePath, editMessageIndex)
	{   
		var ctlEdit=page.get_EditControl(editMessageIndex);
		if(ctlEdit==null)
		{
			page.set_ErrorMessage('There was an unexpected error creating the reply.  Please try again.  Index=' + editMessageIndex); 
			return false;
		}
				
		//WebMethod definition
		//public static Dictionary<string, object> CreateForwardMessage(string SourceFilePath, int EditMessageIndex, bool ForcePlainText, string CurrentFolderPath, int? PageNo)
		if(page.get_FolderPath().toLowerCase()==FOLDER_DRAFTS.toLowerCase())
			PageMethods.CreateForwardMessage(sourceFilePath, editMessageIndex, false, page.get_FolderPath(), g_objPageNo.value, ctlEdit.loadDraftMessageCallback, onError);
		else
			PageMethods.CreateForwardMessage(sourceFilePath, editMessageIndex, false, null, null, ctlEdit.loadDraftMessageCallback, onError);
			
		ctlEdit._ToolBarObject.disable();
		showLoading('');
		return false;
	},
	
	/////////////////////////////////////////////////////////////////////////////////////////////////////////        
	/////////////////////////////////////////////////////////////////////////////////////////////////////////        
	//WebMethod definition
	//public static Dictionary<string, object> DeleteMessage(string FilePath, bool Permenant, string CurrentFolderPath, int? PageNo)
	/////////////////////////////////////////////////////////////////////////////////////////////////////////        
	deleteMessage:function(filePath,permenant)
	{   
		if((filePath==VALUE_NEWFILENAME_PLACEHOLDER) || (filePath.length==0))
			return false;
		PageMethods.DeleteMessage(filePath, permenant, page.get_FolderPath(), g_objPageNo.value, this.onReturnBuildReturnFolderObjects, onError);
		return false;
	},
	/////////////////////////////////////////////////////////////////////////////////////////////////////////        
	//deleteMessages deletes multiple messages.  the parameter fileNames is an array containing all
	//files to delete.  The folder path should not be included in the fileNames.
	//public static Dictionary<string, object> DeleteMessages(string FolderPath, string[] FileNames, bool Permenant, string CurrentFolderPath, int? PageNo)
	/////////////////////////////////////////////////////////////////////////////////////////////////////////        
	deleteMessages:function(folderPath,fileNames,permenant)
	{   
		PageMethods.DeleteMessages(folderPath, fileNames.split(FORMAT_ARRAY_ITEM_SEPERATOR), permenant, page.get_FolderPath(), g_objPageNo.value, this.onReturnBuildReturnFolderObjects, onError);
		return false;
	},
	
	/////////////////////////////////////////////////////////////////////////////////////////////////////////        
	//public static Dictionary<string, object> MoveMessage(string sourceFilePath, string destinationFolderPath, string CurrentFolderPath, int? PageNo)
	/////////////////////////////////////////////////////////////////////////////////////////////////////////        
	moveMessage:function(sourceFilePath,destinationFolderPath)
	{   
		PageMethods.MoveMessage(sourceFilePath, destinationFolderPath, page.get_FolderPath(), g_objPageNo.value, this.onReturnBuildReturnFolderObjects, onError);
		return false;
	},
	/////////////////////////////////////////////////////////////////////////////////////////////////////////        
	//public static Dictionary<string, object> MoveMessages(string SourceFolderPath, string DestinationFolderPath, string[] FileNames)
	/////////////////////////////////////////////////////////////////////////////////////////////////////////        
	moveMessages:function(folderPath,destinationFolderPath, fileNames)
	{   
		PageMethods.MoveMessages(folderPath, destinationFolderPath, fileNames.split(FORMAT_ARRAY_ITEM_SEPERATOR), page.get_FolderPath(), g_objPageNo.value, this.onReturnBuildReturnFolderObjects, onError);
		return false;
	},
	/////////////////////////////////////////////////////////////////////////////////////////////////////////        
	/////////////////////////////////////////////////////////////////////////////////////////////////////////        
	//DeleteDraft(string FileName, bool AutoOnly, string CurrentFolderPath, int? PageNo)
	//when autoOnly==true, only  the .auto file is deleted.
	/////////////////////////////////////////////////////////////////////////////////////////////////////////        
	deleteDraft:function(fileName, autoOnly)
	{   
		if((fileName==VALUE_NEWFILENAME_PLACEHOLDER) || (fileName.length==0))
			return false;
		
		PageMethods.DeleteDraft(fileName, autoOnly, page.get_FolderPath(), g_objPageNo.value, this.onReturnBuildReturnFolderObjects,onError);
		return false;
	},
	
	//////////////////////////////////////////////////////////////////////////////////////////////////////    
	//////////////////////////////////////////////////////////////////////////////////////////////////////    
	//this method checks the server to see if the client and server are out of synch.
	//if the callback method's onCheckServerForUpdatesComplete result parameter==true, the page will need 
	//to be refreshed.
	checkServerForUpdates:function()
	{
		//public static bool CheckServerForUpdates(string ClientTicksValue)
		PageMethods.CheckServerForUpdates(APPLICATION_JS_FILE_TICKS, this.onCheckServerForUpdatesComplete, this.onCheckServerForUpdatesError);
	},
	onCheckServerForUpdatesComplete:function(result) 
	{
		if(result==true)
		{
			page.set_ErrorMessage('the server and client appear to be out of synch.  The page must now reload');
			window.top.location='/default.aspx';
		}
	},

	onCheckServerForUpdatesError:function(error) 
	{
		page.set_ErrorMessage('the server and client appear to be out of synch.  The page must now reload');
		window.top.location='/default.aspx';
	},
	//////////////////////////////////////////////////////////////////////////////////////////////////////

	//////////////////////////////////////////////////////////////////////////////////////////////////////
	//If a email has a vcf file attached, this method will create one or more contact records.
	addContactAttachment:function(Path,FileName,AttachmentPath,Ordinal)
	{
		//public static Dictionary<string, object> AddContactAttachment(string Path, string FileName, string AttachmentPath, string Ordinal)
		PageMethods.AddContactAttachment(Path,FileName,AttachmentPath,Ordinal,this.onAddContactComplete,this.onAddContactError);
	},
	onAddContactComplete:function(result) 
	{
		try{cCntcts._AddressesUpdated=false;}
		catch(e){}
		page.set_Messages(result);
	},
	onAddContactError:function(error) 
	{page.set_ErrorMessage('Unable to add the contact.');}
}
WebMail2.WebMethods.registerClass("WebMail2.WebMethods");
