$(document).ready(function() {
    
    var validator = $("#hypocalc").validate({
                
                //errorClass: "hypocalc-error-message",

                rules: {
			suma: {
                            required: true,
                            number: true
                        },
			urok: {
                            required: true,
                            number: true
                        },
			doba: {
                            required: true,
                            number: true
                        }

		},
		messages: {
			suma: {
                            required: "Vložte sumu",
                            number: "Suma musí byť číslo"
                        },
			urok: {
                            required: "Vložte úrokovú sadzbu",
                            number: "Úroková sadzba musí byť číslo"
                        },
                        doba: {
                            required: "Vložte dobu splácania",
                            number: "Doba splácania musí byť číslo"
                        }
		},
                errorElement: "div",
                //errorContainer: "#messageBox ul",
                errorLabelContainer: "#messageBox",
                //errorClass: "error-message",
                //wrapper: "div",
                debug:true,

                submitHandler: function(form){
                    var suma = $("#suma").attr('value');
                    var urok = $("#urok").attr('value');
                    var doba = $("#doba").attr('value');
                    
                    $("#result").html("<b>Mesačná splátka hypotekárneho úveru predstavuje:</b> " + getResult(suma, urok, doba) + " EUR");
                }
	});

        return false;

});

/*
 * Zatial nefunguje, je potrebne stiahnit plugin Form
 */
function resetForm(){
    $("#suma").val("");
    $("#urok").val("");
    $("#doba").val(2);
    $("#result").html("");

}

function getResult(suma, urok, doba){
    
    suma = parseFloat(suma);
    urok = parseFloat(urok.replace(",", "."));
    doba = parseFloat(doba);

    urok = urok / 100;
    doba = doba * 12;

    urok = Math.pow((1+urok), (1/12))-1;

    v = 1 / (1 + urok);
    pow = Math.pow(v, doba);
    result = Math.round(suma * (urok / (1 - pow)));

    return result;
    
}


