﻿//Copyright WebMail Wou3, Inc. 2007-2010
Type.registerNamespace('WebMail2');
///////////////////////////////////////////////////////////////////////////////////////////////////////////
//PageObject
///////////////////////////////////////////////////////////////////////////////////////////////////////////
WebMail2.Page=function
(
	FolderPathID,
	ViewFilesFirstTab,EditFilesFirstTab,
	NewMessageInterval,MaxClientIdleTime
)
{
	this.ViewFilesFirstTab=ViewFilesFirstTab;
	this.EditFilesFirstTab=EditFilesFirstTab;
	this._FolderPathID=FolderPathID;
	
	this._FolderPath=null;
	this.Status=null;
	//stores the last clicked interval.  If after a specified period of time 
	//(stored in a server variable: Max_ClientIdleTime), the user will be logged off
	//if the browser has been inactive for this amount of time.
	this._lastClicked=0;
	
	
	this._newMessageInterval = NewMessageInterval;
	this._maxClientIdleTime = MaxClientIdleTime;
	//Menu controls initialized by set_MenuObjects
	this._timerID=null;
	this._divNewMessage=null;
	this._linkGetNewMessages=null;
	
}
WebMail2.Page.prototype=
{
	//////////////////////////////////////////////////////////////////////////////////////////////////////    
	composeNewMessage:function()
	{
		var fileNameIndex=-1;
		
		fileNameIndex=this.addEditFileName(VALUE_NEWFILENAME_PLACEHOLDER);
		if(fileNameIndex<0)
		{
			return false;
		}
		clientPageMethods.redirect('default.aspx',page.get_FolderPath());
		return false;
	},
	//////////////////////////////////////////////////////////////////////////////////////////////////////    
	addEditFileName:function(fileName)
	{
		if((fileName==null) || (fileName==""))
			return -1;
			
		var i;
		var firstEmpty=-1;
		var firstConfirm=-1;

		for(i=0; i < g_objEditFileNames.length; i++)
		{
			if (g_objEditFileNames[i].value.length==0)
			{
				//gets the first empty
				firstEmpty=i;
				break;
			}
			else if ((g_objEditFileNames[i].value==VALUE_CONFIRM_SEND_PLACEHOLDER) && (firstConfirm<0))
			{
				//gets the first compose message object where the confirm email address tab is still open
				firstConfirm=i;
			}
			else if ((fileName!=VALUE_NEWFILENAME_PLACEHOLDER) && (g_objEditFileNames[i].value.length>0) &&  (g_objEditFileNames[i].value.toLowerCase()==fileName.toLowerCase()) )
			{
				//if the selected file is already being edited
				firstEmpty=i;
				break;
			}
		}
		//if firstConfirm>=0 an tab was found but it has not been closed after the user sent a message
		//that had potential contacts to add to the contact list.
		if(firstEmpty<0 && firstConfirm>=0)
			firstEmpty=firstConfirm;

		//if firstEmpty>=0 an empty tab was found to open the next message to compose
		if(firstEmpty>=0)
		{
			g_objEditFileNames[firstEmpty].value=fileName;
			g_objSelectedTabIndex.value=firstEmpty + page.EditFilesFirstTab;
			if(page.pageName().toLowerCase()=='default.aspx')
				page.setSelectedTabIndex(g_objSelectedTabIndex.value);
			return firstEmpty;
		}
		this.set_ErrorMessage('You have reached the maximum number of messages you can edit at one time.');    
		return -1;
	},
	//////////////////////////////////////////////////////////////////////////////////////////////////////    
	pageName:function()
	{
		var path=window.top.location.pathname.replace('\\','/');
		
		//if it ends with '/', the file name is not specified.  
		if(path.substr(path.length-1,1)=='/') 
			return 'default.aspx';
		
		var paths=path.split('/');
		var questionMark=paths[paths.length-1].indexOf('?');
		if(questionMark<0)
			return paths[paths.length-1];
		else
			return paths[paths.length-1].substr(0,questionMark);
	},

	
	addEditContact:function(contactID)
	{
		if((contactID==null) || (isNaN(contactID)) )
			return -1;

		var i;
		contactID=Number(contactID);
		for(i=0; i < g_objEditContacts.length; i++)
		{
			//if contactID < 0, it is a new contact
			if ( (g_objEditContacts[i].value.length>0) && (Number(contactID) >= 0) &&  (Number(g_objEditContacts[i].value)==contactID) )
			{
				this.setTabVisible(i + listObject._FirstEditContactTabIndex,true);
				this.setSelectedTabIndex(i + listObject._FirstEditContactTabIndex);
				return -1;
			}
		}
		
		for(i=0; i < g_objEditContacts.length; i++)
		{
			if (g_objEditContacts[i].value.length==0)
			{   
				return i;
			}
		}

		page.set_ErrorMessage('You have reached the maximum number of contacts you can edit at one time.');    
		return -1;
	},
	
	//////////////////////////////////////////////////////////////////////////////////////////////////////////////
	//Status control is has the set_ErrorMessage and set_StatusMessage methods.
	//this method sets a reference to access the page's methods.
	//////////////////////////////////////////////////////////////////////////////////////////////////////////////
	set_StatusControl:function(value)
	{
		this.Status=value;
		if(this.Status!=null)
			this.Status.ShowInitialMessages();
	},
	//////////////////////////////////////////////////////////////////////////////////////////////////////////////
	//results is a dictionary object returned by the PageMethods and displays any status messages if any are returned.
	//////////////////////////////////////////////////////////////////////////////////////////////////////////////
	set_Messages:function(results)
	{
		if(results==null)
			return;
					
		try{this.set_StatusMessage(results.StatusMessage);}
		catch(e){}
		try{this.set_ErrorMessage(results.ErrorMessage);}
		catch(e){}
	},
	//////////////////////////////////////////////////////////////////////////////////////////////////////    
	set_StatusMessage:function(message)
	{
		if(this.Status!=null)
		{this.Status.set_StatusMessage(message);}
	},
	//////////////////////////////////////////////////////////////////////////////////////////////////////    
	set_ErrorMessage:function(message)
	{
		if(this.Status!=null)
		{this.Status.set_ErrorMessage(message);}
	},
	clearStatusMessages:function()
	{
		if(this.Status!=null)
			this.Status.close();
	},
	
	///////////////////////////////////////////////////////////////////////////////////////////////////////////
	//initialize the menu control objects
	///////////////////////////////////////////////////////////////////////////////////////////////////////////
	set_MenuObjects:function(DivNewMessageID, LinkGetNewMessagesID)
	{
		this._divNewMessage=document.getElementById(DivNewMessageID);
		this._linkGetNewMessages=document.getElementById(LinkGetNewMessagesID);
		if((this._divNewMessage!=null) && (this._linkGetNewMessages!=null))
		{
			this.resetNewMessageTimerID();
		}
	},
	///////////////////////////////////////////////////////////////////////////////////////////////////////////

	///////////////////////////////////////////////////////////////////////////////////////////////////////////
	//sets the new message count
	///////////////////////////////////////////////////////////////////////////////////////////////////////////
	set_newMessagesCount:function(messageCount)
	{		
		if(isNaN(messageCount))
			messageCount=0;
		
		if(messageCount>0)
		{
			if(messageCount > 1000)
				this._linkGetNewMessages.innerHTML = 'You have new messages';
			else if(messageCount > 1)
				this._linkGetNewMessages.innerHTML = 'You have ' + messageCount + ' new messages';
			else
				this._linkGetNewMessages.innerHTML = 'You have 1 new message';
			this._divNewMessage.style.display='inline';
		}
		else 
		{
			this._divNewMessage.style.display="none";
		}
		
		if(typeof calendarNavigator!='undefined')
			calendarNavigator.setCalendarLocation();
	},
	///////////////////////////////////////////////////////////////////////////////////////////////////////////
	
	///////////////////////////////////////////////////////////////////////////////////////////////////////////
	//this method is called when the user clicks on the new message link
	///////////////////////////////////////////////////////////////////////////////////////////////////////////
	onClientClickForNewMessasges:function()
	{
		page.set_newMessagesCount(-1);
		//if not on the default.aspx page, the serer side code will redirect to default.aspx.
		if(page.pageName().toLowerCase() != "default.aspx")
		{
			return clientPageMethods.redirect("default.aspx",'\\Inbox');
			return false;
		}

		var folderInbox = foldersArray.get_FolderByPath('\\Inbox');
		if(folderInbox!=null)
			folderInbox.SelectFolder(true,false,10);
		else
			//shouldn't happen.
			return clientPageMethods.redirect("default.aspx",'\\Inbox');

		this.resetLastClicked();
		this.resetNewMessageTimerID();
		
		return false;
	},
	///////////////////////////////////////////////////////////////////////////////////////////////////////////
	
	///////////////////////////////////////////////////////////////////////////////////////////////////////////
	onHasNewMessagesInboxComplete:function(results)
	{
		//add three minutes to the intervals
		page.addToLastClicked();
		//check the next folder for updates.
		foldersArray.autoRefresh();

		if(page.pageName().toLowerCase() == "default.aspx")
		{
			try
			{
				if(cE0.get_FileName().length>0)
					cE0.saveMessage(true,true);
			}
			catch(e){}
			try
			{
				if(cE1.get_FileName().length>0)
					cE1.saveMessage(true,true);
			}
			catch(e){}
			try
			{
				if(cE2.get_FileName().length>0)
					cE2.saveMessage(true,true);
			}
			catch(e){}
		}
		
		//if the client has been sitting idol for a signicant amount of time, stop checking
		if(page.getLastClicked() >= this._maxClientIdleTime)	//Default == 5 HOURS
		{
			page.clearNewMessageTimerID();
			window.top.location = '/logoff.aspx';
			return;
		}
		else
			page.resetNewMessageTimerID();

		if((!results==null) || isNaN(results))
			page.set_newMessagesCount(0);
		else
			page.set_newMessagesCount(Number(results));
			
	},
	//////////////////////////////////////////////////////////////////////////////////////////////////////    
	onHasNewMessagesInboxError:function(error)
	{
		page.onHasNewMessagesInboxComplete(null);
	},
	//////////////////////////////////////////////////////////////////////////////////////////////////////    

	//////////////////////////////////////////////////////////////////////////////////////////////////////    
	//resets the timer, 
	resetNewMessageTimerID:function()
	{
		page.clearNewMessageTimerID();
		this._timerID=setInterval("page.hasNewMessagesInbox()", this._newMessageInterval);
	},
	//////////////////////////////////////////////////////////////////////////////////////////////////////    


	//////////////////////////////////////////////////////////////////////////////////////////////////////    
	//HasNewMessages()
	/////////////////////////////////////////////////////////////////////////////////////////////////////////        
	hasNewMessagesInbox:function()
	{
		PageMethods.HasNewMessages(this.onHasNewMessagesInboxComplete,this.onHasNewMessagesInboxError);
	},
	/////////////////////////////////////////////////////////////////////////////////////////////////////////        
	
	//////////////////////////////////////////////////////////////////////////////////////////////////////    
	//the new message timer is set to periodically check for new messages.
	//this method will clear existing timers
	clearNewMessageTimerID:function()
	{
		var tempTimerID=this._timerID;
		this._timerID=null;
		
		if(tempTimerID !=null)
		{
			try{clearInterval(tempTimerID);}
			catch(e){}
		}
	},
	///////////////////////////////////////////////////////////////////////////////////////////////////////////

	///////////////////////////////////////////////////////////////////////////////////////////////////////////
	//clearTimers
	//Clears the timers whenever a redirect is expected.
	///////////////////////////////////////////////////////////////////////////////////////////////////////////
	clearTimers:function()
	{
		if(this.Status!=null)
		{this.Status.clearTimers();}
		
	},
	//////////////////////////////////////////////////////////////////////////////////////////////////////
	//resets the _lastClicked interval.  This method is called everytime a user clicks on a java script object
	resetLastClicked:function(){this._lastClicked=0;},
	//////////////////////////////////////////////////////////////////////////////////////////////////////
	//adds value to the number of minutes since the user last clicked on a javascript object
	//increments everytime a timer object checks for new messages.
	addToLastClicked:function(){this._lastClicked+=this._newMessageInterval;},
	//////////////////////////////////////////////////////////////////////////////////////////////////////
	//returns the number of minutes since the user last clicked on a javascript object
	getLastClicked:function(){return this._lastClicked;},
	//////////////////////////////////////////////////////////////////////////////////////////////////////    
	
	//////////////////////////////////////////////////////////////////////////////////////////////////////
	//persisted values
	//////////////////////////////////////////////////////////////////////////////////////////////////////
	get_FolderPath:function()
	{
		if(this._FolderPath==null)
		{
			this._FolderPath=document.getElementById(this._FolderPathID);
			if(this._FolderPath==null)
				return "";
		}
		return this._FolderPath.value;
	},
	set_FolderPath:function(value)
	{
		if(this._FolderPath==null)
		{
			this._FolderPath=document.getElementById(this._FolderPathID);
			if(this._FolderPath==null)
				return;
		}
		this._FolderPath.value=value;
	},
	//////////////////////////////////////////////////////////////////////////////////////////////////////
	
	//////////////////////////////////////////////////////////////////////////////////////////////////////
	//updateQuota is in the base object to eliminate the need to check for the current page when 
	//updadiing the quota object.  
	//Withtout this method, there would always be a need to check if pageName=='display.aspx'
	//////////////////////////////////////////////////////////////////////////////////////////////////////
	updateQuota:function(quotaObject){}
	//////////////////////////////////////////////////////////////////////////////////////////////////////
	

}   
WebMail2.Page.registerClass("WebMail2.Page");
///////////////////////////////////////////////////////////////////////////////////////////////////////////

