/*************************************************************************
based on Dynamic Web Coding at dyn-web.com
*************************************************************************/

function onMsgBoxSubmit()
{
    if (document.getElementById('txtMsgPassword').value != '') {return true;} else {return false;}
}

function msgBox(event, message, action, showPasswordBox, objectID, okButtonTitle, cancelButtonTitle)
{
    var focusElID
    if (showPasswordBox)
    {
        var vPwdFormHTML = message + '<br><div align=right><form id=frmMsgBox class=small action=\"' + action + '\" method=\"post\" onsubmit=\"{return (onMsgBoxSubmit())}\" style=\"margin:0\"><input id=\"hidObjectID\" name=\"hidObjectID\" type=\"hidden\" value=\"' + objectID + '\"><input id=\"txtMsgPassword\" name=\"txtMsgPassword\" type=\"password\"><br><input id=\"btnSubmit\" class=small type=\"submit\" value=\"' + okButtonTitle + '\" style=\"width:75px\"> <input class=small type=\"button\" onclick=\"popDialog.hide()\" value=\"' + cancelButtonTitle + '\" style=\"width:75px\"></form></div>';
        focusElID = 'txtMsgPassword';
    }
    else
    {
        var vPwdFormHTML = '<div align=center>' + message + '<br><form id=frmMsgBox class=small action=\"' + action + '\" method=\"post\" style=\"margin:0\"><input id=\"hidObjectID\" name=\"hidObjectID\" type=\"hidden\" value=\"' + objectID + '\"><br><input id=\"btnSubmit\" class=small type=\"submit\" value=\"' + okButtonTitle + '\" style=\"width:75px\"> <input class=small type=\"button\" onclick=\"popDialog.hide()\" value=\"' + cancelButtonTitle + '\" style=\"width:75px\"></form></div>';
        focusElID = 'btnSubmit';
    }
    
    this.hide();
    this.doDialog(event, vPwdFormHTML, focusElID);
}

function lookupBox(event, Content, callbackFunction, okButtonTitle, cancelButtonTitle, focusElID)
{
    var vPwdFormHTML = '<div align=\"center\">' + Content + '<br>'
    //window.alert(Content);
    vPwdFormHTML += '<input id=\"hidObjectID\" name=\"hidObjectID\" type=\"hidden\">';
    vPwdFormHTML += '<input id=\"hidObjectName\" name=\"hidObjectName\" type=\"hidden\">';
    if (okButtonTitle) vPwdFormHTML += '<input id=\"btnSubmit\" class=small type=\"button\" value=\"' + okButtonTitle + '\" style=\"width:75px\" onclick=\"{popDialog.hide(); callbackFunction();}\">';
    if (cancelButtonTitle) vPwdFormHTML += '<input id=\"btnCancel\" class=small type=\"button\" onclick=\"popDialog.hide()\" value=\"' + cancelButtonTitle + '\" style=\"width:75px\">';
    vPwdFormHTML += '</div>';
    
    if (!focusElID)  focusElID = 'btnCancel';
    
    this.hide();
    this.doDialog(event, vPwdFormHTML, focusElID);
}

function postForm(action, objectID)
{
    var vPwdFormHTML = '<form id=\"frmMsgBox\" action=\"' + action + '\" method=\"post\"><input id=\"hidObjectID\" name=\"hidObjectID\" type=\"hidden\" value=\"' + objectID + '\"></form>';
    
    this.popupContainer = document.getElementById( this.popupContainerID );
    this.setContent(vPwdFormHTML);
    
    document.getElementById("frmMsgBox").submit();
}
        
function clearTimer() {
  if (popTooltip.timerId) { clearTimeout(popTooltip.timerId); popTooltip.timerId = 0; }
}


function tipOutCheck(e) {
  e = dw_event.DOMit(e);
  // is element moused into contained by tooltip?
  var toEl = e.relatedTarget? e.relatedTarget: e.toElement;
  if ( this != toEl && !contained(toEl, this) ) popTooltip.hide();
}



function unHookHover() {
    var tip = document.getElementById? document.getElementById(this.popupContainerID): null;
    if (tip) {
        tip.onmouseover = null; 
        tip.onmouseout = null;
        tip = null;
    }
}

function hideTip(objVarName) {
  if (!this.ready) return;
  
  this.timerId = setTimeout(objVarName + ".hide()", this.hideDelay);
}


function doDialog(e, msg, focusElementID) {
    if (!this.ready) return;

    window.alert(msg);
	this.clearTimer;
	this.show(e, msg, focusElementID);
}


function doTooltip(e, msg) {
  if (!this.ready) return;
  
  this.clearTimer();
  
  var tip = document.getElementById? document.getElementById(this.popupContainerID): null;
  
  if ( tip && tip.onmouseout == null ) {
      tip.onmouseout = this.tipOutCheck;
      tip.onmouseover = this.clearTimer;
  }
  this.show(e, msg, '');
}


function init(popupContainerID) 
{  
    if ( document.createElement && document.body && typeof document.body.appendChild != "undefined" ) {
        if ( !document.getElementById(this.popupContainerID) ) {
            var el = document.createElement("DIV");
            this.popupContainerID = popupContainerID; 
            el.id = this.popupContainerID; document.body.appendChild(el);
        }
        this.ready = true;
        
    }
}

function show(e, msg, focusElementID) 
{
    if (this.timer) { clearTimeout(this.timer);	this.timer = 0; }
    
    this.popupContainer = document.getElementById(this.popupContainerID);
    
    if (this.followMouse) dw_event.add(document, "mousemove", this.trackMouse, true);
    
    this.setContent("");  // for mac ie
    this.setContent(msg);
    
    viewport.getAll();
    this.positionTip(e);
    
    var vShowScript = "toggleVis('" + this.popupContainerID + "', 'visible');";
    
    if (focusElementID != '') vShowScript += "document.getElementById('" + focusElementID + "').focus();";
    
    this.timer = setTimeout(vShowScript , this.showDelay);
}
    
function setContent(msg) 
{
    if ( this.popupContainer && typeof this.popupContainer.innerHTML != "undefined" ) this.popupContainer.innerHTML = msg;
}
    
function positionTip(e) 
{
    if ( this.popupContainer && this.popupContainer.style ) {
        // put e.pageX/Y first! (for Safari)
        var x = e.pageX? e.pageX: e.clientX + viewport.scrollX;
        var y = e.pageY? e.pageY: e.clientY + viewport.scrollY;

        if ( x + this.popupContainer.offsetWidth + this.offX > viewport.width + viewport.scrollX ) {
            x = x - this.popupContainer.offsetWidth - this.offX;
            if ( x < 0 ) x = 0;
        } else x = x + this.offX;
    
        if ( y + this.popupContainer.offsetHeight + this.offY > viewport.height + viewport.scrollY ) {
            y = y - this.popupContainer.offsetHeight - this.offY;
            if ( y < viewport.scrollY ) y = viewport.height + viewport.scrollY - this.popupContainer.offsetHeight;
        } else y = y + this.offY;
        
        this.popupContainer.style.left = x + "px"; this.popupContainer.style.top = y + "px";
    }
}
    
function hide() 
{
    if (this.timer) { clearTimeout(this.timer);	this.timer = 0; }
    this.timer = setTimeout("toggleVis('" + this.popupContainerID + "', 'hidden')", this.hideDelay);
    if (this.followMouse) // release mousemove
        dw_event.remove( document, "mousemove", this.trackMouse, true );
    this.popupContainer = null; 
}

function toggleVis(id, vis)   // to check for el, prevent (rare) error
{
    var el = document.getElementById(id);
    if (el) el.style.visibility = vis;
}
    
function trackMouse(e) 
{
	e = dw_event.DOMit(e);
 	this.positionTip(e);
}
    
function Popup(followMouse, offX, offY, popupContainerID, showDelay, hideDelay)
{
    this.followMouse = followMouse;
    this.offX = offX;
    this.offY = offY;
    this.popupContainerID = popupContainerID;
    this.showDelay = showDelay;
    this.hideDelay = hideDelay;
    
    this.ready = false; 
    this.timer = null; 
    this.popupContainer = null; 
    this.timerId = 0;

  
    this.init = init;
    this.show = show;
    this.setContent = setContent;
    this.positionTip = positionTip;
    this.hide = hide;
    this.toggleVis = toggleVis;
    this.trackMouse = trackMouse;
    this.doDialog = doDialog;
    this.doTooltip = doTooltip;
    this.hideTip = hideTip;
    this.unHookHover = unHookHover;
    this.tipOutCheck = tipOutCheck;
    this.clearTimer = clearTimer;
    this.msgBox = msgBox;
    this.lookupBox = lookupBox;
    this.postForm = postForm;
    
    if ( document.createElement && document.body && typeof document.body.appendChild != "undefined" ) {
        
        if ( !document.getElementById(this.popupContainerID) ) {
            
            var el = document.createElement("DIV");
            el.id = this.popupContainerID; 
            document.body.appendChild(el);
            
        }
        this.ready = true;
    }

    dw_event.add(window, "unload", this.unHookHover, true);
}


var viewport = {
  getWinWidth: function () {
    this.width = 0;
    if (window.innerWidth) this.width = window.innerWidth - 18;
    else if (document.documentElement && document.documentElement.clientWidth) 
  		this.width = document.documentElement.clientWidth;
    else if (document.body && document.body.clientWidth) 
  		this.width = document.body.clientWidth;
  },
  
  getWinHeight: function () {
    this.height = 0;
    if (window.innerHeight) this.height = window.innerHeight - 18;
  	else if (document.documentElement && document.documentElement.clientHeight) 
  		this.height = document.documentElement.clientHeight;
  	else if (document.body && document.body.clientHeight) 
  		this.height = document.body.clientHeight;
  },
  
  getScrollX: function () {
    this.scrollX = 0;
  	if (typeof window.pageXOffset == "number") this.scrollX = window.pageXOffset;
  	else if (document.documentElement && document.documentElement.scrollLeft)
  		this.scrollX = document.documentElement.scrollLeft;
  	else if (document.body && document.body.scrollLeft) 
  		this.scrollX = document.body.scrollLeft; 
  	else if (window.scrollX) this.scrollX = window.scrollX;
  },
  
  getScrollY: function () {
    this.scrollY = 0;    
    if (typeof window.pageYOffset == "number") this.scrollY = window.pageYOffset;
    else if (document.documentElement && document.documentElement.scrollTop)
  		this.scrollY = document.documentElement.scrollTop;
  	else if (document.body && document.body.scrollTop) 
  		this.scrollY = document.body.scrollTop; 
  	else if (window.scrollY) this.scrollY = window.scrollY;
  },
  
  getAll: function () {
    this.getWinWidth(); this.getWinHeight();
    this.getScrollX();  this.getScrollY();
  }
  
}


var dw_event = {
  
  add: function(obj, etype, fp, cap) {
    cap = cap || false;
    if (obj.addEventListener) obj.addEventListener(etype, fp, cap);
    else if (obj.attachEvent) obj.attachEvent("on" + etype, fp);
  }, 

  remove: function(obj, etype, fp, cap) {
    cap = cap || false;
    if (obj.removeEventListener) obj.removeEventListener(etype, fp, cap);
    else if (obj.detachEvent) obj.detachEvent("on" + etype, fp);
  }, 

  DOMit: function(e) { 
    e = e? e: window.event;
    e.tgt = e.srcElement? e.srcElement: e.target;
    
    if (!e.preventDefault) e.preventDefault = function () { return false; }
    if (!e.stopPropagation) e.stopPropagation = function () { if (window.event) window.event.cancelBubble = true; }
        
    return e;
  }
  
}

// returns true of oNode is contained by oCont (container)
function contained(oNode, oCont) {
  if (!oNode) return; // in case alt-tab away while hovering (prevent error)
  while ( oNode = oNode.parentNode ) if ( oNode == oCont ) return true;
  return false;
}
