// Polemus Javascript by EBVG 2008 All rights reserved (contact@polemus.com)
var xmlHttp
var qcm_libre
var id_qcm=new Array(,-1) // -1 to avoid a bug in the syntax of javascript
var qcm=new Array()
var answer=new Array()
var n_answer=new Array()
var pictures=new Array("participant_data/5/6/2/com_en_1779562.jpg","participant_data/9/9/1/com_en_1736991.jpg","participant_data/4/6/7/com_en_1981467.jpg","participant_data/7/6/4/com_en_1794764.jpg","participant_data/7/2/3/com_en_1278723.jpg","participant_data/5/0/0/com_en_1111500.jpg","participant_data/8/4/0/com_en_1163840.jpg","participant_data/5/3/0/com_en_1075530.jpg","participant_data/8/2/7/com_en_1869827.jpg","participant_data/8/1/7/com_en_1500817.jpg")
var dok=1001
var i_picture
var _next_question="next question"
var lang="en"
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)