//Copyright (c) 2000 Microsoft Corporation.  All rights reserved.
//<script>
function Record( nType )
{
	if ( CONST_SAVE_NO == g_nMsgSaved )
	{
		g_fReqRecord = true;
		g_nMsgSaved = CONST_SAVE_IMPLICIT;
		SaveMessage();
		return;
	}

	// need generate a temp filename 
	var szFileName = GetTempFileName();
	if( null == szFileName )
	{
		return( false );
	}

	var objDom = tbContentElement.DOM;
	var oDoc = tbContentElement.document;
	var szId=null;
	while( true )
	{
		szId = oDoc.uniqueID;
		if( null == objDom.body.all[ szId ] )
		{
			break;
		}
	}

	var szAuthor = g_szUserDisplayname;
	szAuthor = ( 0 < szAuthor.length ) ? "["+szAuthor+"] " : "";

	szFullName = szFileName;
	var re = /.*[\/\\][~]/;
	szFileName = szFileName.replace( re, "" );
	var szText = '<IMG name=' + g_szEMName +
				' WIDTH=42 HEIGHT=22 Id="' + szFullName + '" style="behavior:url(#EMConduct#' + g_szEMgid + ')"'+
				' TITLE="' + szAuthor + szFileName +'"' +
				' ALT="[' + g_szUserDisplayname + ':' + szFileName +']"' +
				' SRC="' + g_szImagePath + 'winme.gif" border=1>';

	g_ctrlMedia._InsBM = true;

	var objSelection = objDom.selection;
	if( "Control" == objSelection.type )
	{
		var objTr = objSelection.createRange();
		if( null != objTr.length )
		{
			var oItem = objTr.item( 0 );

			var oImg = objDom.createElement(szText);
			oImg = oItem.insertAdjacentElement( 'AfterEnd', oImg );

			oItem = objDom.body.createTextRange();
			oItem.moveToElementText(oImg);
			oItem.select();
		}
	}
	else
	{
		var objTr = null;
		objTr = objSelection.createRange();     
		if( !objTr)
		{
			return( false );
		}

		objTr.pasteHTML(szText);
	}
	return( true );
}

function GetTempFileName()
{
	var szTempFileName = null;
	if( 0 == g_bMedia )
	{
		return( szTempFileName );
	}

	try
	{
		szTempFileName = objEMHttp.TempFileName;
	}
	catch (e)
	{
		g_bMedia = 0;
	}
	return( szTempFileName );
}

function RemoveMMObjectTags( obj )
{
	g_nMMobjs = 0;
	if (obj == null || obj.DOM == null)
	{
		return null;
	}

	// Build up string with all current files.
	var key;
	var szExcessFiles = "";
	if( null != g_arAttachUrl )
	{
		for( key in g_arAttachUrl )
		{
			if( null != g_arAttachUrl[ key ] )
			{
				szExcessFiles += key + " ";
			}
		}
	}

	var objDom = obj.DOM;
	var objs = objDom.getElementsByName(g_szEMName);

	var szBase = "";
	var obj = window.document.all.tags('BASE');
	if( obj && obj.length )
	{
		szBase = obj[0].href;
	}

	var reSec = /\?.*/;
	var reMulti = /([0-9]_multipart\/)+/;
	var szHTML = null;
	var nItems = 0;
	if( null != objs  )
	{
		var e = new Enumerator( objs );
		for (;!e.atEnd();e.moveNext())
		{
			var objItem = e.item();
			if( "IMG" == objItem.tagname )
			{
				if( 'true' == objItem.IsDirty )
					objItem.IsDirty = true;
				else if( 'false' == objItem.IsDirty )
					objItem.IsDirty = false;

				if( true == objItem.IsDirty )
				{
					nItems++;
					objItem.Stop();
				}

				// Remove items that are in use.
				var szCid = objItem.id.replace( reSec, "");
				szCid = szCid.replace( reMulti, "" );

				var re = new RegExp( szCid + " ", "i" );
				szExcessFiles = szExcessFiles.replace( re, "" );
			}
		}

		if( nItems )
		{
			if( !g_fEMgif )
			{

				try
				{
					objEMHttp.ContentType = "image/gif";
					objEMHttp.ContentLocation = g_szEMAudGif + '.gif';
					objEMHttp.ContentId = g_szEMAudGif + '.gif';
					objEMHttp.AddImage(0, szBase+g_szEMAudGif+'.gif' );
					g_fEMgif = true;
				}
				catch (e)
				{
					if( -2147024894 != e.number )
					{
						alert(e.description);
					}
				} 
			}
		}

		var rePath = /.*[\\\/]/;
		for( e.moveFirst(); !e.atEnd(); e.moveNext() )
		{
			var objItem = e.item();
			if( "IMG" == objItem.tagname && objItem.GroupID == g_szEMgid )
			{
				g_nMMobjs++;
				if( true == objItem.IsDirty )
				{
					if( !objItem.IsEmpty )
					{
						var re = /.*[\/\\][~]/;
						szFileName = objItem.Id.replace( re, "" );

						var szUrl = SendAttachment( objItem.Id, szFileName );
						if( 0 < szUrl.length )
						{
							objItem.IsDirty = false;
							objItem.src = g_szEMAudGif + '.gif';
							objItem.Id = szFileName;
							if( null == g_arAttachUrl)
							{
								g_arAttachUrl = new Array();
							}
							g_arAttachUrl[ objItem.Id ] = szUrl.replace( rePath, "" );
						}
					}
					else	// Delete empty bookmark.
					{
						objItem.outerHTML = "";
					}
				}
			}
		}
	}

	if( 0 < szExcessFiles.length )
	{
		var rgDeleted = szExcessFiles.split(" ");
		var i;
		for( i = 0; i < rgDeleted.length; i++ )
		{
			if( 0 < rgDeleted[i].length )
			{
				try
				{
					objEMHttp.DeleteAttachment( szBase + g_arAttachUrl[ rgDeleted[i] ] );
					g_arAttachUrl[ rgDeleted[i] ] = null;
				}
				catch (e)
				{
					if( -2147024894 != e.number )
					{
						alert(e.description);
					}
				} 
			}
		}
	}
}

function SendAttachment(szFileName, szCid)
{
	var rcValue = "";

	if( 0 == szFileName.length  || 0 == szCid.length || 0 == g_bMedia )
	{
		return( rcValue );
	}

	try
	{
		// $REVIEW  need to detect if the content is audio or audio & video.
		objEMHttp.ContentType = "video/x-ms-asf";
		objEMHttp.ContentLocation = szCid;
		rcValue = objEMHttp.PostFile(objMessage.url, szFileName);
	}
	catch (e)
	{
		if( -2147024894 != e.number )
		{
			alert(e.description);
		}
	} 
	return( rcValue );
}

function ctrlMedia_Record()
{
	g_ctrlMedia._fHide = false;
	if( 1 == event.code || 2 == event.code )
	{
		Record( event.code );
	}
	else
	{
		g_ctrlMedia._fHide = false;
	}
	ctrlMedia_positionUpdate();
}

function ctrlMedia_Bookmark(obj)
{
	if( g_ctrlMedia._InsBM )
	{
		g_ctrlMedia._InsBM = false;
		try
		{
			obj.Record();
		}
		catch (e)
		{
			alert(e.description);
		} 
	}
}

function ctrlMedia_readyState()
{
	if( null != event.srcElement.object )
	{
		g_ctrlMedia = event.srcElement;
		g_ctrlMedia._InsBM = false;
		g_ctrlMedia._fHide = true;

		ctrlMedia_AddEventFor( 'command(val)', 'return ctrlMedia_Command(val);' );
		ctrlMedia_AddEventFor( 'LoadBookmark(val)', 'ctrlMedia_Bookmark(val)' );
		ctrlMedia_AddEventFor( 'ResolveFileName(val)', 'return ctrlMedia_ResolveRef(val)' );
		g_ctrlMedia.onresize = ctrlMedia_positionUpdate;

		var objItem = g_ctrlMedia.parentElement;
		if( null != objItem )
		{
			g_szEMgid = g_ctrlMedia.object.GroupID;
			tbContentElement.DOM._EMGroupID = g_szEMgid;
			document.body._EMGroupID = g_szEMgid;

			g_ctrlMedia._minHeight = g_ctrlMedia.object.MinHeight;
			g_ctrlMedia._offset = 0;
			ctrlMedia_positionUpdate();
			objItem.style.display = "";
			g_ctrlMedia.onresize=ctrlMedia_positionUpdate;
			window.onresize=ctrlMedia_positionUpdate;

			if( g_bMedia )
			{
				var objBehave = document.createElement('Object');
				if( objBehave )
				{
					objBehave.id = 'EMConduct';
					objBehave.style.display='none';
					objBehave.tabIndex = -1;
					objBehave.classid='CLSID:D2646502-2424-11D3-9D50-00C04F68AC7D';
					document.body.insertAdjacentElement( 'beforeEnd', objBehave);
				}

				var objDOM = tbContentElement.DOM;

				var objBehave = objDOM.createElement('Object');
				if( objBehave )
				{
					objBehave.id = 'EMConduct';
					objBehave.style.display='none';
					objBehave.tabIndex = -1;
					objBehave.classid='CLSID:D2646502-2424-11D3-9D50-00C04F68AC7D';
					objDOM.all[0].children[0].insertAdjacentElement( 'afterBegin', objBehave);
				}

				var obj = AccessFld(g_winDocAll.multimedia);
				if( obj )
				{
					szHTML = '<SPAN STYLE="behavior:url(#EMConduct#Record);width:50;height:28" OnRecord="ctrlMedia_Record()">';
					obj.innerHTML = szHTML;
					obj.style.display="";
				}

				AddMMObjTags( objDOM );
			}
		}
	}
}

function objEMHttp_readyState(obj)
{
	if( null == obj || null == obj.object )
	{
		g_bMedia = 0;
	}
}

function insertMediaCtrl()
{
	var objBody = document.body;

	try
	{
		var szText =	'<OBJECT ID="objEMHttp" TABINDEX="-1" STYLE="display:none" CLASSID="CLSID:D78736BA-3018-11D3-8006-00C04FB155E5" '+
						' onreadystatechange="objEMHttp_readyState(this)"></OBJECT>';
		objBody.insertAdjacentHTML( "afterBegin", szText );

		szText 	=	'<SPAN id="_spMedia" class="trButtons" STYLE="display:none;position:absolute;z-index:2">'+
						'<OBJECT ID="ctrlMedia" TABINDEX="-1" CLASSID="CLSID:6E767503-024A-11D3-9D47-00C04F68AC7D" '+
						' onreadystatechange="ctrlMedia_readyState()">' +
						'<PARAM NAME="EnableRecord" VALUE="-1">'+
						'<PARAM NAME="ShowHide" VALUE="-1">'+
						'<PARAM NAME="ShowRecord" VALUE="1"></OBJECT></SPAN>';
		objBody.insertAdjacentHTML("afterBegin", szText );
	}
	catch( e )
	{
		g_bMedia = 0;
	}
}

