﻿function UpdateAllChildren(nodes, checked) {
    var i;
    for (i = 0; i < nodes.get_count(); i++) {
        if (checked) {
            nodes.getNode(i).check();
        }
        else {
            nodes.getNode(i).set_checked(false);
        }

        if (nodes.getNode(i).get_nodes().get_count() > 0) {
            UpdateAllChildren(nodes.getNode(i).get_nodes(), checked);
        }
    }
}

function clientNodeChecked(sender, eventArgs) {
    var childNodes = eventArgs.get_node().get_nodes();
    var isChecked = eventArgs.get_node().get_checked();
    UpdateAllChildren(childNodes, isChecked);
}

function Redirect(url, Seconds) {
    timer = setTimeout("window.location = '" + url + "'", Seconds * 1000);
}

function ToggleDiv(elem) {
    if (document.getElementByID(elem).display == 'none') {
        document.getElementByID(elem).display = 'inline';
    }
    else {
        document.getElementByID(elem).display = 'none';
    }
}

function ResizeRequester(max) {

    var divR = $('#divRequester');
    var tdT = $('#tblTitleRight');

    if (divR.height() > max) {
        divR.css('height', max);
        tdT.css('height', max);
    }
    else if (divR.height() < tdT.height()) {
        divR.css('height', tdT.height());
    }
    else if (divR.height() > tdT.height()) {
        tdT.css('height', divR.height());
    }
    
    url = 'url(~/GI.axd?ps=3&h=' + tdT.height() + ')';
    divR.css('background-image', url);
    divR.css('background-repeat', 'repeat-x');

    return;

}

function ResizeRequesterTo(h) {

    var divR = $('#divRequester');
    var tdT = $('#tblTitleRight');

    divR.css('height', h);
    tdT.css('height', h);

    url = 'url(~/GI.axd?ps=3&h=' + h + ')';
    divR.css('background-image', url);
    divR.css('background-repeat', 'repeat-x');

    return;

}

function ResizeLeftTitle(divLeft, tdLeft) {
    if ($('#' + divLeft).css('display') != 'none') {
        $('#' + tdLeft).css('height', $('#' + divLeft).height());
    }
}

function ResizeLeftTitleAny() {
    var strSearch = 'tdRDTitleRight.*';
    var strSearchDiv = 'divHeader.*';

    var divLeft = $('div').filter(function() {
        if ($(this).attr('id').search(strSearchDiv) > -1) {
            return true;
        }
    }
            )[0];

    var tdLeft = $('td').filter(function() {
        if ($(this).attr('id').search(strSearch) > -1) {
            return true;
        }
    }
            )[0];

    if (divLeft.style.display != 'none') {
        tdLeft.height = divLeft.offsetHeight - 2;
    }
}

var ShowDeeplinkSecurityViolationShown = false;

function ShowDeeplinkSecurityViolation() {

    if (!ShowDeeplinkSecurityViolationShown) {
        window.radalert('You have attempted to execute this report using parameters which contain Divisions or Communites you do not have security access rights to. We have tried our best to approximate the report by running it with those Divisions or Communities you do have security access to. Please note, these security approximations can also effect the selected Question. You may not have access to the Division for that question. In this case the first question in the list is used. Please contact your AVID Reports system administrator to request changes to your security rights on AVID Reports.', 500, 300, 'Security Violation');

        ShowDeeplinkSecurityViolationShown = true;
    }

    return false;

}

var ShowDeeplinkSecurityViolationQuestionShown = false;

function ShowDeeplinkSecurityViolationQuestion() {

    if (!ShowDeeplinkSecurityViolationQuestionShown) {
        window.radalert('You have attempted to execute this report using parameters which contain a question that is not compatible with this report. The report was run using the default initial question for this report.', 500, 180, 'Question Incompatible');

        ShowDeeplinkSecurityViolationQuestionShown = true;
    }

    return false;

}

function ValidateBusinessUnit(bu) {

    var tree = $find(bu);
    var nodes = tree.get_allNodes();

    if (tree.get_checkedNodes().length > 0) {
        return true;
    }

    alert("Please select a division or community");
    return false;
}

function ToolbarMouseOver(toolbar, args) {
    if (args.get_item().get_text() == "CrossLink") {
        args.get_item().showDropDown();
    }
}

function ScrollToSelected(sender, eventArgs) {
    sender.get_selectedItem().scrollIntoView();
}

function ValidateDates(sp, ep, sy, ey) {

    var StartPeriod = $find(sp);
    var EndPeriod = $find(ep);
    var StartYear = $find(sy);
    var EndYear = $find(ey);

    if (SelectedTimePeriod == 'M' || SelectedTimePeriod == 'Q') {
        if (StartYear.get_selectedIndex() > EndYear.get_selectedIndex()) {
            alert("Start year is after end year.");
            return false;
        }
        else if ((StartPeriod.get_selectedIndex() > EndPeriod.get_selectedIndex()) && (StartYear.get_selectedIndex() >= EndYear.get_selectedIndex())) {
            alert("Start date is after end date.");
            return false;
        }
    }
    else if (SelectedTimePeriod == 'Y') {
        if (StartYear.get_selectedIndex() > EndYear.get_selectedIndex()) {
            alert("Start year is after end year.");
            return false;
        }
    }
    return true;
}

function SelectTimePeriod(obj, period) {
    SelectedTimePeriod = period;
}