//RootId = commentReplyForm
//-----------------------------------------------------------------------------------
//
//	Crea la form di risposta ai commenti
//	Inserisce la form e tutti i controlli form
//
//	v_RootId:	prefisso utilizzato per nominare i div di layout del pannello (Es: RootId = commentReplyForm)
//	v_PostId:	postfisso utilizzato per distinguere, a livello di visualizzazione, i commenti inseriti dagli 
//			utenti visitor (bianchi) da quelli di risposta inseriti dagli amministratori Haier (Fuxia)
//	v_PostUrl:	action della form
//	v_PostTitle:	titolo del commento (o della reply)
//	v_UserId:	id del utente corrente (quello che scrive il commento)
//
//
//-----------------------------------------------------------------------------------
function CreateRepliesForm(v_RootId, v_PostId, v_PostUrl, v_PostTitle, v_UserId, v_NameInitValue){
	
	var renderTo = $(v_RootId + 'Container' + v_PostId);
	
	
	
	//
	//	carico via Ajax la form di inserimento Post solo 
	//	se è presente in pagina il di replyForm che viene 
	//	inserito solamente se utente corrente ha i privilegi 
	//	per inserire un post.
	//
	//	la struttura della form è definita nel template 
	//	di Edit Post.
	//	
	
	var strReplyParams = {
		func: 'add',
		"class": 'WebGUI::Asset::Post',
		withQuote: 0,
		title: v_PostTitle,
		ajaxview: 1,
		"nameinitvalue": v_NameInitValue,
		reply: 1,
		postId: v_PostId
	};
	
	
	new Ajax.Updater(renderTo, v_PostUrl, {
		method: 'get',
		parameters: strReplyParams			
	});
	
	
}

//-------------------------------------------------------------------------------
//
//	Distrugge la form di inserimento delle risposte ai commenti
//
//-------------------------------------------------------------------------------
function DestroyRepliesForm(v_RootId, v_PostId){
	var renderTo = $(v_RootId + 'Container' + v_PostId);

	renderTo.update('');
}

//-------------------------------------------------------------------------------
//
//	Apre e chiude il pannello con la form di risposta ai commenti
//	Apertura:
//		crea la form
//		apre il pannello
//
//	Chiusura:
//		chiude il pannello
//		distrugge la form
//
//-------------------------------------------------------------------------------
function ToggleRepliesForm(v_RootId, v_PostId, v_PostUrl, v_PostTitle, v_UserId, v_NameInitValue){
	var objElement = $(v_RootId + 'EffectWrapper' + v_PostId);
	var duration = 1.0;

	var objToggleControlOpen = $(v_RootId + 'ToggleOpen' + v_PostId);
	var objToggleControlClose = $(v_RootId + 'ToggleClose' + v_PostId);
	var objToggleFormTitle = $(v_RootId + 'TitleBar' + v_PostId);

	
	if(objElement.visible()){
		//chiusura
		
		Effect.toggle(objElement, 'slide', { 
			duration: duration,
			afterFinish: function(){
				DestroyRepliesForm(v_RootId + 'Form', v_PostId);
			}
			
		});
	
		
		Effect.Fade(objToggleControlClose, {
			afterFinish: function() {
				objToggleControlOpen.appear();
			}
		});
		objToggleFormTitle.fade();
		
		
		
	}else{
		//apertura
		CreateRepliesForm(v_RootId + 'Form', v_PostId, v_PostUrl, v_PostTitle, v_UserId, v_NameInitValue);
				
		Effect.Fade(objToggleControlOpen, {
			afterFinish: function() {
				objToggleControlClose.appear();
			}
		});
		objToggleFormTitle.appear();
		
		Effect.toggle(objElement, 'slide', { 
			duration: duration
		});
	}

	return false;
}


function ToggleComments(vByClick, vContentsWrapper, vCommentsWrapper, v_strCurrentUrl, v_strThreadTitle, v_NameInitValue) {
	var strAnchor;
	var strNewAnchor = '';
	var reAnchor = new RegExp('(.*)#(.*)');
	strAnchor = document.location.href.replace(reAnchor, '$2');

	
	var animationOption;
	
	
	var blnLoadCommentForm = false;


	if(vByClick){
	
		
	
		if(strAnchor  == "comments"){
			strNewAnchor = '#relatedcontents';
		}else{
			strNewAnchor = '#comments';
			blnLoadCommentForm = true;
		}
		
		location.href = strNewAnchor;
	}else{

		
	
		if(strAnchor  == "comments"){

			blnLoadCommentForm = true;
		}
	}


	
	
	if($(vContentsWrapper)){
		TogglePanel(vContentsWrapper, vByClick);
	}

		
	if($(vCommentsWrapper)){
		TogglePanel(vCommentsWrapper, vByClick);
	}

	
	//
	//	carico via Ajax la form di inserimento Post solo 
	//	se è presente in pagina il di replyForm che viene 
	//	inserito solamente se utente corrente ha i privilegi 
	//	per inserire un post.
	//
	//	la struttura della form è definita nel template 
	//	di Edit Post.
	//	
	if(blnLoadCommentForm && $('replyForm')){

		var strReplyUrl = v_strCurrentUrl;
		var strReplyParams = {
			func: 'add',
			"class": 'WebGUI::Asset::Post',
			withQuote: 0,
			title: v_strThreadTitle,
			"nameinitvalue": v_NameInitValue,
			ajaxview: 1
		};

					
		new Ajax.Updater('replyForm', strReplyUrl, {
			method: 'get',
			parameters: strReplyParams			
		});
		
		
	}

	return false;
}



function submitComment(vCommentForm, vCommentFormWrapper, vCommentPostResultPanel, vMessagesPanelId, vContentFieldId){
	var objForm = $(vCommentForm);
	var url = '';	//action della form, utilizzato come url per la chiamata Ajax
	var formParameters = '';	//hash contenente tutti i campi della form
	var strContent = '';	//valore del campo content, unica validazione di invio
	var objMessagePanel = $(vMessagesPanelId);	//div per la visualizzazione di wventuali messaggi errore
	
	//leggo la form
	url = objForm.action.replace(/#.*?$/,'');
	formParameters = objForm.serialize(true);
	
	
	
	formParameters.ajaxview = 1;
	//leggo il campo content della form, se è vuoto la form non è valida
	strContent = $F(vContentFieldId);
	
	//resetto e nascondo eventuali messaggi
	
	objMessagePanel.update('');
	objMessagePanel.hide();
	
	formParameters.func = 'editSave';

	if(strContent != ''){
	
		//nascondo la form e visualizzo il messaggio di attesa
		$(vCommentFormWrapper).fade({ 
			duration: 1.0, 
			afterFinish: function(){
				$(vCommentPostResultPanel).update('<' + 'div class="commentAfterSendMessage">Attendi per favore...<' + '/div' + '>');
				$(vCommentPostResultPanel).appear({ duration: 1.0 });
			}
		});
	
		//---------------------------------------------------
		//
		//	Sposto le operazioni della variabile 
		//	form_submit dentro questa funzione così posso 
		//	controllare il submit
		//
		//---------------------------------------------------
		//invio della form
		new Ajax.Request(url, {
	  		method: 'post',
	  		parameters: formParameters,
	  		onSuccess: function(transport){
	
				//
				//	Se inserimento è avvenuto senza errori, viene visualizzato il template 
				//	Generic content post received template
				//	che ha nel suo content il campo hidden status=1
				//
				//	per accertarmi che inserimento sia riuscito verifico che nel 
				//	transport.responseText ci sia la stringa 
				//
				//	name="status" value="1"
				//
				
				check = transport.responseText.indexOf("submitstatusok");
				
				if (check >= 0){
					
					$(vCommentPostResultPanel).fade({ 
						duration: 1.0, 
						afterFinish: function(){
							$(vCommentPostResultPanel).update('<' + 'div class="commentAfterSendMessage">Grazie per il tuo commento.<' + '/div' + '>');
							$(vCommentPostResultPanel).appear({ duration: 1.0 });
						}
					});
					
				}else{
					
					$(vCommentPostResultPanel).fade({ 
						duration: 1.0, 
						afterFinish: function(){
							$(vCommentPostResultPanel).update('<' + 'div class="commentAfterSendMessage">Si è verificato un errore durante invio del tuo commento, accertati di aver inserito correttamente il codice di verifica e riprova.<' + '/div' + '>');
							$(vCommentPostResultPanel).appear({ duration: 1.0 });
						}
					});
										
				}
			}
		});
	}else{
		
		//il testo del commento non è stato compilato, scrivo un 
		//messaggio di errore di fianco al tasto send
		objMessagePanel.update('Devi inserire il testo del commento');
		objMessagePanel.appear();
	}
	
	return false;
}