function setMainImage(src,large) {
    $('mainImage').src = src;
    largeImage = large;
}

function initProductDetails() {

    var minPrice = null;
    var fromPriceHTML = null;
    var allEqual = true;
    var firstPrice = null;
    var gotAnyPrices = false;

    // Display a "From $xx.xx" value in the from field if we have any options
    for (var stockCode in productOptionDisplayPrices) {
        gotAnyPrices = true;
        if (firstPrice === null) {
            firstPrice = productOptionDisplayPrices[stockCode].v;
        } else {
            if (firstPrice != productOptionDisplayPrices[stockCode].v) {
                allEqual = false;
            }
        }
        if (minPrice === null || parseInt(productOptionDisplayPrices[stockCode].v, 10) < parseInt(minPrice.v, 10)) {
            minPrice = productOptionDisplayPrices[stockCode];
        }
    }
    if (minPrice !== null) {
        fromPriceHTML = (allEqual ? '' : 'From ') + minPrice.d;
    } else {
        fromPriceHTML = 'Unknown';
    }
    if (gotAnyPrices) {
        $('product-price').innerHTML = fromPriceHTML;
    }

    // Set up product swatch image rollover handlers
    for (var otIndex in productOptionTypes) {
        var ot = productOptionTypes[otIndex];
        for (var ovIndex in ot.Values) {
            var id = ot.Values[ovIndex].ID;
            if ($('product-details-option-image-' + id) && $('product-details-option-swatch-' + id)) {
                registerEvent($('product-details-option-swatch-' + id), 'mouseover', getProductOptionImageChangeFuction(id));
            }
        }
    }

    // Set up product options event handlers
    var el = $('substockcode');
    var form = $('product-details-form');
    if (el) {

        if (el.options) {
            // Then we're using a single select list
            el.selectedIndex = 0;
            registerEvent(el, 'change', function() {
                var sc = el.options[el.selectedIndex].value;
                if (sc.length) {
                    $('product-price').innerHTML = productOptionDisplayPrices[sc].d;
                } else {
                    $('product-price').innerHTML = fromPriceHTML;
                }
            });

            registerEvent(form, 'submit', function(event) {
                var e = fixEvent(event);
                if (el.selectedIndex === 0) {
                    alert('To add the product to your basket, please first select from one of the available options.');
                    // Prevent the form from submitting
                    if (e.preventDefault) {
                        e.preventDefault();
                    } else {
                        e.returnValue = false;
                    }
                }

            });

        } else {

            // Using a hidden input for the sub stock code, because of multiple option types
            registerEvent(form, 'submit', function(event) {
                var e = fixEvent(event);
                if (el.value.length === 0) {
                    alert('To add the product to your basket, please first select from the available options.');
                    // Prevent the form from submitting
                    if (e.preventDefault) {
                        e.preventDefault();
                    } else {
                        e.returnValue = false;
                    }
                }

            });

            for (var index in productOptionTypes) {
                var ot = productOptionTypes[index];
                var opEl = $('product-option-' + ot.ID);
                if (opEl) {
                    registerEvent(opEl, 'change', getProductOptionOnChangeFunction(ot.ID, fromPriceHTML));
                }
            }

        }
    }

}

function productOptionImageChange(id) {
    var imageElementID = 'product-details-option-image-' + id;
    if (productDetailsCurrentImage != imageElementID && $(imageElementID)) {
        // Hide the old
        $(productDetailsCurrentImage).style.display = 'none';

        // Display the new
        productDetailsCurrentImage = imageElementID;
        $(productDetailsCurrentImage).style.display = 'block';
    }
}

function getProductOptionImageChangeFuction(id) {
    return function() {
        productOptionImageChange(id);
    }
}

function getProductOptionOnChangeFunction(id, fromPriceHTML) {
    return function() {
        var thisEl = $('product-option-' + id);
        var thisSelectedValue = thisEl.options[thisEl.selectedIndex].value;
        if (thisSelectedValue == '') {

            // Reset all the lists
            for (var index in productOptionTypes) {
                var ot = productOptionTypes[index];

                var el = $('product-option-' + ot.ID);
                var oldPleaseSelect = el.options[0].text;
                removeOptions(el);


                addOption(el, oldPleaseSelect, '');
                el.selectedIndex = 0;
                for (var valueIndex in ot.Values) {
                    var val = ot.Values[valueIndex];
                    addOption(el, val.Name, val.ID);
                }

            }
            $('product-price').innerHTML = fromPriceHTML;

        } else {
            productOptionImageChange(thisSelectedValue);
            var hasOtherOptions = false;
            for (var index in productOptionTypes) {
                var ot = productOptionTypes[index];

                if (id != ot.ID) {
                    hasOtherOptions = true;
                    var el = $('product-option-' + ot.ID);

                    // Store the old selected index for the list
                    var oldVal = el.options[el.selectedIndex].value;

                    var oldPleaseSelect = el.options[0].text;
                    removeOptions(el);

                    addOption(el, oldPleaseSelect, '');

                    // Loop through the option values for this option type and if it is available for this
                    // product with the currently selected value then show it in the list.
                    for (var valueIndex in ot.Values) {

                        var val = ot.Values[valueIndex];

                        for (var optIndex in productOptions) {

                            if (val.ID == productOptions[optIndex]['OptionLink_' + ot.ID] &&
                                thisSelectedValue == productOptions[optIndex]['OptionLink_' + id]) {

                                addOption(el, val.Name, val.ID);

                                if (oldVal == val.ID) {
                                    // Reselect the old selected value (if this is the correct one)
                                    el.selectedIndex = el.options.length-1;

                                    // Set the price and stock code, because this is the combination of the 2 options
                                    $('product-price').innerHTML = productOptionDisplayPrices[productOptions[optIndex].SubStockCode].d;
                                    $('substockcode').value = productOptions[optIndex].SubStockCode;
                                }

                            }
                        }
                    }

                }
            }
            if (!hasOtherOptions) {
                for (var optIndex in productOptions) {

                    if (thisSelectedValue == productOptions[optIndex]['OptionLink_' + id]) {
                        // Set the price and stock code, because there is only 1 option
                        $('product-price').innerHTML = productOptionDisplayPrices[productOptions[optIndex].SubStockCode].d;
                        $('substockcode').value = productOptions[optIndex].SubStockCode;
                    }
                }
            }
        }
    };
}
