/*
 * JavaScript to be used on pages where the wizards can be started.
 */

var baseUrl = "http://veiviser.telenor.net/";
var commandParameter = "cmd";
var startCommand = "start";
var faqCommand = "faq";
var contactCommand = "contact";
var wizardParameter = "w";
var startChoicesParameter = "startChoices";
var choiceParameter = "choice";
var expandParameter = "expand";

/*
 * Starts product wizard #1
 */
function startWizard1() {

	// Get the possible choices, which are on the format w01b01c0x
	// where x is 1, 2 or 3 and could be radio buttons or checkboxes
	var baseId = "w01b01c";
	var choices = getChoices(baseId, 3);

	// Input validation
	if (choices === "") {
		return false;
	}

	// Create the URL
	var url = baseUrl + "veiviser?" + commandParameter + "=" + startCommand;
	
	url += "&";
	url += wizardParameter + "=w01";
	url += "&";
	url += startChoicesParameter + "=" + choices;

	// Start the servlet
	window.location = url;
}

/*
 * Starts product wizard #2
 */
function startWizard2() {

	// Get the possible choices, which are on the format w01b01c0x
	// where x is 1, 2 or 3 and could be radio buttons or checkboxes
	var baseId = "w02b01c";
	var choices = getChoices(baseId, 3);

	// Input validation
	if (choices === "") {
		return false;
	}

	// Create the URL
	var url = baseUrl + "veiviser?" + commandParameter + "=" + startCommand;
	url += "&";
	url += wizardParameter + "=w02";
	url += "&";
	url += startChoicesParameter + "=" + choices;

	// Start the servlet
	window.location = url;
}

/*
 * Starts product wizard #3
 */
function startWizard3() {

	// Get the possible choices, which are on the format w03b01c0x
	// where x is 1, 2 or 3 and could be radio buttons or checkboxes
	var baseId = "w03b01c";
	var choices = getChoices(baseId, 3);

	// Input validation
	if (choices === "") {
		return false;
	}

	// Create the URL
	var url = baseUrl + "veiviser?" + commandParameter + "=" + startCommand;
	url += "&";
	url += wizardParameter + "=w03";
	url += "&";
	url += startChoicesParameter + "=" + choices;

	// Start the servlet
	window.location = url;
}

/*
 * Starts package wizard
 */
function startPackage() {

	// Get the possible choices, which are on the format pkgb01c0x
	// where x is 1, 2 or 3 and could be radio buttons or checkboxes
	var baseId = "pkgcat01b02c";
	var choices = getChoices(baseId, 3);

	// Input validation
	if (choices === "") {
		return false;
	}
	
	// Create the URL
	var url = baseUrl + "pakke?" + commandParameter + "=" + startCommand;
	url += "&";
	url += wizardParameter + "=pkg";
	url += "&";
	url += startChoicesParameter + "=" + choices;
	var expand = $("expand").value;
	url += "&";
	url += expandParameter + "=" + expand;

	// Start the servlet
	window.location = url;
}

/*
 * Starts FAQ wizard (Ofte stilte spørsmål)
 */
function startFaq() {

	// Get the possible choices, which are on the format faq01
	// where x is 1 to 7 and could be radio buttons or checkboxes
	var baseId = "cat";
	var choices = getChoices(baseId, 7);

	// Input validation
	if (choices === "") {
		return false;
	}
	
	// Create the URL
	var url = baseUrl + "servlet?" + commandParameter + "=" + faqCommand;
	url += "&";
	url += choiceParameter + "=" + choices;

	// Start the servlet
	window.location = url;
}

/*
 * Starts contact wizard (Kontaktveiviser)
 */
function startContact() {

	// Get the possible choices, which are on the format cat01
	// where x is 1 to 3 and could be radio buttons or checkboxes
	var baseId = "cat";
	var choices = getChoices(baseId, 4);

	// Input validation
	if (choices === "") {
		return false;
	}
	
	// Create the URL
	var url = baseUrl + "servlet?" + commandParameter + "=" + contactCommand;
	url += "&";
	url += choiceParameter + "=" + choices;

	// Start the servlet
	window.location = url;
}

/*
 * Generates a comma-separated list of checked radiobuttons / checkboxes
 */
function getChoices(baseId, numElements) {
	var result = "";
	for (var index = 1; index <= numElements; index++) {

		// Create the elementID
		indexString = (100 + index).toString().substring(1);
		elementId = baseId + indexString;

		// Is the element selected?
		if (isSelected(elementId)) {
			if (result !== "") {
				result += ",";
			}
			result += elementId;
		}
	}
	return result;
}

/*
 * Finds out if a radio button or checkbox is selected.
 */ 
function isSelected(elementId) {
	var element = $(elementId);
	if (element === null) {
		return false;
	}
	return element.checked;
}

/*
 * Shortcut function for getting an element
 */
 function $(elementId) {
	return document.getElementById(elementId);
}