

$(document).ready(function() {
    AttachOnLoadLogic();
    AttachVirtualPageViewTracking();
});

//This is also run on partial postbacks
function AttachOnLoadLogic() {
    AttachNumberFormat();
//    CreateButtons();
}

function CreateButtons() {
    $('.ButtonBig-Blue, .ButtonBigForward-Green, .ButtonBiggerForward-Green')
        .wrapInner('<span></span>')
        .after('<br class=\'clearBoth\' />');
}


//formatting of input
function AttachNumberFormat() {

    $("input.figure").each(  function() {
        $(this).blur(function() {
            this.value = ThousandSeparator(this.value); 
        });
        
        this.value = ThousandSeparator(this.value);
    });
    
}

function ThousandSeparator(text) {
    var sep = ' ';
    var sRegExp = new RegExp('(-?[0-9]+)([0-9]{3})');
    var sValue = text + '';

    sValue = sValue.replace(/([-., ])+/g, "");

    while (sRegExp.test(sValue)) {
        sValue = sValue.replace(sRegExp, '$1' + sep + '$2');
    }
    return sValue;
}

//function ThousandSeparatorRemove(text) {
//    var sValue = text + '';
//    sValue = sValue.replace(/ /g, "");
//    return sValue;
//}

//VirtualPageView
function AttachVirtualPageViewTracking() {
    var currentPage = document.location.pathname;

    if ((currentPage.toLowerCase().indexOf("sok-om-forbrukslan") > 0)
        || (currentPage.toLowerCase().indexOf("sok-om-kredittkort") > 0)) 
    {
        $('input, area, select').blur(function() {
            //priority of what to use as virtual pagename
            var vp = this.alt || this.id || this.name || this.href;
            SendVirtualPageView(currentPage + '__' + vp);
        });    
    }
}

function SendVirtualPageView(virtualPageAddr) {
    pageTracker._trackPageview(virtualPageAddr);
}


//add thickbox to href & area elements that have a class of .thickbox
function tb_init(domChunk) {
    $(domChunk).click(function() {
        var t = this.title || this.name || null;
        var a = this.href || this.alt;
        var g = this.rel || false;
        tb_show(t, a, g);
        this.blur();
        return false;
    });
}


function ShowApplicationProgress(hideElementID) {

    var hideElement = document.getElementById(hideElementID);
    var showElement = document.getElementById('formWaitWrapper');
    var animGif = document.getElementById('progressBarImg');

    if (hideElement && showElement) {
        hideElement.style.display = 'none';
        showElement.style.display = '';
    }

    if (animGif) {
        //ie doesn't show animated gifs which has been hidden when submiting a page. This trick solves this (setTimeout and random url).
        setTimeout(function() { animGif.src = animGif.src + "?rnd" + new Date().getTime() }, 300);
    }
}


function showImage(imgId,img) {
    var objImg = document.getElementById(imgId);
    if (objImg) 
    {
        if (img != "")
            objImg.src = img;
        else
            objImg.alt ="No image supplied";     
    }          
}   


function expandItem(blockId) {
    var element = document.getElementById(blockId);
    if( element != null) 
    {
        element.style.display = (element.style.display == "block") ? "none" : "block";
    }
}
