$(document).ready(function() {

    $("#wait").html('<img src="images/wait.gif">');
    $("input[name='price'],input[name='age'],input[name='exp']").keypress(function (e) {
        if( e.which!=8 && e.which!=0 && (e.which<48 || e.which>57)) {
            return false;
        }
    });
    $(":text").keydown(function (e) {
        $(this).css("backgroundColor", '#fff')
    });
    $("#mark_select").change(function (e) {
        $("#model_select").css("backgroundColor", '#fff');
        $("#wait").html('<img src="images/wait.gif">');
        $.post("/public/kasko_calc/ajax.php", {query: "models", id: this.value}, function(models) {
            $("#model_select").html(models);
            $("#wait").html("");
        });
    });

    $.post("/public/kasko_calc/ajax.php", {query: "marks"}, function(marks) {
        $("#mark_select").html(marks);
        $("#wait").html("");
        $("#mark_select").change();
    });

    $("#age").blur(function() {
        if($(this).val() < 18) {
            alert('Некорректный возраст водителя.');
            $(this).css("backgroundColor", '#CC0000');
            return false;
        }
        else {
            $(this).css("backgroundColor", '#fff');
            return true;
        }
    });
    $("#exp").blur(function() {
        if(($("#age").val() - $(this).val()) < 18) {
            alert('Некорректный стаж водителя.');
            $(this).css("backgroundColor", '#CC0000');
            return false;
        }
        else {
            $(this).css("backgroundColor", '#fff');
             return true;
        }
    });
    $(":radio[name=sex]").click(function() {
        $("#sex_error").html("");
    });
    $(".btn-more a").click(function() {
        $(".calc form").submit();
        return false;
    });
});



function getModels(mark_id) {
    $("#model_select").css("backgroundColor", '#fff');
    $.post("ajax.php", {query: "models", id: mark_id}, function(models) {
        $("#model_select").html(models);
    });
}



function checkint(obj) {
    if (/^([0-9]+)$/.test(obj.val())) {
        obj.css("backgroundColor", '#fff');
        return true;
    }
    else {
        obj.css("backgroundColor", '#CC0000');
        return false;
    }
}



function checkform() {
    var error = false;
    if($("#model_select").val() == "Модель") {
        $("#model_select").css("backgroundColor", '#CC0000');
        error = true;
    }
    else {
        $("#model_select").css("backgroundColor", '#fff');
    }
    if($("#year").val() == "г.в.") {
        $("#year").css("backgroundColor", '#CC0000');
        error = true;
    }
    else {
        $("#year").css("backgroundColor", '#fff');
    }
    if (!checkint($("#price"))) {
        error = true;
    }
    if (!checkint($("#power"))) {
        error = true;
    }
    if (!checkint($("#age"))) {
        error = true;
    }
    if (!checkint($("#exp"))) {
        error = true;
    }
    if (($(":radio[name=sex]").filter(":checked").val() != 'man') && ($(":radio[name=sex]").filter(":checked").val() != 'woman')) {
        $("#sex_error").html("Вы не указали пол водителя");
        error = true;
    }
    else {
        $("#sex_error").html("");
    }
    if(($("#age").val() - $("#exp").val()) < 18) {
        $("#exp").css("backgroundColor", '#CC0000');
        error = true;
    }
    else {
        $("#exp").css("backgroundColor", '#fff');
    }
    if($("#age").val() < 18) {
        $("#age").css("backgroundColor", '#CC0000');
        error = true;
    }
    else {
        $("#age").css("backgroundColor", '#fff');
    }
    if (error == false) {
        $("#step1").submit();
    }

}

