﻿//Copyright WebMail Wou3, Inc. 2007-2010
Type.registerNamespace('WebMail2');
WebMail2.ListEMailAddresses=
function
(
	ContactsBehaviorID,
	PopupDivID, 
	To, Cc, Bcc, 
	ListDivID,
	SiblingValueControlIDs,
	SelectedValuesControlID,
	UnSelectedColorBack,
	SelectedColorBack,
	SelectedValueAttribute
) 
{ 
	//the last parameter is the toolbar object.  none is associated with this control.
	//the parameter before it is the ListTabIndex.  This control is not contained in tab
	//the parameter before it is the CheckBoxControlID.  This control does not have a check box.
	var baseParams=new Array
		(
			ListDivID,
			SiblingValueControlIDs,
			SelectedValuesControlID,
			UnSelectedColorBack,
			SelectedColorBack,
			SelectedValueAttribute,
			null,
			-1,
			null
		);
	WebMail2.ListEMailAddresses.initializeBase(this,baseParams);
	
	//when the address list is up to date, this._AddressesUpdated will be set to true.
	this._AddressesUpdated=false;
	this._PopupDivID=PopupDivID;
	this._ContactsBehaviorID=ContactsBehaviorID;
	this._To=To;
	this._Cc=Cc;
	this._Bcc=Bcc;
	//DefaultDestination sets the default textbox control on the contacts list to copy addresses to if none other has
	//been selected.  The default is the To recipients
	this._DefaultDestination=To;
	this._Compose=null;
	this._addressSperator=', ';
	//this variable is set to false until the address list is updated by the server.
	//It will also be set to false if the address list is cleared
}

WebMail2.ListEMailAddresses.prototype=
{
get_To:function() {return document.getElementById(this._To).value;},
set_To:function(value) {document.getElementById(this._To).value=value;},

get_Cc:function() {return document.getElementById(this._Cc).value;},
set_Cc:function(value) {document.getElementById(this._Cc).value=value;},

get_Bcc:function() {return document.getElementById(this._Bcc).value;},
set_Bcc:function(value) {document.getElementById(this._Bcc).value=value;},

get_Compose:function() {return this._Compose;},

show:function(SourceObject, ComposeObject)
{
	if(false==this._AddressesUpdated)
	{
		PageMethods.GetEMailAddressList(this.onGetEMailListSuccess,this.onGetEMailListError);
	}
	this._Compose=ComposeObject;
	this.getSelectedValues(true, false, this._addressSperator);
	this.set_To(this.get_Compose().get_To());
	this.set_Cc(this.get_Compose().get_Cc());
	this.set_Bcc(this.get_Compose().get_Bcc());
	//sets the default control to when a Contact is double clicked on
	if(SourceObject.id.replace(SourceObject.id.slice(0,-3),"").toLowerCase()=="bcc")
		this._DefaultDestination=this._Bcc;
	else if(SourceObject.id.replace(SourceObject.id.slice(0,-2),"").toLowerCase()=="cc")
		this._DefaultDestination=this._Cc;
	else
		this._DefaultDestination=this._To;
	
	//when loaded the popup will display for just a brief second with the popup's html loads.
	//by default, the display mode of the popup is set to none.  This resets the display mode 
	//so it is visible from that point on.
	document.getElementById(this._PopupDivID).style.display='block';
	$find(this._ContactsBehaviorID).show(); 
		
	//return false because this method will be used by OnClientClick which must return a false 
	//in order to keep the postback from happneing.
	return false;
},
onGetEMailListSuccess:function(result)
{
	if(cCntcts._ListDiv!=null)
	{
		cCntcts._ListDiv.innerHTML=result.EmailList;
		cCntcts._AddressesUpdated = true;
	}
	else
	{
		page.set_ErrorMessage("The contacts list could not be updated");
		cCntcts._AddressesUpdated = false;
	}
},
onGetEMailListError:function(errors)
{
	page.set_ErrorMessage("The contacts list could not be updated");
	onError(errors);
},

closeAndCopyAddresses:function()
{
	this.get_Compose().set_To(this.get_To());
	
	if((this.get_Cc().length>0) || (this.get_Bcc().length>0))
	{
		this.get_Compose().set_showCC(true);
		this.get_Compose().set_Cc(this.get_Cc());
		this.get_Compose().set_Bcc(this.get_Bcc());
	}
	else
	{
		this.get_Compose().set_Cc('');
		this.get_Compose().set_Bcc('');
	}
	//return false because this method will be used by OnClientClick which must return a false 
	//in order to keep the postback from happneing.
	return this.close();
},


close:function() 
{
	//set the default back to To
	this._DefaultDestination=this._To;
	$find(this._ContactsBehaviorID).hide(); 
	
	return false;
},

//updates a textbox control with the selected list values.  SourceObject is likely a button
addSelectedAddresses:function(SourceObject)
{
	var valuesToAdd=this.getSelectedValues(true, false, FORMAT_ARRAY_ITEM_SEPERATOR);
	valuesToAdd=valuesToAdd.split(FORMAT_ARRAY_ITEM_SEPERATOR);
	valuesToAdd=valuesToAdd.join(this._addressSperator);
	if(valuesToAdd.length==0)
		return false;

	//determine which control was clicked
	var objTbx;
	if(SourceObject.id.replace(SourceObject.id.slice(0,-3),"").toLowerCase()=="bcc")
		objTbx=document.getElementById(this._Bcc);
	else if(SourceObject.id.replace(SourceObject.id.slice(0,-2),"").toLowerCase()=="cc")
		objTbx=document.getElementById(this._Cc);
	else
		objTbx=document.getElementById(this._To);

   if(objTbx.value.length==0)
		objTbx.value=valuesToAdd;
	else
		objTbx.value=objTbx.value.concat(this._addressSperator).concat(valuesToAdd);
	
	return false;
},

//This Anchor or linkbutton that was doubleClicked
addSelectedAddress:function(objSelected)
{
	var objTbx=document.getElementById(this._DefaultDestination);
	if(objTbx.value.length==0)
		objTbx.value=this.selectCurrentOnly(objSelected);
	else
		objTbx.value=objTbx.value.concat(this._addressSperator).concat(this.selectCurrentOnly(objSelected));
}

}
WebMail2.ListEMailAddresses.registerClass("WebMail2.ListEMailAddresses",WebMail2.ListBase);
