
var ProductImages = {
    Show : function() {
        var nodes = document.getElementsByTagName('IMG');
        var lth = nodes.length;
        for (var i = 0; i < lth; ++i) {
            var o = nodes[i].getAttribute('path');
            if (o != null) {
                nodes[i].src = o;
                nodes[i].style.display = 'block';
                nodes[i].style.filter = 'alpha(opacity=100)';
            }
        }
    },
    Hide : function() {
        var nodes = document.getElementsByTagName('IMG');
        var lth = nodes.length;
        for (var i = 0; i < lth; ++i) {
            var o = nodes[i].getAttribute('path');
            if (o != null) nodes[i].style.display = 'none';
        }
    },
    Check: function() {
        var o = document.getElementById('chkShowImages');
        if (!!!o) alert('chkShowImages Element not found');

        if (o.checked) {
            ProductImages.Show();
        }
        else {
            ProductImages.Hide();
        }
    },
    Checked: function() {
        var o = document.getElementById('chkShowImages');
        if (!!!o) alert('chkShowImages Element not found');

        var ShowImages = 'true'
        if (o.checked) {
            ShowImages = 'true';
        }
        else {
            ShowImages = 'false';
        } 
        var url = '/Catalog/ProductView.aspx' + QueryString('ShowImages', ShowImages)
        top.neotekText(url, null);
        ProductImages.Check();
    }
};

function ShowLoadingImage() {
    var oo = document.getElementById('ItemPanel');
    oo.innerHTML = '';
    var div = document.createElement('div');
    div.style.textAlign = 'center';
    div.innerHTML = '<img src="/Owner/Skin/images/progress.gif" />';
    oo.appendChild(div);
}

function ChangeProductView()
{
    var o = document.getElementById('ListStyle');
    var view = o.options[o.selectedIndex];
    view = view.getAttribute('ViewID');
 
    ShowLoadingImage();    
    top.neotekText('/Catalog/ProductView.aspx' + QueryString('ChangeView', view), ProductViewCallback);
}

function ChangeSearchMode()
{
    var o = document.getElementById('ListSearchMode');
    var mode = o.options[o.selectedIndex];
    mode = mode.getAttribute('ModeID');
 
    ShowLoadingImage();    
    window.location = '/Catalog/ProductView.aspx' + QueryString('SearchMode', mode);
}

function ChangePage(PageNum) {
    ShowLoadingImage();
    window.location = '/Catalog/ProductView.aspx' + QueryString('PageNo', PageNum);
}

function QueryString(key,value) {
    var url = window.location.search;
    if (url.length > 1) url = url.substring(1);
    else return null;
    var querystring='';
    var keyValuePairs = url.split("&");
    for (var i = 0; i < keyValuePairs.length; ++i) {
        var k = keyValuePairs[i].split('=')[0];
        if (k.toLowerCase() != key.toLowerCase()){
            if (k.toLowerCase() == 'ref' || k.toLowerCase() == 'id' || k.toLowerCase() == 'txtsearch' || k.toLowerCase() == 'tempview'){
                querystring +='&' + keyValuePairs[i];
            }
        }
    }
    querystring += '&' + key + '=' + value;
    return '?' + querystring.substring(1);   
}

function UpdatePageSize(e) {
    e = e || window.event;
    var o = e.target || e.srcElement;
    window.location = '/Catalog/ProductView.aspx' + QueryString('PageSize', o.value); // Need to update page Numbers so full page refresh is preferred
}

function ProductViewCallback(retVal) {
    var o = document.getElementById('ItemPanel');
    if (o) { 
        o.innerHTML = retVal;
        var scriptNodes = o.getElementsByTagName('script'); // script tags won't run if ajax call (in html)
        var lth = scriptNodes.length;
        for (var i = 0; i < lth; ++i) {
            if (scriptNodes[i].src != '') {
                var script = document.createElement('script');
                script.src = scriptNodes[i].src.toString();
                o.appendChild(script); // Creates another tag but the original won't load and removing it causes other issues
            }
            else 
                eval(scriptNodes[i].innerHTML);
        }
    }
}

function ShowInventoryPopup(e, id)
{
    var settings = {
        URL:'/Catalog/InventoryPopup.aspx?Ajax=1&ProductId=' + id,
        Title:'Inventory',
        ShowClose:true,
        Position:e,
        ShowTriangle:false,
        //ShowTriangle:['left',''],
        Width:400,
        DoFade: false,
        CloseOnLoseFocus:false
    };
    top.cancelPropagation(e);
    top.NeotekPopup.Show(settings);

    e = e || window.event;
    var obj = e.target || e.srcElement;
    if (obj.className == 'NoStock') {
        ShowInventoryPopupEtaTab(0);
    }
}
function ShowInventoryPopupEtaTab(count) {
    if (count > 10) return; // Max 5 sec check time.
    if (top.$('invETATab')) {
        top.ShowTab(1); // Show ETA Tab
    } else {
        setTimeout('ShowInventoryPopupEtaTab(' + (count + 1) + ')', 500);
    }
}

function ShowImage(e, imageURL)
{
    var settings = {
        src:imageURL,
        ShowClose:false,
        Position:e,
        AutoResize:true,
        DoFade:false,
        CloseOnLoseFocus:true
    };
    top.cancelPropagation(e);
   top.NeotekPopup.Show(settings);
}



function ShowTab(num)
{
    var stock = document.getElementById('invStockTbl');
    var eta = document.getElementById('invETATbl');
    if (num == 0) {
        stock.style.display = 'block';
        eta.style.display = 'none';
    } else {
        stock.style.display = 'none';
        eta.style.display = 'block';
    }
}

function OnUnloadCheck() {
    var hasItems = false;
    var df = document.forms['OrderPad'];
    if (df) {
        if (df.Qty) {
            if (df.Qty.length) { // the case when > 1 items on the order pad 
                for (var i = 0; i < df.Qty.length; i++) {
                    var qty;

                    if (df.Qty[i].value == "") {
                        qty = 0;
                    }
                    else {
                        qty = df.Qty[i].value
                    }

                    var orgQty;

                    if (df.OrgQty[i].value == "") {
                        orgQty = 0;
                    }
                    else {
                        orgQty = df.OrgQty[i].value
                    }

                    if ((qty - orgQty) > 0) {
                        hasItems = true;
                        break
                    }
                }
            } 
            else if (df.OrgQty != undefined) { // check to see if there is an original qty to check against
            // The case when there is just one item on the order pad.
                var orgQty;
                var qty;

                if (df.OrgQty.value == "") {
                    orgQty = 0;
                }
                else {
                    orgQty = parseInt(df.OrgQty.value);
                }

                if (df.Qty.value == "") {
                    qty = 0;
                }
                else {
                    qty = parseInt(df.Qty.value);
                }

                if ((qty - orgQty) > 0) {
                    hasItems = true;
                }
            }
            else {
                hasItems = (df.Qty.value != '');
            }
        }
    }
    if (hasItems) {
        if (confirm('You have items not yet submitted to your basket.\n Would you like to add these to your basket now?')) {
            var vals = top.getFormValues(df);
            top.neotekscript(df.action, vals);
        }
    }
}
window.onbeforeunload = OnUnloadCheck; // IE "can't run from freed script" error if onunload tried. (IE7,Vista)