/// <reference name="MicrosoftAjax.js"/>

function BeaconApps_QuestionsNav(ddl) {
    window.location.href = "#q" + ddl.value;
    ddl.value = "1";
}

function BeaconApps_Redirect(url) {
    window.location.href = url;
}

function BeaconApps_ToggleSummaryAnswers(box) {
    var isChecked = box.checked;
    for (i = 1; i <= g_questionCount; i++) {
        document.getElementById(g_questionsIndexID + "_imgSummaryToggle" + i).src = isChecked ? g_BeaconAppsImagesPath + "collapse.gif" : g_BeaconAppsImagesPath + "expand.gif";
        document.getElementById(g_questionsIndexID + "_summaryTable" + i).style.display = isChecked ? "block" : "none";
    }
}

function BeaconApps_OpenPrintInvoice(url, mid) {
    var newUrl = url + "&PONumber=" + document.getElementById(g_textID).value;
    //<!-- function Peak_PopupPage(grid, page, id, mid, title, height, width, left, top, showscroll, showmenu, resizeable) -->
    Peak_PopupPage(null, newUrl, 0, mid, 'PrintInvoice', 550, 550, 5, 5, 1, 0, 1);
}

function BeaconApps_PrintInvoice() {
    // If the user did not enter a PO Number, show an underlined 150px cell.
    // If the user entered a PO Number, show it with no underline.
    if (document.getElementById(g_hidPO).value == "") {
        document.getElementById(g_cellPO).innerHTML = "&nbsp;";
        document.getElementById(g_cellPO).style.borderBottom = "solid 1px black";
    }
    else {
        document.getElementById(g_cellPO).innerHTML = document.getElementById(g_hidPO).value;
        document.getElementById(g_cellPO).style.borderBottom = "";
    }
    window.print();
    window.close();
}

function BeaconApps_ResetReviewerFilters() {
    document.getElementById(g_ReviewerUnitsRootID + "txtRegID").value = "";
    document.getElementById(g_ReviewerUnitsRootID + "txtUnitID").value = "";
}

function BeaconApps_ResetHospitalFilters(txtName, ddlState, txtZip) {
    if (document.getElementById(txtName))
        document.getElementById(txtName).value = "";
    if (document.getElementById(ddlState))
        document.getElementById(ddlState).selectedIndex = 0
    if (document.getElementById(txtZip))
        document.getElementById(txtZip).value = "";
}

function BeaconApps_ValidateHospitalSearch(ddlState, txtZip, rowButton, rowLabel) {
    if (document.getElementById(ddlState) && document.getElementById(txtZip)) {
        if (document.getElementById(ddlState).selectedIndex < 1 && document.getElementById(txtZip).value.replace(/^\s\s*/, '').replace(/\s\s*$/, '') == '') {
            alert('You must select a state or type a zip code to conduct this search');
            if (document.getElementById(rowLabel)) {
                document.getElementById(rowLabel).style.display = "none";
            }
            return false;
        }
        if (document.getElementById(rowLabel)) {
            var displayval;
            if (document.addEventListener) {//if firefox, opera, safari..
                displayval = "table-row";
            }
            else {
                displayval = "block";
            }
            document.getElementById(rowLabel).style.display = displayval;
        }
        document.getElementById(rowButton).style.display = "none";
        return true;
    }
    else {
        button.style.visibility = "visible";
        return false;
    }
}

function BeaconApps_OpenPrintApplication(url) {
    var win = window.open(url, "PrintApplication", "width=700,scrollbars=1");
    win.focus();
}

function BeaconApps_PrintApplication() {
    window.print();
    window.close();
}

function BeaconApps_PrintApplicationPage(id) {
    if (document.getElementById(id)) {
        document.getElementById(id).style.display = "none";
        window.print();
        document.getElementById(id).style.display = "block";
    }
}

function BeaconApps_ShowReviews(gridid, mid, mode) {
    var page = g_BeaconAppsPath + "BeaconAppsReviews.aspx";
    var id = PeakGrid_GetKeyValue(gridid);
    //Peak_OpenPage(grid, page, id, mid, title/field, img, height, width, mode, top, left, showscroll, showmenu, resizeable)
    Peak_OpenPage(gridid, page, id, mid, "Application " + id + " Reviews", '', 600, 700, mode);
}

function BeaconApps_ShowAllReviewAnswers(gridid, mid, mode) {
    var page = g_BeaconAppsPath + "AllReviewersAnswers.aspx";
    var id = PeakGrid_GetKeyValue(gridid);
    //Peak_OpenPage(grid, page, id, mid, title/field, img, height, width, mode, top, left, showscroll, showmenu, resizeable)
    Peak_OpenPage(gridid, page, id, mid, "All " + id + " Reviewer Answers", '', 600, 700, mode);
}

function BeaconApps_ShowAppReviews(id, mid, mode) {
    var page = g_BeaconAppsPath + "BeaconAppsReviews.aspx";
    var img = "";
    //Peak_OpenPage(grid, page, id, mid, title/field, img, height, width, mode, top, left, showscroll, showmenu, resizeable)
    Peak_OpenPage(null, page, id, mid, "App " + id + " Reviews", img, 600, 700, mode);
}

/*** ASSIGN REVIEWERS METHODS ***/

// designed to allow validation and form submission on the AssignReviewers page
function BeaconApps_GetReviewersAndAppsByType(ddl, txt, hidden, lblApps, lblReviewers, lblAvailable, tblResults) {
    //    // hide results table if it is visible
    //    if(document.getElementById(tblResults))
    //        document.getElementById(tblResults).style.visibility = "hidden";

    Page_ClientValidate();
    if (Page_IsValid) {
        var res = document.getElementById(ddl).value;
        var text = document.getElementById(txt).value;

        if (res != "0" && text != "") {
            document.getElementById(hidden).value = "submit";
            document.forms[0].submit();
        }
        else {
            document.getElementById(hidden).value = "";
            BeaconApps_ClearAssignmentLabels(lblApps, lblReviewers, lblAvailable);
            document.forms[0].submit();
        }
    }
}

// Called from the AssignReviewers page when repopulating labels
function BeaconApps_ClearAssignmentLabels(lblApps, lblReviewers, lblAvailable) {
    document.getElementById(lblApps).value = "";
    document.getElementById(lblReviewers).value = "";
    document.getElementById(lblAvailable).value = "";
}

// This requires the page to register the dynamic js variable g_currentEvent
function BeaconApps_InactivationAlert(ddl) {
    if (ddl.checked == false) {
        alert("If you leave this box unchecked, the Assign Reviewers button will inactivate all existing reviewer assignments.");
    }
}

// This requires the page to register the dynamic js variable g_chkKeepExisting
function BeaconApps_ValidateAssignReviewers() {
    Page_ClientValidate();
    if (Page_IsValid) {
        if (document.getElementById(g_chkKeepExisting)) {
            if (document.getElementById(g_chkKeepExisting).checked == false) {
                return confirm("If you leave the 'Keep All Existing' checkbox unchecked, the Assign Reviewers button will inactivate all existing reviewer assignments. " +
                "Click the 'Cancel' button to change the checkbox or " +
                "click 'OK' to continue.");
            }
            else {
                return confirm('Are you sure you want to assign reviewers with the above criteria?');
            }
        }
    }
    return false;
}

// This requires the page to register the dynamic js variables g_hidRevPerAbs
function BeaconApps_SaveRevPerAbstract(ddl) {
    document.getElementById(g_hidRevPerAbs).value = ddl.value;
}

// This requires the page to register the dynamic js variables g_hidAbstractType
function BeaconApps_SaveAbstractType(ddl) {
    document.getElementById(g_hidAbstractType).value = ddl.value;
}

function BeaconApps_ShowAssignments(gridid, mid, mode) {
    var page = g_BeaconAppsPath + "ReviewerAssignments.aspx";
    var id = PeakGrid_GetKeyValue(gridid);
    //Peak_OpenPage(grid, page, id, mid, title/field, img, height, width, mode, top, left, showscroll, showmenu, resizeable)
    Peak_OpenPage(gridid, page, id, mid, "Reviewer " + id, '', 600, 700, mode);
}

function BeaconApps_ShowReviewerAssignments(id, mid, mode) {
    var page = g_BeaconAppsPath + "ReviewerAssignments.aspx";
    Peak_OpenPage(null, page, id, mid, "Reviewer " + id, '', 600, 700, mode);
}

function BeaconApps_PrintPage(control1, control2, closeWindow) {
    document.getElementById(control1).style.display = "none";
    if (document.getElementById(control2)) {
        document.getElementById(control2).style.display = "none";
    }
    window.print();
    if (closeWindow) {
        window.close();
    }
    else {
        document.getElementById(control1).style.display = "block";
        document.getElementById(control2).style.display = "block";
    }
}

function BeaconApps_ShowReviewerAnswers(url, rev, app, mid, mode) {
    //Peak_OpenPage(grid, page, id, mid, title/field, img, height, width, mode, top, left, showscroll, showmenu, resizeable)
    Peak_OpenPage(null, url, 0, mid, 'Rev ' + rev + ' App ' + app, null, 800, 600, mode);
}

function BeaconApps_ToggleElement(element, imageID, appRoot) {
    var displayval;
    var currentDisplay = document.getElementById(element).style.display;
    if (currentDisplay == "none") {
        if (document.addEventListener) {
            //if firefox, opera, safari..
            displayval = "table-row";
        }
        else {
            displayval = "block";
        }
    }
    else {
        displayval = "none";
    }
    document.getElementById(element).style.display = displayval;
    //document.getElementById(imageID).src = currentDisplay == "none" ? appRoot + "/Images/collapse.gif" : appRoot + "/Images/expand.gif";    
    document.getElementById(imageID).src = currentDisplay == "none" ? g_BeaconAppsImagesPath + "collapse.gif" : g_BeaconAppsImagesPath + "expand.gif";
}

function BeaconApps_ConfirmAssignment(ddlReviewerId) {
    if (document.getElementById(ddlReviewerId)) {
        if (document.getElementById(ddlReviewerId).selectedIndex > 0) {
            return confirm("Are you sure you want to assign this reviewer?");
        }
        else {
            alert("Please select a reviewer from the dropdown");
        }
    }
    return false;
}

function BeaconApps_ContactTypeChanged(control, rowID) {
    var displayval;
    if (document.addEventListener) {
        //if firefox, opera, safari..
        displayval = "table-row";
    }
    else {
        displayval = "block";
    }
    var text = control.options[control.selectedIndex].text;
    if (text == "Unit Member" && document.getElementById(rowID)) {
        document.getElementById(rowID).style.display = "none";
    }
    else {
        document.getElementById(rowID).style.display = displayval;
    }
}

// this function essentially enforces a MaxLength property for a text area
function BeaconApps_CheckMaxLength(id, maxLength, displayID) {
    if (document.getElementById(id)) {
        if (document.getElementById(displayID)) {
            if (document.getElementById(id).value.length > 0) {
                document.getElementById(displayID).innerText = "Total Characters: " + (parseInt(document.getElementById(id).value.length));
            }
            else {
                document.getElementById(displayID).innerText = "";
            }
        }

        //*** 10/20/2008 - Todd W - changed ">=" to be just ">" on the max length comparison ***//
        // if the number of characters exceeds the max, strip off the excess chars
        if (document.getElementById(id).value.length > maxLength) {
            alert("You have exceeded the max length of " + maxLength + " characters");
            document.getElementById(id).value = document.getElementById(id).value.substr(0, maxLength);
            document.getElementById(displayID).innerText = "Total Characters: " + maxLength;
            //alert(document.getElementById(displayID));
            document.getElementById(id).focus();
            g_maxLengthShown = true;
        }
    }
}

// this function essentially enforces a MaxLength property for a text area
function BeaconApps_CheckMaxLengthBlur(id, maxLength, displayID) {
    if (document.getElementById(id)) {
        if (document.getElementById(displayID)) {
            document.getElementById(displayID).innerText = "Total Characters: " + (parseInt(document.getElementById(id).value.length));
        }

        // if the number of characters exceeds the max, strip off the excess chars
        if (document.getElementById(id).value.length > maxLength) {
            if (g_maxLengthShown == false) {
                alert("Please note that your text will be truncated to the max length of " + maxLength + " characters");
                document.getElementById(id).value = document.getElementById(id).value.substr(0, maxLength);
                document.getElementById(displayID).innerText = "Total Characters: " + maxLength;
                document.getElementById(id).focus();
                g_maxLengthBlurShown = true;
            }
        }
    }
}

function BeaconApps_TextAreaValidate(boxId, message) {
    if (document.getElementById(boxId)) {
        if (document.getElementById(boxId).value.length == 0) {
            alert('Please enter some text into the text area prior to clicking the button');
            return false;
        }
        else {
            //return confirm('Are you sure you want to save this statement?');
            return confirm(message);
        }
    }
}

function BeaconApps_AssignInternal(gridid, mid, mode) {
    var page = g_BeaconAppsPath + "AssignInternal.aspx";
    var id = PeakGrid_GetKeyValue(gridid);
    //Peak_OpenPage(grid, page, id, mid, title/field, img, height, width, mode, top, left, showscroll, showmenu, resizeable)
    Peak_OpenPage(gridid, page, id, mid, "Assign Internal " + id, '', 600, 700, mode);
}

function BeaconApps_ToggleElement(element, imageID, appRoot) {
    var displayval;
    if (document.addEventListener) {
        //if firefox, opera, safari..
        displayval = "table-row";
    }
    else {
        displayval = "block";
    }
    var currentDisplay = document.getElementById(element).style.display;
    if (currentDisplay == "block" || currentDisplay == "table-row") {
        document.getElementById(element).style.display = "none";
        document.getElementById(imageID).src = g_BeaconAppsImagesPath + "expand.gif"; //appRoot + "/Images/expand.gif";
    }
    else {
        document.getElementById(element).style.display = displayval;
        document.getElementById(imageID).src = g_BeaconAppsImagesPath + "collapse.gif";
    }
}

function BeaconApps_TestAdd(gridid, mid, mode) {
    var testsRoot = g_rootpath + "/DM/Tests/";
    var page = testsRoot + "TestsEdit.aspx?admin=y";
    //var img = g_BeaconAppsImagesPath + "cetest.gif";
    //Peak_OpenPage(gridid, page, id, mid, title/field, img, height, width, mode, top, left, showscroll, showmenu, resizeable)
    Peak_OpenPage(gridid, page, 0, mid, "New Test", null, 500, 500, mode);
}

function BeaconApps_TestEdit(gridid, testId, mid, mode) {
    var testsRoot = g_rootpath + "/DM/Tests/";
    var page = testsRoot + "TestsEdit.aspx?admin=y";
    //var img = g_BeaconAppsImagesPath + "cetest.gif";
    //Peak_OpenPage(gridid, page, id, mid, title/field, img, height, width, mode, top, left, showscroll, showmenu, resizeable)
    Peak_OpenPage(gridid, page, testId, mid, "Test ID " + testId, null, 500, 500, mode);
}

function BeaconApps_PreviewQuestions(grid, mid) {
    var id = PeakGrid_GetKeyValue(grid);
    if (id == null) {
        alert("No row was selected");
        return;
    }
    var url = g_rootpath + "/DM/Tests/PreviewTest.aspx?TestID=" + id;
    var title = "PreviewTest" + id;
    //Peak_PopupPage(gridid, page, id, mid, title, height, width, left, top, showscroll, showmenu, resizeable)
    Peak_PopupPage(grid, url, id, mid, title, 600, 750, 50, 50, true, false, true);
}

function BeaconApps_ViewApplication(gridid, mid, mode) {
    var id = PeakGrid_GetKeyValue(gridid);
    var page = g_BeaconAppsPath + "ViewApplication.aspx?AppID=" + id;
    //Peak_OpenPage(grid, page, id, mid, title/field, img, height, width, mode, top, left, showscroll, showmenu, resizeable)
    Peak_OpenPage(gridid, page, id, mid, "View Application " + id, '', 600, 700, mode, 15, 15, true, false, true);
}

function BeaconApps_CreateApplication(gridid, mid, mode) {
    var id = PeakGrid_GetKeyValue(gridid);
    var unitId = PeakGrid_GetCellValue(gridid, "UnitID");
    var page = g_BeaconAppsPath + "CreateApplication.aspx?AppID=" + id;
    //Peak_OpenPage(grid, page, id, mid, title/field, img, height, width, mode, top, left, showscroll, showmenu, resizeable)
    Peak_OpenPage(gridid, page, id, mid, "Create Application " + unitId, '', 600, 700, mode);
}

function BeaconApps_NewContact(mid, unitID, hospitalID) {
    var page = g_BeaconAppsPath + "CreateUnitContact.aspx?CloseWindow=Close&UnitID=" + unitID + "&HospitalID=" + hospitalID;
    //Peak_PopupPage(gridid, page, id, mid, title, height, width, left, top, showscroll, showmenu, resizeable)
    Peak_PopupPage(null, page, 0, mid, "CreateUnitContact", 750, 600, 15, 15, true, false, true);
}

function BeaconApps_EditReviewerComments(resultsID, questionNum, mid) {
    var page = g_BeaconAppsPath + "EditReviewerComments.aspx?ResultsID=" + resultsID + "&QuestionNum=" + questionNum;
    //Peak_PopupPage(gridid, page, id, mid, title, height, width, left, top, showscroll, showmenu, resizeable)
    Peak_PopupPage(null, page, 0, mid, "EditReviewerComments", 750, 600, 15, 15, true, false, true);
}

function BeaconApps_EditReviewerSummaries(appID, resultsID, mid) {
    var page = g_BeaconAppsPath + "EditReviewerSummaries.aspx?AppID=" + appID + "&ResultsID=" + resultsID;
    //Peak_PopupPage(gridid, page, id, mid, title, height, width, left, top, showscroll, showmenu, resizeable)
    Peak_PopupPage(null, page, 0, mid, "EditReviewerSummaries", 750, 600, 15, 15, true, false, true);
}

/// Overload: Use the grid to get the Unit ID
function BeaconApps_PrimaryContact(gridID, colName, mid, mode) {
    var unitID = PeakGrid_GetCellValue(gridID, colName);
    var page = String.format("{0}{1}{2}", g_BeaconAppsPath, "PrimaryContact.aspx?UnitID=", unitID);
    //Peak_OpenPage(grid, page, id, mid, title/field, img, height, width, mode, top, left, showscroll, showmenu, resizeable)
    Peak_OpenPage(null, page, 0, mid, "Admin For Unit " + unitID, '', 600, 700, mode);
}

/// Overload: Pass in the Unit ID instead of using the grid
function BeaconApps_PrimaryContact_Unit(UnitID, colName, mid) {
    var page = String.format("{0}{1}{2}", g_BeaconAppsPath, "PrimaryContact.aspx?UnitID=", UnitID);
    //Peak_OpenPage(grid, page, id, mid, title/field, img, height, width, mode, top, left, showscroll, showmenu, resizeable)
    Peak_OpenPage(null, page, 0, mid, "Admin For Unit " + UnitID, '', 600, 700, 'TAB');
}

function BeaconApps_PrimaryContact_All(mid) {
    var page = String.format("{0}{1}", g_BeaconAppsPath, "ManageContacts.aspx");
    //Peak_OpenPage(grid, page, id, mid, title/field, img, height, width, mode, top, left, showscroll, showmenu, resizeable)
    Peak_OpenPage(null, page, 0, mid, "Manage Beacon Admins", '', 600, 700, 'TAB');
}

/// Fire all validators.  If the page is valid, pop up a confirm message and return the selected value of true or false.
function BeaconApps_ValidateRequired(confirmMessage) {
    Page_ClientValidate();
    if (Page_IsValid) {
        return confirm(confirmMessage);
    }
    else {
        return false;
    }
}