﻿var isIE = document.all?true:false;

// AJAX function to update dynamic image
function updateImageContainer(value) 
{
    var behavior = $find('dynImgContainer');

    if (behavior) 
    {
        behavior.populate('/GCU/ImageHandler.ashx?path=' + value + '&width=350&height=250');
    }
}


function OpenCalendar(txtBoxId, txtDateSel, strCalendarPath) 
{
    OpenWin(strCalendarPath + "?id=" + txtBoxId + "&date=" + txtDateSel, "Calendar", 200, 200);
}


function OpenWin(url, name, w, h)
{
  // Fudge factors for window decoration space.
  // In my tests these work well on all platforms & browsers.
  w += 32;
  h += 96;
  wleft = (screen.width - w) / 2;
  wtop = (screen.height - h) / 2;
 

  // IE5 and other old browsers might allow a window that is
  // partially offscreen or wider than the screen. Fix that.
  // (Newer browsers fix this for us, but let's be thorough.)
  if (wleft < 0) {
    w = screen.width;
    wleft = 0;
  }
  if (wtop < 0) {
    h = screen.height;
    wtop = 0;
  }

  var win = window.open(url,
    name,
    'width=' + w + ', height=' + h + ', ' +
    'left=' + wleft + ', top=' + wtop + ', ' +
    'location=no, menubar=no, ' +
    'status=no, toolbar=no, scrollbars=no, resizable=no');
  

  // Just in case width and height are ignored
  win.resizeTo(w, h);

  // Just in case left and top are ignored
  win.moveTo(wleft, wtop);
  win.focus();
}

function PreventSpam(name, domain) 
{
    return '<a href=\"mailto:' + name + '@' + domain + '\">' + name + '@' + domain + '</a>'; 
}


function getPosition(event, clientCaller) 
{
    var pointer_div = document.getElementById(clientCaller);

    // IE
    if (window.ActiveXObject) 
    {
        pos_x = window.event.offsetX;
        pos_y = window.event.offsetY;
    }
    
    // FireFox
    else 
    {
        var top = 0, left = 0;
        var elm = pointer_div;
        while (elm) 
        {
            left = elm.offsetLeft;
            top = elm.offsetTop;
            elm = elm.offsetParent;
        }

        pos_x = event.pageX - left;
        pos_y = event.pageY - top;
    }

    return {x: pos_x, y: pos_y };
}



/*
* Retrieve the coordinates of the given event relative to the center
* of the widget.
*
* @param event
*   A mouse-related DOM event.
* @param reference
*   A DOM element whose position we want to transform the mouse coordinates to.
* @return
*   A hash containing keys 'x' and 'y'.
*/
function getRelativeCoordinates(ev, reference) 
{
    var x, y;

    evt = ev || window.event;
    var el = evt.target || evt.srcElement;
    
    if (!window.opera && typeof evt.offsetX != 'undefined') 
    {
           
        // Use offset coordinates and find common offsetParent
        var pos = { x: evt.offsetX, y: evt.offsetY };
        
        // Send the coordinates upwards through the offsetParent chain.
        var e = el;
        while (e) 
        {
            e.mouseX = pos.x;
            e.mouseY = pos.y;
            pos.x += e.offsetLeft;
            pos.y += e.offsetTop;
            e = e.offsetParent;
        }
                
        // Look for the coordinates starting from the reference element.
        var e = reference;
        var offset = { x: 0, y: 0 }
        while (e) 
        {
            if (typeof e.mouseX != 'undefined') 
            {
                x = e.mouseX - offset.x;
                y = e.mouseY - offset.y;
                break;
            }
                
            offset.x += e.offsetLeft;
            offset.y += e.offsetTop;
            e = e.offsetParent;
        }

        // Reset stored coordinates
        e = el;
        while (e) 
        {
            e.mouseX = undefined;
            e.mouseY = undefined;
            e = e.offsetParent;
        }
    }
    else 
    {
        // Use absolute coordinates
        var pos = getAbsolutePosition(reference);
        x = evt.pageX  - pos.x;
        y = evt.pageY - pos.y;
    }

    // Subtract distance to middle
    return { x: x, y: y };
}

/*
* Retrieve the absolute coordinates of an element.
*
* @param element
*   A DOM element.
* @return
*   A hash containing keys 'x' and 'y'.
*/
function getAbsolutePosition(element) 
{
  var r = { x: element.offsetLeft, y: element.offsetTop };
  
  if (element.offsetParent) 
  {
    var tmp = getAbsolutePosition(element.offsetParent);
    r.x += tmp.x;
    r.y += tmp.y;
  }
  
  return r;
  
};
