function AnswerSurvey() {
    var choices;
    var i;
    var isOK = false;
    var $url = '/sondage/repondre.asp?choixid=';
    
    choices = document.getElementsByName("Choix");
    for(var i=0; i<choices.length; ++i) {
        if (choices[i].checked) isOK = true;
    }
    
    if (isOK) {
        for(var i=0; i<choices.length; ++i) {
		    choices[i].disabled = true;
	        if (choices[i].checked) $url += choices[i].value;
	    }
        document.getElementById("SondagePost").style.display = 'none';
		new RemoteProcedure($url, onSurveyAnswered);
	} else {
		alert('Vous devez spécifier un choix de réponse');
	}
    return false;
}

function onSurveyAnswered(xmlDoc) {
	var oResults = xmlDoc.documentElement;
	var tItems = xmlChildNodes(oResults,'item');
    var sList = '';
    
	if (xmlAttributes(tItems[0])["message"] != undefined) {
	    alert(format(xmlAttributes(tItems[0])["message"]));
	    return false;
	}
    
	var count = oResults.getAttribute("count");
	var surveyid = oResults.getAttribute("surveyid");
	var nbchoix = tItems.length;
	var choix = document.getElementById("sondage_choix");
	document.getElementById("sondage_question").innerHTML = oResults.getAttribute("titre");
	choix.innerHTML = '';
    
	for (var i = 0; i < nbchoix; ++i) {
		var attrib = xmlAttributes(tItems[i]);
		percent = roundit(attrib['count'] / count * 100, 1);
		if (percent == 'NaN') percent = '0';
		if (percent == '.0') percent = '0.0';
	    var newDiv = document.createElement('div');
	    newDiv.setAttribute('id', 'choix'+attrib['choixid']);
	    choix.appendChild(newDiv);

		document.getElementById("choix"+attrib['choixid']).innerHTML = 
		    '\n<div id="reponse' + attrib['choixid'] + '" class="reponse" style="width:1%;"></div>' +
		    '\n<div class="poucentage">' + percent + '%</div>' + 
		    '\n<div class="reponse_choix"><br />' + attrib['titre'] + '</div>\n';
        initGrow("reponse"+attrib['choixid'], percent * 0.80);
	}
	document.getElementById("sondage_nbrepondants").innerHTML = 'pour ' + count + ' r&eacute;pondant';
	if (count > 1) document.getElementById("sondage_nbrepondants").innerHTML += 's';
}


function LoadLastSurveyResults() {
    new RemoteProcedure('/sondage/repondre.asp?last', onSurveyAnswered);
}

function GetSurveyResults(id) {
    new RemoteProcedure('/sondage/repondre.asp?id='+id, onSurveyAnswered);
    return false;
}


function format(str) {
    if (str) {
        str = str.replace(/\[/g, "<");
        str = str.replace(/\]/g, ">");
        //str = str.replace(/<br>/g, " ");
        //str = str.replace(/\&/g, "&amp;");
    }
    return str;
}

function initGrow(objName, lngMax) {
	for (var i=0; i<=lngMax; i++)
		setTimeout('setWidth(\''+objName+'\', '+i+')',10*i);
	return false;
}

function setWidth(objName, value) {
	if (value) {
		obj = document.getElementById(objName);
		obj.style.width = value+'%';
	}
}

function roundit(Num, Places) {
   if (Places > 0) {
      if ((Num.toString().length - Num.toString().lastIndexOf('.')) > (Places + 1)) {
         var Rounder = Math.pow(10, Places);
         n1 = Math.round(Num * Rounder) / Rounder;
		 return n1.toFixed(Places)
      }
      else return Num.toFixed(Places);
   }
   else {
   	number = Math.round(Num);
	return number.toFixed(Places);
   } 
}