//The offset values to use for the main password field on the page.
var iTopPosMainOffset = 70;
var iLeftPosMainOffset = 0;
//The offset values to use for the secondary password field on the page.
var iLeftPosAuxOffset = 5;
var iTopPosAuxOffset = 19;

var capslock = {
    init: function() {
        if (!document.getElementsByTagName) {
            return;
        }
        // Find all password fields in the page, and set a keypress event on them
        var inps = document.getElementsByTagName("input");
        for (var i = 0, l = inps.length; i < l; i++) {
            if (inps[i].type == "password") {
                capslock.addEvent(inps[i], "keypress", capslock.keypress);
            }
        }
    },
    addEvent: function(obj, evt, fn) {
        if (document.addEventListener) {
            capslock.addEvent = function(obj, evt, fn) {
                obj.addEventListener(evt, fn, false);
            };
            capslock.addEvent(obj, evt, fn);
        } else if (document.attachEvent) {
            capslock.addEvent = function(obj, evt, fn) {
                obj.attachEvent('on' + evt, fn);
            };
            capslock.addEvent(obj, evt, fn);
        } else {
            // no support for addEventListener *or* attachEvent, so quietly exit
        }
    },
    keypress: function(e) {
        var ev = e ? e : window.event;
        if (!ev) {
            return;
        }
        var targ = ev.target ? ev.target : ev.srcElement;
        // get key pressed
        var which = -1;
        if (ev.which) {
            which = ev.which;
        } else if (ev.keyCode) {
            which = ev.keyCode;
        }
        // get shift status
        var shift_status = false;
        if (ev.shiftKey) {
            shift_status = ev.shiftKey;
        } else if (ev.modifiers) {
            shift_status = !!(ev.modifiers & 4);
        }
        if (((which >= 65 && which <= 90) && !shift_status) ||
        ((which >= 97 && which <= 122) && shift_status)) {
            // uppercase, no shift key
            capslock.show_warning(targ);
        } else {
            capslock.hide_warning(targ);
        }
    },

    show_warning: function(targ) {
        if (!targ.warning) {
            targ.warning = document.createElement('img');
			targ.warning.setAttribute("id", "bubble");
            targ.warning.src = host + "/images/caps_lock_warning_tail_btm.png";
            targ.warning.style.position = "absolute";
			
			
            //targ.warning.style.top = (targ.offsetTop - 73) + "px";
            //targ.warning.style.left = (targ.offsetLeft + targ.offsetWidth - 5) + "px";
            
			var iTopPos = (targ.offsetTop + getPosOfElement(targ, "top") - iTopPosMainOffset); 
			
			targ.warning.style.top = (targ.offsetTop + getPosOfElement(targ, "top") - iTopPosMainOffset) + "px";
			targ.warning.style.left = (targ.offsetWidth + getPosOfElement(targ, "left") - iLeftPosMainOffset) + "px";
			
			if (iTopPos < 0) {
				//Use different image as we are too close to the top of the page.
				targ.warning.src = host + "/images/caps_lock_warning_tail_top.png";
				targ.warning.style.top = iTopPosAuxOffset + "px";
				targ.warning.style.left = (targ.offsetWidth + getPosOfElement(targ, "left") - iLeftPosAuxOffset) + "px";
			}
			
			targ.warning.style.zIndex = "999";
            targ.warning.setAttribute("alt", "Warning: Caps Lock is on");
            if (targ.warning.runtimeStyle) {
                // PNG transparency for IE
                targ.warning.runtimeStyle.filter +=
				"progid:DXImageTransform.Microsoft.AlphaImageLoader(src='caps_lock_warning_tail_btm.png',sizingMethod='scale')";
            }
            document.body.appendChild(targ.warning);
        }
    },
    hide_warning: function(targ) {
        if (targ.warning) {
            targ.warning.parentNode.removeChild(targ.warning);
            targ.warning = null;
        }
    }
};

(function(i) {
    var u = navigator.userAgent; var e = /*@cc_on!@*/false; var st =
setTimeout; if (/webkit/i.test(u)) {
        st(function() {
            var dr = document.readyState;
            if (dr == "loaded" || dr == "complete") { i() } else { st(arguments.callee, 10); } 
        }, 10);
    }
    else if ((/mozilla/i.test(u) && !/(compati)/.test(u)) || (/opera/i.test(u))) {
        document.addEventListener("DOMContentLoaded", i, false);
    } else if (e) {
        (
function() {
    var t = document.createElement('doc:rdy'); try {
        t.doScroll('left');
        i(); t = null;
    } catch (e) { st(arguments.callee, 0); } 
})();
    } else { window.onload = i; } 
})(capslock.init);



/*
 * Gets the exact top-left coords of an element on the page.
 *
 * eTheElement - The element that you want to find the position of.
 * strLeftOrTop - Either "left" or "top" depending on which coord you want.
 */
function getPosOfElement(eTheElement, strLeftOrTop) {
    var left = eTheElement.offsetLeft;
    var top  = eTheElement.offsetTop;
    while(eTheElement = eTheElement.offsetParent) {
        left += eTheElement.offsetLeft;
        top  += eTheElement.offsetTop;
    }
	
	if (strLeftOrTop == "left") {
		return left;	
	} else {
		return top;
	}
}

