var triggerAstericsHide = function (obj) {

    var value =  obj.val();
    var value_placeholder = obj.attr('placeholder');
    var css_id = obj.attr('id');
    var labelr =  'label[for="'+css_id +'"] span.red';

    if (value.length > 0 && value != value_placeholder) {

        $(labelr).css({
            color:'white'
        });
    }
    else if (value.length==0 || value == value_placeholder) {

        $(labelr).css({
            color:'red'
        });
    }
}

function AstericsHide()
{
    $(".necessary").keyup (function(){
        triggerAstericsHide($(this));
    }).blur(function(){
        triggerAstericsHide($(this));
    }).change(function(){
        triggerAstericsHide($(this));
    });

    $(".necessary_select").change(function()
    {
        var css_id = $(this).attr('id');
        var labelr = 'label[for="'+css_id +'"] span.red' ;

        var value =  $(this).val();

        if (value=="-1" || value=="") {
            $(labelr).css({
                color:'red'
            });
        } else {
            $(labelr).css({
                color:'white'
            });
        }
    });
}

function cleanupModals()
{
    $(".ui-dialog").remove();
    $('#formphysic').remove();
    $('#urlico').remove();
}

$('#cabinet a.notlogged, #login-url, #site_sections div.profile:not(.profile-enabled) a').live('click', function()
{
    $("#btStop0").show();
    $("#btSwitchLeft0").hide();
    $("#btSwitchRight0").hide();
    $("#btStop").click();
    one_load_flag = 1;
    var url = this.href;
    var st = document.createElement("link");
    st.setAttribute("rel","stylesheet");
    st.setAttribute("href","/web/css/apply.css");
    document.body.appendChild(st);
    var ms;
    Today = new Date();
    ms = Today.getMilliseconds();
    $.post(
        url,
        {
            randm_kod: ms
        },
        function(data, textStatus) {

            modal = $(data).estdialog({
                modal: true,
                dialogClass :'login-window ui-state-hover est-dialog-notitle',
                width  : '25.5em',
                resizable: false,
                draggable: false,
                close: function(event, ui) {
                    $("#modal-div").remove();
                    $(this).estdialog('destroy');
                    cleanupModals();
					$(".login-window").remove();
                },
				open: function(event, ui) {
					$("#auth_username").focus();
				}
            });
            AstericsHide();
        }
        );
    return false;
});


$('#login-form').live('submit', function()
{
    var url = this.action;
    var data = $(this).serializeArray();
    $.post(
        url,
        data,
        function(json_data)
        {
            if (json_data=="202")
            {
                var user = $('#auth_username').val();
                var host = window.location.host.replace(/[a-z0-9_-]+\.(est.+|bm\d*\.thotal.+)/gi, '$1');
                var url = 'http://' + host + '/cabinet/profile';

                $(".ui-dialog").remove();
                top.location.href = url;
            }
            else
            {
                $ (".error_block").html (json_data);
            }
        },
        "json"
        );
    return false;
});

