﻿(ajaxTools = function() {
    var bshowgif = true
    var gifpath = "/images/loading.gif";
    var classname = "";
    var targetdiv = "";
    var xmlhttpPost = function(strURL, divID) {
        var xmlHttpReq = false;
        var self = this;
        sformData = getquerystring();
        if (bshowgif) {
            try {
                document.getElementById(divID).innerHTML = '<div style="width:40px;margin:auto auto"><img src="' + gifpath + '"></div>';
            } catch (e) {
                alert("Error Locating DIV for Loading Gif: '" + divID + "'\n" + e);
                return false;
            }
        }
        // Mozilla/Safari
        if (window.XMLHttpRequest) {
            self.xmlHttpReq = new XMLHttpRequest();
        }
        // IE
        else if (window.ActiveXObject) {
            self.xmlHttpReq = new ActiveXObject("Microsoft.XMLHTTP");
        }
        self.xmlHttpReq.open('POST', strURL, true);
        self.xmlHttpReq.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
        self.xmlHttpReq.onreadystatechange = function() {
            if (self.xmlHttpReq.readyState == 4) {
                updatepage(self.xmlHttpReq.responseText, divID);
                href2ajax();
            }
        }
        self.xmlHttpReq.send(sformData);
    }
    var updatepage = function(str, divID) {
        if (document.getElementById(divID)) {
            document.getElementById(divID).innerHTML = str;
            document.getElementById(divID).style.visibility = 'visible';
            document.getElementById(divID).style.display = 'block';
        } else {
            alert(divID + ' Not Found');
        }
    }
    var getquerystring = function() {
        var oform
        var oelement
        var qstr = ''
        var oforms = document.getElementsByTagName('form');
        for (i = 0; i < oforms.length; i++) {
            oform = oforms[i];
            if (oform) {
                for (j = 0; j <= oform.elements.length - 1; j++) {
                    oelement = oform.elements[j];
                    if (oelement.name) {
                        switch (oelement.type) {
                            case 'radio':
                            case 'checkbox':
                                if (oelement.checked) {
                                    if (qstr != '') {
                                        qstr += '&' + oelement.name + '=' + escape(oelement.value);
                                    } else {
                                        qstr = oelement.name + '=' + escape(oelement.value);
                                    }
                                }
                                break;
                            default:
                                if (qstr != '') {
                                    qstr += '&' + oelement.name + '=' + escape(oelement.value);
                                } else {
                                    qstr = oelement.name + '=' + escape(oelement.value);
                                }
                                break;
                        }
                    }
                }
            }
        }
        return qstr;
    }
    var ajaxfunction = function(evt) {
        try {
            if (evt.stopPropagation) { evt.stopPropagation(); }
            evt.cancelBubble = true;
            if (evt.preventDefault) { evt.preventDefault(); }
            evt.returnValue = false;
            if (evt["srcElement"]) {
                var uri = evt["srcElement"]["href"];
                var starget = evt["srcElement"]["target"];
            } else if (evt["target"]) {
                var uri = evt["target"]["href"];
                var starget = evt["target"]["target"];
            }
            if (document.getElementById(starget)) {
                xmlhttpPost(uri, starget);
            } else {
                /*var odiv = document.createElement("div");
                odiv.id = starget;
                document.body.appendChild(odiv);
                xmlhttpPost(uri, starget);*/
                alert(starget + ' Not Found - Unable to change Link');
            }
        } catch (e) {
            alert(e);
        }
    }
    var href2ajax = function() {
        var arylinks = document.getElementsByTagName("a");
        for (i = 0; i < arylinks.length; i++) {
            if (arylinks[i].className == classname && classname != '') {
                if (window.addEventListener) {
                    arylinks[i].removeEventListener("click", ajaxfunction, false)
                    arylinks[i].addEventListener("click", ajaxfunction, false);
                } else {
                    arylinks[i].detachEvent("onclick", ajaxfunction);
                    arylinks[i].attachEvent("onclick", ajaxfunction);
                }
            }
        }
    }
    this.genpopup = function(surl) {
        var oelement;
        var centerx = (screen.availWidth / 2) - 475;
        var centery = 50;
        window.scrollTo(0, 0);
        underlay = document.getElementById('underlay');
        underlay.style.visibility = 'visible';
        underlay.style.display = 'block';
        if (document.getElementById('ProductDetail')) {
            oelement = document.getElementById('ProductDetail');
        } else {
            oelement = document.createElement("div");
            oelement.id = "ProductDetail";
            oelement.className = "dialogbox";
            document.body.appendChild(oelement);
        }
        if (!isIE) {
            //oelement.style.position = 'fixed';
            //underlay.style.position = 'fixed';
        }
        oelement.style.top = centery + 'px';
        oelement.style.left = centerx + 'px';
        xmlhttpPost(surl, oelement.id, true);
    }
    this.closepopup = function() {
        if (document.getElementById('underlay')) {
            underlay = document.getElementById('underlay');
            underlay.style.visiblity = 'hidden';
            underlay.style.display = 'none';
            underlay.style.position = 'absolute';
        }
        document.getElementById('ProductDetail').style.visibility = 'hidden';
        document.getElementById('ProductDetail').style.position = 'absolute';
    }
    this.do_xmlhttpPost = function(surl, divid, showgif) {
        bshowgif = showgif;
        if (!document.getElementById(divid)) {
            /*var odiv = document.createElement("div");
            odiv.id = divid;
            document.body.appendChild(odiv);*/
            alert(divid + ' Not Found');
        }
        xmlhttpPost(surl, divid);
    }
    this.set_gifpath = function(spath) { gifpath = spath; }
    this.get_gifpath = function() { return gifpath; }
    this.set_showgif = function(bval) { bshowgif = bval; }
    this.get_showgif = function() { return bshowgif; }
    this.changeLinks = function(c, t) {
        classname = c;
        targetdiv = t;
        href2ajax();
    }
}
)
function xmlhttpPost(surl, divid, showgif) {
    showgif = showgif ? showgif : true;
    var ajtool = new ajaxTools();
    ajtool.do_xmlhttpPost(surl, divid, showgif)
}
function dofadeOut(sid, intervalID, nextcmd) {
    var e = document.getElementById(sid);
    var currentalpha;
    if (!isIE) {
        currentalpha = e.style.opacity;
        if (currentalpha == '') { currentalpha = 1; }
        if (currentalpha > 0) {
            e.style.opacity = currentalpha - .2
        } else {
            e.style.opacity = 0;
            e.style.visibility = 'hidden';
            clearInterval(eval(intervalID));
            eval(nextcmd);
        }
    } else {
        for (i = 0; i < e.children.length; i++) {
            var ochild = e.children[i];
            if (ochild.style.filter != '') {
                currentalpha = ochild.filters.alpha.opacity;
            } else {
                currentalpha = 100;
            }
            if (currentalpha > 0) {
                ochild.style.filter = 'alpha(opacity=' + (currentalpha - 25) + ')';
            }
        }
        if (currentalpha <= 0) {
            e.style.visibility = 'hidden';
            clearInterval(eval(intervalID));
            eval(nextcmd);
        }
    }
}
function dofadeIn(sid, intervalID) {
    var e = document.getElementById(sid);
    //window.scrollTo(0, 0);
    var currentalpha;
    e.style.visibility = 'visible';
    if (!isIE) {
        currentalpha = e.style.opacity;
        if (currentalpha == '') { currentalpha = 0; }
        if (currentalpha < 1) {
            e.style.opacity = (parseFloat(currentalpha) + 0.2);
        } else {
            e.style.opacity = 1;
            clearInterval(eval(intervalID));
        }
    } else {
        if (e.style.filter != '') {
            currentalpha = e.filters.alpha.opacity;
        } else {
            currentalpha = 0;
        }
        if (currentalpha <= 100) {
            if (currentalpha == 100) {
                e.style.filter = '';
            } else {
                e.style.filter = 'alpha(opacity=' + (currentalpha + 25) + ')';
            }
        }
        if (currentalpha >= 100) {
            clearInterval(eval(intervalID));
        }
    }
}
function fadeOut(divid, nextcmd) {
    fadeOutInterval = setInterval('dofadeOut("' + divid + '","fadeOutInterval","' + nextcmd + '")', 100);
    if (underlay) {
        underlay.style.visibility = 'hidden';
    }
}

function getProducts(catid, target) {
    scmd = "xmlhttpPost('content/productlist.asp?catid=" + catid + "','" + target + "')";
    fadeOut(target, scmd);
}
function returnTo(surl, target) {
    //scmd = "xmlhttpPost('" + surl + "','" + target + "')";
    //fadeOut(target, scmd);
    xmlhttpPost(surl, target, true);
}

function prodDetail(pid) {
    var oelement;
    var centerx = (screen.availWidth / 2) - 300;
    var centery = 50;
    underlay = document.getElementById('underlay');
    underlay.style.visibility = 'visible';
    if (document.getElementById('ProductDetail')) {
        oelement = document.getElementById('ProductDetail');
    } else {
        oelement = document.createElement("div");
        oelement.id = "ProductDetail";
        oelement.className = "dialogbox";
        document.body.appendChild(oelement);
    }
    /*if (!isIE) {
    oelement.style.position = 'fixed';
    underlay.style.position = 'fixed';
    }*/
    oelement.style.top = centery + 'px';
    oelement.style.left = centerx + 'px';
    xmlhttpPost('content/productdetail.asp?pid=' + pid, oelement.id, true);
}

function showInDialog(surl) {
    var oelement;
    var centerx = (screen.availWidth / 2) - 300;
    var centery = 50;
    underlay = document.getElementById('underlay');
    underlay.style.visibility = 'visible';
    if (document.getElementById('ProductDetail')) {
        oelement = document.getElementById('ProductDetail');
    } else {
        oelement = document.createElement("div");
        oelement.id = "ProductDetail";
        oelement.className = "dialogbox";
        document.body.appendChild(oelement);
    }
    if (!isIE) {
        //oelement.style.position = 'fixed';
        //underlay.style.position = 'fixed';
    }
    oelement.style.top = centery + 'px';
    oelement.style.left = centerx + 'px';
    xmlhttpPost(surl, oelement.id, true);
}


