// Tools for javascript

// Test if a variable is null or undefined
function fTools_IsValid(aiVariable)
{
	if (typeof(aiVariable) == "undefined" || aiVariable == null)
	{
		return false;
	}
	
	return true;
}

// Test if a variable is valid and of the given type
function fTools_IsValidType(aiVariable, aisType)
{
	if (!fTools_IsValid(aiVariable))
	{
		return false;
	}
	
	if (typeof(aiVariable) != "" + aisType)
	{
		return false;
	}
	
	return true;
}

// Test if a variable is a valid integer
function fTools_IsValidInteger(aiVariable)
{
	return fTools_IsValidType(aiVariable, "number") && !isNaN(aiVariable);
}

// Test if a variable is a valid string
function fTools_IsValidString(aiVariable)
{
	return fTools_IsValidType(aiVariable, "string");
}

// Test if a variable is a valid boolean
function fTools_IsValidBoolean(aiVariable)
{
	return fTools_IsValidType(aiVariable, "boolean");
}

// Test if a variable is a valid object
function fTools_IsValidObject(aiVariable)
{
	return fTools_IsValidType(aiVariable, "object");
}

// Force the reload of a window
function fTools_ForceReload(aioFrame, aisLocation)
{
	// To force the reload on client side, we add a parameter to the URL which is different everytime
	var loDate = new Date();	
	var lsParameter = "bForceReload=" + loDate.getTime();	
	if (aisLocation.indexOf("?") != -1)
	{
		// Add the new parameter to others
		aioFrame.location = aisLocation + "&" + lsParameter;
	}
	else
	{
		// Add the new parameter to the URL
		aioFrame.location = aisLocation + "?" + lsParameter;
	}
}

function fTools_Max(aiA,aiB)
{
    return aiA>aiB?aiA:aiB;
}

// in order not to display the tooltip when the mouse is out of the link
var gTools_timerID = 0;

function fTools_showTip(current,e,text) {
	document.getElementById("tooltip").innerHTML='<div style="border:1px solid black; padding-left: 2pt;">'+text+'</div>';

	var taille = 5*text.length+10;
	
	var x = (navigator.appName.substring(0,3) == "Net") ? e.pageX+15-(taille/2) + "px" : event.clientX+document.body.scrollLeft-(taille/2);
	var y = (navigator.appName.substring(0,3) == "Net") ? e.pageY+15 + "px" : event.clientY+document.body.scrollTop+10;

	document.getElementById("tooltip").style.left = x;
	document.getElementById("tooltip").style.top  = y;
	if (taille < 300)
		document.getElementById("tooltip").style.width = 5*text.length+15 + "px";
	else
		document.getElementById("tooltip").style.width = 300 + "px";

	gTools_timerID = setTimeout("document.getElementById('tooltip').style.visibility='visible'", 500);
}

function fTools_hideTip() {
    if (gTools_timerID) clearTimeout(gTools_timerID);
    document.getElementById("tooltip").style.visibility="hidden";
}
