// flag values for reset selections, when user returns and re-run the cost esticamtor

// array of quantity/charge map for 4 accessories
var accessoryMapArray = new Array();
// Note: if we want more than 4 accessories at cost estimator, please update this,
// or figure out other dynamic approach
var map0 = {};
var map1 = {};
var map2 = {};
var map3 = {};

accessoryMapArray[0] = map0;
accessoryMapArray[1] = map1;
accessoryMapArray[2] = map2;
accessoryMapArray[3] = map3;

// map for main item quantity/charge
var quantityChargeMap = {};

function updateMainProductQuantities(aNumber) {
    if ($('defaultMainProductQuantity')) {$('defaultMainProductQuantity').value = aNumber;};
    if ($('cQuantity')) {$('cQuantity').value = aNumber;};
    if ($('fQuantity')) {$('fQuantity').value = aNumber;};
    if ($('pQuantity')) {$('pQuantity').value = aNumber;};
    document.updateMainItemCharge(aNumber);
}

function updateAccessoryQuantities(index) {
    var tag = 'defaultAccessoryQuantity' + index;
    $(tag).value = $('optionGroup' + index).value;
    document.updateAccessoryCharge($(tag).value, accessoryMapArray[index], index);
}

// function to get the charge from the quantityCharge map for main Item
document.updateMainItemCharge = function(aNumber) {
    var chargeVal = -1;
    for (var key in quantityChargeMap) {
        if (aNumber == key) {
            chargeVal = quantityChargeMap[key];
            $('mainItemCharge').value = parseFloat(chargeVal);
            document.updateEstimateTotal();
        }
    }
};

// function to get the charge from the accessory quantityCharge map
document.updateAccessoryCharge = function(aNumber, map, index) {
    var chargeVal = -1;
    for (var key in map) {
        if (aNumber == key) {
            chargeVal = map[key];
            var hiddenName = "accessoryChargeHidden" + index ;
            $(hiddenName).value = parseFloat(chargeVal);
            document.updateEstimateTotal();
        }
    }
};

// calculate the estimated total and put in hidden for tracking in session,
// in order to display previous estimated total to users, when they return to this page.
document.updateEstimateTotal = function () {
    // add up all hidden charge values for items
    var total = parseFloat(0.0);
    if (!isNaN(parseFloat($('mainItemCharge').value))) {
        total += parseFloat($('mainItemCharge').value);
    }

    for (var i = 0; i < 4; i++) {
        var hiddenName = "accessoryChargeHidden" + i;
        if ($(hiddenName) && !isNaN(parseFloat($(hiddenName).value))) {
            total += parseFloat($(hiddenName).value);
        }
    }
    $('estimatedTotalHidden').value = total;
    $('estimatedTotal').innerHTML = "$" + total.toFixed(2);
};

//initiate array of quantity/charge map for main item
document.initMainItemQuantityChargeMap = function (quantity, charge) {
    quantityChargeMap[quantity] = charge;
};

//initiate array of quantity/charge map for accessories
document.initAccessoryQuantityChargeMapArray = function(quantity, charge, index) {
    accessoryMapArray[index][quantity] = charge;
    accessoryMapArray[index]['no'] = 0.00;
};

// reset accessory selections to original state, when returned user start to re-run the estimator
document.resetAccessorySelections = function() {
    for (var i = 0; i < 4; i++) {
        var optName = "optionGroup" + i;
        $(optName).value = "no";
    }
    $('estimatedTotalHidden').value = 0;
};

// reset main item and accessory selections to original state, when returned user start to re-run the estimator
document.resetMainItemAccessorySelections = function() {
    $('mainItemSelection').selectedIndex = 2;
    document.resetAccessorySelections();
};