﻿    function gotoUri(uri,target)
    {
        switch(target)
        {
            case 'opener':
                if(opener)
                {
                    opener.location.href = uri;
                    opener.focus();
                }
                else
                {
                    var win = window.open(uri);
                    win.focus();
                }
                break;
            case '_top':
                var p = document;
                
                while(true)
                {
                    if(p.parent != null)
                    {
                        p = p.parent;
                    }
                    else
                    {
                        break;
                    }                
                }
                p.location.href = uri;
                
                break;            
            case 'blank':
            case '_blank':
                var win = window.open(uri);
                win.focus();
                break;
            case '':
            case null:
            case '_self':
                location.href = uri;
                break;
            default:
                if(parent.frames[target] != null)
                {
                    parent.frames[target].location.href = uri;
                }
                else
                {
                    var win = window.open(uri);
                    win.focus();
                }
                break;
        }        
    }
    
    function replaceUri(uri,target)
    {
        switch(target)
        {
            case 'opener':
                if(opener)
                {
                    opener.location.replace(uri);
                    opener.focus();
                }
                break;
            case '_top':
                var p = document;
                
                while(true)
                {
                    if(p.parent != null)
                    {
                        p = p.parent;
                    }
                    else
                    {
                        break;
                    }                
                }
                p.location.replace(uri);
                
                break;            
            case 'blank':
            case '_blank':
                break;
            case '':
            case null:
            case '_self':
                location.replace(uri);
                break;
            default:
                if(parent.frames[target] != null)
                {
                    parent.frames[target].location.replace(uri);
                }
                break;
        }        
    }
    
    function openWindow(url,name,width,height,extra,properties,owner)
    {
        // Properties
        // scrollbars: yes/no
        // resizable: yes/no
        // menubar: yes/no
        // status: yes/no
        // titlebar: yes/no
        // toolbar: yes/no
        
        var win = null;
        var str = '';
        
        str = 'height=' + height + ',width=' + width + ',' + extra;
        if(parseInt(navigator.appVersion)>3)
        {
            str += ',left='+(screen.width-width)/2+',top='+parseInt((screen.height-height)/3)+',' + properties;
            win = window.open(url,name,str);
        }
        if(win)
        {
            if(owner)
            {
                win.opener = owner;
            }
            win.focus();
        }
        return win;
    }
    
    function openInfobox(articleNumber,economicLocation,owner)
    {
        if(economicLocation == '')
        {
            economicLocation = '';
        }
       
        openWindow('/produkter/infobox.asp?varenr='+articleNumber+'&lokation='+economicLocation,'I'+articleNumber+'_' +economicLocation,'667','600','10','scrollbars=yes,resizable=yes',owner);
    }
    
    function openDeliveryStateInfo()
    {
        openWindow('/produkter/forklaring-status.asp','DeliveryStateInfo','475','310','10','scrollbars=yes,resizable=yes',this);
    }
    
    function gotoAgent(articleNumber)
    {
        if(opener != null)
        {
            gotoUri('/agent/agent.asp?varenr=' + articleNumber,'opener');
        }
        else
        {
            gotoUri('/agent/agent.asp?varenr=' + articleNumber,'main');
        }
    }
    
    function checkElement(id)
    {
        if($(id).attr('checked') == 'checked')
        {
            $(id).attr('checked','');
        }
        else
        {
            $(id).attr('checked','checked');
        }
    }
    
    function emptyShoppingCart()
    {
        document.forms[0].action+='?mode=empty';
        document.forms[0].submit();   
        
    }
    
    function startOrdering()
    {
        document.forms[0].action+='?mode=order';
        document.forms[0].submit();
    }
    
    function saveShoppingCart(loggedIn)
    {
        if (loggedIn) 
        {
            replaceUri('/shop/kurv_save.asp?rnd='+Math.random()+'','main'); 
        }
        else 
        {
            gotoUri('/kundecenter/login.asp?mode=presavecart','main');
        }
    }
    
    function saveShoppingCartRewrite(obj,field)
    {
        if (obj.selectedIndex > 0) 
        {
            field.disabled = true;
            obj.focus();
        }
        else
        {
            field.disabled = false;
            obj.form.fldInfoboxName.focus();
        }
    }
    
    function getShoppingCart(loggedIn)
    {
        if (loggedIn) 
        {
            replaceUri('/kundecenter/cart.asp?&rnd='+Math.random(),'main');
        }
        else 
        {
            gotoUri('/kundecenter/login.asp?mode=pregetcart','main');
        }
    }
    
    function deleteShoppingCart(articleNumber,economicLocation)
    {
        replaceUri('/shop/kurv.asp?mode=delete&varenr='+articleNumber+'&lokation='+economicLocation,'main');
    }
    
    function updateShoppingCart()
    {
        document.forms[0].action+='?mode=update';
        document.forms[0].submit();
    }
    
    function continueShopping()
    {
        gotoUri('/produkter/forside2.asp?parent=1','main');
    }
