function DossierUpdater(dossierId, section)
{
	this.dossierId = dossierId;
	this.section = section;
	this.callback = null;
}

DossierUpdater.prototype.getInformation = function()
{
	var command = window.ajax.createCommand("DossierUpdater.GetInformation");
	command.callback = this.callback;
	
	command.parameters["DossierId"] = this.dossierId;
	command.parameters["Section"] = this.section;
	
	window.ajax.execute(command);
}

DossierUpdater.prototype.setCurrentSituation = function(situation)
{
	var command = window.ajax.createCommand("DossierUpdater.SetCurrentSituation");
	command.callback = this.callback;

	command.parameters["DossierId"] = this.dossierId;
	command.parameters["Section"] = this.section;
	command.parameters["HasContract"] = situation.hasContract;
	command.parameters["CurrentSupplier"] = situation.currentSupplier;
	command.parameters["CurrentProduct"] = (situation.currentProduct == null) ? 0 : situation.currentProduct.id ;
	command.parameters["IsAvailable"] = situation.isAvailable;
	
	window.ajax.execute(command);
}

DossierUpdater.prototype.getCableInformation = function()
{
	var command = window.ajax.createCommand("DossierUpdater.GetCableInformation");
	command.callback = this.callback;
	command.parameters["DossierId"] = this.dossierId;
	
	window.ajax.execute(command);
}

DossierUpdater.prototype.setCableSituation = function(hasContract, currentSupplier)
{
	var command = window.ajax.createCommand("DossierUpdater.SetCableSituation");
	command.callback = this.callback;
	
	command.parameters["DossierId"] = this.dossierId;
	command.parameters["HasContract"] = hasContract;
	command.parameters["CurrentSupplier"] = (currentSupplier ? currentSupplier : 0);
	
	window.ajax.execute(command);
}

DossierUpdater.prototype.setAction = function(action)
{
	var command = window.ajax.createCommand("DossierUpdater.SetAction");
	command.callback = this.callback;

	command.parameters["DossierId"] = this.dossierId;
	command.parameters["Section"] = this.section;
	command.parameters["Action"] = action;
	
	window.ajax.execute(command);
}

DossierUpdater.prototype.setProductChoice = function(productId)
{
	var command = window.ajax.createCommand("DossierUpdater.SetProductChoice");
	command.callback = this.callback;

	command.parameters["DossierId"] = this.dossierId;
	command.parameters["Section"] = this.section;
	command.parameters["ProductId"] = productId;
	
	window.ajax.execute(command);
}

DossierUpdater.prototype.setElectricityUsage = function(usage)
{
	var command = window.ajax.createCommand("DossierUpdater.SetElectricityUsage");
	command.callback = this.callback;
	
	command.parameters["DossierId"] = this.dossierId;
	command.parameters["Usage"] = usage;
	
	window.ajax.execute(command);
}

DossierUpdater.prototype.setGasUsage = function(usage)
{
	var command = window.ajax.createCommand("DossierUpdater.SetGasUsage");
	command.callback = this.callback;
	
	command.parameters["DossierId"] = this.dossierId;
	command.parameters["Usage"] = usage;
	
	window.ajax.execute(command);
}

function GetProducts(companyId)
{
	var command = window.ajax.createCommand("ProductProvider.GetProducts");
	command.callback = populateProductDropdown;

	command.parameters["CompanyId"] = companyId;
	command.parameters["Section"] = section;

	window.ajax.execute(command);
}

function GetPossibilities()
{
	var command = window.ajax.createCommand("DossierUpdater.GetPossibilities");
	command.callback = showPossibilities;

	command.parameters["DossierId"] = dossierId;
	command.parameters["Section"] = section;
	
	window.ajax.execute(command);
}

function UpdateDate(sect, field, date)
{
	var command = window.ajax.createCommand("DossierUpdater.UpdateDate");
	
	command.parameters["DossierId"] = dossierId;
	command.parameters["Section"] = sect;
	command.parameters["Field"] = field;
	command.parameters["Date"] = date;
	
	window.ajax.execute(command);
}

function GetDocumentStatus()
{
	var command = window.ajax.createCommand("DocumentStatusProvider.GetDocumentStatus");

	command.callback  = showDocumentStatus;
	command.parameters["DossierId"] = dossierId;

	window.ajax.execute(command);
}

function CancelDocument(eanCode)
{
	var command = window.ajax.createCommand("DossierUpdater.CancelDocument");

	command.callback  = GetDocumentStatus;
	command.parameters["DossierId"] = dossierId;
	command.parameters["EanCode"] = eanCode;
	
	window.ajax.execute(command);
}

function PrepareForNextStep()
{
	var command = window.ajax.createCommand("DossierUpdater.PrepareForNextStep");

	command.callback  = function(command, result)
	{
		alert("De huidige status is : " + result.data.status);
	};
	
	command.parameters["DossierId"] = dossierId;
	
	window.ajax.execute(command);
}

function SetNotificationSettings(enableNotifications)
{
	var command = window.ajax.createCommand("DossierUpdater.SetNotificationSettings");

	command.callback  = function(command, result)
	{
	};
	
	command.parameters["DossierId"] = dossierId;
	command.parameters["EnableNotifications"] = enableNotifications;
	
	window.ajax.execute(command);
}

/* These methods track changes in the current Situation */

function populateProductDropdown(command, result)
{
	if(result.code == 1)
	{
		var listbox = document.getElementById(currentSituationScope+"_lbxCurrentProduct");
		
		listbox.options.length = 0;
		listbox.options.add(new Option("Kies uw huidige product...", 0));
		
		for(var i=0; i<result.data.products.length; i++)
		{
			var product = result.data.products[i];
			listbox.options.add(new Option(product.name, product.id));
		}

		// If there is only one option, then automatically choose that option.
		if(result.data.products.length == 1)
		{
			listbox.selectedIndex = 1;
			SetCurrentProduct(listbox.options[1].value);
		}
	}
	else
	{
		alert(result.message);
	}
}

function chooseAvailability(isAvailable)
{
	// Update the dossier on the server.
	SetAvailability(isAvailable);
}

function chooseCurrentSituation(hasContract)
{
	var divCurrentProduct = document.getElementById(currentSituationScope+"_divCurrentProduct");

	// Update the dossier on the server.
	SetCurrentSituation(hasContract);

	if(hasContract)
	{
		divCurrentProduct.style.display = "block";
	}
	else
	{
		divCurrentProduct.style.display = "none";
		document.getElementById(currentSituationScope+"_lbxCurrentCompany").selectedIndex = 0;
		document.getElementById(currentSituationScope+"_lbxCurrentProduct").options.length = 0;
	}
}

function chooseCurrentCompany(element)
{
	var companyId = element.options[element.selectedIndex].value;
	
	// Update the dossier on the server.
	SetCurrentSupplier(companyId);
	
	if(companyId == 0)
	{
		var listbox = document.getElementById(currentSituationScope+"_lbxCurrentProduct");
		listbox.options.length = 0;
	}
	else
	{		
		GetProducts(companyId);
	}
}

function chooseCurrentProduct(element)
{
	var productId = element.options[element.selectedIndex].value;

	// Update the dossier on the server.
	SetCurrentProduct(productId);
}

/* Action Choice */

function chooseAction(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 clickes on the CreateNewContract radiobutten, while it was already checked.
		action = "Switch";
	}

	// Update the dossier on the server.
	SetAction(action);
	
	if(action == "Switch" || action == "CreateNewContract")
	{
		chkSwitch.disabled = false;
	}
	else
	{
		chkSwitch.disabled = true;
		chkSwitch.checked = false;
	}
	
	// Update the product overview.
	
	var productOverview = document.getElementById("ProductOverview");

	if(action == "Switch" || action == "CreateNewContract")
	{
		productOverview.style.display = "block";
		ShowProductRadioButtons(true);
	}
	else
	{
		productOverview.style.display = "none";

		var radProductChoice = document.forms[0].ProductChoice;		

		if(radProductChoice != null)
		{
			// This is a little trick: If there is only one radiobutton, then radioButton isn't an array,
			// but the actual radiobutton. So we put it in an array manually.
			if(!radProductChoice.length) { radProductChoice = new Array(radProductChoice); }		

			for(var i=0; i<radProductChoice.length; i++)
			{
				radProductChoice[i].checked = false;
			}		

			ShowProductRadioButtons(false);
		}
	}
}

function showDocumentStatus(command, result)
{
	var element = document.getElementById("documentStatus");
	element.innerHTML = "";
	
	for(var productGroup in result.data)
	{
		if(result.data[productGroup]._isFunction)
		{
			continue;
		}
		
		var info = result.data[productGroup];
		var productGroupNames = {Electricity: "elektriciteit", Gas: "gas", Water: "water", Warmth: "warmte", RTV: "televisie"};
		var documentTypeNames = {Contract : "Aanvraag", SwitchContract : "Aanvraag", MoveDocument: "Verhuismelding", MeterForm: "Meterstandenformulier"};
		
		var pgElement = document.createElement("div");
		element.appendChild(pgElement);
		pgElement.className = "document";
		
		var headerEl = document.createElement("h3");
		pgElement.appendChild(headerEl);
		
		var headerText = documentTypeNames[info.documentType]+" voor "+productGroupNames[productGroup]+" aan "+info.supplier;
		headerEl.appendChild(document.createTextNode(headerText));
		
		for(var i=0; i < info.history.length; i++)
		{
			var documentEvent = info.history[i];
			var eventEl = document.createElement("div");
			eventEl.className = "document-event";

			var dateEl = document.createElement("b");
			eventEl.appendChild(dateEl);
			
			var date = documentEvent.date.toFriendlyDate('Long');
			
			var hourString = documentEvent.date.getHours().toString();
			if(hourString.length == 1)
			{
				hourString = "0" + hourString;
			}
			
			var minuteString = documentEvent.date.getMinutes().toString();
			if(minuteString.length == 1)
			{
				minuteString = "0" + minuteString;
			}
			
			var time = hourString + ":" + minuteString;

			dateEl.appendChild(document.createTextNode(date+" - "+time));
			eventEl.appendChild(document.createElement("br"));
			eventEl.appendChild(document.createTextNode(documentEvent.message));
			
			pgElement.appendChild(eventEl);
		}
		
		var linkDiv = document.createElement("div");
		linkDiv.style.paddingLeft = "19px";
		linkDiv.style.paddingTop = "1px";
		linkDiv.style.paddingBottom = "5px";
		pgElement.appendChild(linkDiv);
		
		var documentLink = document.createElement("a");
		documentLink.href = "pdf/getOrders.aspx?eancode="+info.eancode;
		documentLink.target = "_blank";
		documentLink.className = "pdfLink";
		documentLink.innerHTML = "toon document";
		linkDiv.appendChild(documentLink);
		
		if(info.hasFaxImage)
		{
			var faxLink = document.createElement("a");
			faxLink.href = "pdf/getFax.aspx?eancode="+info.eancode;
			faxLink.target = "_blank";
			faxLink.className = "faxLink";
			faxLink.innerHTML = "toon fax";
			linkDiv.appendChild(faxLink);
		}
		else if(window.isServicedesk)
		{
			var removeDocLink = document.createElement("a");
			removeDocLink.href = "javascript:CancelDocument('" + info.eancode + "');";
			removeDocLink.className = "removeDocLink";
			removeDocLink.innerHTML = "annuleer " + documentTypeNames[info.documentType].toLowerCase();
			linkDiv.appendChild(removeDocLink);
		}
	}
	
	if(window.isServicedesk)
	{
		document.getElementById("PrepareForNextStep").style.display = "block";
	}
}