// Polemus Javascript by EBVG 2008 All rights reserved (contact@polemus.com)
var xmlHttp
var qcm_libre
var id_qcm=new Array(467,142,473,465,501,461,336,302,312,-1) // -1 to avoid a bug in the syntax of javascript
var qcm=new Array("Designer\'s Days 2009, detournement de mobilier urbain : Damien Gires (agence LePlanB) propose l\'installation Urban Seat. Ce type de detournement peut-il se repandre dans Paris les annees qui viennent ?","A quand le papier peint solaire (production d\'electricite)dans nos rayons de bricolage:","Tendance deco, couleurs : On voit beaucoup d\'objets sortir avec des tons blancs, comme la Fiat 500 nacree, les portables ASUS blancs seashell, iPhones, etc. Quel sera la tendance pour les meubles blancs en 2010 ?","Lego vient d\'annoncer 2 nouveaux modeles reduits dans sa gamme “Architecture”: la replique de la maison “Fallingwater” et celle du musee Guggenheim de New York, de Frank Lloyd Wright. Quels seront les prochaines repliques ?","Tendance couleur 2011 : Pantone a decerne le titre de couleur de l\'annee 2010 au Turquoise. De quelle dominante dependra la couleur tendance pour l\'annee 2011 ?","Tendance couleur 2010 : Quelle va etre la couleur a la mode pour la decoration en 2010 ?","Apres la tour la plus haute de 808 m et le projet de 1200 m, verrons-nous la tour rotative ( dynamique ) a Dubai - New-York ou Moscou en 2010 ?","Dans une interview, Philippe Starck  vient d\'annoncer que le design ne servait a rien, et que dans 2 ans il changeait de metier. Y croyez-vous ou veut-il seulement faire parler de lui ?","Si Starck arrete le design dans 2 ans comme il le dit, a votre avis quel est l\'objet mythique qui restera dans les memoires, parmi toutes ses creations ?")
var answer=new Array(Array("Oui, avec l\'aide de la Mairie","Oui, de maniere anarchique","Non","","Autre"),Array("2008","2012","2017","",""),Array("blanc mat","blanc nacre","blanc satine","blanc avec textures","Autre"),Array("La Tour Eiffel","La Tour de Pise","Le Transamerica, Chicago","Autre",""),Array("rouge","vert","bleu","jaune","Autre"),Array("Jaune, comme en 2009","Bleu","Rouge","Fuschia","Autre"),Array("Oui","Non","Autre","",""),Array("oui","non","","",""),Array("le presse-agrumes Juicy Salif","le fauteuil Bubble club","le tabouret Bubu","chaises Louis ou Victoria Ghost","Autre"))
var n_answer=new Array(Array(0,0,2,0,11),Array(4,14,13,0,0),Array(1,2,0,0,0),Array(0,2,2,0,0),Array(0,0,0,0,0),Array(2,2,0,1,0),Array(5,3,2,0,0),Array(11,15,0,0,0),Array(6,5,3,6,1))
var pictures=new Array("participant_data/0/5/2/com_fr_1217052.jpg","participant_data/6/2/5/com_fr_1192625.png","participant_data/2/3/2/com_fr_1898232.jpg","participant_data/9/9/8/com_fr_1599998.jpg","participant_data/3/2/3/com_fr_1797323.jpg","participant_data/3/8/6/com_fr_1524386.jpg","participant_data/7/0/4/com_fr_1990704.jpg","participant_data/0/9/1/com_fr_1479091.jpg","participant_data/4/6/2/com_fr_1058462.jpg","participant_data/4/4/9/com_fr_1829449.jpg")
var dok=25
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)