$(document).ready(function() {
    $('select option:first').attr('selected', 'yes');
    $("a").click(function() {
        return false
    });
    $("select#veh").change(function() {
		$('span.err').each(function() {
			$(this).hide();
		});
        showFields();
		$('select#limit').trigger('change');
		
    });
    $("input[name='owner']").click(function() {
        if ($("input[name='owner']:checked").val() == '1') {
            $("select#agestazh").attr('disabled', '');
            $("select#limit").attr('disabled', '');
            $("label.labrad1").css("fontWeight", "bold");
            $("label.labrad2").css("fontWeight", "normal")
        } else {
            $("select#agestazh").attr('disabled', 'true');
            $('select#agestazh option:first').attr('selected', 'yes');
            $("select#limit").attr('disabled', 'true');
            $('select#limit option:first').attr('selected', 'yes');
            $("label.labrad1").css("fontWeight", "normal");
            $("label.labrad2").css("fontWeight", "bold")
        }
    });
    $("a.getcalc").click(function() {
        $("select").each(function(i) {
            if (this.disabled != true) {
                if ($('select#' + this.id + '').val() == '0') {
                    this.style.border = "1px solid #da1007";
					$(this).next('span.err').css('display','block');
                    $("#result").text('-')
                }
            }
        });
        var b = $("input[name='owner']:checked").val();
        $('#progress').html('<img src="ajax.gif">');
        var c = $("select#limit").val();
        var d = $("select#agestazh").val();
        var e = $("select#ter").val();
        var f = $("select#veh").val();
        var g = $("select#power").val();
        var h = $("select#srok").val();
        var j = $("select#bae").val();
        if (f == 0) {
            return
        }
        var k = '&func=osago&ajax=true' + '&typeFL=' + b + '&ter=' + e + '&power=' + g + '&ts=' + f + '&srok=' + h + '&bae=' + j + '&limit=' + c + '&agest=' + d;
        $.ajax({
            url: "env.php",
            data: k,
            dataType: "json",
            success: function(a) {
                $("#result").html(a.result + a.currency);
            }
        });
        $('#progress').html('')
    });
    $("a.print").click(function() {
        print()
    });
    function print() {
        window.print()
    };
    $("select").click(function() {
        $("#result").text('-')
    });
    function showFields() {
		$("select").each(function() {
			$(this).attr('disabled','');
		});
		var a = $("select#veh").val();
        var b = $("input[name='owner']:checked").val();
        if (b == "2") {
            $("#limit").attr('disabled', 'true');
            $("#agestazh").attr('disabled', 'true')
        } else {
            $("#limit").attr('disabled', '');
            $("#agestazh").attr('disabled', '')
        }		
        if (a == "11" || a == "12" || a == "13") {
            $("#limit option:first").attr('selected', 'yes');
			$("#limit").attr('disabled', 'true');
            $("#agestazh option:first").attr('selected', 'yes');
			$("#agestazh").attr('disabled', 'true');
            $("#power").attr('disabled', 'true');
            $("#power option:first").attr('selected', 'yes');
            $("#bae").attr('disabled', 'true');
            $("#bae option:first").attr('selected', 'yes')
        }
        if (a == "2" || a == "4" || a == "5" || a == "6" || a == "7" || a == "8" || a == "9" || a == "10") {
            $("#power").attr('disabled', 'true');
            $("#power option:first").attr('selected', 'yes')
        }
        if (a == "1" || a == "3") {
            $("select").attr('disabled', '')
        }

    }
	
	$('select#limit').change(function() {
		if ($(this).val() == 2)
			{
				$('select#agestazh option:first').attr('selected', 'yes')
				$('select#agestazh').attr('disabled','disabled');
			} else {
				$('select#agestazh').attr('disabled','');
			}
	});
	
    $("select").change(function() {
        $("#result").text('-');
        if ($('select#' + this.id + '').val() != '0') {
            this.style.border = "1px solid #339933"
			$(this).next('span.err').css('display','none');
        } else {
            this.style.border = "1px solid #da1007"
			$(this).next('span.err').css('display','block');
        }
    })
});
