//history.forward();

var isFireFox = navigator.userAgent.indexOf('Firefox') != -1;
var isIE = navigator.userAgent.indexOf('MSIE') != -1;

function upperCase(obj) {
    obj.value = obj.value.toUpperCase();
}

function limitLength(obj, max) {
    var s = obj.value;
    var l = 0;
    var i;
    for (i = 0; i < s.length; i++) {
        var c = s.charCodeAt(i);
        if (c >= 0x80) {
            l += 3;
        } else {
            l++;
        }
        if (l >= max) {
            break;
        }
    }
    if (i < s.length)
        obj.value = s.substr(0, i);
}

function highlightRow(dummyId, color) {
    var dummy = document.getElementById(dummyId);
    var tr = dummy;
    while (tr != undefined && tr.tagName != "TR") {
        tr = tr.parentNode;
    }
    if (tr != undefined) {
        for (var i = 0; i < tr.childNodes.length; i++) {
            var td = tr.childNodes[i];
            if (td.tagName != "TD")
                continue;
            td.style.backgroundColor = color;
        }
    }
}

function enterSubmit(event, link) {
    var evt = window.event ? window.event : event;
    if (evt.keyCode == 13) {
        if (!evt.ctrlKey && !evt.altKey) {
            clickIt(link);
        }
    }
}

function clickIt(link) {
    if (typeof(link) == "string") {
        link = document.getElementById(link);
    }
    if (link != undefined) {
        if (navigator.userAgent.indexOf('Firefox') != -1) {
            var evt = link.ownerDocument.createEvent('MouseEvents');
            evt.initMouseEvent('click', true, true, link.ownerDocument.defaultView, 1, 0, 0, 0, 0, false, false, false, false, 0, null);
            link.dispatchEvent(evt);
        } else {
            link.click();
        }
    }
}

function findPos(obj) { 
    var curleft = 0; 
    var curtop = 0; 
    do {
        curleft += obj.offsetLeft;
	curtop += obj.offsetTop;
    } while (obj = obj.offsetParent);

    return {left:curleft,top:curtop};
}

var activePopup = null;

function setActivePopup(popup) {
    if (activePopup != null && activePopup != popup)
        activePopup.hide();
    activePopup = popup;
}

function f_filterResults(n_win, n_docel, n_body) {
	var n_result = n_win ? n_win : 0;
	if (n_docel && (!n_result || (n_result > n_docel)))
		n_result = n_docel;
	return n_body && (!n_result || (n_result > n_body)) ? n_body : n_result;
}
function getScrollLeft() {
	return f_filterResults (
		window.pageXOffset ? window.pageXOffset : 0,
		document.documentElement ? document.documentElement.scrollLeft : 0,
		document.body ? document.body.scrollLeft : 0
	);
}
function getScrollTop() {
	return f_filterResults (
		window.pageYOffset ? window.pageYOffset : 0,
		document.documentElement ? document.documentElement.scrollTop : 0,
		document.body ? document.body.scrollTop : 0
	);
}
function getComponentWidth(c) {
    return c.offsetWidth ? c.offsetWidth : c.clientWidth;
}
function getComponentHeight(c) {
    return c.offsetHeight ? c.offsetHeight : c.clientHeight;
}
function initPopupWindow(inputMask) {
    if (isIE) {
        inputMask.style.filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader(src='../images/inputmask.png', sizingMethod='scale')";
    }
    inputMask.oldOnScroll = window.onscroll;
    inputMask.oldOnResize = window.onresize;
    window.onscroll = function() {
        var inputMask = document.body.inputMask;
        if (inputMask != undefined) {
            if (inputMask.oldOnScroll != undefined)
                inputMask.oldOnScroll();
            inputMask.updatePosition();
        }
    };
    window.onresize = function() {
        var inputMask = document.body.inputMask;
        if (inputMask != undefined) {
            if (inputMask.oldOnResize != undefined)
                inputMask.oldOnResize();
            inputMask.updatePosition();
        }
    };

    inputMask.updatePosition = function() {
        var sLeft = getScrollLeft();
        var sTop = getScrollTop();

        this.style.left = sLeft + "px";
        this.style.top = sTop + "px";

        this.style.width = "100%"; 
        this.style.height = "100%";

        var scnWidth = isIE ? document.documentElement.offsetWidth : document.width ? document.width : this.scrollWidth;
        var scnHeight = isIE ? document.documentElement.offsetHeight : document.height ? document.height : this.scrollHeight;

        if (isIE) {
            this.style.width = scnWidth + "px"; 
            this.style.height = scnHeight + "px";
        }

        if (this.content != undefined && scnWidth && scnHeight) {
            this.content.style.left = (sLeft + (scnWidth - getComponentWidth(this.content)) / 2) + "px";
            this.content.style.top = (100) + "px";//(sTop + (scnHeight - getComponentHeight(this.content)) / 2) + "px";
        }
    };
}

function _isVisible(c) {
    return c.style.display != "none";
}

function _hideIt(c) {
    c.style.display = "none";
}

function _showIt(c) {
    c.style.display = "";
}

function findAndHideComponents(parent, hided) {
    for (var i = 0; i < parent.children.length; i++) {
        var child = parent.children[i];
        if (child.tagName == "SELECT") {
            if (_isVisible(child)) {
                _hideIt(child);
                hided.push(child);
            }
        } else {
            findAndHideComponents(child, hided);
        }
    }
}

function showPopup(panel, vis) {
    var inputMask;
    if (document.body.inputMask == undefined) {
        inputMask = document.body.inputMask = document.createElement("IMG");
        document.body.appendChild(inputMask);

        inputMask.src = "../images/inputmask." + (isIE ? "gif" : "png");
        inputMask.style.position = "absolute";
        inputMask.style.zIndex = 998;
//        inputMask.style.zOrder = 998;

        initPopupWindow(inputMask);
    } else {
        inputMask = document.body.inputMask;
    }

    if (isIE) {
        document.body.hidedComps = new Array();
        findAndHideComponents(document.body, document.body.hidedComps);
    }

    inputMask.content = panel;
    inputMask.vis = vis;

    inputMask.content.visibility = 'hidden';
    _showIt(inputMask.content);
    inputMask.updatePosition();
    if (inputMask.vis != undefined)
        inputMask.vis.value = "left:" + inputMask.content.style.left + "; top:" + inputMask.content.style.top + ";";
    inputMask.content.visibility = 'visible';
    _showIt(inputMask);
}
function hidePopup() {
    var inputMask = document.body.inputMask;
    if (inputMask != undefined) {
        _hideIt(inputMask);
        if (inputMask.content != undefined) {
            _hideIt(inputMask.content);
            inputMask.content = undefined;
            inputMask.vis.value = "";
        }
        
        if (document.body.hidedComps != undefined) {
            for (var i = 0; i < document.body.hidedComps.length; i++) {
                _showIt(document.body.hidedComps[i]);
            }
        }
    }
}
function PopupWindow(panel, vis) {
    this.content = panel;
    this.vis = vis;
    
    this.show = function() {
        if (this.content == undefined)
            alert('Null content');
        else
            showPopup(this.content, this.vis);
    };
    
    this.hide = function() {
        hidePopup();
    };

    var inputMask = document.body.inputMask;
    if (inputMask == undefined) {
        var s = "<IMG id='inputMask' src='../images/inputmask." + (isIE ? "gif" : "png") + "' style='position:absolute; z-index:998;";
        if (this.vis != undefined && this.vis.value != null && this.vis.value != "") {
            var sLeft = getScrollLeft();
            var sTop = getScrollTop();
            s += "width:100%; height:100%; left:" + sLeft + "px; top:" + sTop + "px;";
        } else {
            s += "display:none;"
        }
        s += "'/>";
        document.write(s);
        inputMask = document.body.inputMask = document.getElementById('inputMask');
        initPopupWindow(inputMask); 
    }

    if (isIE) {
        if (document.body._pendingPopups == undefined)
            document.body._pendingPopups = new Array();
        document.body._pendingPopups.push(this);
    } else {
        if (this.vis != undefined && this.vis.value != null && this.vis.value != "") {
            this.show();
        } else {
            _hideIt(this.content);
        }
    }
}

if (isIE && document.body != undefined) {
    document.body._oldOnLoad = document.body.onload;
    document.body.onload = function() {
        if (document.body._pendingPopups != undefined) {
            for (var i = 0; i < document.body._pendingPopups.length; i++) {
                var p = document.body._pendingPopups[i];
                if (p.vis != undefined && p.vis.value != null && p.vis.value != "") {
                    p.show();
                } else {
                    _hideIt(p.content);
                }
            }
        }
        if (document.body._oldOnLoad != undefined)
            document.body._oldOnLoad();
    };
}


var activePopupMenu = undefined;
function PopupMenu(triggerId, menuId) {
    var trigger = document.getElementById(triggerId);
    trigger.menu = this;
    trigger.onmousemove = function() {
        this.menu.show();
    };
    this.trigger = trigger;

    if (menuId != undefined && menuId != null) {
        var menu = document.getElementById(menuId);
        menu.style.position = 'absolute';
        _hideIt(menu);
        this.menu = menu;
    }
    
    this.show = function() {
        if (this.menu == undefined) {
            if (activePopupMenu != undefined)
                activePopupMenu.hide();
            return;
        }
    
        if (_isVisible(this.menu))
            return;
            
        if (activePopupMenu != undefined)
            activePopupMenu.hide();
            
        var pos = findPos(this.trigger);
        
        var x = pos.left;
        var y = pos.top + this.trigger.scrollHeight + 1;
        
//            x += this.trigger.scrollWidth / 2;
//            x -= this.menu.scrollWidth / 2;
        x -= 10;
        if (x < 0)
            x = 0;
        
        this.menu.style.left = x + "px";
        this.menu.style.top = y + "px";
        _showIt(this.menu);
        
        activePopupMenu = this;
    };
    
    this.hide = function() {
        _hideIt(this.menu);
        if (activePopupMenu == this)
            activePopupMenu = undefined;
    };
}

if (document.body != undefined) {
    document.body.onmousemove = function(_evt) {
        if (activePopupMenu == undefined)
            return;
    
        var sLeft = getScrollLeft();
        var sTop = getScrollTop();
        var evt = _evt != undefined ? _evt : window.event;
        var x = sLeft + evt.clientX;
        var y = sTop + evt.clientY;
        
        var pos_t = findPos(activePopupMenu.trigger);
        var pos_m = findPos(activePopupMenu.menu);
        if (y >= pos_t.top && y <= pos_t.top + activePopupMenu.trigger.scrollHeight + 10) {
            return;
        }
        if (x >= pos_m.left && x <= pos_m.left + activePopupMenu.menu.scrollWidth &&
            y >= pos_t.top && y <= pos_m.top + activePopupMenu.menu.scrollHeight) {
            return;
        }
        activePopupMenu.hide();
    };
}

function makeId(refId, id) {
    return refId.substring(0,refId.lastIndexOf(':')+1)+id;
}

function showPopupLazy(id, popupId, visId) {
    var popup = new PopupWindow(document.getElementById(popupId), document.getElementById(visId));
    window[id] = popup;
    popup.show();
}

function trace(o) {
    s = ""
    var i = 0; 
    for (a in o) {
        if (a.toLowerCase().indexOf("partial") >= 0) {
            s += a + "=" + o[a] + "\n";
            i++;
        }
        if (i > 10) {
            if (!confirm(s))
                return;
            i = 0;
            s = "";
        }
    }
    if (i > 0)
        alert(s);
}

//trace(window);
//alert(_doPartialSubmit);
//alert(_findElementById);

