var nSelected = 0;

function getPrecision(nVar, nDec)
{
var i, strVar, nSepPos, nDecNum;
var strZero = "0000000000000000";

if ((nVar == "undefined") || (nVar == null) || (nVar.length == 0))
nVar = "0";

fTmp = parseFloat(1) / 10;
g_strFloatSeparator = fTmp.toLocaleString().substr(1, 1);
strVar = ((Math.round(parseFloat(nVar) * Math.pow(10, nDec))) / Math.pow(10, nDec)).toString();
nSepPos = strVar.indexOf(".");

//nDecNum = strVar.substr(nSepPos + 1).length;
nDecNum = strVar.length - nSepPos;

if (nSepPos == -1)
{
var arrT = new Array(strVar, g_strFloatSeparator, strZero.substr(0, nDec));
strVar = arrT.join("");
}
else
{
var arrT = new Array(strVar.substr(0, nSepPos), g_strFloatSeparator, strVar.substr(nSepPos + 1), strZero.substr(0, nDec - nDecNum + 1));

strVar = arrT.join("");
}

fTmp = 1000;
strTmp = fTmp.toLocaleString();
fTmp = 1;

if (strTmp.length - 4 > fTmp.toLocaleString().length - 1)
{
g_strGroupSeparator = strTmp.substr(1, 1);

i = strVar.indexOf(g_strGroupSeparator);
nLast = -1;
if (g_strGroupSeparator == g_strFloatSeparator)
nLast = strVar.lastIndexOf(g_strGroupSeparator);

while ((i >= 0) && (i != nLast))
{
strVar = strVar.substr(0, i) + strVar.substr(i + 1);
i = strVar.indexOf(g_strGroupSeparator);
nLast--;
}
}
return strVar;
}

function isChecked(nrReponses) {

idChecked = '';

for (i=1;i<=nrReponses;i++){
	if (document.getElementById('vote'+i).checked ==true)
	{
		idChecked = 'vote'+i;
		nSelected = i;
	}
}

if (idChecked == ''){
	alert('Vous devez cocher au moins une case!')
	} else {
		ourDate = new Date();
		ourTime = ourDate.getTime();
		makeHttpRequestSondage('scriptsondage.php?strVote='+document.getElementById(idChecked).value+'&time='+ourTime , 'GET');
		}
}

function makeHttpRequestSondage(scriptpath,urlmethod) {
		if(!initAjax()) {
				alert('Votre navigateur ne supporte pas AJAX!');
				return false;
		}
		strString = "";
		xmlHttp.open(urlmethod,scriptpath,true);
		xmlHttp.onreadystatechange = function() {
				if(xmlHttp.readyState == 4) {
						if(xmlHttp.status == 200) {
							strString = xmlHttp.responseText.toString();
							processResultSondage(strString);
						} else {
							strString = "Page: " + scriptpath + " - " + xmlHttp.status + ": " + xmlHttp.statusText.toString();
						}
				} else {
							//parent.document.getElementById("divContact").innerHTML = "<strong>Envoi du message</strong>";
				}
				
		}
    xmlHttp.send(null);
}

function processResultSondage(strString){
	document.getElementById("nVotes" + nSelected).innerHTML = strString;
	i = 1;
	document.getElementById("nrVotes").innerHTML = parseInt(document.getElementById("nrVotes").innerHTML)+1;
		while (document.getElementById("pourcentage" + i))
		{

			document.getElementById("pourcentage" + i).innerHTML = getPrecision(parseFloat(parseInt(document.getElementById("nVotes" + i).innerHTML)/parseInt(document.getElementById("nrVotes").innerHTML)*100),2)+"%";
			i++;
		}
}