﻿function getUrlVars() {
    var vars = [], hash;
    var hashes = window.location.href.slice(window.location.href.indexOf('?') + 1).split('&');
    for (var i = 0; i < hashes.length; i++) {
        hash = hashes[i].split('=');
        vars.push(hash[0]);
        vars[hash[0]] = hash[1];
    }
    return vars;
}

$j.urlParam = function(name) {
    var results = new RegExp('[\\?&]' + name + '=([^&#]*)').exec(window.location.href);
    if (!results) { return 0; }
    return results[1] || 0;
}


function removeSpace(name) {
    var newName = name;
    while (newName.indexOf('%20') >= 0)
    {
        newName = newName.replace('%20', ' ');
    }
    //æ
    newName = replaceChar(newName, '%C3%A6', 'æ');
    newName = replaceChar(newName, '%C3%86', 'Æ');

    //ø
    newName = replaceChar(newName, '%C3%B8', 'ø');
    newName = replaceChar(newName, '%C3%98', 'Ø');
    
    //å
    newName = replaceChar(newName, '%C3%A5', 'å');
    newName = replaceChar(newName, '%C3%85', 'Å');


    return newName;
}

function replaceChar(str, strReplace, strNewChar) {
    while (str.indexOf(strReplace) >= 0) {
        str = str.replace(strReplace, strNewChar);
    }
    return str;
}

    

$j(function () {

        $j("#searchboxmain").click(function () {
            if (this.value == this.defaultValue) {
                this.value = '';
                return true;
            }
        });

        $j('#searchboxmain').keyup(function (event) {
            if (event.keyCode == '13') {
                event.preventDefault();
                location.href = "/funksjoner/sokeresultat?q=" + $j(this).attr("value");
                return false;
            }
        });




        $j("#searchboxresult").click(function() {
            if (this.value == this.defaultValue) {
                this.value = '';
                return true;
            }
        });
    $j("#searchbuttonresult").click(function () {
        location.href = "/funksjoner/sokeresultat?q=" + $j("#searchboxresult").attr("value");
        return false;
    });
    $j('#searchboxresult').keyup(function (event) {
        if (event.keyCode == '13') {
            event.preventDefault();
            //            location.href = "/functions/sokeresultat?q=" + $j(this).attr("value");
            location.href = "/funksjoner/sokeresultat?q=" + $j(this).attr("value");
            return false;
        }
    });
});


