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


var g_iAddCount=0;
var g_iTabTopOffset = 38;
var g_fHasGottenFolders = false;
var g_rgAuthDomains = new Array();
var g_szrgErrors    = new Array();

/*******Init Error String************/
m_szrgErrors = new Array();
m_szrgErrors[900]   = L_errCommunicationfailure_ErrorMessage;
m_szrgErrors[401]   = L_errLogonFailure_ErrorMessage;
m_szrgErrors[404]   = L_ErrorItemNotFound_ErrorMessage;
m_szrgErrors[403]   = L_errAccessDenied_ErrorMessage;
m_szrgErrors[412]   = L_errPreconditionfailure_ErrorMessage; 
m_szrgErrors[500]   = L_errInternalServerError_ErrorMessage;
m_szrgErrors[501]   = L_errInternalServerError_ErrorMessage;
m_szrgErrors[502]   = L_errBadGateway_ErrorMessage;  //DAV return Bad Gateway.  
// This error occurs when copying folders between private and public stores
m_szrgErrors[503]   = L_errServiceNotAvailable_ErrorMessage;

function objTree_OnAddNodes()
{
	if(g_fHasGottenFolders == false)
	{ 
		if (!g_fPublicOnly)
		{
			g_fHasGottenFolders=true;
			
			if (g_szPublicFolderUrl == "")
			{
				var szPublicFolderUrl = window.location.protocol + "//" + window.location.hostname + "/public/";
				objTree.addHierarchy(szPublicFolderUrl, false);
			}
			else
			{
				objTree.addHierarchy(g_szPublicFolderUrl, false);
			}
		}
	}
}

function objTree_OnFolderOperationComplete()
{
	if(event != null && event.ContentType == 1)
	{
		try
		{
			if(parent != null && parent.viewer != null && null != parent.viewer.RefreshMessageList)
			{
				parent.viewer.RefreshMessageList();
			}
		}
		catch(e)
		{
		}
	}
}

function objTree_OnPickFolder()
{
	event.cancelBubble  = true;
	event.returnValue   = false;
	
	if(event)
	{
		if(event.isContext == true)
		{
			var node = objTree.selectedNode;
			var y=event.clientY+15
			var x = event.clientX;
			idTreeMenu.menuShow(true,x, y);
		}
		else
		{
			Navigate(event.URL + "?Cmd=contents");          
		}
	}
}

function objTree_OnAddHierarchy()
{
	
	objTree.style.display = ""
	event.cancelBubble  = true;
	event.returnValue   = false;
}

function ToggleTab(srcElementID)
{
	event.cancelBubble = true;
	event.returnValue = false; 

	if (!g_fPublicOnly)
	{
		var tempEl;
		var srcEl = window.document.all(srcElementID);
	
		if(g_iAddCount == 0 && srcEl.id == "show_folders")
		{
			g_iAddCount++;
		
			objTree.onreadystatechange = initializeTree;
			objTree.addBehavior(g_szVirtualRoot + "/CONTROLS/ExchangeTree.htc");
		}  
	  
		if(event.srcElement.HEADER == null)
		{
			return;
		}
	
		if(srcEl.id == "show_folders")
		{
			srcEl.style.posTop = g_iTabTopOffset + 25;
		}
		if(srcEl.id == "show_navbar")
		{
			srcEl = window.document.all("show_folders");
			srcEl.style.posTop = window.document.body.offsetHeight - 25;
		}    
	}
}

function initializeTree()
{
	if (objTree.readyState == "complete")
	{
		if (g_fPublicOnly)
		{
			objTree.addHierarchy(window.location, true);
			treeProgress.style.display = "none"
		}
		else
		{
			objTree.addHierarchy(g_szUserBase, true);
			treeProgress.style.display = "none"
		}
	}
}

function window.onload()
{
	if(!g_szUserBase)
	{
		return(false);
	}       

	var node1 = window.document.all("show_navbar");
	var node2 = window.document.all("show_folders");

	if (g_fPublicOnly)
	{
		node1.style.display="none";
		objTree.onreadystatechange = initializeTree;
		objTree.addBehavior(g_szVirtualRoot + "/controls/exchangetree.htc")
		node2.style.position="absolute";
		node2.style.top = g_iTabTopOffset;
		node2.style.height = document.body.offsetHeight;  
		node2.style.zIndex = 15; 
		window.document.all("treepane").style.backgroundColor="window";
		window.document.all("treepane").style.height=document.body.offsetHeight;
	}
	else
	{
		node1.style.position="absolute";
		node1.style.top = g_iTabTopOffset;
		node2.style.position="absolute";
		node2.style.top = document.body.offsetHeight - 25;
		node2.style.zIndex = 15; 
		window.document.all("treepane").style.backgroundColor="window";
	}
}

function window.onresize()
{
	try
	{
		if (!g_fPublicOnly)
		{
			var node = window.document.all("show_navbar");
			node.style.height = document.body.offsetHeight-(g_iTabTopOffset);        
			node = window.document.all("show_folders");
			node.style.height = document.body.offsetHeight-(g_iTabTopOffset+25);  

			if(node.style.posTop !=  g_iTabTopOffset+25)
			{
				node.style.top = document.body.offsetHeight-25;  
			}
		}
		else
		{
			var node = window.document.all("treepane");
			node.style.height = document.body.offsetHeight-(g_iTabTopOffset+25);
			node = window.document.all("show_folders");
			node.style.height = document.body.offsetHeight-(g_iTabTopOffset+25);
		}
	}
	catch(e)
	{
		
	}
}

function Navigate(url)
{
	
	if(parent != null && url != null)
	{
		if(parent.viewer != null)
		{
			parent.viewer.location = url;
		}   
	}
	
	
}

function slide_Tabs()
{
	ToggleTab(event.srcElement.parentElement.id);
}

function DoRenameCommand()
{
	var objFolder   = objTree.selectedNode;
	var szName      = objFolder.displayname;
	var szURL       = objFolder.url;
	
	var szNewName   =   prompt(L_RenameCurrentFolder_Text, szName);
	if(szNewName != null)
	{
		objTree.renameFolder(szURL, szNewName);
	}
}

function DoMoveCopyCommand(mode)
{
		var szDialog                        =   g_szUserBase+"?Cmd=dialog&template=dlg_movecopy"
		var szNewFolderDialog               =   g_szUserBase +"?Cmd=dialog&template=dlg_newfolder"      
		var rgParams                        =   new Array();
		rgParams["g_OpenerTreeCtrl"]        =   objTree;
		rgParams["g_CurrentFolderNode"]     =   objTree.selectedNode;
		rgParams["g_Location"]              =   g_szUserBase;
		rgParams["g_VirtualRoot"]           =   g_szVirtualRoot;
		rgParams["g_NewFolderDialogPath"]   =   szNewFolderDialog;    
		rgParams["g_MoveCopyMode"]          =   mode;
		var hWin = window.showModalDialog(szDialog, rgParams,"dialogHeight:400px;dialogWidth:350px;status:no;resizable:no;help:no;")       
}

function DoNewFolderCommand()
{
		//Verify that user has access to folder
		
		if(!f_Authenticate(objTree.selectedNode.url))
		{
			return(false)
		}
		
		var szNewFolderDialog           =   g_szUserBase +"?Cmd=dialog&template=dlg_newfolder"
		var rgParams                    =   new Array(); 
		rgParams["g_OpenerTreeCtrl"]    =   objTree;                  
		rgParams["g_CurrentFolderNode"] =   objTree.selectedNode;
		rgParams["g_Location"]          =   g_szUserBase;
		rgParams["g_VirtualRoot"]       =   g_szVirtualRoot;
		var hWin = window.showModalDialog(szNewFolderDialog, rgParams,"dialogHeight:400px;dialogWidth:350px;status:no;resizable:no;help:no;")   
		
}

function f_Authenticate(szURL)
{
	var szServerName = f_getServerName(szURL);
	var iStatus = 0;
	var bretval = true;
	var szDocumentDomain = window.document.domain
	if(szDocumentDomain.toLowerCase() != szServerName.toLowerCase())
	{
		//Check the cache to see if already authenticated
		if(null == g_rgAuthDomains[szServerName])
		{
			/*** Send an options to the server to cause authentication to the new domain */
			var objXMLHTTP = new ActiveXObject("Microsoft.XMLHTTP");
			objXMLHTTP.open( "OPTIONS", szURL,false);
			objXMLHTTP.send();
			
			if(200 != objXMLHTTP.status)
			{
				
				f_HandleError(iStatus);
				bretval = false;
			}
			else
			{
				//Set empty string to server name.  We only use this array as a look up.
				
				g_rgAuthDomains[szServerName] = "";
				bretval = true;
			}
		}   
		else
		{
			bretval = true;
		}
	}       
	return(bretval)
	
	
}

function f_getServerName(szURL)
{
	var rgVals = f_getServerName.re.exec(szURL)
	return(rgVals[1])

}
f_getServerName.re =  new RegExp("^https?://([^/]+)","i")

function f_HandleError(num)
{
	if(g_szrgErrors[num])
	{
		alert(g_szrgErrors[num]);
		return true;
	}
	else
		return false;
}



function MenuSelect(item)
{
	switch (item)
	{
		case "Delete": 
			var node    =   objTree.selectedNode;
			var szName  =   node.displayname;
			var szURL   =   node.url;
			if(confirm(L_ConfirmDelete_Text))
			{
				objTree.deleteFolder(szURL);            
			}
		break;
		case "Rename":
			DoRenameCommand();
		break;
		case "Copy":
			DoMoveCopyCommand("Copy");
		break;
		case "Move":
			DoMoveCopyCommand("Move");
		break;
		case "New Folder":
			DoNewFolderCommand();
		break;
		case "Open":
			var node = objTree.selectedNode;
			if(node)
			{
				Navigate(node.url + "?Cmd=contents");       
			}
		break;
		case "OpenNewWindow":
			var node        = objTree.selectedNode;
			var szURL       = node.url + "?Cmd=contents";
			var windowName  = new String(Math.round(Math.random() * 100000));
			var hWin = window.open(szURL,windowName,"toolbar=1,location=1,directories=1,status=1,menubar=1,scrollbars=1,resizable=1")
		break;
	
	}            
	return (false);
}
