
function variant_option_changed(product_id)
{
    var selected_combo = get_selected_combo(product_id);
    if (selected_combo && selected_combo.enabled=='1') {
        set_product_price(product_id, selected_combo);
        set_product_image(product_id, selected_combo);
    }
    else {
        // see if they're all selected
        var gname,field,
            all_selected = true;
        for (var i in window.variant_data[product_id].variants) {
            gname = window.variant_data[product_id].variants[i].groupName.replace(/[^0-9A-z]/g,'_');
            field = document.getElementById('p' + product_id + '-' + gname);
            if (field.value=='') {
                all_selected = false;
            }
        }
        if (all_selected) {
            set_error('Sorry, but that combination of variants is not available for purchase.');
        }
    }
}

function get_selected_combo(product_id)
{
    var selected_combo = false;
    if (typeof window.variant_data[product_id].combos != 'undefined') {
        var combo,
            field,
            gname,
            selected;
        for (var i in window.variant_data[product_id].combos) {
            combo = window.variant_data[product_id].combos[i];
            selected = true;
            for (var j in combo.variants) {
                gname = combo.variants[j].groupName.replace(/[^0-9A-z]/g,'_');
                // get the selected value
                field = document.getElementById('p' + product_id + '-' + gname);
                if (combo.variants[j].variantOptionId) {
                    if (field.value!=combo.variants[j].variantOptionId) {
                        selected = false;
                    }
                }
                else {
                    if (field.value) {
                        selected = false;
                    }
                }
            }
            if (selected) {
                selected_combo = combo;
                break;
            }
        }
    }
    return selected_combo;
}

function set_product_price(product_id, combo)
{
    // get base web cost
    if (typeof window.basecost[product_id] == 'undefined') {
        window.basecost[product_id] = new Number(document.getElementById('p' + product_id + '-productCost').innerHTML.replace(/[^0-9.]/g,''));
    }

    // get additional variant cost
    var cost = new Number(window.basecost[product_id]);
    if (combo.priceChangeType && combo.priceChangeType!='none') {
        // change from basecost
        combo.priceChange = new Number(combo.priceChange); // cast to an int
        switch (combo.priceChangeType) {
            case 'add':
                cost += combo.priceChange;
                break;
            case 'subtract':
                cost -= combo.priceChange;
                break;
            case 'fixed':
                cost = combo.priceChange;
                break;
            default:
                break;
        }
    }

    // calculate volume discount
    if (typeof window.volume_discount != 'undefined') {
        if (typeof window.volume_discount[product_id] != 'undefined') {
            var quantity_field = document.getElementById('p' + product_id + '-itemQuantity');
            if (quantity_field!=null) {
                var quantity = quantity_field.value;
                var total_cost = cost * quantity;
                var r;
                var amount;
                var discount = false;
                for(var i in window.volume_discount[product_id].rules) {
                    r = window.volume_discount[product_id].rules[i];
                    if (r.minAmount >= quantity && r.maxAmount <= quantity) {
                        discount = r;
                    }
                }
                if (discount != false) {
                    // apply the discount
                    if (discount.discountType=='percent') {
                        amount = discount.discountAmount/100 * total_cost;
                    }
                    else {
                        amount = discount.discountAmount;
                    }
                }
            }
        }
    }

    // round the total and show it
    cost = new Number(cost).toFixed(2);
    document.getElementById('p' + product_id + '-productCost').innerHTML = cost;
}

function set_product_image(product_id, combo)
{
    var imageEle;
    if (!(imageEle=document.getElementById('thumbImage'+product_id))) {
        return false;
    }
    // get the original image
    if (typeof window.origImage == 'undefined') {
        window.origImage = {};
    }
    if (typeof window.origImage[product_id] == 'undefined') {
        window.origImage[product_id] = imageEle.src;
    }

    var setSrc = window.origImage[product_id];
    if (combo.image) {
        var matches;
        // origImage = http://.../shop/image.gif?geometry...
        //                     replace ^^^this^^ with the combo.image
        if ((matches = window.origImage[product_id].match(/\/([^\/]+)\?/))) {
            setSrc = window.origImage[product_id].replace(matches[1],combo.image);
        }
    }

    if (imageEle.getAttribute('src')!=setSrc) {
        imageEle.setAttribute('src',setSrc);
    }

    return true;
}

function set_error(error)
{
    alert(error);
}