/*
Copyright (c) ScanFrame 2008.
All Rights Reserved.

$Source: /export/cvs/primo/com/jscripts/calculator.js,v $
$Author: arjan $
$Revision: 1.6 $

Identification:
  JSCRIPTS

Initial Author:
  AVO 20080909

Purpose:
	Library for using a calculator on an input.

*/

// Called from an input ar ascociated button.
SfLib.CalculatorDialog = function (input, lang)
{
	var self = this;
	//
	this.CalculatorDialogUseFrame = true;
	// Assign the control to assign the result to.
	DialogWin.CalculatorInput = input;
	//
	if (!DialogWin.EvModalResultStoredCalcDlg)
	{
		DialogWin.EvModalResultStoredCalcDlg = DialogWin.EvModalResult;
		// Set the handler for the modal result of the page.
		DialogWin.EvModalResult = function(result, name)
		{
			// Chain the call to the previous handler.
			if (name != 'calculator_dialog')
				return DialogWin.EvModalResultStoredCalcDlg(result, name);
			//
			if (result != null)
			{
				if (result != false)
				{
					// Prevent form some keys to go to the input control.
					DialogWin.CalculatorInput.blur();
					// Assign the value stored by the calculator window.
					DialogWin.CalculatorInput.value = Dialog.CalculatorValue;
				}
				// Make the input control get focus when it lost it.
				DialogWin.CalculatorInput.focus();
			}
			// Is an inpage iframe used.
			if (self.CalculatorDialogUseFrame)
			{
				// Close the window when finishing on the last page.
				// Fore chrome the timeout is needed other wise it reports an error.
				setTimeout(function()	{DialogWin.CloseModalInPage();}, 0);
			}
			//
			return true;
		};
	}
	// Determine what to use for a dialog.
	if (this.CalculatorDialogUseFrame)
	{
		// Open the calculator window.
		var url = SfLib.GetBaseUrl(document.location) +	'jscripts/calculator/calculator_inpagedlg.html';
		return SfLib.OpenDialogModalInPage(url, 'calculator_inpage',  250, 356);
	}
	else
	{
		// Open the calculator window.
		var url = SfLib.GetBaseUrl(document.location) +	'jscripts/calculator/calculator.html';
		//
		return SfLib.OpenDialogModal(url, 'calculator_dialog', 250, 356);
	}
};

