function addListener(element, baseName, handler) {
    if (element.addEventListener)
        element.addEventListener(baseName, handler, false);
    else if (element.attachEvent)
        element.attachEvent('on' + baseName, handler);
} // addListener

function switchClass(element_id, class_name) {
	var element = document.getElementById(element_id);
	if (element != null) {
		element.oldClassName = element.className;
		element.className = class_name;
	}
}

function execGlobalAffCommand(myEvent, filmId, myParam, target) {	//Affiche le contenu de la page
		var initCallback = "";
		switch(myEvent)
		{
			case "addComment":
				initCallback="addFormChecks();";
			break;
		}
		displayContent(target, 'includes/display.php?action='+myEvent+'&id='+filmId+'&'+myParam+'', ''+initCallback+'');
} //execGlobalAffCommand
function execGlobalUpdateCommand(myEvent, pageId, myParam, target) {	//Execute les mises a jour et redirige
		var initCallback = "";
		displayContent(target, 'includes/update.php?action='+myEvent+'&id='+pageId+'&'+myParam+'', "displayContent('"+target+"', 'includes/displayPages.php?action=affPageContent&id="+pageId+"&"+myParam+"', \""+initCallback+"\");");
} //execGlobalUpdateCommand





function execAffCommand(myEvent, myParam, target, launchTiny) {	//Affiche le contenu de la page
		var initFoward = "";
		if (launchTiny ==1)
			initFoward="initTinyMce();";
		else if (launchTiny ==2)
			initFoward="addFormChecks();";
		else
			removeTinyMce();
		displayContent(target, 'includes/display.php?action='+myEvent+'&'+myParam+'', initFoward);
} //execAffCommand

function execUpdateCommand(myEvent, myParam, forwardFunc) {	//Execute les mises a jour et redirige
		displayContent("invisible", 'includes/update.php?action='+myEvent+'&'+myParam+'', ""+forwardFunc+"");
} //execGlobalUpdateCommand

function execFormCommand(form) {
	switch (form.getAttribute('id')) {
		case "commentForm": // Ajout de commentaire
			var data="";
			var siteId="";
			for (var index = 0; index < form.elements.length; ++index) {
		        var field = form.elements.item(index);
        		if (index!=0)
					data+="&";
				data += field.getAttribute('id')+"="+escape(getFieldValue(field));
				if (field.getAttribute('id')=='siteId')
					siteId=escape(getFieldValue(field));
      		}
			displayContent('invisible', 'includes/update.php?action=addComment&'+data, "displayContent('comments', 'includes/display.php?action=affComment&"+data+"', '');");
		break;
		case "affCvEditForm": // Edition du cv
			tinyMCE.triggerSave();
			var data="";
			var champ="";
			for (var index = 0; index < form.elements.length; ++index) {
		        var field = form.elements.item(index);
        		if (index!=0)
					data+="&";
				data += field.getAttribute('id')+"="+escape(getFieldValue(field));
				if (field.getAttribute('id')=='champCv')
					champ=escape(getFieldValue(field));
      		}
			var forwardFunc="execAffCommand('affCvArt', 'cvArt="+champ+"', '"+champ+"');";
			execUpdateCommand("affCvEdit", data, forwardFunc);
		break;
		case "editCoordForm": //Edition des coordonnées
			var data="";
			var champ="";
			for (var index = 0; index < form.elements.length; ++index) {
		        var field = form.elements.item(index);
        		if (index!=0)
					data+="&";
				data += field.getAttribute('id')+"="+escape(getFieldValue(field));
      		}
			var forwardFunc="execAffCommand('affCoord', '', 'editContent');";
			execUpdateCommand("affCoordEdit", data, forwardFunc);
		break;
		
		case "addNewsForm": //Ajout d'une news
		case "editNewsForm": //Modification d'une news
		case "delNewsForm": //Suppression d'une news
			if (form.getAttribute('id')!='delNewsForm')
				tinyMCE.triggerSave();
			var data="";
			for (var index = 0; index < form.elements.length; ++index) {
		        var field = form.elements.item(index);
        		if (index!=0)
					data+="&";
				data += field.getAttribute('id')+"="+escape(getFieldValue(field));
      		}
			var forwardFunc="execAffCommand('affNews', '', 'editContent');";
			execUpdateCommand(form.getAttribute('id'), data, forwardFunc);
		break;
		
		case "lostIdForm": //Renvoi des informations de connexion
			var data="";
			for (var index = 0; index < form.elements.length; ++index) {
		        var field = form.elements.item(index);
        		if (index!=0)
					data+="&";
				data += field.getAttribute('id')+"="+escape(getFieldValue(field));
      		}
			var forwardFunc="execAffCommand('affLostIdConfirm', '', 'lostId');";
			execUpdateCommand(form.getAttribute('id'), data, forwardFunc);
		break;
		
	}
}

function initTinyMce()
{
	addFormChecks();
	tinyMCE.execCommand('mceAddControl', false, "contenuText");
	tinyMCE.execCommand('mceFocus', false, "contenuText");
}
function removeTinyMce()
{
		if (document.getElementById('contenuText'))
		{
			tinyMCE.execCommand('mceRemoveControl', false, "contenuText");
			tinyMCE.idCounter=0;
		}
}
