//current asked feature
var choiceI = -1;
//selected complectation
var complectations;
var itemComplectations;
//temporary array for duplicates excepting
var stringBackButton;
var tcom_root;
var rootNew;
var rootOld;
var rootHistory;
var index;
var currentString;
 

function choicesContains(val) {
    for (var i = 0; i < choices.length; i++) {
        if (choices[i][0] == val) {
            return i;
        }
    }
    return -1;
}
;

function showAll() {

    var x;

    var radios = "";
    var isFirst = true;

    rootNew.keys().each(function (x) {
        var s = "<div class='widget' onclick='checkradio(\"r_" + x + "\");chkContinue(\"" + x + "\")'><input type='radio' id='r_" + x + "' name='checker' class='radio' value='" + x + "' ";
        if (isFirst) {
            isFirst = false;
            s += " checked='true'";
            choiceI = x;
        }
        s += "/> <label>" + x + "</label></div>";
        radios += s;
   });

    var checkersContainer = document.getElementById("checkersContainer");
    checkersContainer.innerHTML = radios;

    var backButtonDiv = document.getElementById("backButtonDiv");

    if (index != 0) {
        backButtonDiv.innerHTML = stringBackButton;
    } else {
        backButtonDiv.innerHTML = "";
    }

    //set feature select caption:
    var featureSelectCaption = document.getElementById("featureSelectCaption");
    featureSelectCaption.innerHTML = complectations[index];

    chkContinue(choiceI);

    if (calculate_price_block != '') {
        fix_height_for_ie6(calculate_price_block)
    }
}

function checkradio(x) {
    document.getElementById(x).checked = 'true';
}

function getSize(a) {
    var i = 0;

	if (!a.keys) {
		return 0;
	}

    a.keys().each(function(k) {
        i++;
    });
    return i;
}

function getLast(aa) {
    var s;
    var a = aa;
    while (getSize(a) == 1) {
		if (a.keys) {
			a.keys().each(function (ss) {
				s = ss;
			});
			a = a.get(s);
		}
    }
    return a;
}

function chkContinue(i) {
    //Remember current choice
    choiceI = i;

    rootNew = rootOld.get(i);

    var button = document.getElementById("nextButton");
    var sourcesAvailable = document.getElementById("sourcesAvailable");
    var softwareRequired = document.getElementById("softwareRequired");
    var calculatorPrice = document.getElementById("calculatorPrice");
    var sourcesSoftwareDiv = document.getElementById("sourcesSoftwareDiv");

    if (getSize(rootNew) > 1) {
        button.innerHTML = continue_text;
        sourcesAvailable.innerHTML = '-';
        softwareRequired.innerHTML = '-';
        calculatorPrice.innerHTML = '-';
        sourcesSoftwareDiv.style.visibility = "hidden";
        //return;
    } else {

        var a = getLast(rootNew);

        button.innerHTML = buy_text;
        sourcesSoftwareDiv.style.visibility = "visible";
        //itemComplectations[a][1];
        calculatorPrice.innerHTML = itemComplectations[a][0];
        sourcesAvailable.innerHTML = itemComplectations[a][1];
        softwareRequired.innerHTML = itemComplectations[a][2];

    }
    if (calculate_price_block != '') {
        fix_height_for_ie6(calculate_price_block)
    }
}

function showNext() {
    if (getSize(rootNew) > 1) {
        index++;
        rootOld = rootNew;
        rootHistory[index] = rootNew;
        showAll();
    } else {
        var a = getLast(rootNew);
        document.location = "/cart-add-package-item-" + a + ".html";
    }
}

function showBack() {
    if (index > 0) {
        index--;
        rootNew = rootHistory[index];
        rootOld = rootNew;
        showAll();
    }
}

function initShowChoices() {
    //get translated backButton
    try {
        var backButtonDiv = document.getElementById("backButtonDiv");
        stringBackButton = backButtonDiv.innerHTML;
        backButtonDiv.innerHTML = "";
        rootNew = prepareCalculateHash(tcom_root);
        index = 0;
        rootOld = rootNew;
        rootHistory = new Array();
        rootHistory[index] = rootNew;
        showAll();
    } catch(e) {
    }
    if (calculate_price_block != '') {
        fix_height_for_ie6(calculate_price_block)
    }
}

function prepareCalculateHash(a) {
    var hash = new Hash();

    a.each(function(ar) {
        var current = hash;
        var counter = 0;
        var previous = null;

        ar.each(function(k) {
            counter++;

            if (counter == ar.length - 1) {
                current.set(k, '');
            } else if (counter == ar.length) {
                current.set(previous, k);
            } else {
                if (current.get(k) == null) {
                    var tmpHash = new Hash();
                    current.set(k, tmpHash);
                }

                current = current.get(k);
            }

            previous = k;
        });
    });
	return hash;
}
