// JavaScript Document

var WindowsUtil = {

    getElementAbsPosX:function (el)
{
    var dx = 0;
    if (el.offsetParent) {
        dx = el.offsetLeft + 8;
        while (el = el.offsetParent) {
            dx += el.offsetLeft;
        }
    }
    return dx;
},

getElementAbsPosY:function (el)
{
    var dy = 0;
    if (el.offsetParent) {
        dy = el.offsetTop + el.offsetHeight / 2;
        while (el = el.offsetParent) {
            dy += el.offsetTop;
        }
    }
    return dy;
},

GetAbsWindowBottom:function ()
{
    // Compute the bottom of the popup window and the bottom of
    // the browser window, in absolute co-ordinates - different
    // on all browsers but the below should be accurate usually!
 
    var abswindowbottom = 0;
    if (typeof(window.innerHeight) == 'number')
        abswindowbottom = window.innerHeight;
    else if (document.documentElement && document.documentElement.clientHeight)
        abswindowbottom = document.documentElement.clientHeight;
    else if (document.body && document.body.clientHeight)
        abswindowbottom = document.body.clientHeight;
 
    if (typeof(window.pageYOffset) == 'number')
        abswindowbottom = abswindowbottom + window.pageYOffset;
    else if (document.body && document.body.scrollTop)
        abswindowbottom = abswindowbottom + document.body.scrollTop;
    else if (document.documentElement && document.documentElement.scrollTop)
        abswindowbottom = abswindowbottom + document.documentElement.scrollTop;
    return abswindowbottom;
},

GetAbsWindowsRight:function()
{
   var abswindowright = 0;
    if (typeof(window.innerHeight) == 'number')
        abswindowright = window.innerWidth - 30;
    else if (document.documentElement && document.documentElement.clientWidth)
        abswindowright = document.documentElement.clientWidth - 30;
    else if (document.body && document.body.clientWidth)
        abswindowright = document.body.clientWidth - 30;
 
    if (typeof(window.pageXOffset) == 'number')
        abswindowright = abswindowright + window.pageXOffset;
    
    return abswindowright;

},

PopupDiv:function (name, vis,tagid)
{
    var el = name;
    var tag = tagid;
    if (!document.getElementById(el))  // menu object not found
        return;
    if (vis == 0) {  // hide the menu
        document.getElementById(el).style.display = 'none';
        return;
    }

    // Get menuroot position
    var pos = document.getElementById(tag);
    var dx = this.getElementAbsPosX(pos);
    var dy = this.getElementAbsPosY(pos);
    

    // Compare bottom of menu to bottom of window
    var elHeight = document.getElementById(el).style.height.replace(/px.*/, '');
    var abspopupbottom = dy + parseInt(elHeight) + 10;
    var abswindowbottom = this.GetAbsWindowBottom();

    // If menu goes below bottom of window, move it up!
    //alert(abspopupbottom);
    //alert(abswindowbottom);
    if (abspopupbottom > abswindowbottom)
        dy = dy - (abspopupbottom - abswindowbottom);
        
    //Compare right of menu to right of window
    var elWidth = document.getElementById(el).style.width.replace(/px.*/, '');
    var abspopupright = dx + parseInt(elWidth);
    var abswindowright = this.GetAbsWindowsRight();
    
    //If menu overflow right shift it inwards
     if (abspopupright > abswindowright)
     {
        dx = dx - (abspopupright - abswindowright);
     }
    
    // Set final menu position and make it appear
    document.getElementById(el).style.left = dx + 'px';
    document.getElementById(el).style.top = dy + 'px';
    if (vis > 0)
        document.getElementById(el).style.display = 'block';
},
getCookie : function (Name){ 
var re=new RegExp(Name+"=[^;]+", "i"); //construct RE to search for target name/value pair
if (document.cookie.match(re)) //if cookie found
return document.cookie.match(re)[0].split("=")[1] //return its value
return ""
},

setCookie : function (name, value){
document.cookie = name+"="+value //cookie value is domain wide (path=/)
},

newWindow:function (url){
win=window.open(url);
},

replaceBodyCharacters:function(regexp,replaceText){

var body = document.body.innerHTML;
var reg = new RegExp(regexp);
document.body.innerHTML= document.body.innerHTML.replace(regexp,replaceText);

}


}

