/**
 * Common scripts used on all pages
 *
 * Christian Johansen (christian@ixd.no)
 * www.ixd.no
 */
/**
 * Configure functions to perform on click for different values of rel on links
 */
var newWindow = function(e) { window.open(this.href); return false; };
var appendRelToClass = function(link) { HTMLDOMUtil.addClass(link, link.rel); };
var relEvents = ['external', [newWindow, appendRelToClass], 'document', [newWindow, appendRelToClass],
                 'print', function(e) { if (window.print) { window.print(); return false; } else { return true; } }];
addDOMLoadEvent(function() {
    if (window.Popup) {
        relEvents = relEvents.concat(['popup', function() { return openPopup(tmp_prototype_getURL(this.href)); }]);
        openReferringPopup();
    }
		 HTMLUtil.linkRelEvents(relEvents);
    tableRowMouseOver();
    correctSubNavigation();
    clickEnableProducts();
    correctPopupContext();
    hackSearch();
});
function toggleFAQ(elm) { 
  var FAQcontent = elm.parentNode.getElementsByTagName("div")[1]; 
  FAQcontent.style.display = (FAQcontent.style.display == 'block') ? 'none' : 'block'; 
} 
function tableRowMouseOver() {
    var table, row;
    for (var i = 0; table = document.getElementsByTagName('tbody')[i]; i++)
        for (var j = 0; row = table.getElementsByTagName('tr')[j]; j++)
            row.onmouseover = row.onmouseout = function(e) { HTMLDOMUtil.toggleClass(this, 'active'); };
}
function correctSubNavigation() {
    var nav = document.getElementById('sub_navigation'), element = null, i = 1;
    if (nav == null) return;
    for (var i = 0; i < nav.childNodes.length; i++)
        if (nav.childNodes[i].tagName)
            element = nav.childNodes[i];
    if (element.className.indexOf('open') >= 0)
        HTMLDOMUtil.addClass(nav, 'last-open');
}
function clickEnableProducts() {
    var products = HTMLDOMUtil.getElementsByClassName('product', 'div');
//		 products = products.concat(HTMLDOMUtil.getElementsByClassName('action', 'div', document.getElementById('related_information')));
    for (var i = 0; i < products.length; i++) {
        if (products[i].getElementsByTagName('a').length == 0)
            continue;
        products[i].url = products[i].getElementsByTagName('a')[0].href;
        products[i].onclick = function(e) { window.location = this.url; };
		 		 if (products[i].className.indexOf('action') >= 0)
		 		 		 HTMLDOMUtil.addClass(products[i], 'clickable');
    }
}
function correctPopupContext() {
    var link = document.getElementById('parent_relation');
    if (link == null || link.href == '' || link.href == window.location.href ||
        window.parent.location.href != window.location.href)
        return;
    var url = link.href;
    url += url.indexOf('?') >= 0 ? '&' : '?';
    url += url.indexOf('OpenDocument') < 0 ? 'OpenDocument&' : '';
    window.location = url + 'open_popup=' + URL.encode(window.location.href);
}
function openReferringPopup() {
    var popup = URL.parameter('open_popup');
    if (popup.length <= 0)
        return;
    openPopup(tmp_prototype_getURL(URL.decode(popup)));
}
function openPopup(url, id) {
    id = id || 'resource_window';
    var popup = new IframePopup(url, id);
    popup.render();
    return false;
}
function searchGoogle() {
  var sterm = document.getElementById("gogo").value;
  var reg1 = new RegExp("\"","g"); 
  var reg2 = new RegExp("'", "g");
  var reg3 = new RegExp("<", "g");
  sterm = sterm.replace(reg1, "");
  sterm = sterm.replace(reg2, "");
  sterm = sterm.replace(reg3, "");
  window.location = "/site/stb.nsf/frmgugl.html?ReadForm&q="+sterm;
}
function hackSearch() {
    if (!document.getElementById('search') || !document.getElementById('k'))
        return;
    document.getElementById('search').onsubmit = function(e) {
        window.location.href = 'http://finnfrem.storebrand.no/Proxy/results.aspx?k=' + document.getElementById('k').value.replace(/Ã¦/g, '%C3%A6').replace(/Ã¸/g, '%C3%B8').replace(/Ã¥/g, '%C3%A5').replace(/Ã&#8224;/g, '%C3%86').replace(/Ã&#732;/g, '%C3%98').replace(/Ã&#8230;/g, '%C3%85');
        return false;
    }
}
function openNewWindow(URLToOpen, w, h){
    params = "width=" + w + ",height=" + h + ", scrollbars=1, resizable=1, menubar=0, toolbar=1, location=0";
    popupWin = window.open(URLToOpen, 'stbwindow', params);
    return false;
}
/**
 * TEMPORARY function that adds the parameter ?layout=popup to the
 * links url. This works in the prototype, but is not required for
 * the popups to work. It provides an elegant fallback
 */
function tmp_prototype_getURL(href) {
    var pieces = href.split('#');
	
	href = pieces[0];
	href += href.indexOf('?') >= 0 ? '&' : '?';
	href += href.indexOf('OpenDocument') < 0 ? 'OpenDocument&' : '';
	
    if (pieces.length == 1) {
        return href + 'paged=true&layout=popup';
    } else {
        return href + 'paged=true&layout=popup#section_' + pieces[1];
	}
}
var hWndNettsalg;
 function startChatNettsalg() 
{
   var winWidth = 562;
   var winHeight = 515;
    windowFeatures = "width=" + winWidth + " ,height=" + winHeight + " ,dependent=no,toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=yes,resizable=yes";
                hWndNettsalg = window.open("https://www2.storebrand.no/i3Root/SB_ChatInit.htm?queueName=Nettsalg%20Chat", "_blank", windowFeatures);
                 document.cookie = "cookies=true";
                                if (document.cookie == "")               {
                               hWndNettsalg.document.write ("Web Interactions cannot be run without session cookies enabled.  If you are using IE 6.x, select Tools|Internet Options from the browser menu.  Select the Privacy tab, then the Advanced button.  Make sure the <u>Always allow session cookies</u> option is enabled.  This does not write any cookies to your machine, but allows a particular web session to pass cookies back and forth.");
                               hWndNettsalg.document.close();                }
}
 
var hWndPensjon;
 function startChatPensjon() 
{
   var winWidth = 562;
   var winHeight = 515;
     windowFeatures = "width=" + winWidth + " ,height=" + winHeight + " ,dependent=no,toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=yes,resizable=yes";
                hWndPensjon = window.open("https://www2.storebrand.no/i3Root/SB_ChatInit.htm?queueName=KS%20Pensjon%20Chat", "_blank", windowFeatures);
                 document.cookie = "cookies=true";
                if (document.cookie == "")   {
                               hWndPensjon.document.write ("Web Interactions cannot be run without session cookies enabled.  If you are using IE 6.x, select Tools|Internet Options from the browser menu.  Select the Privacy tab, then the Advanced button.  Make sure the <u>Always allow session cookies</u> option is enabled.  This does not write any cookies to your machine, but allows a particular web session to pass cookies back and forth.");
                               hWndPensjon.document.close();       }
}
var hWndSparing;
 function startChatSparing() {
   var winWidth = 562;
   var winHeight = 515;
     windowFeatures = "width=" + winWidth + " ,height=" + winHeight + " ,dependent=no,toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=yes,resizable=yes";
                hWndSparing = window.open("https://www2.storebrand.no/i3Root/SB_ChatInit.htm?queueName=KS%20Sparing%20Chat", "_blank", windowFeatures); 
                 document.cookie = "cookies=true";
                                if (document.cookie == "")                {
                               hWndSparing.document.write ("Web Interactions cannot be run without session cookies enabled.  If you are using IE 6.x, select Tools|Internet Options from the browser menu.  Select the Privacy tab, then the Advanced button.  Make sure the <u>Always allow session cookies</u> option is enabled.  This does not write any cookies to your machine, but allows a particular web session to pass cookies back and forth.");
                               hWndSparing.document.close();                }
}
var hWndBank;
 function startChatBank() {
   var winWidth = 562;
   var winHeight = 515;
     windowFeatures = "width=" + winWidth + " ,height=" + winHeight + " ,dependent=no,toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=yes,resizable=yes";
                hWndBank = window.open("https://www2.storebrand.no/i3Root/SB_ChatInit.htm?queueName=Bank%20Chat", "_blank", windowFeatures); 
                 document.cookie = "cookies=true";
                                if (document.cookie == "")                {
                               hWndBank.document.write ("Web Interactions cannot be run without session cookies enabled.  If you are using IE 6.x, select Tools|Internet Options from the browser menu.  Select the Privacy tab, then the Advanced button.  Make sure the <u>Always allow session cookies</u> option is enabled.  This does not write any cookies to your machine, but allows a particular web session to pass cookies back and forth.");
                               hWndbank.document.close();                }
}

