$(document).ready(function() {
    var popup = $("#popup");

    if (popup == undefined || popup.val() != "True") {
        $("#dialog").dialog({
            autoOpen: false,
            width: 780,
            modal: true,
            resizable: true,
            close: function() {
                $("li.newelement").remove();
                $("li.edited").removeClass("edited");
            }
        });
    }
});

function deleteDocuments() {

    var selected = $("#documents").find("tr.selected");
    if (selected.length == 0) { alert(CompanyResources.NoDocumentsSelected); return false; }
    
    if (!confirm(CompanyResources.SureDelete + '(' + selected.length + ')?')) return false;

    var selectedDocumentIds = selected.find(".rowid").map(function() {
        return $(this).val();
    }).get();
    
    try {
        $('#documents').load(base + 'companydocument.ivp/deletedocuments', { nolayout: true, id: selectedDocumentIds, companyId: $('#id').val() }, function() {
            selectedIds.length = 0;
            initTable();
            setTab('documents');
        });
    } catch(e) {
         alert(e.message);
    }
   
    return false;
}

function documentHistory(id) {
    openDialog('companydocumenthistory.ivp/list/' + id, 'History');
    return false;
}

function addFile() {
    $("#dialog").load(base + 'companydocument.ivp/create', { companyId: $('#id').val() }, function () {
        $("#dialog").dialog("option", "title", FormatString(CompanyResources.NewDocument));
        setParametersForDocument();
        $("#dialog").dialog("open");
    });
    return false;
}

function viewFile(link, id, tab) {
    $("#dialog").load(base + 'companydocument.ivp/view/' + id, function () {
        $("#dialog").dialog("option", "title", FormatString(CompanyResources.EditDocument));
        setParametersForDocument();
        $("#dialog").dialog("option", "minHeight",0);
        $("#dialog").dialog("open");
        if (tab != undefined)
            $("a#a" + tab).click();
    });
    return false;
}

function setParametersForDocument() {
    
    $("#documentfile").validate({
        submitHandler: function (form) {
            $(form).ajaxSubmit({
                success: function () {
                    dialogClose();
                    loadDocuments(true);
                }
            });
        }
    });

    $('#documentfile input.datetimeRU').calendar({ dateFormat: '%d.%m.%Y' });

    setHelpLinkEvents($('#linkdocumentRelatedHelp'));
    setHelpLinkEvents($('#linkdocumentHelp'));
    setHelpLinkEvents($('#linkdocumentIsActiveHelp'));

    $("#isRelatedToServices").click(function () {
        $('#documentfile #tabs').toggle();
        $("#isRelatedToServices").val($("#isRelatedToServices").val()== "True"?"False":"True");
    });

    $("#arules").click(function() {
         return loadRules();
    });

    $(".disabled").attr("disabled", "disabled");

}

function dialogClose() {
    $("#dialog").dialog("close");
    $("#dialog").html("");
}

function ChangedFile(link) {
    $('input.filechange').removeClass('hidden');
    $('.file').addClass('required');
    $(link).addClass('hidden');
    $("#actualFile").hide();

}

function SelectedType() {    
    if ($('#CompanyDocumentTypeId option:selected').val() == "2") {        
        $('.req').addClass('required');
        $('.contract').removeClass('hidden');

        $('#NameFromUser').val(DocumentResources.Contract);
    }
    else {
        $('.req').removeClass('required');
        $('.contract').addClass('hidden');

        $('#NameFromUser').val('');
    }
}

function changeFile(sender) {
    var path = $("#cdfile").val();
   
    if (path) {
        var name = path.split("\\");
        if(name.length > 1)
            $('#NameFromUser').val(name[name.length - 1]);
        else {
            $('#NameFromUser').val(name[0]);
        }
    }
    $("#error").remove();
    var file = sender.files[0];
    if (file.size !== 0) {
        $(sender).removeClass("red");
              
    } else {        
        $(sender).addClass("red");
        $('#companyDocumentFile').append('<div id="error" class="red">'+'Файл ' + '"' + file.name + '"' + ' пуст.'+'</div>');        
    }
}

function setHelpLinkEvents(obj) {
    if (obj != undefined) {
        var id = obj.attr("id");
        if (id != undefined) {
            var i = id.indexOf('link') + 4;
            var idHelp = id.substring(i, id.length);
            obj.mouseover(function () { $('#' + idHelp).show(); });
            obj.mouseout(function () { $('#' + idHelp).hide(); });
        }
    }
}

function setPopupTab(id) {
    if (!id) return;
    if (!documentReady) return;

    $('.popup .tab').each(function (i, el) { if (el.id == id) $(el).show(); else $(el).hide(); });
    $('.popup>#tabs>ul>li>a').each(function (i, el) { if (el.id == 'a' + id) $(el).addClass('selected'); else $(el).removeClass('selected'); });
    return false;
}
