﻿$(document).ready(function() {
    var SourceSumm = jQuery('div#summa b').html();
    SourceSumm = parseInt(SourceSumm);
    var summpost = jQuery('input#summforpost').val();
    summpost = parseInt(summpost);
	NewSumm = SourceSumm + summpost;
	jQuery('div.price-all').html(' Общая сумма: <b>' + NewSumm + '</b> руб.');

    $("select").change( function() {

        var delivery = $('#delivery option:selected').val();
        var NewSumm = 0;
        var SourceSumm = jQuery('div#summa b').html();
        SourceSumm = parseInt(SourceSumm);
        var payment = $('#payment option:selected').val();
        var summpost = jQuery('input#summforpost').val();
        summpost = parseInt(summpost);

        // считаем скидки
        if (payment == "Web Money"){
            SourceSumm = Math.round(SourceSumm * 0.93);
        }
        if (payment == "Yandex.Деньги"){
            SourceSumm = Math.round(SourceSumm * 0.93);
        }
        if (payment == "Rupay"){
            SourceSumm = Math.round(SourceSumm * 0.93);
        }
        if (payment == "Банковский платеж"){
            SourceSumm = Math.round(SourceSumm * 0.95);
        }
        if (payment == "Contact System"){
            SourceSumm = Math.round(SourceSumm * 1.05);
        }
        if (payment == "Western Union"){
            SourceSumm = Math.round(SourceSumm * 1.05);
        }


        if (delivery == "Ускоренная почтовая доставка"){
            NewSumm = SourceSumm + summpost;
            // $('select#payment option:contains("Наложенный платеж")').remove();
            $('#payment').append('<option value="Наложенный платеж" >Наложенный платеж (оплата при получении на почте)</option>');
            $('#payment').val("Наложенный платеж").attr('selected','selected');
            $('input[name=index]').val('');
            $('input[name=area]').val('');
            $('input[name=city]').val('');
            $('#address').val('');
            $('input[name=country]').val('Россия');
            $('#trcountry').show();
            $('#trindex').show();
            $('#trarea').show();
            $('#trcity').show();
            $('#traddress').show();
        }
        if (delivery == "Курьером по Москве и СПБ в пределах МКАД (КАД)"){
            NewSumm = SourceSumm + 200;
            // при доставке курьером нельзя платить наложенным
            $('select#payment option:contains("Наложенный платеж")').remove();
            $('input[name=index]').val('');
            $('input[name=area]').val('');
            $('input[name=city]').val('');
            $('#address').val('');
            $('input[name=country]').val('Россия');
            $('#trcountry').show();
            $('#trindex').show();
            $('#trarea').show();
            $('#trcity').show();
            $('#traddress').show();
        }
        if (delivery == "Транспортной компанией СПСР"){
            NewSumm = SourceSumm + 900;
            $('select#payment option:contains("Наложенный платеж")').remove();
            $('input[name=index]').val('');
            $('input[name=area]').val('');
            $('input[name=city]').val('');
            $('#address').val('');
            $('input[name=country]').val('Россия');
            $('#trcountry').show();
            $('#trindex').show();
            $('#trarea').show();
            $('#trcity').show();
            $('#traddress').show();
        }
        if (delivery == "Транспортной компанией DHL"){
            NewSumm = SourceSumm + 1600;
            $('select#payment option:contains("Наложенный платеж")').remove();
            $('input[name=index]').val('');
            $('input[name=area]').val('');
            $('input[name=city]').val('');
            $('#address').val('');
            $('input[name=country]').val('Россия');
            $('#trcountry').show();
            $('#trindex').show();
            $('#trarea').show();
            $('#trcity').show();
            $('#traddress').show();
        }
        if (delivery == "Экспресс почтовая доставка EMS"){
            NewSumm = SourceSumm + 1200;
            $('select#payment option:contains("Наложенный платеж")').remove();
            $('input[name=index]').val('');
            $('input[name=area]').val('');
            $('input[name=city]').val('');
            $('#address').val('');
            $('input[name=country]').val('Россия');
            $('#trcountry').show();
            $('#trindex').show();
            $('#trarea').show();
            $('#trcity').show();
            $('#traddress').show();
        }
        if ((delivery == "svm2")||(delivery == "svm3")||(delivery == "svm4")||(delivery == "svm5")||(delivery == "svm6")||(delivery == "svm7")||(delivery == "svm8")){
            $('#payment').val("on").attr('selected','selected');
            NewSumm = SourceSumm;
            $('input[name=index]').val('000000');
            $('input[name=area]').val('Москва');
            $('input[name=city]').val('Москва');
            $('#address').val('самовывоз');
            $('input[name=country]').val('Россия');
            $('#trcountry').hide();
            $('#trindex').hide();
            $('#trarea').hide();
            $('#trcity').hide();
            $('#traddress').hide();
        }
        if ((delivery == "svspb1")||(delivery == "svspb2")||(delivery == "svspb3")||(delivery == "svspb6")){
            $('#payment').val("on").attr('selected','selected');
            NewSumm = SourceSumm;
            $('input[name=index]').val('000000');
            $('input[name=area]').val('Санкт-Петербург');
            $('input[name=city]').val('Санкт-Петербург');
            $('#address').val('самовывоз');
            $('input[name=country]').val('Россия');
            $('#trcountry').hide();
            $('#trindex').hide();
            $('#trarea').hide();
            $('#trcity').hide();
            $('#traddress').hide();
        }
        if (delivery == "svnn1"){
            $('#payment').val("on").attr('selected','selected');
            NewSumm = SourceSumm;
            $('input[name=index]').val('000000');
            $('input[name=area]').val('Нижний Новгород');
            $('input[name=city]').val('Нижний Новгород');
            $('#address').val('самовывоз');
            $('input[name=country]').val('Россия');
            $('#trcountry').hide();
            $('#trindex').hide();
            $('#trarea').hide();
            $('#trcity').hide();
            $('#traddress').hide();
        }
        if (delivery == "svkl1"){
            $('#payment').val("on").attr('selected','selected');
            NewSumm = SourceSumm;
            $('input[name=index]').val('000000');
            $('input[name=area]').val('Калуга');
            $('input[name=city]').val('Калуга');
            $('#address').val('самовывоз');
            $('input[name=country]').val('Россия');
            $('#trcountry').hide();
            $('#trindex').hide();
            $('#trarea').hide();
            $('#trcity').hide();
            $('#traddress').hide();
        }

        jQuery('div.price-all').html(' Общая сумма: <b>' + NewSumm + '</b> руб.');

    })
// .change();
})

