/*!
**********************************************************************
@file uninstallOptions.js

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

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

/**
Implement a Wizard Page in three easy steps:

 1. Subclass WizardPage
 2. Extend with page-specific method overrides
 3. Return the WizardPage subclass name

*/

/***********************************************************************
1. Subclass WizardPage
*/
function uninstallOptions_wp(inController)
{
	this.SetController(inController);

	// Get our session payloads and order them.
	this.payloads = PayloadDependencySort(this.wizardControl.session.sessionPayloads, true);
}

uninstallOptions_wp.prototype = new WizardPage("uninstallOptions");


/***********************************************************************
2. Extend/override with page-specific logic
*/
uninstallOptions_wp.prototype.GetDisplayName = function()
{
	return this.wizardControl.setupMode == kInstallerModeRemove ? this.localization.GetString("locNavTitle", "Options") : null;
}


/**
Return true if it's remove mode and
there are languages or payloads to choose from, otherwise return false
to bypass selection UI
*/
uninstallOptions_wp.prototype.onSwitchTo = function()
{
	var showMe = false;
			
	if (kInstallerModeRemove == this.wizardControl.setupMode)
	{
		var userPreferences = false;
		var session = this.wizardControl.session;
		session.PayloadPolicyInit(kInstallerModeRemove);

		var featureList = this.getElementById("featureList");
		this.wizardPayloadSelect = new WizardPayloadSelect(this.wizardControl.session, kInstallerActionRemove, featureList, this.errorbox);
		var counts = this.wizardPayloadSelect.Refresh();
		if (counts.uiVisible > 0)
			showMe = true;

		// See if we have any user preferences.
		for (var adobeCode in this.payloads)
		{
			var p = this.payloads[adobeCode];
			var payloadSessionData = p.GetSessionData();
			if (payloadSessionData)
			{			
				if (payloadSessionData.userPreferences &&
				  	(payloadSessionData.userPreferences != "0" || payloadSessionData.userPreferences != 0))
				{
					userPreferences = true;	
					showMe = true;
					break;
				}				
			}
		}
		
		// If we have more than one selectable item, hookup the Select All button.
		if (counts.uiSelectable > 0)
		{
			var selectAllElement = this.getElementById("selectAll");
			if (selectAllElement)
			{
				thisCB = this;
				this.selectAllButton = new WizardButton(selectAllElement);
				this.selectAllButton.button.onclick = function() { thisCB.wizardPayloadSelect.SelectAll(); };
				function setSelectAllLabel(inAllSelected)
				{
					if (inAllSelected)
						thisCB.selectAllButton.SetLabel(thisCB.wizardControl.session.localization.GetString("locDeselectAll", "Deselect All"));
					else
						thisCB.selectAllButton.SetLabel(thisCB.wizardControl.session.localization.GetString("locSelectAll", "Select All"));
				}
				this.wizardPayloadSelect.allSelected.Bind(setSelectAllLabel);
			}
		}
		// Otherwise zap it.
		else
		{
			var nodesToRemove = ["selectAll"]; 
			for (removeNode in nodesToRemove)
			{
				var e = this.getElementById(nodesToRemove[removeNode]);
				if (e)
					e.style.display = "none";
			}
		}

		// Initialize the UserPreferences button.
		if (!userPreferences)
		{
			var uninstallPrefNode = this.getElementById('uninstallPreferences');
			if (uninstallPrefNode)
			{
				var parentNode = uninstallPrefNode.parentNode;
				while (parentNode && parentNode.firstChild)
				{
					parentNode.removeChild(parentNode.firstChild);
				}
			}
			this.wizardControl.session.properties['removeUserPrefs'] = 0;
		}
		else 
		{
			var userPrefElement = this.getElementById('uninstallPreferences');
			if (userPrefElement)
			{
				var checkValue = this.wizardControl.session.properties['removeUserPrefs'];
				var checkState = (checkValue && checkValue != 0) ?  true : false;
				userPrefElement.checked = checkState;				
			}
		}
	}
	
	return (showMe);
}


/**
Setup the Language Selection and Payload Selection widgets.
*/
uninstallOptions_wp.prototype.onResourcesLoaded = function()
{
	var thisCB = this;

	// Add the onClick handler to the user prefs checkbox
	// Set the default
	this.wizardControl.session.properties['removeUserPrefs'] = 1;
	var userPrefElement = this.getElementById('uninstallPreferences');
	userPrefElement.onclick = function() {thisCB.onClickRemoveUserPrefs();};	

	// Bind the Next button to the blocking error count
	if (this.errorbox && this.errorbox.errorCount)
	{
		var nextController = function(errorCount)
		{
			if (errorCount > 0)
				thisCB.wizardControl.nextButton.Disable();
			else
				thisCB.wizardControl.nextButton.Enable();
		}
		this.errorbox.errorCount.Bind(nextController);
	}
}


/**
Block forward action if there isn't at least one visible, selectable payload selected.
*/
uninstallOptions_wp.prototype.onNext = function()
{	
	// Make sure existing errors are clear before moving on to final validation.
	if (this.errorbox.errorCount.Get() > 0)
		return false;

	// Set the driver action based on whether or not all visible items are selected.
	var counts = this.wizardPayloadSelect.Refresh();
	var driverAction = (counts.uiSelected == counts.uiVisible) ? kPolicyActionYes : kPolicyActionNo;
	if (this.wizardControl.session.GetSessionData().driverPayloadID)
	{
		var driver = this.wizardControl.session.sessionPayloads[this.wizardControl.session.GetSessionData().driverPayloadID];
		if (driver && driver.policyNode)
		{
			driver.policyNode.SetAction(driverAction);
		}
	}

	// Do a real install simulation to catch fringe cases that
	// the interactive selection constraints miss.
	this.wizardPayloadSelect.ValidateSelections();
	return this.errorbox.errorCount.Get() == 0;
}

uninstallOptions_wp.prototype.onClickRemoveUserPrefs = function()
{
	var userPrefElement = this.getElementById('uninstallPreferences');
	// Set the global property for removeUserPrefs based on the current state
	var removePrefs = 0;
	if (userPrefElement != null &&
		userPrefElement.checked == true)
	{
		removePrefs = 1;	
	}
	this.wizardControl.session.properties['removeUserPrefs'] = removePrefs;
}

/***********************************************************************
3. Last, and very important, return the name of the class.
*/
"uninstallOptions_wp";
