﻿var uploadNewCat = 0;
var mainmenuArray = new Array();
var mainmenuArrayCount = 0;

function addmenuitem(parentid, text, id, tooltip, href, target, className, zindex) {
    var menuitem = '';
    if (parentid == 'mainmenu') {
        menuitem = '<li  class="NeoNavi ' + className + '" ' + (id != null ? 'id="li' + id + '"' : '') + (zindex != null ? 'style="z-index: ' + zindex + ';"' : '') + '>';
        menuitem += '<a ' + (id != null ? 'id="' + id + '"' : '') + ' class="NeoNavi0 ' + className + '" target="' + target + '" title="' + tooltip + '" href="' + href + '">' + text + '</a>';
        menuitem += '</li>';
        mainmenuArray[mainmenuArrayCount] = text;
        mainmenuArrayCount += 1;
    } else {
    menuitem = '<li class="NeoNavi ' + className + '" ' + (id != null ? 'id="li' + id + '"' : '') + (zindex != null ? 'style="z-index: ' + zindex + ';"' : '') + '>';
    menuitem += '<a ' + (id != null ? 'id="' + id + '"' : '') + ' class="NeoNavi ' + className + '" target="' + target + '" title="' + tooltip + '" href="' + href + '">' + text + '</a>';
        menuitem += '</li>';
        if (document.getElementById('ul' + parentid) == null) {
            var el = document.getElementById(parentid);
            if (el != null) {
                var container = '<span>' + el.innerHTML + '<span>';
                el.innerHTML = container;
            }
            el = document.getElementById('li' + parentid);
            if (el != null) { el.innerHTML += '<ul Class="NeoNavm" id="ul' + parentid + '"></ul>'; }

        }
    }
    var ul = document.getElementById('ul' + parentid);
    if (ul != null) { ul.innerHTML += menuitem; }
}

function addmenuitemold(parentid, text, id, tooltip, href, target, className, zindex) {
    addmenuitem(parentid, text, id, tooltip, href, target, className, zindex);
    return;
    var menuitem = '';
    if (parentid == 'mainmenu') {
        menuitem = '<li  class="' + className + '" ' + (id != null ? 'id="li-' + id + '"' : '') + (zindex != null ? 'style="z-index: ' + zindex + ';"' : '') + '><div ' + (id != null ? 'id="' + id + '"' : '') + 'class="' + className + '"><a target="' + target + '" title="' + tooltip + '" href="' + href + '">' + text + '</a><div></li>';
        mainmenuArray[mainmenuArrayCount] = text;
        mainmenuArrayCount += 1;
    } else {
        menuitem = '<li ' + (id != null ? 'id="' + id + '"' : '') + (zindex != null ? 'style="z-index: ' + zindex + ';"' : '') + '><a target="' + target + '" title="' + tooltip + '" href="' + href + '">' + text + '</a></li>';
        if (document.getElementById('ul' + parentid) == null) {
            var el = document.getElementById(parentid);
            if (el.parentElement.className == 'button' || el.parentElement.className == 'link') {
                el.innerHTML += '<div class="dropdown"><ul id="ul' + parentid + '"></ul></div>';
            } else {
                el.innerHTML += '<div class="mini-zone"></div><div><ul id="ul' + parentid + '"></ul></div>';
            }
        }
    }
    var ul = document.getElementById('ul' + parentid);
    ul.innerHTML += menuitem;
}
function addmenuitemv3(parentid, text, id, tooltip, href, target, className, zindex) {
    addmenuitem(parentid, text, id, tooltip, href, target, className, zindex);
    return;
    var menuitem = '';
    //"../owner/'+sSkinPath+'/basket/a.gif"
    if (parentid == 'mainmenu') {
        menuitem = '<li  class="' + className + '" ' + (id != null ? 'id="li-' + id + '"' : '') + (zindex != null ? 'style="z-index: ' + zindex + ';"' : '') + '><a target="' + target + '" title="' + tooltip + '" href="' + href + '">' +
              '<span class="' + className + 'text" >' + text + '</span> <img src="/owner/' + 'skin/images/blank.gif" class="' + className
              + 'mask" border=0  /><img src="/owner/' + 'skin/images/blank.gif" class="' + className + 'clicklayer" border=0  /></a></li>';
        mainmenuArray[mainmenuArrayCount] = text;
        mainmenuArrayCount += 1;
    } else {
        menuitem = '<li ' + (id != null ? 'id="' + id + '"' : '') + (zindex != null ? 'style="z-index: ' + zindex + ';"' : '') + '><a target="' + target + '" title="' + tooltip + '" href="' + href + '">' + text + '</a></li>';
        if (document.getElementById('ul' + parentid) == null) {
            if (document.getElementById(parentid).parentElement.className == 'button') {
                document.getElementById(parentid).innerHTML += '<div class="dropdown"><ul id="ul' + parentid + '"></ul></div>';
            } else {
                document.getElementById(parentid).innerHTML += '<div class="mini-zone"></div><div><ul id="ul' + parentid + '"></ul></div>';
            }
        }
    }
    var ul = document.getElementById('ul' + parentid);
    ul.innerHTML += menuitem;
}





function clearmenu(id) {
    var ul = document.getElementById('ul' + id);
    ul.innerHTML = '';
}


function openHelp() {
    var page = '';
    try {
        page = top.InfoFrame.document.title;
    } catch (e) {
    }
    fnOpenDialog("main/help.aspx?page=" + page, 600, 400, 1, 1);
}

function openFAQ() {
    var page = 'Navigation';
    fnOpenDialog("main/help.aspx?page=" + page, 600, 400, 1, 1);
}

function QuickSearch() {
    top.InfoFrame.location.href = 'catalog/productslist.aspx?txtSearch=' + encodeURI(document.getElementById('SearchText').value.replace(/\+/g, "%2b"));
}

function QuickSearchCrossRef() {
    var val = document.getElementById('SearchText');
    if (val == null) { val = top.InfoFrame.document.getElementById('SearchText'); }
    var x1 = val.value.replace(/\+/g, "%2b");

    var val2 = document.getElementById('SearchText2');
    if (val2 == null) val2 = top.InfoFrame.document.getElementById('SearchText2');
    if (val2 == null) val2 = val; 
    x2 = val2.value.replace(/\+/g, "%2b");
    var x = '';
    if (top.HideLeftPanel == 0) { x = x1; } else { x = x2; }
    
    top.InfoFrame.location.href = 'catalog/GUDCrossRefSearch.aspx?txtSearch=' + x;
    document.getElementById('SearchText').value = '';
}
function url(u) {
    if (u.indexOf('#') > -1) {
        u = u.substring(0, u.indexOf('#'));
    }
    return u;
}
function AccountSet(Name, Code, refreshScreen) {
    showprocessing();
    top.document.getElementById("account").style['display'] = 'block';
    top.document.getElementById("loginform").style['display'] = 'none';
    top.setText(document.getElementById("accountName"), Name);
    top.setText(document.getElementById("accountCode"), Code);
    try {
        top.uploadNewCat = 0;
        top.aLink = new Array();
        top.aMenuItem = new Array();
        neotekscript('/catalog/menu/menuajax.aspx');
        b.loadFromDB();

        resulthref = top.InfoFrame.location.href;
        if (resulthref.indexOf('?') > -1) {
            resulthref += '&';
        } else {
            resulthref += '?';
        }

        var Mainurl = top.InfoFrame.location.pathname.toLowerCase();
        if (Mainurl.indexOf('/basket/checkout') > -1) { // if at checkout, cancel checkout (copied from basket2.js)
            b.cancelCheckout();
        } else if (refreshScreen == null || refreshScreen) { // Not done when logged out
            top.InfoFrame.location.replace(resulthref + 'refresh=1');
        }

        // 'Breakout' function.
        if (typeof Points == 'object')
            Points.Get();
    } catch (e) {
    }
}

function setLoginAccount(canSelect, username, Name, Code) {
    showprocessing();
    top.document.getElementById("btnPickAccount").style['display'] = canSelect == 1 ? 'block' : 'none';
    top.document.getElementById("account").style['display'] = 'block';
    top.document.getElementById("loginform").style['display'] = 'none';
    top.setText(top.document.getElementById("username"), username);
    top.setText(top.document.getElementById("accountName"), Name);
    top.setText(top.document.getElementById("accountCode"), Code);
    // Added Below
    top.aLink = new Array();
    top.aMenuItem = new Array();
    top.neotekscript('/catalog/menu/menuajax.aspx');

    // 'Breakout' function.
    if (typeof Points == 'object')
        Points.Get();
}

function RRPOnly(bool) {
    neotekscript('/Main/SetRRPOnly.aspx?RRPOnly=' + bool);
    //neotekscript('/catalog/menu/menuajax.aspx');
    b.loadFromDB();

    resulthref = top.InfoFrame.location.href;
    if (resulthref.indexOf('checkout.aspx') > -1) {
        top.InfoFrame.location.replace(url(resulthref) + '?cancel=1');
    } else {
        if (top.InfoFrame.location.href.toLowerCase().indexOf('productslist.aspx') > -1 && top.InfoFrame.location.href.toLowerCase().indexOf('ref=') == -1) {
            QuickSearch();
        } else {
            top.InfoFrame.location.replace(url(resulthref));
        }
    }
}


var selectedAcTab;

function actabclick(li) {
    try {
        if (li === selectedAcTab) return; // nothing to do.
        if (selectedAcTab != null) selectedAcTab.className = '';
        li.className = 'select';
        selectedAcTab = li;
        if (li.id == 'tabAll') {
            findAccount('');
        } else {
            findAccount(li.innerHTML);
        }
    } catch (e) {
        alert(e.message);
    }
}

function ListAccounts(xmlHttp) {
    if (xmlHttp.readyState == 4) {
        if (xmlHttp.status == 200) {
            control = document.getElementById('listAccount');
            control.innerHTML = '';
            loadXMLOptions(xmlHttp, control, 'AccountList');
        } 
    }
}

function findAccount(str) {
    if (str==null){
        var str = $('TxtAccountSearch').value;
    }
    var url = '/Ajax/ChangeAccount.aspx?Find=' + str;
    neotekscript(url, null, ListAccounts);
}
function changeAccount(selectObj) {
    if (selectObj == null) {
        var selectObj = $('listAccount');
    }
    try {
        var url = '/Ajax/ChangeAccount.aspx?Id=' + selectObj.value;
        neotekscript(url, null);
        top.NeotekPopup.Close();
    } catch (e) {
    }
}

function accountSelect() {
    var settings = {
        Title: '',
        URL: '/main/AccountSelect.htm',
        IncludeClose: true,
        AutoResize: true,
        AllowDrag: false,
        BackgroundColor: 'Transparent'
    };
    top.NeotekPopup.Show(settings);
}

function fnOpenDialog(url, iWidth, iHeight, scrollbars, resizable) {
    // Commented out because firefox does not recognise the event.
    // Commenting out resulted in no noticeable side effects RN 20070112.
    //event.returnValue = false;
    if (scrollbars == null) scrollbars = 'no';
    if (resizable == null) resizable = 'no';
    var sOpts = "menubar=no,status=no,toolbar=no,scrollbars=" + scrollbars + ",resizable=" + resizable;
    sOpts += ",height=" + String(iHeight) + ",width=" + String(iWidth);
    if ("object" == typeof (window.screen)) {
        var iTop = window.screen.height / 2 - iHeight / 2;
        var iLeft = window.screen.width / 2 - iWidth / 2;
        sOpts += ",left=" + String(iLeft) + ",top=" + String(iTop);
    }

    oDialog = parent.open(url, "_dialog", sOpts);

    window.onblur = fnCloseWindow;
    return false;
}

function fnCloseWindow() {
    try {
        oDialog.close();
    } catch (e) { }
}
function SearchModeRefresh() {
    try {
        top.content.results.document.clear();
    } catch (e) { }
    try {
        top.content.menu.menu.document.clear();
    } catch (e) { }
    try {
        top.content.menu.menu.location.replace(top.content.menu.menu.location.href);
    } catch (e) { }
    try {
        if (top.content.results.location.href.indexOf('checkout.aspx') > -1) {
            top.content.results.location.replace(top.content.results.location.href + '?cancel=1');
        } else {
            top.content.results.location.replace(top.content.results.location.href);
        }
    } catch (e) { }
}

var loginregex = '\\w+';

function LoginIsValid(value, regex) {
    if (value.length == 0) {
        return true;
    }
    var rx = new RegExp(regex);
    var matches = rx.exec(value);
    return (matches != null && value == matches[0]);
}

function Login() {

    var loginId = document.getElementById('login').value;
    var password = document.getElementById('password').value;
    var rememberPassword = document.getElementById('remember').checked;
    var errorString = "";

    if (loginId == "") {
        errorString += "* Login cannot be blank!\n";
    }
    else if (LoginIsValid(loginId, top.loginregex) == false) {
        errorString += "* Login cannot contain non-alphanumeric characters!\n";
    }

    if (password == "") {
        errorString += "* Password cannot be blank!\n";
    }

    if (errorString != "") {
        errorString = "Your input generated the following error(s):\n\n" + errorString +
                      "\nPlease try again.";
        alert(errorString);

        return;
    }

    showprocessing();
    var urlString = '/main/CustomerLogin.aspx?ajax=1&LoginId=' + loginId + '&Password=' +
             password + '&Remember=' + rememberPassword;

    if (top.b.checkedOut == 1) {
        urlString += '&From=toolbarCheckedOut';
    }

    neotekscript(urlString);

}
function Logout() {
    showprocessing();
    neotekscript('/main/CustomerLogin.aspx?ajax=1&Logout=1');
}

function Register() {
    //top.frames['InfoFrame'].document.location.href='/public/register.aspx';
    top.document.location.href = '/public/register.aspx';
}
function showloginform() {
    AccountSet('', '', false);
    document.getElementById("account").style['display'] = 'none';
    document.getElementById("loginform").style['display'] = 'block';
    neotekscript('/main/menuajax.aspx?mainmenuVer=' + mainmenuVer);
}

function reset(exit) {
    if (exit != null) {
        top.document.location.replace(top.document.location.href);
    } else {
        top.InfoFrame.location.href = '/contentpage.aspx';
        showloginform();
    }
}

function loginError(msg) {
    alert(msg);
}

function loginAccepted(cancelCheckout) {

    document.getElementById('login').value = '';
    document.getElementById('password').value = '';
    document.getElementById('remember').checked = false;
    top.b.loadFromDB();
    neotekscript('/main/menuajax.aspx');

    if (cancelCheckout) {
        var host = top.document.location.host;
        top.frames["InfoFrame"].location.replace('http://' + host + '/basket/checkoutcancel.aspx');
    }
}

var hideobject;
var hideobjectowner;
function ElementVisible(owner, visible, Id) {
    hideobject = null;
    var toggle = document.getElementById(Id);
    if (visible == true){
        if (owner.className.indexOf(' Highlight') == -1) {
            owner.className += ' Highlight';
        }
        toggle.style.display = 'block';
    } else {
    hideobject = toggle;
    hideobjectowner = owner;
    setTimeout('delayedhide();', 50);
    }
}

function delayedhide() {
    if (hideobject != null) {
        hideobject.style.display = 'none';
        if (hideobjectowner.className != null) {
            hideobjectowner.className = hideobjectowner.className.replace(' Highlight', '');
        }
    }
}

function profile() {
    try {
        top.InfoFrame.window.location = '/UserAccess/UserProfile.aspx';
    } catch (ex) { alert(ex.message); }
}


