﻿

// 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();
// 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){
        var searchTerm = document.getElementById('searchTerm').value.replace(' ', '_');
        if(searchTerm.length > 1){
            window.location = '/search.html?q=' + document.getElementById('searchTerm').value.replace(' ', '_');
        }
     }
     else {  return true; }
}
var tsandCs = "";
var fltDivHeight = 300;
var fltDivWidth = 500;

function displayTsandCs(){

    var fltDivHTML = '<h1>Terms and Conditions of Use</h1><div>';
    fltDivHTML += tsandCs; 
    fltDivHTML += '</div><br /><span><input onclick="submitTsandCs()" type="checkbox"> I agree to 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 prospectus relating to this application form."; 
    fltDivHTML += '</div><br /><span><input onclick="submitApplication(\'' + docURL + '\')" type="checkbox"> Download application form</span>';

    fltDivHeight = 200;
    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(){
        if(window.location.href.indexOf('?') >= 0){  location.replace(window.location + '&t=1');}
        else{ location.replace(window.location + '?t=1')}
}

function closeTLSCheck(i){
    if (i == 1){
        if(window.location.href.indexOf('?') >= 0){  location.replace('/help.html' + '&tls=1');}
        else{ location.replace('/help.html' + '?tls=1')}
    }
    else if (i == 0){
        if(window.location.href.indexOf('?') >= 0){  location.replace(window.location + '&tls=1');}
        else{ location.replace(window.location + '?tls=1')}
    }
}

function submitApplication(url){
        location.reload();
        window.open(url);
}

function screenScroll(){
    var fltDiv = document.getElementById('floatingDiv');
        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);

//    var sel = document.getElementById(inputFieldID);
//    var searchTerm = sel.options[sel.selectedIndex].value.toLowerCase();
//    
//	var table = document.getElementById(TableID);
//	var ele;
//	for (var r = 1; r < table.rows.length; r++){
//		ele = table.rows[r].cells[columnNumber].innerHTML.replace(/<[^>]+>/g,"");
//		if (ele.toLowerCase().indexOf(searchTerm)>=0 || table.rows[r].id == "skipThis" )
//			table.rows[r].style.display = '';
//		else table.rows[r].style.display = 'none';
//	}
	
}

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 = '';
            for(var c = 0; c < nrcols; c++){
                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 || table.rows[r].id == "skipThis" ){}
		        else {display = 'none';}
	        }
	        table.rows[r].style.display = display;
        }
        
        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].id != "skipThis" && 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;
	    }
	    return amount;
    }
    
    function TLSNotEnabled(){
        document.getElementById('companyLogo').src =  document.getElementById('companyLogo').src.replace("https:", "http:");
        TLSNotEnabledMsg();
    }