$(document).ready(function()
{
    $(".selectManager").attr("disabled", "disabled");
    
    $(".propoziciiButton").click(function()
    {
        $(".Reklama").slideUp(500);
        
        if ($(".сkargi_ta_propozicii").is(":hidden"))
        {
            $(".сkargi_ta_propozicii").slideDown(500);
        }
        else
        {
            $(".сkargi_ta_propozicii").slideUp(500);
            clearForm();
        }
    });
    
    
    $(".propoziciiButtonCancel").click(function()
    {
        $(".сkargi_ta_propozicii").slideUp(500);
        clearForm();
    })
    
    
    $(".selectClub").change(function()
    {
        var clubId = $(".selectClub option:selected").val();
        $(".selectManager").html("");
        
        $.ajax({
            type: 'POST',
            url: "/poslugi/",
            data: {"club": clubId},
            success: function(data)
            {
                var data = JSON.parse(data);
                var options = "<option value='' selected></option>";
                
                for(i=0; i<data.length; i++)
                {
                    options += "<option value='"+data[i]['id']+"'>"+data[i]['name']+"</option>";
                }
                $(".selectManager").append(options);
                $(".selectManager").removeAttr("disabled");
            }
        });
        
    })
    
    
    
    $(".propozicii_send_button").click(function()
    {
        $(".selectClub").parent(".clubSelected").addClass("propoziciiNormal");
        $(".selectManager").parent(".managerSelected").addClass("propoziciiNormal");
        
        var clubId      = $(".selectClub option:selected").val();
        var managerId   = $(".selectManager option:selected").val();
        var text        = $(".textPropozicii").val();
        
        if(clubId == "")
        {
            $(".selectClub").parent(".clubSelected").removeClass("propoziciiNormal");
            $(".selectClub").parent(".clubSelected").addClass("propoziciiRed");
        }
        else
        {
            $(".selectClub").parent(".clubSelected").removeClass("propoziciiRed");
            $(".selectClub").parent(".clubSelected").addClass("propoziciiNormal");
        }
        
        if(managerId == "")
        {
            $(".selectManager").parent(".managerSelected").removeClass("propoziciiNormal");
            $(".selectManager").parent(".managerSelected").addClass("propoziciiRed");
        }
        else
        {
            $(".selectManager").parent(".managerSelected").removeClass("propoziciiRed");
            $(".selectManager").parent(".managerSelected").addClass("propoziciiNormal");
        }
        
        if(text == "")
        {
            $(".textPropozicii").parent(".propoziciiText").removeClass("propoziciiTextNormal");
            $(".textPropozicii").parent(".propoziciiText").addClass("propoziciiTextRed");
        }
        else
        {
            $(".textPropozicii").parent(".propoziciiText").removeClass("propoziciiTextRed");
            $(".textPropozicii").parent(".propoziciiText").addClass("propoziciiTextNormal");
        }
        
        
        if(clubId != "" && managerId != "" && text != "")
        {
            $.ajax({
                type: 'POST',
                url: "/poslugi/",
                data: {"сomplaints": "true", "clubId": clubId, "managerId": managerId, "text": text},
                success: function(data)
                {
                    $(".сkargi_ta_propozicii").slideUp(500);
            		clearForm();
                }
            });
            
            
            
        }
        
        
    });
    
    function clearForm()
    {
        $(".selectClub").val("");
        $(".selectManager").val("");
        $(".textPropozicii").val("");
    }
});
