window.addEvent('domready', function() {
    getShipping();
});

function onSubmit()
{
    if ($$('#orderForm li.jobName input')[0].value == '')
    {
        alert('Please Enter Job Name');
        return false;
    }
}

function getPropertyValueIds()
{
    var elements = $$('#orderForm *');
    var propertyValueIds = '';
    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}$");

        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 + ",";
            }
        }
    }
    return propertyValueIds.substr(0, propertyValueIds.length - 1 );
}

function getShipping()
{
    $('estimate').addClass('disable');

    selectedIndex = $('id_shippingMethod').selectedIndex;
    var propertyValueIds = getPropertyValueIds();

    var myRequest = new Request
    ({
        url: '/core/views/website/pages/products/shipping-options/',
        method: 'post',
        data: 'propertyValueIds='+propertyValueIds+'&selectedIndex='+selectedIndex+'&iItem=' + iItem,
        onSuccess: function(response)
        {
            $('id_shippingMethod').empty();
            var oPricing = JSON.decode(response);
            for(var shippingMethodId = 1; shippingMethodId <= 4; shippingMethodId++)
            {
                if (oPricing['selectedIndex'] == shippingMethodId) {selectedShippingPrice = oPricing.shipping[shippingMethodId].price;}
                shippingOption = new Option();
                shippingOption.innerHTML = oPricing.shipping[shippingMethodId].name + ' - $' + oPricing.shipping[shippingMethodId].price;
                shippingOption.setAttribute('value', shippingMethodId);
                $('id_shippingMethod').appendChild(shippingOption);
            }
            $('id_shippingMethod').selectedIndex = oPricing['selectedIndex'];
            updateEstimates(oPricing);
        }
    });
    myRequest.send();
}

function updateEstimates(oPricing)
{
    var totalAmount = 0;
    var shippingPrice = oPricing.shipping[selectedIndex + 1].price;

    $('estimate').empty();
    $('estimate').removeClass('disable');
    for (var priceFactorName in oPricing.pricing) 
    {
        var price = parseFloat(parseFloat(oPricing.pricing[priceFactorName]).toFixed(2));
        var tr = new Element('tr');
        var td1 = new Element('td', {'class':'label'})
        var td2 = new Element('td', {'class':'value'})
        td1.set('text', priceFactorName + ':');
        td2.set('html', '<ins>' + price.toFixed(2) + '</ins>');
        td1.inject(tr);
        td2.inject(tr);
        tr.inject($('estimate'));
        totalAmount+= price;
    }

    shippingPrice = parseFloat(parseFloat(shippingPrice).toFixed(2));
    totalAmount+= shippingPrice;

    var quantityList = $$('#orderBox li.quantity select')[0];
    var quantity = quantityList.options[quantityList.selectedIndex].text;
    quantity = parseInt(quantity.replace(',', ''));
    var rate = totalAmount / quantity;

    var tr1 = new Element('tr');
    var td1 = new Element('td', {'class':'label'});
    var td2 = new Element('td', {'class':'value'});
    td1.set('text', 'Shipping:');
    td2.set('html', '<ins>' + shippingPrice.toFixed(2) + '</ins>');
    td1.inject(tr1); td2.inject(tr1);

    var tr2 = new Element('tr', {'class':'total'});
    var td1 = td1.clone();
    var td2 = td2.clone();
    td1.set('text', 'Total:');
    td2.set('html', '<ins>' + totalAmount.toFixed(2) + '</ins>');
    td1.inject(tr2); td2.inject(tr2);

    var tr3 = new Element('tr', {'class':'rate'});
    var td1 = td1.clone();
    var td2 = td2.clone();
    td1.set('text', '');
    td2.set('text', rate.toFixed(2) + ' per piece');
    td1.inject(tr3); td2.inject(tr3);

    tr1.inject($('estimate'));
    tr2.inject($('estimate'));
    tr3.inject($('estimate'));
   
}
