/*
----------------------------------------------------------------------------------------------
Bradford & Bingley
default.js
Authors:  Bobby Boydell
Created:  20 Feb 2006
----------------------------------------------------------------------------------------------
*/
var agt=navigator.userAgent.toLowerCase();
autoClearValues = Array();
if (window.addEventListener)
{
    window.addEventListener("load", do_onload, false);
}
else
{
    if (window.attachEvent)
    {
        window.attachEvent("onload", do_onload);
    }
    else
    {
    if (document.getElementById)
        {
            window.onload = do_onload;
        }
    }
}

function do_onload()
{
    jsfAutoClear();
    listContentShowHide();
    jsfHideElements();
}


/*
Start of functions/code to enable pop window to function
Gareth Brittain BT Web Solutions
12th Sept 2006
*/
function addEvent( obj, type, fn )
{
    if (obj.addEventListener)
        obj.addEventListener( type, fn, false );
    else if (obj.attachEvent)
    {
        obj["e"+type+fn] = fn;
        obj[type+fn] = function() { obj["e"+type+fn]( window.event ); }
        obj.attachEvent( "on"+type, obj[type+fn] );
    }
}
function removeEvent( obj, type, fn )
{
    if (obj.removeEventListener)
        obj.removeEventListener( type, fn, false );
    else if (obj.detachEvent)
    {
        obj.detachEvent( "on"+type, obj[type+fn] );
        obj[type+fn] = null;
        obj["e"+type+fn] = null;
    }
}
/* Create the new window */
function openInNewWindow(e) {
    var event;
    if (!e) event = window.event;
    else event = e;
    // Abort if a modifier key is pressed
    if (event.shiftKey || event.altKey || event.ctrlKey || event.metaKey) {
        return true;
    }
    else {
        // Change "_blank" to something like "newWindow" to load all links in the same new window
        var newWindow = window.open(this.getAttribute('href'), '_blank', 'scrollbars=1,menubar=no,height=480,width=660,resizable=yes,toolbar=no,location=no,status=no');
        if (newWindow) {
            if (newWindow.focus) {
                newWindow.focus();
            }
            return false;
        }
        return true;
    }
}
/*
Add the openInNewWindow function to the onclick event of links with a class name of "popupwin"
*/
function getNewWindowLinks() {
    // Check that the browser is DOM compliant
    if (document.getElementById && document.createElement && document.appendChild) {
        // Find all links
        var links = document.getElementsByTagName('a');
        var objWarningText;
        var link;
        for (var i = 0; i < links.length; i++) {
            link = links[i];
            // Find all links with a class name of "popupwin"
             if (/\bpopupwin\b/.test(link.className)) {
                link.onclick = openInNewWindow;
             }
        }
        objWarningText = null;
    }
}
addEvent(window, 'load', getNewWindowLinks);

/*
End of functions/code to enable pop window to function
*/

/*
Start of functions/code to enable redEye link tracking
Gareth Brittain BT Web Solutions
15th Sept 2006
*/

function RedEyeTag(strTag)
{
    imgRedEye = new Image(); 
    imgRedEye.src = strTag;
    return false;
}

function checkNode() {

if (document.getElementsByTagName('a')) // get all elements with a html tag
 {
    var pat = /\blinktrax\b/;
    var hrf;
    var bla = "";
    var nodes = document.getElementsByTagName('a');
        
    for (var i = 0; i <= nodes.length - 1; i++)   // loop through nodes "array"
    {

         if (pat.test(nodes[i].href)) // if url querystring contains "linktrax" assign redEye class
         {
            if(nodes[i].className) {
              //nodes[i].className += " redEye";
                nodes[i].className = "redEye " + nodes[i].className;
            }      
            else
            {
              nodes[i].className = "redEye";
            }                                                   
         }
     }
 }

}

function setRedEyeLink(e) { // function to create redeye tracking url string and assign to image
    var event;
    if (!e) event = window.event;
    else event = e;
    // Abort if a modifier key is pressed
    if (event.shiftKey || event.altKey || event.ctrlKey || event.metaKey) {
        return true;
    }
    else {
                       
        hrf = this.getAttribute('href');  // obtains url
        bla = hrf.substring(hrf.indexOf("linktrax=") + 9, hrf.length); //obtains query string after linktrax
           
        if (bla.indexOf("&") != -1)
        {
                bla = bla.substring(0, bla.indexOf("&"));
        }
            
        //compiles link
                    
        nl  = "../reporting.bradford-bingley.co.uk/cgi-bin/rr.cgi/images/blank.gif";
        nl += "?nourl=" + hrf;
        nl += "&noref=" + document.referrer;
        nl += "&rdinfo=" + bla + "&linktrax=yes";
        
        RedEyeTag(nl);
        
        return true;
    }
}

function getRedEyeLinks() { // obtain all links with a class of "redEye"
    // Check that the browser is DOM compliant
    if (document.getElementById && document.createElement && document.appendChild) {
        // Find all links
        var links = document.getElementsByTagName('a');
        var objWarningText;
        var link;
        for (var i = 0; i < links.length; i++) {
            link = links[i];        
             if (/\bredEye\b/.test(link.className)) {
                link.onclick = setRedEyeLink;
            }
        }
        objWarningText = null;
    }
}

addEvent(window, 'load', checkNode);
addEvent(window, 'load', getRedEyeLinks);

/*
End of functions/code to enable redEye link tracking
*/

/* 
Additional RedEye Promo Stat tracking JS function 
*/

function RedEyeTag(strTag){
    imgRedEye = new Image();
    imgRedEye.src = '../reporting.bradford-bingley.co.uk/cgi-bin/rr.cgi/images/blankc4a6.gif?nourl=' + strTag;
};

/* 
End Promo Stat tracking JS
*/

function jsfAutoClear()
{
    autoClearObjects = document.getElementsByClassName('autoClear');
    
    // Store initial values of autoClear objects in autoClearValues array
    for(i=0;i<autoClearObjects.length;i++)
    {
        autoClearValues[i] = autoClearObjects[i].value;
        autoClearObjects[i].onfocus = clearField;
        autoClearObjects[i].onblur = fillField;
    }
    
    // onFocus
    function clearField()
    {
        for(i=0;i<autoClearObjects.length;i++)
        {
            if(autoClearObjects[i]==this)
            {
                // Only if the current value is the default
                if (this.value == autoClearValues[i]) {
                
                    // Clear the value for user entry
                    this.value = "";    
                }
                
            }
        }
            
    }
    
    // onBlur
    function fillField()
    {
        for(i=0;i<autoClearObjects.length;i++)
        {
            if(autoClearObjects[i]==this)
            {
                // Only if the value is empty
                if (this.value == "") {
                
                    // Replace it with the default value
                    this.value = autoClearValues[i];
                }
            }
        }
    }
}

/* Functionality for dynamically hiding and showing savings table
--------------------------------------------------------------------------------------------*/
function listContentShowHide()
{
    if (agt.indexOf("msie 5.0")==-1)
    {
        var varFirstItemSelected = true; // modified from false gb 01/09/2006
        var chkurl = false; // added gb 01/09/2006
        if ((varShow = document.getElementById('jsContentBlock'))&& (varList = document.getElementById('jsNavBlock')))
        {
            for(i=0;i<varShow.childNodes.length;i++)
            {
                if(varShow.childNodes[i].nodeType==1)
                {
                    // added from here
                    if (!chkurl)   // Ensure that url is only check once
                    {                       
                       var requestval = document.location.search; //obtain querystring from url
                       var mark = requestval.lastIndexOf("tid");  //check if query string contains a tid section         
                       if (mark != -1) // perform if query string contains a tid section         
                       {
                          mark = requestval.substr(mark+4,2); // obtain tab id number
                          if (agt.indexOf("firefox")!= -1 || agt.indexOf("opera")!= -1 || agt.indexOf("safari")!= -1 || agt.indexOf("netscape")!= -1) // perform if browser type not IE
                          {             
                              switch(mark) // modify tab id number
                              {
                                 case "1":  mark = parseInt(mark)+2;
                                            break;
                                 case "2":  mark = parseInt(mark)+3;
                                            break;
                                 case "3":  mark = parseInt(mark)+4;
                                            break;
                                 case "4":  mark = parseInt(mark)+5;
                                            break;
                                 case "5":  mark = parseInt(mark)+6;
                                            break;
                               }
                          }
                          var foundit = true; // Set found flag
                       }
                       else // perform if tab id section is not in the querystring
                       {
                          varFirstItemSelected = false; 
                       }
                       chkurl = true; // set url check falg so url is not checked more than once
                    }
                  
                    if (foundit && i == mark) // perform if found flag is set, and loop value matches the querysting tab id value 
                    {
                       varFirstItemSelected = false;
                    }                   
                    // coded added to allow sub navlinks to asign correct tab ends here

                    varShow.childNodes[i].style.display="none";

                    varList.childNodes[i].childNodes[0].onclick = listContentConnect;
                    if(!varFirstItemSelected)
                    {
                        varShow.childNodes[i].style.display="";
                        varList.childNodes[i].setAttribute("class","selected");
                        varList.childNodes[i].setAttribute("className","selected");
                        varFirstItemSelected = true;
                    }
                }
            }
        }
    }
}
function listContentConnect()
{
        if ((varShow = document.getElementById('jsContentBlock'))&& (varList = document.getElementById('jsNavBlock')))
        {
            for(i=0;i<varShow.childNodes.length;i++)
            {
                if(varShow.childNodes[i].nodeType==1)
                {
                    varShow.childNodes[i].style.display="none";
                    varList.childNodes[i].setAttribute("class","");
                    varList.childNodes[i].setAttribute("className","");
                }
            }
            
            for(i=0;i<varShow.childNodes.length;i++)
            {
                if(varShow.childNodes[i].nodeType==1)
                {
                    if(varList.childNodes[i].childNodes[0]==this)
                    {
                        varShow.childNodes[i].style.display="";
                        varList.childNodes[i].setAttribute("class","selected");
                        varList.childNodes[i].setAttribute("className","selected");
                    }
                }
            }
        }
        return false;
}

/* Hides elements which aren't needed if JavaScript is enabled */
function jsfHideElements() {
        
    var objectsToHide = document.getElementsByClassName('jsHide');
    var objectsToShow = document.getElementsByClassName('jsShow');
    
    for(i=0;i<objectsToHide.length;i++)
    {
        if (agt.indexOf("msie 5.0")==-1)
        {
            objectsToHide[i].style.display="none";
        }
    }
    for(i=0;i<objectsToShow.length;i++)
    {
        objectsToShow[i].style.display="block";
    }
        
}


document.getElementsByClassName = function(cls,n,t)
{
var rtn = [];
n=n==null?document:n;
t=t==null?'*':t;
var els = n.getElementsByTagName ? n.getElementsByTagName(t) : document.all;
els = (!els||!els.length ) && document.all ? document.all : els;
if(cls==null){return els;}
for (var i=0,j=0; i < els.length; i++)
{
if(els[i].className.match("(^|\\s)"+cls+"(\\s|$)"))
{
rtn[j++] = els[i];
}
}
return rtn;
};

function displaySavingsTab(tabNumber)
{
    for(i=1;i<4;i++)
    {
        var tabName = "savingsInfoButton" + i;
        var divName = "savingsInfoTab" + i;
        var tabToHide = document.getElementById(divName);
        var displayTab = document.getElementById(tabName);
        if(i != tabNumber)
        {
            displayTab.className="";
            tabToHide.style.display = "none";
        }
        else
        {
            displayTab.className="selected";
            tabToHide.style.display = "block";
        }
    }
}