﻿//Copyright WebMail Wou3, Inc. 2007-2010
Type.registerNamespace('WebMail2');
WebMail2.DefaultPage=function
(
	FolderPathID, ViewFilesFirstTab, EditFilesFirstTab,
	MaxTabTextLength,PageSize,ContactFormat,ContactEMailFormat,
	NewMessageInterval,MaxClientIdleTime
)
{ 
	////////////////////////////////////////////////////////////////////////////////////////////////////////
	//initializes the object
	var baseParams=new Array
		(
			FolderPathID,ViewFilesFirstTab,EditFilesFirstTab,
			MaxTabTextLength,
			NewMessageInterval,MaxClientIdleTime
		);
	WebMail2.DefaultPage.initializeBase(this,baseParams);
	////////////////////////////////////////////////////////////////////////////////////////////////////////
	this._SpellCheckID=null;
	this._SpellCheckXMLID=null;
	this.Nav=new WebMail2.Navigate(PageSize);
	this._ContactEMailFormat=ContactEMailFormat;
	this._ContactFormat=ContactFormat;
	
	//creates the compose message control array
	this._compose = new Array();
	//creates the view message control array
	this._view = new Array();
	
	//initialize the arrays
	for(i=0; i < g_objEditFileNames.length; i++)
		Array.add(this._compose,null);
	for(i=0; i < g_objViewFileNames.length; i++)
		Array.add(this._view,null);
		
	//quota controls
	this._quotaUsed=null;
	this._quotaBarID=null;
	this._quotaMessage=null;
}
WebMail2.DefaultPage.prototype=
{
	///////////////////////////////////////////////////////////////////////////////////////////////////////////
	//Spell Check Methods
	///////////////////////////////////////////////////////////////////////////////////////////////////////////
	set_SpellCheckID:function(value){this._SpellCheckID=value;},
	get_SpellCheckID:function(){return this._SpellCheckID;},
	
	set_SpellCheckXMLID:function(value){this._SpellCheckXMLID=value;},
	get_SpellCheckXMLID:function(){return this._SpellCheckXMLID;},

	composeNewMessage:function()
	{
		try
		{
			var fileNameIndex=-1;
			fileNameIndex=this.addEditFileName('*');
			if(fileNameIndex<0)
			{
				return false;
			}
			//otherwise choose the ctlEdit and populate
			if(fileNameIndex==0)
			{
				if(cE0!=null)
				{
					cE0.composeNewMessage();
				}
				else
				{
					g_objEditFileNames[fileNameIndex].value='';
				}
			}
			else if(fileNameIndex==1)
			{
				if(cE1!=null)
				{
					cE1.composeNewMessage();
				}
				else
				{
					g_objEditFileNames[fileNameIndex].value='';
				}
			}
			else if(fileNameIndex==2)
			{
				if(cE2!=null)
				{
					cE2.composeNewMessage();
				}
				else
				{
					g_objEditFileNames[fileNameIndex].value='';
				}
			}
		}
		catch(e)
		{displayMessage(e);}
		
		try
		{
			//verify the control was opened.
			if(g_objEditFileNames[fileNameIndex].value=='*' && !page.getTabVisible(page.EditFilesFirstTab+fileNameIndex))
			{
				g_objEditFileNames[fileNameIndex].value='';
			}
		}
		catch(e){}
		
		return false;
	},
	///////////////////////////////////////////////////////////////////////////////////////////////////////////
	
	//////////////////////////////////////////////////////////////////////////////////////////////////////    
	addRecipientToContacts:function(emailAddress,firstNameObject,lastNameObject,companyNameObject,addAddressDiv,editFileIndex)
	{
		var firstName;
		var lastName;
		var companyName;
		var emailDisplayAs=this._ContactEMailFormat;
		var contactDisplayAs=this._ContactFormat;
		
		if((firstNameObject==null) || (firstNameObject.value.replace(' ','').length==0))
			firstName=null;
		else 
			firstName=firstNameObject.value;
		if((lastNameObject==null) || (lastNameObject.value.replace(' ','').length==0))
			lastName=null;
		else 
			lastName=lastNameObject.value;
		if((companyNameObject==null) || (companyNameObject.value.replace(' ','').length==0))
			companyName=null;
		else 
			companyName=companyNameObject.value;
		
		/*
		public static Dictionary<string, object> SaveContact
		(
			int ContactIndex, int ContactID, 
			string ContactFirstName, string ContactMiddleName, string ContactLastName, 
			string Company, string DisplayName, 
			string Address1, string Address2, string City, string State, string Zip, string Country, 
			string BusinessPhone, string HomePhone, string CellPhone, string OtherPhone, 
			bool IsGlobal, string BirthDate, string OtherDateName, string OtherDate, 
			string EMail0, string DisplayName0, 
			string EMail1, string DisplayName1, 
			string EMail2, string DisplayName2
		)
		*/
		
		PageMethods.SaveContact
		( 
			 -1, 0, 
			 firstName,null,lastName, 
			 companyName,formatDisplayAs(contactDisplayAs,firstName,lastName,companyName), 
			 null,null,null,null,null,null,
			 null,null,null,null,
			 false,null,null,null,
			 emailAddress,formatDisplayAs(emailDisplayAs,firstName,lastName,companyName),
			 null,null,
			 null,null,
			 this.onSaveContactComplete, this.onSaveContactError
		 );
		 
		 set_ObjectIsVisible(addAddressDiv, false);
		 
		 var ctlEdit=page.get_EditControl(editFileIndex);
		 if(ctlEdit!=null)
			ctlEdit.RecipientAdded();
	},
	onSaveContactComplete:function(results) 
	{
		if(results==null)
		{
			page.set_ErrorMessage('Unable to add one or more contacts');
			return;
		}
		var contact=results.Contact;
		
		try{cCntcts._AddressesUpdated = false;}
		catch(e){}
		
		if((contact==null) || (contact.ContactID==null) && (contact.ErrorMessage.length==0))
		{
			page.set_ErrorMessage('Unable to add one or more contacts');
		}
		page.set_StatusMessage(contact.StatusMessage);
		page.set_ErrorMessage(contact.ErrorMessage);
	},
	onSaveContactError:function(error)
	{
		page.set_ErrorMessage('Unable to add one or more contacts');
	},
	//////////////////////////////////////////////////////////////////////////////////////////////////////
	
	addViewControl:function(index,viewObject)
	{
		this._view[Number(index)]=viewObject;
		viewObject._FirstTabIndex=this.ViewFilesFirstTab;
	},
	addEditControl:function(index,editObject){this._compose[Number(index)]=editObject;},
	
	get_ViewControl:function(index){return this._view[Number(index)];},
	get_EditControl:function(index){return this._compose[Number(index)];},
	
	set_QuotaObjects:function(quotaUsedID,quotaBarID,quotaMessageID)
	{
		this._quotaUsed=document.getElementById(quotaUsedID);
		this._quotaBar=document.getElementById(quotaBarID);
		this._quotaMessage=document.getElementById(quotaMessageID);
	},
	updateQuota:function(quotaObject)
	{
		if( (quotaObject!=null) && (this._quotaUsed!=null) && (this._quotaUsed!=null) && (this._quotaUsed!=null))
		{
			this._quotaUsed.style.width=quotaObject.QuotaUsedWidth;
			this._quotaUsed.style.visibility=quotaObject.QuotaUsedVisibility;
			this._quotaBar.style.display=quotaObject.QuotaBarDisplay;
			this._quotaMessage.innerHTML=quotaObject.QuotaMessage;
		}
	}
}
WebMail2.DefaultPage.registerClass("WebMail2.DefaultPage", WebMail2.TabbedPage);
