
/**
 * TODO: update set_error so it's not an alert
 *       update process so that variants are selected one at a time, with each select box
 *          disabled until the previous one is selected
 *       update to check stock levels
 */

function setDetailsDiv(show){

    var bits = new Array("features", "description", "additionalInfo", "additionalImages", "shippingInfo", "reviews");

    for (var i=0; i<bits.length; i++)
    {
        if (document.getElementById(bits[i]) && bits[i] != show){
            document.getElementById(bits[i]).style.display='none';
        }
        if (document.getElementById(bits[i] + "Header")){
            document.getElementById(bits[i] + "Header").className = "bgOff";
        }

    }

    if (document.getElementById(show + "Header"))
        document.getElementById(show + "Header").className = "bgOn";

    if (document.getElementById(show))
        document.getElementById(show).style.display='';

}


function set_product_price(combo)
{
    // get base web cost
    if (typeof window.basecost == 'undefined') {
        window.basecost = new Number(document.getElementById('productCost').innerHTML.replace(/[^0-9.]/g,''));
    }

    // get additional variant cost
    var cost = new Number(window.basecost);
    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') {
        var quantity_field = document.getElementById('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.rules) {
                r = window.volume_discount.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('productCost').innerHTML = cost;
}

function set_product_image(combo)
{
    var imageEle;
    if (!(imageEle=document.getElementById('mainImage'+window.productId))) {
        return false;
    }
    // get the original image
    if (typeof window.origImage == 'undefined') {
        window.origImage = imageEle.src;
    }

    var setSrc = window.origImage;
    if (combo.image) {
        var matches;
        // origImage = http://.../shop/image.gif?geometry...
        //                     replace ^^^this^^ with the combo.image
        if ((matches = window.origImage.match(/\/([^\/]+)\?/))) {
            setSrc = window.origImage.replace(matches[1],combo.image);
        }
    }

    if (imageEle.getAttribute('src')!=setSrc) {
        imageEle.setAttribute('src',setSrc);
    }

    return true;
}
	
function variant_option_changed()
{
    var selected_combo = get_selected_combo();
    if (selected_combo && selected_combo.enabled=='1') {
        set_product_price(selected_combo);
        set_product_image(selected_combo);
    }
    else {
        // see if they're all selected
        var gname,field,
            all_selected = true;
        for (var i in window.variant_data.variants) {
            gname = window.variant_data.variants[i].groupName.replace(/[^0-9A-z]/g,'');
            field = document.getElementById(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()
{
    var selected_combo = false;
    if (typeof window.variant_data.combos != 'undefined') {
        var combo,
            field,
            gname,
            selected;
        for (var i in window.variant_data.combos) {
            combo = window.variant_data.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(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_error(error)
{
    alert(error);
}
    
// set the product id from the href
(function()
{
    var matches;
    if ((matches = document.location.href.match(/itemId=([0-9]+)/))) {
        window.productId = matches[1];
    }
})();