/** 
 * @package common
 * @version 1.2 
 * @author Frédéric LECOINTRE<frederic.lecointre@burnweb.net>
 * 
 */
var __functionsToExec = {'main': Array(), 'end':Array()}

/**
 * @param function fFunction
 * @param string sTime
 * @return integer
 */
function registerFunctionAt(fFunction, sTime){

	if(sTime){
		__functionsToExec[sTime][__functionsToExec[sTime].length] = fFunction;
	}//end if
	else{
		sTime = 'main';
		registerFunction(fFunction, 'main');
	}//end else
	
}//end function	

/**
 *
 * @param string sTime
 * @return integer
 */
function execRegisteredFunctionsAt(sTime){

	if(sTime){
	
		for(var i in __functionsToExec[sTime]){
			try{
			
				if(__functionsToExec[sTime][i] != undefined){
					__functionsToExec[sTime][i]();
				}//end if
			
			}//end try
			catch(e){
				alert('Fail to load ' + __functionsToExec[sTime][i] + "\nException: " + e.message);
			}//end catch
			
			__functionsToExec[sTime][i] = undefined;
			
		}//end for

	}//end if
	else{
		execRegisteredFunctionsAt('main');
	}//end else
	
}//end function	

var __oPopup = null;

/**
 * @param string sUrl
 * @param integer iWidth
 * @param integer iHeight
 * @param string sName
 * @return object
 */
function popup(sUrl, iWidth, iHeight, sName){
	var iCoordX = (screen.width /2) - ( iWidth/2);
	var iCoordY = (screen.height /2) - ( iHeight/2);

	if(sName == undefined){
		sName = 'popup';
	}//end if
	
	try{
		if(__oPopup){
			__oPopup.close();
		}//end if
	}//end try
	catch(oException){}
	
	__oPopup = null;
	__oPopup = window.open( sUrl, sName,'status=0, left=' + iCoordX + ' ,top=' + iCoordY + ' ,height=' + iHeight + ' ,width=' + iWidth + ', toolbar=no, status=no, location=no, resizable=no, scrollbars=yes, copyhistory=0, menubar=no');
	__oPopup.focus();
	return false;
	
}//end function

/**
 *
 * @param string sUrl
 * @param string sName
 * @return void
 */
function loadExternalUrl(sUrl, sName){
	if(sName == undefined){
		sName = '';
	}//end if
	var oWindow = window.open( sUrl, sName,'');
	oWindow.focus();
}//end function

/**
 *
 * @param string sId
 * @return object
 */
function elem(sId){
	return window.document.getElementById(sId);
}//end function 

/**
 *
 * @param string sId
 * @param boolean bModeBlock
 * @param boolean bRetval
 * @return object
 */
function showElement(sId, bModeBlock, bRetval){
	var oObj = window.document.getElementById(sId);
	
	if(oObj){
		oObj.style.visibility = 'visible';
		oObj.style.display = (bModeBlock) ? 'block' : 'inline';
	}//end if
	
	if(bRetval){
		return sId;
	}//end if
}//end fiunction

/**
 *
 * @param string sId
 * @param boolean bRetval
 * @return object
 */
function hideElement(sId, bRetval){
	var oObj = window.document.getElementById(sId);
	
	if(oObj){
		oObj.style.visibility = 'hidden';
		oObj.style.display = 'none';
	}//end if
	
	if(bRetval){
		return sId;
	}//end if
}//end fiunction

/**
 *
 * @param string sId
 * @param boolean bRetval
 * @return object
 */
function focusElement(sId, bRetval){
	var oObj = window.document.getElementById(sId);
	
	if(oObj){
		oObj.focus();
	}//end if
	
	if(bRetval){
		return sId;
	}//end if
}//end fiunction

/**
 *
 * @param string sId
 * @param boolean bRetval
 * @return object
 */
function focusSelectElement(sId, bRetval){
	var oObj = window.document.getElementById(sId);
	
	if(oObj){
		oObj.select();
		oObj.focus();
	}//end if
	
	if(bRetval){
		return sId;
	}//end if
}//end fiunction
	
/**
 *
 * @param string sExt;
 * @param array aExt;
 * @return bool
 */
function in_array(sExt, aExt){
	var i = 0;
	for(i=0;i<aExt.length;i++){
		if(sExt==aExt[i]){
			return true;
		}//end if
	}//end for
	return false;
}//end function

/**
 * 
 * @param string method
 * @param object obj
 * @param array prams
 * @return mixed
 * @since 1.0.0
 */
function callFunction(method, obj, params){

	var retval = null;
	var strToEval = 'retval = ';
	if(obj) strToEval = 'obj.';
	
	strToEval += method + '(';
	
	if(params){
	
		var szParams = params.length;
		var i;
		if(szParams > 0){
		
			var arrayToStr = Array();
			for(i = 0; i < szParams; i++)
				arrayToStr[i] = 'params[' + i + ']';
				
			strToEval += arrayToStr.join(',');
			
		}//end if
		
	}//end if

	strToEval += ')';

	try{
		eval(strToEval);
	}//end try
	catch(e){
		throw e;
	}//end catch
	
	return retval;
}//end function

/**
 * 
 * @return integer
 * @since 1.0.0
 */
function getInnerWidth() { 

	if(window.browser.isIe()){
		return window.document.body.clientWidth;
	}//end if
	else{
		return window.innerWidth;
	}//end else
	
}//end function

/**
 * 
 * @return integer
 * @since 1.0.0
 */
function getInnerHeight() { 

	if(window.browser.isIe()){
		return window.document.body.clientHeight;
	}//end if
	else{
		return window.innerHeight;
	}//end else
	
}//end function

/**
 * 
 * @return boolean
 * @since 1.0.0
 */
function eventFalse(){ 
	return false;	
}//end function

/**
 *
 * @param event e
 * @return void
 */
function cancelEvent(e) {

	if (!e){
		var e = window.event;
	}//end if
	
	e.cancelBubble = true;
	
	if (e.stopPropagation){
		e.stopPropagation();
	}//end if
	
}//end function

/**
 * Permet de rendre réactif un objet flash sans que l'utilisateur ai à activer l'objet en cliquant dessus
 * Cette méthode écrit le code html pour insérer l'objet.
 * Exemple d'utilisation:
 *
 * <script type="text/javascript" src="js/patchPluginIE.js"></script>
 * <script type="text/javascript">
 *	aParams = new Array();
 *	aParams['wMode'] = 'transparent';
 *	aParams['scalar'] = 'noborder';
 *	printFlashObject("test.swf", "testId", "550", "400", "#ffffff" , 'position: relative;', aParams);
 *	aParams = null;
 * </script>
 *
 * @param sMovie le nom du fichier flash, ce qui doit aller dans l'attribut value de <param name="movie" value="" />
 * @param sName l'identifiant de l'objet (attribut id de <object />)
 * @param sWidth largeur de l'objet
 * @param sHeight hauteur de l'objet
 * @param sColor couleur de fond de l'objet, par défaut #ffffff
 * @param sStyle la valeur de l'attribut style, vide par défaut
 * @param array aParams paramètres additionnels pour l'objet
 * @return void
 * @version 1.0
 */
 
function printFlashObject(sMovie, sName, sWidth, sHeight, sColor, sStyle, aParams)  {

	if(sColor == undefined){
		sColor = '#ffffff';
	}//end if
	
	if(sName == undefined){
		sName = '';
	}//end if
	
	if(sStyle == undefined){
		sStyle = '';
	}//end if
	
	if(aParams == undefined){
		aParams = new Array();
	}//end if
	
	if(aParams['quality'] == undefined){
		aParams['quality'] = 'best';
	}//end if
	
	if(aParams['loop'] == undefined){
		aParams['loop'] = 'true';
	}//end if
	
	if(aParams['wmode'] == undefined){
		aParams['wmode'] = 'transparent';
	}//end if
	
	var sHtmlContent = '';
	
	if (aParams['href'] != undefined) {
		sHtmlContent += '<a href="' + aParams['href'] + '">';
	}//end if
	
	sHtmlContent += '<object id="' + sName + '"  name="' + sName + '" classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" codebase="http://fpdownload.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=8,0,0,0" ';
	sHtmlContent += ' width="' + sWidth + '" height="' + sHeight + '" align="middle" style="' + sStyle + '">';
	if (aParams['href'] == undefined) {
		sHtmlContent += '<param name="movie" value="' + sMovie + '" />';
	} else {
		sHtmlContent += '<param name="movie" value="' + sMovie + '?href=' + aParams['href'] + '" />';
	}
	sHtmlContent += '<param name="bgcolor" value="' + sColor + '" />';
	
	for(var sIndex in aParams){
		if (sIndex != 'href') {
			sHtmlContent += '<param name="'+ sIndex + '" value="' + aParams[sIndex] + '" />';
		}
	}//end for

	sHtmlContent += '<embed src="' + sMovie;
	if (aParams['href'] == undefined) {
		sHtmlContent += '" bgcolor="' + sColor + '" width="' + sWidth + '" ';
	} else {
		sHtmlContent += '?href=' + aParams['href'] + '" bgcolor="' + sColor + '" width="' + sWidth + '" ';
	}
	
	sHtmlContent += 'height="' + sHeight + '" name="' + sName + '" '; 
	for(var sIndex in aParams){
		sHtmlContent += sIndex + '="' + aParams[sIndex] + '" ';
	}//end for

	sHtmlContent += 'type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer">';
	sHtmlContent += '</object>';
	if (aParams['href'] != undefined) {
		sHtmlContent += '</a>';
	}
	document.write(sHtmlContent);

	// On vide le tableau pour ne pas les animations suivantes
	aParams = null;
	
}//end function



 
function setTailleFenetre(iIdConteneur,iLargeur,bCentre){
	hauteur=elem(iIdConteneur).offsetHeight;
	if(hauteur>650){
		hauteur=650;
	}
	if(document.all){
		window.resizeTo(iLargeur,hauteur+90);
	}
	else{
		window.outerHeight =hauteur+55;	
		window.outerWidth =iLargeur;	
	}
	if(bCentre == undefined || bCentre == true){
		centreFenetre();
	}//end if	
	
}

function centreFenetre(){
	if(document.all){
		var x = (screen.width - document.body.offsetWidth)/2;
		var y = (screen.height - document.body.offsetHeight)/2;
	}
	else{
		var px1 = opener.screenX;
		var px2 = opener.screenX + opener.outerWidth;
		var py1 = opener.screenY;
		var py2 = opener.screenY + opener.outerHeight;
		var x = (px2 - px1 - window.outerWidth) / 2;
		var y = (py2 - py1 - window.outerHeight) / 2;
	}
	window.moveTo(x, y);	
}

/**
** Test si le parametre est un email valide
*/
function isEmail(sValue) {
	var Expression = /^[a-zA-Z][\w\.-]*[a-zA-Z0-9]@[a-zA-Z0-9][\w\.-]*[a-zA-Z0-9]\.[a-zA-Z][a-zA-Z\.]*[a-zA-Z]$/;
	//var Expression = /^[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+(?:[A-Z]{2}|com|org|net|gov|biz|info|name|aero|biz|info|jobs|museum)\b$/;
	return Expression.test(sValue);
}//End function

/**
** Test si le parametre est de type integer
*/
function isInteger(sValue) {
	var Expr = /^[0-9]+$/;
	return Expr.test(sValue);
}//End function

/**
** Test si le parametre est de type float
** Le séparateur est le point
** On peut avoir les motifs suivant :
**	12
**	12.34
**	12.
**	.34
*/
function isFloat(sValue) {
	var Expression = /^[0-9]*\.[0-9]+|[0-9]+\.[0-9]*|[0-9]+$/;
	return Expression.test(sValue);
}//End function

/**
** Test un numéro de téléphone francais
** les séparateurs possibles sont . - et espace
*/
function isFrenchPhoneNumber(sValue) {
	var Expression = /^0[0-9][-.\s]?[0-9]{2}[-.\s]?[0-9]{2}[-.\s]?[0-9]{2}[-.\s]?[0-9]{2}$/;
	return Expression.test(sValue);
}//End function
