/**
 * Shows or hides columns (or elements).
 * @param showFirstColumn enables or disables the first column display.
 * @param firstColumnHeadId the id of the head element. May be omitted if
 *  showFirstColumn is false.
 * @param firstColumnRightId the id of the right element. May be omitted if
 *  showFirstColumn is false.
 * @param firstColumnLeftId the id of the left element. May be omitted if
 *  showFirstColumn is false.
 * @param firstColumnHeadDesc the description placed in the head element. May be
 *  omitted if showFirstColumn is false.
 * @param showSecondColumn enables or disables the second column display.
 * @param secondColumnHeadId the id of the head element. May be omitted if
 *  showFirstColumn is false.
 * @param secondColumnRightId the id of the right element. May be omitted if
 *  showFirstColumn is false.
 * @param secondColumnLeftId the id of the left element. May be omitted if
 *  showFirstColumn is false.
 * @param secondColumnHeadDesc the description placed in the head element. May be
 *  omitted if showFirstColumn is false.
 * @param rightParameters the parameters for the right value.
 * @param leftParameters the parameters for the left value.
 * @param rightElementName the name for the right value.
 * @param leftElementName the name for the left value.
 * @param rightSelectedValue the selected Value for the right value.
 * @param leftSelectedValue the selected Value for the left value.
 */
function showOrHideColumns(
        showFirstColumn,  firstColumnHeadId,  firstColumnRightId,  firstColumnLeftId,  firstColumnHeadDesc,
        showSecondColumn, secondColumnHeadId, secondColumnRightId, secondColumnLeftId, secondColumnHeadDesc,
        rightParameters, leftParameters, rightElementName, leftElementName, rightSelectedValue, leftSelectedValue) {
    if (dijit.byId(firstColumnRightId)) {
        dijit.byId(firstColumnRightId).destroy();
    }
    if (dijit.byId(firstColumnLeftId)) {
        dijit.byId(firstColumnLeftId).destroy();
    }
    if (dijit.byId(secondColumnRightId)) {
        dijit.byId(secondColumnRightId).destroy();
    }
    if (dijit.byId(secondColumnLeftId)) {
        dijit.byId(secondColumnLeftId).destroy();
    }
    if (showFirstColumn) {
        dojo.style(firstColumnHeadId, 'display', '');
        dojo.create('span',
                {
                   innerHTML: firstColumnHeadDesc
                },
                firstColumnHeadId,
                'only');
        if (rightParameters) {
            dojo.style(firstColumnRightId, 'display', '');
            placeInputElement(firstColumnRightId, rightElementName,
                    rightParameters, rightSelectedValue);
        } else {
            dojo.create('span', null, firstColumnRightId, 'only');
            dojo.style(firstColumnRightId, 'display', '');
        }
        if (leftParameters) {
            if (!showSecondColumn) {
                dojo.style(firstColumnLeftId, 'display', '');
                placeInputElement(firstColumnLeftId, leftElementName,
                        leftParameters, leftSelectedValue);
            } else {
                dojo.create('span', null, firstColumnLeftId, 'only');
                dojo.style(firstColumnLeftId, 'display', '');
            }
        } else {
            dojo.create('span', null, firstColumnLeftId, 'only');
            dojo.style(firstColumnLeftId, 'display', '');
        }
    } else {
        dojo.style(firstColumnHeadId, 'display', 'none');
        dojo.style(firstColumnRightId, 'display', 'none');
        dojo.style(firstColumnLeftId, 'display', 'none');
    }
    if (showSecondColumn) {
        dojo.style(secondColumnHeadId, 'display', '');
        dojo.create('span',
                {
                   innerHTML: secondColumnHeadDesc
                },
                secondColumnHeadId,
                'only');
        dojo.create('span', null, secondColumnRightId, 'only');
        dojo.style(secondColumnRightId, 'display', '');
        if (leftParameters) {
            dojo.style(secondColumnLeftId, 'display', '');
            placeInputElement(secondColumnLeftId, leftElementName,
                    leftParameters, leftSelectedValue);
        } else {
            dojo.create('span', null, secondColumnLeftId, 'only');
            dojo.style(secondColumnLeftId, 'display', '');
        }
    } else {
        if (secondColumnHeadId) {
            dojo.style(secondColumnHeadId, 'display', 'none');
            dojo.style(secondColumnRightId, 'display', 'none');
            dojo.style(secondColumnLeftId, 'display', 'none');
        }
    }
}

function updateContactLensParameters() {
    var rightSelectBox = dijit.byId('rightArticle');
    var leftSelectBox = dijit.byId('leftArticle');
    var oldRightArticle = dojo.byId('oldRightArticle').value;
    var oldLeftArticle = dojo.byId('oldLeftArticle').value;
    var newRightArticle = rightSelectBox.attr('value');
    var newLeftArticle = leftSelectBox.attr('value');
    dojo.byId('oldRightArticle').value = newRightArticle;
    dojo.byId('oldLeftArticle').value = newLeftArticle;
    SubscriptionEdit.articleSelected(newRightArticle, oldRightArticle,
            newLeftArticle, oldLeftArticle,
            function(data) {
                eval(data);
                updateData(productParameters, oldRightArticle, oldLeftArticle);
            }
    );
}

function updateData(productParameters, oldRightArticle, oldLeftArticle) {
    if (productParameters.right.productChanged) {
        dijit.byId('rightArticle').attr('value',
                productParameters.right.parType, false);
    }
    if (productParameters.left.productChanged) {
        dijit.byId('leftArticle').attr('value',
                productParameters.left.parType, false);
    }

    if (!productParameters.right.productChanged && productParameters.right.parType != 'xxx') {
        var rightAmount = dijit.byId('rightAmount').attr('value');
        var rightArticle = dijit.byId('rightArticle').attr('value');
        if (dijit.byId('rightBase')) {
            var rightBase = dijit.byId('rightBase').attr('value');
        }
        if (dijit.byId('rightDiop')) {
            var rightDiop = dijit.byId('rightDiop').attr('value');
        }
        if (dijit.byId('rightDiam')) {
            var rightDiam = dijit.byId('rightDiam').attr('value');
        }
        if (dijit.byId('rightDf1')) {
            var rightDf1 = dijit.byId('rightDf1').attr('value');
        }
        if (dijit.byId('rightDf2')) {
            var rightDf2 = dijit.byId('rightDf2').attr('value');
        }
        if (dijit.byId('rightDf3')) {
            var rightDf3 = dijit.byId('rightDf3').attr('value');
        }
        if (dijit.byId('rightDf4')) {
            var rightDf4 = dijit.byId('rightDf4').attr('value');
        }
        if (dijit.byId('rightColor')) {
            var rightColor = dijit.byId('rightColor').attr('value');
        }
    }
    if (!productParameters.left.productChanged && productParameters.left.parType != 'xxx') {
        var leftAmount = dijit.byId('leftAmount').attr('value');
        var leftArticle = dijit.byId('leftArticle').attr('value');
        if (dijit.byId('leftBase')) {
            var leftBase = dijit.byId('leftBase').attr('value');
        }
        if (dijit.byId('leftDiop')) {
            var leftDiop = dijit.byId('leftDiop').attr('value');
        }
        if (dijit.byId('leftDiam')) {
            var leftDiam = dijit.byId('leftDiam').attr('value');
        }
        if (dijit.byId('leftDf1')) {
            var leftDf1 = dijit.byId('leftDf1').attr('value');
        }
        if (dijit.byId('leftDf2')) {
            var leftDf2 = dijit.byId('leftDf2').attr('value');
        }
        if (dijit.byId('leftDf3')) {
            var leftDf3 = dijit.byId('leftDf3').attr('value');
        }
        if (dijit.byId('leftDf4')) {
            var leftDf4 = dijit.byId('leftDf4').attr('value');
        }
        if (dijit.byId('leftColor')) {
            var leftColor = dijit.byId('leftColor').attr('value');
        }
    }

    showOrHideColumns(
            productParameters.head.hasBase, 'headBase', 'rightBaseCell', 'leftBaseCell', 'Base Curve',
            false, null, null, null, null,
            productParameters.right.bases, productParameters.left.bases, 'rightBase', 'leftBase', undefined, undefined);

    showOrHideColumns(
            productParameters.head.hasDiop, 'headDiop', 'rightDiopCell', 'leftDiopCell', 'Power',
            false, null, null, null, null,
            productParameters.right.diops, productParameters.left.diops, 'rightDiop', 'leftDiop', productParameters.right.defaultDiop, productParameters.left.defaultDiop);

    showOrHideColumns(
            productParameters.head.hasDiam, 'headDiam', 'rightDiamCell', 'leftDiamCell', 'Diameter',
            false, null, null, null, null,
            productParameters.right.diams, productParameters.left.diams, 'rightDiam', 'leftDiam', undefined, undefined);

    showOrHideColumns(
            productParameters.head.hasDf11, 'headDf11', 'rightDf11Cell', 'leftDf11Cell', productParameters.head.descDf11,
            productParameters.head.hasDf12, 'headDf12', 'rightDf12Cell', 'leftDf12Cell', productParameters.head.descDf12,
            productParameters.right.df1s, productParameters.left.df1s, 'rightDf1', 'leftDf1', undefined, undefined);

    showOrHideColumns(
            productParameters.head.hasDf21, 'headDf21', 'rightDf21Cell', 'leftDf21Cell', productParameters.head.descDf21,
            productParameters.head.hasDf22, 'headDf22', 'rightDf22Cell', 'leftDf22Cell', productParameters.head.descDf22,
            productParameters.right.df2s, productParameters.left.df2s, 'rightDf2', 'leftDf2', undefined, undefined);

    showOrHideColumns(
            productParameters.head.hasDf31, 'headDf31', 'rightDf31Cell', 'leftDf31Cell', productParameters.head.descDf31,
            productParameters.head.hasDf32, 'headDf32', 'rightDf32Cell', 'leftDf32Cell', productParameters.head.descDf32,
            productParameters.right.df3s, productParameters.left.df3s, 'rightDf3', 'leftDf3', undefined, undefined);

    showOrHideColumns(
            productParameters.head.hasDf41, 'headDf41', 'rightDf41Cell', 'leftDf41Cell', productParameters.head.descDf41,
            productParameters.head.hasDf42, 'headDf42', 'rightDf42Cell', 'leftDf42Cell', productParameters.head.descDf42,
            productParameters.right.df4s, productParameters.left.df4s, 'rightDf4', 'leftDf4', undefined, undefined);

    showOrHideColumns(
            productParameters.head.hasColor, 'headColor', 'rightColorCell', 'leftColorCell', productParameters.head.descColor,
            false, null, null, null, null,
            productParameters.right.colors, productParameters.left.colors, 'rightColor', 'leftColor', undefined, undefined)

    if (!productParameters.right.productChanged &&
            productParameters.right.parType != 'xxx' &&
            productParameters.right.parType == oldRightArticle) {
        dijit.byId('rightAmount').attr('value', rightAmount);
        dijit.byId('rightArticle').attr('value', rightArticle);
        if (rightBase) {
            dijit.byId('rightBase').attr('value', rightBase);
        }
        if (rightDiop) {
            dijit.byId('rightDiop').attr('value', rightDiop);
        }
        if (rightDiam) {
            dijit.byId('rightDiam').attr('value', rightDiam);
        }
        if (rightDf1) {
            dijit.byId('rightDf1').attr('value', rightDf1);
        }
        if (rightDf2) {
            dijit.byId('rightDf2').attr('value', rightDf2);
        }
        if (rightDf3) {
            dijit.byId('rightDf3').attr('value', rightDf3);
        }
        if (rightDf4) {
            dijit.byId('rightDf4').attr('value', rightDf4);
        }
        if (rightColor) {
            dijit.byId('rightColor').attr('value', rightColor);
        }
    }
    if (!productParameters.left.productChanged &&
            productParameters.left.parType != 'xxx' &&
            productParameters.left.parType == oldLeftArticle) {
        dijit.byId('leftAmount').attr('value', leftAmount);
        dijit.byId('leftArticle').attr('value', leftArticle);
        if (leftBase) {
            dijit.byId('leftBase').attr('value', leftBase);
        }
        if (leftDiop) {
            dijit.byId('leftDiop').attr('value', leftDiop);
        }
        if (leftDiam) {
            dijit.byId('leftDiam').attr('value', leftDiam);
        }
        if (leftDf1) {
            dijit.byId('leftDf1').attr('value', leftDf1);
        }
        if (leftDf2) {
            dijit.byId('leftDf2').attr('value', leftDf2);
        }
        if (leftDf3) {
            dijit.byId('leftDf3').attr('value', leftDf3);
        }
        if (leftDf4) {
            dijit.byId('leftDf4').attr('value', leftDf4);
        }
        if (leftColor) {
            dijit.byId('leftColor').attr('value', leftColor);
        }
    }

}

