function isFunction(a) {
    return typeof a == 'function';
}

if (!isFunction(Array.prototype.push)) {
    Array.prototype.push = function (obj) {        
        this[this.length] = obj;
        return this.length;
    };
}

var OnLoadFunctions = new Array();
var OnLoadFunctionsEx = new Array();
function AddOnLoadFunction(OnLoadFunction, OnLoadFunctionEx)
{
    OnLoadFunctions.push(OnLoadFunction);
    OnLoadFunctionsEx.push(OnLoadFunctionEx);
}

var OnUnloadFunctions = new Array();
var OnUnloadFunctionsEx = new Array();
function AddOnUnloadFunction(OnUnloadFunction, OnUnloadFunctionEx)
{
    OnUnloadFunctions.push(OnUnloadFunction);
    OnUnloadFunctionsEx.push(OnUnloadFunctionEx);
}



window.onload = OnBodyLoad;
function OnBodyLoad()
{
	for (var i = 0; i < OnLoadFunctions.length; i++)
    {
        var cfunction = OnLoadFunctions[i];
        var cfunctionex = OnLoadFunctionsEx[i];
        eval("if (window." + cfunction + ") { " + cfunctionex + "; }");
    }   
}

window.onunload = OnBodyUnload;
function OnBodyUnload()
{
	for (var i = 0; i < OnUnloadFunctions.length; i++)
    {
        var cfunction = OnUnloadFunctions[i];
        var cfunctionex = OnUnloadFunctionsEx[i];
        eval("if (window." + cfunction + ") { " + cfunctionex + "; }");
    }  
}




function GetEventObject( evt )
{
	if( !evt && window.event )
	{
		evt = window.event;
	}

    if (evt)
	{
		if( evt.srcElement )
			return evt.srcElement;
		else
			return evt.target;
	}
	else
		return null;
}

function getRealLeft(imgElem)
{
	xPos = eval(imgElem).offsetLeft;
	tempEl = eval(imgElem).offsetParent;
  	while (tempEl != null)
	{
  		xPos += tempEl.offsetLeft;
  		tempEl = tempEl.offsetParent;
  	}
  	
    if(navigator.platform == "MacPPC" && navigator.userAgent.indexOf("MSIE") != -1)
    {           
        //Dans IE pour Mac, il faut additionner la marge du body.
        xPos += parseInt(document.body.currentStyle.marginLeft);       
    }
      	
	return xPos;
}

function getRealTop(imgElem)
 {
	yPos = eval(imgElem).offsetTop;
	tempEl = eval(imgElem).offsetParent;
	while ( tempEl != null )
	{
        //Note: On a pas besoin de passer par ici pour calculer la position avec
        //      opera car offsetTop contient déjà la bonne position.  On a pas besoin
        //      de l'additionnner aux positions de ses éléments parent.
  		yPos += tempEl.offsetTop;
  		
  		if(navigator.userAgent.indexOf("Opera") != -1 && yPos != 0)
  		    tempEl = null;
  		else
  		    tempEl = tempEl.offsetParent;	
  	}
  	
    if( (navigator.platform == "MacPPC" && navigator.userAgent.indexOf("MSIE") != -1) )
    {           
        //Dans IE pour Mac, il faut additionner la marge du body.
        yPos += parseInt(document.body.currentStyle.marginTop);       
    }
              	
	return yPos;
}

function getSubMenuTop(imgElem)
 {
	yPos = eval(imgElem).offsetTop;
	tempEl = eval(imgElem).offsetParent;
	while ( tempEl != null && (!tempEl.id || tempEl.id != "menu"))
	{
        //Note: On a pas besoin de passer par ici pour calculer la position avec
        //      opera car offsetTop contient déjà la bonne position.  On a pas besoin
        //      de l'additionnner aux positions de ses éléments parent.
  		yPos += tempEl.offsetTop;
  		
  		if(navigator.userAgent.indexOf("Opera") != -1 && yPos != 0)
  		    tempEl = null;
  		else
  		    tempEl = tempEl.offsetParent;	
  	}
  	
    if( (navigator.platform == "MacPPC" && navigator.userAgent.indexOf("MSIE") != -1) )
    {           
        //Dans IE pour Mac, il faut additionner la marge du body.
        yPos += parseInt(document.body.currentStyle.marginTop);       
    }
              	
	return yPos;
}

AddOnLoadFunction("SetupOnKeyDownHandler", "SetupOnKeyDownHandler()");

function SetupOnKeyDownHandler()
{    
    var bodys = document.getElementsByTagName("body");
    if (bodys && bodys.length > 0)
    {
        bodys[0].onkeydown = OnBodyKeyDown;
    }
}

function OnBodyKeyDown( evt )
{
    var keyCode = -1;
    var ctl = GetEventObject(evt);
    
    if (evt && evt.keyCode)
    {
        keyCode = evt.keyCode;
    }
    else if (event && event.keyCode)
    {
        keyCode = event.keyCode;
    }
    
    if (keyCode == 13 && ctl && ctl.id)
    {
        switch (ctl.id)
        {
            case "ctl00_ctl00_cphBody_cphContent_tbMailingList":
                if( ctl.value == "" )
                    return false;
                
                var fsubmitdealer = document.getElementById("ctl00_ctl00_cphBody_cphContent_ibSubscribeMailingList");
                if (fsubmitdealer)
                {
                    fsubmitdealer.click();
                    return false;
                }
                break;
            case "ctl00_ctl00_cphBody_cphContent_tbMessage":
                return true;
                break;
            default:
                break;
        }
    }
    
    return (keyCode != 13);
}


function getElementsByClass(searchClass,node,tag) {
	var classElements = new Array();
	if ( node == null )
		node = document;
	if ( tag == null )
		tag = '*';
	var els = node.getElementsByTagName(tag);
	var elsLen = els.length;
	var pattern = new RegExp("(^|\s)"+searchClass+"(\s|$)");
	for (i = 0, j = 0; i < elsLen; i++) {
		if ( pattern.test(els[i].className) ) {
			classElements[j] = els[i];
			j++;
		}
	}
	return classElements;
}

function onClickMailingList(evt)
{
    var el = Event.element(evt);
    if( el.value == "Votre courriel" || el.value == "Your email" )
        el.value = "";
}