var totSave=0;
var totSaveDone=0;
/*
jQuery(document).ready(
	function()
	{
		jQuery("img[id^='SendPoll']").bind('click',function() {
			sendPoll(this.id);
		});
	}
);
*/
function isPollCompiled(fullradiolist,checkedradiolist,fullchecklist,checkedchecklist)
{

	//Inizio Radio *****************************************************************
	radioGroupIdList='*';
	radioTest=1;

	//creo lista completa padri dei radio.
	fullradiolist.each(function()
	{
		if(radioGroupIdList.indexOf(this.name)==-1){radioGroupIdList = radioGroupIdList + this.name + '*';}
	});

	//rimuovo dalla lista i padri di quelli checked		
	checkedradiolist.each(function()
	{
		radioGroupIdList = radioGroupIdList.replace('*' + this.name + '*','*');
	});

	if(radioGroupIdList=='*')
		{radioTest=1;}
	else
		{radioTest=0;}
	//Fine Radio *******************************************************************

	//Inizio checkbox **************************************************************
	checkGroupIdList='*';
	checkTest=1;

	//creo lista completa padri dei radio.
	fullchecklist.each(function()
	{
		if(checkGroupIdList.indexOf(GetFatherId(this.id))==-1){checkGroupIdList = checkGroupIdList + GetFatherId(this.id) + '*';}
	});

	//rimuovo dalla lista i padri di quelli checked		
	checkedchecklist.each(function()
	{
		checkGroupIdList = checkGroupIdList.replace('*' + GetFatherId(this.id) + '*','*');
	});

	if(checkGroupIdList=='*')
		{checkTest=1;}
	else
		{checkTest=0;}
	//Fine checkbox *****************************************************************


	if((checkTest==1)&&(radioTest==1))
	{
		//tutto ok
		return true;
	}
	else
	{
		//almeno ad una domanda non ha risposta
		return false;
	}
	
}
			
function sendPoll(idPoll)
{			
	//valorizzo variabili generali
	totSave=0;
	totSaveDone=0;

	//Inizio recupero elenco completo di oggetti di questo blocco sondaggio*********
		var CurrentProductFathersIDList = idPoll.replace('SendPoll','');
		var formPollCollection = jQuery('#FormPoll' + CurrentProductFathersIDList);
	//Fine recupero elenco completo di oggetti di questo blocco sondaggio***********
	
	
	//Inizio recupero lista di oggetti per sapere quanti salvataggi devo fare*******
		var fullradiolist = jQuery(':radio',formPollCollection);
		var fullcheckboxList = jQuery(':checkbox',formPollCollection);
	
		radiolist = jQuery(':radio:checked',formPollCollection);
		checkboxList = jQuery(':checkbox:checked',formPollCollection);
		totSave = radiolist.size() + checkboxList.size();
	//Fine recupero lista di oggetti per sapere quanti salvataggi devo fare*********

	//Inizio tast sondaggio completo************************************************
	if (!isPollCompiled(fullradiolist,radiolist ,fullcheckboxList,checkboxList ))
	{
		alert('Per poter inviare il sondaggio deve rispondere a tutte le domande.');
		return false;
	}
	//Fine tast sondaggio completo**************************************************

	fullradiolist.attr('disabled','disabled');
	fullcheckboxList.attr('disabled','disabled');
	jQuery('#divSendPoll' + idPoll.replace('SendPoll','')).hide();
	jQuery('#divPollSaving' + idPoll.replace('SendPoll','')).show();

	
	//Inizio Radio *****************************************************************	
		radiolist.each(function()
		{
			var ProductFathersIDList = this.id.replace('rdo','');
			var ProductFathersIDListP = this.name;
			Value ='';
			save(Value,ProductFathersIDList,ProductFathersIDListP,idPoll);
		});
	//Fine Radio *******************************************************************

	//Inizio Checkbox **************************************************************
		checkboxList.each(function()
		{
			var ProductFathersIDList = this.id.replace('cbx','');
			var ProductFathersIDListP = '';
			Value ='';
			save(Value,ProductFathersIDList,ProductFathersIDListP,idPoll);	
		});
	//Fine Checkbox ****************************************************************

	fullradiolist.removeAttr('disabled','disabled');
	fullcheckboxList.removeAttr('disabled','disabled');

	jQuery('#divSendPoll' + idPoll.replace('SendPoll','')).show();
	jQuery('#divPollSaving' + idPoll.replace('SendPoll','')).hide();
	
	//Inizio Reset Form ************************************************************
	myform=jQuery('#FormPoll' + idPoll.replace('SendPoll',''));

	jQuery(":input", myform).not(':button, :submit, :reset, :hidden').val('').removeAttr('checked').removeAttr('selected');
	//Fine Reset Form **************************************************************	
}



function save(Value,ProductFathersIDList,ProductFathersIDListP,idPoll)
{
	action='save';
	
	var queryParams = {
				adminid : 0,
				Value : Value ,
				Action : action, 
				id_session : jQuery('#sessionjs').attr('value'), 
				QuestionCardID : jQuery('#QuestionCardIDjs').attr('value'),
				QuestionCard1ID : jQuery('#QuestionCard1IDjs').attr('value'), 
				customerid : jQuery('#customeridjs').attr('value'),
				ProductFathersIDList : ProductFathersIDList, 
				ProductFathersIDListP : ProductFathersIDListP
				}
				
	jQuery.ajax({
		url: "/AllLanguages/Pagine/Sondaggi/Action/SaveAnswer.asp",
		data: queryParams ,
		type: 'POST',
		timeout: 25000,
		async:false,
		error: function(err){
			alert('error');
		},
		success: function(data)
		{
			ValutaRisultatoSalvataggio(data);
 				
	 		if (totSave==totSaveDone)
	 		{
				finitoSalvataggio(idPoll);
				//aggiornaVotanti();
	 		}
		}
	});
}

//valuto l'xml di risultato del salvataggio
function ValutaRisultatoSalvataggio(myxml)
{

		jQuery(myxml).find('root').each(function()
		{
			id_Ret=jQuery(this).find('id_Ret').text();
			DesRet =jQuery(this).find('DesRet').text();
			PosRet =jQuery(this).find('PosRet').text();
			ProductFathersIDListSection=jQuery(this).find('ProductFathersIDListSection').text();				      
			QuestionCardID=jQuery(this).find('QuestionCardID').text();		
			QuestionCard1ID=jQuery(this).find('QuestionCard1ID').text();	
	
			if (id_Ret>0)
			{
				totSaveDone++;
	
				jQuery('#QuestionCardIDjs').attr('value',QuestionCardID);
				jQuery('#QuestionCard1IDjs').attr('value',QuestionCard1ID);
				//tutto ok
			}
			else
			{
				alert('errore durande il salvataggio')
			}
		})
	
}

function finitoSalvataggio(idPoll)
{
	//alert('salvati tutti. PollResultjs:' + jQuery('#PollResultjs').attr('value') + '    MultiAnswerjs:' + jQuery('#MultiAnswerjs').attr('value') + '    idPoll:' + idPoll.replace('SendPoll',''));
	jQuery('#QuestionCardIDjs').attr('value','0');
	jQuery('#QuestionCard1IDjs').attr('value','0');

	var PollResultjs=jQuery('#PollResultjs').attr('value');
	var MultiAnswerjs=jQuery('#MultiAnswerjs').attr('value');

	if (MultiAnswerjs=='0')
	{
	//	alert('Nascondo il sondaggio ' + '#Sondaggio' + idPoll.replace('SendPoll','') + ' e mostro feedback' );
		jQuery('#Sondaggio' + idPoll.replace('SendPoll','')).hide();
		jQuery('#Feedback' + idPoll.replace('SendPoll','')).show();
		
	}
	
	if ((PollResultjs=='AfterResponse') ||(PollResultjs=='Always'))
	{
	//	alert('aggiorno o visualizzo i risultati ' + 'PollResult' + idPoll.replace('SendPoll',''));
		loadResult(idPoll.replace('SendPoll',''));
	}
	
	//se ha finito abilito l'invio dei commenti
	jQuery('#comment').removeAttr('disabled','disabled');
	jQuery('#btnCm').removeAttr('disabled','disabled');
	//jQuery('#chkprivacy').removeAttr('disabled','disabled');
	jQuery('.advise').hide();
}

/*function aggiornaVotanti()
{
	var queryParams = {
		id_session : jQuery('#sessionjs').attr('value'),
		ProductFathersIDList: jQuery('#ProductFathersIDListjs').attr('value')
	}
				
	jQuery.ajax({
		url: "/AllLanguages/Pagine/Sondaggi/Action/getVoter.asp",
		data: queryParams ,
		type: 'POST',
		timeout: 25000,
		async:true,
		cache:false,
		error: function(){
			alert('error');
		},
		success: function(data)
		{
			var ProductFathersIDList = jQuery('#ProductFathersIDListjs').attr('value');
			jQuery('#PollVoter' + ProductFathersIDList.replace(/\./g,'_')).empty();
			jQuery('#PollVoter' + ProductFathersIDList.replace(/\./g,'_')).append(data);
		}
	});
}*/


function loadResult(idPoll)
{
	//rimuovo PollResult + idPoll.replace('SendPoll','')
	//faccio chiamata ajax per risultato
	//inserisco dopo 'Sondaggio' + idPoll.replace('SendPoll','') il nuovo result

	jQuery('#PollResult' + idPoll.replace('SendPoll','')).remove();
	jQuery('#divResultLoading' + idPoll.replace('SendPoll','')).show();
		
	
	var queryParams = {
				adminid : 0,
				id_session : jQuery('#sessionjs').attr('value'), 
				customerid : jQuery('#customeridjs').attr('value'),
				ProductFathersIDList :  idPoll.replace('SendPoll','') 
				}
				
	jQuery.ajax({
		url: "/AllLanguages/Pagine/Sondaggi/Sondaggio_cnt_Risultato.asp",
		data: queryParams ,
		type: 'POST',
		timeout: 25000,
		async:true,
		error: function(err){
			alert('error');
		},
		success: function(data)
		{
			jQuery('#Feedback' + idPoll.replace('SendPoll','')).after(data);
			jQuery('#divResultLoading' + idPoll.replace('SendPoll','')).hide();
		}
	});
	
	//aggiornamento lista votanti
	var ProductFathersIDList = idPoll.replace('SendPoll','');
	ProductFathersIDList = ProductFathersIDList.replace(/\_/g,'.')
	var queryParamsVoter = {
				id_session : jQuery('#sessionjs').attr('value'),
				ProductFathersIDList :  ProductFathersIDList 
				}
				
	jQuery.ajax({
		url: "/AllLanguages/Pagine/Sondaggi/Sondaggio_cnt_Voter.asp",
		data: queryParamsVoter ,
		type: 'POST',
		timeout: 25000,
		async:true,
		cache:false,
		error: function(){
			alert('error');
		},
		success: function(data)
		{
			//jQuery('#PollVoter' + ProductFathersIDList.replace(/\./g,'_')).empty();
			jQuery('#PollVoter' + idPoll.replace('SendPoll','')).replaceWith(data);
		}
	});
	
}


//Utilities
function GetFatherId(stringa)
{
	myArray=stringa.split('_');
	tmpString='';
	for (x=1;x<myArray.length-2;x=x+1)
	{
		if (tmpString!=''){tmpString=tmpString + '_';}
		tmpString=tmpString + myArray[x];
	}
	return '_' + tmpString + '_' ;
}

