//Copyright (c) 2000 Microsoft Corporation.  All rights reserved.
//<script>

var g_szImagePath;
var g_winDocAll;
var g_ctrlMedia = null;
var m_otxtBody = null;
var m_otblHeader = null;
var m_oEMCell = null;
var g_szContentClass;

function InitBodyHeight( nHeight )
{
	if( null != m_otxtBody )
	{
		var i;
		var objs = m_otxtBody.children;
		for( i = 0; i < objs.length; i++ )
		{
			if( "TABLE" == objs[i].tagName )
			{
				objs[i].border = 0;
				with( objs[i].style )
				{
					overflow = "auto";
					height = "100%";
					backgroundColor = "window";
					borderRight = "threedshadow 2px solid";
					borderTop = "threedshadow 2px inset";
					borderLeft = "threedshadow 2px inset";
					borderBottom = "threedshadow 2px solid"
				}
				break;
			}
		}
	}
}

function window.onload()
{
	g_winDocAll = window.document.all;
	try  //g_iInstanceType may not even exist (in which case we can ignore this)
	{
		if (g_iInstanceType != null && g_iInstanceType.length > 0) //this is a read appt response form
		{
			if (Number(g_iInstanceType) > 0) //recurring
			{
				//put the recurrence pattern in the When field
				if (g_PrintRecurrencePattern.length)
				{
					g_winDocAll['spanWhenText'].innerText = g_PrintRecurrencePattern;
				}
			}
		}
	}
	catch(e)
	{
	}

	g_szImagePath = g_szVirtualRoot + "/img/";

	window.document.attachEvent("onkeydown",event_onkeydown);

	m_otblHeader = AccessFld(g_winDocAll.tblHeader);

	m_otxtBody = AccessFld(g_winDocAll.txtMsgBody);
	InitBodyHeight();

	var fEnableMM = false;
	if( null != m_otxtBody )
	{
		if( null != g_rgInlines )
		{
			var oInlines = inlineAttachments(document);
			if( null != oInlines )
			{
				fEnableMM = true;
			}
		}
	}

	if( fEnableMM )
	{
		var obj = AccessFld(g_winDocAll.multimedia);
		if( obj )
		{
			var fInstalled = oClientCaps.isComponentInstalled("{6E767503-024A-11D3-9D47-00C04F68AC7D}", "ComponentID", g_szEMVer);

			if( fInstalled )
			{
				insertMediaCtrl();
			}
			else
			{
//				obj.style.display="";
			}
		}
	}

	var szText = "";
	var iImp = parseInt(g_szImportance);
	if( !isNaN(iImp) )
	{
		if (iImp == 2)
		{
			szText = L_MessageSentWithHighImportance_Text;
		}
		else if (iImp == 0)
		{
			szText = L_MessageSentWithLowImportance_Text;
		}
	}

	var iFlag = parseInt(g_szFlag);
	if( !isNaN(iFlag) )
	{
		//array set in webclientutil.js
		if( 0 < L_rgszFlags[iFlag].length )
		{
			if( szText.length )
			{
				szText += '\r\n' + L_rgszFlags[iFlag];
			}
			else
			{
				szText = L_rgszFlags[iFlag];
			}
		}
	}
	if( 0 < szText.length )
	{
		var obj = AccessFld( g_winDocAll.trStatus );
		obj.style.display = "";

		obj = AccessFld( g_winDocAll.idStatusText );
		obj.style.display = "";
		obj.innerText = szText;
	}
	CheckHeaderFieldsHeight();

	if( null != g_ctrlMedia )
	{
		ctrlMedia_positionUpdate();
	}
}

function window.onbeforeprint()
{
	if( null != m_otxtBody )
	{
		m_otxtBody._szTitle = document.title;
		document.title = "";

		var objs = m_otxtBody.children;
		for( i = 0; i < objs.length; i++ )
		{
			if( "TABLE" == objs[i].tagName )
			{
				with( objs[i].style )
				{
					overflow = "visible";
					height = "auto";
					borderRight = "";
					borderTop = "";
					borderLeft = "";
					borderBottom = ""
				}
				break;
			}
		}
	}
}

function window.onafterprint()
{
	document.title = m_otxtBody._szTitle;

	if( null != g_ctrlMedia )
	{
		ctrlMedia_positionUpdate();
	}
	InitBodyHeight( m_otxtBody._nHeight );
}

function tbPick(szCommand)
{
	if(event.button == 2)
	{
		return(false);
	}
	var szURL  = window.document.location.protocol + "//" + window.document.location.hostname  + window.document.location.pathname + "?Cmd=" + szCommand;
	var fOpen = true;
	switch(szCommand.toUpperCase())
	{
	case "PRINT":
		window.print();
		fOpen=false;
		break;
	case "MOVE":
		//Move message function is defined in webclientutil.js
		var szMsgURL = window.document.location.protocol + "//" + window.document.location.hostname  + window.document.location.pathname;
		var szContentClass = "urn:content-classes:message";
		if (MoveMessage(g_szUserBase, szMsgURL, g_szVirtualRoot, szContentClass))
		{
			if (window.opener != null) 
			{
				window.opener.RefreshMessageList();
			}
			self.close();
		}
		fOpen=false;
		break;
	case "DELETE":
		DeleteMessage(szURL, g_szUserBase)
		fOpen=false;
		break;
	case "MEDIATOOL":
		insertMediaCtrl();
		fOpen=false;
		break;
	case "HELP":
		LaunchHelp();
		fOpen=false;
		break;
	}
	if(fOpen)
	{
		var oDate = new Date();
		szURL += "&tzoffset=" + oDate.getTimezoneOffset();
		openNewWindow(szURL, "", 640, 480);
		self.close();
	}
	return(true);
}

function event_onkeydown()
{
	switch(event.keyCode)
	{
	case 27:
		window.close();
		break;
	}
	return(true);
}

function CheckHeaderFieldsHeight()
{
	var fields = m_otblHeader.children[4].children;
	var i;
	var nBegin = 2;
	var nLine = fields[nBegin].children[0].children[0].offsetHeight;
	for( i = nBegin; i < fields.length - 1; i++ )
	{
		var objItem = fields[i].children[1];
		if( objItem )
		{
			objItem = objItem.children[0];
			if( objItem )
			{
				objItem.style.overflow = "visible";
				objItem.runtimeStyle.overflow = "auto";
				objItem.runtimeStyle.height = (objItem.scrollHeight > nLine) ? 2 * nLine : nLine;
			}
		}
	}

	var objItem = fields[fields.length - 1].children[1];
	if( objItem )
	{
		objItem = objItem.children[0];
		if( objItem && ((objItem.scrollHeight > objItem.offsetHeight) || (objItem.scrollWidth > objItem.offsetWidth)) )
		{
			objItem.style.overflow = "visible";
			objItem.runtimeStyle.overflow = "auto";
			objItem.runtimeStyle.height = 2 * nLine;
		}
	}
}

function ctrlMedia_readyState()
{
	if( null != event.srcElement.object )
	{
		var obj = AccessFld(g_winDocAll.multimedia);
		if( obj )
		{
			obj.style.display="none";
		}

		g_ctrlMedia = event.srcElement;
		var objItem = g_ctrlMedia.parentElement;
		objItem.style.display = "";
		g_ctrlMedia._fHide = true;

		ctrlMedia_AddEventFor( 'command(val)', 'return ctrlMedia_Command(val);' );
		ctrlMedia_AddEventFor( 'ResolveFileName(val)', 'return ctrlMedia_ResolveRef(val)' );

		g_szEMgid = g_ctrlMedia.object.GroupID;
		document.body._EMGroupID = g_szEMgid;

		var szHTML = '<object ID="EMConduct" CLASSID="CLSID:D2646502-2424-11D3-9D50-00C04F68AC7D" style="display:none"></object>';
		document.body.insertAdjacentHTML( "afterBegin", szHTML );

		if( null != m_otxtBody )
		{
			AddMMObjTags( document );
		}
		
		if( null != objItem )
		{
			g_ctrlMedia._minHeight = g_ctrlMedia.object.MinHeight;
			g_ctrlMedia._offset = 0;
			g_ctrlMedia.onresize=ctrlMedia_positionUpdate;
			ctrlMedia_positionUpdate();
			window.onresize=ctrlMedia_positionUpdate;
		}
	}
}

function insertMediaCtrl()
{
	var objBody = document.body;

	try
	{
		var szText =	'<SPAN id="_spMedia" class="trButtons" STYLE="display:none;position:absolute;z-index:2">'+
						'<OBJECT ID="ctrlMedia" CLASSID="CLSID:6E767503-024A-11D3-9D47-00C04F68AC7D" '+
						' onreadystatechange="ctrlMedia_readyState()">' +
						'<PARAM NAME="EnableRecord" VALUE="0">'+
						'<PARAM NAME="ShowHide" VALUE="-1">'+
						'</OBJECT></SPAN>';
		objBody.insertAdjacentHTML("afterBegin", szText );
	}
	catch( e )
	{
	}
}
