function SprawdzFormularz(typ)
{
    var Imie = $('#imie');
    var Nazwisko = $('#nazwisko');
    if (typ == 0) {
        var KodPocztowy = $('#kodpocztowy');
        var Miasto = $('#miasto');
    }
    var Wojewodztwo = $('#wojewodztwo');
    var Telefon = $('#telefon');
    var Zgoda = $('#zgoda');
    //var Godziny = $('#godziny_kontaktu');
	
    var Komunikat = '';
    var OK = true;
	
    if (Imie.val().length == 0)
    {
        $('#imie_info').attr('class', "error");
        Komunikat += "Musisz podać imię.\n";
        Imie.focus();
        OK = false;
    }
    else
        $('#imie_info').attr('class', 'p_ok');
	
    if (Nazwisko.val().length == 0)
    {
        $('#nazwisko_info').attr('class', "error");
        Komunikat += "Musisz podać nazwisko.\n";
        if (OK)
            Nazwisko.focus();
        OK = false;
    }
    else
        $('#nazwisko_info').attr('class', 'p_ok');
    
    if (typ == 0) {
        if (KodPocztowy.val().length == 0)
        {
            $('#kodpocztowy_info').attr('class', "error");
            Komunikat += "Musisz podać kod pocztowy.\n";
            if (OK)
                KodPocztowy.focus();
            OK = false;
        }
        else if (!KodPocztowy.val().match('[0-9]{2}-[0-9]{3}'))
        {
            Komunikat += "Kod pocztowy musi być w formacie XX-YYY\n";
            if (OK)
                KodPocztowy.focus();
		
            $('#kodpocztowy_info').attr('class', 'error');
            OK = false;
        }
        else
            $('#kodpocztowy_info').attr('class', 'p_ok');
	
        if (Miasto.val().length == 0)
        {
            $('#miasto_info').attr('class', "error");
            Komunikat += "Musisz podać miasto.\n";
            if (OK)
                Miasto.focus();
            OK = false;
        }
        else
            $('#miasto_info').attr('class', 'p_ok');
    }
	
    if (Wojewodztwo.val() == 0)
    {
        $('#wojewodztwo_info').attr('class', "error");
        Komunikat += "Musisz podać województwo.\n";
        OK = false;
    }
    else
        $('#wojewodztwo_info').attr('class', 'p_ok');
    //
    //if (Godziny.val() == 0)
    //{
    //        $('#godziny_kontaktu_info').attr('class', "error");
    //        Komunikat += "Musisz podać godziny kontaktu.\n";
    //        OK = false;
    //    }
    //    else
    $('#godziny_kontaktu_info').attr('class', 'p_ok');

    if (Telefon.val().length == 0)
    {
        $('#telefon_info').attr('class', "error");
        Komunikat += "Musisz podać telefon.\n";
        if (OK)
            Telefon.focus();
        OK = false;
    }
    else
    if ((!Telefon.val().match('[0-9]{9}')) || (Telefon.val().length > 9))
    {
        $('#telefon_info').attr('class', 'error');
        Komunikat += "Telefon musi się składać z 9 cyfr.\n";
        if (OK)
            Telefon.focus();
        OK = false;
    }
    else
        $('#telefon_info').attr('class', 'p_ok');
	
    if (Zgoda.val() == 0)
    {
        $('#zgoda_info').attr('class', "error");
        Komunikat += "Musisz wyrazić zgodę na przesłanie informacji handlowej.";
        OK = false;
    }
    else
        $('#zgoda_info').attr('class', 'p_ok');
	
    if (!OK)
    {
        alert(Komunikat);
        return false;
    }
	
    if (confirm('Czy wysłać wniosek?')) {
        $('#contact_form').submit();
        return true;
    }
    
    return false;
}


function PrzeliczRate() {
    var metoda = '/Wniosek/PrzeliczRate';

    var ac = $('#ac').val();
    var nowy = $('#nowy').val();
    var zakup = $('#zakup').val();
    var liczba_rat = $('#liczba_rat').val();
    var rok_produkcji = $('#rok_produkcji').val();
    var kwota_kredytu = $('#kwota_kredytu').val();
    
    /*var parametry = '';
    parametry += '/liczba_rat/' + liczba_rat;
    parametry += '/rok_produkcji/' + rok_produkcji;
    parametry += '/kwota_kredytu/' + kwota_kredytu;
    parametry += '/ac/' + ac;
    parametry += '/zakup/' + zakup;*/

    $('#text_ukryty').html('<img src="/view/images/ajax-loader.gif" alt="Proszę czekać"/>&nbsp;Proszę czekać...');
    $.post(metoda, {
        "liczba_rat": liczba_rat,
        "rok_produkcji": rok_produkcji,
        "kwota_kredytu": kwota_kredytu,
        "nowy": nowy,
        "ac": ac,
        "zakup": zakup
    },
    function(data){
        $('#text_ukryty').html('');
        if (data.result == -1) {
            alert(data.message);
            return false;
        }
        else
        {
            //!
            if ( data.result == 1) {
                $('#kwota_raty').text(data.data[0]['Wplata']);
            //var tekst = 'ubezpieczenie:'+data.data[data.data.length-5]+', opr. nom.: '+data.data[data.data.length-6]+', IP:'+data.data[data.data.length-3]+', PB: '+data.data[data.data.length-4]+ ', Promocja: '+data.data[data.data.length-2]+', Brutto: '+data.data[data.data.length-1]
            //$('#text_ukryty').css('font-size' ,'10px');
            //$('#text_ukryty').text(''+tekst+'');
            //$('#text_ukryty').text('<p>Kwota brutto: ' + data[0]['Kredyt']+'</p>');
            }
            else {
                alert(data.message);
            }
        }
    }, "json"); 
    

    return false;
}

$(document).ready(function() {
    //$(".checkbox input").hide();
    $(".checkbox").click(function(){

        var ac = $('#ac').val();
        var bez_ac = $('#bez_ac').val();
        var nowy = $('#nowy').val();
        var uzywany = $('#uzywany').val();
        var zakup = $('#zakup').val();
        var przewlaszczenie = $('#przewlaszczenie').val();

        if ($(this).children("input").attr('id') == 'ac') {
            //alert('ac');
            if (bez_ac == 1 && ac == 0) {
                $('#bez_ac').parent().removeClass("checked");
                $('#bez_ac').parent().addClass("unchecked");
                $('#bez_ac').val(0);
            }
            else if (bez_ac == 0 && ac == 1) {
                $('#bez_ac').parent().removeClass("unchecked");
                $('#bez_ac').parent().addClass("checked");
                $('#bez_ac').val(1);
            }
        }

        if ($(this).children("input").attr('id') == 'bez_ac') {
            //alert('bez_ac');
            if (bez_ac == 1 && ac == 0) {
                $('#ac').parent().removeClass("unchecked");
                $('#ac').parent().addClass("checked");
                $('#ac').val(1);
            }
            else if (bez_ac == 0 && ac == 1) {
                $('#ac').parent().removeClass("checked");
                $('#ac').parent().addClass("unchecked");
                $('#ac').val(0);
            }
        }

        if ($(this).children("input").attr('id') == 'zakup') {
            //alert('ac');
            if (przewlaszczenie == 1 && zakup == 0) {
                $('#przewlaszczenie').parent().removeClass("checked");
                $('#przewlaszczenie').parent().addClass("unchecked");
                $('#przewlaszczenie').val(0);
            }
            else if (przewlaszczenie == 0 && zakup == 1) {
                $('#przewlaszczenie').parent().removeClass("unchecked");
                $('#przewlaszczenie').parent().addClass("checked");
                $('#przewlaszczenie').val(1);
            }
        }

        if ($(this).children("input").attr('id') == 'przewlaszczenie') {
            //alert('bez_ac');
            if (zakup == 0 && przewlaszczenie == 1) {
                $('#zakup').parent().removeClass("unchecked");
                $('#zakup').parent().addClass("checked");
                $('#zakup').val(1);
            }
            else if (zakup == 1 && przewlaszczenie == 0) {
                $('#zakup').parent().removeClass("checked");
                $('#zakup').parent().addClass("unchecked");
                $('#zakup').val(0);
            }
        }

        if ($(this).children("input").attr('id') == 'nowy') {
            //alert('ac');
            if (uzywany == 1 && nowy == 0) {
                $('#uzywany').parent().removeClass("checked");
                $('#uzywany').parent().addClass("unchecked");
                $('#bez_ac').val(0);
            }
            else if (uzywany == 0 && nowy == 1) {
                $('#uzywany').parent().removeClass("unchecked");
                $('#uzywany').parent().addClass("checked");
                $('#uzywany').val(1);
            }
        }

        if ($(this).children("input").attr('id') == 'uzywany') {
            //alert('bez_ac');
            if (uzywany == 1 && ac == 0) {
                $('#nowy').parent().removeClass("unchecked");
                $('#nowy').parent().addClass("checked");
                $('#nowy').val(1);
            }
            else if (uzywany == 0 && ac == 1) {
                $('#nowy').parent().removeClass("checked");
                $('#nowy').parent().addClass("unchecked");
                $('#nowy').val(0);
            }
        }
        
        if($(this).children("input").val()==1){
            // uncheck
            $(this).children("input").val(0);
            $(this).removeClass("checked");
            $(this).addClass("unchecked");
        }else{
            // check
            $(this).children("input").val(1);
            $(this).removeClass("unchecked");
            $(this).addClass("checked");
        }


    //alert($(this).children("input").attr("checked"));
    });

    $("#slider_kwota_kredytu").slider({
        value: 10000,
        min: 1000,
        max: 250000,
        step: 100,
        slide: function( event, ui ) {
            $( "#kwota_kredytu" ).val( ui.value );
        }
    });
    $( "#kwota_kredytu" ).val( $("#slider_kwota_kredytu").slider( "option", "value" ) );
    $("#slider_kwota_kredytu").removeClass('ui-corner-all');
    $("#slider_rok_produkcji").slider({
        value: 2009,
        min: 1991,
        max: 2011,
        step: 1,
        slide: function( event, ui ) {
            $( "#rok_produkcji" ).val( ui.value );
        }
    });
    $( "#rok_produkcji" ).val( $("#slider_rok_produkcji").slider( "option", "value" ) );
    $("#slider_rok_produkcji").removeClass('ui-corner-all');
    $("#slider_liczba_rat").slider({
        value:96,
        min: 12,
        max: 120,
        step: 1,
        slide: function( event, ui ) {
            $( "#liczba_rat" ).val( ui.value );
        }
    });
    $( "#liczba_rat" ).val( $("#slider_liczba_rat").slider( "option", "value" ) );
    $("#slider_liczba_rat").removeClass('ui-corner-all');
});

