var myPC = "";
var myDiv = null;
var res = "";
var lightBoxTimeOut = 1000;
var xpart;
var lbDiv = null;
var lbLocation = "";
var timer1 = null;
var listenerDone = false;
var safetoclose = false;
var thisUrl = document.domain;
if (thisUrl == "") {
    thisUrl = parent.window.location.href
}
if (thisUrl.search("iis") == -1) {
    thisUrl = "http://www.brightminds.co.uk"
} else {
    thisUrl = "http://iis"
}
function mouseX(d) {
    if (d.pageX) {
        return d.pageX
    } else {
        if (d.clientX) {
            return d.clientX + (document.documentElement.scrollLeft ? document.documentElement.scrollLeft : document.body.scrollLeft)
        } else {
            return null
        }
    }
}
function mouseY(d) {
    if (d.pageY) {
        return d.pageY
    } else {
        if (d.clientY) {
            return d.clientY + (document.documentElement.scrollTop ? document.documentElement.scrollTop : document.body.scrollTop)
        } else {
            return null
        }
    }
}
function inLightBox(e) {
    if (document.layers) {
        var i = e.pageX;
        var k = e.pageY;
        var l = document.layers.lbDiv;
        if ((i > l.left) && (i < l.left + l.clip.width) && (k > l.top) && (k < l.top + l.clip.height)) {
            return true
        }
    } else {
        if (document.all) {
            var l = window.event.srcElement;
            while (l.parentElement != null) {
                if (l.id == "lbDiv") {
                    return true
                }
                l = l.parentElement
            }
        } else {
            if (document.getElementById) {
                var l = e.originalTarget;
                while (l.parentNode != null) {
                    if (l.id == "lbDiv") {
                        return true
                    }
                    l = l.parentNode
                }
            }
        }
    }
}
function tryHideLightBox(d) {
    if (safetoclose && !inLightBox(d)) {
        lightBoxClose()
    }
}
function attachListener() {
    if (document.layers) {
        document.captureEvents(Event.MOUSEUP)
    }
    window.oldOMU = document.onmousedown;
    if (window.oldOMU != null) {
        document.onmousedown = new Function("window.oldOMU(); tryHideLightBox();")
    } else {
        document.onmousedown = tryHideLightBox
    }
}
function lightBoxClose() {
    safetolcose = false;
    clearTimeout(timer1);
    lbDiv.style.display = "none";
    if (lbLocation != "") {
        location = lbLocation
    }
    if ((lbLocation == "") && (ajaxRefresh != "")) {
        location = ajaxRefresh
    }
}
function createLightBox() {
    if (lbDiv == null) {
        lbDiv = document.createElement("div");
        lbDiv.setAttribute("id", "lbDiv");
        lbDiv.setAttribute("name", "lbDiv");
        lbDiv.className = "lightBox";
        lbDiv.position = "absolute";
        lbDiv.innerHTML = "<div></div>";
        document.body.appendChild(lbDiv);
        attachListener()
    }
}
function showLightBox(c, i, g) {
    safetoclose = false;
    createLightBox();
    el = lbDiv;
    el.className = "lightBox";
    el.position = "absolute";
    el.innerHTML = '<div id="lightBoxC"><div id="lightBoxCpic"><div id="lbTxtHolder"><div id="lbTxtT">Adding...</div><div id="lbTxt">' + c + '</div><div id="lbTxtB">to your Basket</div><div id="lbTxtS"></div></div></div></div><div id="lightBoxR"></div><div id="lightBoxB"></div>';
    if (g) {
        document.getElementById("lightBoxCpic").style.backgroundImage = "url('http://www.brightminds.co.uk/products/images/150/" + g + ".jpg')"
    }
    x = mouseX(i) - 318;
    y = mouseY(i) - 100;
    w = 320;
    h = 200;
    sw = -1;
    if (self.innerWidth) {
        sw = self.innerWidth
    } else {
        if (document.documentElement && document.documentElement.clientWidth) {
            sw = document.documentElement.clientWidth
        } else {
            if (document.body) {
                sw = document.body.clientWidth
            }
        }
    }
    wmin = (document.documentElement.scrollLeft ? document.documentElement.scrollLeft : 0);
    sw = sw + wmin;
    sh = -1;
    if (self.innerHeight) {
        sh = self.innerHeight
    } else {
        if (document.documentElement && document.documentElement.clientHeight) {
            sh = document.documentElement.clientHeight
        } else {
            if (document.body) {
                sh = document.body.clientHeight
            }
        }
    }
    hmin = (document.documentElement.scrollTop ? document.documentElement.scrollTop : 0);
    sh = sh + hmin;
    if ((sw > -1)) {
        if (x < 0) {
            x = 0
        }
        if ((x + w) > sw - 10) {
            x = sw - w - 10
        }
    }
    if ((sh > -1)) {
        if (y < 0) {
            y = 0
        }
        if ((y + h) > sh - 10) {
            y = sh - h - 10
        }
    }
    if (x < wmin) {
        x = wmin
    }
    if (y < hmin) {
        y = hmin
    }
    el.style.left = x + "px";
    el.style.top = y + "px";
    el.style.display = "block";
    clearTimeout(timer1)
}
function showAjaxErrorBox(c) {
    createLightBox();
    safetolcose = false;
    clearTimeout(timer1);
    lbDiv.style.display = "none";
    el = lbDiv;
    el.className = "ajaxErrorBox";
    el.position = "absolute";
    el.style.display = "block";
    el.innerHTML = '<div id="ajaxErrorBoxC"><div id="ajeb_heading">Please note:</div><div id="ajeb_body"><ul>' + c + '</ul></div><div id="ajeb_footer"><a href="javascript:lightBoxClose();"> Close Message Box</a></div></div>';
    x = 10;
    y = 10;
    w = el.offsetWidth;
    h = el.offsetHeight;
    w = document.getElementById("ajeb_body").offsetWidth + 2;
    el.style.width = w;
    sw = -1;
    if (self.innerWidth) {
        sw = self.innerWidth
    } else {
        if (document.documentElement && document.documentElement.clientWidth) {
            sw = document.documentElement.clientWidth
        } else {
            if (document.body) {
                sw = document.body.clientWidth
            }
        }
    }
    wmin = (document.documentElement.scrollLeft ? document.documentElement.scrollLeft : 0);
    sw = sw + wmin;
    sh = -1;
    if (self.innerHeight) {
        sh = self.innerHeight
    } else {
        if (document.documentElement && document.documentElement.clientHeight) {
            sh = document.documentElement.clientHeight
        } else {
            if (document.body) {
                sh = document.body.clientHeight
            }
        }
    }
    hmin = (document.documentElement.scrollTop ? document.documentElement.scrollTop : 0);
    sh = sh + hmin;
    x = (sw - w) / 2;
    y = (sh - h - 20) / 2;
    if ((sw > -1)) {
        if (x < 0) {
            x = 0
        }
        if ((x + w) > sw - 10) {
            x = sw - w - 10
        }
    }
    if ((sh > -1)) {
        if (y < 0) {
            y = 0
        }
        if ((y + h) > sh - 10) {
            y = sh - h - 10
        }
    }
    if (x < wmin) {
        x = wmin
    }
    if (y < hmin) {
        y = hmin
    }
    el.style.left = x + "px";
    el.style.top = y + "px";
    document.getElementById("ajeb_footer").style.width = w + "px";
    document.getElementById("ajeb_heading").style.width = w + "px";
    document.getElementById("ajaxErrorBoxC").style.width = w - 2 + "px"
}
function buyOneAjax(g, e) {
    var i = thisUrl + "/BUYajax.asp?part=" + g + "&quantity=1";
    xpart = g;
    http.open("GET", i, true);
    pauseBuy(200);
    http.onreadystatechange = function () {
        getBuyRes()
    };
    http.send(null)
}
function buySomeAjaxDD(n, e, k, l) {
    var m = document.getElementById("part1").value;
    buySomeAjax(n, m, k, l)
}
function buySomeAjax(H, U, G, X) {
    if (H == "") {
        a = 1;
        X = "NOPRESEL"
    } else {
        a = document.getElementById("qty" + H).value
    }
    b = parseInt(a);
    if (isNaN(b)) {
        alert("ERROR - quantity entered - " + a + " - is INVALID");
        return false
    }
    var Y = "";
    var Z = new Date();
    var S = thisUrl + "/BUYajax.asp?part=" + U + "&quantity=" + a + "&when=" + Z.getTime();
    var O;
    if (document.getElementById("gift_wrapYes")) {
        O = document.getElementById("gift_wrapYes").checked
    }
    if (O) {
        var T = document.getElementById("gift_msg").value;
        var V = document.getElementById("wrapSelect");
        for (var J = 0; J < V.options.length; J++) {
            if (V.options[J].selected) {
                var W = V.options[J].value
            }
        }
        S = S + "&gift_wrap=" + W + "&gift_msg=" + T
    }
    var L;
    var N;
    var Q;
    var e;
    var i;
    var I;
    var K;
    L = "";
    var P;
    if (document.getElementById("personalisationYes")) {
        N = document.getElementById("personalisationYes").checked
    }
    if (N) {
        var M = document.getElementById("perso_number_count").value;
        var R = document.getElementById("perso_string_count").value;
        L = L + "&pnc=" + M + "&psc=" + R;
        for (P = 1; P <= R; P++) {
            if (document.getElementById("perso_string" + P)) {
                e = document.getElementById("perso_string" + P).value;
                I = document.getElementById("perso_string_option" + P).value;
                L = L + "&ps" + P + "=" + e + "&pso" + P + "=" + I
            }
        }
        for (P = 1; P <= M; P++) {
            if (document.getElementById("perso_number" + P)) {
                i = document.getElementById("perso_number" + P).value;
                K = document.getElementById("perso_number_option" + P).value;
                L = L + "&pn" + P + "=" + i + "&pno" + P + "=" + K
            }
        }
        if (document.getElementById("dDate")) {
            Q = document.getElementById("dDate").value;
            L = L + "&perso_date=" + Q
        }
    }
    S = S + L;
    xpart = U;
    if (X != "NOPRESEL" && (document.getElementById("numPre"))) {
        numPre = parseInt(document.getElementById("numPre").value);
        Y = "&preParts=";
        for (var J = 0; J < numPre; ++J) {
            j = J + 1;
            if (document.getElementById("buyPre0" + j).checked != "") {
                Y = Y + document.getElementById("partPre0" + j).value + "|"
            }
        }
    }
    S = S + Y;
    http.open("GET", S, true);
    lbLocation = "";
    showLightBox(a + " of " + U, G, U);
    pauseBuy(200);
    http.onreadystatechange = function () {
        getBuyRes()
    };
    http.send(null);
    return false
}
function getBuyRes() {
    if (http.readyState == 1) {
        res = "Waiting for response................";
        if (document.getElementById("lbTxtS")) {
            document.getElementById("lbTxtS").innerHTML = res
        }
    }
    if (http.readyState == 4) {
        res = http.responseText;
        if (res.search("#####") > -1) {
            aaa = res.split("#####");
            document.getElementById("ajaxBasket").innerHTML = aaa[0];
            showAjaxErrorBox(aaa[1])
        } else {
            err = "";
            if ((res.substr(0, 2) == "<%") || (res == "")) {
                err = "Y";
                res = ""
            }
            if (err == "Y") {
                if (document.getElementById("lbTxtT")) {
                    document.getElementById("lbTxtT").innerHTML = "Error:"
                }
                if (document.getElementById("lbTxtB")) {
                    document.getElementById("lbTxtB").innerHTML = ""
                }
                if (document.getElementById("lbTxt")) {
                    document.getElementById("lbTxt").innerHTML = "Server did not respond."
                }
                if (document.getElementById("lbTxtS")) {
                    document.getElementById("lbTxtS").innerHTML = "Please try again in a few seconds."
                }
            } else {
                document.getElementById("ajaxBasket").innerHTML = res;
                if (document.getElementById("lbTxtT")) {
                    document.getElementById("lbTxtT").innerHTML = "Added"
                }
                if (document.getElementById("lbTxtS") && document.getElementById("basket_itemcount")) {
                    q = document.getElementById("basket_itemcount").innerHTML;
                    if (q == 1) {
                        q = q + " item"
                    } else {
                        q = q + " items"
                    }
                    document.getElementById("lbTxtS").innerHTML = "Your basket now holds " + q
                }
            }
            clearTimeout(timer1);
            timer1 = window.setTimeout("lightBoxClose()", lightBoxTimeOut);
            safetoclose = true
        }
    }
}
function getBuyAjax() {
    var k;
    try {
        k = new ActiveXObject("Msxml2.XMLHTTP")
    } catch (e) {
        try {
            k = new ActiveXObject("Microsoft.XMLHTTP")
        } catch (i) {
            try {
                k = new XMLHttpRequest()
            } catch (l) {
                k = false
            }
        }
    }
    return k
}
function pauseBuy(f) {
    var g = new Date();
    var i = null;
    do {
        i = new Date()
    } while (i - g < f)
};
