﻿$(function () {

    UpdateStatus();
    FirstTest();

    $(".DropDownListJudet").val("0");
    $(".DropDownListJudet").val("");
    $("div.td_loading").fadeOut(100);

    $(".next_link").click(function () {
        StartLoading();
        if ($(".DropDownListJudet").val() == "" || $(".DropDownListJudet").val() == 0 ||
            $(".DropDownListLocalitate").val() == "" || $(".DropDownListLocalitate").val() == 0 ||
            $(".DropDownListTipPersoana:checked").val() == "" || $(".DropDownListTipPersoana:checked").val() == 0 ||
            $(".DropDownListTipVehicul").val() == "" || $(".DropDownListTipVehicul").val() == 0 ||
            $(".DropDownListMarca").val() == "" || $(".DropDownListMarca").val() == 0) {
            alert("Completati toate campurile!");
            UpdateStatus();
            StopLoading();
            return;
        }
        $.ajax({
            url: "STSession.aspx?tp=" + $(".DropDownListTipPersoana:checked").val() +
                                    "&tv=" + $(".DropDownListTipVehicul").val() +
                                    "&j=" + $(".DropDownListJudet").val() +
                                    "&l=" + $(".DropDownListLocalitate").val() +
                                    "&m="+ $(".DropDownListMarca").val()+
                                    "&r=" + Math.random(),
            success: function (data, status, xmlreq) {
                window.location = "CalculatorRca.aspx";
            }
        });
        UpdateStatus();
        return false;
    })

    $(".DropDownListJudet").change(function () {
        StartLoading();
        $(".DropDownListLocalitate").html("").html("<option value='0'>Se incarca...</option>");
        // apel ajax
        if ($(".DropDownListJudet").val() == "" || $(".DropDownListJudet").val() == 0) {
            $(".DropDownListLocalitate").html("").html("<option value='0'>Selectati judetul</option>");
            UpdateStatus();
            StopLoading();
            return;
        } else {
            $.ajax({
                url: "Loadloc.aspx?cj=" + $(".DropDownListJudet").val() + "&r=" + Math.random(),
                success: function (data, status, xmlreq) {
                    $(".DropDownListLocalitate").html(data);
                    UpdateStatus();
                }
            });
        }
        StopLoading();
    });

    $(".DropDownListTipVehicul").change(function () {
        StartLoading();
        UpdateStatus();
        StopLoading();
    });

    $(".DropDownListLocalitate").change(function () {
        StartLoading();
        UpdateStatus();
        StopLoading();
    });

    $(".DropDownListTipPersoana").change(function () {
        StartLoading();
        UpdateStatus();
        StopLoading();
    });
    $(".DropDownListMarca").change(function () {
        StartLoading();
        UpdateStatus();
        StopLoading();
    });

    function UpdateStatus() {
        var step = 0;
        if ($(".DropDownListTipPersoana:checked").val() != "" && $(".DropDownListTipPersoana:checked").val() != 0) {
            step++;
        }
        if ($(".DropDownListTipVehicul").val() != "" && $(".DropDownListTipVehicul").val() != 0) {
            step++;
        }
        if ($(".DropDownListJudet").val() != "" && $(".DropDownListJudet").val() != 0) {
            step++;
        }
        if ($(".DropDownListLocalitate").val() != "" && $(".DropDownListLocalitate").val() != 0) {
            step++;
        }

        $("#flux_loader01").stop().animate(
        { backgroundPosition: "(" + calcPos(step) + "px 0px)" },
        { duration: 1000 });
        $("#completat").text(calcPerc(step));

        if (step == 4) {
            $(".td_next").css("visibility", "visible");
        } else {
            $(".td_next").css("visibility", "hidden");
        }

    }

    function FirstTest() {
    }

    function calcPos(nr) {
        var total = $("#hidden_total_controls").text();
        var val = (100 * (nr + 1 - 1)) / total;
        bgPos = 500 - val / 100 * 500;
        return -(Math.round(bgPos));
    }
    function calcPerc(nr) {
        var total = $("#hidden_total_controls").text();
        return Math.round((100 * (nr + 1 - 1)) / total);
    }

    function StartLoading() {
        $("div.td_loading").fadeIn();
    }

    function StopLoading() {
        $("div.td_loading").fadeOut(500);
    }

}); 
