﻿function addLoadEvent(func) {
    var oldload = window.onload;
    if (typeof window.onload != 'function') {
        window.onload = func;
    }
    else {
        window.onload = function() {
            if (oldload) {
                oldload();
            }
            func();
        }
    }
}
function centerObj(inObj) {
    centerH(inObj);
    centerV(inObj);
}

function centerH(inObj) {
    var lca;
    var lcb;
    var lcx;
    var iebody;
    var dsocleft;
    if (window.innerWidth) {
        lca = window.innerWidth;
    } else {
        lca = document.body.clientWidth;
    }
    lcb = inObj.offsetWidth;
    lcx = (Math.round(lca / 2)) - (Math.round(lcb / 2));
    //alert(lca + " : " + lcb+ " : " + lcx);
    iebody = (document.compatMode && document.compatMode != "BackCompat") ? document.documentElement : document.body;
    dsocleft = document.all ? iebody.scrollLeft : window.pageXOffset;
    inObj.style.left = lcx + dsocleft + "px";

}

function centerV(inObj) {
    var lca;
    var lcb;
    var lcy;
    var iebody;
    var dsoctop;
    if (window.innerHeight) {
        lca = window.innerHeight;
    } else {
        lca = document.body.clientHeight;
    }
    lcb = inObj.offsetHeight;
    lcy = (Math.round(lca / 2)) - (Math.round(lcb / 2));
    iebody = (document.compatMode &&
    document.compatMode != "BackCompat") ?
    document.documentElement : document.body;
    dsoctop = document.all ? iebody.scrollTop : window.pageYOffset;
    inObj.style.top = lcy + dsoctop + "px";

}

function getBrowserSize() {
    var myWidth = 0, myHeight = 0;
    if (typeof (window.innerWidth) == 'number') {
        //Non-IE
        myWidth = window.innerWidth;
        myHeight = window.innerHeight;
    } else if (document.documentElement && (document.documentElement.clientWidth || document.documentElement.clientHeight)) {
        //IE 6+ in 'standards compliant mode'
        myWidth = document.documentElement.clientWidth;
        myHeight = document.documentElement.clientHeight;
    } else if (document.body && (document.body.clientWidth || document.body.clientHeight)) {
        //IE 4 compatible
        myWidth = document.body.clientWidth;
        myHeight = document.body.clientHeight;
    }
    return new Array(myWidth,myHeight);
}


//IMageSwap
function swapImgRestore() { //v3.0
    var i, x, a = document.sr;
    for (i = 0; a && i < a.length && (x = a[i]) && x.oSrc; i++) 
        x.src = x.oSrc;
}
function preloadImages() { //v3.0
    var d = document;
    if (d.images) {
        if (!d.p) 
            d.p = new Array();
        var i, j = d.p.length, a = preloadImages.arguments;
         for (i = 0; i < a.length; i++)
            if (a[i].indexOf("#") != 0) {
                d.p[j] = new Image;
                d.p[j++].src = a[i]; }
    }
}

function findObj(n, d) { //v4.01
    var p, i, x; if (!d) d = document; if ((p = n.indexOf("?")) > 0 && parent.frames.length) {
        d = parent.frames[n.substring(p + 1)].document; n = n.substring(0, p);
    }
    if (!(x = d[n]) && d.all) x = d.all[n]; for (i = 0; !x && i < d.forms.length; i++) x = d.forms[i][n];
    for (i = 0; !x && d.layers && i < d.layers.length; i++) x = findObj(n, d.layers[i].document);
    if (!x && d.getElementById) x = d.getElementById(n); return x;
}

function swapImage() { //v3.0
    var i, j = 0, x, a = swapImage.arguments; 
    document.sr = new Array; 
    for (i = 0; i < (a.length - 2); i += 3)
        if ((x = findObj(a[i])) != null) {
            document.sr[j++] = x;
            if (!x.oSrc) 
                x.oSrc = x.src; 
            x.src = a[i + 2]; 
        }
    }

    function onMouseOverPic(id, arg1, arg2, arg3, arg4) {
        var obj = document.getElementById(id);
//        currentButton();
        if (obj && String(obj.src).indexOf("_on.png") == -1) {
//            alert(obj.src);
            swapImage(arg1, arg2, arg3, arg4);
        }
    }

    function ajaxObject(url, callbackFunction) {
        var that = this;
        this.updating = false;
        this.abort = function() {
            if (that.updating) {
                that.updating = false;
                that.AJAX.abort();
                that.AJAX = null;
            }
        }
        this.update = function(passData, postMethod) {
            if (that.updating) { return false; }
            that.AJAX = null;
            if (window.XMLHttpRequest) {
                that.AJAX = new XMLHttpRequest();
                if (that.AJAX.overrideMimeType) {
                    that.AJAX.overrideMimeType('text/xml');
                }                
            } else {
                that.AJAX = new ActiveXObject("Microsoft.XMLHTTP");
            }
            if (that.AJAX == null) {
                return false;
            } else {
                that.AJAX.onreadystatechange = function() {
                    if (that.AJAX.readyState == 4) {
                        that.updating = false;
                        that.callback(that.AJAX.responseText, that.AJAX.status, that.AJAX.responseXML);
                        that.AJAX = null;
                    }
                }
                that.updating = new Date();
                if (/post/i.test(postMethod)) {
                    var uri = urlCall + '?' + that.updating.getTime();
                    that.AJAX.open("POST", uri, true);
                    that.AJAX.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
                    that.AJAX.setRequestHeader("Content-length", 500);                    
                    that.AJAX.send(passData);
                } else {
                    var uri = urlCall + '?' + passData + '&timestamp=' + (that.updating.getTime());
                    that.AJAX.open("GET", uri, true);
                    that.AJAX.send(null);
                }
                return true;
            }
        }
        var urlCall = url;
        this.callback = callbackFunction || function() { };
    }

    function parseXML(txt) {
        var xmlDoc;
        if (window.DOMParser) {
            parser = new DOMParser();
            xmlDoc = parser.parseFromString(txt, "text/xml");
        }
        else // Internet Explorer
        {
            xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
            xmlDoc.async = "false";
            xmlDoc.loadXML(txt);
        }
        return xmlDoc;
    }


    function makeRequest(url, parameters, rq, response) {
        requester = rq;
        http_request = false;
        if (window.XMLHttpRequest) { // Mozilla, Safari,...
            http_request = new XMLHttpRequest();
            if (http_request.overrideMimeType) {
                http_request.overrideMimeType('text/xml');
            }
        }
        else if (window.ActiveXObject) { // IE
            try {
                http_request = new ActiveXObject("Msxml2.XMLHTTP");
            } catch (e) {
                try {
                    http_request = new ActiveXObject("Microsoft.XMLHTTP");
                } catch (e) { }
            }
        }
        if (!http_request) {
            alert('Cannot create XMLHTTP instance');
            return false;
        }
        http_request.onreadystatechange = response;
        http_request.open('GET', url + parameters, true);
        http_request.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
        http_request.setRequestHeader("Content-length", 500);
        http_request.setRequestHeader("Connection", "close");
        http_request.send(null);
    }

    function showContents() {
        if (http_request.readyState == 4) {
            if (http_request.status == 200) {
                var resdoc = http_request.responseText;
                eval(requester + "(resdoc, http_request.readyState, http_request.responseXML);");
            } else {
                      	alert('There was a problem with the request.');
            }
        }
    }    
