function ActionChoice(dossierUpdater)
{
	this.dossierUpdater = dossierUpdater;
	
	this.divCantMoveSupplier = document.getElementById("divCantMoveSupplier");
	this.divCantMoveProduct = document.getElementById("divCantMoveProduct");
	this.divNoChoice = document.getElementById("divNoChoice");
	this.divActionChoice = document.getElementById("divActionChoice");

	this.tdCreateNewContract = document.getElementById("tdCreateNewContract");
	this.tblSwitch = document.getElementById("tblSwitch");
	this.tdMoveContract = document.getElementById("tdMoveContract");
	this.tdEndContract = document.getElementById("tdEndContract");
	this.tdDoNothing = document.getElementById("tdDoNothing");
	
	var radioButtons = document.forms[0].radAction;
	
	for(var i=0; i<radioButtons.length; i++)
	{
		radioButtons[i].actionChoice = this;
		radioButtons[i].onclick = function() { this.actionChoice.choose(this); };
	}
	
	document.forms[0].chkSwitch.actionChoice = this;
	document.forms[0].chkSwitch.onclick = function() { this.actionChoice.choose(this); };
	
	this.action = "";
	
	this.onchoose = null;
}

ActionChoice.prototype.showPossibilities = function(possibilities, product)
{
	// By default, hide all the divs
	this.divCantMoveSupplier.style.display = "none";
	this.divCantMoveProduct.style.display = "none";
	this.divNoChoice.style.display = "none";
	this.divActionChoice.style.display = "none";
	
	// Only show the possibilities if they are known.
	if(possibilities != null)
	{
		if(!possibilities.canCreateNewContract && !possibilities.canSwitch && !possibilities.canMoveContract && !possibilities.canEndContract)
		{
			this.divNoChoice.style.display = "block";
		}
		else
		{
			this.divActionChoice.style.display = "block";
			
			this.tdCreateNewContract.style.display = possibilities.canCreateNewContract ? "block" : "none";
			this.tblSwitch.style.display = possibilities.canSwitch ? "block" : "none";
			this.tdMoveContract.style.display = possibilities.canMoveContract ? "block" : "none";
			this.tdEndContract.style.display = possibilities.canEndContract ? "block" : "none";
			this.tdDoNothing.style.display = possibilities.canDoNothing ? "block" : "none";
			
			if(product != null)
			{
				// Set the supplier and product labels
				document.getElementById("spanSupplier").innerHTML = product.company.name;
				document.getElementById("spanProduct1").innerHTML = product.name;
				document.getElementById("spanProduct2").innerHTML = product.name;
				document.getElementById("spanProduct3").innerHTML = product.name;
				document.getElementById("spanProduct4").innerHTML = product.name;

				if(product.company.status != "Supported")
				{
					this.divCantMoveSupplier.style.display = "block";
				}
				else if(product.availability != "Supported")
				{
					this.divCantMoveProduct.style.display = "block";
				}
			}
		}
	}
}

ActionChoice.prototype.selectChoice = function(choice)
{
	this.action = choice;
	var form = document.forms[0];
	
	if(choice == "Switch")
	{
		form.chkSwitch.checked = true;
		form.chkSwitch.disabled = false;
		choice = "CreateNewContract";
	}
	else if(choice == "CreateNewContract")
	{
		form.chkSwitch.checked = false;
		form.chkSwitch.disabled = false;
	}
	else
	{
		form.chkSwitch.checked = false;
		form.chkSwitch.disabled = true;
	}
	
	for(var i=0; i<form.radAction.length; i++)
	{
		form.radAction[i].checked = (form.radAction[i].value == choice);
	}
}

ActionChoice.prototype.choose = function(element)
{
	var action = element.value;
	var chkSwitch = document.forms[0].chkSwitch;
	
	if(action == "Switch" && chkSwitch.checked == false)
	{
		action = "CreateNewContract";
	}
	else if(action == "CreateNewContract" && chkSwitch.checked == true)
	{
		// This can happen if the user clicks on the CreateNewContract radiobutton, while it was already checked.
		action = "Switch";
	}

	if(action == "Switch" || action == "CreateNewContract")
	{
		chkSwitch.disabled = false;
	}
	else
	{
		chkSwitch.disabled = true;
		chkSwitch.checked = false;
	}

	this.action = action;
	
	this.dossierUpdater.callback = null;
	this.dossierUpdater.setAction(action);
	
	if(this.onchoose != null)
	{
		this.onchoose(action);
	}
}

/* This is for a switch dossier */

function SwitchActionChoice(dossierUpdater)
{
	this.dossierUpdater = dossierUpdater;
	
	this.divActionChoice = document.getElementById("divActionChoice");
	this.tdCreateNewContract = document.getElementById("tdCreateNewContract");
	this.tdDoNothing = document.getElementById("tdDoNothing");
	
	var radioButtons = document.forms[0].radAction;
	
	for(var i=0; i<radioButtons.length; i++)
	{
		radioButtons[i].actionChoice = this;
		radioButtons[i].onclick = function() { this.actionChoice.choose(this); };
	}
	
	this.action = "";
	
	this.onchoose = null;
}

SwitchActionChoice.prototype.showPossibilities = function(possibilities, product)
{
	// By default, hide all the divs
	this.divActionChoice.style.display = "none";
	
	// Only show the possibilities if they are known.
	if(possibilities != null)
	{
		this.divActionChoice.style.display = "block";
			
		this.tdCreateNewContract.style.display = possibilities.canCreateNewContract ? "block" : "none";
		this.tdDoNothing.style.display = possibilities.canDoNothing ? "block" : "none";
	}
}

SwitchActionChoice.prototype.selectChoice = function(choice)
{
	this.action = choice;
	var form = document.forms[0];
	
	for(var i=0; i<form.radAction.length; i++)
	{
		form.radAction[i].checked = (form.radAction[i].value == choice);
	}
}

SwitchActionChoice.prototype.choose = function(element)
{
	var action = element.value;

	this.action = action;
	
	this.dossierUpdater.callback = null;
	this.dossierUpdater.setAction(action);
	
	if(this.onchoose != null)
	{
		this.onchoose(action);
	}
}
