$(document).ready(function(){
    var date_start = false;
    var date_finish = false;
    $("#tcal").hide();
    $("#select_m1").change(function()
    {
        var day = $("#select_d1 option:selected").html();
        var month = new Date();
        var year = month.getFullYear();
        if (parseInt($("#select_m1 option:first").val())==11 && parseInt($("#select_m1 option:selected").val())==1 || parseInt($("#select_m1 option:first").val())==12 && parseInt($("#select_m1 option:selected").val())!=12)
        year = year+1;
        $("#year1").val(year);
        //alert($("#select_m1 option:selected").val());
        var dayCount = new Date(year, parseInt($("#select_m1 option:selected").val()), 0).getDate();
        var m = (month.getMonth()+1).toString();
        if (m.length==1)
        	m = "0"+m;
        $("#select_d1").empty();
        if ($("#select_m1 option:selected").val() == m)
        {
        	for(var i = month.getDate(); i<=dayCount; i++)
	        {
	                addDay = i.toString();
	                if (addDay.length==1)
	                	addDay = "0"+addDay;
	                $('#select_d1').append('<option value="'+addDay+'">'+addDay+'</option>');
	        }
	        if ($("#select_d1").get(0).options.length > day-month.getDate()+1)
	        {
                var value = day;
                if (value.length==1)
	                	value = "0"+value;
	        	$("#select_d1 option[value='"+value+"']").attr('selected', 'selected');
	        }
	        else
	        	$("#select_d1 option:last").attr('selected', 'selected');
        }
        else
        {
	        for(var i = 1; i<=dayCount; i++)
	            {
	                addDay = i.toString();
	                if (addDay.length==1)
	                	addDay = "0"+addDay;
	                $('#select_d1').append('<option value="'+addDay+'">'+addDay+'</option>');
	            }
	        if ($("#select_d1").get(0).options.length > day)
	        {
                var value = day;
                if (value.length==1)
	                	value = "0"+value;
	        	$("#select_d1 option[value='"+value+"']").attr('selected', 'selected');
	        }
	        else
	        	$("#select_d1 option:last").attr('selected', 'selected');
        }
        if (parseInt($("#select_m1 option:selected").val()) == parseInt($("#select_m1 option:last").val()))
        {
        	$("#select_d1").empty();
        	for(var i = 1; i<=month.getDate(); i++)
	        {
	                addDay = i.toString();
	                if (addDay.length==1)
	                	addDay = "0"+addDay;
	                $('#select_d1').append('<option value="'+addDay+'">'+addDay+'</option>');
	        }
	        if ($("#select_d1").get(0).options.length > day)
	        {
                var value = day;
                if (value.length==1)
	                	value = "0"+value;
	        	$("#select_d1 option[value='"+value+"']").attr('selected', 'selected');
	        }
	        else
	        	$("#select_d1 option:last").attr('selected', 'selected');
        }
    });
    $("#select_m2").change(function()
    {
        var day = $("#select_d2 option:selected").html();
        var month = new Date();
        var year = month.getFullYear();
        if (parseInt($("#select_m2 option:first").val())==11 && parseInt($("#select_m2 option:selected").val())==1 || parseInt($("#select_m2 option:first").val())==12 && parseInt($("#select_m2 option:selected").val())!=12)
        year = year+1;
        $("#year2").val(year);
        //alert($("#select_m1 option:selected").val());
        var dayCount = new Date(year, parseInt($("#select_m2 option:selected").val()), 0).getDate();
        var m = (month.getMonth()+1).toString();
        if (m.length==1)
        	m = "0"+m;
        $("#select_d2").empty();
        if ($("#select_m2 option:selected").val() == m)
        {
        	for(var i = month.getDate(); i<=dayCount; i++)
	        {
	                addDay = i.toString();
	                if (addDay.length==1)
	                	addDay = "0"+addDay;
	                $('#select_d2').append('<option value="'+addDay+'">'+addDay+'</option>');
	        }
	        if ($("#select_d2").get(0).options.length > day-month.getDate()+1)
	        {
                var value = day;
                if (value.length==1)
	                	value = "0"+value;
	        	$("#select_d2 option[value='"+value+"']").attr('selected', 'selected');
	        }
	        else
	        	$("#select_d2 option:last").attr('selected', 'selected');
        }
        else
        {
	        for(var i = 1; i<=dayCount; i++)
	            {
	                addDay = i.toString();
	                if (addDay.length==1)
	                	addDay = "0"+addDay;
	                $('#select_d2').append('<option value="'+addDay+'">'+addDay+'</option>');
	            }
	        if ($("#select_d2").get(0).options.length > day)
	        {
                var value = day;
                if (value.length==1)
	                	value = "0"+value;
	        	$("#select_d2 option[value='"+value+"']").attr('selected', 'selected');
	        }
	        else
	        	$("#select_d2 option:last").attr('selected', 'selected');
        }
        if (parseInt($("#select_m2 option:selected").val()) == parseInt($("#select_m2 option:last").val()))
        {
        	$("#select_d2").empty();
        	for(var i = 1; i<=month.getDate(); i++)
	        {
	                addDay = i.toString();
	                if (addDay.length==1)
	                	addDay = "0"+addDay;
	                $('#select_d2').append('<option value="'+addDay+'">'+addDay+'</option>');
	        }
	        if ($("#select_d2").get(0).options.length > day)
	        {
                var value = day;
                if (value.length==1)
	                	value = "0"+value;
	        	$("#select_d2 option[value='"+value+"']").attr('selected', 'selected');
	        }
	        else
	        	$("#select_d2 option:last").attr('selected', 'selected');
        }
    });
    $("#date_start").click(function() {
    	if (date_start)
    	{
    		$("#tcal").hide();
    		date_start = false;
    	}
    	else
    	{
    		$("#tcal").show();
    		date_start = true;
    	}
    	date_finish = false;
	  });
    $("#date_finish").click(function() {
    	if (date_finish)
    	{
    		$("#tcal").hide();
    		date_finish = false;
    	}
    	else
    	{
    		$("#tcal").show();
    		date_finish = true;
    	}
    	date_start = false;
	  });
    $('.poisk_click').click(function()
    {
    	if (date_start)
    	{
    		var mon = $(this).attr('id').substr(0,2);
    		var day = $(this).attr('id').substr(3,2);
    		$("#select_m1 option[value='"+mon+"']").attr('selected', 'selected');
    		$("#select_m1").change();
    		$("#select_d1 option[value='"+day+"']").attr('selected', 'selected');
    		$("#tcal").hide();
    		date_start = false;
    	}
    	if (date_finish)
    	{
    		var mon = $(this).attr('id').substr(0,2);
    		var day = $(this).attr('id').substr(3,2);
    		$("#select_m2 option[value='"+mon+"']").attr('selected', 'selected');
    		$("#select_m2").change();
    		$("#select_d2 option[value='"+day+"']").attr('selected', 'selected');
    		$("#tcal").hide();
    		date_finish = false;
    	}
    });
});
