// Polemus Javascript by EBVG 2008 All rights reserved (contact@polemus.com)
var xmlHttp
var qcm_libre
var id_qcm=new Array(504,460,428,503,502,366,490,492,488,486,429,415,-1) // -1 to avoid a bug in the syntax of javascript
var qcm=new Array("Sarkozymetre : Concernant le regime des retraites, nous allons prendre la mesure suivante en priorite :","Presidentielles 2012, Dominique de Villepin sera-t-il candidat a la presidentielle de 2012 ?","Sarkozymetre (barometre des promesses): \"A la fin de mon quinquennat, on aura augmente de 33 % les credits mis pour chaque etudiant\" Cette promesse sera-t-elle tenue ?  ","Sarkozymetre : Le chomage va reculer en France des cette annee (2010).","Interdiction du voile integral : Quelle proposition sera adoptee en pratique pour empecher les gens de porter un voile cachant le visage ?","Nicolas Sarkozy, lasse de la laicite version francaise, veut faire evoluer les relations entre eglises et etat. Que va-t-il essayer de faire passer","Projets pour Rachida Dati : Quels sont les projets de l\'ancienne Garde des sceaux pour 2011 ?","Barometre des Promesses : Combien de militants UMP y aura-t-il fin 2012 ?","Crise sociale 2010 : Alors que la crise economique s\'estompe, la crise sociale prend le relais. Quels en seront les consequences les plus graves en 2010 ?","Interdiction de deguisements : Le depute UMP du Nord a depose une proposition de loi \"visant a interdire l\'ensemble des vetements ou accessoires permettant de masquer l\'identite d\'une personne\". Qu\'est ce qui sera interdit, finalement ?","Sarkozymetre (barometre des promesses): \"Le non-remplacement d\'un fonctionnaire sur deux partant a la retraite sera maintenu.\" Cette promesse sera-t-elle tenue ?","Bertrandmetre (barometre des promesses) : Le nouveau secretaire general de l\'UMP a annonce un objectif de 500000 adherents d\'ici a 2012. A combien d\'adherents UMP sera-t-on d\'ici debut 2012 ?")
var answer=new Array(Array("reculer l\'age de depart","diminuer les pensions","trouver d\'autres financements","introduire la capitalisation","Autre"),Array("Oui, pour l\'UMP","Oui, en solo","Non, car trop seul","Non, car ineligible","Autre"),Array("Oui","Non","","","Autre"),Array("Oui","Non","","","Autre"),Array("Une amende","Garde a vue","Arrachage du masque","Oublier ce sujet","Autre"),Array("loi de 1905 inchangee","laicite positive","abrogation de la laicite","theocracie en France","Autre"),Array("Rester au parlement europeen","Devenir Maire de Paris","Poste ministeriel","Presidente de l\'UMP","Autre"),Array("500000 ou plus","400000 ou plus","300000 ou plus","200000 ou plus","Autre"),Array("Chomage en hausse","Crise violente, emeutes, etc.","Greves pontuelles ou generales","Fin de crise progressive","Autre"),Array("les lunettes de soleil","les perruques","le voile islamique","rien, c\'est absurde","Autre"),Array("Oui","Non","","","Autre"),Array(">= 500000","< 500000","","","Autre"))
var n_answer=new Array(Array(1,0,0,0,0),Array(0,3,2,0,1),Array(1,4,0,0,2),Array(1,0,0,0,0),Array(1,0,0,1,0),Array(1,5,0,2,1),Array(0,2,1,0,0),Array(1,0,1,1,0),Array(4,0,0,0,0),Array(0,0,1,1,1),Array(5,0,0,0,1),Array(1,5,0,0,1))
var pictures=new Array("participant_data/5/5/1/com_fr_1216551.jpg","participant_data/1/3/8/com_fr_1316138.jpg","participant_data/6/3/4/com_fr_1020634.jpg","participant_data/1/0/1/com_fr_1255101.jpg","participant_data/1/3/6/com_fr_1789136.jpg","participant_data/7/1/9/com_fr_1159719.jpg","participant_data/6/2/6/com_fr_1843626.jpg","participant_data/8/0/9/com_fr_1023809.jpg","participant_data/1/5/5/com_fr_1085155.jpg","participant_data/9/3/8/com_fr_1531938.jpg")
var dok=1
var i_picture
var _next_question="question suivante"
var lang="fr"
var font_size="0.90em"
var img_picture

var chrsIso=new Array("\\","\'");
var chrsUni=new Array(""," ");
function decode(s){
	var chrsCnt=s.length
	var out=s
	for(var k=0;k<chrsCnt;k++) {
		out=out.replace(chrsIso[k],chrsUni[k]);
	}
	return out;
}

function ecrire_cookie(nom, valeur, expires) {
  document.cookie=nom+"="+escape(valeur)+
  ((expires==null) ? "" : ("; expires="+expires.toGMTString()));
}

function arguments_cookie(offset) {
  var endstr=document.cookie.indexOf (";", offset);
  if (endstr==-1) endstr=document.cookie.length;
  return unescape(document.cookie.substring(offset, endstr));
}

function ecrire_cookie_persistant(nom, valeur) {
	date=new Date;
  date.setMonth(date.getMonth()+1);
  ecrire_cookie(nom, valeur, date);
}

function lire_cookie(nom) {
  var arg=nom+"=";
  var alen=arg.length;
  var clen=document.cookie.length;
  var i=0;
  while (i<clen){
    var j=i+alen;
    if (document.cookie.substring(i, j)==arg) return arguments_cookie(j);
    i=document.cookie.indexOf(" ",i)+1;
    if (i==0) break;
  }
  return null; 
}

function setText(obj, text) {
	longueurCible = document.getElementById(obj).firstChild.length
	document.getElementById(obj).firstChild.replaceData(0, longueurCible, text)
}

function lineChoix(mode, num_choix, text_choix, stat) {
			var row=document.createElement("tr")
			// button
			if (mode==1) {
			var cell_button=document.createElement("td")
			cell_button.setAttribute("width",15)
    	var button_choix=document.createElement("input")
			button_choix.removeAttribute("disabled")
			button_choix.setAttribute("type","radio")
			button_choix.setAttribute("value",num_choix)
			button_choix.onclick=function() {getVote(num_choix);}
			cell_button.appendChild(button_choix)
			row.appendChild(cell_button)
			}
			// text
			var cell_choix=document.createElement("td")
			cell_choix.vAlign="top"
    	var text_choix=document.createTextNode(decode(unescape(text_choix)))
			cell_choix.appendChild(text_choix)
			row.appendChild(cell_choix)
			// stats
			if (mode==2) {
			var cell_stat=document.createElement("td")
			cell_stat.setAttribute("width",25)
    	var text_stat=document.createTextNode(Math.floor(100*stat)+"%")
			cell_stat.appendChild(text_stat)
			row.appendChild(cell_stat)
			}
			// bar
			if (mode==2) {
			var cell_bar=document.createElement("td")
			cell_bar.setAttribute("width",55)
			var img_bar=document.createElement("img");
			img_bar.setAttribute("src", "http://www.polemus.com/polevote/poll.gif");
			img_bar.setAttribute("width", 1+50*stat);
			img_bar.setAttribute("height", 8);
			cell_bar.appendChild(img_bar)
			row.appendChild(cell_bar)
			}
			return row
}

function table_logo() {
		var table_l=document.createElement("table")
		var tablebody_logo=document.createElement("tbody")
		var row_logo=document.createElement("tr")
		var cell_logo=document.createElement("td")
		var img_logo=document.createElement("img")
		img_logo.setAttribute("src", "http://www.polemus.com/polevote/logo_polevote.bmp")
		img_logo.setAttribute("onclick", "window.open('http://www.polemus.com/Polemus.php5?lang="+lang+"')")
		img_logo.setAttribute("width", 200)
		cell_logo.appendChild(img_logo)
		row_logo.appendChild(cell_logo)
		tablebody_logo.appendChild(row_logo)
		table_l.appendChild(tablebody_logo)
		return table_l
}

function startPolevote(param) {
	qcm_libre=-1;
	for(i=0; i<(id_qcm.length-1); i++)
		if (lire_cookie("qcm"+id_qcm[i])==null) qcm_libre=i;
	if (qcm_libre>=0) { // display QCM
		// prepare form rendering
		var zone=document.getElementById("poleform")
		while(zone.hasChildNodes()) zone.removeChild(zone.lastChild)
		zone.style.fontSize=font_size
		// logo
		zone.appendChild(table_logo())
		// text of the question
		var table_qcm=document.createElement("table")
		var tablebody_qcm=document.createElement("tbody")
		table_qcm.setAttribute("width", 200)
		table_qcm.style.fontSize=font_size
		table_qcm.style.color='#000000'
		var row_qcm=document.createElement("tr")
		var cell_qcm=document.createElement("td")
 		var text_qcm=document.createTextNode(decode(unescape(qcm[qcm_libre])))
		cell_qcm.onclick= function() {window.open("http://www.polemus.com/Polemus.php5?PredictionRequest="+id_qcm[qcm_libre]+"&lang="+lang);}
		cell_qcm.appendChild(text_qcm)
		row_qcm.appendChild(cell_qcm)
		tablebody_qcm.appendChild(row_qcm)
		table_qcm.appendChild(tablebody_qcm)
		zone.appendChild(table_qcm)
		// table
		var table=document.createElement("table")
		table.cellPadding=0; table.cellSpacing=0
		table.style.fontSize=font_size
		table_qcm.style.color='#000000'
		table.setAttribute("width", 200);
		var tablebody=document.createElement("tbody")
		for (a=0; a<5; a++) if (answer[qcm_libre][a]!="") {
			tablebody.appendChild(lineChoix(1, a+1, answer[qcm_libre][a], 0))
		}
		table.appendChild(tablebody)
		zone.appendChild(table)
		// if last question, set picture counter
		ecrire_cookie_persistant("picture"+dok,0)
	} else { // End of the vote
		// prepare form rendering
		var zone=document.getElementById("poleform")
		while(zone.hasChildNodes()) zone.removeChild(zone.lastChild)
		// logo
		zone.appendChild(table_logo())
		// put a cycling picture
		i_picture=lire_cookie("picture"+dok)
		//alert(i_picture)
		if(isNaN(i_picture)) {i_picture=0;}
		if(pictures[i_picture]!="") {
			loadPicture("http://www.polemus.com/"+pictures[i_picture])
			i_picture++
			if(i_picture>=pictures.length) {
				i_picture=0
			}
			ecrire_cookie_persistant("picture"+dok,i_picture)
		}
	}
}

function isImageOk(img) {
		if(typeof(img)=="undefined") {return false;}
		//alert(img.complete+" "+img.getAttribute("src")+" "+img.getAttribute("width")+" "+img.getAttribute("height"))
    if(!img.complete) {return false;}
    //if (img.getAttribute("width")<1) {return false;}
    //if (img.getAttribute("height")<1) {return false;}
    return true;
}

function loadPicture(pname) {
				img_picture=document.createElement("img")
				img_picture.setAttribute("src", pname)
				img_picture.setAttribute("onclick", "window.open('http://www.polemus.com/Polemus.php5?lang="+lang+"')")
				if(img_picture.getAttribute("width")>0) {
					var w=img_picture.getAttribute("width")
					var h=img_picture.getAttribute("height")
					var nw=200
					var nh=h*(nw/(w+.0001))
					if (nh>200) {
						nh=200
						nw=w*(nh/(h+.0001))
					}
				} else {
					var nw=200
					var nh=200
				}
				var nw=200
				var nh=200
				img_picture.setAttribute("width", nw)
				img_picture.setAttribute("height", nh)
				var zone=document.getElementById("poleform")
				var table_picture=document.createElement("table")
				var tablebody_picture=document.createElement("tbody")
				var row_picture=document.createElement("tr")
				var cell_picture=document.createElement("td")
				cell_picture.align="center"
				cell_picture.appendChild(img_picture)
				row_picture.align="center"
				row_picture.appendChild(cell_picture)
				tablebody_picture.appendChild(row_picture)
				table_picture.appendChild(tablebody_picture)
				zone.appendChild(table_picture)
				t=setTimeout('startPolevote()',10000);
}

function crossDomainHTTPRequest(url) {
	var oScript = document.createElement("script")
	oScript.src = url
	oScript.id="dummyscript"
	document.body.appendChild(oScript);
	//document.body.removeChild(oScript);
}

function getVote(vote) {
	//var url="polevote_v1.php5" // en local
	var url="http://www.polemus.com/polevote/polevote_v1.php5" // sur internet
	crossDomainHTTPRequest(url+"?v="+vote+"&q="+id_qcm[qcm_libre]+"&l="+lang+"&u="+document.domain)
	// compute temporary stats
	n_answer[qcm_libre][vote-1]+=1
	sum=n_answer[qcm_libre][0]+n_answer[qcm_libre][1]+n_answer[qcm_libre][2]+n_answer[qcm_libre][3]+n_answer[qcm_libre][4]+.00000001
	// prepare form rendering
	var zone=document.getElementById("poleform")
	while(zone.hasChildNodes()) zone.removeChild(zone.lastChild)
	zone.style.fontSize=font_size
	// logo
	zone.appendChild(table_logo())
	// text of the question
	var table_qcm=document.createElement("table")
	var tablebody_qcm=document.createElement("tbody")
	table_qcm.setAttribute("width", 200);
	table_qcm.style.fontSize=font_size
	var row_qcm=document.createElement("tr")
	var cell_qcm=document.createElement("td")
	var text_qcm=document.createTextNode(qcm[qcm_libre])
	cell_qcm.onclick= function() {window.open("http://www.polemus.com/Polemus.php5?PredictionRequest="+id_qcm[qcm_libre]+"&lang="+lang);}
	cell_qcm.appendChild(text_qcm)
	row_qcm.appendChild(cell_qcm)
	tablebody_qcm.appendChild(row_qcm)
	table_qcm.appendChild(tablebody_qcm)
	zone.appendChild(table_qcm)
	// table
	var table=document.createElement("table")
	table.style.fontSize=font_size
	table.setAttribute("width", 200);
	var tablebody=document.createElement("tbody")
	for (a=0; a<5; a++) if (answer[qcm_libre][a]!="") {
		tablebody.appendChild(lineChoix(2, a+1, answer[qcm_libre][a], n_answer[qcm_libre][a]/sum))
	}
	table.appendChild(tablebody)
	zone.appendChild(table)
	// next button
 	var button_next=document.createElement("input")
	button_next.style.fontSize=font_size
	button_next.style.backgroundColor='#FFFFFF'
	button_next.style.color='#000000'
	button_next.setAttribute("type","button")
	button_next.setAttribute("value",_next_question)
	//button_next.setAttribute("onclick","startPolevote(0)")
	button_next.onclick=function() {startPolevote(0);}
	zone.appendChild(button_next)
	// set cookie to avoid multiple votes by single users
	ecrire_cookie_persistant("qcm"+id_qcm[qcm_libre],"1")
} 

function getNext() {
	startPolevote()
} 

startPolevote(0)