//=========================================================================================================
//
// Copyright 2002, 2003 Macromedia, Inc. All rights reserved.
//
// Feature: Paste Fix
// Author:  JDH
// Module:  InsertOfficeDoc.js
// Purpose:	Insert Office Document menu item handler.
// Updates:
//	8/1/02 - Started file control
//
//=========================================================================================================

	// The threshold where we should warn the user they are importing a big file

MM.od_WarnThreshold = 100000;

	// The threshold where we should not allow the user to import the file.

MM.od_MaxThreshold = 200000;

var g_file = null;
var g_retVal = null;

function receiveArguments()
{
	g_file = arguments[0];
	g_retVal = arguments[1];
}

function run()
{
 	var insertedAnything = insertOfficeDoc( g_file );
	if (g_retVal)
		g_retVal[0] = insertedAnything;
}

// insertOfficeDoc - Inserts the given file name into the current document.
function insertOfficeDoc( file )
{
	// ensure it's a convertible office doc. if not, punt.
	if (!dw.isOfficeDocument(file, true))
	{
		alert(dw.loadString("insert doc dialog/not office doc error"));
		return false;
	}

	var insertedAnything = false;

	// Switch on Excel or Word.  Use one of those to open up the file, and
	// to save the contents as HTML and return it.

	var usedWord = false;

	var result;
	if ( file.match( /[.]xls/i ) )
	{
		result = dw.excelSaveAsHTML( file );
	}
	else
	{
		result = dw.wordSaveAsHTML( file );
		usedWord = true;
	}

	// Get the text and the base URL    

	var text = result[ 0 ];
	var url = result[ 1 ];

	// Go through a bunch of logic to warn or alert the user to large files,
	// and either insert it or not

	if ( text != null && text.length > 0 )
	{
		if ( text.length < MM.od_WarnThreshold )
		{
			dw.getDocumentDOM().pasteInHTML( text, url );			
			insertedAnything = true;
		}
		else
		{
		    // if we are in headless mode, we do not give more options in case
		    // the file is too big. We only ask if he wants to continue or cancel
		    // we do this as we cannot show a JS based dialog in headless mode
		    var isHeadless = dw.isHeadlessMode();
		    var retVal;		    
		    if(isHeadless == true)		        
		        retVal = confirm(dw.loadString("office intergration/file too big"));
		    else		    
		    {   
		        // 210007- we put up a hidden auto closing dialog, which
		        // seems to reset the window disabler to make the cofirm be a modal dialog
		        dw.OpenAutoCloseHiddenDialog(50);
			    retVal = confirm( MM.MSG_odWarnWithLink ); 			    
			}
			    
			if ( retVal == true )
			{
				dw.getDocumentDOM().pasteInHTML( text, url );
				insertedAnything = true;
			}
			else
			{
			    if(isHeadless == true)
			        return false;
			        
				var dropResult = [ false ];
				/*const*/ var allowInsertContents = false;
				/*const*/ var rememberPrefs = false;
				dw.runCommand("DropOffice2.htm", file, dropResult, allowInsertContents, rememberPrefs);
				insertedAnything = dropResult[0];
			}
		}
		// take out code to stop the office import. Allow users to import as large a doc as 
		// they want. bug #142912 -- jcheng
//		else
	//	{
	///		alert( MM.MSG_odStopWithLink );

	//		var dropResult = [ false ];
			/*const*/ //var allowInsertContents = false;
			/*const*/ //var rememberPrefs = false;
	//		dw.runCommand("DropOffice2.htm", file, dropResult, allowInsertContents, rememberPrefs);
	//		insertedAnything = dropResult[0];
//		}

	}
	else
	{
		if ( usedWord )
		{
			alert( MM.MSG_odWordUnableToSaveAs );
			insertedAnything = true;
		}
		else
		{
			alert( MM.MSG_odExcelUnableToSaveAs );
			insertedAnything = true;
		}
	}	

	return insertedAnything;
}
