//Copyright (c) 2000 Microsoft Corporation.  All rights reserved.
//<script>
var g_nFontHeight = 13;

function EditResolvedName(szName, szEmail, szAddressType, nIndex, nType)
{
	var szDialog		=	g_szUserBase+"?Cmd=dialog&template=dlg_editname"
	var objRecipField	=	null;

	var rgParams		=	new Array();
	rgParams["displayname"]	=	szName;
	rgParams["email"]		=	szEmail;
	rgParams["type"]		=	szAddressType;
	var hWin = window.showModalDialog(szDialog, rgParams,"dialogHeight:160px;dialogWidth:500px;status:no;resizable:no;help:no;scroll:no;")       
	if(rgParams["displayname"] != "" && rgParams["email"] != "" && rgParams["type"] != "")
	{
		objMessage_UpdateRecipients.bClearRecipients = false;

		if (rgParams["reciptype"] == 0)
		{
			//Delete recipient
			objMessage.editRecipient(nIndex,"dn", "addr", "type", 0)
		}
		else
		{
			//Modify recipient
			objMessage.editRecipient(nIndex,rgParams["displayname"], rgParams["email"], rgParams["type"], nType)
		}
	}
}

function objMessage_OnCheckNames()
{
	EnableButtons();
}


//iType indentifies which recipient well the event was generated
//iType 1 = To, iType 2 = Cc, iType 3 = Bcc
function event_onrecipclick(iType)
{
	if (event.srcElement != null)
	{
		var objElement;
		if(event.srcElement.tagName == "U")
		{
			objElement = event.srcElement.parentElement;
		}
		else if(event.srcElement.tagName == "A")
		{
			objElement = event.srcElement
		}

		if (objElement != null)
		{
			// objElement.type is email type ie.. smtp: ex: etc..
			//itype to = 1 , cc = 2, bcc = 3
			if(objElement.name == "resolved")
			{
				 EditResolvedName(objElement.innerText, objElement.id, objElement.type, objElement.nIndex, iType)
			}
			else if(objElement.name="unresolved")
			{
				LaunchANRDialog(objElement.nIndex, objElement.innerText, iType)
			}
		}
	}
}

function event_ondblclick(iType)
{

}

function LaunchANRDialog(recipIndex, szDisplayName, iType)
{
	var szDialog							=	g_szUserBase+"?Cmd=dialog&template=dlg_anr"
	var rgParams							=	new Array();

	rgParams["returnedRecipientName"]	=	null;
	rgParams["returnedRecipientEmail"]	=	null;
	rgParams["recipientTDData"]			=	null;
	rgParams["returnedRecipientEmail"]	= "";
	rgParams["returnedRecipientName"]	= "";
	rgParams["g_EditNameDialog"]		=	g_szUserBase+"?Cmd=dialog&template=dlg_editname"
	rgParams["g_DisplayName"] = szDisplayName;
	try 
	{
		var xmlHTTP = new ActiveXObject("Microsoft.xmlhttp");
		var szURL = objMessage.url + "?Cmd=editrecipient&Index=" + recipIndex;
		xmlHTTP.open("GET",szURL,false); 
		//Pass accept language up to server to maintain user context.
		if(g_szAcceptLang != "")
		{
			xmlHTTP.setRequestHeader( "Accept-Language:", g_szAcceptLang);	 
		}	
		xmlHTTP.setRequestHeader( "Content-type:", "text/xml");	 
		xmlHTTP.send();
		rgParams["recipientTDData"] = xmlHTTP.responseText;
	}
	catch(e)
	{
	}

	var vRetval = window.showModalDialog(szDialog, rgParams,"dialogHeight:300px;dialogWidth:500px;status:no;resizable:no;help:no;scroll:no")       
	if (vRetval!=null)
	{
		objMessage_UpdateRecipients.bClearRecipients = false;

		if (rgParams["returnedRecipientType"] == 0)
		{
			objMessage.editRecipient(recipIndex,"dn", "addr", "type", 0)
		}
		else
		{
			objMessage.editRecipient(recipIndex,rgParams["returnedRecipientName"], rgParams["returnedRecipientEmail"], "smtp", iType)
		}
	}
	else
	{
		return(false)
	}

}

function objMessage_UpdateRecipients()
{
	//g_objDivTo g_objDivCc g_objDivBcc are initialized in primary js file
	g_objDivTo.innerHTML = objMessage.toHTML;
	private_AdjustFieldHeight(g_objDivTo);

	g_objDivCc.innerHTML = objMessage.ccHTML;
	private_AdjustFieldHeight(g_objDivCc);

	g_objDivBcc.innerHTML = objMessage.bccHTML;
	private_AdjustFieldHeight(g_objDivBcc);
	
	if (objMessage_UpdateRecipients.bClearRecipients)
	{
		g_winDocAll["MsgTo"].value = ""
		g_winDocAll["MsgCc"].value = ""
		g_winDocAll["MsgBcc"].value = ""
	}

	if(g_winDocAll("divFreebusy") != null && m_curTab==1)
	{
		var objRecips = objMessage.resolvedRecipients
		divFreebusy.addAttendees(objRecips);

	}
	
	objMessage_UpdateRecipients.bClearRecipients = true;
}
// Function properties
objMessage_UpdateRecipients.bClearRecipients = true;

function private_AdjustFieldHeight(node)
{
	with( node.style )
	{
		display = (node.innerHTML == "") ? "none" : "";
		
		if (node.scrollHeight < (2 * g_nFontHeight) )
		{
			posHeight= g_nFontHeight;
		}
		else
		{
			posHeight= (2 * g_nFontHeight);
		}
	}
}

function InitializeRecipientFields(fIsNewMessage)
{
	//g_objDivTo g_objDivCc g_objDivBcc are initialized in primary js file
	private_AdjustFieldHeight(g_objDivTo)
	private_AdjustFieldHeight(g_objDivCc)
	private_AdjustFieldHeight(g_objDivBcc)
	objMessage.onerror = objMessage_OnError;
}
//on the event stack: event.status; event.statusText; event.responseText
function objAppointment_OnSave()
{
	EnableButtons();
	if(200 == event.status)
	{
		var obj = window.document.all.tags('BASE');
		
		if( obj && obj.length )
		{
			obj[0].href = objMessage.url + "/";

			obj = document.all("idAttachmentWell");
			var rePath = /(<A href=\")[^"]*\//ig;
			obj.innerHTML=obj.innerHTML.replace( rePath, '$1');
		}
		
		if(g_fOpenAttach)
		{
			openAttach();
			g_fOpenAttach=false;
		}
		if(window.opener && window.opener.RefreshMessageList)
		{
			window.opener.RefreshMessageList();
		}
		if( g_fCloseAfterSave)
		{
			self.close();
		}
	}
}

//on the event stack: event.status; event.statusText; event.responseText
function objMessage_OnSave()
{
	EnableButtons();
	if(event.status == 200)
	{
		if(g_fOpenAttach==true)
		{
			openAttach();
			g_fOpenAttach=false;
		}
		if( g_fCloseAfterSave==true)
		{
			self.close();
		}
	}
}
//on the event stack: event.status; event.statusText; event.responseText
function objAppointment_OnSend()
{
	if (objMessage.sent == true)
	{
		if(window.opener && window.opener.RefreshMessageList)
		{
			window.opener.RefreshMessageList();
		}
		self.close();
	}
	else
	{
		var objRecips = objMessage.unresolvedRecipients
		if (objRecips != null)
		{
			//Loop - Change the event to resolve and send
			objMessage.onupdaterecipients	= resolveAndSend;
			resolveAndSend();
		}
		EnableButtons();
	}
}
//on the event stack: event.status; event.statusText; event.responseText
function objMessage_OnSend()
{
	if (objMessage.sent == true)
	{
		self.close();
	}
	else
	{
		var objRecips = objMessage.unresolvedRecipients
		if (objRecips != null)
		{
			//Loop - Change the event to resolve and send
			objMessage.onupdaterecipients	= resolveAndSend;
			resolveAndSend();
		}

		EnableButtons();
	}

}

function resolveAndSend()
{
	objMessage_UpdateRecipients();
	var objRecips = objMessage.unresolvedRecipients;
	if (objRecips != null)
	{
		resolveRecip(objRecips)
	}
	else
	{
		//All recips are resolved or there are no recipients
		objMessage.onupdaterecipients = objMessage_UpdateRecipients;
		var objResolvedRecips = objMessage.resolvedRecipients
		if(objResolvedRecips != null)
		{
			objMessage.send();
		}
		else
		{
			//alert the user to add recipients
		}
	}
}

function resolveAllRecipients()
{
	objMessage_UpdateRecipients();
	var objRecips = objMessage.unresolvedRecipients;
	if (objRecips != null)
	{
		resolveRecip(objRecips)
	}
	else
	{
		//All recips are resolved
		objMessage.onupdaterecipients = objMessage_UpdateRecipients;
	}
}
function resolveRecip(objRecips)
{
		var objRecipient;
		var szID, iType, retval;

		if(objRecips.length != null)
		{
			//Note - Index stays static
			//When the recipient is resolved or removed the index will shuffle
			objRecipient = objRecips[0]
		}
		else
		{
			objRecipient = objRecips
		}
		//review - we may want to put the recipient type on the html from the server
		szID = objRecipient.parentElement.id
		if(szID == "recipTo")
		{
			iType = 1;
		}
		else if(szID == "recipCc")
		{
			iType = 2;
		}
		else
		{
			iType = 3;
		}
		retval = LaunchANRDialog(objRecipient.nIndex, objRecipient.innerText,iType)
		if (retval == false)
		{
			//Abort the loop - The user bailed out of the ANR dialog
			objMessage.onupdaterecipients = objMessage_UpdateRecipients;
			return(retval)
		}
		else
		{
			return(true)
		}
}

function objMessage_OnError()
{
	EnableButtons();
}
