﻿var l365_common_opacity = 0;

function l365_common_getElementLeft(element)
{
    var eTemp, xPos;

    xPos = element.offsetLeft;
    eTemp = element.offsetParent;
    while (eTemp != null)
    {
        xPos += eTemp.offsetLeft;
        eTemp = eTemp.offsetParent;
    }
    return xPos;
}


function l365_common_getElementTop(element)
{
    var eTemp, yPos;

    yPos = element.offsetTop + element.offsetHeight;
    eTemp = element.offsetParent;
    while (eTemp != null)
    {
        yPos += eTemp.offsetTop;
        eTemp = eTemp.offsetParent;
    }
    return yPos;
}

function l365_common_getScrollPosition()
{
    var x, y;
    
    if (document.body.parentElement && (document.body.parentElement.scrollTop || document.body.parentElement.scrollTop === 0))
    {
        x = document.body.parentElement.scrollLeft;
        y = document.body.parentElement.scrollTop;
    }
    else if (window.pageYOffset || window.pageYOffset === 0)
    {
        x = window.pageXOffset;
        y = window.pageYOffset;
    }
    else
    {
        x = document.body.scrollLeft;
        y = document.body.scrollTop;
    }
    
    return {"x":x, "y":y};
}

function l365_common_getWindowDimensions()
{
    var x, y;
    
    // get window size
    if (typeof window.innerWidth != 'undefined')
    {
        x = window.innerWidth,
        y = window.innerHeight
    }
    else if (typeof document.documentElement != 'undefined'
        && typeof document.documentElement.clientWidth !=
        'undefined' && document.documentElement.clientWidth != 0)
    {
        x = document.documentElement.clientWidth,
        y = document.documentElement.clientHeight
    }
    else
    {
        x = document.getElementsByTagName('body')[0].clientWidth,
        y = document.getElementsByTagName('body')[0].clientHeight
    }
 
    return {"x":x, "y":y};
}

function l365_common_getElementDimensions(element)
{
  var changed_display = false;
  var objWidth = 0;
  var objHeight = 0;
  
  if (element.offsetWidth || element.offsetWidth === 0)
  {
    objWidth = element.offsetWidth;
    objHeight = element.offsetHeight;
  }
  else if (document.defaultView.getComputedStyle)
  {
    objHeight = document.defaultView.getComputedStyle(element, "").getPropertyValue("height");
    objWidth = document.defaultView.getComputedStyle(element, "").getPropertyValue("width");
  }
  else if (el.clip)
   {
     objWidth = element.clip.width;
     objHeight = element.clip.height;
   }

  return { "height":objHeight, "width":objWidth };
}

function l365_common_fadeOverlay()
{
    var overlay = document.getElementById("overlay");
    
    // get scroll position
    var scroll = l365_common_getScrollPosition();
    
    overlay.style.height = Math.max(Math.max(document.body.scrollHeight, document.documentElement.scrollHeight),
        Math.max(document.body.offsetHeight, document.documentElement.offsetHeight),
        Math.max(document.body.clientHeight, document.documentElement.clientHeight));

    overlay.style.filter = "alpha(opacity=0)";
    overlay.style.opacity = 0;
    setTimeout("l365_common_doFadeOverlay()", 10);
}

function l365_common_doFadeOverlay()
{
    var overlay = document.getElementById("overlay");
    
    if (l365_common_opacity < 60)
    {
        overlay.style.filter = "alpha(opacity=" + l365_common_opacity + ")";
        overlay.style.opacity = l365_common_opacity/100;
        setTimeout("doFadeOverlay()", 10);
        l365_common_opacity += 5;
    }
    else
    {
        l365_common_opacity = 0;
    }
}

function l365_common_showPop(element, locElement)
{
    var e = document.getElementById(element);
    var eLoc = document.getElementById(locElement);
    
    // position element
    e.style.left = l365_common_getElementLeft(eLoc);
    e.style.top  = l365_common_getElementTop(eLoc);
    
    // display pop element
    e.style.display = "block";
}

function l365_common_showPopCentred(element, focusElement)
{
    var e = document.getElementById(element);
    var viewportwidth;
    var viewportheight;
 
    // display pop element
    e.style.display = "block";
    e.style.position = "absolute";

    // get window and element sizes + scroll pos
    var winSize = l365_common_getWindowDimensions();
    var dim = l365_common_getElementDimensions(e);
    var scroll = l365_common_getScrollPosition();
    
    // position element
    e.style.left = (winSize.x / 2 - (dim.width / 2) + scroll.x) + "px";
    e.style.top  = (winSize.y / 2 - (dim.height / 2) + scroll.y) + "px";
    
    // check if focus element is specified
    if (focusElement != null && document.getElementById(focusElement) != null)
    {
        // set focus to requested control
        document.getElementById(focusElement).focus();
    }
}

function l365_common_hidePop(element)
{
    var e = document.getElementById(element);
    
    // hide pop element
    e.style.display = "none";
}

function l365_common_hideBalloon(element)
{
    var e = document.getElementById(element);
    
    // hide pop element
    e.style.display = "none";
    
    // clear window balloon flag
    window.balloon = null;
}

function l365_cCentreBalloon(balloon)
{
    var overlay = document.createElement("div");
    var e = document.getElementById(balloon);
    
    // get scroll position
    var scroll = l365_common_getScrollPosition();
    
    // set z-orders (overlay 1 behind 
    e.style.zOrder = 100001;
    overlay.style.zOrder = 100000;
    
    // create overlay div
    overlay.id = "balloon_overlay";
    overlay.style.height = Math.max(Math.max(document.body.scrollHeight, document.documentElement.scrollHeight),
        Math.max(document.body.offsetHeight, document.documentElement.offsetHeight),
        Math.max(document.body.clientHeight, document.documentElement.clientHeight));
    document.body.appendChild(overlay);

    // apply overlay style
    overlay.cssClass = "modalBackground";
    
    // get window and element sizes
    var winSize = l365_common_getWindowDimensions();
    var dim = l365_common_getElementDimensions(e);
    
    // position element
    e.style.left = (winSize.x / 2 - (dim.width / 2) + scroll.x) + "px";
    e.style.top  = (winSize.y / 2 - (dim.height / 2) + scroll.y) + "px";
}

function l365_cCloseBalloon(balloon)
{
    var overlay = document.getElementById("balloon_overlay");
    var b = document.getElementById(balloon);
    
    // destroy the overlay div
    document.body.removeChild(overlay);
    
    // hide the popup (by moving hard left - done to avoid GMap bug with display:none map areas)
    b.style.left = -1000;
}

function l365_common_showBalloonBelow(event, balloon, focusElement, defaultElement, move)
{
    var b = document.getElementById(balloon);
    
    // close any existing balloon
    if (window.balloon != null)
        l365_common_hideBalloon(window.balloon.id);

    // display pop element
    b.style.position = "absolute";
    b.style.display = "block";

    // get window and element sizes + scroll pos
    var dim = l365_common_getElementDimensions(b);
    var scroll = l365_common_getScrollPosition();
    
    // position element
    b.style.left = ((event.clientX + scroll.x) - (dim.width - 33)) + "px";
    b.style.top  = ((event.clientY + scroll.y) + 20) + "px";
    
    // save balloon into window object
    window.balloon = b;
    
    // check if focus element is specified
    if (focusElement != null && document.getElementById(focusElement) != null)
    {
        // set focus to requested control
        document.getElementById(focusElement).focus();
    }
    
    // set keydown event handler for Enter to fire a click on the default element
    if (defaultElement != null)
    {
        eDefault = document.getElementById(defaultElement);
        // TODO: finish this off!
    }
}

function l365_common_showBalloonAbove(event, balloon, focusElement, defaultElement, move)
{
    var b = document.getElementById(balloon);
    
    // close any existing balloon
    if (window.balloon != null)
        l365_common_hideBalloon(window.balloon.id);
    
    // display pop element
    b.style.position = "absolute";
    b.style.display = "block";

    // get window and element sizes + scroll pos
    var dim = l365_common_getElementDimensions(b);
    var scroll = l365_common_getScrollPosition();
    
    // position element
    b.style.left = ((event.clientX + scroll.x) - 33) + "px";
    b.style.top  = ((event.clientY + scroll.y) - (dim.height)) + "px";
    
    // save balloon into window object
    window.balloon = b;
    
    // check if focus element is specified
    if (focusElement != null && document.getElementById(focusElement) != null)
    {
        // set focus to requested control
        document.getElementById(focusElement).focus();
    }
    
    // set keydown event handler for Enter to fire a click on the default element
    if (defaultElement != null)
    {
        eDefault = document.getElementById(defaultElement);
        // TODO: finish this off!
    }
}

function l365_common_togglePop(element, locElement)
{
    var e = document.getElementById(element);
    
    if (e.style.display == "none" || e.style.display == "")
        l365_common_showPop(element, locElement);
    else
        l365_common_hidePop(element);
}

function l365_cDuplicateValue(src, dst)
{
    var s, d;
    s = document.getElementById(src);
    d = document.getElementById(dst);
    if (d != null && s != null)
    {
        d.value = s.value;
        if (document.createEventObject)
        {
            // dispatch for IE
            var evt = document.createEventObject();
            return d.fireEvent('onchange',evt)
        }
        else
        {
            // dispatch for firefox + others
            var evt = document.createEvent("HTMLEvents");
            evt.initEvent("onchange", true, true ); // event type,bubbling,cancelable
            return !d.dispatchEvent(evt);
        }
    }
}

function l365_cLimitDescText(e)
{
    var t;
    if (!e) var e = window.event;
    if (e.srcElement) t = e.srcElement;
    else if (e.target) t = e.target;

    key = e.keyCode ? e.keyCode : e.which;

    if (t.value.length >= 1000 && key != 8 && key != 46)
        return false;

    return true;
}

function l365_cCountDescText(e)
{
    var t;
    if (!e) var e = window.event;
    if (e.srcElement) t = e.srcElement;
    else if (e.target) t = e.target;
    document.getElementById("DescCounter").innerText = 1000 - t.value.length;
}
