var fade_timeout=5000;
var timer=0;

function chk() {
    start_loading();
    var ok=true;
    $("input.req").each(function (i) {
        if (this.value === "") {
            this.style.backgroundColor = "#FFEFCE";
            ok=false;
        }
    });
    if (!ok) last_event("Недостаточно данных");
    return ok;
}

function last_event(msg) {
    end_loading();
    if (msg=='') {
        $("#last_event",top.document).text('');
        return;
    }
    $("#last_event",top.document).html(msg);
    timer=fade_timeout;
    setTimeout("hide()", 1000);
}

function hide(){
    if (timer<=0) $("#last_event",top.document).text('');
    else {
        timer=timer-1000;
        setTimeout("hide()", 1000);
    }
}


function end_loading() {
    $("#loading",top.document).removeClass("loading");
}

function start_loading() {
    $("#loading",top.document).addClass("loading");
}

function reload_page(ifno) {
    if (ifno=='top') {
        top.document.location.replace(top.document.location);

        $("#iframe0").contents().html("").height(0);
        $("#iframe1").contents().html("").height(0);
    }
    if (ifno=='iframe0') {
        ifr=parent.document.getElementById(ifno);
        if (ifr!=null) ifr.contentDocument.location.replace(ifr.contentDocument.location);

        $("#iframe1").contents().html("").height(0);
    }
    if (ifno=='iframe1') {
        ifr=parent.document.getElementById(ifno);
        if (ifr!=null) ifr.contentDocument.location.replace(ifr.contentDocument.location);
    }
}

function set_curr(tr) {
    $("#strip tr").removeClass("sel");
    tr.addClass("sel");
    start_loading();
}

function chk_curr() {
    if ($("#curr_id").val()==='') {
      last_event('Не выбрана текущая запись');
      return false;
    } else return true;
}

function strip_func(base,ty,del) {
    $("#strip tr").each(function() {
        var tr=$(this);
        var id=$(this).attr('id');

        switch (ty) {
        case 0: ad='_edit'; break;
        case 1: ad='_case';
        }

        $(this).find("a").attr('href',base+ad+'.php?id='+id).click(function (o) { set_curr(tr); });

        if (del) {
            $(this).find(".del").click(function (o) {
                if (confirm("Удалить запись?")) document.location=base+"_del.php?id="+id;
            })
        }
    });
}

function toggle_access(ty, menu_id, access_idx) {
    var res=false;
    $.ajax({
        async: false,
        type: "POST",
        url: "ajax/__toggle_access.php",
        data: "ty="+ty+"&mid="+menu_id+"&aidx="+access_idx,
        success: function(msg){
            if (msg=="OK") res=true; else last_event('NOK');
        }
    });
    return res;
}

