function changeMe()
{
    var types           = new Array();
    var surfaces        = new Array();
    var type            = 0;
    var surface         = 0;
    var price           = 0;
    var priceOption     = 0;
    var percentOption   = 0;
    var totalBase       = 0;
    var totalOption     = 0;
    var totalOptions    = 0;
    var totalOptionsTmp = 0;
    var total           = 0;
    var rate            = 0.2;


    surfaces[0] = 150;


    surfaces[1] = 200;


    surfaces[2] = 250;


    surfaces[3] = 400;


    surfaces[4] = 600;


    surfaces[5] = 800;


    surfaces[6] = 1200;


    surfaces[7] = 2000;



    types[0] = new Array(3);
    types[0][0] = 3850;
    types[0][1] = 4100;
    types[0][2] = 4850;


    types[1] = new Array(3);
    types[1][0] = 3650;
    types[1][1] = 3920;
    types[1][2] = 4620;


    types[2] = new Array(3);
    types[2][0] = 3470;
    types[2][1] = 3750;
    types[2][2] = 4400;


    types[3] = new Array(3);
    types[3][0] = 3300;
    types[3][1] = 3550;
    types[3][2] = 4100;


    types[4] = new Array(3);
    types[4][0] = 3140;
    types[4][1] = 3390;
    types[4][2] = 3990;


    types[5] = new Array(3);
    types[5][0] = 2990;
    types[5][1] = 3220;
    types[5][2] = 3800;


    types[6] = new Array(3);
    types[6][0] = 2850;
    types[6][1] = 3060;
    types[6][2] = 3630;


    types[7] = new Array(3);
    types[7][0] = 2700;
    types[7][1] = 2910;
    types[7][2] = 3450;


    types[8] = new Array(3);
    types[8][0] = 2600;
    types[8][1] = 2800;
    types[8][2] = 3350;


    if ($('#budget_nb_m2').val()) {
        surface = parseInt($('#budget_nb_m2').val());
        price = 0;
        for (var i = 0; i < 8; i++) {
            if (surface >= surfaces[i]) {
                price = price +1;
            }
        }
    }

    if ($('input[@name=budget_type_finition]:checked').val()) {
        type = parseInt($('input[@name=budget_type_finition]:checked').val());
    }

    totalBase = surface * types[price][type];

    totalBase = totalBase + totalBase * rate;

    $('#budget_base_price').val((totalBase ? totalBase : ''));

    formatPrice($('#budget_base_price'));


    if ($('#budget_options_outdoor_pool_nat:checked').val()) {
        priceOption     = parseInt($('#budget_options_price_outdoor_pool_nat').val());
        percentOption   = parseInt($('#budget_options_percent_outdoor_pool_nat').val());
        totalOption     = priceOption + parseInt(totalBase * percentOption / 100);

        $('#budget_options_total_outdoor_pool_nat').val((totalOption ? totalOption : ''));

        totalOptionsTmp = parseInt($('#budget_options_total_outdoor_pool_nat').val());

        totalOptions += (totalOptionsTmp ? totalOptionsTmp : 0);
    } else {
        $('#budget_options_total_outdoor_pool_nat').val('');
    }

    formatPrice($('#budget_options_total_outdoor_pool_nat'));


    if ($('#budget_options_outdoor_pool_conv:checked').val()) {
        priceOption     = parseInt($('#budget_options_price_outdoor_pool_conv').val());
        percentOption   = parseInt($('#budget_options_percent_outdoor_pool_conv').val());
        totalOption     = priceOption + parseInt(totalBase * percentOption / 100);

        $('#budget_options_total_outdoor_pool_conv').val((totalOption ? totalOption : ''));

        totalOptionsTmp = parseInt($('#budget_options_total_outdoor_pool_conv').val());

        totalOptions += (totalOptionsTmp ? totalOptionsTmp : 0);
    } else {
        $('#budget_options_total_outdoor_pool_conv').val('');
    }

    formatPrice($('#budget_options_total_outdoor_pool_conv'));


    if ($('#budget_options_zone_ext:checked').val()) {
        priceOption     = parseInt($('#budget_options_price_zone_ext').val());
        percentOption   = parseInt($('#budget_options_percent_zone_ext').val());
        totalOption     = priceOption + parseInt(totalBase * percentOption / 100);

        $('#budget_options_total_zone_ext').val((totalOption ? totalOption : ''));

        totalOptionsTmp = parseInt($('#budget_options_total_zone_ext').val());

        totalOptions += (totalOptionsTmp ? totalOptionsTmp : 0);
    } else {
        $('#budget_options_total_zone_ext').val('');
    }

    formatPrice($('#budget_options_total_zone_ext'));


    if ($('#budget_options_minergie:checked').val()) {
        priceOption     = parseInt($('#budget_options_price_minergie').val());
        percentOption   = parseInt($('#budget_options_percent_minergie').val());
        totalOption     = priceOption + parseInt(totalBase * percentOption / 100);

        $('#budget_options_total_minergie').val((totalOption ? totalOption : ''));

        totalOptionsTmp = parseInt($('#budget_options_total_minergie').val());

        totalOptions += (totalOptionsTmp ? totalOptionsTmp : 0);
    } else {
        $('#budget_options_total_minergie').val('');
    }

    formatPrice($('#budget_options_total_minergie'));


    if ($('#budget_options_minergie_p:checked').val()) {
        priceOption     = parseInt($('#budget_options_price_minergie_p').val());
        percentOption   = parseInt($('#budget_options_percent_minergie_p').val());
        totalOption     = priceOption + parseInt(totalBase * percentOption / 100);

        $('#budget_options_total_minergie_p').val((totalOption ? totalOption : ''));

        totalOptionsTmp = parseInt($('#budget_options_total_minergie_p').val());

        totalOptions += (totalOptionsTmp ? totalOptionsTmp : 0);
    } else {
        $('#budget_options_total_minergie_p').val('');
    }

    formatPrice($('#budget_options_total_minergie_p'));


    if ($('#budget_options_lift:checked').val()) {
        priceOption     = parseInt($('#budget_options_price_lift').val());
        percentOption   = parseInt($('#budget_options_percent_lift').val());
        totalOption     = priceOption + parseInt(totalBase * percentOption / 100);

        $('#budget_options_total_lift').val((totalOption ? totalOption : ''));

        totalOptionsTmp = parseInt($('#budget_options_total_lift').val());

        totalOptions += (totalOptionsTmp ? totalOptionsTmp : 0);
    } else {
        $('#budget_options_total_lift').val('');
    }

    formatPrice($('#budget_options_total_lift'));


    if ($('#budget_options_automation:checked').val()) {
        priceOption     = parseInt($('#budget_options_price_automation').val());
        percentOption   = parseInt($('#budget_options_percent_automation').val());
        totalOption     = priceOption + parseInt(totalBase * percentOption / 100);

        $('#budget_options_total_automation').val((totalOption ? totalOption : ''));

        totalOptionsTmp = parseInt($('#budget_options_total_automation').val());

        totalOptions += (totalOptionsTmp ? totalOptionsTmp : 0);
    } else {
        $('#budget_options_total_automation').val('');
    }

    formatPrice($('#budget_options_total_automation'));


    if ($('#budget_options_audio:checked').val()) {
        priceOption     = parseInt($('#budget_options_price_audio').val());
        percentOption   = parseInt($('#budget_options_percent_audio').val());
        totalOption     = priceOption + parseInt(totalBase * percentOption / 100);

        $('#budget_options_total_audio').val((totalOption ? totalOption : ''));

        totalOptionsTmp = parseInt($('#budget_options_total_audio').val());

        totalOptions += (totalOptionsTmp ? totalOptionsTmp : 0);
    } else {
        $('#budget_options_total_audio').val('');
    }

    formatPrice($('#budget_options_total_audio'));


    if ($('#budget_options_video:checked').val()) {
        priceOption     = parseInt($('#budget_options_price_video').val());
        percentOption   = parseInt($('#budget_options_percent_video').val());
        totalOption     = priceOption + parseInt(totalBase * percentOption / 100);

        $('#budget_options_total_video').val((totalOption ? totalOption : ''));

        totalOptionsTmp = parseInt($('#budget_options_total_video').val());

        totalOptions += (totalOptionsTmp ? totalOptionsTmp : 0);
    } else {
        $('#budget_options_total_video').val('');
    }

    formatPrice($('#budget_options_total_video'));


    if ($('#budget_options_fireplace:checked').val()) {
        priceOption     = parseInt($('#budget_options_price_fireplace').val());
        percentOption   = parseInt($('#budget_options_percent_fireplace').val());
        totalOption     = priceOption + parseInt(totalBase * percentOption / 100);

        $('#budget_options_total_fireplace').val((totalOption ? totalOption : ''));

        totalOptionsTmp = parseInt($('#budget_options_total_fireplace').val());

        totalOptions += (totalOptionsTmp ? totalOptionsTmp : 0);
    } else {
        $('#budget_options_total_fireplace').val('');
    }

    formatPrice($('#budget_options_total_fireplace'));


    if ($('#budget_options_bloc:checked').val()) {
        priceOption     = parseInt($('#budget_options_price_bloc').val());
        percentOption   = parseInt($('#budget_options_percent_bloc').val());
        totalOption     = priceOption + parseInt(totalBase * percentOption / 100);

        $('#budget_options_total_bloc').val((totalOption ? totalOption : ''));

        totalOptionsTmp = parseInt($('#budget_options_total_bloc').val());

        totalOptions += (totalOptionsTmp ? totalOptionsTmp : 0);
    } else {
        $('#budget_options_total_bloc').val('');
    }

    formatPrice($('#budget_options_total_bloc'));


    if ($('#budget_options_tepaniaki:checked').val()) {
        priceOption     = parseInt($('#budget_options_price_tepaniaki').val());
        percentOption   = parseInt($('#budget_options_percent_tepaniaki').val());
        totalOption     = priceOption + parseInt(totalBase * percentOption / 100);

        $('#budget_options_total_tepaniaki').val((totalOption ? totalOption : ''));

        totalOptionsTmp = parseInt($('#budget_options_total_tepaniaki').val());

        totalOptions += (totalOptionsTmp ? totalOptionsTmp : 0);
    } else {
        $('#budget_options_total_tepaniaki').val('');
    }

    formatPrice($('#budget_options_total_tepaniaki'));


    if ($('#budget_options_aspirator:checked').val()) {
        priceOption     = parseInt($('#budget_options_price_aspirator').val());
        percentOption   = parseInt($('#budget_options_percent_aspirator').val());
        totalOption     = priceOption + parseInt(totalBase * percentOption / 100);

        $('#budget_options_total_aspirator').val((totalOption ? totalOption : ''));

        totalOptionsTmp = parseInt($('#budget_options_total_aspirator').val());

        totalOptions += (totalOptionsTmp ? totalOptionsTmp : 0);
    } else {
        $('#budget_options_total_aspirator').val('');
    }

    formatPrice($('#budget_options_total_aspirator'));


    if ($('#budget_options_cold_room:checked').val()) {
        priceOption     = parseInt($('#budget_options_price_cold_room').val());
        percentOption   = parseInt($('#budget_options_percent_cold_room').val());
        totalOption     = priceOption + parseInt(totalBase * percentOption / 100);

        $('#budget_options_total_cold_room').val((totalOption ? totalOption : ''));

        totalOptionsTmp = parseInt($('#budget_options_total_cold_room').val());

        totalOptions += (totalOptionsTmp ? totalOptionsTmp : 0);
    } else {
        $('#budget_options_total_cold_room').val('');
    }

    formatPrice($('#budget_options_total_cold_room'));


    if ($('#budget_options_wellness:checked').val()) {
        priceOption     = parseInt($('#budget_options_price_wellness').val());
        percentOption   = parseInt($('#budget_options_percent_wellness').val());
        totalOption     = priceOption + parseInt(totalBase * percentOption / 100);

        $('#budget_options_total_wellness').val((totalOption ? totalOption : ''));

        totalOptionsTmp = parseInt($('#budget_options_total_wellness').val());

        totalOptions += (totalOptionsTmp ? totalOptionsTmp : 0);
    } else {
        $('#budget_options_total_wellness').val('');
    }

    formatPrice($('#budget_options_total_wellness'));


    if ($('#budget_options_sauna:checked').val()) {
        priceOption     = parseInt($('#budget_options_price_sauna').val());
        percentOption   = parseInt($('#budget_options_percent_sauna').val());
        totalOption     = priceOption + parseInt(totalBase * percentOption / 100);

        $('#budget_options_total_sauna').val((totalOption ? totalOption : ''));

        totalOptionsTmp = parseInt($('#budget_options_total_sauna').val());

        totalOptions += (totalOptionsTmp ? totalOptionsTmp : 0);
    } else {
        $('#budget_options_total_sauna').val('');
    }

    formatPrice($('#budget_options_total_sauna'));


    if ($('#budget_options_hammam:checked').val()) {
        priceOption     = parseInt($('#budget_options_price_hammam').val());
        percentOption   = parseInt($('#budget_options_percent_hammam').val());
        totalOption     = priceOption + parseInt(totalBase * percentOption / 100);

        $('#budget_options_total_hammam').val((totalOption ? totalOption : ''));

        totalOptionsTmp = parseInt($('#budget_options_total_hammam').val());

        totalOptions += (totalOptionsTmp ? totalOptionsTmp : 0);
    } else {
        $('#budget_options_total_hammam').val('');
    }

    formatPrice($('#budget_options_total_hammam'));


    if ($('#budget_options_indoor_pool:checked').val()) {
        priceOption     = parseInt($('#budget_options_price_indoor_pool').val());
        percentOption   = parseInt($('#budget_options_percent_indoor_pool').val());
        totalOption     = priceOption + parseInt(totalBase * percentOption / 100);

        $('#budget_options_total_indoor_pool').val((totalOption ? totalOption : ''));

        totalOptionsTmp = parseInt($('#budget_options_total_indoor_pool').val());

        totalOptions += (totalOptionsTmp ? totalOptionsTmp : 0);
    } else {
        $('#budget_options_total_indoor_pool').val('');
    }

    formatPrice($('#budget_options_total_indoor_pool'));


    $('#budget_options_price').val((totalOptions ? totalOptions : ''));

    formatPrice($('#budget_options_price'));

    total = parseInt(totalBase) + parseInt(totalOptions);

    $('#budget_total_price').val((total ? total : ''));

    formatPrice($('#budget_total_price'));
}


function removeQuotes(string)
{
    return string.replace(/'/g, "");
}

function removeQuotesPrice(input)
{
    var price = input.val();

    input.val(removeQuotes(price));
}

function formatPrice(input, isFloat, sfx)
{
    var value    = input.val();
    var position = 0;
    var decimal  = '';
    var counter  = 0;
    var ret      = '';
    var i;

    position = value.indexOf('.');

    if (position > 0)
    {
        decimal = value.substr(position + 1);
        value   = value.substr(0, position);
    }

    for (i = value.length - 1; i >= 0; i--)
    {
        if (value.charAt(i) == parseInt(value.charAt(i)) + '')
        {
            if (counter % 3 == 0 && counter != 0) {
                ret = '\'' + ret;
            }
            ret = parseInt(value.charAt(i)) + ret;
            counter++;
        }
    }

    if (isFloat && parseInt(decimal)) {
        ret += '.' + decimal;
    }

    if (sfx) {
        ret += sfx;
    }

    input.val(ret);
}

function formatAllPrices(form)
{
    $('.price').change(function()
    {
        formatPrice($(this), 0);
    }
    );
    $('.price').trigger('change');

    if (form) {
        $('#' + form).submit(function()
        {
            $('.price').each(function()
            {
                removeQuotesPrice($(this));
            }
            );
        }
        );
    }
}

function checkBtn(btn, checkbox, byclass, trigger)
{
    var btn = '#' + btn;

    var info = '';

    if (byclass) {
        info = 'class';
    } else {
        info = 'name';
    }

    $("input[" + info + "='" + checkbox + "']").each(function()
    {
        if ($(btn + ':checked').val()) {
            this.checked = true;
        } else {
            this.checked = false;
        }

        if (trigger) $(this).trigger(trigger);
    }
    );
}
