  function days_in_february(year){
          return (((year % 4 == 0) && ( (!(year % 100 == 0)) || (year % 400 == 0))) ? 29 : 28 );
      }//end days_in_february()


 function valid_date(year, month, day){
              var valid = true;

              if( year < parseInt(<?= date('Y') ?>-2) || year > parseInt(<?= date('Y') ?>) ){
		        valid = false;
              }

              if( month < 1 || month > 12 ){
		        valid = false;
              }

              days_feb = days_in_february(year);

              if( month == 2 && day > parseInt(days_feb) ){
		        valid = false;
              }

              if( day < 1 || day > 31 ){
		        valid = false;
              }

              return valid;

        }//end valid_date

        function get_day(day){
            zile = new Array();
            zile[0] = 'Luni';
            zile[1] = 'Marti';
            zile[2] = 'Miercuri';
            zile[3] = 'Joi';
            zile[4] = 'Vineri';
            zile[5] = 'Sambata';
            zile[6] = 'Duminica';

            return zile[day];
        }//end get_day()

        function get_month(month){
            luni = new Array();
            luni[0] = 'Ianuarie';
            luni[1] = 'Februarie';
            luni[2] = 'Martie';
            luni[3] = 'Aprilie';
            luni[4] = 'Mai';
            luni[5] = 'Iunie';
            luni[6] = 'Iulie';
            luni[7] = 'August';
            luni[8] = 'Septembrie';
            luni[9] = 'Octombrie';
            luni[10] = 'Noiembrie';
            luni[11] = 'Decembrie';

            return luni[month]
        }//end get_month()

        function format_b_day(day, month, year, day_of_week){
            return get_day(day_of_week)+", "+day+" "+get_month(month)+" "+year;
        }//format_b_day()

        function format_day(day, month, year, day_of_week){
            return day+" "+get_month(month)+" "+year;
        }//format_day()

        function format_hb(){
            return get_day(day_of_week)+", "+day+" "+get_month(month)+" "+year;
        }//format_hb()

        function valid_no_of_days(n_days){
		    if( n_days >= 22 && n_days <= 44 ){
		        return true;
		    }else{
		        return false;
		    }
        }


        function calculate_ovulation(){
            var year = parseInt($("#test_year").val());
            var day = parseInt($("#test_day").val());
            var month = parseInt($("#test_month").val());
            var n_days = parseInt($("#test_no_of_days").val());

            var m_sec = 86400000;

            if( valid_date(year,month,day) ){
            		if( valid_no_of_days(n_days) ){
                        var s_d = new Date();
                        s_d.setDate(day);
                        s_d.setMonth(month-1);
                        s_d.setYear(year);
                        s_d.setTime(s_d.getTime() + ((n_days * m_sec) - m_sec * 14));

                        var n_m = s_d.getMonth()+1;
                        var n_y = s_d.getFullYear();
                        var n_d = s_d.getDate();



                        $("#test_results").fadeIn();
                        $(".test_results_ov").html('Ovulatia este pe '+n_d+"/"+n_m+"/"+n_y);

                        var fertile = 'Zile fertile: ';

                        s_d.setTime(s_d.getTime() - m_sec * 3);
                        var n_m = s_d.getMonth()+1;
                        var n_y = s_d.getFullYear();
                        var n_d = s_d.getDate();

                        fertile += n_d+"/"+n_m+"/"+n_y+" - ";

                        s_d.setTime(s_d.getTime() + m_sec * 5);
                        var n_m = s_d.getMonth()+1;
                        var n_y = s_d.getFullYear();
                        var n_d = s_d.getDate();

                        fertile += n_d+"/"+n_m+"/"+n_y;

                        $(".test_results_fertile").html(fertile);
            		}else{
		                alert("Durata ciclului menstrual trebuie sa fie intre 22 si 44 de zile!");
            		}

            }else{
		        alert('Data aleasa nu este valida! \nAlegeti o alta data!');
            }
        }//end calculate_ovulation

        function calculate_birth(){
            var year = parseInt($("#test_year").val());
            var day = parseInt($("#test_day").val());
            var month = parseInt($("#test_month").val());

            if( valid_date(year,month,day) ){
            	var f_date = new Date(year, month-1, day);
                f_date.setDate(f_date.getDate() + 7*40);

                data_nasterii = format_b_day(f_date.getDate(), f_date.getMonth(), f_date.getFullYear(), f_date.getDay());

                var f_date = new Date(year, month-1, day);
                f_date.setDate(f_date.getDate() + 7*6);
                first_hb = format_b_day(f_date.getDate(), f_date.getMonth(), f_date.getFullYear(), f_date.getDay());

                $("#test_results").fadeIn();
                $(".test_results_data_nasterii").html(data_nasterii);
                $(".test_results_first_hb").html(first_hb);

                var weeks = '<div class="column"><ul>';
                var f_date = new Date(year, month-1, day);

                for( var i=1;i<41;i++ ){
                	f_date.setDate(f_date.getDate() + 7);
                    if( i == 21 ){
		                weeks += '</ul></div><div class="column"><ul>';
                    }
                    var weeks = weeks+"<li>Saptamana "+i+" - "+format_day(f_date.getDate(), f_date.getMonth(), f_date.getFullYear(), f_date.getDay())+"</li>";
                }//end for
                weeks += "</ul></div>";

                $("#tab").html(weeks);

            }else{
		        alert('Data aleasa nu este valida! \nAlegeti o alta data!');
            }
        }//end calculate_birth

        function calculate_bmi(){
            var greutate = parseInt($("#test_greutate").val());
            var h_m = parseFloat($("#test_h_m").val())*100;
            var bmi = (Math.round((greutate * 10000) / (h_m * h_m)));
            var bmi_c = '';

               if( bmi<19 ){
                    bmi_c = "Subponderal(a)";
               }else if( bmi>=19 && bmi <= 25){
                    bmi_c = "Normal(a)";
               }else if( bmi>=26 && bmi<=29 ){
                    bmi_c = "Predispus(a) la riscuri de sanatate";
               }else if( bmi>=30 && bmi<=40 ){
                    bmi_c = "Obez(a)";
               }else if( bmi >40 ){
                    bmi_c = "Obezitate extrema";
               }

            $(".test_results_bmi").html(bmi);
            $(".test_results_bmi_c").html(bmi_c);
            $("#test_results").fadeIn();


        }//end calculate_bmi

        function isNum(nr){
            return nr*1 ? true:false;
        }

        function num_bound(nr, min, max){
		    if( nr >= min || nr <= max){ return true }else{ return false }
        }

        function validate_calorii( tc_g, tc_h, tc_m ){
            var flag_v = true;

            if( !isNum(tc_g) ){ alert("Greutatea trebuie sa aiba o valoare numerica!"); flag_v = false; return flag_v; }

            if( !num_bound(tc_h, 0, 99) ){ alert("Campul 'ore' trebuie sa aiba o valoare numerica cuprinsa intre 0 si 99!"); flag_v = false; return flag_v; }

            if( !num_bound(tc_m, 0, 60) ){ alert("Campul 'minute' trebuie sa aiba o valoare numerica cuprinsa intre 0 si 60!"); flag_v = false; return flag_v; }

            return flag_v;
        }//end validate_calorii()

        function calculate_calorii(){
            var tc_g = parseInt($("#tc_g").val());
            var tc_h = parseInt($("#tc_h").val());
            var tc_m = parseInt($("#tc_m").val());
            var tc_act = parseFloat($("#tc_act").val());

            if( !isNum(tc_m) ){ tc_m = 0; }

            if( validate_calorii(tc_g, tc_h, tc_m) ){
                var t_u = tc_h*60+tc_m;

                var calories = parseInt(t_u) * tc_g * tc_act;
                calories = parseInt(calories);

                if( !isNum(calories) ){ calories = '0' }

                $(".test_results_tc").html(calories);
                $("#test_results").fadeIn();
            }


        }//end calculate_calorii

        function validate_age(age){
        	 var flag_v = true;
             if( !num_bound(age, 0, 120) ){ alert("Campul 'varsta' trebuie sa aiba o valoare numerica cuprinsa intre 0 si 120!"); flag_v = false; return flag_v; }
             return flag_v;
        }//end validate_age

        function calculate_frecventa(){
            var fc_v = parseInt($("#fc_v").val());
            var fc_p = parseInt($("#fc_p").val());
            var b_c = 220;
            var max_hr_b = b_c-fc_v;



            if( validate_age(fc_v) ){
            	var max_hr = Math.round(((max_hr_b*0.50)/60) * fc_p);
            	var min_hr = Math.round(((max_hr_b*0.75)/60) * fc_p);
                var max_hr_p = Math.round((max_hr_b/60) * fc_p);

                var to_display = "In timp ce faci exercitii fizice, ar trebui sa numeri intre "+max_hr+" si "+min_hr+" batai ale inimii in "+fc_p+" de secunde si nu ar trebui sa depasesti "+max_hr_p+" batai de inima.";

                $(".test_results_fc").html(to_display);
                $("#test_results").fadeIn();
            }


        }//end calculate_frecventa

        $(document).ready(function() {
        		$("#tc_act").change(function(){
                    $("#test_results").fadeOut();
                });

                $("#fc_p").change(function(){
                    $("#test_results").hide();
                    calculate_frecventa();
                });
        });

