fAddEvent(window, "load", fInitDropdown);

function fInitDropdown() {
    
    var select = document.getElementById("jsUsefulLink");
    var newWindowClass = getElementsByClass('jsNewWindow',select,"option");
    var submitButton = document.getElementById("fUsefulSubmit");
    var aClose = document.getElementById("iloginClose");

    var links = new Array();
    links[23] = "http://www.northernrock.co.uk";
    links[24] = "http://www.northernrock-guernsey.com/";
    links[25] = "http://companyinfo.northernrock.co.uk/";
    links[26] = "http://www.nr-foundation.org.uk/";

    if(newWindowClass) {
        var popupArray = new Array();
        for (var n=0; n < newWindowClass.length; n++) {
	        popupArray.push(newWindowClass[n].value);
        }

        // capture the onclick event
        if (submitButton) {
            submitButton.onclick = function() {
	            if ( popupArray.inArray(select.value) ) {
		            window.open(links[select.value]);
		            return false;
	            } else {
		            return true;
	            }
            }
        }
    }
    
    if(aClose){
        aClose.innerHTML = "close this window";
        aClose.href="#";
        aClose.onclick = function () {
        window.close();
        return true;
        }
    }
 }

// function to return an array of items 
function getElementsByClass(searchClass,node,tag) {
	var classElements = new Array();
	if ( node == null )
		node = document;
	if ( tag == null )
		tag = '*';
	var els = node.getElementsByTagName(tag);
	var elsLen = els.length;
	var pattern = new RegExp("(^|\\s)"+searchClass+"(\\s|$)");
	for (i = 0, j = 0; i < elsLen; i++) {
		if ( pattern.test(els[i].className) ) {
			classElements[j] = els[i];
			j++;
		}
	}
	return classElements;
}

function fAddEvent(obj, evType, fn){ if(obj.addEventListener){ obj.addEventListener(evType, fn, false); return true; } else if(obj.attachEvent){ var r = obj.attachEvent("on"+evType, fn); return r; } else return false; }

// extend the array functionality with this handy function
Array.prototype.inArray = function (value) {
	var i;
	for (i=0; i < this.length; i++) {
		if (this[i] === value) {
			return true;
		}
	}
	return false;
};

/* table hide shows */
var ratesTable = document.getElementById("iRatesTable");
if (ratesTable) {
	var toggleLinks = getElementsByClass('jsSeeBelow',ratesTable,"td");
	var toggleNotes = getElementsByClass('jsCollapsee',ratesTable,"td");
	var i = 0;
	
	for (i; i < toggleLinks.length; i++) {
	    updateToggleLinks(toggleLinks[i],toggleNotes[i]);
	}
}

function updateToggleLinks(toggle,note) {
	toggle.innerHTML = "<a>Show notes</a>";
	toggle.className = "jsSeeBelowClosed";
	note.className = "jsCollapseeClosed";
	toggle.onclick = function () {
		if (toggle.className == "jsSeeBelowClosed" ) { 	
			note.className = "jsCollapseeOpen";
			toggle.innerHTML = "<a>Hide notes</a>";
			toggle.className = "jsSeeBelowOpen";
		} else {
			toggle.innerHTML = "<a>Show notes</a>";
			note.className = "jsCollapseeClosed";
			toggle.className = "jsSeeBelowClosed";
		}
	}
}
