// IE detection
var ie = (document.all) ? true:false;

// Ajoute une fonction a l'evenement onload de window
//function addLoadEvent(p_fFunction)
//{
//	var tabArguments = new Array();
//	for(var iArg = 0; iArg < arguments.length; iArg++)
//	{
//		if(iArg != 0) tabArguments.push(arguments[iArg]);
//	}
//	
//	var oldonload = window.onload;
//	
//	if (typeof window.onload != 'function')
//	{
//		window.onload = p_fFunction.apply(null, tabArguments);
//	}
//	else 
//	{
//		window.onload = function()
//		{
//			oldonload();
//			p_fFunction.apply(null, tabArguments);
//		}
//	}
//}

function addLoadEvent(func)
{
    var oldonload = window.onload;

    if (typeof window.onload != 'function')
    {
        window.onload = func;
    }
    else 
    {
        window.onload = function()
        {
            oldonload();
            func();
        }
    }
}


// LightBox
function openLightBox(url, width, height, caption)
{
	var objLink = document.createElement('a');

	objLink.setAttribute('href',url);
	objLink.setAttribute('rel','lightbox');
	objLink.setAttribute('title',caption);
	if(typeof width != 'undefined')
	{
		objLink.setAttribute('width',width);
	}
	
	if(typeof height != 'undefined')
	{
		objLink.setAttribute('height',height);
	}
	Lightbox.prototype.start(objLink);
}


/**
 * Display a centrered popup or a submodal popup (depending on the value of the subModal parameter)
 * @param string sUrl Url of the page to load
 * @param string sName Name of the popup (ignored for subModal widow)
 * @param int iWdith Width of the popup
 * @param int @iHeight Height of the window
 * @param string sScroll Display scrollbars value : 'yes' 'no' (ignored for subModal)
 * @param boolean bResize Set resizability of the popup (ignored for subModal)
 * @param boolean bSubModal 
 */
function popup(sUrl, sName, iWidth, iHeight, sScroll, bResizable, bSubModal) {
    if (true || typeof(bSubModal) == "undefined" || !bSubModal)
    {
    	if (typeof(resize) == "undefined") {
    		resize = true;
    	}
    	var iScreenWidth = (screen.width - iWidth) / 2;
    	var iScreenHeight = (screen.height - iHeight) / 2;
    	window.open(sUrl, sName, 'height=' + iHeight + ', width=' + iWidth + ', top=' + iScreenHeight + ', left=' + iScreenWidth + ', scrollbars=' + sScroll + ((bResizable) ? ', resizable' : ""))
    }
	else
	{
		showPopWin(sUrl, iWidth, iHeight, null);
	}
}

/**
 * try to determine if the popup is a submodal or a window.open
 */
function isAPopup()
{
    return (window == top);
}

/**
 * code to put on "close window" buttons - function to call is different if submodal or window.open
 * @param boolean bReload Reload the top location
 */
function popupClose(bReload)
{
	if (typeof(bReload) == "undefined")
	{
		bReload = false;
	}
	
	if (isAPopup())
	{
		if (bReload)
		{
			opener.document.location.reload();
		}
		self.close();
	}
    else
	{
		if (bReload)
		{
			parent.window.top.document.location.reload();
		}
		else
		{
			window.top.hidePopWin();
		}
	}
}

/**
 * add an event on onBlur so that the popup autoclose itself when loosing focus
 */
function autoClose()
{
	return;
	if (isAPopup())
	{
        addEvent(window, "blur", function() { window.close(); });
    }
}

function GetVarFromUrl(p_sVarName)
{
    //http://www.toto.com/nono.aspx?var=value&var2=value2
    var sValueToFind = "";
    var sConcernedUrlPart = document.location.search;
    sConcernedUrlPart = sConcernedUrlPart.substring(1);
    //alert(sConcernedUrlPart);
    if(sConcernedUrlPart.indexOf("&") > -1)
    {
        var asVarValueArray = sConcernedUrlPart.split("&");
        for(var i = 0; i < asVarValueArray.length; i++)
        {
            sValueToFind = FindValue(asVarValueArray[i], p_sVarName);
            if(sValueToFind != "")
            {
                break;
            }
        }
    }
    else
    {
        sValueToFind = FindValue(sConcernedUrlPart, p_sVarName);
    }
    
    return sValueToFind;
}

function FindValue(p_sStringWithEqual, p_sVarName)
{
    var sValueToFind = "";
    var asValueArray = p_sStringWithEqual.split("=");
    if(asValueArray.length > 0)
    {
        if(asValueArray[0] == p_sVarName)
        {
            sValueToFind = asValueArray[1];
        }
    }
    
    return sValueToFind;
}