//=========================================================================================================
//
// Copyright 2003 Macromedia, Inc. All rights reserved.
//
// Feature: FlashPaper
// Author:  SRJ
// Module:  FlashPaperOptions.js
// Purpose:	implement the FlashPaper Options dialog
//
//=========================================================================================================

var helpDoc = MM.HELP_flashPaperOptions;

var g_file = null;
var g_returnArray = null;
var g_paperInfo = null;
var g_paperInfoFile = "";	// the file for which g_paperInfo is valid.
var g_curUnits = 0;

// ----------------------------------------
function inchToMM(inch) { return inch * 25.4; }
function mmToInch(mm) { return mm / 25.4; }

// ----------------------------------------
function roundToThousandths(val) 
{
	val *= 1000.0;
	val = Math.round(val);
	val /= 1000.0;
	return val;
}

// ----------------------------------------
/*const*/ var MIN_PAPER_SIZE_MM = 75;	// roughly 3 inches.

// ----------------------------------------
/*const*/ var g_paperSizes =
[
	{ id: 1, width: inchToMM(8.5), height: inchToMM(11) },			/* Letter 8 1/2 x 11 in               */
	{ id: 2, width: inchToMM(8.5), height: inchToMM(11) },			/* Letter Small 8 1/2 x 11 in         */
	{ id: 3, width: inchToMM(11), height: inchToMM(17) },			/* Tabloid 11 x 17 in                 */
	{ id: 4, width: inchToMM(17), height: inchToMM(11) },			/* Ledger 17 x 11 in                  */
	{ id: 5, width: inchToMM(8.5), height: inchToMM(14) },			/* Legal 8 1/2 x 14 in                */
	{ id: 6, width: inchToMM(5.5), height: inchToMM(8.5) },			/* Statement 5 1/2 x 8 1/2 in         */
	{ id: 7, width: inchToMM(7.25), height: inchToMM(10.5) },		/* Executive 7 1/4 x 10 1/2 in        */
	{ id: 8, width: 297, height: 420 },								/* A3 297 x 420 mm                    */
	{ id: 9, width: 210, height: 297 },								/* A4 210 x 297 mm                    */
	{ id: 10, width: 210, height: 297 },							/* A4 Small 210 x 297 mm              */
	{ id: 11, width: 148, height: 210 },							/* A5 148 x 210 mm                    */
	{ id: 12, width: 250, height: 354 },							/* B4 (JIS) 250 x 354                 */
	{ id: 13, width: 182, height: 257 },							/* B5 (JIS) 182 x 257 mm              */
	{ id: 14, width: inchToMM(8.5), height: inchToMM(13) },			/* Folio 8 1/2 x 13 in                */
	{ id: 15, width: 215, height: 275 },							/* Quarto 215 x 275 mm                */
	{ id: 16, width: inchToMM(10), height: inchToMM(14) },			/* 10x14 in                           */
	{ id: 17, width: inchToMM(11), height: inchToMM(17) },			/* 11x17 in                           */
	{ id: 18, width: inchToMM(8.5), height: inchToMM(11) },			/* Note 8 1/2 x 11 in                 */
	{ id: 19, width: inchToMM(3.875), height: inchToMM(8.875) },	/* Envelope #9 3 7/8 x 8 7/8          */
	{ id: 20, width: inchToMM(4.125), height: inchToMM(9.5) },		/* Envelope #10 4 1/8 x 9 1/2         */
	{ id: 21, width: inchToMM(4.5), height: inchToMM(10.375) },		/* Envelope #11 4 1/2 x 10 3/8        */
	{ id: 22, width: inchToMM(4.75), height: inchToMM(11) },		/* Envelope #12 4 3/4  x 11           */
	{ id: 23, width: inchToMM(5), height: inchToMM(11.5) },			/* Envelope #14 5 x 11 1/2            */
	{ id: 24, width: inchToMM(17), height: inchToMM(22) },			/* C size sheet                       */
	{ id: 25, width: inchToMM(22), height: inchToMM(34) },			/* D size sheet                       */
	{ id: 26, width: inchToMM(34), height: inchToMM(44) },			/* E size sheet                       */
	{ id: 27, width: 110, height: 220 },							/* Envelope DL 110 x 220 mm           */
	{ id: 28, width: 162, height: 229 },							/* Envelope C5 162 x 229 mm           */
	{ id: 29, width: 324, height: 458 },							/* Envelope C3  324 x 458 mm          */
	{ id: 30, width: 229, height: 324 },							/* Envelope C4  229 x 324 mm          */
	{ id: 31, width: 114, height: 162 },							/* Envelope C6  114 x 162 mm          */
	{ id: 32, width: 114, height: 229 },							/* Envelope C65 114 x 229 mm          */
	{ id: 33, width: 250, height: 353 },							/* Envelope B4  250 x 353 mm          */
	{ id: 34, width: 176, height: 250 },							/* Envelope B5  176 x 250 mm          */
	{ id: 35, width: 176, height: 125 },							/* Envelope B6  176 x 125 mm          */
	{ id: 36, width: 110, height: 230 },							/* Envelope 110 x 230 mm              */
	{ id: 37, width: inchToMM(3.875), height: inchToMM(7.5) },		/* Envelope Monarch 3.875 x 7.5 in    */
	{ id: 38, width: inchToMM(3.625), height: inchToMM(6.5) },		/* 6 3/4 Envelope 3 5/8 x 6 1/2 in    */
	{ id: 39, width: inchToMM(14.875), height: inchToMM(11) },		/* US Std Fanfold 14 7/8 x 11 in      */
	{ id: 40, width: inchToMM(8.5), height: inchToMM(12) },			/* German Std Fanfold 8 1/2 x 12 in   */
	{ id: 41, width: inchToMM(8.5), height: inchToMM(13) },			/* German Legal Fanfold 8 1/2 x 13 in */
	{ id: 42, width: 250, height: 353 },							/* B4 (ISO) 250 x 353 mm              */
	{ id: 43, width: 100, height: 148 },							/* Japanese Postcard 100 x 148 mm     */
	{ id: 44, width: inchToMM(9), height: inchToMM(11) },			/* 9 x 11 in                          */
	{ id: 45, width: inchToMM(10), height: inchToMM(11) },			/* 10 x 11 in                         */
	{ id: 46, width: inchToMM(15), height: inchToMM(11) },			/* 15 x 11 in                         */
	{ id: 47, width: 220, height: 220 },							/* Envelope Invite 220 x 220 mm       */
	//{ id: 48, width: 1, height: 1 },								** RESERVED--DO NOT USE               */
	//{ id: 49, width: 1, height: 1 },								** RESERVED--DO NOT USE               */
	{ id: 50, width: inchToMM(9.5), height: inchToMM(12) },			/* Letter Extra 9.5 x 12 in			  */
	{ id: 51, width: inchToMM(9.5), height: inchToMM(15) },			/* Legal Extra 9.5 x 15 in            */
	{ id: 52, width: inchToMM(11.69), height: inchToMM(18) },		/* Tabloid Extra 11.69 x 18 in        */
	{ id: 53, width: inchToMM(9.27), height: inchToMM(12.69) },		/* A4 Extra 9.27 x 12.69 in           */
	{ id: 54, width: inchToMM(8.5), height: inchToMM(11) },			/* Letter Transverse 8.5 x 11 in      */
	{ id: 55, width: 210, height: 297 },							/* A4 Transverse 210 x 297 mm         */
	{ id: 56, width: inchToMM(9.5), height: inchToMM(12) },			/* Letter Extra Transverse 9.5 x 12 in*/
	{ id: 57, width: 227, height: 356 },							/* SuperA/SuperA/A4 227 x 356 mm      */
	{ id: 58, width: 305, height: 487 },							/* SuperB/SuperB/A3 305 x 487 mm      */
	{ id: 59, width: inchToMM(8.5), height: inchToMM(12.69) },		/* Letter Plus 8.5 x 12.69 in         */
	{ id: 60, width: 210, height: 330 },							/* A4 Plus 210 x 330 mm               */
	{ id: 61, width: 148, height: 210 },							/* A5 Transverse 148 x 210 mm         */
	{ id: 62, width: 182, height: 257 },							/* B5 (JIS) Transverse 182 x 257 mm   */
	{ id: 63, width: 322, height: 445 },							/* A3 Extra 322 x 445 mm              */
	{ id: 64, width: 174, height: 235 },							/* A5 Extra 174 x 235 mm              */
	{ id: 65, width: 201, height: 276 },							/* B5 (ISO) Extra 201 x 276 mm        */
	{ id: 66, width: 420, height: 594 },							/* A2 420 x 594 mm                    */
	{ id: 67, width: 297, height: 420 },							/* A3 Transverse 297 x 420 mm         */
	{ id: 68, width: 322, height: 445 },							/* A3 Extra Transverse 322 x 445 mm   */
	{ id: 69, width: 200, height: 148 },							/* Japanese Double Postcard 200 x 148 mm */
	{ id: 70, width: 105, height: 148 },							/* A6 105 x 148 mm                 */
	{ id: 71, width: 240, height: 332 },							/* Japanese Envelope Kaku #2       */
	{ id: 72, width: 216, height: 277 },							/* Japanese Envelope Kaku #3       */
	{ id: 73, width: 120, height: 235 },							/* Japanese Envelope Chou #3       */
	{ id: 74, width: 90, height: 205 },								/* Japanese Envelope Chou #4       */
	{ id: 75, width: inchToMM(11), height: inchToMM(8) },			/* Letter Rotated 11 x 8 1/2 11 in */
	{ id: 76, width: 420, height: 297 },							/* A3 Rotated 420 x 297 mm         */
	{ id: 77, width: 297, height: 210 },							/* A4 Rotated 297 x 210 mm         */
	{ id: 78, width: 210, height: 148 },							/* A5 Rotated 210 x 148 mm         */
	{ id: 79, width: 364, height: 257 },							/* B4 (JIS) Rotated 364 x 257 mm   */
	{ id: 80, width: 257, height: 182 },							/* B5 (JIS) Rotated 257 x 182 mm   */
	{ id: 81, width: 148, height: 100 },							/* Japanese Postcard Rotated 148 x 100 mm */
	{ id: 82, width: 148, height: 200 },							/* Double Japanese Postcard Rotated 148 x 200 mm */
	{ id: 83, width: 148, height: 105 },							/* A6 Rotated 148 x 105 mm         */
	{ id: 84, width: 332, height: 240 },							/* Japanese Envelope Kaku #2 Rotated */
	{ id: 85, width: 277, height: 216 },							/* Japanese Envelope Kaku #3 Rotated */
	{ id: 86, width: 235, height: 120 },							/* Japanese Envelope Chou #3 Rotated */
	{ id: 87, width: 205, height: 90 },								/* Japanese Envelope Chou #4 Rotated */
	{ id: 88, width: 128, height: 182 },							/* B6 (JIS) 128 x 182 mm           */
	{ id: 89, width: 182, height: 128 },							/* B6 (JIS) Rotated 182 x 128 mm   */
	{ id: 90, width: inchToMM(12), height: inchToMM(11) },			/* 12 x 11 in                      */
	{ id: 91, width: 105, height: 235 },							/* Japanese Envelope You #4        */
	{ id: 92, width: 235, height: 105 },							/* Japanese Envelope You #4 Rotated*/
	{ id: 93, width: 146, height: 215 },							/* PRC 16K 146 x 215 mm            */
	{ id: 94, width: 97, height: 151 },								/* PRC 32K 97 x 151 mm             */
	{ id: 95, width: 97, height: 151 },								/* PRC 32K(Big) 97 x 151 mm        */
	{ id: 96, width: 102, height: 165 },							/* PRC Envelope #1 102 x 165 mm    */
	{ id: 97, width: 102, height: 176 },							/* PRC Envelope #2 102 x 176 mm    */
	{ id: 98, width: 125, height: 176 },							/* PRC Envelope #3 125 x 176 mm    */
	{ id: 99, width: 110, height: 208 },							/* PRC Envelope #4 110 x 208 mm    */
	{ id: 100, width: 110, height: 220 },							/* PRC Envelope #5 110 x 220 mm    */
	{ id: 101, width: 120, height: 230 },							/* PRC Envelope #6 120 x 230 mm    */
	{ id: 102, width: 160, height: 230 },							/* PRC Envelope #7 160 x 230 mm    */
	{ id: 103, width: 120, height: 309 },							/* PRC Envelope #8 120 x 309 mm    */
	{ id: 104, width: 229, height: 324 },							/* PRC Envelope #9 229 x 324 mm    */
	{ id: 105, width: 324, height: 458 },							/* PRC Envelope #10 324 x 458 mm   */
	{ id: 106, width: 215, height: 146 },							/* PRC 16K Rotated                 */
	{ id: 107, width: 151, height: 97 },							/* PRC 32K Rotated                 */
	{ id: 108, width: 151, height: 97 },							/* PRC 32K(Big) Rotated            */
	{ id: 109, width: 165, height: 102 },							/* PRC Envelope #1 Rotated 165 x 102 mm */
	{ id: 110, width: 176, height: 102 },							/* PRC Envelope #2 Rotated 176 x 102 mm */
	{ id: 111, width: 176, height: 125 },							/* PRC Envelope #3 Rotated 176 x 125 mm */
	{ id: 112, width: 208, height: 110 },							/* PRC Envelope #4 Rotated 208 x 110 mm */
	{ id: 113, width: 220, height: 110 },							/* PRC Envelope #5 Rotated 220 x 110 mm */
	{ id: 114, width: 230, height: 120 },							/* PRC Envelope #6 Rotated 230 x 120 mm */
	{ id: 115, width: 230, height: 160 },							/* PRC Envelope #7 Rotated 230 x 160 mm */
	{ id: 116, width: 309, height: 120 },							/* PRC Envelope #8 Rotated 309 x 120 mm */
	{ id: 117, width: 324, height: 229 },							/* PRC Envelope #9 Rotated 324 x 229 mm */
	{ id: 118, width: 458, height: 324 }							/* PRC Envelope #10 Rotated 458 x 324 mm */
];

// ----------------------------------------
function receiveArguments()
{
	g_file = arguments[0];
	g_returnArray = arguments[1];

	// only reset if it's a different file.
	if (g_file != g_paperInfoFile)
	{
		g_paperInfo = null;
		g_paperInfoFile = "";
	}

	/*const*/ var warnUserIfNotAvailable = true;
	if (!isFlashPaperAvailable(warnUserIfNotAvailable))
	{
 		return "abort"; 
	}

	var isTextOnlyMode = dw.getDocumentDOM().getCCSharedSetting_TextOnlyInNonTemplates();

	if (isTextOnlyMode)
	{
		// doh. can't insert. punt.
		alert(dw.loadString("page props/edit text only"));
 		return "abort"; 
	}

	// if an office doc, suck out the settings and use those...
	// (but only if we don't already have settings!)
	if (g_file != null && g_file != '' && g_paperInfo == null)
	{
		var officePaperInfo = dw.getOfficePrintInfo(g_file);
		// ...but only if we can get such settings. (the above call currently always
		// returns null for PPT files.)
		if (officePaperInfo != null)
		{
			g_paperInfo = officePaperInfo;
			g_paperInfoFile = g_file;
		}
	}

	// otherwise, get the current settings from FlashPaperConnect and use those
	if (g_paperInfo == null)
	{
		g_paperInfo = FlashPaperConnect.getCurrentPaperSettings();
		g_paperInfoFile = g_file;
	}

	initializeUI();
}

// ----------------------------------------
function commandButtons()
{
	return new Array( "PutButtonsOnBottom", 
					  "OkButton",			MM.BTN_OK,		"if (checkValid()) { window.close(); run(); }",
				      "CancelButton",		MM.BTN_Cancel,	"window.close();",
					  "PutButtonOnLeft",	MM.BTN_Help,	"displayHelp();");
}

// ----------------------------------------
function initializeUI()
{
	var orient = 0;
	var paperSize = 1;
	var width = inchToMM(4);
	var height = inchToMM(6);

	if (g_paperInfo != null)
	{
		orient = g_paperInfo[0];
		paperSize = g_paperInfo[1];
		width = g_paperInfo[2];
		height = g_paperInfo[3];
	}

	g_curUnits = Number(dw.getPreferenceString("General Preferences", "FlashPaper Units", 0));
	document.theForm.customunits.selectedIndex = g_curUnits;

	if (orient == 0)
	{
		document.theForm.orientation[0].checked = true;
		document.theForm.orientation[0].focus();
	}
	else
	{
		document.theForm.orientation[1].checked = true;
		document.theForm.orientation[1].focus();
	}

	var found = false;
	for (var i = 0; i < document.theForm.papersizelist.options.length; ++i)
	{
		if (document.theForm.papersizelist.options[i].value == paperSize)
		{
			setStandardPaperSize();
			document.theForm.papersizelist.selectedIndex = i;
			found = true;
			break;
		}
	}
	if (!found)
	{
		// could be a "standard" size that isn't in our UI.
		// try looking it up.
		if (width == 0 || height == 0)
		{
			for (var i = 0; i < g_paperSizes.length; ++i)
			{
				if (g_paperSizes[i].id == paperSize)
				{
					width = g_paperSizes[i].width;
					height = g_paperSizes[i].height;
					break;
				}
			}
		}

		document.theForm.papersizelist.selectedIndex = 0;
		if (document.theForm.customunits.selectedIndex == 0)
		{
			width = mmToInch(width);
			height = mmToInch(height);
		}
		document.theForm.customwidth.value = roundToThousandths(width);
		document.theForm.customheight.value = roundToThousandths(height);
		setCustomPaperSize();
	}

	var officeAddInIncludeOutlinePreferenceString = dw.getPreferenceString("General Preferences", "FlashPaper OfficeAddInIncludeOutline", "true");
	if (officeAddInIncludeOutlinePreferenceString.toLowerCase() == "true")
	{
		document.theForm.outline.checked = true;
	}
	else
	{
		document.theForm.outline.checked = false;
	}
}

// ----------------------------------------
function setStandardPaperSize()
{
	document.theForm.papersize[0].checked = true;

	document.theForm.papersizelist.disabled = false;

	document.theForm.customwidth.disabled = true;
	document.theForm.customheight.disabled = true;
	document.theForm.customunits.disabled = true;
}

// ----------------------------------------
function setCustomPaperSize()
{
	document.theForm.papersize[1].checked = true;

	document.theForm.papersizelist.disabled = true;

	document.theForm.customwidth.disabled = false;
	document.theForm.customheight.disabled = false;
	document.theForm.customunits.disabled = false;
}

// ----------------------------------------
function onUnitsChanged()
{
	if (g_curUnits != document.theForm.customunits.selectedIndex)
	{
		var width = document.theForm.customwidth.value;
		var height = document.theForm.customheight.value;
		if (g_curUnits == 0)
		{
			// was inch, now mm
			width = inchToMM(width);
			height = inchToMM(height);
			if (width < MIN_PAPER_SIZE_MM || isNaN(width)) 
				width = MIN_PAPER_SIZE_MM;
			if (height < MIN_PAPER_SIZE_MM || isNaN(height)) 
				height = MIN_PAPER_SIZE_MM;
		}
		else
		{
			// was mm, now inch
			if (width < MIN_PAPER_SIZE_MM || isNaN(width)) 
				width = MIN_PAPER_SIZE_MM;
			if (height < MIN_PAPER_SIZE_MM || isNaN(height)) 
				height = MIN_PAPER_SIZE_MM;
			width = mmToInch(width);
			height = mmToInch(height);
		}

		document.theForm.customwidth.value = roundToThousandths(width);
		document.theForm.customheight.value = roundToThousandths(height);
		g_curUnits = document.theForm.customunits.selectedIndex;
	}
}

// ----------------------------------------
function onPaperSizeChanged()
{
	if (document.theForm.papersize[0].checked)
	{
		setStandardPaperSize();
	}
	else
	{
		setCustomPaperSize();
	}
}

// ----------------------------------------
function onFlashPaperLinkClicked()
{
	dwscripts.displayDWHelp(MM.HELP_aboutFlashPaper);
}

// ----------------------------------------
function checkValid()
{
	if (!document.theForm.papersize[0].checked)
	{
		width = document.theForm.customwidth.value;
		height = document.theForm.customheight.value;
		if (document.theForm.customunits.selectedIndex == 0)
		{
			width = inchToMM(width);
			height = inchToMM(height);
		}
		if (width < MIN_PAPER_SIZE_MM || isNaN(width) || 
			height < MIN_PAPER_SIZE_MM || isNaN(height)) 
		{
			alert(BAD_PAPERSIZE_ENTERED);
			return false;
		}
	}

	return true;
}

// ----------------------------------------
function run()
{
	if (typeof(FlashPaperConnect) == "undefined" || FlashPaperConnect.isAvailable() != 0)
	{
		return;
	}

	var orient;
	if (document.theForm.orientation[0].checked)
		orient = 0;
	else
		orient = 1;

	var paperSize = 256;	// user-defined
	var width = 1;
	var height = 1;
	if (document.theForm.papersize[0].checked)
	{

		var sel = document.theForm.papersizelist.selectedIndex;
		if (sel < 0) sel = 0;
		paperSize = document.theForm.papersizelist.options[sel].value;
		for (var i = 0; i < g_paperSizes.length; ++i)
		{
			if (g_paperSizes[i].id == paperSize)
			{
				width = g_paperSizes[i].width;
				height = g_paperSizes[i].height;
				break;
			}
		}
	}
	else
	{
		width = document.theForm.customwidth.value;
		height = document.theForm.customheight.value;
		if (document.theForm.customunits.selectedIndex == 0)
		{
			width = inchToMM(width);
			height = inchToMM(height);
		}
	}

	// many apps get very unhappy with tiny paper sizes.
	// constrain to a plausible size.
	if (width < MIN_PAPER_SIZE_MM || isNaN(width)) 
		width = MIN_PAPER_SIZE_MM;
	if (height < MIN_PAPER_SIZE_MM || isNaN(height)) 
		height = MIN_PAPER_SIZE_MM;

	dw.setPreferenceString("General Preferences", "FlashPaper Units", document.theForm.customunits.selectedIndex.toString());
	dw.setPreferenceString("General Preferences", "FlashPaper OfficeAddInIncludeOutline", document.theForm.outline.checked.toString().toUpperCase());

	if (g_returnArray)
	{
		// 'portrait' is always narrower than it is tall,
		// 'landscape' is the opposite.
		if (width > height)
		{
			var tmp = width;
			width = height;
			height = tmp;
		}
		if (orient == 0)
			g_returnArray[0] = [ roundToThousandths(width), roundToThousandths(height) ];
		else
			g_returnArray[0] = [ roundToThousandths(height), roundToThousandths(width) ];
	}

	if (g_paperInfo != null)
	{
		// save the selection(s) in case we return to this dialog.
		g_paperInfo[0] = orient;
		g_paperInfo[1] = paperSize;
		g_paperInfo[2] = width;
		g_paperInfo[3] = height;
	}
}
