window.addEvent('domready', function() {
    var productSizes = $$('select[name=templatePropertyValueId]')[0];
    if (productSizes != 'undefined' && productSizes == true) updateTemplateLinks(productSizes);
    setProductOptions();
    getShipping();

    try { setTotalSizes(); }
    catch(e) {}
});

function getShipping()
{
    var elements = $$('#orderForm *');
    var propertyValueIds   = '';
    var propertyValueNames = '<propertyValueNames>';

    if ($('id_shippingMethod'))
    {
        selectedIndex = $('id_shippingMethod').selectedIndex;
    }
    else
    {
        selectedIndex = 0;
    }


    for(var i = 0; i < elements.length; i++)
    {
        var shippingElementName = new RegExp("^shippingMethodId");
        var digits              = new RegExp("^[0-9]+$");
        var dropDownListName    = new RegExp("^properties(\[[0-9]+\]){2}$");
        var textName            = new RegExp("^propertyValueNames(?:\[[0-9]+\]){2}$");

        if (! elements[i] || elements[i] == null) continue;
        if (shippingElementName.exec(elements[i].get('name'))) { break; }

        if (dropDownListName.exec(elements[i].get('name')) && elements[i].value)
        {
            if (digits.exec(elements[i].value))
            {
                propertyValueIds = propertyValueIds + elements[i].value + ",";
            }
        }

        if ( textName.exec(elements[i].get('name')))
        {
            propertyValueNames = propertyValueNames + '<property id="' + elements[i].get('name') + '">' + elements[i].value + "</property>";
        }
    }
    propertyValueNames+= '</propertyValueNames>';

    propertyValueIds   = propertyValueIds.substr(0, propertyValueIds.length     - 1 );
    makeRequest('/core/views/website/pages/products/shipping-options/', 'POST', 'propertyValueIds='+propertyValueIds+'&propertyValueNames='+propertyValueNames+'&selectedIndex='+selectedIndex+'&iItem=' + iItem);
}

function getResponse(xml)
{
    try
    {
        xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
        xmlDoc.async = "false";
        xmlDoc.loadXML(xml);
    }
    catch(e)
    {
        parser = new DOMParser();
        xmlDoc = parser.parseFromString(xml, "text/xml");
    }

    switch (xmlDoc.childNodes[0].nodeName)
    {
        case 'pricing':
            $('id_shippingMethod').innerHTML = '';
            price          = xmlDoc.getElementsByTagName('price')[0].childNodes[0].nodeValue;
            shippingPrices = xmlDoc.getElementsByTagName('shippingPrice');

            if (selectedIndex == -1) {selectedIndex = 0;}

            for(var i = 0; i < shippingPrices.length; i++)
            {
                shippingPrice      = shippingPrices[i].childNodes[0].nodeValue;
                shippingMethodId   = shippingPrices[i].getAttribute('shippingMethodId');
                shippingMethodName = shippingPrices[i].getAttribute('shippingMethodName');
                if (selectedIndex == i) {selectedShippingPrice = shippingPrice;}

                shippingOption = new Option();
                shippingOption.innerHTML = shippingMethodName + ' - $' + shippingPrice;
                //shippingOption = xmlDoc.createElement('option');

                shippingOption.setAttribute('value', shippingMethodId);
                $('id_shippingMethod').appendChild(shippingOption);
            }
            $('id_shippingMethod').selectedIndex = selectedIndex;
            updateEstimates();

        break;

        case 'uploadProgress':
            var percentage = parseInt(xmlDoc.childNodes[0].firstChild.nodeValue);
            var barWidth   = parseInt(percentage * 3.3);
            if (barWidth > 330) {barWidth = 330}
            $('bar').style.width = barWidth + 'px';
            if (percentage == '100')
            {
                clearInterval(interval);
                window.location = '/shopping-cart/';
            }
        break;
    }
}

function updateEstimates()
{
    selectedIndex = ($('id_shippingMethod').selectedIndex);
    shippingPrice = shippingPrices[selectedIndex].childNodes[0].nodeValue;

    price         = parseFloat(price);
    shippingPrice = parseFloat(shippingPrice);
    totalCost     = price + shippingPrice;
    totalCost     = totalCost.toFixed(2);

    $('estShippingCost').getElement('ins').innerHTML = '$' + shippingPrice;
    $('estSubtotalCost').getElement('ins').innerHTML = '$' + price;
    $('estTotalCost').getElement('ins').innerHTML = '$' + totalCost;
}
