﻿
    //
    //
    //
    function Position(l, t) {
        this.left = l;
        this.top  = t;
        this.toString = function(){
            return "(" + this.left + "," + this.top + ")"
        };
        this.move = function(dl, dt) {
            this.left += dl;
            this.top  += dt;
        };
    }
    
    //
    //
    //
    function Dimensions(w, h) {
        this.width  = w;
        this.height = h;
        this.toString = function(){
            return "(" + this.width + "," + this.height + ")"
        };
    }
    
    //
    //
    //
    function Common() {
    
        this.LoadStatements = new Array();
        this.CurrentFocus = "";
        
        //
        //
        //
        this.AddLoadStatement = function(s) {
            this.LoadStatements[this.LoadStatements.length] = s;
        }        

        //
        //
        //
        this.HandleLoad = function() {
            
            //
            // quit if this function has already been called
            //
            if (arguments.callee.done) return;

            //
            // flag this function so we don't do the same thing twice
            //
            arguments.callee.done = true;

            //
            // Execute statements.
            //
            for (var i = 0; i < this.LoadStatements.length; i++) {
                eval(this.LoadStatements[i]);
            }
            
            //
            //
            //
            this.SetFocusHandlers();
        }
        
        //
        //
        //
        this.ScrollTo = function(id) {
            var elem = this.GetElement(id);
            elem.scrollIntoView();
        }

        //
        //
        //
        this.SetFocusHandlers = function() {
            var form = document.forms[0];
            var handler;
            for (var i = 0; i < form.elements.length; i++){
                handler = form.elements[i].onfocus;
                if (!handler) {
                    form.elements[i].onfocus = function(){Common.CurrentFocus = this.id;}
                } else {
                    form.elements[i].onfocus = function(){Common.CurrentFocus = this.id;handler();}
                }
            }
//        window.onfocus = function(){CurrentFocus = "<<body>>"}    
        }

/*
        //
        //
        //        
        this.SmoothScrollTo = function(top) {
            var scroll_pos = this.ScrollPosition();
//            var scroll_max = this.ScrollMax();
            var new_top = scroll_pos.top + Math.ceil((top - scroll_pos.top)/20);

            if (scroll_pos.top < new_top) {
                if (this.ScrollToPosition(new_top)) {
                    setTimeout(function(){Common.SmoothScrollTo(top);}, 10);
                }
            }
        }
        
        //
        //
        //
        this.ScrollPosition = function() {

            // all except Explorer
            if (self.pageYOffset) 
            {
                return new Position(
                    self.pageXOffset,
                    self.pageYOffset
                );                    
            }
            
        	// Explorer 6 Strict Mode
            if (document.documentElement) {
                return new Position(
                    document.documentElement.scrollLeft,
                    document.documentElement.scrollTop
                );                    
            }
            
            // other Explorers
            if (document.body) {
                return new Position(
                    document.body.scrollLeft,
                    document.body.scrollTop
                );                    
            }
            
            alert("Can't get scroll position!");
            return null;
        }
        
        //
        //
        //
        this.ScrollMax = function() {
            var viewport = this.ViewportDimensions();
            var document = this.DocumentDimensions();
            alert(viewport + ":" + document);
            return new Position(
                document.width - viewport.width,
                document.height - viewport.height
            );
            return new Position(
                window.scrollMaxX,
                window.scrollMaxY
            );
        }
        
        //
        //
        //
        this.ViewportDimensions = function() {

            // all except Explorer
            if (self.innerHeight) 
            {
                return new Dimensions(
                    self.innerWidth,
                    self.innerHeight
                );                    
            }
            
        	// Explorer 6 Strict Mode
            if (document.documentElement) {
                return new Dimensions(
                    document.documentElement.clientWidth,
                    document.documentElement.clientHeight
                );                    
            }
            
            // other Explorers
            if (document.body) {
                return new Dimensions(
                    document.body.clientWidth,
                    document.body.clientHeight
                );                    
            }
            
            alert("Can't get viewport dimensions!");
            return null;
        }

        //
        //
        //
        this.DocumentDimensions = function() {
            var x,y;
            var test1 = document.body.scrollHeight;
            var test2 = document.body.offsetHeight
            if (test1 > test2) // all but Explorer Mac
            {
                return new Dimensions(
                    document.body.scrollWidth,
                    document.body.scrollHeight
                );                    
            }
            else // Explorer Mac;
                 //would also work in Explorer 6 Strict, Mozilla and Safari
            {
                return new Dimensions(
                    document.body.offsetWidth,
                    document.body.offsetHeight
                );                    
            }

//            if (document.body.clientWidth) {
//                return new Dimensions(
 //                   document.body.clientWidth,
   //                 document.body.clientHeight
     //           );                    
       //     }
            
            alert("Can't get document dimensions!");
            return null;
        }
        
        //
        //
        //
        this.ScrollToPosition = function(top) {
            if (!isNaN(parseInt(document.body.scrollTop))) {
                document.body.scrollTop = top;
                return true;
            }            
            if (!isNaN(parseInt(document.documentElement.scrollTop))) {
                document.documentElement.scrollTop = top;
                return true;
            }
            alert("Can't scroll to position!");            
            return false;
        }
*/

        //
        // GetElement() 
        //
        this.GetElement = function(id) {
            if (document.getElementById) {
                return document.getElementById(id);
            }
            if (document.all) {
                return document.all[id];
            }
            alert("Can't get element!");
            return null;
        }
        
        //
        // CancelBubble() : Cancel bubbling for an event.
        //
        this.CancelBubble = function(e) {
            var e = e ? e : window.event;
            e.cancelBubble = true;
            if (e.stopPropagation) {
                e.stopPropagation();
            }
        }
        
        //
        // ClickButton() : Call click() for a button.
        //
        this.ClickButton = function(id) {
            var elem = this.GetElement(id);
            elem.click();
        }
        
        //
        //
        //
        this.ShowElement = function(id) {
            var elem = this.GetElement(id);
            elem.style.visibility = "visible";
        }
        
        //
        //
        //
        this.HideElement = function(id) {
            var elem = this.GetElement(id);
            elem.style.visibility = "hidden";
        }

        //
        //
        //
        this.WindowOffset = function(elem) {
            if (elem.offsetParent) {
                if (
                    elem.offsetParent.tagName == "BODY" ||
                    elem.offsetParent.tagName == "HTML" // IE7 skips body?
                ) {
                    return new Position(elem.offsetLeft, elem.offsetTop);
                } else {
                    var offset = this.WindowOffset(elem.offsetParent);
                    offset.move(elem.offsetLeft, elem.offsetTop);
                    return offset;
                }
            }
            alert("Can't get window offset!");
//            Debug.Object(elem);
            return null;
        }
        
    }
    Common = new Common();
