swfobject.embedSWF("/php/kredyty-i-inwestycje/ofc/open-flash-chart.swf", "my_chart", "600", "240","9.0.0", "expressInstall.swf",{"data-file": encodeURIComponent('/php/kredyty-i-inwestycje/kalkulator.php?loanAmount=200000&loanPeriod=20&loanInterest=6&investmentAmount=300&investmentPeriod=20&investmentInterest=7&si=1'), "loading":"Rysowanie wykresu ..."} );

var si = false;
function recalculate() {
	if($('#l_amount').val() < 1 || $('#l_period').val() <1 || $('#l_interest').val() <1 || $('#i_amount').val() <1 || $('#i_period').val() < 1 || $('i_interest').val() <1) {
		alert('Aby zobaczyć symulację wszystkie pola muszą być wypełnione, a ich wartości powinny być większe od zera');
		return false;
	}
    var url = '/php/kredyty-i-inwestycje/kalkulator.php?';
    url += 'loanAmount=' + document.getElementById('l_amount').value;
    url += '&loanPeriod=' + document.getElementById('l_period').value;
    url += '&loanInterest=' + document.getElementById('l_interest').value;

    url += '&investmentAmount=' + document.getElementById('i_amount').value;
    url += '&investmentPeriod=' + document.getElementById('i_period').value;
    url += '&investmentInterest=' + document.getElementById('i_interest').value;

    if (si) {
        si = false;
        url += '&si=1';
    } else {
        si = true;
        setTimeout(recalculate, 1000);
    }

    findSWF('my_chart').reload(url);
    getResults();
}

function findSWF(movieName) {
    if (navigator.appName.indexOf("Microsoft")!= -1) {
//         return window["ie_" + movieName];
		return window[movieName];
    } else {
        return document[movieName];
    }
}

function getResults() {
    var url="/php/kredyty-i-inwestycje/tabela.php?";
    url += 'loanAmount=' + document.getElementById('l_amount').value;
    url += '&loanPeriod=' + document.getElementById('l_period').value;
    url += '&loanInterest=' + document.getElementById('l_interest').value;

    url += '&investmentAmount=' + document.getElementById('i_amount').value;
    url += '&investmentPeriod=' + document.getElementById('i_period').value;
    url += '&investmentInterest=' + document.getElementById('i_interest').value;



    $.get(url, {}, function(response) {
        $("#result").html(response);
    });
}

function getIMG() {
	return '<img class="chart hidden" src="data:image/png;base64,' + $('#my_chart')[0].get_img_binary() + '" >';
}

$(document).ready(function() {
    getResults();
    $(".print").click(function() {
		$("#kalkulator .flash .chart").remove();
		$("#kalkulator .flash").append(getIMG());
		window.print();
    });
});

