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

var g_rgXmlDataNodes = new Array();
var g_szImagePath;
var g_fLoadComplete = false;
var g_rgDINode = new Array();


function tbContentElement_DocumentComplete()
{
	if ( g_fLoadComplete == true)
	{
		var objDOM;
		var szBodyHTML;	
		objDOM = tbContentElement.DOM;
		if(idReplyFwdMsgTextHeader)
		{
				szTextHeader = idReplyFwdMsgTextHeader.innerHTML;
		}	
		szBodyHTML =  objDOM.body.innerHTML;
		objDOM.body.innerHTML = '<DIV>&nbsp;</DIV><BLOCKQUOTE dir=ltr style="MARGIN-RIGHT: 0px">' + szTextHeader + szBodyHTML + '</BLOCKQUOTE>';
	}
}

function window.onload()
{
	var x;
	var szType;
	var gfNewMessage = false;
	var szURL  = window.document.location.protocol + "//" + window.document.location.hostname  + window.document.location.pathname; 
	var szURLParams = document.location.search;
	szURLParams = szURLParams.toLowerCase();

	var objBody = document.body;
	var obj = AccessFld(objBody.all.idFormatbar);
	obj.addBehavior( g_szVirtualRoot + "/CONTROLS/formatbar.htc");

	fixIE_width();

	if (szURLParams.indexOf("cmd=new") != -1 )
	{
		gfNewMessage = true;
		InitializeHTMLEditor(tbContentElement);
		txtSubject.attachEvent("onchange", event_onSubjectChange);        
	}
	else if (szURLParams.indexOf("cmd=reply") != -1 )
	{		
		gfNewMessage = false;
	}
	else if (szURLParams.indexOf("cmd=forward") != -1 )
	{	
		gfNewMessage = false;
	}
	else if (szURLParams.indexOf("cmd=open") != -1 )
	{
		gfNewMessage = false;
	}
	else
	{
		gfNewMessage = false;
	}
	if (!gfNewMessage)
	{	    
		loadBodyContents(szURL);	    
	}    
	if(szURLParams != null)
	{
		szType = szURLParams.split("=");
		szType = szType[1];
	}  
	g_szImagePath = g_szVirtualRoot + "/img/";
	setTimeout("forceImagePaint()",100);
}

function loadBodyContents(szURL)
{
	try
	{
		tbContentElement.LoadURL(szURL+"?Cmd=body") 
	}
	catch(e)
	{
		//Guard against any mhtml errors here
	}    
	g_fLoadComplete = true;
	return (true);
}

function forceImagePaint()
{  
	window.document.body.insertAdjacentHTML("beforeEnd","<img style='display:none' src='"+g_szVirtualRoot + "/img/post.gif'>");
}


function event_onSubjectChange() 
{  
	try 
	{
		_tdThreadTopic.innerText = event.srcElement.value
	}
	catch(e)
	{
		//Fail silently
	}
}
function window.document.onkeydown()
{  
	if(event.keyCode==27) //ESC = close
	{
		if( confirm(L_AreYouSureCloseMessage_Text) )
		{
			window.close();
		}
	}

	if( event && !event.ctrlKey)
	{
		 return(true);
	}
	else if(event.keyCode==13) //ctrl-Enter = Send
	{ 
		onCbClick( "Send" );       
	} 
	else if(event.keyCode==83) //ctrl-S = Save
	{
		onCbClick( "Save" );       
	}
}

var g_fOpenAttach		=	false;
function onCbClick(szCommand, varValue )
{
	if(event && event.button == 2)
	{
		return(false);
	}
	switch(szCommand.toUpperCase()) 
	{
		case "POST":
			UpdateMessageData();
			try 
			{
				objMessage.post();
			}
			catch(e)
			{
				alert(e.description)
				break;
			}
		  
			break;
		case "SAVE":
			UpdateMessageData();
			try 
			{
				objMessage.save();
			}
			catch(e)
			{
				alert(e.description)
				break;
			}            
			break;          
		case "ATTACH":
			UpdateMessageData();
			objMessage.save();
			g_fOpenAttach=true;
			//will call objMessage_OnSave()
			break;            
		case "IMPHIGH":
			objMessage.importance = varValue?2:1;
			break;
		case "IMPLOW":
			objMessage.importance = varValue?0:1;
			break;   
		case "OPTIONS":
			openOptions();
			break;
		case "HELP":
			LaunchHelp();	
		break;            
	}
}

function openAttach()
{
	var msgURL = objMessage.url;
	var rgParams = new Array();
	rgParams["MessageURL"] = msgURL;
	rgParams["WindowOpener"] = window;
	rgParams["InsertAttachWellAfterElement"] = tbContentElement;
	var szPath	=	g_szUserBase+"?Cmd=dialog&template=dlg_attach";
	var hWin = window.showModelessDialog(szPath, rgParams,"dialogHeight:400px;status:no;resizable:yes;help:no;center:yes;")
}

function UpdateMessageData()
{
	objMessage.subject		=	txtSubject.value;
	objMessage.htmlBody		=	tbContentElement.DocumentHTML;
	objMessage.setSchemaProperty("urn:schemas:httpmail:","thread-topic",  _tdThreadTopic.innerText);    
	objMessage.addPostData("MsgClass", "IPM.Post");  
}

function objMessage_OnSave()
{
	var status			=	event.status;
	var statusText		=	event.statusText;
	var	responseText	=	event.responseText;
	var unResCount		=	event.unresolvedRecipCount;
	if(status == 200)
	{
		var obj = window.document.all.tags('BASE');
		if( obj && obj.length )
		{
			obj[0].href = objMessage.url + "/";

			obj = document.all("idAttachmentWell");
			var rePath = /(<A href=\")[^"]*\//ig;
			obj.innerHTML=obj.innerHTML.replace( rePath, '$1');
		}
		
		if(g_fOpenAttach==true)
		{
			openAttach();
			g_fOpenAttach=false;
		}
	}		
}
function CloseForm()
{
	self.close();
}
function objMessage_OnPost()
{
	var status			=	event.status;
	var statusText		=	event.statusText;
	var	responseText	=	event.responseText;	
	if(status == 200)
	{
		CloseForm();
	}
}
