// setupDefnPage() - Setup the definition page
function setupDefnPage()    {

    // Get our parameters
    var termName   = "";
    var termDefine = "";
    var codeCount  = 0;
    var codesArray = new Object();
    if (opener && opener.PopupParams)   {
        termName   = opener.PopupParams.Term;
        termDefine = opener.PopupParams.Definition;
        codeCount  = opener.PopupParams.CodeCount
        codesArray = opener.PopupParams.CodesArray;
    } else {
        alert("Definition could not be found.");
        return;
    }

    // Get our Term and load it
    var pageObj = document.getElementById("TermSpan");
    pageObj.innerHTML = termName;

    // Ditto for the definition
    pageObj = document.getElementById("DefPara");
    var defText = termDefine;
    var nPosn = defText.StrAt("-");
    if (nPosn > 0) {
        var cTitle = defText.Left(nPosn-1);
        defText = defText.Substr(nPosn);
        defText = "<b>" + cTitle + "</b>" + defText;
    } else {
        defText = "<b>" + defText + "</b>";
    }
    pageObj.innerHTML = defText;

    // Load the codes if we have them
    if (codeCount > 0)  {
        pageObj = document.getElementById("CodesTable");
        for (row = 0; row < codeCount; row++)    {
            var newRow = pageObj.insertRow(-1);      // -1 appends
            if (row % 2 == 1)   {
                addClass(newRow, "altRow");
            }

            // First cell has the code
            var cRowNum = "" + pageObj.rows.length - 1;  // Skip header row
            var newCell = newRow.insertCell(0);     // 0 = cell number - 1
            newCell.innerHTML = codesArray[row].Fld1;

            // 2nd cell has the definition
            var newCell = newRow.insertCell(1);
            newCell.innerHTML = codesArray[row].Fld2;
        }
        removeClass(pageObj, "hidden");
    }
}

// callDefinitionSvc() - Do a web request to get a definition
var memoPrefix = "";
function callDefinitionSvc(e)  {
    var hdgObj = getEventObject(e);
    var hdgText = hdgObj.firstChild.nodeValue;
    var nPosn = hdgText.StrAt("(");
    if (nPosn > 0)  {
        hdgText = hdgText.Left(nPosn-1).RTrim();
    }

    // Don't do anything if the value was just cleared
    if (hdgText.length > 0) {
        var url = currentDomain() + "cps/svc/GetTermDef.wwsoap";
        var pl = new SOAPClientParameters();
        pl.add("cLogin", "%DF%96%9D%AF%AF%9C%93%8A%B2%DF");
        pl.add("cPassword", "%DF%A9%BE%B1%B4%DF%AD%B2%BA%DF");
        pl.add("cTerm", hdgText);
	    SOAPClient._sendSoapRequest(url, "GetTermDef", pl, true,
	      saveDefinition, null);
    }
    return true;
}

// Record our parameters for the popup window
var PopupParams = new Object();

// saveDefinition() - Load the web request's DODAAC address result
function saveDefinition(cXML)  {

    // Clean up the XML
    var cClnXML = cXML.StrTran('&lt;','<');
    cClnXML = cClnXML.StrTran('&gt;','>');

    // We may have an error
    if (cClnXML.length == 0 || cClnXML.StrAt("Invalid") > 0 ||
      cClnXML.StrAt("info not found") > 0)   {
        return;
    }

    // Extract our GovtTermInfo
    var cXMLIn = SOAPClient._getElementsByTagName(cClnXML, "GovtTermInfo", true);

    // Get the attributes we care about
    var cErrMsg = SOAPClient._getAttributByName(cXMLIn, "ErrorMsg");
    if (!Empty(cErrMsg)) {
        alert(cErrMsg);
        return;
    }

    // Get some attributes we need
    var cTerm = SOAPClient._getAttributByName(cXMLIn, "TermIn");
    var codeCnt = SOAPClient._getAttributByName(cXMLIn, "CodeCount");

    // Get the definition
    var cDef = SOAPClient.getValuesByTagName(cXMLIn, "Definition");

    // Put all of our codes into an array
    var codesArray = new Array();
    for (var x = 1; x <= codeCnt; x++)   {
        var theCode = SOAPClient.getValuesByTagName(cXMLIn, 'Code' + x);
        var codeDef = SOAPClient.getValuesByTagName(cXMLIn, 'Def' + x);
        codesArray[x-1] = new LoadChoice(theCode, codeDef);
    }

    // Create an object for our results
    PopupParams.Term = cTerm;
    PopupParams.Definition = cDef;
    PopupParams.CodeCount = codeCnt;
    PopupParams.CodesArray = codesArray;

    // Popup our window with this stuff
    if (codeCnt == 0)   {
        var wid = 475;
        var ht = 270
        var scrollit = "no";
    } else {
        wid = 575;
        ht = 500
        scrollit = "yes";
    }
    openPopup(wid, ht, scrollit, "yes", hrefDefineWindow);
}

// tableHdgSetup() - Setup needed table heading links on the NSN report
function tableHdgSetup()   {

    // Setup table heading anchors for web service definition retrieval, except
    //  for the following:
    var ignoreHdgs = "City State,Date,Date Assigned,Description,Email,Fax," +
      "Item Name,Mfr. Name,Name,Phone,Pages,Price,Qty Ship,Quantity," +
      "Ship Date,Title,Unit Weight,Unit Volume,Unit Price,Web Site," +
      "CAGE and Name,Awards,Origin,History and Forecast"

    //  First, get all tables with the nsnReport class
    var oTables = getTagElementsWithClass("table", "nsnReport");

    // For each table, get its headings
    for (var y=0; y < oTables.length; y++)  {
        var thisTable = oTables[y];
        var oHdgs = thisTable.getElementsByTagName("th");
        for (var z=0; z < oHdgs.length; z++)  {

            // Unless we're to ignore this heading, add an onclick handler and
            //  a link look class
            var thisHdg = oHdgs[z];
            var hdgText = thisHdg.innerHTML;
            if (ignoreHdgs.StrAt(hdgText) == 0)  {
                addListener(thisHdg, 'onclick', callDefinitionSvc);
                addClass(thisHdg, "WonBluLinkLook");
            }
        }
    }
}

// getPDF() - Display a message and submit a request for a PDF report
function getPDF(cRequest)  {

    // Check the counter
    var cTmpReq = cRequest.Left(10);
    if (cTmpReq == '/PMGetFull') {
        var bOK = updateCounter(1);
        if (!(bOK))  {
            return bOK;
        }
    }

    // Get our objects
    var oBuyMenu = document.getElementById("BuyMenu");
    var oMiniMenu = document.getElementById("MiniMenu");

    // Change the Buy menu and hide the Mini menu
    var cMsg = 'The PDF report is being retrieved. "For best results, we recommend printing the PDF report in Landscape mode."';
    if (oBuyMenu)   {
        removeClass(oBuyMenu, "clear floatLeft");
        oBuyMenu.innerHTML = cMsg;
        addClass(oBuyMenu, "MessageNote");
    }
    if (oMiniMenu)   {
        removeClass(oMiniMenu, "clear floatLeft");
        addClass(oMiniMenu, "hidden");
    }

    // Submit the request
    window.location.href = currentDomain() + tossLeadingSlash(cRequest);
}

// dispPDFMsg() - Display a PDF message
function dispPDFMsg(nPrintNum, nNumToSubtract)  {

    // Update the report counter
    if (nNumToSubtract > 0)  {
        var bOK = updateCounter(nNumToSubtract);
        if (!(bOK))  {
            return bOK;
        }
    }
    // Get our object
    var oPrint = document.getElementById("Print" + nPrintNum);

    // Remove the class
    removeClass(oPrint, "BuyLink centertext");

    // Make them hidden
    addClass(oPrint, "hidden");

    // Display a message
    var cMsg = 'The PDF report is being retrieved. "For best results, we recommend printing the PDF report in Landscape mode."';
    oPrint.innerHTML = cMsg;
    removeClass(oPrint, "hidden");
    addClass(oPrint, "MessageNote");

    // Done
    return true;
}

// sendMsg() - Display a message
function sendMsg(cMsgNum)  {

    switch(cMsgNum)  {
    case '1':
        var cMsg = "The Similar feature is only available for logged in customers.";
        break;
    case '2':
        var cMsg = "The Print/Save feature is only available for logged in customers.";
        break;
    case '3':
        var cMsg = "The Parametric Search feature is only available for logged in customers.";
        break;
    default:
        var cMsg = "This feature is not available.";
    }

    alert(cMsg);

    // Done
}

// updateCounter() - Update the display counter
function updateCounter(nNumToSubtract)  {

    // Get our object
    var oDisplay = document.getElementById("RptCounter");

    // Put out the display html
    var cText = oDisplay.innerHTML;
    if (!Empty(cText))  {
        var nCol = cText.StrAt(' ');

        // The count is at the beginning
        var cCount = cText.Left(nCol - 1);
        var nCount = makeINumb( cCount );

        // Alert them if less that zero
        if (nCount - nNumToSubtract < 0)  {
            var cVar = 'report';
            if (!(nNumToSubtract==1))  {
                cVar = cVar + 's';
            }
            var cVar2 = 'report';
            if (!(nCount==1))  {
                cVar2 = cVar2 + 's are';
            }  else  {
                cVar2 = cVar2 + ' is';
            }
            alert('The request is to get ' + nNumToSubtract + ' full ' + cVar + ', but only ' + nCount + ' ' + cVar2 + ' available.');
            return false;
        }

        // Substract it
        nCount = nCount - nNumToSubtract;

        // Update the display
        var cVar = 'report';
        if (!(nCount==1))  {
            cVar = cVar + 's';
        }
        oDisplay.innerHTML = nCount + ' full ' + cVar + ' remaining';
    }

    // Done
    return true;
}