﻿

// Type.registerNamespace('EdgarOnline.UI');

function doContextMenu(e)
{
    var evt = (e) ? e : window.event;
    var target = evt.srcElement;
    var menuTemplate = document.getElementById('contextMenuTemplate');
    var menuText = menuTemplate.innerHTML;
    var contextMenuDiv = document.getElementById('contextMenuText');
    
    while (!target.id) target = target.parentNode;
    var labelText = target.innerHTML;
    var labelId = target.id;
    labelId= labelId.replace("'","\\'");
    if (labelId) menuText = menuText.replace(/#id#/g, labelId);
        
    if (labelText) menuText = menuText.replace(/#text#/g, labelText);
    
    // Process Menu Parameters
    var menuParams = document.getElementsByTagName("input");

    for (i=0; i<menuParams.length; i++)
    {
        if (menuParams[i].className == 'contextMenuParam')
        {
            menuText = menuText.replace(eval("/#" + menuParams[i].id + "#/g"), menuParams[i].value);
        }
    }
    
    contextMenuDiv.innerHTML = menuText;
    
    popupContextMenu(evt);
}

function popupContextMenu(evt)
{
    var contextMenu = document.getElementById('contextMenu');
    var contextMenuShadow = document.getElementById('contextMenuShadow');
    var contextMenuDiv = document.getElementById('contextMenuText');
    
    var mT, mL, mH, mW, wH, wW, pX, pY;
    
    mL = evt.clientX;
    mT = evt.clientY;
    mW = contextMenuDiv.clientWidth;
    mH = contextMenuDiv.clientHeight;
    
    if (document.getElementById && !document.all)
    {
        wH = window.innerHeight;
        wW = window.innerWidth;
        pX = window.pageXOffset;
        pY = window.pageYOffset;
    }
    else
    {
        wH = document.documentElement.clientHeight;
        wW = document.documentElement.clientWidth;
        pX = document.documentElement.scrollLeft;
        pY = document.documentElement.scrollTop;
    }
    
    wW = wW + pX;
    wH = wH + pY;
    mL = mL + pX;
    mT = mT + pY;
    
    if (eval(mL + mW) > wW) mL = mL - mW;
    if (mL < pX) mL = wW - mW;
    if (mL < pX) mW = pX;
    
    if (eval(mT + mH) > wH) mT = mT - mH;
    if (mT < pY) mT = wH - mH;
    if (mT < pY) mT = pY;
    
    if (document.getElementById && !document.all)
    {
        // Firefox Version
        contextMenu.style.left = mL + 'px';
        contextMenu.style.top = mT + 'px';
        contextMenuShadow.style.height = eval(contextMenuDiv.offsetHeight - 2) + 'px';            
        contextMenuShadow.style.width = eval(contextMenuDiv.offsetWidth - 2) + 'px';
        evt.preventDefault(true); 
    }
    else
    {
        contextMenu.style.left = mL;
        contextMenu.style.top = mT;
        contextMenuShadow.style.height = contextMenuDiv.clientHeight;            
        contextMenuShadow.style.width = contextMenuDiv.clientWidth;            
        evt.returnValue = false;
    }
    
    contextMenu.style.visibility="visible";
}

function popupWindow(WindowName, WindowText, WindowHtml)
{
    var popT, popL, popH, popW, wH, wW, pX, pY;
    var popWin = document.getElementById(WindowName);
    var popWinText = document.getElementById(WindowText);
 
    popWinText.innerHTML = WindowHtml;
    var hitAnchor = document.getElementById('HitAnchor');
    if (hitAnchor != null)
    {
        var iScrollTop = hitAnchor.offsetTop;
        popWinText.scrollTop = iScrollTop;
    }
    
    popWin.style.visibility="visible";
}

function positionPopupWindow(WindowName, TargetElement)
{
    var tElement, tPos, tX, tY;
    
    var popWin = document.getElementById(WindowName);
    var popH = popWin.clientHeight;
    var popW = popWin.clientWidth;
    
    if (TargetElement == "window")
    {
        if (document.getElementById && !document.all)
        {
            tX = window.pageXOffset;
            tY = window.pageYOffset;
        }
        else
        {
            tX = document.documentElement.scrollLeft;
            tY = document.documentElement.scrollTop;
        }
        tX = tX + 100;
        tY = tY + 50;
    }
    else
    {
        tElement = document.getElementById(TargetElement).parentNode;
        tPos = findPos(tElement);
        tX = tPos[0]; //tElement.offsetLeft;
        tY = tPos[1]; //tElement.offsetTop;
    }
    
    if (document.getElementById && !document.all)
    {
        wH = window.innerHeight;
        wW = window.innerWidth;
        pX = window.pageXOffset;
        pY = window.pageYOffset;
    }
    else
    {
        wH = document.documentElement.clientHeight;
        wW = document.documentElement.clientWidth;
        pX = document.documentElement.scrollLeft;
        pY = document.documentElement.scrollTop;
    }
    
    popT = tY - popH; // + pY;
    popL = tX - popW; // + pX;
    
    if (popT < pY) popT = tY;
    if (popL < pX) popL = tX;
    
    if (document.getElementById && !document.all)
    {
        // Firefox Version
        popWin.style.left = popL + 'px';
        popWin.style.top = popT + 'px';
    }
    else
    {
        popWin.style.left = popL;
        popWin.style.top = popT;
    }
}

function closePopupWindow(WindowName)
{
    var popWin = document.getElementById(WindowName);
    if (popWin != "undefined")
        popWin.style.visibility="hidden";
}

function popupWindowTitle(WindowTitleId, Title)
{
    var popWinTitle = document.getElementById(WindowTitleId);
    if (popWinTitle != "undefined")
        popWinTitle.innerText = Title;
}

function ViewSource()
{
    if (document.getElementById && !document.all)
    {
       window.location = "view-source:" + window.location.href;
    }
    else
    {
        if (typeof document.body.style.maxHeight == "undefined")
           window.location = "view-source:" + window.location.href;
    }
}

function SaveFavorite(add,desc)
{
    if (desc=='') desc=add;
    
    if (window.sidebar)
    	window.sidebar.addPanel(desc, add,"");
    else if (document.all)
        window.external.AddFavorite(add,desc);
}

function CopyToClipboard(elemId) 
{
    var buffer = document.getElementById('copyBuffer');
    var target = document.getElementById(elemId);
    
    if (buffer)
    {
        buffer.innerText = document.getElementById(elemId).innerText;
        var r = buffer.createTextRange();
        r.execCommand("Copy");
    }
}

function OpenInExcel()
{
	var target=window.location.href;
	var targetTable = 0;
	
	try
	{
        // Find the Xbrl Table
        var pageTables = document.getElementsByTagName("table");

        for (i=0; i<pageTables.length; i++)
        {
            targetTable++;
            
            if (pageTables[i].id == 'Xbrlheader')
            {
                break;
            }
        }
        
	    var xl = new ActiveXObject("Excel.Application");
	    var book = xl.Workbooks.Add;
	    xl.Visible = true;
	    var table = book.Worksheets(1).QueryTables.Add("URL;" + target, book.WorkSheets(1).Cells(1,1))
	    table.EditWebPage = "";
	    table.FieldNames = true;
	    table.RowNumbers = false;
	    table.FillAdjacentFormulas = false;
	    table.PreserveFormatting = true;
	    table.RefreshOnFileOpen = false;
	    table.BackgroundQuery = true;
	    table.RefreshStyle = 0 ;
	    table.SavePassword = false;
	    table.SaveData = true;
	    table.AdjustColumnWidth = true;
	    table.RefreshPeriod = 0;
	    table.WebSelectionType = 3;
	    table.WebFormatting = 3;
	    table.WebTables = targetTable; //14; //this.id;
	    table.WebPreFormattedTextToColumns = true;
	    table.WebConsecutiveDelimitersAsOne = true;
	    table.WebSingleBlockTextImport = false;
	    table.WebDisableDateRecognition = false;
	    table.WebDisableRedirections = false;
	    table.Refresh();
	}
	catch(e)
	{
	}
}


function SelectAll()
{
    document.execCommand("SelectAll");
}

function Refresh()
{
    window.location.reload( false );
}

function ViewSourceDocument(SrcDocId)
{
    // T_3M_2006_3_00
    var srcDocIdParts = SrcDocId.split("|");
    var sContextId = srcDocIdParts[0];
    var arrContextIdParts = sContextId.split("_");
    var sTicker = arrContextIdParts[0];
    var sDuration = arrContextIdParts[1]; // 3M|TTM|YTD
    var sYear = arrContextIdParts[2];
    var sQtr = arrContextIdParts[3];
    
    var elementName = srcDocIdParts[1];
    var appPath = document.getElementById("appPath").value;
    var linkBackUrl = appPath + "/XbrlLinkback.aspx?trt=Fiscal&np=1"; //trt=NumberOfPeriods&
    if (sDuration == "3M")
        { linkBackUrl = linkBackUrl + "&dt=Q"; }
    else if (sDuration == "YTD")
        { linkBackUrl = linkBackUrl + "&dt=A"; }
    else
        { linkBackUrl = linkBackUrl + "&dt=T"; }
        
    linkBackUrl = linkBackUrl + "&qs=" + sQtr + "&qe=" + sQtr + "&ys=" + sYear + "&ye=" + sYear;
    linkBackUrl = linkBackUrl + "&ticker=" + sTicker + "&en=" + elementName;
    
    this.open(linkBackUrl, "LinkBackWindow", "toolbar=yes,menubar=yes,location=yes,scrollbars=yes,resizable=yes");
}

function lookupReportSectionKey(contextId)
{
    var contextList = document.getElementById("contextIdList").value.split("|");
    var reportSectionList = document.getElementById("reportSectionKeyList").value.split("|");
    
    for(var i=0; i < contextList.length; i++ )
    {
        if (contextId == contextList[i])
            return(reportSectionList[i]);
    }
}        

function getLinkBackUrls(SrcDocId)
{
    var srcDocIdParts = SrcDocId.split("|");
    var sContextId = srcDocIdParts[0];
    var arrContextIdParts = sContextId.split("_");
    
    var sTicker = arrContextIdParts[0];
    var sDuration = arrContextIdParts[1]; // 3M|TTM|YTD
    var sYear = arrContextIdParts[2];
    var sQtr = arrContextIdParts[3];
    var elementName = srcDocIdParts[1];
    
    var appPath = document.getElementById("appPath").value;
    var propertyKey = document.getElementById("propertyKey").value;
    var propertyValue = document.getElementById("propertyValue").value;
    // var linkBackResult = document.getElementById("linkBackResult").value;
    
    var refXbrlWebService = XbrlWebService;
    
    // Position the Popup Window - In case we need it later
    positionPopupWindow("popupWindow", SrcDocId);

    //call web service to get the new html markup for the next 4 quarters.
    if (refXbrlWebService)
    {
        document.body.style.cursor='wait';
        refXbrlWebService.GetLinkBackUrl(propertyKey, propertyValue, sTicker, sQtr, sYear, sDuration, elementName, OnComplete, OnTimeOut, OnError); 
    }
    else
        alert("The Source Document Service is unavailable. Please try again later.");

}


function getRelatedNotesUrl(SrcDocId)
{
    var srcDocIdParts = SrcDocId.split("|");
    var sContextId = srcDocIdParts[0];
    var arrContextIdParts = sContextId.split("_");
    
    var sTicker = arrContextIdParts[0];
    var sDuration = arrContextIdParts[1]; // 3M|TTM|YTD
    var sYear = arrContextIdParts[2];
    var sQtr = arrContextIdParts[3];
    var elementName = srcDocIdParts[1];
    
    var appPath = document.getElementById("appPath").value;
    var propertyKey = document.getElementById("propertyKey").value;
    var propertyValue = document.getElementById("propertyValue").value;
    // var linkBackResult = document.getElementById("linkBackResult").value;
    
    var refXbrlWebService = XbrlWebService;
    // Position the Popup Window - In case we need it later
    positionPopupWindow("popupWindowDp", "window");

    //call web service to get the new html markup for the next 4 quarters.
    if (refXbrlWebService)
    {
        document.body.style.cursor='wait';
        refXbrlWebService.GetRelatedNotesUrl(propertyKey, propertyValue, sTicker, sQtr, sYear, sDuration, elementName, OnComplete_RelNotes, OnTimeOut_RelNotes, OnError_RelNotes); 
    }
    else
        alert("The Related Notes Service is unavailable. Please try again later.");

}


function OnComplete(result)
 {
    document.body.style.cursor='default'
    
    var arrUrls = result; //.split(",");
    var popupText;
    
    if (arrUrls)
    {
        if (arrUrls.length == 0)
        {
            alert("Sorry, there are no Source Documents available for the selected data point.")
        }
        else if (arrUrls.length == 1)
        {
           var objFin = window.open(arrUrls[0].replace("@","'"), "objFin", "");
           if (objFin)
                objFin.focus();
           else
           {
                location.href=arrUrls[0].replace("@","'");
           }
        }
        else
        {
            popupText = "The item you selected was derived from multiple Source Documents.<br><br>Please select the document you wish to view.<br><br>";

            for (var i=0; i < arrUrls.length; i++)
            {
                popupText = popupText + "<a href=\"" + arrUrls[i] + "\" target=\"LinkBackWindow\">Document No. " + eval(i + 1) + "</a><br>";
            }
            
            popupWindow("popupWindow", "popupWindowText", popupText);
        }
    }
    else
    {
        alert("Sorry, there are no Source Documents available for the selected data point.")
    }
 }

function OnTimeOut(result) 
{
    document.body.style.cursor='default'
    alert("The View Source Document Request Timed Out. Please Try Again.");
}

function OnError(result) 
{
    document.body.style.cursor='default'
    alert("The View Source Document Request Failed. Please Try Again.");
}

function OnComplete_RelNotes(result)
 {
    document.body.style.cursor='default'
    
    var arrUrls = result; //.split(",");
    var popupText;
    var rnInfo;
    
    if (arrUrls)
    {
        if (arrUrls.length == 0)
        {
            alert("Sorry, there are no Related Notes available for the selected data point.");
        }
        else if (arrUrls.length == 1)
        {
           rnInfo = arrUrls[0].split("|");

           if (rnInfo[0] == "note")
           {
               var objFin = window.open(rnInfo[1].replace("@","'"), "objFin", "");
               if (objFin)
                    objFin.focus();
               else
               {
                    location.href=arrUrls[1].replace("@","'");
               }
           }
           else if (rnInfo[0] == "foot")
           {
                if (confirm("Sorry, there are no Related Notes available for the selected data point. Would you like to view the complete footnotes?"))
                {
                    var objFin = window.open(rnInfo[1].replace("@","'"), "objFin", "");
                    if (objFin)
                        objFin.focus();
                    else
                        location.href=arrUrls[1].replace("@","'");
                }
           }
        }
        else
        {
            popupText = ""; //The following Related Notes are available. Please select the item you wish to view.<br><br>";

            var sPrevTitle = "";
            var sHitAnchor = "";
            var bFoundNote = false;
            
            for (var i=0; i < arrUrls.length; i++)
            {
                rnInfo = arrUrls[i].split("|");
                if (rnInfo[2] != sPrevTitle)
                {
                    if (rnInfo[5] == "True")
                    {
                        sHitAnchor = " id=\"HitAnchor\"";
                        bFoundNote = true;
                    }
                    popupText = popupText + "<hr size=1><div " + sHitAnchor + " title=\"" + rnInfo[4] + "\" style=\"font-weight: bold;\">" + rnInfo[2] + "</div><br>";
                    sPrevTitle = rnInfo[2];
                }
                popupText = popupText + "<li style=\"margin:4 0 0 17; text-indent: -16\"><a href=\"" + rnInfo[0] + "\" target=\"LinkBackWindow\">" + rnInfo[3] + "</a></li>";
            }
            popupText = popupText + "<hr size=1>";
            
            if (bFoundNote)
                popupText = "The following Related Notes are available. Please select the item you wish to view.<br><br>" + popupText;
            else
                popupText = "<div id=\"HitAnchor\" style=\"font-weight: bold;\">Sorry, There are no Related Notes available for the selected item.</div><br>The following Balance Sheet items have Related Notes.<br><br>" + popupText;

            popupWindowTitle("popupWindowTitleDp", "View Related Notes");
            popupWindow("popupWindowDp", "popupWindowTextDp", popupText);
        }
    }
    else
    {
        alert("Sorry, there are no Related Notes available for the selected data point.");
    }
 }

function OnTimeOut_RelNotes(result) 
{
    document.body.style.cursor='default'
    alert("The View Related Notes Request Timed Out. Please Try Again.");
}

function OnError_RelNotes(result) 
{
    document.body.style.cursor='default'
    alert("The View Related Notes Request Failed. Please Try Again.");
}


function mousedownPopupWinHandler()
{
    var arrPopupWin = new Array("popupWindow", "popupWindowDp");        
    var popWin;
    
    for (var i=0; i < arrPopupWin.length; i++)
    {        
        popWin = document.getElementById(arrPopupWin[i]);        
        if (popWin == null)
            continue;
        
        if (popWin.style.visibility == 'visible')
        {
            var x = window.event.clientX;
            var y = window.event.clientY;

            var bounds = Sys.UI.Control.getBounds(popWin);

            if (!((x >= bounds.x) && (x <= bounds.x + bounds.width) && (y >= bounds.y) && (y <= bounds.y + bounds.height)))
            {
                this.closePopupWindow(arrPopupWin[i]);
            }
        }
    }                      
}

function findPos(obj) {
    var curleft = curtop = 0;
    if (obj.offsetParent) {
        curleft = obj.offsetLeft
        curtop = obj.offsetTop
        while (obj = obj.offsetParent) {
	        curleft += obj.offsetLeft
	        curtop += obj.offsetTop
        }
    }
    return [curleft,curtop];
}

function displayFootNoteElements(SrcDocId)
{
    document.body.style.cursor = 'default';
    //alert(SrcDocId);

    var appPath = document.getElementById("appPath").value;
    
    var srcDocIdParts = SrcDocId.split("|");
    var sContextId = srcDocIdParts[0];
    var arrContextIdParts = sContextId.split("_");        
    
    var sTicker = arrContextIdParts[0];    
    var sDataset = arrContextIdParts[1];    
    var sDurationType = arrContextIdParts[2]; 
    var sTimeRangeType = arrContextIdParts[3];
    var sStartYr = arrContextIdParts[4];
    var sStartQtr = arrContextIdParts[5];
    var sEndYr = arrContextIdParts[6];
    var sEndQtr = arrContextIdParts[7];
        
    var sElement = srcDocIdParts[1];  
    //alert(sElement);
    var arrDataPoints = sElement.split(";");    

    //position the popup window
    positionPopupWindow("popupWindowDp", "spanXBRL");
    
    var sHtmlString = "<a onmouseover=\"window.status='View FootNotes'; return true;\" onmouseout=\"window.status=''; return true;\" title=\"View Footnotes\"" 
                + " href=\"" + appPath + "/XbrlSourceViewer.aspx";        
                  
    if (isNumber(sTicker))
    {
        sHtmlString += "&?companyId=" + sTicker;
    }    
    else
    {
        sHtmlString += "?ticker=" + sTicker;
    }
        
    
    sHtmlString += "&ds=" + sDataset + "&dt=" + sDurationType + "&trt=" + sTimeRangeType + "&ys=" + sStartYr + "&qs=" + sStartQtr + "&ye=" + sEndYr + "&qe=" + sEndQtr;
    //alert(sHtmlString);    
    
    var sLinkString;
    var sInnerText;    
    var popupText = "The link you selected contains multiple data points.<br>Please select the data point you wish to view footnotes for.<br><br>";
    for (var i=0; i < arrDataPoints.length; i++) 
    {                
        sInnerText = GetFootNoteElementDescription(sContextId, arrDataPoints[i]);
        //alert(sInnerText);    
        //alert(arrDataPoints[i]);
        
        if (sInnerText != null) 
        {
            sLinkString = sHtmlString;
            sLinkString += "&en=" + arrDataPoints[i] + "\"";
            //alert(sLinkString);
            
            popupText += sLinkString;
            popupText += " target=\"_blank\">" + sInnerText + "</a><br><br>";
        }
    }    
          
    
    popupWindow("popupWindowDp", "popupWindowTextDp", popupText);
}

function isNumber(a) 
{
    return typeof a == 'number' && isFinite(a);
}

function GetFootNoteElementDescription(contextId, dataPoint) 
{
    var oTextArea;
    var sText;
    var textAreaKey = contextId + "|" + dataPoint;
    //alert(textAreaKey);                
    
    if (textAreaKey != null)
    {
        oTextArea = document.getElementById(textAreaKey);
        if (oTextArea != null)
            sText = oTextArea.value;
    }        
    
    return sText;
}