﻿

// Return an Array of elements that contain the class passed in
document.getElementsByClassName = function(clsName) {
    var retVal = new Array();
    var elements = document.getElementsByTagName("*");
    for (var i = 0; i < elements.length; i++) {
        if (elements[i].className.indexOf(" ") >= 0) {
            var classes = elements[i].className.split(" ");
            for (var j = 0; j < classes.length; j++) {
                if (classes[j] == clsName) {
                    retVal.push(elements[i]);
                }
            }
        }
        else if (elements[i].className == clsName) {
            retVal.push(elements[i]);
        }
    }
    return retVal;
}

var funcs = new Object();


    function updateTB() {
        var inps = document.getElementsByClassName("inporter");
        var str = '';
        for (var i = 0; i < inps.length; i++) {

            str += inps[i].value + '|';
        }
        document.getElementsByClassName("masterImporter")[0].value = str;
    }



// Add event to object
//function addEvent(obj, evType, fn, useCapture){
//      if (obj.addEventListener){ obj.addEventListener(evType, fn, useCapture); return true; } 
//      else if (obj.attachEvent){ return obj.attachEvent("on"+evType, fn); } 
//      else { alert("Handler could not be attached"); }
//}

// Added because of conflict with Mootools
function addEventMan(obj, evType, fn, useCapture) {
    if (obj.addEventListener) { obj.addEventListener(evType, fn, useCapture); return true; }
    else if (obj.attachEvent) { return obj.attachEvent("on" + evType, fn); }
    else { alert("Handler could not be attached"); }
}

// remove event from object
//function removeEvent(obj, type, fn) {
//	if (obj.detachEvent) {
//		try{	obj.detachEvent('on'+type, funcs[type+fn]);
//				funcs[type+fn] = null;
//				obj = null;
//			}catch(err){}
//	}else{obj.removeEventListener(type, fn, false);}
//}

// Added because of conflict with Mootools
function removeEventMan(obj, type, fn) {
    if (obj.detachEvent) {
        try {
            obj.detachEvent('on' + type, funcs[type + fn]);
            funcs[type + fn] = null;
            obj = null;
        } catch (err) { }
    } else { obj.removeEventListener(type, fn, false); }
}

// Get the elements Y position 
function getYPosition(oElement) {
    var y = 0;
    while (oElement) {
        y += oElement.offsetTop;
        oElement = oElement.offsetParent;
    }
    return y;
}

// Get the elements X position
function getXPosition(oElement) {
    var x = 0;
    while (oElement) {
        x += oElement.offsetLeft;
        oElement = oElement.offsetParent;
    }
    return x;
}


function resizeDividers() {

    // fix the first header if it is too wide
    var titleH1 = document.getElementById("titleH1")

    if (titleH1 != null && parseInt(titleH1.clientHeight) > 60) {
        titleH1.style.marginTop = '-140px';
    }

    // Redraw dividers 
    var oColumnDivs = document.getElementsByClassName("column");
    var iHeight = 0
    for (var i = 0; i < oColumnDivs.length; i++) {
        if (iHeight < oColumnDivs[i].clientHeight) {
            iHeight = oColumnDivs[i].clientHeight
        }
    }

    var oDividerDivs = document.getElementsByClassName("divider");
    for (var i = 0; i < oDividerDivs.length; i++) {
        oDividerDivs[i].style.height = iHeight + 'px';
        oDividerDivs[i].style.visibility = 'visible';
    }

}

function displayNews(year) {
    try {
        if (year != 0) {
            var divs = document.getElementById("newsItems").getElementsByTagName("div");
            for (var i = 0; i < divs.length; i++) {
                divs[i].style.display = 'none'
            }

            document.getElementById('news_' + year).style.display = 'block';

            var spans = document.getElementById("yearLinks").getElementsByTagName("span");
            var thisYear;
            for (var i = 0; i < spans.length; i++) {
                thisYear = spans[i].id.replace("link_", "");
                document.getElementById("link_" + thisYear).innerHTML = '<a href="javascript:displayNews(\'' + thisYear + '\');\">' + thisYear + '</a>';
            }
            
            document.getElementById("link_" + year).innerHTML = year;
        }
        else {
            var divs = document.getElementById("newsItems").getElementsByTagName("div");
            for (var i = 0; i < divs.length; i++) {

                divs[i].style.display = 'block'

             }
        }
    }
    catch (err) { }
}

/* Hide email address Function */

function sendEmail(personId) {
    document.getElementById("IFRemail").src = "/actions/mailto.aspx?p=" + personId;
}

function searchKeyPress(e) {
    var key;
    
    if (window.event) { key = window.event.keyCode; }
    else { key = e.which; }

    if (key == 13) {
        launchSearch();
    }
    else { return true; }
}

function launchSearch() {

    var searchTerm = document.getElementById('searchTerm').value.replace(' ', '_');
    if (searchTerm.length > 1) {
        window.location = '/search.html?q=' + document.getElementById('searchTerm').value.replace(' ', '_');
    }
}

var tsandCs = "";
var fltDivHeight = 290;
var fltDivWidth = 540;

function displayTsandCs() {

    var fltDivHTML = '<h1>Terms and Conditions of Use</h1><div>';
    fltDivHTML += tsandCs;
    fltDivHTML += '</div><br /><span><input onclick="submitTsandCs();" type="checkbox"> I have read and understood the information set out in the Terms and Conditions of Use</span>';

    displayFloatingDiv(fltDivHTML);
}

function displayApplicationPop(docURL) {

    var fltDivHTML = '<h1>Application Check</h1><div style="width:170px;height:110px;">';
    fltDivHTML += "Please confirm that you have read and understood the brochure and/or prospectus relating to this application form.";
    fltDivHTML += '</div><br /><span style="display:block;padding:0px;"><input onclick="submitApplication(\'' + docURL + '\');" type="checkbox"> Download application form<br/><input onclick="location.reload();" type="checkbox"> I do not agree</span>';

    fltDivHeight = 230;
    fltDivWidth = 200;
    displayFloatingDiv(fltDivHTML);
}


function TLSNotEnabledMsg() {

    var SEfltDivHTML = '<h1>Important</h1><div style="font-size:12px;width:250px;height:110px;">';
    SEfltDivHTML += 'To access items stored in our secure area you will need to enable TLS 1.0.'
    SEfltDivHTML += '</div><br /><a style="margin-left:5px;" href="javascript:void(0)" onclick="closeTLSCheck(1)"> How to enable TLS 1.0</a> | <a href="javascript:void(0)" onclick="closeTLSCheck(0)"> Ignore</a>';

    fltDivHeight = 200;
    fltDivWidth = 280;
    displayFloatingDiv(SEfltDivHTML);
}

function displayFloatingDiv(fltDivHTML) {
    var fltDiv = null;
    var fltDivC = null;
    var fltDivS = null;

    fltDiv = document.createElement('div');
    fltDiv.className = 'TsandCsScreen';
    fltDiv.style.height = getScrollHeight() + 'px';
    fltDiv.style.width = getScrollWidth() + 'px';
    fltDiv.style.top = getScrollTop() + 'px';
    fltDiv.style.left = getScrollLeft() + 'px';
    fltDiv.id = "floatingDiv";
    document.body.appendChild(fltDiv);

    fltDivC = document.createElement('div');
    fltDivC.className = 'TsandCsContent';
    fltDivC.style.height = fltDivHeight + 'px';
    fltDivC.style.width = fltDivWidth + 'px';
    fltDivC.style.top = ((getScrollHeight() / 2) - (fltDivHeight / 2)) + 'px';
    fltDivC.style.left = ((getScrollWidth() / 2) - (fltDivWidth / 2)) + 'px';
    fltDivC.id = "floatingDivContent";
    document.body.appendChild(fltDivC);
    document.getElementById('floatingDivContent').innerHTML = fltDivHTML;

    fltDivS = document.createElement('div');
    fltDivS.className = 'TsandCsShadow';
    fltDivS.style.height = fltDivHeight + 'px';
    fltDivS.style.width = fltDivWidth + 'px';
    fltDivS.style.top = (((getScrollHeight() / 2) - (fltDivHeight / 2)) + 20) + 'px';
    fltDivS.style.left = (((getScrollWidth() / 2) - (fltDivWidth / 2)) + 20) + 'px';
    fltDivS.id = "floatingDivShadow";
    document.body.appendChild(fltDivS);

    addEventMan(window, "scroll", function() { screenScroll(); }, true);
    addEventMan(window, "resize", function() { screenScroll(); }, true);
}

function submitTsandCs() {
    window.location.replace(BuildUrl(window.location.href, 't=1'));
}


function closeTLSCheck(i) {
    if (i == 1) {
        if (window.location.href.indexOf('?') >= 0) { window.location.replace('/help.html' + '&tls=1'); }
        else { window.location.replace('/help.html' + '?tls=1') }
    }
    else if (i == 0) {
        window.location.replace(BuildUrl(window.location.href, 'tls=1'));
    }
}

function BuildUrl(URL, stringToAdd) {
    var Part1 = '';
    var Part2 = '';

    if (URL.indexOf('?') >= 0) {
        Part1 = URL.substring(0, URL.indexOf('?'));
        Part2 = '&' + URL.substring(URL.indexOf('?') + 1);
    }
    else {
        Part1 = URL;
    }

    return Part1 + '?' + stringToAdd + Part2;
}


function submitApplication(url) {
    location.reload();
    window.open(url);
}

function screenScroll() {
    var fltDiv = document.getElementById('floatingDiv');
    
    if (fltDiv != null) {
        fltDiv.style.height = getScrollHeight() + 'px';
        fltDiv.style.width = getScrollWidth() + 'px';
        fltDiv.style.top = getScrollTop() + 'px';
        fltDiv.style.left = getScrollLeft() + 'px';

        var fltDivC = document.getElementById('floatingDivContent');
        fltDivC.style.top = ((getScrollHeight() / 2) - (fltDivHeight / 2)) + 'px';
        fltDivC.style.left = ((getScrollWidth() / 2) - (fltDivWidth / 2)) + 'px'

        var fltDivS = document.getElementById('floatingDivShadow');
        fltDivS.style.top = (((getScrollHeight() / 2) - (fltDivHeight / 2)) + 20) + 'px';
        fltDivS.style.left = (((getScrollWidth() / 2) - (fltDivWidth / 2)) + 20) + 'px';
    }
}


function changeBlog(elemId, obj) {
    var oDivs = document.getElementById(elemId).parentNode.getElementsByTagName("div");

    for (var i = 0; i < oDivs.length; i++) {
        oDivs[i].style.display = 'none';
    }

    document.getElementById(elemId).style.display = 'block';

    var oldItem = document.getElementsByClassName('selectedBlog');

    for (var i = 0; i < oldItem.length; i++) {
        oldItem[i].className = '';
    }

    obj.className = 'selectedBlog';
}

function showHideText(id) {

    if (document.getElementById('a_' + id).innerHTML != 'Hide Commentary') {
        document.getElementById('a_' + id).innerHTML = 'Hide Commentary';
        document.getElementById(id).style.display = 'inline';
    }
    else {
        document.getElementById('a_' + id).innerHTML = 'View Commentary';
        document.getElementById(id).style.display = 'none';
    }
}

function showHideText2(id) {

    if (document.getElementById('b_' + id).innerHTML != 'Hide 10 Largest') {
        document.getElementById('b_' + id).innerHTML = 'Hide 10 Largest';
        document.getElementById(id).style.display = 'inline';
    }
    else {
        document.getElementById('b_' + id).innerHTML = 'View 10 Largest';
        document.getElementById(id).style.display = 'none';
    }
}

function getScrollWidth() { return document.body.parentNode.clientWidth; }
function getScrollHeight() { return document.body.parentNode.clientHeight; }
function getScrollTop() { return document.body.parentNode.scrollTop; }
function getScrollLeft() { return document.body.parentNode.scrollLeft; }

addEventMan(window, "load", function() { resizeDividers(); }, true);


function filterTable(inputFieldID, TableID, columnNumber) {

    filterTablewithDD(TableID);
}


function filterTablewithDD(TableID) {

    var table = document.getElementById(TableID);
    var ele;

    var nrcols = document.getElementById(TableID).getElementsByTagName('tr')[1].getElementsByTagName('td').length;
    
    for (var r = 1; r < table.rows.length; r++) {
        display = 'table-row';

        for (var c = 1; c < nrcols - 1; c++) {
            if (table.rows[r].className.indexOf("FilterThis") != -1) {
                try {
                    ele = table.rows[r].cells[c].innerHTML.replace(/<[^>]+>/g, "");
                    var sel = document.getElementById('select_' + c);

                    var searchTerm = sel.options[sel.selectedIndex].value.toLowerCase();

                    if (ele.toLowerCase().indexOf(searchTerm) >= 0) { } else { display = 'none'; }
                }
                catch (e) { display = 'none'; }
            }
        }
        try {
            table.rows[r].style.display = display;
        }
        catch (e) {
            table.rows[r].style.display = 'block';
        }
    }

    totalcolumn(TableID)
}

function totalcolumn(TableID) {
    var output = document.getElementById("valTotal")
    var table = document.getElementById(TableID);
    var tot = 0;

    for (var r = 1; r < table.rows.length; r++) {
        if (table.rows[r].className == "FilterThis" && table.rows[r].style.display != 'none') {
            var number = table.rows[r].cells[output.parentNode.cellIndex].innerHTML.replace("£", "").replace(",", "");
            tot = tot + parseFloat(number)
        }
    }

    output.innerHTML = CommaFormatted('' + tot + '');
}

function CommaFormatted(amount) {
    if (amount != '0') {
        var delimiter = ","; // replace comma if desired
        if (amount.indexOf('.') < 0) {
            amount = amount + '.00';
        }
        var a = amount.split('.', 2)
        var d = a[1];
        var i = parseInt(a[0]);
        if (isNaN(i)) { return ''; }
        var minus = '';
        if (i < 0) { minus = '-'; }
        i = Math.abs(i);
        var n = new String(i);
        var a = [];
        while (n.length > 3) {
            var nn = n.substr(n.length - 3);
            a.unshift(nn);
            n = n.substr(0, n.length - 3);
        }
        if (n.length > 0) { a.unshift(n); }
        n = a.join(delimiter);
        if (d.length < 1) { amount = n; }
        else { amount = n + '.' + d; }
        amount = minus + "£" + amount;

        amount = amount.substring(0, amount.indexOf('.') + 3)
    }
    return amount;
}

function TLSNotEnabled() {
    document.getElementById('companyLogo').src = document.getElementById('companyLogo').src.replace("https:", "http:");
    TLSNotEnabledMsg();
}

function showDrillDown(portid, obj) {
    oPort = document.getElementById(portid).parentNode;

    // try{ document.getElementById(portid).firstChild.innerHTML = 'Loading....';}catch(e){}

    try { oPort.style.display = (oPort.style.display == 'none') ? 'table-row' : 'none'; } catch (e) { oPort.style.display = (oPort.style.display == 'none') ? 'block' : 'none'; }
    obj.firstChild.src = (oPort.style.display == 'none') ? '/_images/plus.gif' : '/_images/minus.gif';

    obj.parentNode.parentNode.lastChild.style.fontWeight = (oPort.style.display == 'none') ? 'normal' : 'bold';

}

function displayLZMod(sTableId) {

    var oTable = document.getElementById(sTableId);

    oTable.style.display = (oTable.style.display == 'block') ? 'none' : 'block';
    resizeDividers();

    document.getElementById('panel1').childNodes[0].innerHTML = '';
}


function LaunchPDF(OPMPortfolio) {

    // I have moved the removal of the iframe to the start because safari removes it before it can render the pdf.
    // If iframe existes remove it
    if (document.getElementById("launchPDF")) {
        document.getElementsByTagName("body")[0].removeChild(document.getElementById("launchPDF"))
    }


    ifrm = document.createElement("iframe");
    ifrm.setAttribute("src", "/actions/OPMValuations.aspx?portId=" + OPMPortfolio);
    ifrm.style.position = "absolute";
    ifrm.style.top = "-10000px";
    ifrm.style.left = "-10000px";
    ifrm.id = "launchPDF";
    document.getElementsByTagName("body")[0].appendChild(ifrm);

    // document.getElementsByTagName("body")[0].removeChild(document.getElementById("launchPDF"))
    
   // window.open("/actions/OPMValuations.aspx?portId=" + OPMPortfolio);
}

function LaunchRP_PDF(id, fileName) {

    // I have moved the removal of the iframe to the start because safari removes it before it can render the pdf.
    // If iframe existes remove it
    if (document.getElementById("launchPDF")) {
        document.getElementsByTagName("body")[0].removeChild(document.getElementById("launchPDF"))
    }


    ifrm = document.createElement("iframe");
    ifrm.setAttribute("src", "/actions/OPMRiskProfile.aspx?id=" + id + "&fileName=" + fileName);
    ifrm.style.position = "absolute";
    ifrm.style.top = "-10000px";
    ifrm.style.left = "-10000px";
    ifrm.id = "launchPDF";
    document.getElementsByTagName("body")[0].appendChild(ifrm);

    // document.getElementsByTagName("body")[0].removeChild(document.getElementById("launchPDF"))

    // window.open("/actions/OPMValuations.aspx?portId=" + OPMPortfolio);
}
function LaunchRPCAF_PDF(id, fileName) {

    // I have moved the removal of the iframe to the start because safari removes it before it can render the pdf.
    // If iframe existes remove it
    if (document.getElementById("launchPDF")) {
        document.getElementsByTagName("body")[0].removeChild(document.getElementById("launchPDF"))
    }


    ifrm = document.createElement("iframe");
    ifrm.setAttribute("src", "/actions/CAFEORiskProfile.aspx?id=" + id + "&fileName=" + fileName);
    ifrm.style.position = "absolute";
    ifrm.style.top = "-10000px";
    ifrm.style.left = "-10000px";
    ifrm.id = "launchPDF";
    document.getElementsByTagName("body")[0].appendChild(ifrm);

    // document.getElementsByTagName("body")[0].removeChild(document.getElementById("launchPDF"))

    // window.open("/actions/OPMValuations.aspx?portId=" + OPMPortfolio);
}
