function selectOneValue(defaultVal, backupVal) {
    if (defaultVal != null && defaultVal.length > 0 && defaultVal > -1) {
        return defaultVal;
    } else {
        return backupVal;
    }
}

function setWaitMsg(message) {
    document.getElementById('waitMsg').innerHTML = "<img src=\"img/indicator.gif\"> " + message;
    showObject(document.getElementById('waitMsg'));
}

function setMessage(message) {
    document.getElementById('waitMsg').innerHTML = message;
    showObject(document.getElementById('waitMsg'));
}

function clearWaitMsg() {
    hideObject(document.getElementById('waitMsg'));
}

function adminKommune() {
    hideObject(document.getElementById('sokeSpan'));
    showObject(document.getElementById('adminSpan'));
    document.getElementById('adminKomm').submit();
}

function toggleMatrSearch() {
    document.forms['search'].matrikkelsoek.value = 1;
    document.getElementById('searchWindowFrame').style.height = 142;
    hideObject(document.getElementById('simpleSearch'));
    hideObject(document.getElementById('advancedSearch'));
    showObject(document.getElementById('matrSoek'));
    Set_Cookie("sokeTypeCook", document.forms['typeSok'].sokeType.options[document.forms['typeSok'].sokeType.selectedIndex].value, 9999999);
}

function toggleAdvSearch() {
    document.forms['search'].matrikkelsoek.value = 0;
    document.getElementById('searchWindowFrame').style.height = 257;
    showObject(document.getElementById('simpleSearch'));
    showObject(document.getElementById('advancedSearch'));
    hideObject(document.getElementById('matrSoek'));
    Set_Cookie("sokeTypeCook", document.forms['typeSok'].sokeType.options[document.forms['typeSok'].sokeType.selectedIndex].value, 9999999);
}
function delegateSearchType(id) {
    if (id == "0") {
        toggleSimpleSearch()
    }
    if (id == "1") {
        toggleAdvSearch()
    }
    if (id == "2") {
        toggleMatrSearch()
    }
}

function toggleSimpleSearch() {
    document.forms['search'].matrikkelsoek.value = 0;
    //alert("type:1");
    showObject(document.getElementById('simpleSearch'));
    hideObject(document.getElementById('advancedSearch'));
    hideObject(document.getElementById('matrSoek'));
    document.getElementById('searchWindowFrame').style.height = 142;
    Set_Cookie("sokeTypeCook", document.forms['typeSok'].sokeType.options[document.forms['typeSok'].sokeType.selectedIndex].value, 9999999);
}

function trim(inputString) {
    if (typeof inputString != "string") {
        return inputString;
    }
    var retValue = inputString;
    var ch = retValue.substring(0, 1);
    while (ch == " ") { // Check for spaces at the beginning of the string
        retValue = retValue.substring(1, retValue.length);
        ch = retValue.substring(0, 1);
    }
    ch = retValue.substring(retValue.length - 1, retValue.length);
    while (ch == " ") { // Check for spaces at the end of the string
        retValue = retValue.substring(0, retValue.length - 1);
        ch = retValue.substring(retValue.length - 1, retValue.length);
    }
    while (retValue.indexOf("  ") != -1) {
        retValue = retValue.substring(0, retValue.indexOf("  ")) + retValue.substring(retValue.indexOf("  ") + 1, retValue.length);
    }
    return retValue; // Return the trimmed string back to the user
}

function checkValues() {
    if (document.forms['search'].sokeier.value == 0) {
        alert('Du må velge Eierkommune');
        document.forms['search'].sokeier.focus();
        return false;
    }
    if ((document.forms['search'].matrikkelsoek.value == 1) && (document.forms['search'].gnr.value == null || document.forms['search'].gnr.value == "")) {
        alert('Vennligst angi gårdsnummer (Gnr) som minimum');
        document.forms['search'].gnr.focus();
        return false;
    }
    if (isVisible(document.getElementById('advancedSearch'))) {
        if (trim(document.forms['search'].planDato.value).length != 10 && trim(document.forms['search'].planDato.value).length != 0) {
            alert('Dato-feltet må enten være tomt,\neller inneholde en dato på formatet dd.mm.åååå (f.eks. 25.01.1985)');
            document.forms['search'].planDato.focus();
            return false;
        } else if (isVisible(document.getElementById('planDatoTilSpan')) && trim(document.forms['search'].planDatoTil.value).length != 10 && trim(document.forms['search'].planDatoTil.value).length != 0) {
            alert('Tildato-feltet må enten være tomt,\neller inneholde en dato på formatet dd.mm.åååå (f.eks. 25.01.1985)');
            document.forms['search'].planDatoTil.focus();
            return false;
        }
    }
    return true;
}

function doChangeKommune() {
    if (document.forms['search'].sokeier.selectedIndex > 0) {
        document.forms['changeKommune'].sokeier.value = document.forms['search'].sokeier.options[document.forms['search'].sokeier.selectedIndex].value;
        /*       chosenKomm = document.forms['search'].sokeier.options[document.forms['search'].sokeier.selectedIndex].value;
         //alert(chosenKomm);
         document.getElementById('stedsNavnDesc').innerHTML = chosenKomm;*/
        document.forms['changeKommune'].submit();
/*        if (document.forms['changeKommune'].sokeier.value == "906") {
            enableMatrSoek();
        }*/
    }
    //doSearch(0, 'Endrer eier-kommune', 0)"
}
/*function enableMatrSoek() {
    var dropdown = document.getElementById("sokeType");
    var option = document.createElement("option");
    option.setAttribute("value", 2);        //<option value="2">Matrikkelsøk</option>
    var text = document.createTextNode("Matrikkelsøk");
    option.appendChild(text);
    dropdown.appendChild(option);
    //alert ("matr. is called");
}*/

function doSearch(antall, msg, rapp) {
    hideObject(document.getElementById('adminSpan'));
    showObject(document.getElementById('sokeSpan'));
    if (checkValues()) {
        /*Set_Cookie("planIdCook", document.forms['search'].planId.value, 9999999);
        Set_Cookie("saksNrCook", document.forms['search'].saksNr.value, 9999999);
        Set_Cookie("saksBehandlerCook", document.forms['search'].saksBehandler.value, 9999999);

        Set_Cookie("planNavnCook", document.forms['search'].planNavn.value, 9999999);
        Set_Cookie("stedsNavnCook", document.forms['search'].stedsNavn.value, 9999999);
        Set_Cookie("planDatoCook", document.forms['search'].planDato.value, 9999999);
        Set_Cookie("planDatoTilCook", document.forms['search'].planDatoTil.value, 9999999);

        Set_Cookie("planDatoBefAftCook", document.forms['search'].befAft.options[document.forms['search'].befAft.selectedIndex].value, 9999999);*/
        Set_Cookie("kildeCook", document.forms['search'].sokeier.options[document.forms['search'].sokeier.selectedIndex].value, 9999999);
//        Set_Cookie("planTypeCook", document.forms['search'].planType.options[document.forms['search'].planType.selectedIndex].value, 9999999);
//        Set_Cookie("planStatusCook", document.forms['search'].planStatus.options[document.forms['search'].planStatus.selectedIndex].value, 9999999);
        Set_Cookie("planStatusCook", document.forms['search'].overstatus.options[document.forms['search'].overstatus.selectedIndex].value, 9999999);
//        Set_Cookie("antTreffCook", document.forms['search'].antTreff.options[document.forms['search'].antTreff.selectedIndex].value, 9999999);
        Set_Cookie("advSearchCook", (isVisible(document.getElementById('advancedSearch')) ? 'show' : 'hide'), 9999999);

        document.forms['search'].simple.value = (isVisible(document.getElementById('advancedSearch')) ? 'show' : 'hide');
        document.forms['search'].antall.value = antall;

        if (rapp == 1) {
            document.forms['search'].excel.value = "yes";
            document.forms['search'].target = "_blank";
            document.forms['search'].action = "planSok/braplanRapport.jsp";
        } else {
            document.forms['search'].excel.value = "no";
            document.forms['search'].target = "_sokeframe";
            document.forms['search'].action = "planSok/searchPlan.jsp";
            setWaitMsg(msg);
        }

        raiseButton(document.getElementById('sokresbutt'));

        document.forms['search'].submit();
    }
}

function logOut() {
    document.forms['logout'].submit();
}

function viewPlan(uid) {
    document.forms['visPlan'].uid.value = uid;
    window.open("about:blank", "_visplan", "width=718,height=525,scrollbars=no", "_visplan", true);
    document.forms['visPlan'].submit();
}

function deletePlan(planuid, kortnavn) {
    if (confirm('ADVARSEL!\nEr du sikker på at du vil slette planen \'' + kortnavn + '\' ?')) {
        document.forms['slettPlan'].objektuid.value = planuid;
        document.forms['slettPlan'].planid.value = kortnavn;

        setWaitMsg('Sletter plan \'' + kortnavn + '\'');
        document.forms['slettPlan'].submit();
    }
}

function editPlan(planid) {
    document.forms['gotoSearch'].uid.value = planid;
    document.forms['gotoSearch'].submit();
}

function editUsers() {
    setWaitMsg('Henter frem bruker-informasjon');
    document.forms['editUser'].submit();
}

function Get_Cookie(name) {
    var start = document.cookie.indexOf(name + "=");
    var len = start + name.length + 1;
    if ((!start) && (name != document.cookie.substring(0, name.length))) return '';
    if (start == -1) return '';
    var end = document.cookie.indexOf(";", len);
    if (end == -1) end = document.cookie.length;
    return unescape(document.cookie.substring(len, end));
}

function Set_Cookie(name, value, nSeconds) {
    var todays_date = new Date();
    var expires = new Date(todays_date.getTime() + (1000 * 60 * 60 * 24 * 365));
    document.cookie = name + '=' + value + ';expires=' + expires.toGMTString() + ';'
}

function optionSelected(opt, value) {
    for (var i = 0; i < opt.length; i++) {
        if (opt.options[i].value == value) {
            opt.options[i].selected = true;
        }
    }
}

function clearDefault(field, defValue) {
    if (field.value.indexOf(defValue) > -1) {
        field.value = '';
    }
}

function changeDatoValg(verdi) {
    if (verdi == 3) {
        showObject(document.getElementById('planDatoTilSpan'));
    } else {
        hideObject(document.getElementById('planDatoTilSpan'));
    }
}

function clearSearch() {
    document.forms['search'].planId.value = '';
    document.forms['search'].saksNr.value = '';
    document.forms['search'].planNavn.value = '';

    //only clear advanced fields if they are showing....
    if (isVisible(document.getElementById('advancedSearch'))) {

        document.forms['search'].saksBehandler.value = '';
        document.forms['search'].stedsNavn.value = '';
        document.forms['search'].planDato.value = 'dd.mm.åååå';
        document.forms['search'].planDatoTil.value = 'dd.mm.åååå';

        optionSelected(document.forms['search'].planType, 0);
        optionSelected(document.forms['search'].planStatus, 0);
        optionSelected(document.forms['search'].antTreff, 100);
    }

    //only clear matr fields if they are showing....
    if (isVisible(document.getElementById('matrSoek'))) {
        document.forms['search'].gnr.value = '';
        document.forms['search'].bnr.value = '';
        document.forms['search'].fnr.value = '';
    }
}

function raiseButton(butval) {
    lowerButtons();
    butval.className = 'upTab';
}

function lowerButtons() {
    document.getElementById('sokresbutt').className = 'downTab';
    document.getElementById('forslagbutt').className = 'downTab';
    document.getElementById('vedtakbutt').className = 'downTab';
}

function visVedtak() {
    document.forms['lastHendelse'].kildeId.value = document.forms['search'].sokeier.value;
    document.forms['lastHendelse'].status.value = "4";
    raiseButton(document.getElementById('vedtakbutt'));
    setWaitMsg('Søker etter nye planvedtak');
    document.forms['lastHendelse'].submit();
}

function visForslag() {
    document.forms['lastHendelse'].kildeId.value = document.forms['search'].sokeier.value;
    document.forms['lastHendelse'].status.value = "3";

    raiseButton(document.getElementById('forslagbutt'));
    setWaitMsg('Søker etter nye planforslag');
    document.forms['lastHendelse'].submit();
}

function trySearch(charValue) {
    if (charValue == 13) {
        doSearch(5000, 'Søker etter planer', 0);
    }
}
