//-----------------------------------------------
//clase grupo de preguntas que contiene preguntas, placeholderid div donde se pintaran las preguntas, placeholder
//summary donde se sacara el summario de respuestas. Se puede usar el mismo
//-----------------------------------------------
function Questions(grupoid,placeholderid, placeholdersummary)
{
	this.Questions=[];
	this.GrupoID=grupoid;
	this.SelectedQuestion="";
	this.PlaceHolder=document.getElementById(placeholderid);
	this.PlaceHolderSummary=document.getElementById(placeholdersummary);

}
	Questions.prototype.Add = function (question)	//Metodo para aņadir preguntas
	{
		var newindex=this.Questions.length;
		this.Questions[newindex]=question;
	}
	Questions.prototype.render = function () //render all questions
	{ 
		
	
		
		for(i=0;i<this.Questions.length;i++) 
		{
			var TABLE=document.createElement("TABLE");
			TABLE.width=501;
			TABLE.className="Info_MOT_txtTable";
			var TBODY=document.createElement("TBODY");
		
			var row=document.createElement("TR");
			var col=document.createElement("TD");
			col.className="Info_MOT_txtTable";
			col.innerHTML="<img src=images/"+(i+1).toString()+".gif>"+this.Questions[i].Literal;
			//alert(this.Questions[i].Answers.length); //ojo, da un Invalid argument si vale 0 this.Questions[i].Answers.length
			col.colSpan=2;
			row.appendChild(col);
			TBODY.appendChild(row);
			TABLE.appendChild(TBODY);
			
			for(j=0;j<this.Questions[i].Answers.length;j++)
			{
				row=document.createElement("TR");
				col=document.createElement("TD");
				col.width=492;
				col.className="Info_MOT_decoTd";
				col.innerHTML="<img src=\"file://///Spa-apps01/inetpub/ocu/site_images/inf_MOT/listArrow.gif\" width=\"5\" height=\"13\" hspace=\"12\">"+this.Questions[i].Answers[j].Literal;
				row.appendChild(col);
				col=document.createElement("TD");
				this.Questions[i].Answers[j].SetRadioButton(this.GrupoID, "radio"+i.toString()+j.toString());
				col.width=21;
				col.appendChild(this.Questions[i].Answers[j].Radio);
				row.appendChild(col);	 
				TBODY.appendChild(row);	
				this.PlaceHolder.appendChild(TABLE);	
			}
			var TABLE2=document.createElement("TABLE");
			var TBODY2=document.createElement("TBODY");
			var summaryrow=document.createElement("TR");
			var summarycol=document.createElement("TD");
			summarycol.id="summary"+i.toString();
			summaryrow.appendChild(summarycol);
			TBODY2.appendChild(summaryrow);
			TABLE2.appendChild(TBODY2);
			this.PlaceHolder.appendChild(TABLE2);
		}
	}

	/*Questions.prototype.ShowSummary = function ()
	{
		var obj=this;
		//this.Questions[i].Answers[j].Radio.onclick=function(e){obj.ShowSummary()};	
		this.SelectedQuestion=radio.name;
		alert(this.SelectedQuestion);
	}*/
	

//-----------------------------------------------
//clase preguntas contiene un array de respuestas.
//-----------------------------------------------
function Question(questionid,literal)
{
	this.QuestionID=questionid;
	this.Literal=literal;
	this.Answers=[];
}
	Question.prototype.Add = function (answer)	//Metodo para aņadir respuestas a la pregunta
	{
		var newindex=this.Answers.length;
		this.Answers[newindex]=answer;
	}
	
	
//-----------------------------------------------
//Clase Respuesta
//-----------------------------------------------
function Answer(literal, percentaje, question)
{
	this.Question=question; //question en la que estoy
	this.Literal=literal;
	this.Percentaje=percentaje;
	this.Radio=document.createElement("input");	
}
	Answer.prototype.DebugRadios = function ()
	{
		//alert("Percentaje:"+this.Percentaje+"\n\rNombre:"+this.Radio.name+"\n\r");
	}
	Answer.prototype.SetRadioButton = function (name, id) //Esto es por el bug, no vale create element + setattribute ('name', value)
	{
		var obj=this;
		this.Radio=document.createElement("<INPUT TYPE=RADIO NAME="+name+">");
		this.Radio.ID=id;
		this.AnswerID=this.Radio.ID.split('radio');
		this.Radio.onclick=function(e){obj.ShowSummary()};	
	}
	Answer.prototype.ShowSummary = function () //mostrar el resultado
	{
		var summaryobj=document.getElementById("summary"+this.Question.QuestionID);
		//clean child nodes
		for(i=0;i<summaryobj.childNodes.length;i++)
		{
			summaryobj.removeChild(summaryobj.childNodes.item(i))
		}
		
		//remove negrita de las demas preguntas
		for(i=0;i<this.Question.Answers.length;i++)
		{
			this.Question.Answers[i].Literal=this.Question.Answers[i].Literal.replace('<b>','');
			this.Question.Answers[i].Literal=this.Question.Answers[i].Literal.replace('</b>','');
			this.Question.Answers[i].Percentaje=this.Question.Answers[i].Percentaje.replace('</b>','');
			this.Question.Answers[i].Percentaje=this.Question.Answers[i].Percentaje.replace('<b>','');
		}
		
		var table=document.createElement("Table");
		table.className="Info_MOT_txtTable";
		table.width=501;
		var tbody=document.createElement("tbody");
		this.Literal="<b>"+this.Literal+"</b>";
		this.Percentaje="<b>"+this.Percentaje+"</b>";
		var row=document.createElement("TR");
		row.className="Info_MOT_bgtd";
		var col=document.createElement("TD");
		col.className="Info_MOT_menuSubLink";
		col.align="center"
		col.innerHTML="<b>Frecuencia</b>";
		col.width=334;
		row.appendChild(col);
		var col=document.createElement("TD");
		col.width=167;
		col.align="center"
		col.innerHTML="<b>Respuesta de los conductores espa&ntilde;oles</b>";
		row.appendChild(col);
		tbody.appendChild(row);
		    
		
		for(i=0;i<this.Question.Answers.length;i++)
		{
			var row=document.createElement("TR");
			var col=document.createElement("TD");
			col.className="Info_MOT_decoTd";
			var literal=this.Question.Answers[i].Literal.replace("<b>","");
			col.innerHTML="<img src=\"file://///Spa-apps01/inetpub/ocu/site_images/inf_MOT/listArrow.gif\" width=\"5\" height=\"13\" hspace=\"12\">"+this.Question.Answers[i].Literal;
			row.appendChild(col);
			var col=document.createElement("TD");
			col.align="center";
			col.innerHTML=this.Question.Answers[i].Percentaje;
			row.appendChild(col);
			tbody.appendChild(row);
		}
		table.appendChild(tbody)
		summaryobj.appendChild(table);
	}
	
	
///Static	
function ShowSummary(e,args)
{
	  var targ;

      if (!e) var e = window.event;

      if (e.target) targ = e.target;

      else if (e.srcElement) targ = e.srcElement;

	 // alert(args.getAttribute("RowID"));

}	
