// $Id: common.js,v 1.7 2009/09/11 12:52:20 RogerSeguin Exp $
/*
  Copyright (c) 2006 Roger Seguin <roger_seguin@msn.com, http://rmlx.dyndns.org>
 
  Redistribution and use, with or without modification, are permitted
  provided that the following conditions are met:
 
  1. Redistributions of source code must retain the above copyright
     notice, this list of conditions and the following disclaimer.
  2. Redistributions in binary form must reproduce the above copyright
     notice, this list of conditions and the following disclaimer in the
     documentation and/or other materials provided with the distribution.
 
  THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
  IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
  INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
  THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/


	/****	<<Miscellaneous	****/

function WriteEmail (preStr, postStr)
	// Roger Seguin, 2006
{
    var             email = '';
    email += "wwwrmlx";
    email += "@";
    email += "gmail";
    email += ".";
    email += "com";
    var             mailto = '"mailto:' + email + '"';
    if (preStr)
	document.write (preStr);
    document.write ('<' + 'a href=' + mailto + '>' + email + '</' + 'a>');
    if (postStr)
	document.write (postStr);
    return (0);
}


function protoWritePageName ()
	// Roger Seguin, 2007
{
    if (!(parent.frames.length))
	return (0);
    var             HTTP = "http://";
    var             ThisPage = document.location.href;
    var             shortname =
	ThisPage.substr (HTTP.length, ThisPage.length - HTTP.length);
    var             pos = shortname.indexOf ("/");
    shortname = shortname.substr (pos, shortname.length - pos);

    pos = shortname.lastIndexOf ("/") + 1;
    var             dirname = shortname.substr (0, pos);
    var             basename =
	shortname.substr (pos, shortname.length - pos);

    if (basename.substr (0, basename.lastIndexOf (".")) == "index") {
	shortname = dirname;
	ThisPage = ThisPage.substr (0, ThisPage.lastIndexOf ("/") + 1);
    }

    document.write ('<a href="' + ThisPage +
		    '" target="_blank" title="Open this page in frameless window">'
		    + shortname + '</a>');
    return (0);
}

function WritePageName ()
	// Roger Seguin, 2006
{
    if (!(parent.frames.length))
	return (0);
    var             HTTP = "http://";
    var             ThisPage = document.location.href;
    var             shortname =
	ThisPage.substr (HTTP.length, ThisPage.length - HTTP.length);
    var             pos = shortname.indexOf ("/");
    shortname = shortname.substr (pos, shortname.length - pos);
    document.write ('<a href="' + ThisPage +
		    '" target="_blank" title="Open this page in frameless window">'
		    + shortname + '</a>');
    return (0);
}


function ObjectPosition (obj)
	// Roger Seguin, 2006
{
    var             x = obj.offsetLeft, y = obj.offsetTop;
    while ((obj = obj.offsetParent)) {
	x += obj.offsetLeft;
	y += obj.offsetTop;
    }
    return[x, y];
}


function MousePosition (evt)
	// Roger Seguin, 2006
{
    var             x = y = 0;
    if (evt) {
	if (evt.pageX || evt.pageY) {
	    x = evt.pageX;
	    y = evt.pageY;
	}
	else if (evt.clientX || evt.clientY) {	// for MSIE
	    x = evt.clientX + document.body.scrollLeft
		+ document.documentElement.scrollLeft;
	    y = evt.clientY + document.body.scrollTop
		+ document.documentElement.scrollTop;
	}
    }
    return[x, y];
}

	/****	Miscellaneous>>	****/

	/****	<<Tooltips	****/

var             m_TooltipTimerId = 0;
var             m_TooltipId = 0;

function _privateShowTooltip (TooltipId, aiCoord, Xoffset, Yoffset)
	// Roger Seguin, 2006
{
    var             tooltip = document.getElementById (TooltipId);
    var             X = 0, Y = 1;

    if (m_TooltipTimerId && (TooltipId == m_TooltipId)) {
	clearTimeout (m_TooltipTimerId);
	m_TooltipTimerId = 0;
	tooltip.style.visibility = 'hidden';
    }
    x = aiCoord[X] + Xoffset;
    y = aiCoord[Y] + Yoffset;
    tooltip.style.left = x + 'px';
    tooltip.style.top = y + 'px';
    tooltip.style.visibility = 'visible';
    m_TooltipId = TooltipId;
    return (0);
}

function ShowTooltip (TooltipId, Obj)
	// Roger Seguin, 2006
{
    var             aiCoord = ObjectPosition (Obj);
    var             Xoffset = 40, Yoffset = 15;
    return (_privateShowTooltip (TooltipId, aiCoord, Xoffset, Yoffset));
}

function ShowTooltipByMousePos (TooltipId, Evt)
	// Roger Seguin, 2006
{
    var             aiCoord = MousePosition (Evt);
    var             Xoffset = 20, Yoffset = -15;
    return (_privateShowTooltip (TooltipId, aiCoord, Xoffset, Yoffset));
}

function HideTooltip (TooltipId)
	// Roger Seguin, 2006
{
    var             tooltip = document.getElementById (TooltipId);
    tooltip.style.visibility = 'hidden';
    if (TooltipId == m_TooltipId)
	m_TooltipTimerId = 0;
    return (0);
}

function DelayHideTooltip (TooltipId, Delay)
	// Roger Seguin, 2006
{
    if (Delay)
	m_TooltipTimerId =
	    setTimeout ('HideTooltip ("' + TooltipId + '")', Delay);
    else
	HideTooltip (TooltipId);
    return (0);
}

	/****	Tooltips>>	****/
