/* Box Objects ------------------------------------------------*/
function point2d (x,y) {
    this.x = x;
    this.y = y;

    this.addPoint = function (toX, toY) {
        this.x += toX;
        this.y += toY;
    }

    this.subPoint = function (toX, toY) {
        this.x -= toX;
        this.y -= toY;
    }
}

function boxParam2d (x,y,w,h) {
    this.x = x;
    this.y = y;
    this.w = w;
    this.h = h;
}

function box2d () {

    this.x = 0;
    this.y = 0;
    this.w = 0;
    this.h = 0;
    this.box1 = new boxParam2d(0,0,0,0);
    this.box2 = new boxParam2d(0,0,0,0);
    this.step1 = new point2d(0,0);
    this.growW = new Array;
    this.growH = new Array;
    this.gstep = 0;
    this.fscale = 1;
    this.int1;
    this.int2;
    this.wait = 10;
    this.objId = '';
    this.nSteps = 50;
    this.n = 0;
    this.moving = false;
    this.nOpac = 0;
    this.onCompletion = '';
    this.ie5 = (document.getElementById && document.all);

    this.setObjectId = function (id) {
        this.objId = id;
    }

    this.setBox = function (x,y,w,h) {
        this.box1.x = this.x = x;
        this.box1.y = this.y = y;
        this.box1.w = this.w = w;
        this.box1.h = this.h = h;
    }

    this.setOpac = function (nOpac) {
        if (!(nOpac === undefined)) this.nOpac = nOpac;

        var obj = document.getElementById(this.objId);
        if (!obj) return;
        if(this.ie5){
           obj.style.filter="alpha(opacity=" + this.nOpac + ")";
    	   obj.filters.alpha.opacity = this.nOpac;
        } else {
    	    obj.style.MozOpacity = (this.nOpac / 100);
            obj.style.KhtmlOpacity = (this.nOpac / 100);
        }
    }

    this.isShown = function () {
        var obj = document.getElementById(this.objId);
        if (!obj) return;

        return (obj.style.display == 'block');
    }

    this.hideBox = function () {
        var obj = document.getElementById(this.objId);
        if (!obj) return;

        obj.style.display = 'none';
    }

    this.showBox = function () {
        var obj = document.getElementById(this.objId);
        if (!obj) return;

        obj.style.display = 'block';
    }

    this.drawBox = function () {
        var obj = document.getElementById(this.objId);
        if (!obj) return;

        var x = this.x;
        var y = this.y;
        var w = this.w;
        var h = this.h;

        /* obj.innerHTML =  'X:' + x + '<BR />'; */
        /* obj.innerHTML += 'Y:' + y + '<br />'; */
        /* obj.innerHTML += 'W:' + w + '<br />'; */
        /* obj.innerHTML += 'H:' + h + '<br />'; */

        obj.style.left      = x + 'px';
        obj.style.top       = y + 'px';
        obj.style.width     = w + 'px';
        obj.style.height    = h + 'px';
    }

    this.boxStep = function (onCompletion, maxSteps) {
        this.x += this.step1.x;
        this.y += this.step1.y;
        this.w = this.growW[this.n];
        this.h = this.growH[this.n]
        this.n++;
        if (this.n >= maxSteps) {
            clearInterval(this.int1);
            this.n = 0;
            this.moving = false;
            if (!(onCompletion === undefined)) {
                this.runCompletion(onCompletion);
            }
        }
        this.drawBox();
    }

    this.scaleFactor = function (l1, l2, nSteps) {
        if (nSteps === undefined) var nSteps = this.nSteps;
        sf = 1;
        this.fscale = l2 / l1;
        rf = this.fscale - 1.0;
        rf /= (nSteps - 1);
        return rf;
    }

    this.moveBox = function (x,y,w,h,n,onCompletion) {
        var oInstance = this;

        /* Trap extra clicks or move commands before a prior move finishes */
        if (this.moving) return;

        if (this.x == x && this.y == y && this.w == w && this.h == h) return;

        this.box2.x = x;
        this.box2.y = y;
        this.box2.w = w;
        this.box2.h = h;

        if (!(n === undefined)) this.wait = (n / this.nSteps);

        this.step1.x = (this.box2.x - this.x) / this.nSteps;
        this.step1.y = (this.box2.y - this.y) / this.nSteps;

        var g1 = this.scaleFactor (this.w, this.box2.w);
        var g2 = this.scaleFactor (this.h, this.box2.h);

        this.gstep = (g1 <= g2) ? g1 : g2;
        for (var i=0; i<this.nSteps; i++) {
            this.growW[i] = this.w * (1 + (this.gstep * i));
            this.growH[i] = this.h * (1 + (this.gstep * i));
        }

        this.moving = true;
        this.int1 = setInterval(function(){oInstance.boxStep(onCompletion, this.nSteps);},this.wait);
    }

    this.restoreBox = function() {
        if (this.box1.x == this.box2.x) return;
        this.moveBox(this.box1.x, this.box1.y, this.box1.w, this.box1.h);
    }

    this.growBox = function (toW, toH, onCompletion, speed, nSteps) {
        var oInstance = this;

        /* Trap extra clicks or move commands before a prior move finishes */
        if (this.moving) return;

        if (speed === undefined) var speed = 20;
        if (nSteps === undefined) var nSteps = 100;

        this.step1.x = 0;
        this.step1.y = 0;
        this.box1.w = toW;
        this.box1.h = toH;

        var g1 = this.scaleFactor (this.w, toW, nSteps);
        var g2 = this.scaleFactor (this.h, toH, nSteps);

        for (var i=0; i<nSteps; i++) {
            this.growW[i] = this.w * (1 + (g1 * i));
            this.growH[i] = this.h * (1 + (g2 * i));
        }

        this.moving = true;
        this.int1 = setInterval(function(){oInstance.boxStep(onCompletion, nSteps);}, speed);
    }

    this.runCompletion = function (onCompletion) {
        if (onCompletion !== undefined) this.onCompletion = onCompletion;

        if (typeof(this.onCompletion) == 'function') {
            this.onCompletion();
        } else {
            eval(this.onCompletion);
        }
        /* Clear the completion value */
        this.onCompletion = '';
    }

    this.boxFade = function (step, onCompletion) {
        if (onCompletion !== undefined) this.onCompletion = onCompletion;

        this.nOpac += step;
        if (this.nOpac >= 100) {
            clearInterval(this.int2);
            this.nOpac = 100;
            this.runCompletion();
        }
        if (this.nOpac <= 0) {
            clearInterval(this.int2);
            this.nOpac = 0;
            this.runCompletion();
        }
        this.setOpac();
    }

    this.fadeIn = function (speed, nSteps, onCompletion) {
        var oInstance = this;

        if (speed === undefined) speed = 20;
        if (nSteps === undefined) nSteps = 100;
        if (onCompletion !== undefined) this.onCompletion = onCompletion;

        this.nOpac = 0;
        var step = 100 / nSteps;

        this.int2 = setInterval(function() {oInstance.boxFade(step);}, speed);
    }

    this.fadeOut = function (speed, nSteps, onCompletion) {
        var oInstance = this;

        if (speed === undefined) speed = 20;
        if (nSteps === undefined) nSteps = 100;
        if (!(onCompletion === undefined)) this.onCompletion = onCompletion;

        this.nOpac = 100;
        var step = -100 / nSteps;

        this.int2 = setInterval(function() {oInstance.boxFade(step);}, speed);
    }
}

/*--------------------------------------------------------------+
|   Window Manager Functions                                    |
+--------------------------------------------------------------*/
function WindowManager() {

    this.box = new Array;
    this.scr = new Array;
    this.href = new Array;
    this.length = 0;
    this.width = 0;
    this.height = 0;
    this.speed = 20;
    this.nSteps = 25;
    this.top = 0;
    this.left = 5;
    this.init_w = 250;
    this.init_h = 5;

    this.createWindow = function (divId, scr, href) {
        var obj = document.getElementById(divId);
        if (!obj) return;

        obj.style.display = 'none';

        this.box[this.length] = new box2d();
        this.box[this.length].setObjectId(divId);
        this.scr[this.length] = scr;
        this.href[this.length] = href;
        this.length++;
    }

    this.hideWindow = function (hWindow) {
        this.box[hWindow].hideBox();
    }

    this.startScript = function (hWindow) {
        AddScriptElement(this.scr[hWindow], this.href[hWindow]);
    }

    this.drawWindow = function (divId) {
        var oInstance = this;
        if (divId === undefined || divId == '') return;
        for (var i=0; i<this.length; i++) {
            if (divId == this.box[i].objId) break;
        }
        if (i == this.length) {
            window.alert("Window Manager Error!  Unable to draw window '" + divId + "'");
            return;
        }

        with (this.box[i]) {
            setBox(this.left,this.top,this.init_w,this.init_h);
            drawBox();
            showBox();
            fadeIn(this.speed, this.nSteps);
            growBox(this.width, this.height, function(){AddScriptElement(oInstance.scr[i], oInstance.href[i]);}, this.speed, this.nSteps);
        }
    }

    this.windowHide = function (hWindow, divId) {
        this.box[hWindow].hideBox();
        this.hideWindows(hWindow, divId);

        var obj = document.getElementById(this.box[hWindow].objId);
        if (!obj) return;
//         while (obj.childNodes[0]) {
//             obj.removeChild(obj.childNodes[0]);
//         }
    }

    this.hideWindows = function (hWindow, divId) {
        var oInstance = this;
        var hNextWindow = hWindow + 1;

        /* Stop recursive processing when we reach end of window list */
        if (hWindow >= this.length) {
            this.drawWindow (divId);
            return;
        }

        /* Hide this window if it is shown */
        if (this.box[hWindow].isShown() && this.box[hWindow].objId != divId) {
            this.box[hWindow].fadeOut(this.speed, this.nSteps, function() {oInstance.windowHide(hWindow,divId);});
            return;
        }
        this.hideWindows(hNextWindow, divId);
        return;
    }

    this.showWindow = function (divId, width, height, speed, nSteps) {
        var oInstance = this;
        if (width === undefined) width = 750;
        if (height === undefined) height = 400;
        if (speed === undefined) speed = 20;
        if (nSteps === undefined) nSteps = 25;

        this.width = width;
        this.height = height;
        this.speed = speed;
        this.nSteps = nSteps;

        this.hideWindows(0, divId);
    }
}

/*--------------------------------------------------------------+
|                                                               |
|   Function to Spoof Email Scanning Robots                     |
|                                                               |
+---------------------------------------------------------------+
|   All email references should call this function.             |
+--------------------------------------------------------------*/

function sendmail (name, host1, host2, text) {
    var tg="<";
    var at="@";
    document.write(tg+'img src="/art/mail_16.gif" class="ma' + 'il" alt="" />');
    document.write(tg+'a hr'+'ef=mai'+'lto:'+name);
    document.write(at+host1+host2+' class="ma' + 'il">'+text+tg+'/a>');
}

/*--------------------------------------------------------------+
|                                                               |
|   Function to Add Scripting Elements during runtime           |
|                                                               |
+--------------------------------------------------------------*/
function AddScriptElement (url, href) {
    var script = document.createElement('script');
    script.src = url;
    document.getElementsByTagName('head')[0].appendChild(script);

    if (href !== undefined) {
        var link = document.createElement('link');
        link.setAttribute('rel','stylesheet');
        link.setAttribute('type','text/css');
        link.setAttribute('href',href);
        document.getElementsByTagName('head')[0].appendChild(link);
    }
}
/*--------------------------------------------------------------+
|                                                               |
|   Functions called as part of the user management system      |
|                                                               |
+--------------------------------------------------------------*/
function RequireLogin(divId) {
    var link = escape('winMgr.showWindow(\'' + divId + '\');');
    AddScriptElement('/sys/usrwarn.php?act1=' + link + '&msgtype=1');
}
/* Display the login popup */
function SignIn() {
    AddScriptElement("/sys/login.php");
}
/* Display the user commands popup */
function UsrMenu() {
    AddScriptElement("/sys/usrmenu.php");
}
/* Basic popup manipulation commands */
function ClosePopUp (id) {
    var obj = document.getElementById(id);
    if (!obj) return;
    obj.style.display = 'none';
}
function InitPage() {
    AddScriptElement('/sys/initusr.php');
}
