function todo() {
    alert("NOT IMPLEMENTED BIATCH")
}
function simpleSwap(toHide, toShow) {
    simpleSwapHelper(toHide, toShow, 'block');
}
function simpleSwapDefault(toHide, toShow) {
    simpleSwapHelper(toHide, toShow, '');
}
function simpleSwapInline(toHide, toShow) {
    simpleSwapHelper(toHide, toShow, 'inline');
}
function simpleSwapHelper(toHide, toShow, displayValue) {
    if (typeof(toHide) == 'object') {
        toHide.style.display = 'none';
    } else {
        document.getElementById(toHide).style.display = 'none';
    }
    if (typeof(toShow) == 'object') {
        toShow.style.display = displayValue;
    } else {
        document.getElementById(toShow).style.display = displayValue;
    }
}
function toggleBlockAndHandle(id) {
    toggleNodeAndImage(id, false, false);
    toggleNodeAndImage(id + 'Handle', false, false, true);
}
function toggleWholeReview(nodeName) {
    toggleNodeAndImage(nodeName, false, false);
    nodeName = "short_summary" + nodeName;
    toggleNodeAndImage(nodeName, false, false, true);
}
function toggleActivity(nodeName) {
    nodeName = "activity_log" + nodeName;
    toggleNodeAndImage(nodeName, false, false);
}
function expandAll(ids, prefix) {
    toggleAll(ids, true, false, prefix);
}
function collapseAll(ids, prefix) {
    toggleAll(ids, false, true, prefix);
}
function expandSelected(allIds, selectedIds, prefix) {
    //first collapse all
    toggleAll(allIds, false, true, prefix);
    toggleAll(selectedIds, true, false, prefix);
}
function expandAllReviews(ids) {
    toggleAll(ids, true, false);
    toggleAll(ids, false, true, 'short_summary', true)
    return false;
}
function collapseAllReviews(ids) {
    toggleAll(ids, false, true);
    toggleAll(ids, true, false, 'short_summary', true)
    return false;
}
function toggleType(ids, prefix, inline) {
    toggleAll(ids, false, false, prefix, inline)
}
function toggleBasic(nodeName) {
    toggleNodeAndImage(nodeName, false, false, false)
}
function collapseBasic(nodeName) {
    toggleNodeAndImage(nodeName, false, true, false)
}
function expandBasic(nodeName) {
    toggleNodeAndImage(nodeName, true, false, false)
}
function toggleInline(nodeName) {
    toggleNodeAndImage(nodeName, false, false, true)
}
function toggleAll(ids, forceOpen, forceClose, prefix, inline) {
    if (prefix == null) prefix = '';
    for (var i = 0; i < ids.length; i++) {
        var theNode = prefix + ids[i];
        toggleNodeAndImage(theNode, forceOpen, forceClose, inline);
    }
    return false;
}
function toggleNodeAndImage(nodeName, forceOpen, forceClose, inline) {
    var node = document.getElementById(nodeName);
    if (node == null) return;
    var img = document.getElementById(nodeName + 'img');
    if (img != null) var swapImage = true;
    var open = (node.style.display == 'none');
    open = (!forceClose) && (forceOpen || open);
    if (open) {
        if (node.style.display == 'none') {
            if (inline) {
                node.style.display = 'inline';
            } else {
                node.style.display = 'block';
            }
            if (swapImage) img.src = fishEyePageContext + '/' + fishEyeSTATICDIR + '/images/arrow_open.gif';
        }
    } else {
        if (node.style.display == 'block' || node.style.display == '' || node.style.display == 'inline') {
            node.style.display = 'none';
            if (swapImage) img.src = fishEyePageContext + '/' + fishEyeSTATICDIR + '/images/arrow_closed.gif';
        }
    }
    return;
}

var currentTab;
var currentBucket;
// rewrote so we don't set styles we don't have to.
function selectTab(tab) {
    if (currentTab) {
        currentTab.className = '';
        currentBucket.style.display = 'none';
    }
    currentTab = document.getElementById(tab + 'Tab');
    currentBucket = document.getElementById(tab + 'Bucket');
    currentTab.className = 'active';
    currentBucket.style.display = '';
}
function clearField(obj, defaultValue) {
    if (obj.value == defaultValue) {
        obj.value = '';
    }
}

function rollover(obj) {
    if (obj.tagname = 'IMG') {
        var imgsrc = obj.src.replace(/\.gif$/, '');
        obj.src = imgsrc + '_over.gif';
    }
    return false;
}

function rollout(obj) {
    if (obj.tagname = 'IMG') {
        var imgsrc = obj.src.replace(/\_over\.gif$/, '.gif');
        obj.src = imgsrc;
    }
    return false;
}
function toggleOverflow(handle, element) {
    var el = $(element);
    var hd = $(handle);
    if (el.style.overflow == 'hidden' || el.style.overflow == '') {
        el.style.overflow = 'visible';
        el.style.height = 'auto';
        hd.src = hd.src.replace(/expand\.gif$/, 'collapse.gif')
    } else {
        el.style.overflow = 'hidden';
        el.style.height = '1.3em';
        hd.src = hd.src.replace(/collapse\.gif$/, 'expand.gif')
    }
}


function submitDefaultForm(command) {
    document.defaultForm.command.value = command;
    document.defaultForm.submit();
}

function showDiffForm(frxid) {
    simpleSwapInline('updateFrxFromFormToggle' + frxid, 'updateFrxFromFormSpan' + frxid);
    toggleNodeAndImage('frxinner' + frxid, true, false);
}

function toggleWording(handle) {
    var handleEl = $(handle);
    var handleText = handleEl.innerHTML.substring(0, 4);
    switch (handleText) {
        case 'Show':
            handleEl.innerHTML = handleEl.innerHTML.replace(/^Show/, 'Hide');
            return true;
        case 'Hide':
            handleEl.innerHTML = handleEl.innerHTML.replace(/^Hide/, 'Show');
            return true;
        case 'Expa':
            handleEl.innerHTML = handleEl.innerHTML.replace(/^Expand/, 'Collapse');
            return true;
        case 'Coll':
            handleEl.innerHTML = handleEl.innerHTML.replace(/^Collapse/, 'Expand');
            return true;
        case 'More':
            handleEl.innerHTML = handleEl.innerHTML.replace(/^More/, 'Less');
            return true;
        case 'Less':
            handleEl.innerHTML = handleEl.innerHTML.replace(/^Less/, 'More');
            return true;
    }
    return false;
}

function toggleBasicAndWording(toToggle, handle) {
    toggleBasic(toToggle);
    toggleWording(handle);
}

function toggleChangeDiffButtons(handle) {
    toggleType(frxIdsOnThisPage, 'changeDiffButton', true);
    toggleWording(handle);
}

function show(toShow, bool) {
    var ob = $(toShow);
    if (ob) {
        if (bool) {
            ob.style.display = 'block';
        } else {
            ob.style.display = 'none';
        }
    }
}

function editInfoBox(field, over) {
    try {
        var cte = $('cte');
        if (over) {
            if (cte && (cte.parentNode != field)) {
                cte.parentNode.removeChild(cte);
            }

            Element.addClassName(field, 'editable');
            if (!Element.hasClassName(field, 'disabled')) {
                if (!cte) {
                    field.innerHTML = field.innerHTML + '<span id="cte" class="cte">Click to Edit</span>'
                }
            }
        } else {
            Element.removeClassName(field, 'editable');
            if (cte) {
                cte.parentNode.removeChild(cte);
            }
        }
    } catch(ex) {
        //        window.alert(ex);
    }
}

function editInfoBoxActivate(event, el) {
    event = event || window.event;
    var target = event.target || event.srcElement;
    if (target && (target.tagName.toLocaleLowerCase() == "a")) {
        return true; // be nice to anchors
    } else {
        simpleSwap('details', 'editDetails');
        return true;
    }
}

//This validates the saneness of the team selection removing and disabling the
//selection of author and moderator as reviewers.
function validateTeamSelection() {
    var mod = document.editDetailsForm.newModerator.value;
    var auth = document.editDetailsForm.newAuthor.value;
    var reviewers = document.editDetailsForm.reviewers;
    if (reviewers && reviewers.length) {
        for (var i = 0; i < reviewers.length; i++) {
            if (reviewers[i].value == mod || reviewers[i].value == auth) {
                reviewers[i].checked = false;
                reviewers[i].disabled = true;
            } else {
                reviewers[i].disabled = false;
            }
        }
    } else if (reviewers) {
        reviewers.checked = false;
        reviewers.disabled = true;
    }
}

function showEditDetailsForm(focus) {
    var focusid = $(focus) ? $(focus).id : focus;
    simpleSwap('details', 'editDetails');
    $(focusid).focus();
}

var editDetailsFormChange = false;
var editDetailsRolesChange = false;
function markChanged(rolesChanged) {
    editDetailsFormChange = true;
    if (rolesChanged) {
        editDetailsRolesChange = true;
    }
}

function submitDetailsForm() {
    //IE displays tabs incorrectly after form submission, so reload the page (CRUC-170)
    var cruc170Hack = navigator.userAgent.indexOf("MSIE") > -1;

    if (editDetailsRolesChange || cruc170Hack) {
        var refreshPage = function() {
            window.location = fishEyePageContext + '/cru/' + permaId + '/';
        }
        postEditDetailsForm(refreshPage);
    } else {
        postEditDetailsForm();
    }
}

function checkEditForm(donext) {
    if (editDetailsFormChange) {
        postEditDetailsForm(donext);
    } else {
        if (donext) {
            donext();
        }
    }
    return false;//do a link action if called from <a>
}

function checkEditAndSubmitThis(formElement) {
    var submitter = function() {
        formElement.form.submit();
    }
    checkEditForm(submitter);
}

function checkEditFormAndLink(url) {
    var submitter = function() {
        window.location = url;
    }
    checkEditForm(submitter);
}

//todo remove this test and make it server side since it can only run from the review page see CBL-607
function anyReviewers() {
    var form = document.editDetailsForm;
    //don't bother checking if the form doesn't exist just return true. The user has decided to approve
    //from the dashboard. fix in CBL-607
    if (!form) {
        return true;
    }
    var reviewers = form.reviewers;
    for (var i = 0; i < reviewers.length; i++) {
        if (reviewers[i].checked) return true;
    }
    return false;
}

function command(cmd, pid, button) {
    var perma = (pid) ? pid : permaId;
    if (button) button.disabled = true;
    var donext = function() {
        var url = fishEyePageContext + '/cru/' + perma + '/';
        window.location = url + cmd;
    }
    //check and post the editDetailsForm if it has changed
    checkEditForm(donext);
}

function showLinkedReview(el) {
    simpleSwapInline(el, 'linkReviewForm');
    $('parentReviewId').focus();
}

function onEnter(e, func) {
    var key = e.which || e.keyCode;
    var el = e.target || e.srcElement;
    if (key == 13 && el.type != 'textarea') {
        Event.stop(e)
        if (func) func();
    }
}

function onEnterAndTab(e, func) {
    var key = e.which || e.keyCode;
    var el = e.target || e.srcElement;
    if ((key == 13 || key == 9) && el.type != 'textarea') {
        Event.stop(e)
        if (func) func();
    }
}

function submitCruSearch(inputEl) {
    window.location = fishEyePageContext + "/cru/search?query=" + encodeURIComponent(inputEl.value);
}

function searchReviews(el) {
    window.location = fishEyePageContext + "/cru/search?query=" + encodeURIComponent(document.forms.searchForm["search.text"].value);
}
function searchComments(el) {
    window.location = fishEyePageContext + "/cru/commentSearch?search.text=" + encodeURIComponent(document.forms.searchForm["query"].value);
}

var ovAnk = false; //tracks whether the mouse is over a "real" anchor
function toggleSensitively(id) {
    if (!ovAnk) toggleBasic(id)
}

function disableFormEl(el, otherElName, value) {
    el.form.elements[otherElName].disabled = value;
    return true;
}
