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

var m_iIMGTagIndex          =   0;
var m_iLinkTagIndex         =   1;
var m_iDIVWellIndex         =   4;
var m_iExpressIndex         =   5;
var m_iXMLIndex             =   6;
var m_iLinkIMGIndex         =   0;
var m_iLinkSpanIndex        =   1;
var m_objNewFolderHTTP;   //This object will be used to create new folders
var m_szIllegalCharacters       = /[\/\\&~\?\*\|\<\>\"\#\;:\+]/;

var m_szPlusIcon                = "tree-splus.gif";
var m_szMinusIcon               = "tree-sminus.gif";
var m_szNoChildrenIcon          = "tree-clear9.gif";

var m_szDefaultFont             = "Tahoma";
var m_iDefaultFontSize          = "12px";
var m_szDefaultBGColor          = "window";

var m_szNewFolderName           = null;
var m_szNewFolderURL            = null;
var m_szContentClass            = null;
var m_szParentFolderURL         = null;
var m_szCurrentPrivURL          = "";
var m_szLastRenderedURL         = "";
var m_objLastFocusedElement     = null;
var m_objLastPickElement        = null;
var m_fHasConnected             = false;
var m_objAfterElement           = null;
var m_rgSpecialFolderURL        = new Array();
var m_gobjHTTPSpecialFolders    = null;
var m_objHTTPRenderFolder       = null; //xmlhttp object used for expanding nodes
var m_objMSHTTP_Request         = null;
var m_objHTTPMoveCopy           = null; //xmlhttp object used for move copy operation
var m_objHTTPRenameFolder       = null; //xmlhttp object used for renaming folders

var m_objContentClassHTTP       = null;
var m_szrgErrors                = null;
var m_szAcceptLang              = "";  //Supports acceptLang Property
var m_objAllElements            = this.all; //Global reference to the htc elements collections
var m_objIMGPlus                = null;
var m_objIMGMinus               = null;
var m_objIMGClear               = null;
var m_szDocumentDomain          = window.document.domain;
var m_rgAuthDomains             = new Array();  //Contains authenticated domain names

var m_rgReturnValue = { "HASSUBS" : 0,
						"CONTENTCLASS" : 0,
						"DISPLAYNAME" : 0,
						"HREF" : 0
					  };

var m_rgNodeIMGS = { "TOP" : "tree-mailbox.gif",
					 "URN:CONTENT-CLASSES:CALENDARFOLDER" : "tree-appointment.gif",
					 "URN:CONTENT-CLASSES:CONTACTFOLDER" : "tree-contact.gif",
					 "URN:CONTENT-CLASSES:MAILFOLDER" : "tree-folder.gif",
					 "URN:CONTENT-CLASSES:JOURNALFOLDER" : "journal.gif",
					 "URN:CONTENT-CLASSES:NOTEFOLDER" : "stickynote.gif",
					 "URN:CONTENT-CLASSES:TASKFOLDER" : "task.gif",
					 "DEFAULT" : "tree-folder.gif"
				   };

/*******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;
	/****************/

var m_bstrRequestXML = '' +
		'<searchrequest xmlns="DAV:">' +
		  '<sql> Select "DAV:displayname" as displayname, "http://schemas.microsoft.com/exchange/content-href" as contenthref, ' +
		  '"DAV:hassubs" as hassubs,"DAV:contentclass" as contentclass ' +
			'FROM Scope(\'SHALLOW TRAVERSAL OF ""\') ' +
			'WHERE "DAV:isfolder" = true ' +
			'ORDER BY "DAV:displayname" ASC' +
			'</>' +
		'</>';


var m_szMaster_evt_TransferDataXML = '<transferdata xmlns:a="DAV:">' +
										'<item>' +
											'<objectname></objectname>' +
											'<objectid></objectid>' +
											'<content-class></content-class>' +
											'<droptype></droptype>' +
											'<a:href></a:href>' +
										'</item>' +
									'</transferdata>';

var m_objXMLMaster_evt_TransferData = null;
var m_fShowLines = true;
var m_fShowIcons = true;
var m_fAllowDragDrop = true;

/************PROPERTY ACCESS METHODS****************/

function get_AcceptLang()
{
	return(m_szAcceptLang)
}

function put_AcceptLang(szAcceptLang)
{
	m_szAcceptLang = szAcceptLang;
}

function put_ShowLines(newVal)
{
	if("boolean" == typeof(newVal))
	{
		m_fShowLines = newVal;
	}
	else if( "string" == typeof(newVal) )
	{
		// Convert to boolean
		m_fShowLines = (newVal == "true");
	}
}

function put_ShowIcons(newVal)
{
	if("boolean" == typeof(newVal))
	{
		m_fShowIcons = newVal;
	}
	else if( "string" == typeof(newVal) )
	{
		// Convert to boolean
		m_fShowIcons = (newVal == "true");
	}
}

function put_AllowDragDrop(newVal)
{
	if("boolean" == typeof(newVal))
	{
		m_fAllowDragDrop = newVal;
	}
	else if( "string" == typeof(newVal) )
	{
		// Convert to boolean
		m_fAllowDragDrop = (newVal == "true");
	}
}

function get_SelectedNode()
{
	if( null == m_objLastPickElement)
	{
		return(null);
	}
	
	var retObject = new Object();
	var oItem = m_objLastPickElement.parentElement;
	retObject.id            = oItem.id;
	retObject.url           = oItem.url;
	retObject.contentclass  = oItem.contentclass;
	retObject.displayname   = oItem.displayname;
	retObject.left          = oItem.offsetLeft
	return(retObject);
}
/******************************************/

/************EVENT FIRING METHODS****************/
function fire_OnAddNodes(szAddFolderURL)
{
	var objEvent = createEventObject();

	objEvent.AddedFolderURL = szAddFolderURL;
	id_OnAddNodes.fire(objEvent);
}

function fire_OnRemoveNodes(szRemovedFolderURL)
{
	var objEvent = createEventObject();

	objEvent.RemovedFolderURL = szRemovedFolderURL;
	id_OnRemoveNodes.fire(objEvent);
}

function fire_OnPickFolder(node, fContext)
{
	var objEvent = createEventObject();

	objEvent.URL         = node.url;;
	objEvent.DisplayName = node.children[m_iLinkTagIndex].children[m_iLinkSpanIndex].innerText;
	objEvent.isContext   = fContext;
	id_OnPickFolder.fire(objEvent);
}

function fire_OnConnected()
{
	var objEvent = createEventObject();

	objEvent.ConnectedURL = m_szCurrentPrivURL;
	id_OnConnected.fire(objEvent);
}

function fire_OnRenderURLComplete(iStatus, StatusText)
{
	var objEvent = createEventObject();

	objEvent.LastRenderedURL = m_szLastRenderedURL;
	objEvent.iStatus         = iStatus;
	objEvent.StatusText      = StatusText;
	id_OnRenderURLComplete.fire(objEvent);
}

function fire_OnFolderOperationComplete(szCommand, iStatus, szStatusText, szLocation, szResponseXML, iContentType)
{
	var objEvent = createEventObject();

	objEvent.Command        = szCommand;
	objEvent.iStatus        = iStatus;
	objEvent.StatusText     = szStatusText;
	objEvent.Location       = szLocation;
	objEvent.ResponseXML    = szResponseXML;
	objEvent.ContentType    = iContentType;
	id_OnFolderOperationComplete.fire(objEvent);
}
/******************************************/
/************EVENT HANDLING METHODS****************/

function evt_OnDocumentReady()
{
	if(this.style.fontFamily)
	{
		m_szDefaultFont = this.style.fontFamily;
	}
	
	if(this.style.fontSize)
	{
		m_iDefaultFontSize = this.style.fontSize;
	}
	if(this.style.backgroundColor)
	{
		m_szDefaultBGColor = this.style.backgroundColor;
	}
	
	if(m_fShowLines == false)
	{
		with(this.__idMasterNode.children[0])
		{
			children[2].style.display =
			children[3].style.display =
			children[5].style.display = "none";
		}
	}
	if(m_fShowIcons == false)
	{
		/*Remove the IMG tag and replace it with a DIV to avoid Gets on the images*/
		with(this.__idMasterNode.children[0])
		{
			// Extend the line from the + img tag to the caption by 5 pixels
			children[3].style.width = 15;
			// Since the img is no longer going to be shown extend the line up 4 pixels more to
			// accomodate the lack of a sybling's img tag.
			children[2].style.height = 19;
			children[2].style.top = -9;
			// Remove folder icon.
			children[1].children[0].removeNode(true);
			var node = window.document.createElement("DIV");
			// Insert a div to take its place.
			children[1].insertAdjacentElement("afterBegin", node);
		}
	}
}

function evt_OnKeyDown()
{

	event.cancelBubble=true;

	var iCode       = event.keyCode;
	var fCtrl       = event.ctrlKey;
	var tempEl      = null;

	if(null == m_objLastFocusedElement)
	{
		return(false);
	}

	var iAction=0
	switch(iCode)
	{
		case 13://enter
			event.returnValue=false;
			mf_SelectNode(m_objLastFocusedElement, false);
			return(false);
		case 37://left
		case 109:
			iAction=3;
			break;
		case 39://right
		case 107:
			iAction = 2;
			break;
		case 187:
			if(event.shiftKey)
			{
				iAction = 2;
			}
			break;
		case 189:
			if(event.shiftKey)
			{
				iAction = 3;
			}
			break;
	}

	tempEl = m_objLastFocusedElement.parentElement;
	if(null == tempEl)
	{
		return(false);
	}

	if(iAction == 2)
	{
		var containerNode = tempEl.children[m_iDIVWellIndex];

		if(containerNode.style.display == "none")
		{
			// Call expand node and if it currently has no children
			// force a call to the server to retrieve sub elements.
			mf_ExpandNode(tempEl, containerNode.children.length == 0 );
		}
	}
	else if(iAction == 3)
	{
		mf_CollapseNode(tempEl);

	}

	tempEl.children[1].focus();
}

function evt_OnDragStart()
{
	switch(event.srcElement.name)
	{
		case "link":
		{
			if(m_fAllowDragDrop != true)
			{
				return;
			}
			window.event.dataTransfer.effectAllowed="all";
			//BUILD XML stream for the data string
			if (m_objXMLMaster_evt_TransferData == null)
			{
				m_objXMLMaster_evt_TransferData = new ActiveXObject("Microsoft.xmldom")
				m_objXMLMaster_evt_TransferData.loadXML(m_szMaster_evt_TransferDataXML)
			}
	
			var objXML =  m_objXMLMaster_evt_TransferData.documentElement;
			var node = objXML.selectSingleNode("item/objectid");
			node.text = event.srcElement.parentElement.id;
			node = objXML.selectSingleNode("item/content-class");
			node.text = event.srcElement.parentElement.contentclass;
	
			node = objXML.selectSingleNode("item/a:href");
			node.text = event.srcElement.parentElement.id;
			node = objXML.selectSingleNode("item/objectname");
			node.text = event.srcElement.innerText;
			node = objXML.selectSingleNode("item/droptype");
			node.text = "folder";

			var dataString = objXML.xml;
	
			window.event.dataTransfer.setData("Text", dataString);
			window.event.dataTransfer.setData("URL", event.srcElement.parentElement.id)
			break;
		}
		default:
			event.cancelBubble=true;
			event.returnValue=false;
			break;
	}
}

function evt_OnDrop()
{
	switch(event.srcElement.name)
	{
		case "link":
		{
			if(m_fAllowDragDrop != true)
			{
				return(false);
			}
			var szData = window.event.dataTransfer.getData("Text");
			var szURL  = window.event.srcElement.parentElement.parentElement.id;

			var szCommand = (window.event.ctrlKey) ? "copy" : "move";

			f_DropXMLData(szData,szURL,szCommand);
			break;
		}
	}
}

function evt_OnDragOver()
{
	
	switch(event.srcElement.name)
	{
		case "link":
		{
			if(m_fAllowDragDrop != true)
			{
				return(false);
			}
			
			event.cancelBubble= true;
			event.returnValue = false;
	
			if (window.event.dataTransfer.getData("Text") != null)
			{
				window.event.dataTransfer.dropEffect = (event.ctrlKey) ? "copy" : "move";
			}
			break;
		}
	}
}

function evt_OnContextMenu()
{
	switch(event.srcElement.name)
	{
		case "link":
		{
			event.cancelBubble=true;
			event.returnValue=false;
			var tempEl = event.srcElement.parentElement;
			tempEl.children[1].focus();
			mf_SelectNode(tempEl, true);
			break;
		}
	}
}

function evt_OnClick()
{
	switch(event.srcElement.name)
	{
		case "link":
		{
			var tempEl = event.srcElement.parentElement;
			mf_SelectNode(tempEl, false);
			break;
		}
		case "expand":
		{
			event.cancelBubble=true;
			event.returnValue=false;
			var containerNode = event.srcElement.parentElement.children[m_iDIVWellIndex];
	
			if(containerNode.style.display == "none")
			{
				// Call expand node and if it currently has no children
				// force a call to the server to retrieve sub elements.
				mf_ExpandNode(event.srcElement.parentElement, containerNode.children.length == 0);
			}
			else
			{
				mf_CollapseNode(event.srcElement.parentElement);
			}
			break;
		}
	}
}

function evt_OnFocus()
{
	switch(event.srcElement.name)
	{
		case "link":
			m_objLastFocusedElement = event.srcElement;
			break;
	}
}

/******************************************/
/************PUBLIC METHODS****************/
function f_CloneTree(treeNode)
{
	// Copy the item wholesale.
	this.innerHTML = treeNode.innerHTML;

	// Now all that is left is to find the highlighted item in the source.
	var divColl = m_objAllElements.tags("DIV");
	var i;
	for(i=0; i < divColl.length; i++)
	{
		var newNode = divColl[i];
		if(newNode.masterNode)
		{
			if(newNode.children[m_iLinkTagIndex].style.backgroundColor.toUpperCase() == "HIGHLIGHT")
			{
				m_objLastPickElement = m_objLastFocusedElement = newNode.children[m_iLinkTagIndex];
				break;
			}
		}
	}
}

function f_AddHierarchy(szRootURL, fExpanded)
{
	if(szRootURL == null || szRootURL.length == 0)
	{
		return(null)
	}

	var props,displayName, contentClass, hassubs, href;
	var http_propFind  = new ActiveXObject("Microsoft.XMLhttp");
	var objFolder = new Object();
	var szPropfind =    '<?xml version="1.0"?>' +
						'<a:propfind xmlns:a="DAV:" xmlns:b="urn:schemas:httpmail:" >' +
						'<a:prop><a:href/><a:displayname/><a:hassubs/><a:contentclass/>' +
						'<b:inbox/><b:calendar/><b:sentitems/><b:deleteditems/><b:contacts/><b:drafts/><b:outbox/><b:sendmsg/><b:msgfolderroot/>' +
						'</></>';

	try 
	{
		http_propFind.open( "PROPFIND", szRootURL,false);
		http_propFind.setRequestHeader( "Depth:", "0");
		http_propFind.setRequestHeader( "Brief:", "t");
		http_propFind.setRequestHeader( "Content-type:", "text/xml");
		http_propFind.send(szPropfind);
	}
	catch(e)
	{
		var iErrCode = (e.number & 0xFFFF)
		if( iErrCode == 5)
		{
			alert(L_errCrossDomainAccessDenied_ErrorMessage)
		}
		else
		{
			alert(e.description + " " + iErrCode)
		}
				
		return(false);
	}	

	if(http_propFind.status != 207)
	{
		return(null);
	}

	var objResponseXML = http_propFind.responseXML;
	href            =   objResponseXML.selectNodes("a:multistatus/a:response/a:propstat/a:prop/a:href");
	displayName     =   objResponseXML.selectNodes("a:multistatus/a:response/a:propstat/a:prop/a:displayname");
	contentClass    =   objResponseXML.selectNodes("a:multistatus/a:response/a:propstat/a:prop/a:content-class");
	hassubs         =   objResponseXML.selectNodes("a:multistatus/a:response/a:propstat/a:prop/a:hassubs");
	mf_initSpecialFolders(objResponseXML)

	if( displayName.length )
	{
		displayName = displayName[0].text;
	}
	if( displayName == null || displayName == "" )
	{
		displayName = szRootURL;
	}

	objFolder.displayName   = displayName ;
	objFolder.contentClass  = (contentClass.length) ? contentClass[0].text : "";
	objFolder.url           = (href.length) ? href[0].text : "";
	objFolder.hassubs       = (hassubs.length) ? (hassubs[0].text != "0") : false;
	var node = f_AddFolder(objFolder, null);
	if(null == node)
	{
		//The node will not be added if it is a duplicate folder in the hierarchy
		return(null)
	}

	if(fExpanded == true)
	{
	   m_objAfterElement = node;
	   mf_RenderURL(szRootURL, node);
	}
	else
	{
		m_szLastRenderedURL = szRootURL
		fire_OnRenderURLComplete(http_propFind.status, http_propFind.statusText)
	}
	return(node);
}

function f_DropXMLData(szXMLData, szDestinationFolderURL, szCommand)
{
	if(szXMLData == null)
	{
		return(false);
	}
	var objXML = new ActiveXObject("Microsoft.xmldom");
	objXML.loadXML(szXMLData);
	if(objXML.xml == null || objXML.xml == "")
	{
		return(false);
	}

	var objHREFNodes = objXML.selectNodes("transferdata/item/a:href")
	szCommand = szCommand.toLowerCase();
	var szXMLVerb = szCommand;
	if(objHREFNodes.length > 1)
	{
		szCommand = "b" + szCommand;
	}

	var objContentTypeNode = objXML.selectSingleNode("transferdata/item/content-class");
	var szContentClass = objContentTypeNode.text;

	var iContentType = 0;
	if (szContentClass.search(/ipm[.]/i) != -1)
	{
		iContentType = 1;  //Set content type Message or document
	}
	else if (szContentClass.search(/ipf[.]/i) != -1 || szContentClass.indexOf("folder") >= 0)
	{
		iContentType = 2;  //Set content type to hierarchy
	}

	m_szLastVerb = szCommand;


	var szXMLRequest = "";
	var szFolderURL  = "";
	switch(szCommand)
	{
		case "bcopy":
		case "bmove":
			if (objHREFNodes.length > 0)
			{
				
			  //Find out where the messages came from
				var szOriginFolderURL = objHREFNodes.item(0).text;
				var re = /(.*\/)[^\/]+$/;
				var rgRes = szOriginFolderURL.match(re);
				if( null == rgRes[1] )
				{
					return(false);
				}
				szFolderURL = rgRes[1];

				var szXMLRequest;
				szXMLRequest = "<?xml version='1.0'?><a:" + szXMLVerb + " xmlns:a='DAV:'><a:target>";
				
				for(i=0; i < objHREFNodes.length; i++)
				{
					szXMLRequest += objHREFNodes.item(i).xml;
				}
				
				szXMLRequest += "</a:target></a:" + szXMLVerb + ">";
			}
				
			break;

		case "move":
		case "copy":
			szFolderURL = objXML.selectSingleNode("transferdata/item/a:href").text;

			var szURLCompName = szFolderURL
			var iIndex = szURLCompName.substr(0,szURLCompName.length-1).lastIndexOf("/")
			szURLCompName = szURLCompName.substr(iIndex + 1)
			szDestinationFolderURL = szDestinationFolderURL + szURLCompName ;
			break;
	}

	if (szFolderURL != "")
	{
		mf_MoveCopyObject(szFolderURL,szDestinationFolderURL, szCommand,szXMLRequest, iContentType);
	}
	else
	{
		return(false);
	}
	return(true);
}


//$$NOTE: objFolderDesc needs to have the following expando properties:
//  - content-class
//  - displayName
//  - url
//  - IMG
//  - hassubs

function f_AddFolder(objFolderDesc, objAfterElement)
{
	m_obobjAfterElement = objAfterElement;
	var szContentClass  = objFolderDesc.contentClass;
	var szURL           = objFolderDesc.url;
	var szDisplayName   = objFolderDesc.displayName;
	var szIMG           = objFolderDesc.Image;
	var fHasSubs        = objFolderDesc.hassubs;
	
	if(szIMG == null)
	{
		szIMG = mf_GetFolderIMG(szURL, szContentClass);
		szIMG = this.ImagePath + szIMG;
	}
	return mf_AddNode(szIMG,szDisplayName, fHasSubs, szURL, objAfterElement,szContentClass);
}

function f_RemoveFolder(szFolderURL)
{
	var removeElement =   m_objAllElements[szFolderURL];
	if(removeElement == null)
	{
		return(false);
	}

	var parentEl = removeElement.parentElement;
	removeElement.children[m_iExpressIndex].style.border = "";

	removeElement.removeNode(true);
	var childCount = parentEl.children.length;

	// Back up from the node container to master Node of this element.
	var oParent = parentEl.parentElement;
	if(childCount == 0)
	{
		oParent.children[m_iIMGTagIndex].src = this.ImagePath + m_szNoChildrenIcon;
		oParent.children[m_iIMGTagIndex].style.backgroundColor = "transparent";
	}
	else if( childCount > 0 )
	{
		var borderEl = parentEl.children[childCount-1];
		if(borderEl != null)
		{
			borderEl.children[m_iExpressIndex].style.height = 0;
		}
	}

	mf_ExpandNode(oParent, false);
	fire_OnRemoveNodes(szFolderURL);
	return(true);
}

function f_CreateFolder(szParentURL,szNewFolderName, szContentClass)
{
	if(mf_IsIllegalFolderName(szNewFolderName))
	{
		alert(L_ErrorIllegalCharacter_Text);
		return;
	}

	if(!mf_Authenticate(szParentURL))
	{
		return;
	}
	m_szLastVerb = "MKCOL";
	m_szNewFolderName   = szNewFolderName;
	
	if(szParentURL.charAt(szParentURL.length-1) != "/")
	{
		szNewFolderName = "/" + toUTF8(szNewFolderName) + "/"
	}
	else
	{
		szNewFolderName = toUTF8(szNewFolderName) + "/"
	}

	var szNewLocation = szParentURL + szNewFolderName;
	evt_onCreateFolder.parentFolderURL = szParentURL;
	evt_onCreateFolder.szContentClass  = szContentClass
	mf_doMKCOL(szNewLocation,szContentClass,evt_onCreateFolder)
}

function f_DeleteFolder(szFolderURL)
{
	if(!mf_Authenticate(szFolderURL))
	{
		return;
	}
	
	var urlIndex = szFolderURL.indexOf(m_rgSpecialFolderURL["msgfolderroot"]);
	if (urlIndex != -1) //Check to see if we are in the private store
	{
		urlIndex =  szFolderURL.indexOf(m_rgSpecialFolderURL["deleteditems"]);  //Check to see if the folder is already in deleted items

		if (urlIndex == -1)
		{
			var iOffSet = 0;
			if (szFolderURL.charAt(szFolderURL.length-1) == "/")
			{
				iOffSet = 1
			}
			var iPos = szFolderURL.lastIndexOf("/", szFolderURL.length - 1 - iOffSet);
			
			var szFolderName = szFolderURL.substring(iPos + 1, szFolderURL.length - iOffSet);
			
			var szDestination = m_rgSpecialFolderURL["deleteditems"] + szFolderName;
			mf_MoveCopyObject(szFolderURL,szDestination,"MOVE", "", 2);
		}
		else
		{
			mf_DeleteFolder(szFolderURL);
		}
	}
	else
	{
		mf_DeleteFolder(szFolderURL);
	}
}

function f_RefreshFolder(szObjectURL)
{
	var node = m_objAllElements(szObjectURL);
	mf_CollapseNode(node);
	node.children[m_iDIVWellIndex].innerHTML = "";
	mf_ExpandNode(node, false);
	mf_RenderURL(szObjectURL, node);
}

function f_RenameFolder(szObjectURL, szNewName)
{
	if(mf_IsIllegalFolderName(szNewName))
	{
		alert(L_ErrorIllegalCharacter_Text);
		return(false);
	}
	
	//We must authenticate to the folder if it is in another domain
	if(!mf_Authenticate(szObjectURL))
	{
		return(false)
	}

	// Retrieve the base path minus the last folder.
	var re = /(.*\/)[^\/]+[\/]?$/;
	var rgRes = szObjectURL.match(re);
	if( null == rgRes[1] )
	{
		return(false);
	}
	var szFolderURL = rgRes[1];

	var newURL = szFolderURL + toUTF8(szNewName);
	var objFolder = m_objAllElements(szObjectURL)
	if (null==objFolder)
	{
		//If the folder is not in the tree control then return false
		return(false)
	}

	m_objHTTPRenameFolder = new ActiveXObject("Microsoft.XMLHTTP");
	m_objHTTPRenameFolder.open( "MOVE",szObjectURL,true);
	m_objHTTPRenameFolder.setRequestHeader( "Destination:", newURL);
	m_objHTTPRenameFolder.setRequestHeader("Overwrite:", "F");
	m_objHTTPRenameFolder.setRequestHeader("Translate:", "F");
	m_objHTTPRenameFolder.setRequestHeader("Allow-rename:", "t");
	m_objHTTPRenameFolder.onreadystatechange = evt_onRenameFolder;
	evt_onRenameFolder.objFolder = objFolder;
	evt_onRenameFolder.szNewName = szNewName;
	m_objHTTPRenameFolder.send();
}

function evt_onRenameFolder()
{
	if(null !=  m_objHTTPRenameFolder && m_objHTTPRenameFolder.readystate == 4 && evt_onRenameFolder.busy == false)
	{
		evt_onRenameFolder.busy = true;
		var iStatus = m_objHTTPRenameFolder.status;
		if(mf_HandleError(iStatus))
		{
			evt_onRenameFolder.busy = false;
			return;
		}
		var szLocation = m_objHTTPRenameFolder.getResponseHeader("Location")
		
		var objFolder = evt_onRenameFolder.objFolder;
		if(null == objFolder)
		{
			return;
		}

		objFolder.id = szLocation;
		objFolder.url = szLocation;
		objFolder.displayname = evt_onRenameFolder.szNewName;

		var objAnchor = objFolder.children[m_iLinkTagIndex];
		objAnchor.href = szLocation;
		objAnchor.children[m_iLinkSpanIndex].innerText  = evt_onRenameFolder.szNewName;

		//Collapse the folder
		mf_CollapseNode(objFolder);
		//Clear the the children so subfolder data is retrieved on the next
		//expansion so all url properties are current.
		objFolder.children[m_iDIVWellIndex].innerHTML = "";
		m_objHTTPRenameFolder = null;
		evt_onRenameFolder.busy = false;
		evt_onRenameFolder.objFolder = null;
		evt_onRenameFolder.szNewName = null;
	}
}
evt_onRenameFolder.busy = false;
evt_onRenameFolder.objFolder = null;
evt_onRenameFolder.szNewName = null;

function mf_ConstructBatchXML(rgObjects, szVerb)
{
	var szXMLBatchString = "<?xml version='1.0'?><a:" + szVerb + " xmlns:a='DAV:'><a:target>";

	var i;
	for(i=0; i < rgObjects.length; i++)
	{
		szXMLBatchString += "<a:href>" + rgObjects[i] + "</a:href>";
	}
	szXMLBatchString += "</a:target></a:" + szVerb + ">";
	return(szXMLBatchString);
}

function f_MoveFolder(szObjectURL, szDestinationURL)
{
	if(null != szObjectURL && null != szDestinationURL)
	{
		if(szObjectURL != szDestinationURL)
		{
			mf_MoveCopyObject(szObjectURL,szDestinationURL,"move", null, 2)
		}		
		return(true);
	}
	return(false);	
}


function f_CopyFolder(szObjectURL, szDestinationURL)
{
	if(null != szObjectURL && null != szDestinationURL)
	{
		if(szObjectURL != szDestinationURL)
		{
			mf_MoveCopyObject(szObjectURL,szDestinationURL,"copy", null, 2)
		}		
		return(true);
	}
	return(false);		
}

function mf_DoBatchOperation(rgObjectURL, szDestinationURL, szVerb,iContentType)
{
	
	// Retrieve the base path minus the last folder.
	var re = /(.*\/)[^\/]*$/;
	var rgRes = rgObjectURL[0].match(re);
	if( null == rgRes[1] )
	{
		return(false);
	}
		
	var szFolderURL = rgRes[1];
	/*******************/ 
	var szXML = mf_ConstructBatchXML(rgObjectURL, String(szVerb).toLowerCase());
	mf_MoveCopyObject(szFolderURL, szDestinationURL, szVerb,szXML, iContentType);
}

function mf_MoveCopyObject(szFolderURL, szDestinationFolderURL, szCommand, szXMLRequest, iContentType)
{
	szCommand = szCommand.toUpperCase();

	m_szObjectURL       = szFolderURL;
	m_szDestinationURL  = szDestinationFolderURL;
	m_szLastVerb        = szCommand;

	try
	{
		m_objHTTPMoveCopy = new ActiveXObject("Microsoft.XMLHTTP");
		m_objHTTPMoveCopy.open( m_szLastVerb, m_szObjectURL,true);
		m_objHTTPMoveCopy.setRequestHeader( "Destination:", m_szDestinationURL);
		m_objHTTPMoveCopy.setRequestHeader( "Content-type:", "text/xml");
		m_objHTTPMoveCopy.setRequestHeader("Overwrite:", "F");
		m_objHTTPMoveCopy.setRequestHeader("Translate:", "F");
		m_objHTTPMoveCopy.setRequestHeader("Allow-rename:", "t");
		if (iContentType == 2 ) //If the content type is heirarchy we want to set the event method that processe
		{
			m_objHTTPMoveCopy.onreadystatechange = evt_onMoveCopyFolder;
		}
		else
		{
			m_objHTTPMoveCopy.onreadystatechange = evt_onMoveCopyContentComplete;
		}
			
		m_objHTTPMoveCopy.send(szXMLRequest);
	}
	catch(e)
	{
		throw(e);
	}
}

function f_ExpandFolder(szNodeID, flag)
{
	var node = m_objAllElements(szNodeID);

	if(node == null)
	{
		return;
	}
	mf_ExpandNode(node, flag);
}

function f_CollapseFolder(szNodeID)
{
	var node = m_objAllElements(szNodeID);
	if(node == null)
	{
		return;
	}
	mf_CollapseNode(node);
}

/******************************************/
/************On Ready State FUNCTIONS****************/

function evt_onMoveCopyFolder()
{
	if(m_objHTTPMoveCopy != null && evt_onMoveCopyFolder.busy == false && m_objHTTPMoveCopy.readyState == 4)
	{
		evt_onMoveCopyFolder.busy = true;
		var iStatus         = m_objHTTPMoveCopy.status;
		var szLocation      = m_objHTTPMoveCopy.getResponseHeader("Location");
		if(mf_HandleError(iStatus))
		{
			evt_onMoveCopyFolder.busy = false;
			return;
		}		
		if(iStatus == 201)
		{
			if(m_szLastVerb == "MOVE")
			{
				f_RemoveFolder(m_szObjectURL)
			}

			var iOffSet = 0;
			if (szLocation.charAt(szLocation.length-1) == "/")
			{
				iOffSet = 1
			}

			
			var iPos = szLocation.lastIndexOf("/", szLocation.length - 1 - iOffSet);
			var szFolderName = szLocation.substring(0,iPos + 1);
			
			mf_RenderURL(szFolderName, m_objAllElements(szFolderName));
		}
		 else
        {
            alert(m_objHTTPMoveCopy.status + "\n" + m_objHTTPMoveCopy.statusText);
        }
		var szStatusText    = m_objHTTPMoveCopy.statusText;
		fire_OnFolderOperationComplete(m_szLastVerb, iStatus, szStatusText, szLocation,"")
		evt_onMoveCopyFolder.busy = false;
		m_objHTTPMoveCopy = null;
		m_szLastVerb=null;
	}
}
evt_onMoveCopyFolder.busy = false;

function evt_onMoveCopyContentComplete()
{
	if(m_objHTTPMoveCopy != null && !evt_onMoveCopyContentComplete.busy && m_objHTTPMoveCopy.readyState == 4)
	{
		evt_onMoveCopyContentComplete.busy = true;
		var iStatus         = m_objHTTPMoveCopy.status;
		//Server rerturns 400 bad request for BMOVE across stores
		if(iStatus == 400 )
		{
			alert(m_szrgErrors[502])
			evt_onMoveCopyContentComplete.busy = false;
			return;
		}
		if(mf_HandleError(iStatus))
		{
			evt_onMoveCopyContentComplete.busy = false;
			return;
		}
		var szStatus        = m_objHTTPMoveCopy.statusText;
		
		var szLocation      = m_objHTTPMoveCopy.getResponseHeader("Location")
		var szResponseXML   = m_objHTTPMoveCopy.responseXML;
		fire_OnFolderOperationComplete(m_szLastVerb, iStatus, szStatus, szLocation,szResponseXML, 1)
		m_szLastVerb=null;
		evt_onMoveCopyContentComplete.busy = false;
	}
}
evt_onMoveCopyContentComplete.busy = false;

function evt_onCreateFolder()
{
	if(m_objNewFolderHTTP != null && !evt_onCreateFolder.busy && m_objNewFolderHTTP.readyState == 4)
	{
		var iStatus = m_objNewFolderHTTP.status
		evt_onCreateFolder.busy = true;
		if(mf_HandleError(iStatus))
		{
			evt_onCreateFolder.busy = false;
			m_objNewFolderHTTP = null;
			return;
		}		
		var szURL = m_objNewFolderHTTP.getResponseHeader("Location");
		var szStatusText = m_objNewFolderHTTP.statusText;
		
		if (iStatus == 207 || iStatus == 201)
		{
			m_szNewFolderURL = szURL;
			//Set the context for the render url handler.
			mf_onRenderFolder.contextCommand = "MKCOL";
			mf_onRenderFolder.status          = iStatus;
			mf_RenderURL(evt_onCreateFolder.parentFolderURL, window.document.all(evt_onCreateFolder.parentFolderURL));
		}
		 else
        {
            alert(m_objNewFolderHTTP.status + "\n" + m_objNewFolderHTTP.statusText);
        }
		m_objNewFolderHTTP = null;
		evt_onCreateFolder.busy = false;
	}
}
evt_onCreateFolder.busy = false;
evt_onCreateFolder.parentFolderURL = "";
evt_onCreateFolder.szContentClass  = "";

function evt_onDeleteFolder()
{
	
	var iStatus = 0;
	
	if(m_objMSHTTP_Request != null && evt_onDeleteFolder.busy != true && m_objMSHTTP_Request.readyState == 4)
	{
		evt_onDeleteFolder.busy = true;
		iStatus = m_objMSHTTP_Request.status
		if(mf_HandleError(iStatus))
		{
			evt_onDeleteFolder.busy= false;
			return;
		}
		var szLocation      = m_objMSHTTP_Request.getResponseHeader("Location")
		var szStatusText    = m_objMSHTTP_Request.statusText;
		var szResponseXML   = m_objMSHTTP_Request.responseXML;
		m_objMSHTTP_Request = null;
		if(iStatus == "200")
		{
			f_RemoveFolder(m_szObjectURL);
			fire_OnFolderOperationComplete(m_szLastVerb, iStatus, szStatusText, szLocation,szResponseXML)
		}
	
		this.style.cursor = "";
	}
	evt_onDeleteFolder.busy = false;
}
evt_onDeleteFolder.busy = false;

function mf_initSpecialFolders(objResponseXML)
{
	var szURLNode = objResponseXML.selectNodes("a:multistatus/a:response/a:href");;
	if(szURLNode[0] != null)
	{
		szURLNode = szURLNode[0].text;
		m_szCurrentPrivURL = szURLNode;
	}

	var xmlNodes = objResponseXML.selectNodes("a:multistatus/a:response/a:propstat/a:prop/*");
	var i;
	for(i=0;i <= xmlNodes.length - 1; i++)
	{
		var szNodeText = xmlNodes.item(i).text;
		if (szNodeText.charAt(szNodeText.length-1) != "/")
		{
			szNodeText += "/"
		}

		m_rgSpecialFolderURL[xmlNodes.item(i).baseName] = szNodeText;
		switch(xmlNodes.item(i).baseName)
		{
			case "inbox":
				 m_rgNodeIMGS[szNodeText]  =    "tree-inbox.gif";
			break;
			case "sentitems":
				m_rgNodeIMGS[szNodeText]   =    "tree-sent_itm.gif";
			break;
			case "deleteditems":
				m_rgNodeIMGS[szNodeText]   =    "tree-deleted.gif";
			break;
			case "outbox":
				m_rgNodeIMGS[szNodeText]   =    "tree-outbox.gif";
			break;
		}
	}
}

function mf_onRenderFolder()
{
	if(m_objHTTPRenderFolder != null && m_objHTTPRenderFolder.readyState == 4 && mf_onRenderFolder.busy != true)
	{
		mf_onRenderFolder.busy = true;
		var objXML      = m_objHTTPRenderFolder.responseXML;
		var iStatus     = m_objHTTPRenderFolder.status;
		var szStatus    = m_objHTTPRenderFolder.statusText;
		mf_HandleError(iStatus);

		m_objHTTPRenderFolder = null;

		nodeMultistatus = objXML.documentElement;
		if (nodeMultistatus==null)
		{
			mf_onRenderFolder.busy = false;
			return(false);
		}

		//Get the collection of nodes that we want
		var objCollHREF = nodeMultistatus.selectNodes("a:response/a:href");
		var objCollProps = nodeMultistatus.selectNodes("a:response/a:propstat/a:prop");
		var szBaseImgPath = this.ImagePath;
		//Iterate through each node
		var i;
		for (i = 0; i < objCollProps.length; i++)
		{
			//Check to see of contenthref is available
			var objNode = objCollProps.item(i).selectSingleNode("contenthref")
			var szHREF = (null != objNode) ? objNode.text : objCollHREF(i).text;
			
			objNode		= objCollProps.item(i).selectSingleNode("contentclass");
			var szContentClass = (null != objNode)? objNode.text : "";
			
			objNode		= objCollProps.item(i).selectSingleNode("hassubs");
			var szHasSubs = (null != objNode)? objNode.text : "";
			szHasSubs = (szHasSubs == "1") ? true : false;

			objNode		= objCollProps.item(i).selectSingleNode("displayname");
			var szDisplayName = (null != objNode)? objNode.text : "";

			var szIMG = mf_GetFolderIMG(szHREF, szContentClass);
			szIMG          =  szBaseImgPath + szIMG;

			mf_AddNode(szIMG,szDisplayName, szHasSubs, szHREF, m_objAfterElement,szContentClass);
		}

		var szID = (m_objAfterElement != null) ? m_objAfterElement.id : "";

		mf_onRenderFolder.busy = false;
		
		this.expandFolder(m_objAfterElement.id);
		//Check for context.  oncreateFolder sets context so
		//after the tree control paints the folder the event fires.
		if(null != mf_onRenderFolder.contextCommand)
		{
			fire_OnFolderOperationComplete(mf_onRenderFolder.contextCommand, mf_onRenderFolder.status, "","","",0);
			mf_onRenderFolder.contextCommand = null;
		}
		fire_OnRenderURLComplete(iStatus, szStatus);
		mf_onRenderFolder.szContextCommand = null;
	}
}
mf_onRenderFolder.busy = false;
mf_onRenderFolder.contextCommand = null;
mf_onRenderFolder.status = 0;

/******************************************/
/************PRIVATE METHODS****************/
//szURL - URL url to folder or vroot
//This function returns true if the authentication was successful or if we are already authenticated.
function mf_Authenticate(szURL)
{
	var szServerName = mf_getServerName(szURL);
	var iStatus = 0;
	var bretval = true;
	if(m_szDocumentDomain.toLowerCase() != szServerName.toLowerCase())
	{
		//Check the cache to see if already authenticated
		if(null == m_rgAuthDomains[szServerName])
		{
			/*** Send an options to the server to cause authentication to the new domain */
			var objXMLHTTP = new ActiveXObject("Microsoft.XMLHTTP");
			try
			{
				//Note: This is operation is done synchronously.
				objXMLHTTP.open( "OPTIONS", szURL,false);
				objXMLHTTP.send();
			}
			//If the users security settings are set so that cross domain access
			//is set to prompt and the user selects cancel then xmlhttp throws an error.
			catch(e)
			{
				var iErrCode = (e.number & 0xFFFF)
				if( iErrCode == 5)
				{
					alert(L_errCrossDomainAccessDenied_ErrorMessage)
				}
				else
				{
					alert(e.description + " " + iErrCode)
				}
				m_objHTTPRenderFolder = null;
				mf_onRenderFolder.busy = false;
				return(false);
			
			}
			if(200 != objXMLHTTP.status)
			{
				mf_HandleError(iStatus);
				bretval = false;
			}
			else
			{
				//Set empty string to server name.  We only use this array as a look up.

				m_rgAuthDomains[szServerName] = "";
				bretval = true;
			}
		}
		else
		{
			bretval = true;
		}
	}
	return(bretval)
	
	
}

function mf_RenderURL(szRootURL, objAfterElement)
{
	m_objAfterElement = objAfterElement;
	m_objHTTPRenderFolder = new ActiveXObject("Microsoft.XMLHTTP");
	m_objHTTPRenderFolder.open("SEARCH", szRootURL, true);
	m_objHTTPRenderFolder.setRequestHeader("Content-type", "text/xml");
	m_objHTTPRenderFolder.setRequestHeader("Brief:", "t");
	m_objHTTPRenderFolder.setRequestHeader("Translate", "f");
	if(m_szAcceptLang == "")
	{
		m_objHTTPRenderFolder.setRequestHeader("Accept-Language:",window.clientInformation.userLanguage);
		
	}
	else
	{
		m_objHTTPRenderFolder.setRequestHeader("Accept-Language:",m_szAcceptLang);
	}	
	m_objHTTPRenderFolder.onreadystatechange = mf_onRenderFolder;
	m_objHTTPRenderFolder.send(m_bstrRequestXML);

	m_szLastRenderedURL = szRootURL;
	return(true);
}

function mf_doMKCOL(szURL, szContentClass, fCallBack)
{
	var szXML = '<a:propertyupdate xmlns:a="DAV:" xmlns:b="http://schemas.microsoft.com/mapi/proptag/" >' +
				'<a:set>' +
					'<a:prop><b:0x3613001f>' + szContentClass + '</></>' +
				'</>' +
				'</>';
	
	m_objNewFolderHTTP = new ActiveXObject("Microsoft.XMLHTTP");
	m_objNewFolderHTTP.open("MKCOL", szURL, true);
	m_objNewFolderHTTP.setRequestHeader("Overwrite:", "F");
	m_objNewFolderHTTP.setRequestHeader("Content-type", "text/xml");
	m_objNewFolderHTTP.setRequestHeader("Translate", "f");
	if(fCallBack != null)
	{
		m_objNewFolderHTTP.onreadystatechange = fCallBack;
	}
	m_objNewFolderHTTP.send(szXML);
}

function mf_SelectNode(node, fFlag)
{
	if(m_objLastPickElement != null)
	{
		m_objLastPickElement.style.backgroundColor  = "transparent";
		m_objLastPickElement.style.color            = "windowtext";
	}

	m_objLastPickElement = node;
	m_objLastFocusedElement = m_objLastPickElement;

	m_objLastPickElement.style.backgroundColor  = "highlight";
	m_objLastPickElement.style.color            = "highlighttext";

	fire_OnPickFolder(m_objLastPickElement.parentElement, fFlag);
}

function mf_DeleteFolder(szURL)
{
		m_szLastVerb = "DELETE";
		m_szObjectURL = szURL;
		if(m_objMSHTTP_Request == null)
		{
			m_objMSHTTP_Request = new ActiveXObject("Microsoft.XMLHTTP");
		}
		m_objMSHTTP_Request.open( "DELETE", szURL,true);
		evt_onDeleteFolder.szURL = szURL;
		m_objMSHTTP_Request.onreadystatechange = evt_onDeleteFolder;
		m_objMSHTTP_Request.send();
		this.style.cursor = "wait";
		return(true);
}

function mf_AddNode(szIMG, szCaption, fHasChildren, szURL, objAfterElement, szContentClass)
{
	var tempNode = this.__idMasterNode;
	var newNode  = null;

	if(szContentClass == "")
	{
		szContentClass = "urn:content-classes:mailfolder";
	}
	if(window.document.all(szURL) != null)
	{
		return(null);
	}
	if(tempNode != null)
	{
		newNode = tempNode.children[0].cloneNode(true);
		newNode.id = szURL;
		newNode.url = szURL;
		var objCollChildren = newNode.children;
		var objAnchor = objCollChildren[m_iLinkTagIndex];
		
		if(fHasChildren == true)
		{
			objCollChildren[m_iIMGTagIndex].src = this.ImagePath + m_szPlusIcon;
			objCollChildren[m_iIMGTagIndex].style.backgroundColor = "window";
		}
		else
		{
			objCollChildren[m_iIMGTagIndex].src = this.ImagePath + m_szNoChildrenIcon;
			objCollChildren[m_iIMGTagIndex].style.backgroundColor = "transparent";
		}
		newNode.contentclass = szContentClass;
		newNode.displayname  = szCaption;
		
		objAnchor.href = szURL;
		objAnchor.children[m_iLinkIMGIndex].src         = szIMG;
		objAnchor.children[m_iLinkSpanIndex].innerText  = szCaption;

		if(objAfterElement == null)
		{
			this.insertAdjacentElement("BeforeEnd", newNode);
		}
		else
		{
			if(objAfterElement == null)
			{
				//Done
				return;
			}
			else
			{
				objAfterElement.children[m_iDIVWellIndex].insertAdjacentElement("BeforeEnd", newNode);
			}
		}
		var parent = newNode.parentElement;
		var index = parent.children.length-2;

		if(objAfterElement == null)
		{
			newNode.children[2].style.display="none";
		}
		if(parent.children[index] != null)
		{
			parent.children[index].children[m_iExpressIndex].style.height  = "100%";
		}
	
		fire_OnAddNodes(newNode.id);
		
		return(newNode);
	}
}

function mf_GetFolderIMG(szURL, szContentClass)
{
	var szIMG = "";

	if( null != m_rgNodeIMGS[szURL])
	{
		return m_rgNodeIMGS[szURL];
	}

	if( null == szContentClass || "" == szContentClass )
	{
		return(m_rgNodeIMGS["DEFAULT"]);
	}

	szIMG = m_rgNodeIMGS[szContentClass.toUpperCase()];
	if( null == szIMG )
	{
		szIMG = m_rgNodeIMGS["DEFAULT"];
	}
	return(szIMG);
}

function mf_getServerName(szURL)
{
	var rgVals = mf_getServerName.re.exec(szURL)
	return(rgVals[1])
}
mf_getServerName.re =  new RegExp("^https?://([^/]+)","i")

function mf_ExpandNode(node, fRefreshTree )
{
	var szGetURL = node.id;
	if(!mf_Authenticate(szGetURL))
	{
		return;
	}

	if( fRefreshTree )
	{
		mf_RenderURL(szGetURL, node);
	}
	else
	{
		if(node.children[m_iDIVWellIndex].children.length > 0)
		{
			node.children[m_iDIVWellIndex].style.display = "";
			node.children[m_iIMGTagIndex].src   = this.ImagePath + m_szMinusIcon;
			node.children[m_iIMGTagIndex].style.backgroundColor = "window"
		}
		else
		{
			node.children[m_iDIVWellIndex].style.display = "none";
			node.children[m_iIMGTagIndex].src   = this.ImagePath + m_szNoChildrenIcon;
			node.children[m_iIMGTagIndex].style.backgroundColor = "transparent"
		}
	}
}

function mf_CollapseNode(node)
{
	node.children[m_iDIVWellIndex].style.display = "none";
	if(node.children[m_iDIVWellIndex].children.length > 0)
	{
		node.children[m_iIMGTagIndex].src = this.ImagePath + m_szPlusIcon;

		//check to see if any child objects are the highlighted items
		if(m_objLastPickElement != null)
		{
			for( var tempParent = m_objLastPickElement.parentElement;
				 null != tempParent;
				 tempParent = tempParent.parentElement )
			{
				if(tempParent.id == node.id)
				{
					mf_SelectNode(node.children[m_iLinkTagIndex], false);
					return;
				}
			}
		}
	}
}

function mf_IsIllegalFolderName(szName)
{
	szName = szName.replace(/^\s*|\s*$/g, "");
	return (szName.length ==0 || szName.search(m_szIllegalCharacters) != -1);
}

function mf_HandleError(num)
{
	if(m_szrgErrors[num])
	{
		alert(m_szrgErrors[num]);
		return true;
	}
	else
		return false;
}

//returns all characters utf8 encoded
function toUTF8(szInput)
{
	var x,uch="",szRet="";

	for (x=0; x<szInput.length; x++)
	{
		var wch =szInput.charCodeAt(x);
		if (!wch)
		{
			break;  // Japanese-IE5 does not give us the correct length
		}
		if (!(wch & 0xFF80))
		{
			szRet += "%" + wch.toString(16);
		}
		else if (!(wch & 0xF000))
		{
			uch = "%" + (wch>>6 | 0xC0).toString(16) +
				  "%" + (wch & 0x3F | 0x80).toString(16);
			szRet += uch;
		}
		else
		{
			uch = "%" + (wch >> 12 | 0xE0).toString(16) +
				  "%" + (((wch >> 6) & 0x3F) | 0x80).toString(16) +
				  "%" + (wch & 0x3F | 0x80).toString(16);
			szRet += uch;
		}
	}
	return(szRet);
}

/******************************************/