/*!
**********************************************************************
@file WizardPage.js

Copyright 2003-2006 Adobe Systems Incorporated.                     
All Rights Reserved.                                                
                                                                    
NOTICE: All information contained herein is the property of Adobe   
Systems Incorporated.                                                                                                                    

***********************************************************************
*/

/**
WizardPage constructor.
The constructor does NOT automatically load the page.  See the LoadRequest() method.
@param		inPageName		Base name of the page, matching the filesystem name.
*/
function WizardPage(inPageName)
{
	/** This unique wizard page name */
	this.pageName = inPageName;
	/** Localization object */
	this.localization = null;
	/** The contents of the BODY tag in the imported documentRoot.  Valid iff document can be successfully loaded */
	this.importedBodyFragment = null;
	/** The WizdardControl owning this page instance */
	this.wizardControl = null;
	/** The alert.html template stream */
	this.alertTemplate = null;
	/** The alert.xml resource map */
	this.alertResourceMap = null;
}


WizardPage.prototype.SetController = function(inController)
{
	this.wizardControl = inController;
}


/**
Return a localized string representing the user interface name
for this workflow step.  This will be displayed in the title bar of the user interface
*/
WizardPage.prototype.GetPageTitleDisplayName = function()
{
	return "Wizard Page";
}

/**
Return a localized string representing the user interface name
for this workflow step.  This will be displayed in the navigation status area
*/
WizardPage.prototype.GetNavDisplayName = function()
{
	return "Wizard Page";
}


/**
Initiate the loading of the wizard.
*/
WizardPage.prototype.LoadResources = function()
{
	var workflowMode=this.wizardControl.session.workflowMode;
	var htmlPath = _concatPaths([this.wizardControl.session.GetResourcesPath(), 'pages/' + workflowMode + "/" + this.pageName + '/' + this.pageName + '.html'],
 		this.wizardControl.session.properties["platform"]);
	var htmlResource = this.wizardControl.session.LoadFile(htmlPath);
	if (htmlResource && htmlResource.data)
	{
		/*
		Transit the wizard page HTML into a node owned by the Ahmbed container
		document.  Then we can manage its visibility by simply calling appending
		and removing it.		
		*/
		this.wizardControl.session.LogDebug("HTML data complete: " + this.pageName);
		this.importedBodyFragment = document.createElement("div");
		this.importedBodyFragment.innerHTML = htmlResource.data;

		/*
		Load the localization data and localize the page DOM.
		*/
		var xmlPath = _concatPaths([this.wizardControl.session.GetResourcesPath(), 'pages/' + workflowMode + "/" + this.pageName + '/' + this.pageName + '.xml'],
	 		this.wizardControl.session.properties["platform"]);
		this.localization = new Localization(this.wizardControl.session, xmlPath, this.wizardControl.session.properties, false);
		if (this.localization)
			this.localization.LocalizeDOM(this.importedBodyFragment);
		else
			this.wizardControl.session.LogError("Localizing wizard page failed: " + this.pageName);

		/*
		Make a WizardError if there is an error box in the UI.
		*/
		var errorElement = this.getElementById("error");
		if (errorElement)
		{
			this.errorbox = new WizardError(errorElement, this.getElementById("content"), this.wizardControl.textColor);
		}

		/*
		Call the page method to let it know the UI is available.
		*/
		this.onResourcesLoaded(); 
	}
	else
	{
		this.wizardControl.session.LogError("Error loading HTML data for: " + this.pageName);
	}
}


/**
Return true if the wizard page wants to be included in the
workflow.  If this method returns false, the WizardControl
will have no further interaction with this object.

A page loaded is not necessairly valid.  See isValid() method.
*/
WizardPage.prototype.isActive = function()
{
	return true;
}


/**
Return the root node of the wizard page DOM.

The DOM may or may not be attached in the node hieararchy.
*/
WizardPage.prototype.GetUIDOM = function()
{
	return this.importedBodyFragment;
}

/**
Removes all navigation buttons from the UI
*/
WizardPage.prototype.ClearButtonArea = function()
{
	var buttonarea = document.getElementById("navButtons");
	while (buttonarea.hasChildNodes())
		buttonarea.removeChild(buttonarea.lastChild);
}

/**
Populate the nav button area. Must be overridden by each pages specific buttons
*/

WizardPage.prototype.FillButtonArea = function()
{
}

WizardPage.prototype.RenderInitialWizardImagesOfPage = function()
{
}

/**
Default getProperties delegate implementation

Pack up input, select and textarea element values as a property map.
Elements must have a name attribute to be included.
*/
WizardPage.prototype.getProperties = function()
{
	var DOM = this.GetUIDOM();
	if (!DOM)
	{
		alert("WizardPage.getProperties(): Can't find the DOM!");
		return null;
	}
	
	pmap = new Object();
	fields = [ "input", "select", "textarea" ];
	for (var fi = 0; fi < fields.length; fi++)
	{
		var nodes = DOM.getElementsByTagName(fields[fi]);
		for (var ni = 0; ni < nodes.length; ni++)
		{
			var node = nodes[ni];
			if (node.name)
			{
				switch (node.type)
				{
					case "radio":
						if (node.value && node.checked)
							pmap[node.name] = node.value;
						break;
					case "checkbox":
						if (node.checked)
							pmap[node.name] = node.value;
						break;
					case "button":
						break;
					default:
						if (node.value)
							pmap[node.name] = node.value;
				}
			}
		}
	}
	return pmap;
}


/**
Get an element based on id.

document.getElementId() doesn't work when the page isn't attached
into the document tree.  This does the same sort of thing, but just
returns the first element that has a matching id attribute.
*/
WizardPage.prototype.getElementById = function(inId)
{
	var idMatch = function(node, idmatch)
	{
		if (node)
		{
			if (1 == node.nodeType && node.id && idmatch == node.id)
			{
				return node;
			}
			else
			{
				for (var n = 0; n < node.childNodes.length; n++)
				{
					var cn = idMatch(node.childNodes[n], idmatch);
					if (cn)
						return cn;
				}
			}
		}
		return null;
	};

	return idMatch(this.GetUIDOM(), inId);

}


/**
Page defined handler for when the page resources are loaded.
The HTML and XML UI resources are not available before this
method is called.
*/
WizardPage.prototype.onResourcesLoaded = function()
{
	return true;
}


/**
Page defined onSwitchTo handler.

If a page wants to opt out of the workflow for any reason, it
should define an onSwitchTo handler that returns true under
the conditions the page should be shown and false
under the conditions the page should be skipped.

By default this simply calls the isActive() method.  Override
if the participation cannot be determined at load time.
*/
WizardPage.prototype.onSwitchTo = function()
{
	return this.isActive();
}


/**
Page defined onNext handler.

If a page wants to block the switch to the next page, it should define
an onNext handler that returns false under the conditions that the
switch should be blocked.
*/
WizardPage.prototype.onNext = function()
{
	return true;
}


/**
Page defined onBack handler.

If a page wants to block the switch to the previous page, it should define
an onBack handler that returns false under the conditions that the
switch should be blocked.
*/
WizardPage.prototype.onBack = function()
{
	return true;
}


/**
Page defined onShow handler.

If a page wants to invoke code as soon as the page is shown to the user,
override this method.
*/
WizardPage.prototype.onShow = function()
{
	this.setWindowTitle();
	this.wizardControl.session.UISetCloseBoxEnabled(1);
	
	this.wizardControl.focusItem = null;

	return true;
}

/**
Set the window title based on this page's title
*/
WizardPage.prototype.setWindowTitle = function()
{
	var workflowMode=this.wizardControl.session.workflowMode;
	if (workflowMode == kWorkflowModeUninstall)
    	this.wizardControl.session.UISetWindowTitle(this.wizardControl.session.localization.GetString("locProductNameUninstall", "[productName] - [pageTitle]", { pageTitle: this.GetPageTitleDisplayName()}));
    else
    	this.wizardControl.session.UISetWindowTitle(this.wizardControl.session.localization.GetString("locProductNameInstall", "[productName] Install - [pageTitle]", { pageTitle: this.GetPageTitleDisplayName()}));
}


/**
Page defined onCancel handler.

If a page wants to block or otherwise hook into action when
a user clicks the Quit/Cancel button, override this method.
Return false to block exiting the installer.
*/
WizardPage.prototype.onCancel = function()
{
	// Setup the HTML body text for the alert...
	var alertObject = null;
	var resultDialog = null;
	var doCancel = false;
	
	try
	{
		//var alertObj = new WizardAlert(this.wizardControl.session);
		//alertObj.SetTitle(this.wizardControl.session.localization.GetString("locAlertCancelTitle", "Cancel Installation"));
		var bodyText = "";
		bodyText += this.wizardControl.session.localization.GetString("locAlertCancelBody",
				"<p>Click Resume to continue this Installing.</p>");
		//alertObj.AddButton(this.wizardControl.session.localization.GetString("locAlertCancelResume", "Resume"), "2", { accessKey: "r" } );
		//alertObj.AddButton(this.wizardControl.session.localization.GetString("locAlertCancelQuit", "Quit"), "1", { accessKey: "q" } );

		//resultDialog = alertObj.Run();
		//doCancel = (resultDialog.returnValue == "1");
		
		//WizardAlert1(inSession, inTitle, inBody, buttonsArray, subTitle)
		var button1 = {label:this.wizardControl.session.localization.GetString("locAlertCancelQuit", "Quit"), left:"187px", top:"106px", returnCode:"1", hotkey:"Q", defaultOption:"0"};
		var button2 = {label:this.wizardControl.session.localization.GetString("locAlertCancelResume", "Resume"), left:"325px", top:"106px", returnCode:"2", hotkey:"R", defaultOption:"7"};
		var buttonsArray = new Array(button1,button2);
		var extraOptions = {bNoPrefixProductName:true};
		resultDialog = new WizardAlert1(this.wizardControl.session,
				this.wizardControl.session.localization.GetString("locAlertCancelTitle", "[productName] Installer"),
				bodyText, buttonsArray,
				this.wizardControl.session.localization.GetString("locAlertCancelSubTitle", "Are you sure you want to quit the installer?"),
				extraOptions);
		doCancel = (resultDialog.returnValue == "1");
	}
	catch (ex)
	{
	}
	finally
	{
		//alertObject = null;
		resultDialog = null;
	}
	return doCancel;
}

/**
Makes one element out of an array of elements visible on the page, hides all others
*/
WizardPage.prototype.makeElementVisibleInList = function(elementIDToShow, allIDSArray)
{
	for(var anID in allIDSArray)
	{
		if(allIDSArray[anID]==elementIDToShow)
		{
		    if(this.getElementById(allIDSArray[anID]))
			    this.getElementById(allIDSArray[anID]).style.display="block";
		}
		else
		{
		    if(this.getElementById(allIDSArray[anID]))
			    this.getElementById(allIDSArray[anID]).style.display="none";
		}
	}
}


////////////////////////////////////////////////////////////////////////////
// Dead Code section - To be removed if not required anymore
// This section contains functions that are not referenced anywhere
////////////////////////////////////////////////////////////////////////////


/**
Page defined onSetupModeChanged handler.

If a page wants to invoke code when the setup mode has changed, it should
override this method.
*/
WizardPage.prototype.onSetupModeChanged = function(inOldMode, inNewMode)
{
	return;
}

/*
Extend This Function For Each WizardPage As Required, For Example, Welcome!
G
*/
WizardPage.prototype.ConfigureAutoReverseRTL = function()
{
	/* For html elements which need special handling for rtl, define the behaviour here by a functional attribute called inElement.specialrtlhandling*/
	//_BEGIN_TEMPLATE_CODING
	var el1=document.getElementById("_global_variable_example");
	if(el1)
		el1.specialrtlhandling = function()
								{
									var e=this;//element el1
									//proceed to write your own code now!
								};
}

/*
Reverse Everything Under "container" including "contentwrap" except the children of "contentwrap" once :: Done By AutoReverseRTLOnce();
Reverse the children of "contentwrap" everytime :: Done By AutoReverseRTL();
GENERAL PRINCIPLE
Any Element Which Is Dynamically Added To Screen Or Position Changed After OnShow() should call ForceElementAutoReverseRTL();
*/

/*
Reverse Everything Under "container" including "contentwrap" except the children of "contentwrap" once :: Done By AutoReverseRTLOnce();
G
*/
WizardPage.prototype.AutoReverseRTLOnce = function()
{
	if(gSession.maindomrev)
		return;
	else
	{
		gSession.maindomrev=true;
		//EXTENDABLE TO WIZARDS ALL, INCLUDING VIRTUO COMPONENTO
		if(gSession.languagertl)
		{
			var celement = document.getElementById("contentwrap");
			celement.ignorechildren=true;
			document.getElementById("container").ignore=true;
			WizardReverseDisplay1(document.getElementById("container"));
		}
		//EXTENDABLE TO WIZARDS ALL, INCLUDING VIRTUO COMPONENTO
	}
};

/*
Reverse the children of "contentwrap" everytime :: Done By AutoReverseRTL();
G
To Be Called In Every WizardPages onShow();
G
*/
WizardPage.prototype.AutoReverseRTL = function()
{
	//EXTENDABLE TO WIZARDS ALL, INCLUDING VIRTUO COMPONENTO
	if(gSession.languagertl && !(gSession.reversedpage[this.pageName]))
	{
		var celement = document.getElementById("contentwrap");
		celement.ignorechildren=false;
		for(var i=0;i<celement.children.length;i++)
		{
			WizardReverseDisplay1(celement.children[i]);
		}
		gSession.reversedpage[this.pageName]=true;
	}
	//EXTENDABLE TO WIZARDS ALL, INCLUDING VIRTUO COMPONENTO
};

/*
Any Element Which Is Dynamically Added To Screen Or Position Changed After OnShow() should call ForceElementAutoReverseRTL();
G
*/
WizardPage.prototype.ForceElementAutoReverseRTL = function(inElement)
{
	//EXTENDABLE TO WIZARDS ALL, INCLUDING VIRTUO COMPONENTO
	if(gSession.languagertl)
	{
		WizardReverseDisplay1(inElement);
	}
	//EXTENDABLE TO WIZARDS ALL, INCLUDING VIRTUO COMPONENTO
};

WizardPage.prototype.getDefaultElementForEnterKey = function()
{
	return null;
}

WizardPage.prototype.getDefaultElementForEscapeKey = function()
{
	return null;
}