var FriendMail = Class.create({
	formId:     'sendLink',
	
	initialize: function() {
        //
    },

    send: function() {
    	$('submit').disabled = true;
    	var pars = $(this.formId).serialize();
        //new Ajax.Request('http://localhost/etrend/services/SendUrlToFriend.xml', {
        new Ajax.Request('services/SendUrlToFriend.xml', {
            method: 'get',
            //			parameters: {'from_name':$('from_name').value, 'email':$('to_email').value, 'message':$('message_to_text').value, 'url':$('actual_url_input').value, 'article_uid':$('article_uid').value},
            parameters: pars,
            onSuccess: function(response) {
        		$('submit').disabled = false;
        		var xmlDoc = response.responseXML;
                if (!xmlDoc) {
                    alert('Nepodarilo sa spracovať odpoveď zo servera');
                    return;
                }

                var errors = xmlDoc.getElementsByTagName('error');

                if (errors.length > 0) {
                	if (errors.length == 1) {
        				var error = errors[0].attributes.getNamedItem('message').nodeValue + "\n";
        				var parts = error.split("\n");
        				alert(parts[0]);
        				if (parts.length == 3) {
        					$('captcha_wrapper').innerHTML = parts[1];
        				}
        			} else {
        				var result = '';
        				for (var i = 0; i < errors.length; i++) {
        					result += errors[i].attributes.getNamedItem('message').nodeValue + "\n";
        				}
        				alert(result);
        				return;
        			}
                }

                var response = xmlDoc.getElementsByTagName('message');
                var r = response[0];
                var text = r.attributes.getNamedItem("text").nodeValue;
                alert(text);
            },
            onFailure: function() {
                alert('Na serveri vznikla chyba. Nepodarilo sa odoslať link vášmu priatelovi. Skúste to prosím znovu');
                $('submit').disabled = false;
            }
        });

    }
});

var SendCorrectionMail = Class.create({
	formId:     'sendCorrection',
	
	initialize: function() {},

    send: function() {
    	$('send_correction_submit').disabled = true;
    	var pars = $(this.formId).serialize();
        //new Ajax.Request('http://localhost/etrend/services/SendCorrection.xml', {
        new Ajax.Request('services/SendCorrection.xml', {
            method: 'get',
            parameters: pars,
            onSuccess: function(response) {
        		$('send_correction_submit').disabled = false;
        		var xmlDoc = response.responseXML;
                if (!xmlDoc) {
                    alert('Nepodarilo sa spracovať odpoveď zo servera');
                    return;
                }

                var errors = xmlDoc.getElementsByTagName('error');

                if (errors.length > 0) {
                	if (errors.length == 1) {
        				var error = errors[0].attributes.getNamedItem('message').nodeValue + "\n";
        				var parts = error.split("\n");
        				alert(parts[0]);
        				if (parts.length == 3) {
        					$('captcha_wrapper_error').innerHTML = parts[1];
        				}
        			} else {
        				var result = '';
        				for (var i = 0; i < errors.length; i++) {
        					result += errors[i].attributes.getNamedItem('message').nodeValue + "\n";
        				}
        				alert(result);
        				return;
        			}
                }

                var response = xmlDoc.getElementsByTagName('message');
                var r = response[0];
                var text = r.attributes.getNamedItem("text").nodeValue;
                alert(text);
            },
            onFailure: function() {
                alert('Na serveri vznikla chyba. Nepodarilo sa odoslať link vášmu priatelovi. Skúste to prosím znovu');
                $('send_correction_submit').disabled = false;
            }
        });

    }	
});

var Registration = Class.create({
    initialize: function(){},
    register: function()
    {
        new Ajax.Request('services/Registration.xml',{
            method: 'post',
            parameters: {
                'name':$('reg_name').value,
                'email':$('reg_mail').value,
                'tel':$('reg_tel').value,
                'address':$('reg_address').value,
                'event':$('reg_event').value
            },
            onSuccess: this.parseResponse.bindAsEventListener(this),
            onFailure: function(transport){
                alert('chyba');
            }
        });
    },
    parseResponse: function(response)
    {
        var xmlDoc = response.responseXML;
        if (!xmlDoc) {
            alert('Nepodarilo sa spracovať odpoveď zo servera');
            return;
        }

        var errors = xmlDoc.getElementsByTagName('error');
        if (errors.length > 0) {
            var result = '';
            for (var i = 0; i < errors.length; i++) {
                result += errors[i].attributes.getNamedItem('message').nodeValue + "\n";
            }
            alert(result);
            return;
        }

        var response = xmlDoc.getElementsByTagName('message');
        var r = response[0];
        var text = r.attributes.getNamedItem("text").nodeValue;
        if (text == 'ok') {
            $('registracia').innerHTML = 'Vasa registracia prebehla uspesne';
        } else {
            alert(text);
        }
    }
});

function openPopup(url,name,height,width,opt_show_scrollbars){
    var scrollbar_param=opt_show_scrollbars?",scrollbars=1":"";
    var newwindow=window.open(url,name,'height='+height+',width='+width+scrollbar_param);
    if(newwindow&&!newwindow.opener){
        newwindow.opener=window;
    }
    if(window.focus){
        newwindow.focus()
    }
    return false;
}

//calculator stuff
var Calculator =  Class.create({
    initialize: function(){},
    calc: function(){
        alert('pocitam')
    },
    clear: function(){
        alert('cistim')
    },
    validateNumeric: function(value,errorElem,zero,limit,text){
		
        if(limit && value > limit){
            if(errorElem != null)
                errorElem.innerHTML = text;
            return false;
        }else if(isNaN(value)){
            if(errorElem != null)
                errorElem.innerHTML = 'hodnota musí byť číslo';
            return false;
        }else if(value <= 0 && zero){
            if(errorElem != null)
                errorElem.innerHTML = 'hodnota musí byť väčšia ako 0';
            return false;
        }else{
            if(errorElem != null)
                errorElem.innerHTML = '';
            return true;
        }
    }
});
/*--------------*/
var UverCalculator = Class.create(Calculator,{
    initialize: function(){
        this.form = $('uver_form');
    },
    calc: function(){
		
        this.dlh = parseFloat($('vyska_uveru').value);
        this.popl = parseFloat($('vstup_poplatok').value);
        this.period = parseFloat($('mesac_poplatok').value);
        this.splatka = parseFloat($('mesac_splatka').value);
        this.doba = parseFloat($('doba').value);

        if(!this.validate()){
            $('uver_result').innerHTML = '';
            return;
        }
		
        rate = 0;
        d = -1;
        x = 1.000001;

        uver = this.dlh - this.popl;
        this.splatka = this.splatka + this.period;
        // algoritmus kalkulačky
        while (d < 0)
        {
            d = uver * (Math.pow(x, (this.doba + 1))) - (this.splatka * x * (Math.pow(x, this.doba) - 1) / (x - 1));
            if (d >= 0) {
                rate = (x - 1) * 12 * 100; break;
            }
            x = x + 0.00001;
        }
        rate = (rate / 100 / 12) + 1;
        rate = (Math.pow(rate, 12) - 1) * 100;
        //		alert('Úverujúca inštitúcia Vám ponúka pôžičku za ročnú úrokovú sadzbu (RPMN) ' + Math.round(rate * 10)/10 + '%');
        $('uver_result').innerHTML = '<strong>Úverujúca inštitúcia Vám ponúka pôžičku za ročnú úrokovú sadzbu (RPMN) ' + Math.round(rate * 10)/10 + '%</strong>';
    },
    clear: function(){
        this.form.reset();
        $('vyska_uveru_error').innerHTML = '';
        $('vstup_poplatok_error').innerHTML = '';
        $('mesac_poplatok_error').innerHTML = '';
        $('mesac_splatka_error').innerHTML = '';
        $('doba_error').innerHTML = '';
        $('uver_result').innerHTML = '';
    },
    validate: function(){
        var valide = true;
        if(!this.validateNumeric(this.dlh, $('vyska_uveru_error'), true)){
            valide = false;
        }
        if(!this.validateNumeric(this.popl, $('vstup_poplatok_error'), false, this.dlh, 'výška úveru musí byť väčšia ako hodnota')){
            valide = false;
        }
        if(!this.validateNumeric(this.period, $('mesac_poplatok_error'), false)){
            valide = false;
        }
        if(!this.validateNumeric(this.splatka, $('mesac_splatka_error'), true)){
            valide = false;
        }
        if(!this.validateNumeric(this.doba, $('doba_error'), true)){
            valide = false;
        }
        return valide;
    }
});

/*--------------*/
var HypoCalculator = Class.create(Calculator,{
    initialize: function(){
        this.form = $('hypo_form');
    },
    clear: function(){
        this.form.reset();
        $('hypo_result').innerHTML = '';
    },
    calc: function(){
        this.dlh = parseFloat($('hypo_vyska_uveru').options[$('hypo_vyska_uveru').selectedIndex].text);
        this.sadzba = parseFloat($('hypo_sadzba').value.replace(",", "."));
        this.doba = parseFloat($('hypo_doba_splacania').options[$('hypo_doba_splacania').selectedIndex].text);

        if(!this.validate()){
            $('hypo_result').innerHTML = '';
            return;
        }
        this.sadzba = this.sadzba/100;
        this.doba = this.doba * 12;
		
        this.sadzba = Math.pow((1+this.sadzba), (1/12))-1;

        v = 1 / (1 + this.sadzba);
        pow = Math.pow(v, this.doba);
        splatka = Math.round(this.dlh * (this.sadzba / (1 - pow)));

        $('hypo_result').innerHTML = '<strong>Mesačná splátka hypotekárneho úveru predstavuje ' + splatka + ' EUR.</strong>';
    },
    validate: function(){
        var valid = true;
        if(!this.validateNumeric(this.sadzba, $('hypo_sadzba_error'), true)){
            valid = false;
        }
        return valid;
    }
});
/*--------------*/
var EuroCalculator = Class.create(Calculator,{
    initialize: function(){
        this.form = $('euro_form');
    },
    clear: function(){
        this.form.reset();
        $('euro_result').innerHTML = '';
    },
    calcEur: function(){
        this.suma = parseFloat($('euro_suma').value.replace(",", "."));
        
        if(!this.validate()){
            $('euro_result').innerHTML = '';
            return;
        }
        
        prevod = Math.round(this.suma/30.126*1000)/1000;

        $('euro_result').innerHTML = '<strong>' + this.suma + ' Sk je ' + prevod + ' &euro;</strong>';
    },
    calcSk: function(){
        this.suma = parseFloat($('euro_suma').value.replace(",", "."));
        
        if(!this.validate()){
            $('euro_result').innerHTML = '';
            return;
        }
        prevod = Math.round(this.suma*30.126*1000)/1000;

        $('euro_result').innerHTML = '<strong>' + this.suma + ' &euro; je ' + prevod + ' Sk</strong>';
    },
    validate: function(){
        var valid = true;
        if(!this.validateNumeric(this.suma, $('euro_suma_error'), true)){
            valid = false;
        }
        return valid;
    }
});
/*--------------*/
var LizingCalculator = Class.create(Calculator,{
    initialize: function(){
        this.form = $('lizing_form');
    },
    calc: function(){
		
        this.cena = parseFloat($('lizing_cena_predmetu').value); // obstarávacia cena
        this.akont = parseFloat($('lizing_akont').options[$('lizing_akont').selectedIndex].text); // akontácia v %
        this.popl = parseFloat($('lizing_vstup_poplatky').value); // vstupné poplatky
        this.splatka = parseFloat($('lizing_splatka').value); // mesačná splátka1doba = parseFloat(document.leas.fldDoba.value); // doba splácania v mesiacoch
        this.doba = parseFloat($('lizing_doba').value); // doba splácania v mesiacoch
        this.odkup1 = parseFloat($('lizing_odkup').value); // odkupná cena
        //this.odkup2 = parseFloat($('lizing_respekt').options[$('lizing_respekt').selectedIndex].text);

        if(!this.validate()){
            $('lizing_result').innerHTML = '';
            return;
        }

        if ((this.cena > 0) && (this.splatka > 0) && (this.doba > 0))
        {
            // úprava parametrov

            rate = 0;
            d = -1;
            x = 1.000001;

            uver = this.cena - ((this.akont / 100) * this.cena) - this.popl;

            // algoritmus kalkulačky

            while (d < 0)
            {
                d = uver * (Math.pow(x, (this.doba + 1))) - (this.splatka * x * (Math.pow(x, this.doba) - 1) / (x - 1)) - this.odkup1;
                if (d >= 0) {
                    rate = (x - 1) * 12 * 100; break;
                }
                x = x + 0.00001;
            }

            // tabuľka s výsledkom

            rate = (rate / 100 / 12) + 1;
            rate = (Math.pow(rate, 12) - 1) * 100;

            $('lizing_result').innerHTML = '<strong>Lízingová spoločnosť Vám ponúka lízingový úver za ročnú úrokovú sadzbu (RPMN) ' + Math.round(rate*10)/10 + '%</strong>';
        }
    },
    clear: function(){
        this.form.reset();
        $('lizing_result').innerHTML = '';
        $('lizing_cena_error').innerHTML = '';
        $('lizing_vstup_error').innerHTML = '';
        $('lizing_splatka_error').innerHTML = '';
        $('lizing_doba_error').innerHTML = '';
        $('lizing_odkup_error').innerHTML = '';
    },
    validate: function(){
        var valid = true;
        var cena = false;
		
        if(!this.validateNumeric(this.cena, $('lizing_cena_error'), true)){
            valid = false;
        }else {
            cena = true;
        }
        if(!this.validateNumeric(this.popl, $('lizing_vstup_error'), true)){
            valid = false;
        }
        if(!this.validateNumeric(this.splatka, $('lizing_splatka_error'), true,this.cena,'Splátka nesmie prevyšovať cenu obstarania')){
            valid = false;
        }
        if(!this.validateNumeric(this.doba, $('lizing_doba_error'), true)){
            valid = false;
        }
        if(!this.validateNumeric(this.odkup1, $('lizing_odkup_error'), true)){
            valid = false;
        }else if(cena){
            this.setRespect();
        }
        return valid;
    },
    setRespect: function(){
        sec = Math.round((this.odkup1 / this.cena)*100);
        if(sec > 30) sec = 30;
        $('lizing_respekt')[sec].selected = true;
    },
    setOdkup: function(){
        var odkup2 = $('lizing_respekt').options[$('lizing_respekt').selectedIndex].text;
        $('lizing_odkup').value = Math.round((odkup2 / 100) * this.cena);
        this.calc();
    }
});
/*--------------*/
var FondCalculator = Class.create(Calculator,{
    initialize: function(){
        this.form = $('fond_form');
    },
    calc: function(){
        this.popl = parseFloat($('fond_vstup').value.replace(",", ".")); // výška spotrebného úveru
        this.roc_popl = parseFloat($('fond_rok_poplatok').value.replace(",", ".")); // vstupné poplatky
        this.vynos = parseFloat($('fond_rok_vynos').value.replace(",", ".")); // mesačná splátka
        this.doba = parseFloat($('fond_doba').value); // doba splácania v mesiacoch

        if(!this.validate()){
            $('fond_result').innerHTML = '';
            return;
        }
        this.popl = this.popl/100;
        this.roc_popl = this.roc_popl/100;
        this.vynos = this.vynos/100;

        value1 = (1 - this.popl)*(1 + this.vynos)-((1 - this.popl)+(1 - this.popl)*(1 + this.vynos))/2*this.roc_popl-1;
        value2=Math.pow((1+value1)*Math.pow(((1+this.vynos)-(1+this.vynos/2)*this.roc_popl),(this.doba-1)),(1/this.doba))-1;
        if (this.doba<=1)
            rate = value1;
        else
            rate = value2;

        rate = Math.round(rate*10000)/100;
        rate = new String(rate.toFixed(2));

        $('fond_result').innerHTML = '<strong>Ročný výnos investície po odrátaní poplatkov ' + rate.replace('.', ',') + '%</strong>';
		
    },
    clear: function(){
        this.form.reset();
        $('fond_result').innerHTML = '';
        $('fond_vstup_error').innerHTML = '';
        $('fond_rok_poplatok_error').innerHTML = '';
        $('fond_rok_vynos_error').innerHTML = '';
        $('fond_doba_error').innerHTML = '';
    },
    validate: function(){
        var valide = true;
        if(!this.validateNumeric(this.popl, $('fond_vstup_error'), true, 100, 'Hodnota musí buť menšia ako 100%')){
            valide = false;
        }
        if(!this.validateNumeric(this.roc_popl, $('fond_rok_poplatok_error'), true, 100, 'Hodnota musí buť menšia ako 100%')){
            valide = false;
        }
        if(!this.validateNumeric(this.doba, $('fond_doba_error'), true)){
            valide = false;
        }
        if(!this.validateNumeric(this.vynos, $('fond_rok_vynos_error'), true, 100, 'Hodnota musí buť menšia ako 100%')){
            valide = false;
        }
        return valide;
    }
});
/*--------------*/
var PoistCalculator = Class.create(Calculator,{
    initialize: function(){
        this.form = $('poist_form');
        this.arrLxM = new Array(100000,99014,98945,98892,98847,98823,98803,98785,98762,98740,98716,98695,98676,98651,98618,98576,98531,98476,98421,98354,98280,98188,98091,97982,97865,97748,97634,97530,97411,97281,97133,96987,96840,96681,96504,96319,96138,95938,95711,95449,95173,94869,94527,94134,93691,93201,92647,92033,91359,90639,89834,88932,87918,86816,85681,84484,83208,81812,80394,78908,77259,75409,73455,71449,69357,97126,64785,62384,59897,57330,54674,51922,49093,46163,43232,40222,37266,34319,21400,28529,25726,23011,20404,17922,15584,13404,11394,9563,7919,6461,5189,4098,3177,2415,1797,1307,928,641,430,280,177);
        this.arrDxM = new Array(986,69,53,46,24,19,18,23,22,24,22,19,25,32,42,45,55,55,67,74,91,98,109,117,117,114,104,119,130,148,147,147,158,177,186,180,200,228,261,276,304,342,393,443,489,555,613,674,719,805,902,1014,1102,1135,1197,1276,1395,1418,1487,1649,1850,1955,2005,2092,2231,2341,2401,2487,2567,2856,2752,2829,2930,2930,3010,2957,2947,2919,2871,2803,2715,2608,2481,2338,2180,2010,1830,1645,1457,1272,1092,921,762,618,490,379,287,210,150,104,177);
        this.arrLxZ = new Array(100000,99290,99214,99207,99183,99168,99146,99127,99109,99091,99072,99054,99040,99030,99017,98998,98968,98937,98907,98873,98838,98804,98774,98745,98727,98707,98677,98634,98590,98550,98516,98481,98447,98413,98370,98314,98246,98177,98098,98005,97906,97798,97686,97544,97375,97177,96965,96743,96507,96237,95932,95604,95272,94902,94492,94040,93562,93036,92461,91812,91111,90343,89551,88632,87590,86441,85239,83909,82425,80809,79055,77171,75087,72800,70251,67523,64586,61437,58068,54488,50709,46755,42656,38453,34197,29947,25771,21743,17938,14427,11274,8527,6214,4343,2894,1827,1085,601,308,144,61);
        this.arrDxZ = new Array(710,76,7,24,15,22,19,17,18,20,18,14,10,13,19,30,31,30,34,35,34,30,29,19,20,30,43,43,40,34,35,33,34,43,56,67,69,79,93,99,107,112,142,169,198,211,222,236,270,305,328,332,370,409,453,477,527,575,650,701,768,792,919,1041,1149,1201,1330,1485,1615,1754,1884,2084,2287,2549,2729,2936,3150,3369,3581,3778,3954,4099,4203,4256,4250,4175,4028,3805,3511,3153,2747,2312,1872,1449,1067,742,484,293,164,83,61);
    },
    mDx: function(Age, v)
    {
        return parseFloat(this.arrLxM[Age]) * (Math.pow(v, Age));
    },
    zDx: function (Age, v)
    {
        return parseFloat(this.arrLxZ[Age]) * (Math.pow(v, Age));
    },
    mDxn: function (Age, v)
    {
        return parseFloat(this.arrLxM[Age]) * (Math.pow(v, (Age + 1)));
    },
    zDxn: function (Age, Length, v)
    {
        return parseFloat(this.arrLxZ[Age + Length]) * (Math.pow(v, (Age + 1 + Length)));
    },
    mCx: function (Age, v)
    {
        return parseFloat(this.arrDxM[Age]) * (Math.pow(v, (Age + 1)));
    },
    zCx: function zCx(Age, v)
    {
        return parseFloat(this.arrDxZ[Age]) * (Math.pow(v, (Age + 1)));
    },
    calc: function(){
        // inicializácia premenných
        this.vek = parseFloat($('poist_vek').options[$('poist_vek').selectedIndex].text);
        this.doba = parseFloat($('poist_obdobie').options[$('poist_obdobie').selectedIndex].text);
        this.suma1 = parseFloat($('poist_suma_dozitie').options[$('poist_suma_dozitie').selectedIndex].text);
        this.suma2 = parseFloat($('poist_suma_umrtie').options[$('poist_suma_umrtie').selectedIndex].text);
        this.rate = parseFloat($('poist_sadzba').value);

        if(!this.validate()){
            $('poist_result').innerHTML = '';
            return;
        }

        this.rate = this.rate / 100;

        if ((this.vek + this.doba) <= 65)
        {
            v = 1 / (1 + this.rate);
            radios = this.form.getInputs('radio','sex');
            radios.each(function(item){
                if(item.checked){
                    sex = item.value;
                }
            });
            if (sex == 0)

            { //výpočet pre muža

                //***********Mx**********************

                var Mx = 0;
                for(i = 0; i < (101 - this.vek); i++) Mx = Mx + this.mCx(this.vek + i, v);

                //***********Mxn*********************

                var Mxn = 0;
                for(i = 0; i < (101 - (this.vek + this.doba)); i++) Mxn = Mxn + this.mCx(this.vek + this.doba + i, v);

                //***********Dxn*********************

                Dxn = this.mDx(this.vek + this.doba, v)

                //***********Nx**********************

                var Nx = 0;
                for(i = 0; i < (101 - this.vek); i++) Nx = Nx + this.mDx(this.vek + i, v);

                //***********Dx**********************

                Dx = this.mDx(this.vek, v);

                //***********Nxn*********************

                var Nxn = 0;
                for(i = 0; i < (101 - (this.vek + this.doba)); i++) Nxn = Nxn + this.mDx(this.vek + this.doba + i, v);

            } // end of MAN

            else

            { //výpočet pre ženu

                //**********Mx**********************

                Mx = 0;
                for(i = 0; i < (101 - this.vek); i++) Mx = Mx + this.zCx(this.vek + i, v);

                //**********Mxn*********************

                Mxn = 0;
                for(i = 0; i < (101 - (this.vek + this.doba)); i++) Mxn = Mxn + this.zCx(this.vek + this.doba + i, v);

                //**********Dxn*********************

                Dxn = this.zDx(this.vek + this.doba, v)

                //**********Nx**********************

                Nx = 0;
                for(i = 0; i < (101 - this.vek); i++) Nx = Nx + this.zDx(this.vek + i, v);

                //**********Dx**********************

                Dx = this.zDx(this.vek, v);

                //**********Nxn*********************

                Nxn = 0;
                for(i = 0; i < (101 - (this.vek + this.doba)); i++) Nxn = Nxn + this.zDx(this.vek + this.doba + i, v);

            } // end of WOMAN

            // výsledok
			
            PremNetto = ((this.suma1 * Dxn + this.suma2 * (Mx - Mxn)) / (Nx - Nxn)) / 12;
            PremBruttoMin = PremNetto * 1.2;
            PremBruttoMax = PremNetto * 1.3;

            $('poist_result').innerHTML = '<p><strong>Čisté mesačné predpísané poistné činí ' + Math.round(PremNetto) + ' EUR </strong></p>';
            $('poist_result').innerHTML += '<p><strong>Skutočné mesačné poistné (okrem krytia rizika zahŕňa aj zodpovedajúcu časť obstarávacích, udržiavacích a ostatných prevádzkových nákladov poisťovne) sa bude pohybovať od ' + Math.round(PremBruttoMin) + ' EUR do ' + Math.round(PremBruttoMax) + ' EUR</strong></p>';
        }
    },
    clear: function(){
        this.form.reset();
        $('poist_result').innerHTML = '';
        $('poist_sadzba_error').innerHTML = '';
    },
    validate: function(){
        var valid =  true;
        if(!this.validateNumeric(this.rate, $('poist_sadzba_error'), false, 10, 'Technická sadzba spravidla neprevyšuje 10 % p.a.!')){
            valid = false;
        }
        return valid;
    }
});
var MzdaCalculator = Class.create(Calculator,{
    initialize: function(constants){
        this.form = $('mzda_form');
        this.constants = constants;
    },
    clear: function(){
        this.form.reset();
        $('mzda_result').style.display = 'none';
    },
    calc: function(){
        this.HM = parseFloat($('mzda_hruba').value.replace(',', '.'));
        this.PD = parseInt($('mzda_deti').value);
        this.DDP = parseFloat($('mzda_ddp').value.replace(',', '.'));

        if(!this.validate()){
            $('mzda_result').style.display = 'none';
            return;
        }

        if(this.DDP > 33.19){
            this.DDP = 33.19;
        }
		
        this.odvodyZamestnanec();
        this.odvodyZamestnavatel();
        this.dane();

        $('mzda_HM').innerHTML = this.HM
        $('mzda_CM').innerHTML = Math.round(this.CM*100)/100;
        $('mzda_CNP').innerHTML = Math.round(this.CNP*100)/100;
        $('mzda_SO').innerHTML = Math.round(this.SO*100)/100;
        $('mzda_NP').innerHTML = Math.round(this.NP*100)/100;
        $('mzda_SP').innerHTML = Math.round(this.SP*100)/100;
        $('mzda_IP').innerHTML = Math.round(this.IP*100)/100;
        $('mzda_PN').innerHTML = Math.round(this.PN*100)/100;
        $('mzda_ZP').innerHTML = Math.round(this.ZP*100)/100;
        $('mzda_SOZ').innerHTML = Math.round(this.SOZ*100)/100;
        $('mzda_NPZ').innerHTML = Math.round(this.NPZ*100)/100;
        $('mzda_SPZ').innerHTML = Math.round(this.SPZ*100)/100;
        $('mzda_IPZ').innerHTML = Math.round(this.IPZ*100)/100;
        $('mzda_PNZ').innerHTML = Math.round(this.PNZ*100)/100;
        $('mzda_GPZ').innerHTML = Math.round(this.GPZ*100)/100;
        $('mzda_UPZ').innerHTML = Math.round(this.UPZ*100)/100;
        $('mzda_RFZ').innerHTML = Math.round(this.RFZ*100)/100;
        $('mzda_ZPZ').innerHTML = Math.round(this.ZPZ*100)/100;
        $('mzda_D').innerHTML = Math.round(this.D*100)/100;
        $('mzda_result').style.display = 'block';
    },
    validate: function(){
        var valid = true;
		
        if(!this.validateNumeric(this.PD, $('mzda_deti_error'), false)){
            valid = false;
        }
        if(!this.validateNumeric(this.DDP, $('mzda_ddp_error'), false)){
            valid = false;
        }
        if(!this.validateNumeric(this.constants.MIN_MZDA, $('mzda_hruba_error'), true,this.HM, 'Mzda musí byť väčšia ako minimálna mzda ' + this.constants.MIN_MZDA)){
            valid = false;
        }
		
        return valid;
    },
    odvodyZamestnanec: function(){
        var HM = this.HM;
        if(HM > this.constants.MVZ_NP){
            HM = this.constants.MVZ_NP;
        }
        //nemocenske poistenie
        this.NP = HM * this.constants.NPK;
        //starobne poistenie
        HM = this.HM;
        if(HM > this.constants.MVZ_SP){
            HM = this.constants.MVZ_SP;
        }
        this.SP = HM * this.constants.SPK;

        //invalidne poistenie
        HM = this.HM;
        if(HM > this.constants.MVZ_IP){
            HM = this.constants.MVZ_IP;
        }
        this.IP = HM * this.constants.IPK;

        //poistenie v nezamestnanosti
        HM = this.HM;
        if(HM > this.constants.MVZ_PN){
            HM = this.constants.MVZ_PN;
        }
        this.PN = HM * this.constants.PNK;

        //zdravotn0 poistenie
        HM = this.HM;
        if(HM > this.constants.MVZ_ZP){
            HM = this.constants.MVZ_ZP;
        }
        this.ZP = HM * this.constants.ZPK;

        //odvody spolu
        this.SO = this.NP + this.SP + this.IP + this.PN + this.ZP;
    },
    odvodyZamestnavatel: function(){
        var HM = this.HM;
        if(HM > this.constants.MVZZ_NPZ){
            HM = this.constants.MVZZ_NPZ;
        }
        //nemocenske poistenie
        this.NPZ = HM * this.constants.NPZK;

        //starobne poistenie
        HM = this.HM;
        if(HM > this.constants.MVZZ_SPZ){
            HM = this.constants.MVZZ_SPZ;
        }
        this.SPZ = HM * this.constants.SPZK;

        //invalidne poistenie
        HM = this.HM;
        if(HM > this.constants.MVZZ_IPZ){
            HM = this.constants.MVZZ_IPZ;
        }
        this.IPZ = HM * this.constants.IPZK;

        //poistenie v nezamestnanosti
        HM = this.HM;
        if(HM > this.constants.MVZZ_PNZ){
            HM = this.constants.MVZZ_PNZ;
        }
        this.PNZ = HM * this.constants.PNZK;

        //garancne poistenie
        HM = this.HM;
        if(HM > this.constants.MVZZ_GPZ){
            HM = this.constants.MVZZ_GPZ;
        }
        this.GPZ = HM * this.constants.GPZK;

        //urazove poistenie
        HM = this.HM;
        if(HM > this.constants.MVZZ_UPZ){
            HM = this.constants.MVZZ_UPZ;
        }
        this.UPZ = HM * this.constants.UPZK;
		
        //rezevny fond
        HM = this.HM;
        if(HM > this.constants.MVZZ_RFZ){
            HM = this.constants.MVZZ_RFZ;
        }
        this.RFZ = HM * this.constants.RFZK;

        //zdravotne poistenie
        HM = this.HM;
        if(HM > this.constants.MVZZ_ZPZ){
            HM = this.constants.MVZZ_ZPZ;
        }
        this.ZPZ = HM * this.constants.ZPZK;

        //odvody spolu
        this.SOZ = this.NPZ + this.SPZ + this.IPZ + this.PNZ + this.GPZ + this.UPZ + this.RFZ + this.ZPZ;
    },
    dane: function(){
        this.ZD = this.HM - (this.SO + this.DDP);
		
        //nezdanietelne minimum
        if(this.ZD <= (86 * this.constants.ZM)){
            this.MNM = this.constants.NMD/12;
        }
        else{
            NM = (44.2*this.constants.ZM)-(this.ZD/4);
            if(NM < 0){
                NM = 0;
            }
            this.MNM = NM / 12;
        }

        //zaklad bez minima
        if(!$('mzda_nepracujuci').checked){
            this.ZD -= this.MNM;
        }else{
            this.ZD -= (this.MNM*2);
        }
        if(this.ZD < 0){
            this.ZD = 0;
        }

        //dan
        this.D = (this.ZD * this.constants.DK) - (this.constants.DBD * this.PD);

        //cista mzda
        this.CM = this.HM - (this.SO + this.D);

        //celkove naklady prace
        this.CNP = this.HM + this.SOZ;
    }
});



var Menu = Class.create({
	
    tmpFnc: null,
	initialize: function(param) {
	
        var e = Prototype.emptyFunction;
        //zistime ktore licka maju ulka a v zavisloti od toho vytvorime tagy <i>
        var sub = $$('ul#'+param+' li ul');
        sub.each(function(node){
            //vytvorime si element
            element = new Element('i', {
            });
            element.update("<span>viac</span>");

            node.up().down('a').insert({
                after: element
            });
        });
		
        var nodes = $$('ul#'+param+' span');
        //nastavime handler pre jednotlive elementy, pri udalosti onclick
        nodes.each(function(node){
            Event.observe(node, 'click', this.onClick.bindAsEventListener(this));
        }.bind(this));
		
        //nastavime handler pre onmouse over pre jednotlive acka
        var nodes = $$('ul#'+param+' a');
        nodes.each(function(node){
            Event.observe(node, 'mouseover', this.onMouseOver.bindAsEventListener(this));
        }.bind(this));
		
        //nastavime handler pre onmouse out pre jednotlive acka
        nodes.each(function(node){
            Event.observe(node, 'mouseout', this.onMouseOut.bindAsEventListener(this));
        }.bind(this));
		
        //nastavime handler pre onmouse over pre jednotlive icka
        var nodes = $$('ul#'+param+' i');
        nodes.each(function(node){
            Event.observe(node, 'mouseover', this.onMouseOverI.bindAsEventListener(this));
        }.bind(this));
		
        //nastavime handler pre onmouse out pre jednotlive icka
        nodes.each(function(node){
            Event.observe(node, 'mouseout', this.onMouseOutI.bindAsEventListener(this));
        }.bind(this));
		
        this.param = param;
    },
	
    hideAll : function ()
    {
        //v pripade ze nastane udalost onclick, schovame vsetky casti v menu
        nodes = nodes = $$('ul#'+this.param+' ul');
        nodes.each(function(node){
            node.setStyle({
                display: 'none'
            });
            node.up().removeClassName('active');
			
        });
    },
	
	
    onClick: function(e,obj)
    {
        element = (e.target.up().up().down('ul'));
        // v pripade ze menu je rozrolovane a potrebujeme ho schovat

        if (element.getStyle('display')!='block')
        {
		
            this.hideAll();
            element.setStyle({
                display: 'block'
            });
            e.target.up().up().addClassName('active');
            this.tmpFnc = this.onClickA.bindAsEventListener(this);
            e.stop();
            document.observe('click', this.tmpFnc);
        }
        else
        {
            this.hideAll();
        }
    },
	
    onMouseOver: function(e,obj)
    {
        try
        {
		
            e.target.up().down('i').addClassName('hover');
        }
        catch (e)
        {
        }
		
    },
	
    onMouseOut: function(e,obj)
    {

        try
        {

            e.target.up().down('i').removeClassName('hover');
        }
        catch (e)
        {
        }
    },
	
    onMouseOverI: function(e,obj)
    {
		
        try
        {
            e.target.up().up().down('a').addClassName('hover2');
            e.target.up().addClassName('hover2');
        }
        catch (e)
        {
        }
		
    },
	
    onMouseOutI: function(e,obj)
    {
	
        try
        {
            e.target.up().up().down('a').removeClassName('hover2');
            e.target.up().removeClassName('hover2');
        }
        catch (e)
        {
        }
    },
    
    onClickA: function(e, obj)
    {
    	this.hideAll();
    	document.stopObserving('click',this.tmpFnc);
    }
	
});

/*
funcia na zmenu districtov podla krajniny
 */

function changeCountry(selectItem)
{
	var district = $('district');
	var tenement = $('tenement');

    if (selectItem.value == -1) {
        district.disabled = true;
        district.value = -1;
        tenement.disabled = true;
        tenement.value = -1;
        return;
    } else {
        district.disabled = false;
        tenement.disabled = false;

        var disarr = $$('#district option');
        disarr.each(function(e){
            if (selectItem.value == 111427) {
                e.style.display = 'block';
            }
            else {
                e.style.display = 'none';
				district.disabled = true;
				district.value = -1;
            }
        });
    }
}


var Calendar = Class.create();
Calendar.prototype = {
    initialize: function(year, month) {
        this.current_year  = year;
        this.current_month = month;
    },

    getPrevMonth: function(){
        if(this.current_month == 1)
        {
            this.current_month = 12;
            this.current_year = this.current_year - 1;
        }
        else
        {
            this.current_month = this.current_month - 1;
        }

        this.MainAjax(this.current_month, this.current_year);
    },

    getNextMonth: function(){
        if(this.current_month == 12)
        {
            this.current_month = 1;
            this.current_year = this.current_year + 1;
        }
        else
        {
            this.current_month = this.current_month + 1;
        }

        this.MainAjax(this.current_month, this.current_year);
    },

    MainAjax: function(mo, ye){
        var params = 'month='+mo+'&year='+ye;
        new Ajax.Updater('calendar_wrapper','services/CalendarBox.html',{
            method:'post',
            parameters: params
        });
    }
}

/*
funcia na indikaciu zostavajucich pismen
 */

function changeCountLetter(e,count)
{
	var str;
	
	//v zavislosti ci to robime ako udalost, alebo posielame referenciu
	if (e.target)
		e=e.target;
	
	var diff=count-e.getValue().length;

	
	if (diff <0) 
	{
		str = "Prekročili ste počet znakov";
	}
	else if (diff==0)
	{
		str = "Zostáva 0 znakov";
	}
	else if (diff == 1)
	{
		str = "Zostáva 1 znak";
	}
	else if (diff <5)
	{
		str = "Zostávajú "+diff+" znaky";
	}
	else if (diff <=160)
	{
		str = "Zostáva "+diff+" znakov";
	}
	
	
	
    $(e.id+'_counter').update(str);
}

var Rates = Class.create({
	initialize: function() {
		//
	},

	getCrossRates: function() {

		new Ajax.Request('services/GetCrossRates.xml', {
			method: 'post',
			parameters: {
				'type':$('rate_type').value,
				'time':$('rates_time').value
			},
			onSuccess: this.parseResponse,
			onFailure: function() {
				alert('Na serveri vznikla chyba. Nepodarilo sa získať krížové kurzy.');
			}
		});

	},

	parseResponse: function(response) {
		var xmlDoc = response.responseXML;
		
		if (!xmlDoc) {
			alert('Nepodarilo sa spracovať odpoveď zo servera');
			return;
		}

		var errors = xmlDoc.getElementsByTagName('error');
		if (errors.length > 0) {
			var result = '';
			for (var i = 0; i < errors.length; i++) {
				result += errors[i].attributes.getNamedItem('message').nodeValue + "\n";
			}
			alert(result);
			return;
		}

		var response = xmlDoc.getElementsByTagName('message');
		var r = response[0];
		var text = r.attributes.getNamedItem("text").nodeValue;
		
		var rates_graph = new SWFObject("fileadmin/template/swf/Line.swf", "Krížové kurzy", "250", "200", "9.0.45", "0", "0", "#ffffff");
		rates_graph.addParam("flashVars", "dataXML=" + text);
		rates_graph.addParam("allowFullScreen", "false");
		rates_graph.addParam("wmode", "opaque");
		rates_graph.write("rates_graph");
	}
});

function changeCheckedNewsletter(param,section)
{
	
	
	if (section=='')
	{
		nodes  = ($$("#newsletter_checkboxs input"));
		
		nodes.each(function(node){

			if ( $('newsletter_check_all').checked==true)
				node.checked="checked";
			else
				node.checked="";
		
		});
	}
	else 
	{
		nodes  = ($$("#"+param.up().up().id+" input"));

		nodes.each(function(node){
			
			type=node.id.split('_');
			if (type[2]==section )
			{
				if ($('section_'+section).checked==true)
				{
					node.checked="checked";
				}
				else
						node.checked="";
			}
			
		
		});
	}
}

var mmEquator = Class.create({
	maxHeight: 0,

	initialize: function(objectArray){
		var obj = $$(objectArray);

		obj.each(function(e) {
			curHeight = e.getHeight();
			if(this.maxHeight < curHeight) this.maxHeight = curHeight;
		}.bind(this));

		obj.each(function(e) {
			e.setStyle({
				height: this.maxHeight+'px'
			});
			console.log(this.maxHeight);
		}.bind(this));
	}
});

var BoxHeights = Class.create({
	maxh: 0,
	boxes: Array(),
	num: 0,
	equalise: function() {
		this.num = arguments.length;
		for (var i=0;i<this.num;i++) if (!$(arguments[i])) return;
		this.boxes = arguments;
		this.maxheight();
		for (var i=0;i<this.num;i++) $(arguments[i]).style.height = this.maxh+"px";
	},
	maxheight: function() {
		var heights = new Array();
		for (var i=0;i<this.num;i++) {
			if (navigator.userAgent.toLowerCase().indexOf('opera') == -1) {
				heights.push($(this.boxes[i]).scrollHeight);
			} else {
				heights.push($(this.boxes[i]).offsetHeight);
			}
		}
		heights.sort(this.sortNumeric);
		this.maxh = heights[this.num-1];
	},
	sortNumeric: function(f,s) {
		return f-s;
	}
});

//
//Trieda pre mazanie defaultnych textov v inputoch
// pri zanechani prazdneho inputu sa naspet vrati hodnota na podovnu
//
var InputCleaner = Class.create({
	inputId: '',
	input: null,
	oldValue: '',
	initialize: function(inputId) {
		this.inputId = inputId;
		this.input = $(this.inputId);
		if (this.input != null) {
			this.oldValue = this.input.getValue();
			this.input.observe('focus', this.onActivate.bindAsEventListener(this));
			this.input.observe('blur', this.onDeactivate.bindAsEventListener(this));
		}
	},
	onActivate: function(event) {
		if (this.input.getValue() == this.oldValue) {
			this.input.setValue('');
		}
	},
	onDeactivate: function(event) {
		if (this.input.getValue() == '') {
			this.input.setValue(this.oldValue);
		}
	}
});



function displayArticle(anchor,id)
{
	$$('#article_number li').each(function(item){ item.removeClassName('a')});
	anchor.parentNode.addClassName('a');

	$$('#article_context li').each(function(item){ item.removeClassName('a'); item.style.display='none'});
	$('top_'+id).addClassName('a')
	$('top_'+id).style.display = 'block';
	date = $('top_'+id+'_date');
	$('article_title').innerHTML = '<span>Články</span> / ' + date.innerHTML;
}

Event.observe(document, 'dom:loaded', function() {
	$$("div.img-easy").each(function(div) { div.style.width = (div.down("img").width + 8)+"px"; });
}); 

