//Copyright (c) 2000 Microsoft Corporation.  All rights reserved.
//<script> VID colors
var g_iCurFlag = -1;
var g_iCurStatus = -1;
var g_szImagePath;
var g_winDocAll;
var m_otxtBody = null;
var m_otblHeader = null;

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 > 0) //recurring
		{
			//put the recurrence pattern in the When field
			if (g_PrintRecurrencePattern.length)
			{
				g_winDocAll['spanWhenText'].innerText = g_PrintRecurrencePattern;
			}    
		}
	} 
	catch(e)
	{
	}
	
	var x=0,iImp=0,iFlag=0;
	
	g_szImagePath = g_szVirtualRoot + "/img/";

	if( null != g_winDocAll.txtMsgBody )
	{
		m_otxtBody = AccessFld(g_winDocAll.txtMsgBody);
		InitBodyHeight();
	}

	window.document.attachEvent("onkeydown",event_onkeydown);
	m_otblHeader = AccessFld(g_winDocAll.tblHeader);

	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();
}

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;
	InitBodyHeight( m_otxtBody._nHeight );
}

function event_nop()
{
	return;
}

function event_listen_ATD_noresponse()
{
	if(m_objXmlHTTP.readystate == 4)
	{
		m_objXmlHTTP.onreadystatechange = event_nop;
		if(m_objXmlHTTP.status < 300)
		{
			var nErrno = m_objXmlHTTP.responseXML.selectSingleNode("result/errorcode")
			if (null != nErrno && 0 != nErrno.text)
			{
				if (null != m_objXmlHTTP.responseXML.selectSingleNode("result/errortext"))
				{
					alert(m_objXmlHTTP.responseXML.selectSingleNode("result/errortext").text);
				}
				else
				{
					alert(nErrno.text+" "+L_idsUnknownError_Text);
				}	
			}
			else
			{

				//Refresh Message List
				if(window.opener && window.opener.RefreshMessageList)
				{
						window.opener.RefreshMessageList();
				}
				
				if (null != m_objXmlHTTP.responseXML.selectSingleNode("result/redirecturl"))
				{
					this.location = m_objXmlHTTP.responseXML.selectSingleNode("result/redirecturl").text;
				}
				else
				{
					window.close();
				}
				
			}	
		}
		else
		{
			alert("Error " + m_objXmlHTTP.status + ": " + m_objXmlHTTP.statusText);        
		}
	}


}

var m_objXmlHTTP;
function tbPick(szCommand) 
{
	if(event.button != 2)
	{
		if (g_szURL.length)
		{
			var szURL  = g_szURL;
		}
		else 
		{
			var szURL  = window.document.location.protocol + "//" + window.document.location.hostname  + window.document.location.pathname + "?Cmd=" + szCommand; 
		}    
		
		var fOpen = true;
		
		switch(szCommand.toUpperCase()) 
		{
			case "REMOVEFROMCALENDAR":
		        m_objXmlHTTP = new ActiveXObject("Microsoft.XMLhttp");
		        m_objXmlHTTP.open("GET", szURL + "?Cmd=" + szCommand.toLowerCase(), true);
				if(g_szAcceptLang != "" )
				{
					m_objXmlHTTP.setRequestHeader("Accept-Language:",g_szAcceptLang);
				}
		        m_objXmlHTTP.setRequestHeader("Content-Type", "text/xml");
		        m_objXmlHTTP.onreadystatechange	= event_listen_ATD_noresponse;	
		        m_objXmlHTTP.send(); 
				fOpen=false;
				break;		
			case "ACCEPT":
			case "TENTATIVE":
			case "DECLINE":
				var oDate = new Date();
		        m_objXmlHTTP = new ActiveXObject("Microsoft.XMLhttp");
		        m_objXmlHTTP.open("GET", szURL + "?Cmd=" + szCommand.toLowerCase() + "&tzoffset=" + oDate.getTimezoneOffset(), true);
		        if(g_szAcceptLang != "" )
				{
					m_objXmlHTTP.setRequestHeader("Accept-Language:",g_szAcceptLang);
				}
		        m_objXmlHTTP.setRequestHeader("Content-Type", "text/xml");
		        m_objXmlHTTP.onreadystatechange	= event_listen_ATD_noresponse;	
		        m_objXmlHTTP.send(); 
				fOpen=false;
				break;

			case "PRINT":
				window.print();
				 fOpen=false;
				break;

			case "HELP":
				LaunchHelp();	
				fOpen=false;
				break;

			case "MOVE":
				alert("movecopy nyi");
				fOpen=false;
				break;

			case "DELETE":
				if( g_fPublicFolder )
				{
					DeleteMessage(szURL, g_szUserBase)
				}
				else
				{
					tbPick("DECLINE");
				}
				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[0].children;
	var i;
	var nBegin = 3;	// This magic number is where the header fields we care about
					// in this function start.  3 makes this code skip over the
					// status and online fields in the form.
	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;
			}
		}
	}
}

