﻿function loadWindowGeneral(wndPopupVar, url, title, width, height, iconClass) {
    try {
        wndPopupVar.clearContent();
        wndPopupVar.setHeight(height);
        wndPopupVar.setWidth(width);
        wndPopupVar.minHeight = height;
        wndPopupVar.minWidth = width;
        //var html = String.format("<iframe width=\"100%\" id=\"Window1_iframe\" height=\"100%\" src=\"{0}\" frameborder=\"0\"></iframe>", url)           
        //wndPopupVar.body.update(html);
        wndPopupVar.setTitle(title);
        wndPopupVar.maximizable = true;
        
        wndPopupVar.bodyBorder = false;
        wndPopupVar.border = false;
        wndPopupVar.plain = true;
        if (iconClass != "")
            wndPopupVar.setIconClass(iconClass);

        //if (!wndPopupVar.iframe)        
            wndPopupVar.load({ url: url, mode: 'iframe' });
        wndPopupVar.show();
    }
    catch (e) {
        Ext.Msg.alert('Error', e.message);
    }
}


var newWindow = function(config, url, id, renderToLevel) {
    var renderToObject;
    var window;

    if (parent.parent.parent != null && renderToLevel == 3) {
        renderToObject = parent.parent.parent;
    }
    else if (parent.parent.Ext.getBody() != null && renderToLevel == 2) {
        renderToObject = parent.parent;
    }
    else if (parent.Ext.getBody() != null && renderToLevel == 1) {
        renderToObject = parent;
    }
    else {
        renderToObject = this;
    }

    body = renderToObject.Ext.getBody();
    window = renderToObject.Ext.getCmp(id);
    if (window != null)
        window.close();

    new renderToObject.Ext.Window(renderToObject.Ext.apply({
        renderTo: renderToObject.Ext.getBody(),
        resizable: true,
        height: 500,
        width: 500,
        frame: true,
        padding: 5,
        id: id,
        autoLoad: { maskMsg: "Laddar...", showMask: true, mode: "iframe", url: url }
    }, config)).show();
}



var lastItem = null;
function markSelectedTRorTD(selectedItem, color) {
    try {
        if (color == "")
            color = "eaeaea";

        if (lastItem != null && lastItem != 0) {
            if (lastItem.tagName == "TR") {
                //var rows = selectedItem.parentNode.getElementsByTagName("tr");
                //for (i = 0; i < rows.length; i++) {
                //    rows[i].bgColor = "FFFFFF";
                //}
                lastItem.bgColor = "FFFFFF";
            }
            else if (lastItem.tagName == "TD") {
                var cells = lastItem.parentNode.rows[i].getElementsByTagName("td");
                for (j = 0; j < cells.length; j++) {
                    cells[j].bgColor = "FFFFFF";
                }
            }
        }
        
        lastItem = selectedItem;
    /*
        else if (selectedItem.tagName == "TD") {
            var rows = selectedItem.parentNode.parentNode.getElementsByTagName("tr");
            for (i = 0; i < rows.length; i++) {
                var cells = rows[i].getElementsByTagName("td");
                for (j = 0; j < cells.length; j++) {
                    cells[j].bgColor = "FFFFFF";
                }
            }
        }
*/
        selectedItem.bgColor = color;
/*
        //Mark group BEGIN
        if ((selectedItem.cells[0].innerHTML.charAt(0) != ' ' && selectedItem.cells[0].innerHTML.charAt(0) != '&') && selectedItem.tagName == "TR") {

            var markNext = true;
            var nextElement = selectedItem.nextSibling;
            while (markNext) {
                if (nextElement != null) {
                    if (nextElement.cells[0].innerHTML.charAt(0) == '&' || nextElement.cells[0].innerHTML.charAt(0) == ' ') {
                        nextElement.bgColor = color;
                        nextElement = nextElement.nextSibling;
                    }
                    else
                        markNext = false;
                }
                else
                    markNext = false;
            }
        }
        //Mark group END
        */
    }
    catch (e) {
        Ext.Msg.alert('markSelectedTRorTD', 'markSelectedTRorTD():' + e.message);
    }
}

function FirstComboValue(cbComboBox, IdField) {
    try {
        if (cbComboBox.store.data.length != 0)
            cbComboBox.setValue(cbComboBox.store.getAt(0).get(IdField));
    }
    catch (e) {
        Ext.Msg.alert('Error settig first value', e.message);
    }
}

function FirstComboValue_IfOneValue(cbComboBox, IdField) {
    try {
        if (cbComboBox.store.data.length != 0 && cbComboBox.store.data.length == 1) {
            cbComboBox.setValue(cbComboBox.store.getAt(0).get(IdField));
            return true;
        }   
    }
    catch (e) {
        Ext.Msg.alert('Error settig first value', e.message);
    }
    return false;
}

function UppdateraKundkorgGlobalWrapper() {
    var body;
    try {
        
        if (parent.parent.parent.pnlKundkorg.getBody() != null)
            body = parent.parent.parent.pnlKundkorg.getBody();
        else if (parent.parent.pnlKundkorg.getBody() != null)
            body = parent.parent.pnlKundkorg.getBody();
        else if (parent.pnlKundkorg.getBody() != null)
            body = parent.pnlKundkorg.getBody();
        else
            body = pnlKundkorg.getBody();        
        
        if (body != null) {
            if (body.pnlOrder != null) {
                body.Ext.net.DirectMethods.UpdateraKundkorg({ success: function(result) { body.pnlOrder.update(result); }, failure: function(result) { UppdateraKundkorgGlobalWrapperFailure(body) } });
            }
        }
    }
    catch (e) {
        Ext.Msg.alert('Error', e.message);
    }
}

var pnlWeb2Print_PreviewUrlUrl = ""
function Web2Print_LoadSavedPreview() {
    try {
        if (pnlWeb2Print_PreviewUrlUrl != "") {
            if (parent.parent.parent.pnlWeb2Print.getBody() != null)
                body = parent.parent.parent.pnlWeb2Print.getBody();
            else if (parent.parent.pnlWeb2Print.getBody() != null)
                body = parent.parent.pnlWeb2Print.getBody();
            else if (parent.pnlWeb2Print.getBody() != null)
                body = parent.pnlWeb2Print.getBody();
            else
                body = pnlWeb2Print.getBody();

            if (body != null) {
                var varPreview = body.pnlPreview;
                varPreview.loadIFrame({ url: pnlWeb2Print_PreviewUrlUrl });
            }
        }
    }
    catch (e) {
        Ext.Msg.alert('Error - Web2Print_LoadSavedPreview', e.message);
    }
}
function Web2Print_ClearPreview() {
    var body;
    try {

        if (parent.parent.parent.pnlWeb2Print.getBody() != null)
            body = parent.parent.parent.pnlWeb2Print.getBody();
        else if (parent.parent.pnlWeb2Print.getBody() != null)
            body = parent.parent.pnlWeb2Print.getBody();
        else if (parent.pnlWeb2Print.getBody() != null)
            body = parent.pnlWeb2Print.getBody();
        else
            body = pnlWeb2Print.getBody(); 
                     
        
        if (body != null) {
            var varPreview = body.pnlPreview;
            if (varPreview.autoLoad != null)
                pnlWeb2Print_PreviewUrlUrl = varPreview.autoLoad.url;
            varPreview.loadIFrame({ url: 'ImageHandler.ashx?Type=' });
        }        
    }
    catch (e) {
        Ext.Msg.alert('Error - Web2Print_ClearPreview', e.message);
    }
}

function UppdateraKundkorgGlobalWrapperFailure(body) {

    try {
        if (updateSuccessfull == false) {
            Ext.Msg.alert('Kundkorgsfel', "Något fick fel vid uppdateringen av kundkorgen");
            if (body != null) {
                if (body.pnlOrder != null) {body.pnlOrder.update("Fel vid uppdatering av kundkorg"); }
            }        
        }
    } 
    catch (e) {
        Ext.Msg.alert('Error', e.message);
    }       
}

function UppdateraKundkorgLabel(antal) {
    try {
        var button = null;
        var panel = null
        if (parent.parent.parent.btnKundkorg != null)
            button = parent.parent.parent.btnKundkorg;
        else if (parent.parent.btnKundkorg != null)
            button = parent.parent.btnKundkorg;
        else if (parent.btnKundkorg != null)
            button = parent.btnKundkorg;
        else
            button = btnKundkorg;

        if (parent.parent.parent.pnlKundkorg != null)
            panel = parent.parent.parent.pnlKundkorg;
        else if (parent.parent.pnlKundkorg != null)
            panel = parent.parent.pnlKundkorg;
        else if (parent.pnlKundkorg != null)
            panel = parent.pnlKundkorg;
        else
            panel = pnlKundkorg;
                
        if (button != null)
            button.setText(antal);
        if (panel != null)
            panel.setTitle(antal);

    }
    catch (e) {
        Ext.Msg.alert('Error', e.message);
    }    
}


function SessionExpired() {
    try {    
        var Page = "Login.aspx?SessionExpired=true";
        if (parent.parent.parent.window.location != null)
            parent.parent.parent.window.location = Page;
        else if (parent.parent.window.location != null)
            parent.parent.window.location = Page;
        else if (parent.window.location != null)
            parent.window.location = Page;
        else
            window.location = Page;    
    }
    catch (e) {
        Ext.Msg.alert('Error', e.message);
    }
}


function VisaBestallning(Produktnr, Batchid, Pris) {

    winBestallning.center();
    winBestallning.show();

    numAntal.focus(false, 200);

    txtProduktnr.setValue(Produktnr);
    
    if (Batchid != "")
        txtBatchid.setValue(Batchid);
                
    txtPris.setValue(Pris);
    numAntal.setValue("");
}


function VisaBild(url, title) {

    try
    {
        var html = String.format("<iframe width=\"100%\" id=\"Window1_iframe\" height=\"100%\" src=\"{0}\" frameborder=\"0\"></iframe>", url)

        if (parent.parent.winImage != null) {
            parent.parent.winImage.body.update(html);
            parent.parent.winImage.setTitle(title);
            parent.parent.winImage.show();
        }
        else if (parent.winImage != null) {
            parent.winImage.body.update(html);
            parent.winImage.setTitle(title);
            parent.winImage.show();
        }
        else if (winImage != null) {
            winImage.body.update(html);
            winImage.setTitle(title);
            winImage.show();
        }
    }
    catch(e)
    {
        Ext.Msg.alert('Error',e.message);    
    }       
}

function MaskEnterKeypress(e1)
{
    var isEnter = false;
    try {
        isEnter = e1.getKey() == e1.ENTER;
        if (isEnter) 
        {
            e1.preventDefault(); 
        }
    }
    catch(e)
    {
        Ext.Msg.alert('Error',e.message);    
    }    
    return isEnter;
}

function openNewWindow(title, url, id, modal, ClearReload_Web2Print, CheckForAcrobat, width, height) {
    try {
        if (width == 0)
            width = 760;
        if (height == 0)
            height = 500
            if (ClearReload_Web2Print) {
                Web2Print_ClearPreview();
                newWindow({ title: title, height: height, width: width, minHeight: height, minWidth: width, modal: modal, maximizable: true, listeners: { hide: function() { Web2Print_LoadSavedPreview(); } } }, url, id, 1);
            }
            else
                newWindow({ title: title, height: height, width: width, minHeight: height, minWidth: width, modal: modal, maximizable: true }, url, id, 1);
    }
    catch (e) {
        Ext.Msg.alert('Error', e.message);
    }    
}

function AcrobatReaderMessage() {
    var acrobat = false;
    try {
        acrobat = CheckForAcrobatReader();
        if (acrobat == false)
            ExtNotification('Acrobat Reader', 'Acrobat Reader verkar ej vara installerat, detta program behövs för att kunna visa PDF-dokument. <a target="_new" href="http://get.adobe.com/se/reader">Ladda hem Acrobat Reader</a>', 'icon-pagewhiteacrobat');
        return acrobat;
    }
    catch (e) {
        Ext.Msg.alert('Error', e.message);
    }
}

function CheckForAcrobatReader() {
    var found = false;
    var info = '';
    try {

        try {
            acrobat4 = new ActiveXObject('PDF.PdfCtrl.1');
            if (acrobat4) {
                found = true;
                info = 'v. 4.0';
            }
        }
        catch (e) {
            //??? 
        }

        if (!found) {
            try {
                acrobat7 = new ActiveXObject('AcroPDF.PDF.1');
                if (acrobat7) {
                    found = true;
                    info = 'v. 7+';
                }
            }
            catch (e) {
                //??? 
            }

            if (!found && navigator.plugins && navigator.plugins.length > 0) {
                for (var i = 0; i < navigator.plugins.length; i++) {
                    if (navigator.plugins[i].name.indexOf('Adobe Acrobat') > -1) {
                        found = true;
                        info = navigator.plugins[i].description + ' (' + navigator.plugins[i].filename + ')';
                        break;
                    }
                }
            }
        }
        return found;
    }
    catch (e) {
        Ext.Msg.alert('Error', e.message);
    }

}


function ExtNotification(title, html, icon) {
    Ext.net.Notification.show({
        iconCls: icon,
        autoHide: false,
        html: html,
        title: title,
        alignToCfg: {
            offset: [-18, -45]
        }
    });
}
