﻿var detect = navigator.userAgent.toLowerCase();
var OS,browser,version,total,thestring;
var ie,ie5,ie6,ns,ns6,ns7;
var ActivPortalRootURL;

var pathElem = window.location.pathname.split('/');
if ( pathElem.length > 2 )
	ActivPortalRootURL = window.location.protocol + '//' + window.location.hostname + '/' + pathElem[1] + '/';
else
	ActivPortalRootURL = window.location.protocol + '//' + window.location.hostname + '/';

if (checkIt('konqueror'))
{
	browser = "Konqueror";
	OS = "Linux";
}
else if (checkIt('omniweb')) browser = "OmniWeb"
else if (checkIt('opera')) browser = "Opera"
else if (checkIt('webtv')) browser = "WebTV";
else if (checkIt('icab')) browser = "iCab"
else if (checkIt('msie')) browser = "Internet Explorer"
else if (!checkIt('compatible'))
{
	browser = "Netscape Navigator"
	var nsversion=detect.split('netscape');
	version=nsversion[1];
}
else browser = "An unknown browser";

if (!version) version = detect.charAt(place + thestring.length);

if (!OS)
{
	if (checkIt('linux')) OS = "Linux";
	else if (checkIt('x11')) OS = "Unix";
	else if (checkIt('mac')) OS = "Mac"
	else if (checkIt('win')) OS = "Windows"
	else OS = "an unknown operating system";
}

ie5=(browser=="Internet Explorer"&&version==5);
ie6=(browser=="Internet Explorer"&&version>=6);
ie=(ie5||ie6);
ns6=(browser=="Netscape Navigator"&&version.indexOf('6')>-1);
ns7=(browser=="Netscape Navigator"&&version.indexOf('7')>-1);
ns=(ns6||ns7);
/*if (ns7) alert('NS 7 !');
if(!ie6) alert('Votre navigateur n\'est pas compatible avec ce site. Veuillez utiliser Microsoft Internet Explorer 6.0 minimum.');*/


/*************************************************************/
function checkIt(string)
{
	place = detect.indexOf(string) + 1;
	thestring = string;
	return place;
}

/*************************************************************/
function fnWindowOpen(url,title,x,y,resize,lift,sLeft,sRight)
{
	var sLeft=null;
	var sTop=null
	var newwindow=null;
	try
	{
		var sXOffset = 20;
		var sYOffset = 50;
		if (screen.width - sXOffset < eval(x)) x = screen.width - sXOffset;
		if (screen.height - sYOffset < eval(y)) y = screen.width - sYOffset;
		if (!sLeft) sLeft=Math.round(((screen.width - sXOffset)/2)-(x/2));
		if (!sRight) sTop=Math.round(((screen.Height - sYOffset)/2)-(y/2));
		newwindow=window.open(url,title,"width="+x+",height="+y+",left="+sLeft+",top="+sTop+",status=no,titlebar=no,toolbar=no,menubar=no,location=no,resizable=" + (resize ? 'yes':'no') + ',scrollbars=' + (lift ? 'yes':'no'));
	}
	catch(e){
		newwindow=window.open(url,title,"width="+x+",height="+y+",status=no,titlebar=no,toolbar=no,menubar=no,location=no,resizable=" + (resize ? 'yes':'no') + ',scrollbars=' + (lift ? 'yes':'no'));	
	}
	
/*
	var sleft=Math.round((screen.width/2)-(x/2));
	var stop=Math.round((screen.height/2)-(y/2));
	window.open("tbcprop.html","TbcProp","top="+stop+",left="+sleft+",height="+pheight+",width="+pwidth+",status=no,toolbar=no,menubar=no,location=no,resizable=yes");
*/
	//var newwindow=window.open(url,title,(x)?"width="+x+",":"" + (y)?"height="+y+",":"" + "status=no,titlebar=no,toolbar=no,menubar=no,location=no,resizable=" + (resize ? 'yes':'no') + ',scrollbars=' + (lift ? 'yes':'no'));
	
	if (newwindow && window.focus) {newwindow.focus()}
	return newwindow;
	

}

/*************************************************************/
//Check Browser
function checkBrowser(test,action)
{
	return;
	if (eval(test)) 
	{
		alert('Warning ! you need Internet Explorer 5+ or Netscape 6 to use this component');
		eval(action);
	}
}


/************************************************************/
function addOptionToSel(select,oOption)
{
	if (ie)	select.add(oOption);
	if (ns)	select.appendChild(oOption);
}

function getElByID(strID)
{
	return document.getElementById(strID)
		
}


//*******************************************************
//	fn_DisplayError
//*******************************************************
function fnDisplayError(Number,Description)
{
	alert(Description);
}


//*******************************************************
//	fn_RefreshOpener
//*******************************************************
function fnRefresh()
{
	document.location.href = document.location.href;
}

//*******************************************************
//	fn_RefreshOpener
//*******************************************************
function fnRefreshOpener()
{
	window.opener.document.location.href = window.opener.document.location.href;
}


//*******************************************************
//	HasWindowOpener
//*******************************************************
function HasWindowOpener()
{
	return ( window.opener != null && window.opener != 'undefined' );
}


//*******************************************************
//	GetAbsoluteTop
//*******************************************************
function GetAbsoluteTop(elem)
{
	var topPosition = 0;

	while (elem)
	{
		if (elem.tagName.toLowerCase() == 'body')
		{
			break;
		}
		topPosition += elem.offsetTop;
		elem = elem.offsetParent;
	}
	return topPosition;
}


//*******************************************************
//	GetAbsoluteLeft
//*******************************************************
function GetAbsoluteLeft(elem)
{
	var leftPosition = 0;
		
	while (elem)
	{
		if (elem.tagName.toLowerCase() == 'body')
		{
			break;
		}
		leftPosition += elem.offsetLeft;
		elem = elem.offsetParent;
	}
	return leftPosition;
}

//*******************************************************
//	GetParentElementToTagName
//*******************************************************
function GetParentElementToTagName(oObj,sTagname)
{
	if( oObj.tagName.toLowerCase() == "body" ) return null;
	if( oObj.tagName.toLowerCase() == sTagname.toLowerCase() ) 
	{
		return oObj;
	}
	return GetParentElementToTagName(oObj.parentNode,sTagname)
}


//*******************************************************
//	URLEncode
//*******************************************************
function URLEncode(sOriginString)
{
	var SAFECHARS = "0123456789" +					// Numeric
					"ABCDEFGHIJKLMNOPQRSTUVWXYZ" +	// Alphabetic
					"abcdefghijklmnopqrstuvwxyz" +
					"-_.!~*'()";					// RFC2396 Mark characters
	var HEX = "0123456789ABCDEF";

	var plaintext = sOriginString;
	var encoded = "";
	for (var i = 0; i < plaintext.length; i++ ) {
		var ch = plaintext.charAt(i);
	    if (ch == " ") {
		    encoded += "+";				// x-www-urlencoded, rather than %20
		} else if (SAFECHARS.indexOf(ch) != -1) {
		    encoded += ch;
		} else {
		    var charCode = ch.charCodeAt(0);
			if (charCode > 255) {
				//alert(charCode);
			    /*alert( "Unicode Character '" 
                        + ch 
                        + "' cannot be encoded using standard URL encoding.\n" +
				          "(URL encoding only supports 8-bit characters.)\n" +
						  "A space (+) will be substituted." );
				encoded += "+";*/
				//encoded += ch;
				encoded += "%u";
				encoded += HEX.charAt((charCode >> 12) & 0xF);
				encoded += HEX.charAt((charCode >> 8) & 0xF);
				encoded += HEX.charAt((charCode >> 4) & 0xF);
				encoded += HEX.charAt(charCode & 0xF);
			} else {
				encoded += "%";
				encoded += HEX.charAt((charCode >> 4) & 0xF);
				encoded += HEX.charAt(charCode & 0xF);
			}
		}
	} // for

	return encoded;
}

function GetWindowRootActivportal(oWin)
{
	if( oWin != null )
	{
		if (oWin.opener != null && oWin.opener != undefined && (oWin.Dashboard == undefined || oWin.Dashboard == null))
		{
				var oCurrentWin = GetWindowRootActivportal(oWin.opener);
				if( oCurrentWin != null && oCurrentWin.Dashboard != undefined && oCurrentWin.Dashboard != null )
					return oCurrentWin;
		}
		else
			//if( oWin.Dashboard != undefined && oWin.Dashboard != null )
			{
				if( oWin.parent != oWin )
				{
					var oCurrentWin = GetWindowRootActivportal(oWin.parent);
					if( oCurrentWin != null && oCurrentWin.Dashboard != undefined && oCurrentWin.Dashboard != null )
						return oCurrentWin;
				}
				return oWin;
			}
	}
	return null;
}		


function StopEvent(e) {
	//alert(e);
	e = new Event(e).stop();
}
