<!--
var evalResult	= null;
var sTreePageSet = '';
var bQuoteSaved	= false;
var bQuoteValid	= false;

function SetQuoteSaved(pValue) {
    bQuoteSaved = pValue;
    bQuoteValid = pValue;
}

function SetQuoteValid(pValue) {
    bQuoteValid = pValue;
}

function GetModuleString(pnModuleID) {
    // return the custom string that is related to this Module ID.
    // HARD-CODED
    var sResult = '';
    switch (pnModuleID) {
        case 1:	// Vehicles.
            sResult = 'Vehicle';
            break;
        case 2:	// Trailers
            sResult = 'Trailer';
            break;
        case 3:	// AllRisks
            sResult = 'AllRisk';
            break;
        case 5:	// Mechanicals
            sResult = 'Mechanical';
            break;
        case 6:	// SmallCraft
            sResult = 'SmallCraft';
            break;
        case 7:	// Hunter
            sResult = 'Hunter';
            break;
        case 8:	// Hunting Risk Item
            sResult = 'HunterRI';
            break;
        case 11:	// ATV
            sResult = 'ATV';
            break;
        case 12:	// DIRTSURE
            sResult = 'DIRTSURE';
            break;
        case 13:	// AllRisk
            sResult = 'Dirtsure_AllRisk';
            break;
        case 14:	// Trailer
            sResult = 'Dirtsure_Trailer';
            break;
    }
    
    return sResult;
}
function CallModuleNew(pnModuleID) {
    // Load the specified module's "New Item" page.
    var sCmdString = GetModuleString(pnModuleID);
    var sFullCmd = '';
    
    if (window.top.AccessCode < 3) {
        alert('Permission denied.');
        return;
    }
    
    // HARD-CODED Exception For Agent Code 7106!
    if (pnModuleID == 6) {
        if (msBrokerCode == '7106') {
            sCmdString += msBrokerCode;
        }
    }
    if (ModuleNewEnabled(pnModuleID)) {
        // show the "Loading page" banner.
        window.top.frames('main').divLoading.style.display = '';
        window.top.frames('main').divContent.style.display = 'none';
        // load the page.
        sFullCmd = 'window.top.frames(\'main\').location.href = \'' + sCmdString + '.asp?Action=New&ClientKey=' + msClientKey + '&IDate=' + msInceptionDate + '&VMod=' + pnModuleID + '\';';
        eval(sFullCmd);
        // select the tree node corresponding to this call.
        try {
            trePolicy.Nodes.Item('lvl_003_Key_n' + pnModuleID).Selected = true;
        } catch (e) {}
    } else {
        alert('Sorry, that is not allowed at this time.');
    }
}

function CallQuoteModuleNew(pnModuleID) {
    // Load the specified module's "New Item" page.
    var sCmdString = 'Q_' + GetModuleString(pnModuleID);
    var sFullCmd = '';
    if ((bQuoteSaved == false) || (msClientKey == '0') || (bQuoteValid == false)) {
        if (bQuoteValid == false) {
            alert('This quote is not valid. Please save as a valid quote bofore adding any items.');
        } else {
            alert('Please save the changes to the quote before adding the items.');
        }
    } else {
        if (ModuleNewEnabled(pnModuleID)) {
            // show the "Loading page" banner.
            window.top.frames('main').divContent.style.display = 'none';
            try {
                window.top.frames('main').divSection.divLoading.style.display = '';
                window.top.frames('main').divSection.divContent.style.display = 'none';
            } catch (e) {}
            try {
                window.top.frames('main').divSection.style.display = '';
                window.top.frames('main').divSection.style.width = window.top.frames('main').document.body.clientWidth;
                window.top.frames('main').divSection.style.height = '100%';
            } catch (e) {}
            
            try {
                // load the page.
                sFullCmd = 'window.top.frames(\'main\').fraSection.location.href = \'LoadPage.htm\';window.setTimeout(\'window.top.frames("main").fraSection.location.href = "' + sCmdString + '.asp?QuoteNr=' + msClientKey + '&QMod=' + pnModuleID + '"\', 100);';
                eval(sFullCmd);
                // select the tree node corresponding to this call.
                trePolicy.Nodes.Item('lvl_003_Key_n' + pnModuleID).Selected = true;
            } catch (e) {
                try {
                    trePolicy.Nodes.Item('lvl_003_Key_n' + pnModuleID).Parent.Selected = true;
                    trePolicy_NodeClick();
                } catch (e) {}
            }
        } else {
            alert('Sorry, that is not allowed at this time.');
        }
    }
}

function HideSectionFrame() {
    if ((sTreePageSet == 'Quote') && (PolicyWindowLoaded()))
        try {
            window.top.frames('main').divSection.style.display = 'none';
            window.top.frames('main').divContent.style.display = '';
        } catch (e) {}
}

function CallModuleLoad(pnModuleID, psItemStatus, psItemKey) {
    // Load the specified module item maintenance page.
    var sCmdString = GetModuleString(pnModuleID);
    var sItemAction = (psItemStatus == 'P') ? 'ConvertToPolicy' : 'Update';
    var sFullCmd = '';
    var sModuleID = '000' + parseInt(pnModuleID).toString();
    
    if (sModuleID.length > 3) {
        sModuleID = sModuleID.substr(sModuleID.length - 3, 3);
    }
    
    // HARD-CODED Exception For Agent Code 7106!
    if (pnModuleID == 6) {
        if (msBrokerCode == '7106') {
            sCmdString += msBrokerCode;
        }
    }
    
    // show the "Loading page" banner.
    try {
        window.top.frames('main').divLoading.style.display = '';
        window.top.frames('main').divContent.style.display = 'none';
    } catch (e) {}
    
    // load the page.
    sFullCmd = 'window.top.frames(\'main\').location.href = \'' + sCmdString + '.asp?Action=' + sItemAction + '&ClientKey=' + msClientKey + '&ItemKey=' + psItemKey + '&IDate=' + msInceptionDate + '&VMod=' + pnModuleID + '\';';
    eval(sFullCmd);
    
    try {
        try {
            // select the tree node corresponding to this call.
            trePolicy.Nodes.Item('lvl_003_Key_' + sModuleID + '-' + psItemStatus + '-' + psItemKey).Selected = true;
        } catch (e) {
            trePolicy.Nodes.Item('lvl_003_Key_' + sModuleID + '-' + psItemStatus + '-' + psItemKey).Parent.Selected = true;
            trePolicy_NodeClick();
        }
    } catch (e) {}
}

function CallQuoteModuleLoad(pnModuleID, psItemKey) {
    // Load the specified module item maintenance page.
    var sCmdString = 'Q_' + GetModuleString(pnModuleID);
    var sFullCmd = '';
    var sModuleID = '000' + parseInt(pnModuleID).toString();
    if (sModuleID.length > 3) {
        sModuleID = sModuleID.substr(sModuleID.length - 3, 3);
    }
    
    if ((bQuoteSaved == false) || (msClientKey == '0')) {
        alert('Please save the changes to the quote before editing the items.');
    } else {
        // show the "Loading page" banner.
        try {
            window.top.frames('main').divContent.style.display = 'none';
        } catch (e) {}
        try {
            window.top.frames('main').divSection.divLoading.style.display = '';
            window.top.frames('main').divSection.divContent.style.display = 'none';
        } catch (e) {}
        try {
            window.top.frames('main').divSection.style.display = '';
            window.top.frames('main').divSection.style.width = window.top.frames('main').document.body.clientWidth;
            window.top.frames('main').divSection.style.height = '100%';
        } catch (e) {}
        
        // load the page.
        if (bQuoteValid) {
            sFullCmd = '';
        } else {
            sFullCmd = '&Locked=1';
        }
        sFullCmd = 'window.top.frames(\'main\').fraSection.location.href = \'LoadPage.htm\';window.setTimeout(\'window.top.frames("main").fraSection.location.href = "' + sCmdString + '.asp?QuoteNr=' + msClientKey + '&ItemKey=' + psItemKey + '&QMod=' + pnModuleID + sFullCmd + '"\', 100);';
        eval(sFullCmd);
        try {
            try {
                // select the tree node corresponding to this call.
                trePolicy.Nodes.Item('lvl_003_Key_' + sModuleID + '-Q-' + psItemKey).Selected = true;
            } catch (e) {
                trePolicy.Nodes.Item('lvl_003_Key_' + sModuleID + '-Q-' + psItemKey).Parent.Selected = true;
                trePolicy_NodeClick();
            }
        } catch (e) {}
    }
}

function ModuleNewEnabled(pnModuleID) {
    // Find out if the user is allowed to press the "New Item" button for this module.
    var sCmdString = GetModuleString(pnModuleID);
    var bResult = false;
    var iNodeIndx = GetNewButtonIndex(pnModuleID);
    var oTreeNode = null;
    
    try {
        oTreeNode = trePolicy.Nodes.Item(iNodeIndx);
    } catch (e) {}
    // Get whether or not the button is enabled in the policy Window.
    if (PolicyWindowLoaded() == true) {
        evalResult = null;
        eval('try{evalResult = (window.top.frames(\'main\').btnNew' + sCmdString + '.disabled == false);}catch(e){evalResult=false;}');
        bResult = evalResult;
        evalResult = null;
        if (oTreeNode != null)
            oTreeNode.Checked = bResult;
    } else
    // If the main window is not loaded, then find out if the button was enabled the last time the window loaded.
    if (!bResult) {
        if (oTreeNode != null) bResult = oTreeNode.Checked;
    }
    
    return bResult;
}

function GetNewButtonIndex(pnModuleID) {
    var nModules = (nSysNodes - 1) / 2;
    var nResult = null;
    
    for(var i = nModules + 2; i <= 2 * nModules + 1; i ++) {
        // Get the Node's Database key.
        sKey = trePolicy.Nodes.Item(i).Key.substr(12, trePolicy.Nodes.Item(i).Key.length - 12);
        // Check if this is the "New module" node for this module.
        if (sKey == 'n' + pnModuleID) {
            nResult = i;
            break; // exit the for loop.
        }
    }
    
    return nResult;
} // end GetNewButtonIndex

function PolicyWindowLoaded() {
    // find out if the data frame contains the policy admin page.
    
    try {
        if (sTreePageSet == 'Policy')
            return (window.top.frames('main').location.href.search(/\/PolicyAdmin\.asp/i) >= 0)
        else
            return (window.top.frames('main').location.href.search(/\/QuoteAdmin\.asp/i) >= 0);
    } catch (poErr) {
        return false;
    }
}

function trePolicy_NodeClick() {
    var	nlevel = parseInt(trePolicy.SelectedItem.Key.substr(4, 3));
    var sKey = trePolicy.SelectedItem.Key.substr(12, trePolicy.SelectedItem.Key.length - 12);
    var sParentKey = (nlevel > 1) ? trePolicy.SelectedItem.Parent.Key.substr(12, trePolicy.SelectedItem.Parent.Key.length - 12) : null;
    var nModuleID = null;
    var bTriedNew = false;
    
    if (IsWindowLocked()) {
        alert('The tree view will not work until you have completed the current form.');
        return;
    }
    switch (nlevel) {
        case 1:
            // Show the main policy page. If that page is already shown, then go to the top of the page.
            HideSectionFrame();
            window.top.frames('main').location.replace(sTreePageSet + 'Admin.asp?ClientKey=' + msClientKey + '#PolicyTop');
            break;
        case 2:
            // go to the main policy page, bookmark for the clicked module.
            nModuleID = parseInt(sKey);
            HideSectionFrame();
            window.top.frames('main').location.replace(sTreePageSet + 'Admin.asp?ClientKey=' + msClientKey + '#Module' + nModuleID);
            break;
        case 3:
            nModuleID = parseInt(sParentKey);
            // Show the particular item, or load a new item.
            try {
                // There could be an error here as we are looking at a print / email page.
                if ((sKey.substr(0, 1) == 'n') && (trePolicy.SelectedItem.Text.substr(0, 4) == 'New ')) {
                    // New module item.
                    bTriedNew = true
                    if (sTreePageSet == 'Policy')
                        CallModuleNew(nModuleID)
                    else
                        CallQuoteModuleNew(nModuleID);
                } else {
                    // Load item.
                    if (sTreePageSet == 'Policy')
                        CallModuleLoad(nModuleID, sKey.substr(4, 1), sKey.substr(6))
                    else
                        CallQuoteModuleLoad(nModuleID, sKey.substr(6));
                }
            } catch (e) {
                // Since the load failed, re-load the admin page, and then try again.
                //  Reload the admin page.
                HideSectionFrame();
                window.top.frames('main').location.replace(sTreePageSet + 'Admin.asp?ClientKey=' + msClientKey + '#PolicyTop');
                //  Try load the item again.
                
                if (bTriedNew) {
                    // New module item.
                    if (sTreePageSet == 'Policy')
                        window.setTimeout('try {CallModuleNew(' + nModuleID + ')} catch(e) {}', 500)
                    else
                        window.setTimeout('try {CallQuoteModuleNew(' + nModuleID + ')} catch(e) {}', 500);
                } else {
                    // Load item.
                    if (sTreePageSet == 'Policy')
                        window.setTimeout('try {CallModuleLoad(' + nModuleID + ', ' + sKey.substr(4, 1) + ', ' + sKey.substr(6) + ')} catch(e) {}', 500)
                    else
                        window.setTimeout('try {CallQuoteModuleLoad(' + nModuleID + ', ' + sKey.substr(6) + ')} catch(e) {}', 500);
                }
            }
            break;
    }
}

//-->

