$(function() {
    	var oldAnswerSet='';
    	var oldQuestionNumber='';
    	var currentAnswerSet='';
    	var arrayofsameQuestions=new Array();
    	var arrayofsameAnswerLabels=new Array();
    	$('div.multichoice').each(function(){
    		//alert('id: '+$(this).attr('id'));
    		var mcJOS;
    		mcJOS='div#'+$(this).attr('id')+' div table';
    		currentAnswerSet=$(mcJOS).text();

    		if(oldAnswerSet==''){
    			oldAnswerSet=$(mcJOS).text();
    			oldQuestionNumber=$(this).attr('id');
    		}else if(currentAnswerSet!=oldAnswerSet){
    			oldAnswerSet=oldAnswerSet;
    		}else if(currentAnswerSet==oldAnswerSet){
    			oldAnswerSet=oldAnswerSet;
    			//alert($(this).attr('id'));
    			//arrayofsameQuestions[(arrayofsameQuestions.length)]=oldQuestionNumber;
    			arrayofsameQuestions[(arrayofsameQuestions.length)]=$(this).attr('id');
    		}
    	});
    	if(arrayofsameQuestions.length>1){
    		//FIX by including the first question
    		arrayofsameQuestions[(arrayofsameQuestions.length)]=oldQuestionNumber
    		//Create the header
    		
    		var selectorOfAnswer='#'+arrayofsameQuestions[0]+' div.content div.ablock table.answer input';    		
    		var numberOfMultiChoice=$(selectorOfAnswer).length;
    		
    		var headerContent='<div id="rcp_answerHeaderMultichoice"  class="que multichoice clearfix"><div class="info">&nbsp;</div><div  class="content"><div class="ablock clearfix"><div class="prompt">&nbsp;</div><table class="answer"><tr>';
    		var widthsize=Math.round(100/numberOfMultiChoice);
    		//widthsize=$().
    		$('#'+oldQuestionNumber+' div div table tr td label').each(function(){
    			arrayofsameAnswerLabels[(arrayofsameAnswerLabels.length)]=$(this).text();
    			headerContent +='<td width="'+widthsize+'%">'+$(this).text().trim().replace(' ','<br/>')+'</td>';
    		})
    		headerContent +='</tr></table></div></div></div>';
    		//alert(headerContent);
    		$('#'+oldQuestionNumber).before(headerContent);
    		$('div#rcp_answerHeaderMultichoice').css('font-weight','bold');

    		//Replace table for each existing answer set row
    		var selector='';
    		var tableSelector='';
    		var inputElement= new Array();
    		for(var i=0;i<arrayofsameQuestions.length;i++){
    			inputElement= new Array();
    			selector='div#'+arrayofsameQuestions[i]+' div.content div table.answer tr td input';
    			//alert(selector);


    			tableSelector='div#'+arrayofsameQuestions[i]+' div.content div table.answer';
    			var newTableContent='';
    			newTableContent='<table class="answer"><tr>';


    			$(selector).each(function(){
    				//alert($(this).attr('id'));
    				//    				var inputEL= document.getElementById($(this).attr('id')).innerHTML;
    				//    				alert(inputEL);
    				inputElement[inputElement.length]=$(this).html();
    				newTableContent +='<td width="'+widthsize+'%"><input type="radio" id="'+$(this).attr('id')+'" name="'+$(this).attr('name')+'" value="'+$(this).attr('value')+'"';
    				if($(this).attr('checked')!='' && $(this).attr('checked')!=undefined ){
    					newTableContent +=' checked="checked" ';
    				}
    				newTableContent +='/></td>';


    			});
    			newTableContent+='</tr></table>';
    			//alert(newTableContent);
    			$(tableSelector).replaceWith(newTableContent)
    			//alert(printarray(inputElement));
    		}



    	}

    });

    String.prototype.trim = function() {
    	return this.replace(/^\s+|\s+$/g,"");
    }
    String.prototype.ltrim = function() {
    	return this.replace(/^\s+/,"");
    }
    String.prototype.rtrim = function() {
    	return this.replace(/\s+$/,"");
    }


    function printarray(vararray){
    	var result='';
    	for(var i=0;i<vararray.length;i++){
    		result +=vararray[i];
    	}
    	return result;
    }
