﻿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="' + 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) {
            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 addmenuitemv3(parentid, text, id, tooltip, href, target, className, zindex) {
    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 about() {
    // New Popup for About - Not fully tested yet
    //    var settings = {
    //        Title : ' ',
    //        URL : '~/public/about.aspx',
    //        IncludeClose : true,
    //        FadeBoxIn : true,
    //        AutoResize : true,
    //        ShowTriangle : false,
    //        BackgroundColor : '#d4d0c8',
    //        Width : 400
    //    };
    //    top.NeotekPopup.Show(settings);

    var w = window.screen.width;
    var h = window.screen.height;
    var l = (w - 400) / 2;
    var t = (h - 260) / 2;
    wAbout = parent.open('/public/about.aspx', '_about', 'fullscreen=0,toolbar=0,location=0,directories=0,status=0,menubar=0,scrollbars=0,resizable=0');
    wAbout.blur();
    window.focus();
    wAbout.resizeTo(400, 260);
    wAbout.moveTo(l, t);
    wAbout.focus();
}

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');
    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 += '?';
        }
        if (refreshScreen == null || refreshScreen) { // Not done when logged out
            top.InfoFrame.location.replace(resulthref + 'refresh=1');
        }
    } 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');
}

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));
        }
    }
}

function accountSelect(ScriptPath) {
    fnOpenDialog(ScriptPath + "AccountSelect.aspx", 580, 305);
}

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');
    }
}

function ElementVisible(owner, visible, Id) {
    var toggle = document.getElementById(Id);
    if (visible == true) {
        owner.className += ' Highlight';
        toggle.style.display = 'block';
    } else {
        toggle.style.display = 'none';
        if (owner.className != null) {
            owner.className = owner.className.replace(' Highlight', '');
        }
    }
}
function profile() {
    try {
        top.InfoFrame.window.location = '/UserAccess/UserProfile.aspx';
    } catch (ex) { alert(ex.message); }
}

