﻿// <copyright file="ReportUtilities.js" company="BioCentury Publications Inc."> 
// All contents Copyright © 2005-2009 BioCentury Publications, Inc. ALL RIGHTS RESERVED
// 
// http://www.biocentury.com
// </copyright>

//  Used in Reports' Tables to show and to hide the collapse/expand (-/+) columns

function ShowHide(a) 
{
    var colName = a.id.split('_')[5];
    var showHideCol = a.id.split('_')[6];
    
    var idImg = a.id.substring(0, a.id.lastIndexOf('_'));
    var imgShowW = "<br><img id=\"" + idImg + "_0\" onclick=\"ShowHide(this);\" title=\"Expand column data\" style=\"cursor:hand;\" border=0 src=../Images/expand.gif" + ">";
    var imgHideW = "<br><img id=\"" + idImg + "_1\" onclick=\"ShowHide(this);\" title=\"Collapse column data\"style=\"cursor:hand;\" border=0 src=../Images/collapse.gif" + ">";

    var divsa = document.getElementsByTagName('a');
    var divs = new Array();
    var k=0;
    for (var j = 0; j < divsa.length; j++) 
    {
        if (divsa[j] != null && divsa[j] != 'undefined')
            if (divsa[j].childNodes != null && divsa[j].childNodes != 'undefined' && divsa[j].childNodes.length > 0) 
            {
                if (divsa[j].childNodes[0].id != null && divsa[j].childNodes[0].id != 'undefined' && divsa[j].childNodes[0].id != '') 
                {
                    if (divsa[j].childNodes[0].id.indexOf(colName + '_0') > 0) 
                    {
                        divs[k] = divsa[j];
                        k++;
                    }
                }
        }
    }

    //var divs = document.getElementsByName(colName);
    
    var parentElem = document.getElementById(a.id).parentNode;

    for (var i = 0; i < divs.length; i++) 
    {
        if (showHideCol == '0')
            divs[i].className = "DivVisible";
        else
            divs[i].className = "DivHidden";
    }
    
    if (showHideCol == '0')
        parentElem.innerHTML = '<SPAN>' + colName + '</SPAN>' + imgHideW;
    else
        parentElem.innerHTML = '<SPAN>' + colName + '</SPAN>' + imgShowW;
}

function printAlert(view, module) {
    var message = "Some wide reports will not fit on page. Alternatively you can use Export to Excel and then print.";
    if (module == 'Financings')
        if (view == 'custom' || view == 'expanded')
            alert(message);
    if (module == 'Pipeline' || module == 'IUProfile')
        alert(message);
    if (module == 'Stocks')
        alert(message);
}


var resizeDelay;

//if (window.attachEvent)  // IE only - resize DIVs now + on resize
//{
//    window.attachEvent("onresize", function() { doResize(); });
//}


//window.onresize = doResize

first = true;

function doResize() {
    if (!first) {
        clearTimeout(resizeDelay);
        resizeDelay = setTimeout(function() { setDIVHeight_Table('gridviewDiv'); }, 200);
    }
    first = false;
}



// Edited and commented by Marius , 10/12/2008
// Sets the correct height on window resize.
// TO-DO replace the hardcoded id
function setDIVHeight_Table(gridID, module) {
    // gridview = the gridviewTable
    gridview = document.getElementById('ctl00_cphMain_customControl_gvTables');

    if (gridview != null) 
    {

        //td main = the cell that contains all the general info, options, table and debug log
        tdmain = document.getElementById('tdMain');
        maximumWidth = Math.round(tdmain.offsetWidth * 98 / 100);
        maximumHeight = tdmain.offsetHeight;
        WriteIn(tdmain.offsetHeight);
        
        bodyWidth = Math.round(document.body.clientWidth*95/100);
	    bodyHeight = document.body.clientHeight;
        

        // entireGridview = the div that holds both the header and the body of the table. this has overflow:hidden
	    entireGridview = document.getElementById('ctl00_cphMain_customControl_gridviewDiv');

        // headerGridview = the div that holds the header. this has overflow:hidden
	    headerGridview = document.getElementById('ctl00_cphMain_customControl_ScrollingGridView1$divHdr');

        // tableGridview = the div that holds the body of the table. this has overflow:scroll
	    tableGridview = document.getElementById('ctl00_cphMain_customControl_ScrollingGridView1$divContent');

        

        // height of the entire gridview
        totalHeight = headerGridview.offsetHeight;
        totalHeight += gridview.offsetHeight;
        //totalHeight += getTotalHeight(gridview, module);

        // diffHeight = the height of the other controls in tdMain
        diffHeight = 0;
        // the general info div
        if (document.getElementById('DivElement') != null) 
        {
            diffHeight += document.getElementById('DivElement').offsetHeight;
        }
        // the debug log div
        if (document.getElementById('ctl00_cphMain_customControl_pDebugLog') != null) 
        {
            diffHeight += document.getElementById('ctl00_cphMain_customControl_pDebugLog').offsetHeight;
        }

        maximumHeight = maximumHeight - diffHeight - 50;

        tableGridview.style.height = "260px";
        entireGridview.style.height = "300px";
        
//        if (totalHeight > maximumHeight) 
//        {
//            if (maximumHeight > 300) {
//                entireGridview.style.height = maximumHeight + 'px';
//            }
//            else {
//                entireGridview.style.height = "300px";
//            }
//        }
//        else {
//            entireGridview.style.height = totalHeight;
//        }
        
    }


}


// for testing purposes
function WriteIn(str) {
    document.getElementById("myDiv").innerHTML += "> " + str;  //+ " " + strL;
}

function getTotalHeight(datagrid, module) {
    var _totalHeight = 0;
    if (module != null && module == 'Stock') {
        _totalHeight = document.getElementById('tblData').offsetHeight;
    }
    else 
    {
        for (var i = 0; i < datagrid.rows.length; i++) 
        {
            _totalHeight += datagrid.rows[i].offsetHeight;
        }
    }
    return _totalHeight;
}

//-------------------------------------------------------------------------------
// ADD - Bela Gaspar
// Descr: because jpg are acces denied, they are called with Article.ashx
// Used in Financial Performance - indexes.html
//-------------------------------------------------------------------------------
function loadImages(imagesArray) {
    for (var i = 0; i < imagesArray.split(',').length; i++) {
        var filePath = document.location.href.substring(0, document.location.href.lastIndexOf('=') + 1);
        var filePath1 = document.location.href.substring(filePath.length, document.location.href.lastIndexOf('/') - 4) + imagesArray.split(',')[i];

        document.getElementById(imagesArray.split(',')[i].split('.')[0]).src = filePath + filePath1;
    }
}

function setDIVHeight_MarketIndicator(gridID, module) {
    bodyWidth = Math.round(document.body.clientWidth * 95 / 100);
    bodyHeight = document.body.clientHeight;
    //alert(bodyWidth + ' - ' + bodyHeight);
    var _totalHeight = 0;
    if (document.getElementById('columnTable1')) {
        _totalHeight = document.getElementById('columnTable1').offsetHeight;

        _totalHeight += document.getElementById('tblData1').offsetHeight;

    }
    var datagrid = document.getElementById('columnTable1');
    //alert(datagrid);
    if (datagrid != null) {
        var div = document.getElementById('CompanyList1');
        var maxGridWidth = bodyWidth;
        var diffHeight = 0;
        if (document.getElementById('DivElement') != null) diffHeight += document.getElementById('DivElement').offsetHeight;
        var maxGridHeight = bodyHeight - diffHeight - 80;

        if (_totalHeight > maxGridHeight) {
            if (maxGridHeight > 200)
                div.style.height = maxGridHeight + 'px';
            else
                div.style.height = "200px";
        }
        else {
            if (datagrid.offsetWidth >= maxGridWidth)
                div.style.height = _totalHeight + 18 + 'px';
            else
                div.style.height = _totalHeight + 2 + 'px';
        }

        if (datagrid.offsetWidth >= maxGridWidth) {
            if (maxGridWidth > 300)
                div.style.width = maxGridWidth + 'px';
            else
                div.style.width = "300px";
        }
        else {
            if (_totalHeight > maxGridHeight)
                div.style.width = datagrid.offsetWidth + 19 + 'px';
            else
                div.style.width = datagrid.offsetWidth + 2 + 'px';
        }
    }
}

//used in index.html to resize the div CompanyList
function setDIVHeight_Financing(gridID, module) {
    bodyWidth = Math.round(document.body.clientWidth * 95 / 100);
    bodyHeight = document.body.clientHeight;
    
    if (document.body.scrollWidth > document.body.clientWidth)
        bodyHeight += 16;
    
    var datagrid = document.getElementById('columnTable');
    var _totalHeight = 0;
    if (datagrid) {
        _totalHeight = document.getElementById('columnTable').offsetHeight;

        _totalHeight += getTotalHeight(datagrid, module);

    }

    if (datagrid != null) {
        var div = document.getElementById(gridID);
        var maxGridWidth = bodyWidth;

        var diffHeight = 0;
        if (document.getElementById('DivElement') != null) diffHeight += document.getElementById('DivElement').offsetHeight;

        if (document.getElementById('saveListDiv') != null) diffHeight += document.getElementById('saveListDiv').offsetHeight;

        var maxGridHeight = bodyHeight - diffHeight - 20;
        if (module != null && module == 'Stock')
            maxGridHeight -= 50;

        if (_totalHeight > maxGridHeight) {
            if (maxGridHeight > 200)
                div.style.height = maxGridHeight + 'px';
            else
                div.style.height = "200px";
        }
        else {
            if (datagrid.offsetWidth >= maxGridWidth)
                div.style.height = _totalHeight + 20 + 'px';
            else
                div.style.height = _totalHeight + 4 + 'px';
        }

        if (datagrid.offsetWidth >= maxGridWidth) {
            if (maxGridWidth > 300)
                div.style.width = maxGridWidth + 'px';
            else
                div.style.width = "300px";
        }
        else {
            if (_totalHeight > maxGridHeight)
                div.style.width = datagrid.offsetWidth + 19 + 'px';
            else
                div.style.width = datagrid.offsetWidth + 2 + 'px';
        }
    }
}

function exportCSV(fileName, link) {
    var filePath = document.location.href.substring(0, document.location.href.lastIndexOf('=') + 1);
    var filePath1 = document.location.href.substring(filePath.length, document.location.href.lastIndexOf('/') - 4) + fileName;
    link.href = filePath + filePath1;  
}

function showHTML(id, value) {
    var link = document.getElementById(id).href;
    if (value != null)
        document.getElementById(id).href = "../bcapp/NewsCenter/Article.ashx?item=" + link;
    else
        document.getElementById(id).href = "../../bcapp/NewsCenter/Article.ashx?item=" + link;
}

//-------------------------------------------------------------------------------
//Drill down to financings form CP
//-------------------------------------------------------------------------------
function createFinancingsReportFromProfile(hrefRoot) {
    var cID = hdnCPCompanyID; //document.getElementById(hdnCPCompanyID).value;
    var lID = hdnCPListID; //document.getElementById(hdnCPListID).value;
    popup = false;
    if (document.getElementById(hdnIsPopup).value == "False")
    {
        var doc = document;
    }
    else//popup
    {
        try {
            var doc = window.opener.document;
            popup = true;
        }
        catch (e) {
            //window.close();
            //return false;
            var doc = document;
        }
    }
    if (lID != null && lID != "")
        doc.location.href = hrefRoot + 'Financings/TablesF.aspx?ListID=' + lID;
    else
        doc.location.href = hrefRoot + 'Financings/TablesF.aspx?CompanyID=' + cID;
    if (popup) {
        window.close();
    }
}

//-------------------------------------------------------------------------------
// Drill down to Pipeline from: Company Profile and Product Profile (Product Summary Chart)
//-------------------------------------------------------------------------------
function createPipelineReportFromProfile(hrefRoot, diseaseID, diseaseName, phaseGroupName, phaseID, phaseName, productName) 
{
    var companyID = hdnCPCompanyID;  //document.getElementById(hdnCPCompanyID).value;
    var listID = hdnCPListID; //document.getElementById(hdnCPListID).value;

    popup = false;
    
    if (document.getElementById(hdnIsPopup).value == "False") 
    {
        var doc = document;
    }
    else//  
    {
        try {
            var doc = window.opener.document;
            popup = true;
        }
        catch (e) {
            //window.close();
            //return false;
            var doc = document;
        }
    }
    
    DeleteFilterSessions(6);
    
    if ((diseaseID == null || phaseID == null) && productName == null)
        doc.location.href = hrefRoot + 'Pipeline/TablesP.aspx?CompanyID=' + companyID;
    else 
    {
        // when it's called from 'Product Profile' only
        if (productName != null) 
        {
            var productNameSession = productName + '|' + productName + '|' +
                    '&lt;SubFilter FilterID=&quot;ProductName&quot;&gt;&lt;r NameSubstring=&quot;' + productName + '&quot;/&gt;&lt;/SubFilter&gt;';
            SaveInSession('ProductName', productNameSession);
        }

        // when it's called from 'Company Profile' or 'Product Profile'
        if (diseaseName != 'Total') 
        {
            //set session for Disease
            //Autoimmune|
            //DiseaseClassID=436|
            //<SubFilter FilterID="DiseaseClassIndication"><r DiseaseClassID="436" /></SubFilter>
            var diseaseSession = diseaseName + '|DiseaseClassID=' + diseaseID +
                                '|&lt;SubFilter FilterID=&quot;DiseaseClassIndication&quot;&gt;&lt;r DiseaseClassID=&quot;' + diseaseID + '&quot; /&gt;&lt;/SubFilter&gt;';            
            SaveInSession('DiseaseClass', diseaseSession);
        }
        
        //set session for Phase
        //Development|
        //GroupID=19|
        //<SubFilter FilterID="PhaseOfDevelopment"><r GroupID="19"/></SubFilter>
        if (phaseID != null) 
        {
            var phaseSession = phaseName + '|' + phaseGroupName + '=' + phaseID +
                            '|&lt;SubFilter FilterID=&quot;PhaseOfDevelopment&quot;&gt;&lt;r ' + phaseGroupName + '=&quot;' + phaseID + '&quot; /&gt;&lt;/SubFilter&gt;';
            SaveInSession('PhaseofDevelopment', phaseSession);
        }
        
        doc.location.href = hrefRoot + 'Pipeline/TablesP.aspx?CompanyID=' + companyID;
    }
    if (popup) 
    {
        // for IE7 - Bela - need to test it
        var oMe = window.self;
        oMe.open('', '_self', '');
        oMe.close();
        
        window.close();
    }
}

//-------------------------------------------------------------------------------
// Drill down from Company Profile - Pipeline Detailed II
//-------------------------------------------------------------------------------
function createCompanyDrillDown(theCompanyID) {
    //var theCompanyID = hdnCPCompanyID;
    if (theCompanyID == null) theCompanyID = "";

    if (document.getElementById(hdnIsPopup).value == "False") 
    {
        document.location.href = "CompanyProfile.aspx?CompanyID=" + theCompanyID;
    }
    else//popup
    {
        try {
            document.location.href = "PopUpPage.aspx?ControlType=CompanyProfile&CompanyID=" + theCompanyID;
        }
        catch (e) {
            window.close();
            return false;
        }
    }
}

// used for opening the page to view the removed records from the alert
function openTablesRemoved(module, deleted) {
    window.open("../Common/PopUpPage.aspx?ControlType=TablesRemoved&module=" + module + "&deleted=" + deleted, "_blank", "status=yes,toolbar=no,resizable=yes,menubar=no,scrollbars=yes");
}