﻿//onerror = handleErr;
var errorTxt = "";

//var drawMenuLevel0 brukes ikke - alle poster vises alltid
var drawMenuLevel1 = 0
var drawMenuLevel2 = 0

function pageLoad() {
    //document.getElementById('javaMessage').innerHTML = "test";
    drawProdMenuLeft(drawMenuLevel1, drawMenuLevel2);

    //var debug = "";
    //debug += '<br>href:' + location.href;
    //debug += '<br>location:' + window.location;
    //debug += '<br>pathname:' + location.pathname;
    //debug += '<br>getRelPath:' + getRelPath();
    //debug += '<br>stripPath:' + stripPath();
    //document.getElementById('divExtras').innerHTML = debug;
}

function drawProdMenuLeft(mLevel1, mLevel2) {
    // arg = MasterPage.vMenu_draw(mLevel1, mLevel2, getCookie("menuID"), OnComplete, OnTimeOut, OnError);
    arg = MasterPage.vMenu_draw(mLevel1, mLevel2, 1, OnComplete, OnTimeOut, OnError); //
    return (arg);
}

function OnComplete(arg) {
    document.getElementById('divMenu').innerHTML = arg;
}

function OnTimeOut(arg) {
    alert("Timeout when loading the product menu. The site may not be available." + arg);
}

function OnError(arg) {
    alert("An error occured when loading the product menu" + arg);
}

//var appPath = '<%=Request.PhysicalApplicationPath %>';
function processProdMenuLeftClick(buttonClicked, currentMenuLevel) {
    if (location.pathname != '/cart/Default.aspx') { // '/vista_ibsen_live/cart/Default.aspx'
        // reLOAD PAGE: // "vista_ibsen_live/" +
        window.location =  getRelPath() + "cart/Default.aspx?loc=2&btnC=" + buttonClicked + "&mLvl=" + currentMenuLevel;
    }
    else {
        //Postback to mainUpdatePanel:
        var prm = Sys.WebForms.PageRequestManager.getInstance();
        prm._doPostBack('ctl00_ContentPlaceHolder1_upMain', '2:' + escape(currentMenuLevel) + ':' + escape(buttonClicked));
        // window.location.href = '../cart/prod.aspx';
    }
}

function drawProducts(clickSource) {   //( clickSource, searchIn, searchStr, loginID ) loginID trengs i flerbrukermiljø.
    //var searchIn = document.getElementById('ctl00_ContentPlaceHolder1_fldSearchIn').value;
    //var searchStr = document.getElementById('ctl00_ContentPlaceHolder1_fldSearch').value;
    //arg = DefaultClassKunder.drawFooter(clickSource, searchIn, searchStr, 0, OnComplete, OnError, OnTimeOut);
    //return (arg); //  /vista_ibsen_live
    if (location.pathname != '/cart/Default.aspx') {
        //window.location = '/Default.aspx'; //"vista_ibsen_live/" +
        window.location =  getRelPath() + "cart/Default.aspx?loc=3&searchString=" + escape(document.getElementById('fldSearch').value);
    }
    else {
        //code to be executed if condition is not true: do postback
        var prm = Sys.WebForms.PageRequestManager.getInstance();
        prm._doPostBack('ctl00_ContentPlaceHolder1_upMain', '3:' + escape(document.getElementById('fldSearch').value));
    }

    // window.location.href = '/Default.aspx';
    //document.getElementById('divExtras').innerHTML = str;
    //document.getElementById('ctl00_fldSearch').value = location.pathname;
}

function getRelPath() {
    var path = '';
    for (var i = 0; i < document.location.pathname.split('/').length - 2; i++)
        path += '../';
    return path;
}

function stripPath() { // FOR TEST ONLY
    var path = document.location;
    var str = new String(path);
    var len = str.length;
    var start = str.lastIndexOf("/");
    var document_path = str.substring(start + 1, len);
    return document_path;
}

function handleErr(msg, url, l) {
    errorTxt = "There was an error on this page.\n\n";
    errorTxt += "Error: " + msg + "\n";
    errorTxt += "URL: " + url + "\n";
    errorTxt += "Line: " + l + "\n\n";
    errorTxt += "Click OK to continue.\n\n";
    errorTxt += window.location + '<br />';
    errorTxt += getRelPath() + "Default.aspx?loc=3&searchString="; //vista_ibsen_live/
    alert(errorTxt);
    return true;
}

//function CSP_focus(id) {
//    var o = document.getElementById(id);
//    if (o != null)
//        o.focus();
//}

function setCookie(c_name, value, expiredays) {
    var exdate = new Date();
    exdate.setDate(exdate.getDate() + expiredays);
    document.cookie = c_name + "=" + escape(value) + ((expiredays == null) ? "" : ";expires=" + exdate.toGMTString()); // + "; path=/;");
    // ved å unnlate '; path=/' får vi forskjellige cookies for hvert skjema 
}

function getCookie(c_name) {
    if (document.cookie.length > 0) {
        c_start = document.cookie.indexOf(c_name + "=");
        if (c_start != -1) {
            c_start = c_start + c_name.length + 1;
            c_end = document.cookie.indexOf(";", c_start);
            if (c_end == -1) c_end = document.cookie.length;
            return unescape(document.cookie.substring(c_start, c_end));
        }
    }
    return "";
}
