﻿function ProductUpdate(buttonRef) {
    var curProduct = buttonRef.parentNode;
    var curElement;
    var name = '', type = '', size = '', price = '', graphics = '', text = '', cusTxt = '', bolTxt, bolGfx, bolCust;
    
    for (var i = 0; i < curProduct.childNodes.length - 1; i++) {
        curElement = curProduct.childNodes[i];

        switch (curElement.className) {
            case 'Name':
                name = curElement.value;
                break;
            case 'Type':
                type = curElement.options[curElement.selectedIndex].text;
                break;
            case 'Size':
                size = curElement.options[curElement.selectedIndex].value;
                if (curElement.selectedIndex) var number = curElement.selectedIndex;
                switch (number) {
                    case 0:
                    case 1:
                    case 2:
                    case 3:
                        price = 10;
                        break;
                    case 4:
                    case 5:
                        price = 12;
                        break;
                    default:
                        price = 10;
                        break;
                }
                break;
            case 'NoGfx':
                bolGfx = curElement.checked;
                break;
            case 'NoTxt':
                bolTxt = curElement.checked;
                break;
            case 'cusTxt':
                bolCust = curElement.checked;
                break;
            case 'customText':
                cusTxt = curElement.firstChild.nextSibling.value;
                break;
            default:
                break;
        }
    }

    if (bolGfx && bolTxt) {
        price = 8;
        name = "Plain T-Shirt";
        text = "NoText";
        graphics = "NoGraphics";
    }
    else if (bolGfx) {
        graphics = "NoGraphics";
    }
    else if (bolTxt) {
        text = "NoText";
    }

    if (bolCust && cusTxt.length > 0) {
        price += 4;
        text = "Add custom text: '" + cusTxt + "'";
    }

    for (var i = 0; i < curProduct.childNodes.length - 1; i++) {
        curElement = curProduct.childNodes[i];

        switch (curElement.className) {
            case 'product-title':
                curElement.value = name;
                break;
            case 'product-attr-type':
                curElement.value = type;
                break;
            case 'product-attr-size':
                curElement.value = size;
                break;
            case 'product-attr-text':
                curElement.value = text;
                break;
            case 'product-attr-graphic':
                curElement.value = graphics;
                break;
            case 'product-price':
                curElement.value = price;
                break;
        }
    }
}

function customText(checkBoxRef) {
    var curProduct = checkBoxRef.parentNode;
    var curElement;
    for (var i = 0; i < curProduct.childNodes.length - 1; i++) {
        curElement = curProduct.childNodes[i];
        if (curElement.className == 'customText') break;
    }

    if (checkBoxRef.checked) {
       curElement.style.display = "";
    }
    else
    {
       curElement.style.display = "none";
    }        
}
