/**
Copyright (c) 2007 Sopresti. All rights reserved.
Created 01. 05. 2007 by Sopresti  (Web: http://www.sopresti.com )
Last modified: 
**/

// draw bar for one question
function drawBarQuestion(divname,sumArray){

			var graphic = new jsGraphics(divname);	
			graphic.setPrintable(true);
			graphic.setFont("arial","10px",Font.ITALIC_BOLD); 
			
			var bgColor="#E3EAEF";
			var bgColor_bad="#ff4400";
			var bgColor_good="#88ff00";
			var bgColor_no="#88ffff";			
			var bgColor_partial="#ffff00";
			var yBegin=0;
			var addText="%";
			var heightBar=40;
			var xSum=10;
			var xEndBar=150;			
			var i=0;
			// ratio about  design % in bar sum
			// 1%= x pixel
			var ratio=(xEndBar-xSum)/100;
		    var morepixel=-15;
			var msg="";							

	        // values of sum for the question
			tempArray=sumArray;
					
			if(tempArray[0]!=0){		
						graphic.setColor(bgColor_good);
						graphic.fillRect(xSum,(i+morepixel),tempArray[0]*ratio,10);
						graphic.setColor("black");
						graphic.drawRect(xSum,(i+morepixel),tempArray[0]*ratio,10);
						msg="";
						msg=msg.concat(tempArray[0]).concat(addText);
						graphic.drawString(msg,xSum+tempArray[0]*ratio+5,(i+morepixel));
			}
							
			if(tempArray[1]!=0){		
						graphic.setColor(bgColor_bad);
						graphic.fillRect(xSum,(i+morepixel)+10,tempArray[1]*ratio,10);
						graphic.setColor("black");
						graphic.drawRect(xSum,(i+morepixel)+10,tempArray[1]*ratio,10);
						msg="";
						msg=msg.concat(tempArray[1]).concat(addText);
						graphic.drawString(msg,xSum+tempArray[1]*ratio+5,(i+morepixel)+10);
			}
							
			if(tempArray[2]!=0){		
						graphic.setColor(bgColor_no);
						graphic.fillRect(xSum,(i+morepixel)+20,tempArray[2]*ratio,10);
						graphic.setColor("black");
						graphic.drawRect(xSum,(i+morepixel)+20,tempArray[2]*ratio,10);	
						msg="";	
						msg=msg.concat(tempArray[2]).concat(addText);
						graphic.drawString(msg,xSum+tempArray[2]*ratio+5,(i+morepixel)+20);
					}
	
			graphic.paint();

}

// draw arc
function myDrawArcFunction(goodValue,badValue,noanswerValue,divName,texte)
{
			//pos par raport au div
			var x=1;
			var y=1;
			var width=100;
			var height=100;
			
			//Arc
			var goodArc=(360/100)*goodValue;
			var badArc=(360/100)*badValue;
			var noanswerArc=360-goodArc-badArc;
			
			//rectangle lexique
			var widthRectangle=20;
			var heightRectangle=10;
			var xRectangle=x+width+10;
			var yRectangle=y+10;
			
			var jg = new jsGraphics(divName);
			jg.setPrintable(true);
			var msg="";
			
		    //contour gris
			 jg.setColor("grey");
			 jg.fillArc(x+2,y+3,width,height,0,360); 

			 //fond
			 jg.setColor("black"); 
			 jg.fillArc(x-1,y-1,width+2,height+2,0,360); 
			 
			//=======================
			// GOOD RESPONSE
			//=======================
			 jg.setColor("#9999FF"); // green
			if(goodValue!=0){
				 jg.fillArc(x,y,width,height,0,goodArc-1); 
				 }
			 jg.fillRect(xRectangle, yRectangle, widthRectangle, heightRectangle);
			 jg.setColor("#000000"); // black
			 jg.drawRect(xRectangle-1, yRectangle-1, widthRectangle+1, heightRectangle+1);
			 
			  if(goodValue<10){
			 msg="0";
			 msg=msg.concat(goodValue.toString());
			 }
			 else {
			  msg=goodValue.toString();
			 }
			
			 jg.drawString(msg.concat("% correcte(s)"),xRectangle+30,yRectangle);  
			 yRectangle=yRectangle+20;
			
			//=======================
			// NO ANSWER
			//=======================
			 jg.setColor("#FFFFCC"); // blue
			if(noanswerValue!=0){
				 jg.fillArc(x,y,width,height,goodArc+1,goodArc+noanswerArc-1); 
			 }
			 jg.fillRect(xRectangle, yRectangle, widthRectangle, heightRectangle);
			 jg.setColor("#000000"); // black
		 	 jg.drawRect(xRectangle-1, yRectangle-1, widthRectangle+1, heightRectangle+1); 
		 	 
			 if(noanswerValue<10){
				 msg="0";
				 msg=msg.concat(noanswerValue.toString());
			 }
			 else {
				  msg=noanswerValue.toString();
			 }
			 jg.drawString(msg.concat("% non renseignée(s)"),xRectangle+30,yRectangle);  
			 yRectangle=yRectangle+20;
			
			//======================= 
			// BAD RESPONSE 
			//=======================

			 jg.setColor("#993366"); // red
			if(badValue!=0){
			 jg.fillArc(x,y,width,height,goodArc+noanswerArc+1,badArc+goodArc+noanswerArc); 
			 }			 
			 jg.fillRect(xRectangle, yRectangle, widthRectangle, heightRectangle);
			 jg.setColor("#000000"); // black
		  	 jg.drawRect(xRectangle-1, yRectangle-1, widthRectangle+1, heightRectangle+1);
			
			 if(badValue<10){
				 msg="0";
				 msg=msg.concat(badValue.toString());
			 }
			 else {
				  msg=badValue.toString();
			 }
			 jg.drawString(msg.concat("%  incorrecte(s)"),xRectangle+30,yRectangle);  
			 yRectangle=yRectangle+20;
			 jg.drawLine((x+(width/2)), (y+height/2), (x+(width)), (y+height/2)); 
			 jg.setFont("arial","12px",Font.ITALIC_BOLD); 
			 jg.drawString(texte,x,y+height+10); 

			  jg.paint();
}

/**
function draw bar
title 		: array of the label of each array of bar [title1,title2]
valeurs 	: array of array of the values of each bar [[10,20,30],...,[20,20,20]]
divname 	: div in the html code
*/	
function myDrawCompareNoteFunction(valApplicant,valMin,valMoyenne,valMax,divname,addText,notebase)	
{	
					
			var addText = (addText == null) ? "%" : addText;			
			var notebase = (notebase == null) ? 100 : notebase;			
				
			var bar = new jsGraphics(divname);	
			bar.setPrintable(true);
			var bgColor="#CCCCCC";
			var bgColor_bad="#993366";
			var bgColor_good="#9999FF";
			var bgColor_no="#FFFFCC";
			var widthBar=400;
			
			// add space for the text
			var ratio=(widthBar-40)/notebase;
			var xBegin=10;
			var yBegin=10;
			var heightBar=0;
			var increment=0;
			var xBeginBar=2;
			var yEchelle=5;
			var xBeginEchelle=90;
			var heightEchelle=15;
			var xEndBar=600;
			var yBeginBar=20;
			var heightBar=20;
			notebase=20;
			var ratioBar=(xEndBar-xBeginEchelle-20)/notebase;
			
			//DRAW ECHELLE
			bar.setColor("black");
			bar.drawRect(xBeginBar,yEchelle,(xEndBar-xBeginBar)+50,heightEchelle);
			bar.setColor(bgColor_no);
			bar.fillRect(xBeginBar+1,yEchelle+1,(xEndBar-xBeginBar)+50-2,heightEchelle-1);
			bar.setColor("black");
			bar.setFont("arial","12px",Font.ITALIC_BOLD); 	
			bar.drawString("Score :" ,xBeginBar,yEchelle);
			bar.setFont("arial","12px"); 	
			for (i=0; i <= 20; i=i+1) {
				bar.drawString(i,ratioBar*i-3+xBeginEchelle,yEchelle);
			}
			
			//DRAW NOTE MIN MAX
			//draw broder
			bar.setColor("black");
			bar.drawRect(xBeginBar,yBeginBar,(xEndBar-xBeginBar)+50,heightBar);
			//draw bar note
			bar.setColor(bgColor_good);
			bar.fillRect(xBeginEchelle+ratioBar*valMin,yBeginBar+1,ratioBar*(valMax-valMin)+1,heightBar-1);
			// draw min max and moyenne
			bar.setColor("black");
			bar.drawString("Min=".concat(valMin),xBeginEchelle+ratioBar*valMin-65,yBeginBar+5);
			bar.drawString("Max=".concat(valMax),xBeginEchelle+ratioBar*valMax+5,yBeginBar+5);

			//DRAW NOTE MOYENNE APPLICANT
			//border
			bar.drawRect(xBeginBar,yBeginBar+heightBar,(xEndBar-xBeginBar)+50,heightEchelle);
			bar.setColor(bgColor_no);
			bar.fillRect(xBeginBar+1,yBeginBar+heightBar+1,(xEndBar-xBeginBar)+50-2,heightEchelle-1);
			bar.setColor("black");
			var rightText="";
			var lenMoyenne=200;
			var lenApplicant=105;
			var leftText="";
			var rightVal=0;
			var leftVal=0;
			var leftLen=0;
			var leftColor;
			var rightColor;
			// manage text on the right or left between candidat or average
			if(valMoyenne>valApplicant){
			rightText="Moyenne=".concat(valMoyenne);
			rightColor=bgColor_bad;
			rightVal=xBeginEchelle+ratioBar*valMoyenne+2;
			leftText="Candidat=".concat(valApplicant);
			leftVal=xBeginEchelle+ratioBar*valApplicant;
			leftLen=95;
			leftColor="black";
			} else {
			leftText="Moyenne=".concat(valMoyenne);
			leftVal=xBeginEchelle+ratioBar*valMoyenne;
			leftColor=bgColor_bad;
			leftLen=90;
			rightText="Candidat=".concat(valApplicant);
			rightVal=xBeginEchelle+ratioBar*valApplicant+2;
			rightColor="black";
			}
			//draw text on the right
			bar.setColor(rightColor);
			bar.drawString(rightText,rightVal,yBeginBar+heightBar+1);
			bar.fillRect(rightVal-2,yBeginBar+1,5,heightBar-1);
			
			//draw text on the left 
			bar.setColor(leftColor);
			bar.drawString(leftText,leftVal-leftLen,yBeginBar+heightBar+1);
			bar.fillRect(leftVal-2,yBeginBar+1,5,heightBar-1);

			bar.paint();
}


function drawContexte(title,valuesUser,valuesAverage,colors,divname){
var nbrLines=valuesUser.length ;
if(nbrLines==2){
	drawContexte2(title,valuesUser,valuesAverage,colors,divname);
}

if(nbrLines==3){
	drawContexte3(title,valuesUser,valuesAverage,colors,divname);
}

if(nbrLines==4){
	drawContexte4(title,valuesUser,valuesAverage,colors,divname);
}

}

/*
Draw schema 
title 	: array of title of each quizz ["titleQuizz1","titleQuizz2"]
valuesUser	: array of note of each user [valueUser1Quizz1,valueUser1Quizz2] (note on 100 pts)
valuesAverage	: array of note of average [value1Quizz1,value1Quizz2] (note on 100 pts)
colors	: array color of average[0] and user[1] 
divname	:	div in html page
*/
function drawContexte2(realTitle,valuesUser,valuesAverage,colors,divname){
	
	var graphics = new jsGraphics(divname);	
	graphics.setPrintable(true);
	var bgColor="#CCCCCC";
	var widthBar=100;
	var heightBar=100;
	// add space for the text
	var ratio=(widthBar)/100;
	var xBegin=0;
	var yBegin=0;
	graphics.setFont("arial","10px",Font.ITALIC_BOLD); 
	var title=formatTitle(realTitle);

	var arrayX=new Array(xBegin+1,xBegin+1,valuesUser[1]*ratio+1);
	var arrayY=new Array(heightBar-1,heightBar-valuesUser[0]*ratio,heightBar-1);
	var arrayAverageX=new Array(xBegin,xBegin,valuesAverage[1]*ratio+1);
	var arrayAverageY=new Array(heightBar-1,heightBar-valuesAverage[0]*ratio,heightBar-1);
	
	
	var aire=(valuesUser[1]*valuesUser[0])/2;

	graphics.setColor(colors[1]);
	graphics.fillPolygon(arrayX,arrayY);
	graphics.setColor(colors[0]);
	graphics.setStroke(2);
	graphics.drawPolygon(arrayAverageX,arrayAverageY);
	graphics.setStroke(1);
	graphics.setColor("black");
	//var msg="aire : ";
	//msg=msg.concat(aire);
	//graphics.drawString(msg,xBegin+widthBar/2,yBegin+heightBar/2);

	// axe y
	graphics.drawLine(xBegin,yBegin,xBegin,heightBar);
	var msg="";
	msg=msg.concat(title[0]);
	graphics.drawString(msg,xBegin+5,0);	
	
	//axe x
	graphics.drawLine(xBegin,heightBar,widthBar,heightBar);
	var msg="";
	msg=msg.concat(title[1]);
	graphics.drawString(msg,widthBar-30,heightBar);	
	
	graphics.setColor(bgColor);
	//graphics.drawLine(new Array(xBegin,xBegin,widthBar),new Array(yBegin,heightBar,heightBar));	
	graphics.drawLine(xBegin,yBegin,widthBar,heightBar);	
	graphics.drawLine(xBegin,yBegin+(heightBar/3),xBegin+2*(widthBar/3),heightBar);	
	graphics.drawLine(xBegin,yBegin+2*(heightBar/3),xBegin+(widthBar/3),heightBar);	
	drawLegende(realTitle,colors,graphics,heightBar,widthBar);
	graphics.paint();
}


function drawContexte4(realTitle,valuesUser,valuesAverage,colors,divname){
	//y et x en haut a gauche 0,0
	var graphics = new jsGraphics(divname);	
	graphics.setPrintable(true);
	var bgColor="#CCCCCC";
	var widthBar=200;
	var heightBar=200;
	// add space for the text
	var ratio=(heightBar/2)/100;
	var xBegin=0;
	var yBegin=0;
	var title=formatTitle(realTitle);
//	graphics.fillRect(xBegin,yBegin,widthBar-xBegin,heightBar-yBegin);	
	graphics.setFont("arial","10px",Font.ITALIC_BOLD); 
	
	

	var arrayX=new Array(xBegin+(widthBar/2),
											valuesUser[1]*ratio+1+(widthBar/2),
											(widthBar/2),
											(widthBar/2)-(valuesUser[2]*ratio+1),
											xBegin+(widthBar/2)
											);

	var arrayY=new Array((heightBar/2)-valuesUser[0]*ratio,
											(heightBar/2),
											valuesUser[3]*ratio+(heightBar/2),
											(heightBar/2),
											(heightBar/2)-valuesUser[0]*ratio
											);

	var arrayAverageX=new Array(xBegin+(widthBar/2)+1,
											valuesAverage[1]*ratio+1+(widthBar/2),
											(widthBar/2)+1,
											(widthBar/2)-(valuesAverage[2]*ratio+1),
											xBegin+(widthBar/2)+1
											);

	var arrayAverageY=new Array((heightBar/2)-valuesAverage[0]*ratio,
											(heightBar/2),
											valuesAverage[3]*ratio+(heightBar/2),
											(heightBar/2),
											(heightBar/2)-valuesAverage[0]*ratio
											);

	graphics.setColor(colors[1]);
	graphics.fillPolygon(arrayX,arrayY);	
	graphics.setColor(colors[0]);
	graphics.setStroke(2);
	graphics.drawPolygon(arrayAverageX,arrayAverageY);
	graphics.setStroke(1);
	graphics.setColor("black");
	
	//graphics.drawLine(6,6,10,10);
	// axe y
	graphics.drawLine(xBegin+(widthBar/2),yBegin,xBegin+(widthBar/2),heightBar);
	var msg="";
	msg=msg.concat(title[0]);
	graphics.drawString(msg,xBegin+(widthBar/2)+10,yBegin);	
	var msg="";
	msg=msg.concat(title[3]);
	graphics.drawString(msg,xBegin+(widthBar/2)+10,heightBar-5);	
		
	//axe x
	graphics.drawLine(xBegin,heightBar/2,widthBar,heightBar/2);
	var msg="";
	msg=msg.concat(title[1]);
	graphics.drawString(msg,widthBar-30,heightBar/2+5);	
	var msg="";
	msg=msg.concat(title[2]);
	graphics.drawString(msg,xBegin,heightBar/2+5);	
	
	var aire=((valuesUser[1]+valuesUser[2])*valuesUser[0])/2+((valuesUser[1]+valuesUser[2])*valuesUser[3])/2;
	//var msg="aire : ";
	//msg=msg.concat(aire);
	//graphics.drawString(msg,xBegin+20,yBegin+20);

	graphics.setColor(bgColor);
	graphics.drawLine(xBegin+(widthBar/2),yBegin								,widthBar,heightBar/2);	
	graphics.drawLine(xBegin+(widthBar/2),yBegin+(heightBar/6)	,widthBar/2+xBegin+2*(widthBar/6),heightBar/2);	
	graphics.drawLine(xBegin+(widthBar/2),yBegin+2*(heightBar/6),widthBar/2+xBegin+(widthBar/6),heightBar/2);	
	graphics.drawLine(xBegin+(widthBar/2),yBegin								,xBegin,heightBar/2);	
	graphics.drawLine(xBegin+(widthBar/2),yBegin+(heightBar/6)	,xBegin+(widthBar/6),heightBar/2);	
	graphics.drawLine(xBegin+(widthBar/2),yBegin+2*(heightBar/6),xBegin+2*(widthBar/6),heightBar/2);	

	graphics.drawLine(xBegin+(widthBar/2),heightBar,widthBar,heightBar/2);	
	graphics.drawLine(xBegin+(widthBar/2),yBegin+heightBar/2+2*(heightBar/6)	,widthBar/2+xBegin+2*(widthBar/6),heightBar/2);	
	graphics.drawLine(xBegin+(widthBar/2),yBegin+heightBar/2+(heightBar/6),widthBar/2+xBegin+(widthBar/6),heightBar/2);	
	graphics.drawLine(xBegin+(widthBar/2),yBegin+heightBar					,xBegin,heightBar/2);	
	graphics.drawLine(xBegin+(widthBar/2),yBegin+heightBar/2+2*(heightBar/6)	,xBegin+(widthBar/6),heightBar/2);	
	graphics.drawLine(xBegin+(widthBar/2),yBegin+heightBar/2+(heightBar/6),xBegin+2*(widthBar/6),heightBar/2);	
	drawLegende(realTitle,colors,graphics,heightBar,widthBar);
	graphics.paint();
}


function drawContexte3(realTitle,valuesUser,valuesAverage,colors,divname){
	
	var graphics = new jsGraphics(divname);	
	graphics.setPrintable(true);
	var bgColor="#CCCCCC";
	var widthBar=200;
	var heightBar=100;
	// add space for the text
	var ratio=(heightBar)/100;
	var xBegin=0;
	var yBegin=0;
	var title=formatTitle(realTitle);
//	graphics.fillRect(xBegin,yBegin,widthBar-xBegin,heightBar-yBegin);	
	graphics.setFont("arial","10px",Font.ITALIC_BOLD); 
	
	

	var arrayX=new Array(xBegin+(widthBar/2),valuesUser[1]*ratio+1+(widthBar/2),(widthBar/2)-(valuesUser[2]*ratio+1));
	var arrayY=new Array(heightBar-valuesUser[0]*ratio,heightBar-1,heightBar-1);
	
	var arrayAverageX=new Array(xBegin+(widthBar/2),valuesAverage[1]*ratio+(widthBar/2)+1,(widthBar/2)-(valuesAverage[2]*ratio+1));
	var arrayAverageY=new Array(heightBar-valuesAverage[0]*ratio,heightBar-1,heightBar-1);
	

	graphics.setColor(colors[1]);
	graphics.fillPolygon(arrayX,arrayY);	
	
		graphics.setColor(colors[0]);
		graphics.setStroke(2);
	graphics.drawPolygon(arrayAverageX,arrayAverageY);
	graphics.setStroke(1);
	graphics.setColor("black");
	// axe y
	graphics.drawLine(xBegin+(widthBar/2),yBegin,xBegin+(widthBar/2),heightBar);
	var msg="";
	msg=msg.concat(title[0]);
	graphics.drawString(msg,xBegin+(widthBar/2)+10,0);	
	
	//axe x
	graphics.drawLine(xBegin,heightBar,widthBar,heightBar);
	var msg="";
	msg=msg.concat(title[1]);
	graphics.drawString(msg,widthBar-30,heightBar+5);	
	var msg="";
	msg=msg.concat(title[2]);
	graphics.drawString(msg,xBegin,heightBar+5);	
	
	var aire=((valuesUser[1]+valuesUser[2])*valuesUser[0])/2;
	//var msg="aire : ";
	//msg=msg.concat(aire);
	//graphics.drawString(msg,xBegin+20,yBegin+20);

	graphics.setColor(bgColor);
	graphics.drawLine(xBegin+(widthBar/2),yBegin,widthBar,heightBar);	
	graphics.drawLine(xBegin+(widthBar/2),yBegin+(heightBar/3),widthBar/2+xBegin+2*(widthBar/6),heightBar);	
	graphics.drawLine(xBegin+(widthBar/2),yBegin+2*(heightBar/3),widthBar/2+xBegin+(widthBar/6),heightBar);	
	graphics.drawLine(xBegin+(widthBar/2),yBegin,xBegin,heightBar);	
	graphics.drawLine(xBegin+(widthBar/2),yBegin+(heightBar/3),xBegin+(widthBar/6),heightBar);	
	graphics.drawLine(xBegin+(widthBar/2),yBegin+2*(heightBar/3),xBegin+2*(widthBar/6),heightBar);	
		
	drawLegende(realTitle,colors,graphics,heightBar,widthBar);
	graphics.paint();
}

function formatTitle(title){
	var newTitle=new Array();
	 for (var i = 0; i < title.length; i++) {
	 if(title[i].length>10){
	 newTitle[i]=(i+1)+": "+	title[i].substring(0,7)+"...";
	}	else {
		newTitle[i]=(i+1)+": "+title[i];
	 	
	}
}
return newTitle;
}


function drawLegende(title,colors,graphics,heightBar,widthBar){
		
	
	// add space for the text
	var ratio=(heightBar/2)/100;
	var xBegin=0;
	var yBegin=0;
//	var graphics = new jsGraphics(divname);	
					// LEXIQUE
				graphics.setColor("#000000"); // black
				graphics.drawString("Vue d'ensemble des compétences du candidat (graphique en toile d'araignée)",0,heightBar+20);
				
				//rectangle lexique
				var widthRectangle=20;
				var heightRectangle=10;
				var xRectangle=widthBar+10+50;
				var yRectangle=0;
					
			 graphics.setColor(colors[0]); // black
			 graphics.fillRect(xRectangle, yRectangle, widthRectangle, heightRectangle);
			 graphics.setColor("#000000"); // black
			 graphics.drawRect(xRectangle-1, yRectangle-1, widthRectangle+1, heightRectangle+1);
			 graphics.drawString("Moyenne",xRectangle+30,yRectangle);  
			 
			 yRectangle= yRectangle+20;
			 graphics.setColor(colors[1]); // black
			 graphics.fillRect(xRectangle, yRectangle, widthRectangle, heightRectangle);
			 graphics.setColor("#000000"); // black
			 graphics.drawRect(xRectangle-1, yRectangle-1, widthRectangle+1, heightRectangle+1);
			 graphics.drawString("Candidat",xRectangle+30,yRectangle);  
	
			graphics.setColor("#000000");
			for (var i = 0; i < title.length; i++) {
				yRectangle= yRectangle+20;
			  
			 graphics.drawString((i+1)+": "+title[i],xRectangle,yRectangle);  
				
			}	
			
}
