/**
*
*  Kolonihagen javascript
*
**/

function fixUrlString(text) 
{
    // Funker bare på første forekomst as strengen. Javascript bug? Bruker ikke mer tid på dette... Georg
    r = text.replace(/ /, "%20");
    r = r.replace("?", "%3f");
    r = r.replace(/&/, "%26");
    r = r.replace(/=/, "%3d");
    return r;
}

function gotoUrl(url) {

    // alert('default.aspx' + url);
    window.location = 'default.aspx' + url;
}

function postbarSearchLostFocus(box, helptext) {
    if (box.value == '') {
        box.value = helptext;
    }
}

function postbarSearchGotFocus(box, helptext) {
    if (box.value == helptext) {
        box.value = '';
    }
}

function postbarSearchGotoResultpage(did, viewtype, textfieldid, helptext) {
    data = document.getElementById(textfieldid).value;
    if (data != '')
    {
        if (data != helptext) 
        {
            window.location = '?did=' + did + '&viewtype=' + viewtype + '&postbarsearch=' + fixUrlString(data);
        }
    }
}

function addToCart(did) {
    // alert('?did=' + did + '&viewtype=addtocart');
    gotoUrl('?did=' + did + '&viewtype=addtocart');
}

function addCheckout(did) {
    // alert('?did=' + did + '&viewtype=addtocart');
    gotoUrl('?did=' + did + '&viewtype=addtocart');
}

function AddUserInvoiceLines(checkbox) {
    if (checkbox.checked == false) {
        document.getElementById("adduser-invoice1").style.display = "inherit";
        document.getElementById("adduser-invoice2").style.display = "inherit";
        document.getElementById("adduser-invoice3").style.display = "inherit";
    }
    else {
        document.getElementById("adduser-invoice1").style.display = "none";
        document.getElementById("adduser-invoice2").style.display = "none";
        document.getElementById("adduser-invoice3").style.display = "none";
    }
}

function CheckOrgNumber(number) {
    var numCount = 0;
    var validChars = '0123456789';
    for (var c = 0; c < number.length; c++) {
        if (validChars.indexOf(number.charAt(c)) >= 0) {
            numCount++; 
        }
    }
    return numCount++;
}

function TogglePayment(id, src, lockedvalue) {
    if (CheckOrgNumber(src.value) < 9){
        // document.getElementById(id).disabled = true;
        document.getElementById(id).disabled = true;
        document.getElementById(id).value = lockedvalue;
    }
    else{
        document.getElementById(id).disabled = false;
    }
}

function ClickIfEnter(buttonName) {
    var button;
    alert('buttonName');
    if (event.keyCode && event.keyCode == 13) {
        alert('buttonName');
        button = document.getElementById(buttonName);
        button.Click();
    }
}