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

var g_lastSingleRowObject = event;
var g_fRowChanged = true;
var g_szLastMSG = "";
var g_msgViewerTop;
var g_winDocAll;
var g_HTTPEmptyDeletedItems = null;

var m_fPreviewPane = false;
var m_oUserOpts = null;
var m_szOptsFile = "OLWA_Settings";
var m_szPreviewPane = "PreviewPane";

function onGroupExpanded()
{
	setTimeout("fnForceImagePaint()",100)
}

function event_exchangeviewer_onerror()
{
	if (event.command == "BDELETE") {
		alert(L_errCantDelete_ErrorMessage);
	}
}
function idMsgViewer_onbeforedelete()
{
	if(!confirm(L_WarningDeletePermanent_Text))
	{
		event.returnValue = false;
	}
}
function window.onload()
{
	g_winDocAll = window.document.all;
	g_msgViewerTop=idMsgViewer.offsetTop;
	idMsgViewer.addBehavior(g_szVirtualRoot + "/CONTROLS/view.htc");
	
	m_oUserOpts = g_winDocAll.userSettings;
	if( null != m_oUserOpts )
	{
		m_oUserOpts.load( m_szOptsFile );
		var szShow = m_oUserOpts.getAttribute( m_szPreviewPane );
		if( "true" == szShow )
		{
			onCbClickEvent(g_winDocAll.previewpane, false);
		}
	}
	if(!g_fPublicOnly &&
	   !fVerifyTimeZone(g_tzOffsetJan, g_tzOffsetJul ))
	{
		idStatus.style.display = "";
		idStatus.insertAdjacentText("beforeEnd", g_szTimeZoneConflict)
	}
	window.onresize();
	//note g_szVirtualRoot must be set to call fnForceImageRepaint
	setTimeout("fnForceImagePaint()",100)


}

function window.onresize()
{
	var nHeight = (document.body.clientHeight - g_msgViewerTop);
	if( 0 >= nHeight )
	{
		return;
	};
	if( m_fPreviewPane )
	{
		g_winDocAll.previewPane.runtimeStyle.height = g_winDocAll.idMsgViewer.runtimeStyle.height = (nHeight - 5) / 2;
	}
	else
	{
		g_winDocAll.idMsgViewer.runtimeStyle.height = nHeight;
	}
}

function changePage()
{
	if (event.keyCode == 13)
	{
		g_winDocAll.idMsgViewer.gotoPage(idPageControl_PageNumInput.value)
	}
}

function persistSortOrder()
{
	//Following code calls the server with a paramerterized url to persist the view descriptor

	var objCol = event.col
	if (objCol != null)
	{
		//Note: toUTF8() is defined in webclientutil.js
		var szURL = idMsgViewer.url + "?Cmd=getview&SortBy=" + toUTF8(objCol.colName) + "&SortOrder=" + event.sortDir;

		if(persistSortOrder.m_objPersistView== null)
		{
			persistSortOrder.m_objPersistView = new ActiveXObject("Microsoft.xmlhttp")
		}
		persistSortOrder.m_objPersistView.open("GET",szURL, true);
		if(g_szAcceptLang != "" )
		{
			persistSortOrder.m_objPersistView.setRequestHeader("Accept-Language:",g_szAcceptLang);
		}
		
		persistSortOrder.m_objPersistView.send()

	}

}
persistSortOrder.m_objPersistView = null;

function changeView(szXMLViewPath)
{
	szXMLURL = g_szUserBase + g_szFolder + "/?Cmd=getview&View=" + szXMLViewPath;
	//Todo: We'll need to change this after the view translation becomes async
	idMsgViewer.viewDescriptor =  szXMLURL;
	idMsgViewer.update();
}

function updateContext()
{
	idPageControl_PageNumInput.value = idMsgViewer.currentPage;
	idPageControl_PageMaxInput.value = idMsgViewer.pageCount;

	tdPageControl.style.display = (idMsgViewer.pageCount <= 1) ? "none" : "";

	 setTimeout("fnForceImagePaint()",100)
	//TODO: IE5 image repaint workaround
	//1 ms seems to work but .1sec is fine
}

function ShowHideFolders()
{
	try
	{
		if(window.parent && window.parent.navbar)
		{
			var obj = window.parent.document.all;
			var objFRAMESET=null;
			var i;
			for(i=0; i < obj.length;i++)
			{
				if(obj[i].tagName.toUpperCase()=="FRAMESET")
				{
					objFRAMESET = obj[i];
				}
			}
			if(objFRAMESET)
			{
				if(objFRAMESET.cols == "0,*")
				{
					objFRAMESET.cols = "150,*";
				}
				else
				{
					objFRAMESET.cols = "0,*";
				}
			}
		}
	}
	catch(e)
	{
	}
}

function MoveMessages()
{
	var szParentURL = g_szUserBase;
	var rgParams = new Array();
	var szFolderName = g_szFolder;
	var szDialog = g_szUserBase+"?Cmd=dialog&template=dlg_movecopy"
	var szNewFolderDialog = g_szUserBase +"?Cmd=dialog&template=dlg_newfolder"
	var szDataString					=	idMsgViewer.getSelectedXML();
	if(szDataString == "")
	{
		alert(L_NoMessagesSelected_Text);
		return(false);
	}

	rgParams["g_OpenerTreeCtrl"]		=	null;
	rgParams["g_CurrentFolderNode"]		=	null;
	rgParams["g_Location"]				=	g_szUserBase;
	rgParams["g_VirtualRoot"]			=	g_szVirtualRoot;
	rgParams["g_NewFolderDialogPath"]	=	szNewFolderDialog;
	rgParams["g_MoveCopyMode"]			=	"Move";
	rgParams["g_MessageDataString"]		=	szDataString;
	rgParams["out_DestinationURL"]		=	"";
	rgParams["out_ObjectURL"]			=	"";
	rgParams["out_Verb"]				=	"";
	var bMove = window.showModalDialog(szDialog, rgParams,"dialogHeight:400px;dialogWidth:350px;status:no;resizable:no;help:no;")
	if (bMove)
	{
		RefreshMessageList();
	}
}

function RefreshMessageList()
{
	if (idMsgViewer != null)
	{
		idMsgViewer.refreshPage();
	}
}

function event_emptydeleteditems()
{
	if (g_HTTPEmptyDeletedItems && g_HTTPEmptyDeletedItems.readystate == 4)
	{
		if(g_HTTPEmptyDeletedItems.status < 300)
		{
			var nErrno = g_HTTPEmptyDeletedItems.responseXML.selectSingleNode("result/errorcode")
			if (null != nErrno && 0 != nErrno.text)
			{
				if (null != g_HTTPEmptyDeletedItems.responseXML.selectSingleNode("result/errortext"))
				{
					alert(g_HTTPEmptyDeletedItems.responseXML.selectSingleNode("result/errortext").text);
				}
				else
				{
					alert(nErrno.text+" "+L_idsUnknownError_Text);
				}	
			}
			else
			{
				if (0 == g_szFolder.indexOf(g_szDeletedItemsURL))
				{
					this.location = g_szUserBase + g_szDeletedItemsURL + "/?Cmd=contents";
				}
			}	
		}
		else
		{
			alert("Error " + g_HTTPEmptyDeletedItems.status + ": " + g_HTTPEmptyDeletedItems.statusText);        
		}
	}
}

function onCbClick(szCommand, fState)
{
	if(event && event.button == 2)
	{
		return(false);
	}

	switch(szCommand.toUpperCase())
	{
		case "NEWITEM":
		case "NEWMESSAGE":
			openNewWindow(g_szUserBase + g_szDraftsURL + "/?Cmd=new","",640, 500);
		break;

		case "NEWCONTACT":
			openNewWindow(g_szUserBase + g_szContactsURL + "/?Cmd=new","",700, 500);
		break;

		case "NEWAPPOINTMENT":
			openNewWindow(g_szUserBase + g_szCalendarURL + "/?Cmd=new","",700, 500);
		break;

		case "NEWPOST":
			openNewWindow(g_szUserBase + g_szFolder + "/?Cmd=newpost","",700, 500);
		break;

		case "REPLY":
			if(idMsgViewer)
			{
				idMsgViewer.openMessage(1);
			}
		break;
		case "REPLYALL":
		    if(idMsgViewer)
		    {
		    	idMsgViewer.openMessage(2);
		    }
		break;
		case "FORWARD":
		    if(idMsgViewer)
		    {
		    	idMsgViewer.openMessage(3);
		    }
		break;
		case "MOVECOPY":
			MoveMessages();
		break;
		case "DELETE":
		    if(idMsgViewer)
		    {
		    	idMsgViewer.deleteSelectedItems();
		    }
		break;
		case "EMPTYDELETEDITEMS":
			if (confirm(g_szConfirmEmptyDeleted))
			{
				var szURL = g_szUserBase + "?Cmd=emptydeleteditems";
				g_HTTPEmptyDeletedItems = new ActiveXObject("Microsoft.xmlhttp");
				g_HTTPEmptyDeletedItems.onreadystatechange = event_emptydeleteditems;
				g_HTTPEmptyDeletedItems.open("GET", szURL, true);
				g_HTTPEmptyDeletedItems.send();
			}
		break;
		case "PREVIEWPANE":
		    if(null == g_winDocAll.previewPane)
			{
				return(false);
			}
			if( fState )
			{
				g_winDocAll.previewPane.style.display = "";
				g_winDocAll.idMsgViewer.linkSpec = "%Dataurl%?cmd=preview";
			}
			else
			{
				g_winDocAll.previewPane.style.display = "none";
				g_winDocAll.idMsgViewer.linkSpec = "";
			}
			if( m_fPreviewPane != fState )
			{
				if( null != m_oUserOpts )
				{
					m_oUserOpts.setAttribute( m_szPreviewPane, (fState) ? 'true': 'false');
					m_oUserOpts.save(m_szOptsFile);
				}
			}
			m_fPreviewPane = fState;
			window.onresize();
		break;
		case "SHOWFOLDERS":
			ShowHideFolders();
		break;
		case "CHECKMESSAGES":
			RefreshMessageList();
		break;
		case "ADDRESSBOOK":
			openGal();
		break;
		case "HELP":
			LaunchHelp();
		break;
	}
}

