//Affichage d'un commentaire pour une question
displayComment = function(idQuestion) {
	eltComment = $('QuestionComment'+idQuestion);
	if(eltComment) {
		if(eltComment.hasClassName('ActiveComment')) {
			eltComment.removeClassName('ActiveComment');
			eltComment.addClassName('InactiveComment');
		} else {
			eltComment.removeClassName('InactiveComment');
			eltComment.addClassName('ActiveComment');
		}
	}
};

//Affichage des commentaires em mode une question par page
displayCommentOneQuestion = function(idQuestion) {
	//On affiche et active le commentaire de la question en cours
	eltComment = $('QuestionComment'+idQuestion);
	if(eltComment.hasClassName('ActiveComment')) {
		eltComment.removeClassName('ActiveComment') ;
//		animateInactiveComment(eltComment);
		eltComment.addClassName('InactiveComment');
	} else {
		eltComment.removeClassName('InactiveComment') ;
		eltComment.addClassName('ActiveComment');
		animateActiveComment(eltComment);
	}
};

//Fonctionnement des commentaires en mode question multi
updateCommentMulti = function(strComment, aIdQuestion) {
	$A(aIdQuestion).each(function(idQuestion) {
		eltTextarea = $('QuestionCommentMulti'+idQuestion);
		if(eltTextarea) {
			eltTextarea.value = strComment;
		}
	});
};

//Validation du questionnaire
validateQuestionnaire = function() {
	oParam = $('QuestionnaireForm').serialize(true);
	oParam.a = 'validateQuestionnaire';
	oParam.p = _ID_PROJECT;
	oParam.mode = _MODE;
	
	eltBody = document.body;
	eltBody.padding = 0;
		
	$('Content').innerHTML = '';
	$('AjaxLoadingShadow').update(_AJAX_LOADING);
	$('AjaxLoadingShadow').setStyle({display: 'block'});
	new Ajax.Request('/index.php', {
		method: 'POST',
		parameters: oParam,
		onComplete: function(oXmlResponse) {
			$('Content').innerHTML += oXmlResponse.responseText;
			evalScripts($('Content'));
		}
	});
};

//Validation partiel du questionnaire
validatePartialQuestionnaire = function() {
	oParam = $('QuestionnaireForm').serialize(true);
	oParam.a = 'validatePartialQuestionnaire';
	oParam.p = _ID_PROJECT;
	oParam.mode = _MODE;
	
	new Ajax.Request('/index.php', {
		method: 'POST',
		parameters: oParam
	});
};

/*
 * Affichage du formulaire de conversion
 * @param boolean bClosablePopup Indique si on propose la fermeture de la popup
 * @param string strConversionFunction Indique la fonction utilisée pour la conversion (demande de diag, conversion seule) afin de savoir quel callback appelé
 */
displayConversionForm = function(bClosablePopup, strConversionFunction) {
	if (bClosablePopup == undefined) {
		bClosablePopup = true;
	}
	if (strConversionFunction == undefined) {
		strConversionFunction = '';
	}
	Dialog.info('', {
		windowParameters: {
			className: 'theme',
			title: '',
			width: 200,
			height: 200,
			draggable: false,
			resizable: false,
			hideEffect:Element.hide,
			showEffect:Element.show,
			closable:bClosablePopup,
			recenterAuto: false
		}
	});
	
	Dialog.setInfoMessage(_AJAX_LOADING);
	new Ajax.Request("/index.php", {
		 method: 'post',
		 asynchronous: true,
		 contentType: 'application/x-www-form-urlencoded',
		 encoding: 'UTF-8',
		 parameters: {
			p: _ID_PROJECT,
			a: 'executeConversion',
			mode: _MODE,
			convFunction: strConversionFunction
		},
		 onComplete: function(oHttpResponse) {
		 	Dialog.setInfoMessage(oHttpResponse.responseText);
		 	Windows.focusedWindow.updateWidth();
		 	Windows.focusedWindow.updateHeight();
		 	Windows.focusedWindow.showCenter();
			evalScripts('modal_dialog_message');
		 }
	});
};

displayConversionFormNoClosable = function() {
	displayConversionForm(false);
}

openDiag = function() {
	displayConversionForm(true, 'ConversionDiag');
};

//Validation du formulaire de conversion
validateConversion = function(strConversionFunction) {
	oParam = $('FormLinkConversion').serialize(true);
	oParam.a = 'executeConversion';
	oParam.p = _ID_PROJECT;
	oParam.mode = _MODE;
	oParam.convFunction = strConversionFunction? strConversionFunction : '';
	
	Dialog.setInfoMessage(_AJAX_LOADING);	
	new Ajax.Request("/index.php", {
		 method: 'post',
		 asynchronous: true,
		 contentType: 'application/x-www-form-urlencoded',
		 encoding: 'UTF-8',
		 parameters: oParam,
		 onComplete: function(oHttpResponse) {
		 	Dialog.setInfoMessage(oHttpResponse.responseText);
		 	Windows.focusedWindow.updateWidth();
		 	Windows.focusedWindow.updateHeight();
		 	Windows.focusedWindow.showCenter();
			evalScripts('modal_dialog_message');
		 }
	});
};

//Affichage du formulaire d'envoi à un ami
displayShareForm = function() {
	Dialog.info('', {
		windowParameters: {
			className: 'theme',
			title: '',
			width: 200,
			height: 200,
			draggable: false,
			resizable: false,
			hideEffect:Element.hide,
			showEffect:Element.show
		}
	});
	
	Dialog.setInfoMessage(_AJAX_LOADING);
	new Ajax.Request("/index.php", {
		 method: 'post',
		 asynchronous: true,
		 contentType: 'application/x-www-form-urlencoded',
		 encoding: 'UTF-8',
		 parameters: {a: 'executeShare', p: _ID_PROJECT, mode: _MODE, type:'share'},
		 onComplete: function(oHttpResponse) {
		 	Dialog.setInfoMessage(oHttpResponse.responseText);
		 	Windows.focusedWindow.updateWidth();
		 	Windows.focusedWindow.updateHeight();
		 	Windows.focusedWindow.showCenter();
			evalScripts('modal_dialog_message');
		 }
	});
};

//Validation du formulaire d'envoi à un ami
validateShareForm = function(strType) {	
	oParam = $('FormLinkShare').serialize(true);
	oParam.a = 'executeShare';
	oParam.p = _ID_PROJECT;
	oParam.mode = _MODE;
	oParam.type = strType;
	
	Dialog.setInfoMessage(_AJAX_LOADING);	
	new Ajax.Request("/index.php", {
		 method: 'post',
		 asynchronous: true,
		 contentType: 'application/x-www-form-urlencoded',
		 encoding: 'UTF-8',
		 parameters: oParam,
		 onComplete: function(oHttpResponse) {
		 	Dialog.setInfoMessage(oHttpResponse.responseText);
		 	Windows.focusedWindow.updateWidth();
		 	Windows.focusedWindow.updateHeight();
		 	Windows.focusedWindow.showCenter();
			evalScripts('modal_dialog_message');
		 }
	});
};

//Affichage du formulaire Expert
displayExecuteExpert = function() {
	Dialog.info('', {
		windowParameters: {
			className: 'theme',
			title: '',
			width: 200,
			height: 200,
			draggable: false,
			resizable: false,
			hideEffect:Element.hide,
			showEffect:Element.show,
			recenterAuto:false
		}
	});
	
	Dialog.setInfoMessage(_AJAX_LOADING);
	oParam = {c: 'ProjectL4L', a:'executeExpert', p: _ID_PROJECT, mode: _MODE};
	
	new Ajax.Request("/index.php", {
		 method: 'post',
		 asynchronous: true,
		 contentType: 'application/x-www-form-urlencoded',
		 encoding: 'UTF-8',
		 parameters: oParam,
		 onComplete: function(oHttpResponse) {
		 	Dialog.setInfoMessage(oHttpResponse.responseText);
		 	Windows.focusedWindow.updateWidth();
		 	Windows.focusedWindow.updateHeight();
		 	Windows.focusedWindow.showCenter();
		 	evalScripts('modal_dialog_message');
		 }
	});
};

//Validation du formulaire Expert
validateExecuteExpert = function() {
	oParam = $('FormLinkExpert').serialize(true);
	oParam.a = 'executeExpert';
	oParam.p = _ID_PROJECT;
	oParam.mode = _MODE;
	
	Dialog.setInfoMessage(_AJAX_LOADING);	
	new Ajax.Request("/index.php", {
		 method: 'post',
		 asynchronous: true,
		 contentType: 'application/x-www-form-urlencoded',
		 encoding: 'UTF-8',
		 parameters: oParam,
		 onComplete: function(oHttpResponse) {
		 	Dialog.setInfoMessage(oHttpResponse.responseText);
		 	Windows.focusedWindow.updateWidth();
		 	Windows.focusedWindow.updateHeight();
		 	Windows.focusedWindow.showCenter();
			evalScripts('modal_dialog_message');
		 }
	});
};

//Affichage de la question suivante en mode une question par page
displayNextQuestion = function() {
	
	if(nCurrentQuestion == nTotalQuestion) {
		return false;
	}
	//Masquage du commentaire de la question actuelle
	eltComment = $('QuestionComment'+nCurrentQuestion);
	eltComment.removeClassName('ActiveComment');
	eltComment.addClassName('InactiveComment');
	
	validatePartialQuestionnaire();
	strCurrentId = 'Question'+nCurrentQuestion;
	nCurrentQuestion++;
	strNextId = 'Question'+nCurrentQuestion;
	
		$(strCurrentId).hide();
		$(strNextId).show();
	
	if(nCurrentQuestion == nTotalQuestion) {
		displayValidationButton(true);	
	} else if($('question_'+nCurrentQuestion).value == 0) {
		$('NextQuestion').stopObserving('click');
		$('NextQuestion').removeClassName('NextQuestionActive');
		$('NextQuestion').addClassName('NextQuestionInactive');
	}
	
	//Affichage conditionnel du commentaire de la question suivante
	eltComment = $('QuestionComment'+nCurrentQuestion);
	if($F(eltComment.down('textarea')) != '') {
		eltComment.removeClassName('InactiveComment') ;
		eltComment.addClassName('ActiveComment');
	}
	
	//Gestion du lien "Question précédente"
	if(nCurrentQuestion != 1) {
		$('PreviousQuestion').setStyle({visibility: 'visible'});
	}
};

//Affichage de la question précédente en mode une question par page
displayPreviousQuestion = function() {
	
	if(nCurrentQuestion == 1) {
		return false;
	}
	//Masquage du commentaire de la question actuelle
	eltComment = $('QuestionComment'+nCurrentQuestion);
	eltComment.removeClassName('ActiveComment');
	eltComment.addClassName('InactiveComment');
	
	if(nCurrentQuestion == nTotalQuestion) {
		displayValidationButton(false);
	}
	
	strCurrentId = 'Question'+nCurrentQuestion;
	nCurrentQuestion--;
	strPreviousId = 'Question'+nCurrentQuestion;

	if($('question_'+nCurrentQuestion).value == 1) {
		$('NextQuestion').stopObserving('click');
		$('NextQuestion').observe('click', function() { 
			displayNextQuestion() });
		$('NextQuestion').removeClassName('NextQuestionInactive');
		$('NextQuestion').addClassName('NextQuestionActive');
	} else {
		$('NextQuestion').stopObserving('click');
		$('NextQuestion').removeClassName('NextQuestionActive');
		$('NextQuestion').addClassName('NextQuestionInactive');
	}
	
	//Affichage conditionnel du commentaire de la question suivante
	eltComment = $('QuestionComment'+nCurrentQuestion);
	if($F(eltComment.down('textarea')) != '') {
		eltComment.removeClassName('InactiveComment') ;
		eltComment.addClassName('ActiveComment');
	}
	
	//Gestion du lien "Question précédente"
	if(nCurrentQuestion == 1) {
		$('PreviousQuestion').setStyle({visibility: 'hidden'});
	}
	
		$(strCurrentId).hide();
		$(strPreviousId).show();


};

p01Callback = function() {
	Dialog.closeInfo();
};

p02Callback = function() {
	Dialog.closeInfo();
	validateQuestionnaire();
};

p03Callback = function() {
	Dialog.closeInfo();
};

//Validation du formulaire de conversion
displayLegalMention = function() {
	Dialog.info('', {
		windowParameters: {
			className: 'theme',
			title: '',
			width: 200,
			height: 200,
			draggable: false,
			resizable: false,
			hideEffect:Element.hide,
			showEffect:Element.show,
			recenterAuto:false
		}
	});
	
	oParam = { a: 'displayLegalNotices',  p: _ID_PROJECT, mode: _MODE};
	Dialog.setInfoMessage(_AJAX_LOADING);	
	
	new Ajax.Request("/index.php", {
		 method: 'post',
		 asynchronous: true,
		 contentType: 'application/x-www-form-urlencoded',
		 encoding: 'UTF-8',
		 parameters: oParam,
		 onComplete: function(oHttpResponse) {
		 	Dialog.setInfoMessage(oHttpResponse.responseText);
		 	Windows.focusedWindow.updateWidth();
		 	Windows.focusedWindow.updateHeight();
		 	Windows.focusedWindow.showCenter();
			evalScripts('modal_dialog_message');
		 }
	});
};

var oTimer;
displayValidationMessage = function() {
	$('FooterMessage').hide();
	Effect.Appear($('InfoValidation'), { duration: 1.0 });
	oTimer = new PeriodicalExecuter(hideValidationMessage, 7);
};

hideValidationMessage = function() {
	Effect.Fade('InfoValidation', {
		duration: 1.0,
		afterFinish: function() {
			$('FooterMessage').show();
		}
	});
	if(oTimer) oTimer.stop();
};

evalScripts = function(strIdElement) {
	var eltElement = $(strIdElement);
	if(!eltElement)
		return false;
	var aEltScripts = eltElement.getElementsByTagName("script");
	for (var i=0; i<aEltScripts.length; i++) {
		var eltScript=aEltScripts[i];
		eltScript.setAttribute("type", "text/javascript");
		if (eltScript.src && eltScript.src!="") {} 
		else {
			window.eval(eltScript.text);
		}
	}
};
