var questionsCount  = 1;
var answersCount = 2;
var rowCount = 1;

function updateValue (text, parentElt)
{
    if (text.length > 0) {
        var params  = { tex: text, wrap: "1" };

        var newImage    = document.createElement("img");
        newImage.setAttribute("alt","Math font");
        newImage.setAttribute("src","http://www.codecogs.com/gif.latex?" + text); 
		newImage.setAttribute("border", "none");

        document.getElementById(parentElt).getElementsByTagName('div')[1].innerHTML = "";        

        document.getElementById(parentElt).getElementsByTagName('div')[1].appendChild(newImage);
    }
}

function addTestQuestion()
{
	++questionsCount;
	++rowCount;
	answersCount = 1;

    var newSeparatorRow  = document.createElement("tr");
    newSeparatorRow.id = "sRow[" + questionsCount + "]";
	
	sCol1 = document.createElement("td");

	sCol2 = document.createElement("td");
    sDiv2 = document.createElement("div");
	sDiv2.className = "dashed_border";

	sCol2.appendChild(sDiv2);
	
	newSeparatorRow.appendChild(sCol1);
	newSeparatorRow.appendChild(sCol2);
	
    var newQuestionRow  = document.createElement("tr");
    newQuestionRow.id = "qRow[" + questionsCount + "]";
    newQuestionRow.className = "question_row";
	
    // question number
    var qCol1 = document.createElement("td");
    qCol1.innerHTML
        = "<span class=\"row_number\">" + rowCount + ".</span>";

	// input column
	var qCol3  =  document.createElement("td");
	qCol3.id = "question_" + questionsCount + "_question";
	var qDiv    = createDiv ( "question[" + questionsCount + "]", questionsCount, "question", "Question: " );
	qCol3.appendChild(qDiv);
    
	// action column
    var qCol4    = document.createElement("td");
    qCol4.className  = "action_column";
	var qImg1 = document.createElement("img");
	qImg1.setAttribute("src", "../images_ror/icons/close.gif");
	qImg1.onclick = function () { removeRowTest( questionsCount ) };
	qCol4.appendChild(qImg1);
		
	newQuestionRow.appendChild(qCol1);	
	newQuestionRow.appendChild(qCol3);	
	newQuestionRow.appendChild(qCol4);

    var newChoicesRow  = document.createElement("tr");
    newChoicesRow.id = "cRow[" + questionsCount + "]";
	
    var cCol1 = document.createElement("td");

    var cCol3  =  document.createElement("td");
	var cInp1;
    try{
		cInp1 = document.createElement('<input type="radio" name="choices_[' + questionsCount + ']" />');
	}catch(err){
		cInp1 = document.createElement('input');
		cInp1.setAttribute("type","radio");
	}
	/*var cInp1 = document.createElement("input");
	cInp1.setAttribute("name","choices_[" + questionsCount + "]");*/
	cInp1.setAttribute("id","choices_[" + questionsCount + "]_tf");
	cInp1.setAttribute("value",1);
	/*cInp1.setAttribute("type","radio");*/
	cInp1.onclick = function () { showAnswers(this, questionsCount,"question_" + questionsCount + "_add") };
	var cInp2;
    try{
		cInp2 = document.createElement('<input type="radio" name="choices_[' + questionsCount + ']" />');
	}catch(err){
		cInp2 = document.createElement('input');
		cInp2.setAttribute("type","radio");
	}
    /*var cInp2 = document.createElement("input");
	cInp2.setAttribute("name","choices_[" + questionsCount + "]");*/
	cInp2.setAttribute("id", "choices_[" + questionsCount + "]_mc");
	cInp2.setAttribute("value",2);
	/*cInp2.setAttribute("type","radio");*/
	cInp2.onclick = function () { showAnswers(this, questionsCount,"question_" + questionsCount + "_add") };
	var cLbl1 = document.createElement("label");
	cLbl1.setAttribute("for","choices_[" + questionsCount + "]_tf");
	cLbl1.innerHTML = "True/False";
	var cLbl2 = document.createElement("label");
	cLbl2.setAttribute("for","choices_[" + questionsCount + "]_mc");
	cLbl2.innerHTML = "Multiple Choice";
    cCol3.appendChild(cInp1);
    cCol3.appendChild(cLbl1);
	cCol3.appendChild(cInp2);
    cCol3.appendChild(cLbl2);
	
	newChoicesRow.appendChild(cCol1);	
	newChoicesRow.appendChild(cCol3);	

var newImageRow  = document.createElement("tr");
    newImageRow.id = "iRow[" + questionsCount + "]";
	
	iCol1 = document.createElement("td");

	iCol2 = document.createElement("td");
    iDiv2 = document.createElement("div");
	iDiv2.innerHTML = "Image File:<br/><input name='image[" + questionsCount + "]' type='file' />";

	iCol2.appendChild(iDiv2);
	
	newImageRow.appendChild(iCol1);
	newImageRow.appendChild(iCol2);

	var newAnswerRow1  = document.createElement("tr");
    newAnswerRow1.id = "aRow[" + questionsCount + "]";
    newAnswerRow1.className = "answers_row";
	
    // empty first col in answer row
    var aCol1 = document.createElement("td");

    // input column
    var aCol3    = document.createElement("td");
	aCol3.id = "question_" + questionsCount + "_answers";
    // wrong answer
	var aDiv1    = createDiv ( "question_" + questionsCount + "_answer[1]", questionsCount, "rightanswer", "Right Answer: " );
	aCol3.appendChild(aDiv1);
	
	++answersCount;

	var aDiv2    = createDiv ( "question_" + questionsCount + "_answer[2]", questionsCount, "wronganswer", "Wrong Answer(s): " );
	aCol3.appendChild(aDiv2);

	newAnswerRow1.appendChild(aCol1);	
	newAnswerRow1.appendChild(aCol3);	

	var newAnswerRow2  = document.createElement("tr");
    newAnswerRow2.id = "aRowTF[" + questionsCount + "]";
    newAnswerRow2.className = "answers_row";
	
    // empty first col in answer row
    var a2Col1 = document.createElement("td");

    // input column
    var a2Col2    = document.createElement("td");
	a2Col2.id = "question_" + questionsCount + "_answers_t_f";

    var a2Div1    =  document.createElement("div");
	a2Div1.id = "question_1_answer_tf[" + questionsCount + "]";
	a2Div1.innerHTML = "Right Answer:<br/>";
	var a2Lbl1 = document.createElement("label");
	a2Lbl1.setAttribute("for","true_rad[" + questionsCount + "]");
	a2Lbl1.innerHTML = "True";
	var a2Inp1 = document.createElement("input");
	a2Inp1.name = "question_1_answer_tf[" + questionsCount + "]";
	a2Inp1.type = "radio";
	a2Inp1.value = "true";
	a2Inp1.id = "true_rad[" + questionsCount + "]";
	var a2Lbl2 = document.createElement("label");
	a2Lbl2.setAttribute("for","false_rad[" + questionsCount + "]");
	a2Lbl2.innerHTML = "False";
	var a2Inp2 = document.createElement("input");
	a2Inp2.name = "question_1_answer_tf[" + questionsCount + "]";
	a2Inp2.type = "radio";
	a2Inp2.value = "false";
	a2Inp2.id = "false_rad[" + questionsCount + "]";
	a2Div1.appendChild(a2Inp1);
	a2Div1.appendChild(a2Lbl1);
	a2Div1.appendChild(a2Inp2);
	a2Div1.appendChild(a2Lbl2);
	a2Col2.appendChild(a2Div1);

	newAnswerRow2.appendChild(a2Col1);	
	newAnswerRow2.appendChild(a2Col2);	
	
	var newRequisitionRow  = document.createElement("tr");
    newRequisitionRow.id = "reqRow[" + questionsCount + "]";	
	reqCol1 = document.createElement("td");
	reqCol2 = document.createElement("td");
    reqDiv2 = document.createElement("div");
	reqDiv2.innerHTML = "Requisition Number:<br/><input name='requisition[" + questionsCount + "]' type='text' />";
	reqCol2.appendChild(reqDiv2);	
	newRequisitionRow.appendChild(reqCol1);
	newRequisitionRow.appendChild(reqCol2);

    document.getElementById('question_answer_table').getElementsByTagName('tbody')[0].appendChild(newSeparatorRow);
    document.getElementById('question_answer_table').getElementsByTagName('tbody')[0].appendChild(newQuestionRow);
    document.getElementById('question_answer_table').getElementsByTagName('tbody')[0].appendChild(newRequisitionRow);
    document.getElementById('question_answer_table').getElementsByTagName('tbody')[0].appendChild(newImageRow);
    document.getElementById('question_answer_table').getElementsByTagName('tbody')[0].appendChild(newChoicesRow);    
	document.getElementById('question_answer_table').getElementsByTagName('tbody')[0].appendChild(newAnswerRow1);
	document.getElementById('question_answer_table').getElementsByTagName('tbody')[0].appendChild(newAnswerRow2);
}


function addQuestion()
{
	++questionsCount;
	++rowCount;
	answersCount = 1;

    var newSeparatorRow  = document.createElement("tr");
    newSeparatorRow.id = "sRow[" + questionsCount + "]";
	
	sCol1 = document.createElement("td");

	sCol2 = document.createElement("td");
    sDiv2 = document.createElement("div");
	sDiv2.className = "dashed_border";

	sCol2.appendChild(sDiv2);
	
	newSeparatorRow.appendChild(sCol1);
	newSeparatorRow.appendChild(sCol2);
	
    var newQuestionRow  = document.createElement("tr");
    newQuestionRow.id = "qRow[" + questionsCount + "]";
    newQuestionRow.className = "question_row";
	
    // question number
    var qCol1 = document.createElement("td");
    qCol1.innerHTML
        = "<span class=\"row_number\">" + rowCount + ".</span>";

	// input column
	var qCol3  =  document.createElement("td");
	qCol3.id = "question_" + questionsCount + "_question";
	var qDiv    = createDiv ( "question[" + questionsCount + "]", questionsCount, "question", "Question: " );
	qCol3.appendChild(qDiv);
    
	// action column
    var qCol4    = document.createElement("td");
    qCol4.className  = "action_column";
	var qImg1 = document.createElement("img");
	qImg1.setAttribute("src", "../images_ror/icons/close.gif");
	qImg1.onclick = function () { removeRow( questionsCount ) };
	qCol4.appendChild(qImg1);
		
	newQuestionRow.appendChild(qCol1);	
	newQuestionRow.appendChild(qCol3);	
	newQuestionRow.appendChild(qCol4);

    var newChoicesRow  = document.createElement("tr");
    newChoicesRow.id = "cRow[" + questionsCount + "]";
	
    var cCol1 = document.createElement("td");

    var cCol3  =  document.createElement("td");
	var cInp1;
    try{
		cInp1 = document.createElement('<input type="radio" name="choices_[' + questionsCount + ']" />');
	}catch(err){
		cInp1 = document.createElement('input');
		cInp1.setAttribute("type","radio");
	}
	/*var cInp1 = document.createElement("input");
	cInp1.setAttribute("name","choices_[" + questionsCount + "]");*/
	cInp1.setAttribute("id","choices_[" + questionsCount + "]_tf");
	cInp1.setAttribute("value",1);
	/*cInp1.setAttribute("type","radio");*/
	cInp1.onclick = function () { showAnswers(this, questionsCount,"question_" + questionsCount + "_add") };
	var cInp2;
    try{
		cInp2 = document.createElement('<input type="radio" name="choices_[' + questionsCount + ']" />');
	}catch(err){
		cInp2 = document.createElement('input');
		cInp2.setAttribute("type","radio");
	}
    /*var cInp2 = document.createElement("input");
	cInp2.setAttribute("name","choices_[" + questionsCount + "]");*/
	cInp2.setAttribute("id", "choices_[" + questionsCount + "]_mc");
	cInp2.setAttribute("value",2);
	/*cInp2.setAttribute("type","radio");*/
	cInp2.onclick = function () { showAnswers(this, questionsCount,"question_" + questionsCount + "_add") };
	var cLbl1 = document.createElement("label");
	cLbl1.setAttribute("for","choices_[" + questionsCount + "]_tf");
	cLbl1.innerHTML = "True/False";
	var cLbl2 = document.createElement("label");
	cLbl2.setAttribute("for","choices_[" + questionsCount + "]_mc");
	cLbl2.innerHTML = "Multiple Choice";
    cCol3.appendChild(cInp1);
    cCol3.appendChild(cLbl1);
	cCol3.appendChild(cInp2);
    cCol3.appendChild(cLbl2);
	
	newChoicesRow.appendChild(cCol1);	
	newChoicesRow.appendChild(cCol3);	

var newImageRow  = document.createElement("tr");
    newImageRow.id = "iRow[" + questionsCount + "]";
	
	iCol1 = document.createElement("td");

	iCol2 = document.createElement("td");
    iDiv2 = document.createElement("div");
	iDiv2.innerHTML = "Image File:<br/><input name='image[" + questionsCount + "]' type='file' />";

	iCol2.appendChild(iDiv2);
	
	newImageRow.appendChild(iCol1);
	newImageRow.appendChild(iCol2);

	var newAnswerRow1  = document.createElement("tr");
    newAnswerRow1.id = "aRow[" + questionsCount + "]";
    newAnswerRow1.className = "answers_row";
	
    // empty first col in answer row
    var aCol1 = document.createElement("td");

    // input column
    var aCol3    = document.createElement("td");
	aCol3.id = "question_" + questionsCount + "_answers";
    // wrong answer
	var aDiv1    = createDiv ( "question_" + questionsCount + "_answer[1]", questionsCount, "rightanswer", "Right Answer: " );
	aCol3.appendChild(aDiv1);
	
	++answersCount;

	var aDiv2    = createDiv ( "question_" + questionsCount + "_answer[2]", questionsCount, "wronganswer", "Wrong Answer(s): " );
	aCol3.appendChild(aDiv2);

	newAnswerRow1.appendChild(aCol1);	
	newAnswerRow1.appendChild(aCol3);	

	var newAnswerRow2  = document.createElement("tr");
    newAnswerRow2.id = "aRowTF[" + questionsCount + "]";
    newAnswerRow2.className = "answers_row";
	
    // empty first col in answer row
    var a2Col1 = document.createElement("td");

    // input column
    var a2Col2    = document.createElement("td");
	a2Col2.id = "question_" + questionsCount + "_answers_t_f";

    var a2Div1    =  document.createElement("div");
	a2Div1.id = "question_1_answer_tf[" + questionsCount + "]";
	a2Div1.innerHTML = "Right Answer:<br/>";
	var a2Lbl1 = document.createElement("label");
	a2Lbl1.setAttribute("for","true_rad[" + questionsCount + "]");
	a2Lbl1.innerHTML = "True";
	var a2Inp1 = document.createElement("input");
	a2Inp1.name = "question_1_answer_tf[" + questionsCount + "]";
	a2Inp1.type = "radio";
	a2Inp1.value = "true";
	a2Inp1.id = "true_rad[" + questionsCount + "]";
	var a2Lbl2 = document.createElement("label");
	a2Lbl2.setAttribute("for","false_rad[" + questionsCount + "]");
	a2Lbl2.innerHTML = "False";
	var a2Inp2 = document.createElement("input");
	a2Inp2.name = "question_1_answer_tf[" + questionsCount + "]";
	a2Inp2.type = "radio";
	a2Inp2.value = "false";
	a2Inp2.id = "false_rad[" + questionsCount + "]";
	a2Div1.appendChild(a2Inp1);
	a2Div1.appendChild(a2Lbl1);
	a2Div1.appendChild(a2Inp2);
	a2Div1.appendChild(a2Lbl2);
	a2Col2.appendChild(a2Div1);

	newAnswerRow2.appendChild(a2Col1);	
	newAnswerRow2.appendChild(a2Col2);	

    document.getElementById('question_answer_table').getElementsByTagName('tbody')[0].appendChild(newSeparatorRow);
    document.getElementById('question_answer_table').getElementsByTagName('tbody')[0].appendChild(newQuestionRow);
    document.getElementById('question_answer_table').getElementsByTagName('tbody')[0].appendChild(newImageRow);
    document.getElementById('question_answer_table').getElementsByTagName('tbody')[0].appendChild(newChoicesRow);    
	document.getElementById('question_answer_table').getElementsByTagName('tbody')[0].appendChild(newAnswerRow1);
	document.getElementById('question_answer_table').getElementsByTagName('tbody')[0].appendChild(newAnswerRow2);
}

function addAnswer(rowId)
{
	++answersCount;
	
	var answerDivs = document.getElementById("question_" + rowId + "_answers").getElementsByTagName('div');
	var answerCount = 0;
    for (var i = 0; i < answerDivs.length; i++) {
	    if (answerDivs[i].className == "answer_row") {
		    ++answerCount;	
		}
	}
	var nextAns = ++answerCount;

    var newAnswerDiv1 = createDiv( "question_" + rowId + "_answer[" + nextAns + "]", rowId, "distractor", "Wrong Answer: ");
    document.getElementById("question_" + rowId + "_answers").appendChild(newAnswerDiv1);

}

function addDistractor ()
{
    if (distractorColumnNumbers.length == 0) {
        var distractorColumnHead        = document.createElement("th");
        distractorColumnHead.id         = "distractor-column-head";
        distractorColumnHead.innerHTML  = "Distractors";

        $("#answer-table thead tr .action-column").before(distractorColumnHead);
    }

    ++distractorColumnCount;

    $("#distractor-column-head").get(0).colSpan = distractorColumnCount;

    for (var i = 1; i <= answerRowCount; ++i) {
        var newDistractorColumn
            = createColumn("distractor-" + i + "-" + distractorColumnCount);

        $("#row-" + i + " .action-column").before(newDistractorColumn);
    }

    var newDistractorFoot = document.createElement("td");
    newDistractorFoot.id    = "foot-" + distractorColumnCount;
    newDistractorFoot.className = "distractor-foot";

    newDistractorFoot.innerHTML
        = "<img src=\"/images/icons/16x16/close.gif\" alt=\"Remove distractor\" onclick=\"removeDistractorColumn(" + distractorColumnCount + ");\"/>";

    $("#answer-table tfoot .action-column").before(newDistractorFoot);

    distractorColumnNumbers.push(distractorColumnCount);

    updateDroppableAreas();
}


function createDiv (divId, rowId, type, label)
{
    var div  = document.createElement("div");
    div.id   = divId;
	div.className = "answer_row";
	
	var innerDiv1 = document.createElement("div");
	innerDiv1.className = "value_input";
	
	var lbl = document.createElement("label");
	lbl.className = "quiz_label";
	lbl.innerHTML = label;
	
	var inp = document.createElement("input");
	inp.name = divId;
	inp.className = "quiz_input";
	inp.size = 20;
	inp.onchange = function () { updateValue( this.value, divId  ) };
	inp.onblur = function () { if (this.value != '') { showImageContainer( divId ) } };

    innerDiv1.appendChild(inp);
	
	var img1 = document.createElement("img");
	var img2 = document.createElement("img");
	if (type == "distractor") {
	    img1.setAttribute("src", "../images_ror/icons/check.gif");
        img1.onclick = function () { showImageContainer( divId ) };
    	innerDiv1.appendChild(img1);
	    img2.setAttribute("src", "../images_ror/icons/close.gif");
        img2.onclick = function () { removeAnswer( divId ) };
    	innerDiv1.appendChild(img2);
	} 
	if (type == "question") {
	    img1.setAttribute("src", "../images_ror/icons/check.gif");
        img1.onclick = function () { showImageContainer( divId ) };
    	innerDiv1.appendChild(img1);
//	    img2.setAttribute("src", "../images_ror/icons/close.gif");
//      img2.onclick = function () { removeRow( rowId ) };
//		innerDiv1.appendChild(img2);
        div.appendChild(lbl);
    } 
	if (type == "wronganswer") {
	    img1.setAttribute("src", "../images_ror/icons/check.gif");
        img1.onclick = function () { showImageContainer( divId ) };
    	innerDiv1.appendChild(img1);
	    img2.setAttribute("src", "../images_ror/icons/add.gif");
        img2.onclick = function () { addAnswer( rowId ) };
		img2.id = "question_" + questionsCount + "_add";
    	innerDiv1.appendChild(img2);
        div.appendChild(lbl);
	}
	if (type == "rightanswer") {
	    img1.setAttribute("src", "../images_ror/icons/check.gif");
        img1.onclick = function () { showImageContainer( divId ) };
    	innerDiv1.appendChild(img1);
        div.appendChild(lbl);
	}

	var innerDiv2 = document.createElement("div");
	innerDiv2.className = "image_container";
	innerDiv2.onclick = function () { showValueInput ( divId ) };

	if (type == "wronganswer") {
	    var img2 = document.createElement("img");
	    img2.setAttribute("src", "../images_ror/icons/add.gif");
        img2.onclick = function () { addAnswer( rowId ) };
		img2.id = "question_" + questionsCount + "_add";
    	innerDiv2.appendChild(img2);
	}

	var lbl2 = document.createElement("label");
	lbl2.className = "quiz_label";
	lbl2.innerHTML = label;

    innerDiv2.appendChild(lbl2);

	div.appendChild(innerDiv1);
	div.appendChild(innerDiv2);

    if (type != "question") {
		var innerDiv3 = document.createElement("div");
		innerDiv3.className = "value_input";
	
		var inp3 = document.createElement("input");
		inp3.name = "question_" + questionsCount + "_image_answer[" + answersCount + "]";
		inp3.setAttribute('type','file');
	
		innerDiv3.appendChild(inp3);
	
		div.appendChild(innerDiv3);
	}

    return div;
}

function showValueInput ( parentElt )
{
    document.getElementById(parentElt).getElementsByTagName('div')[0].style.display = 'block';
    document.getElementById(parentElt).getElementsByTagName('div')[1].style.display = 'none';
    document.getElementById(parentElt).getElementsByTagName('input')[0].focus();
}

function showImageContainer (parentElt)
{
	if (document.getElementById(parentElt).getElementsByTagName('div')[0].getElementsByTagName('input')[0].value != "") {
		updateValue(document.getElementById(parentElt).getElementsByTagName('div')[0].getElementsByTagName('input')[0].value, parentElt);
        document.getElementById(parentElt).getElementsByTagName('div')[0].style.display = 'none';
        document.getElementById(parentElt).getElementsByTagName('div')[1].style.display = 'block';
	}
}

function removeRowTest (rowId)
{
	var qRowToRemove = document.getElementById("qRow[" + rowId + "]");
	var cRowToRemove = document.getElementById("cRow[" + rowId + "]");
	var iRowToRemove = document.getElementById("iRow[" + rowId + "]");
	var aRowToRemove = document.getElementById("aRow[" + rowId + "]");
	var sRowToRemove = document.getElementById("sRow[" + rowId + "]");
	var reqRowToRemove = document.getElementById("reqRow[" + rowId + "]");
    if (rowCount > 1) {
        qRowToRemove.parentNode.removeChild(qRowToRemove);
        cRowToRemove.parentNode.removeChild(cRowToRemove);
        iRowToRemove.parentNode.removeChild(iRowToRemove);
        aRowToRemove.parentNode.removeChild(aRowToRemove);
        sRowToRemove.parentNode.removeChild(sRowToRemove);
        reqRowToRemove.parentNode.removeChild(reqRowToRemove);

        fixRowNumbers();

        --rowCount;
    }
}

function removeRow (rowId)
{
	var qRowToRemove = document.getElementById("qRow[" + rowId + "]");
	var cRowToRemove = document.getElementById("cRow[" + rowId + "]");
	var iRowToRemove = document.getElementById("iRow[" + rowId + "]");
	var aRowToRemove = document.getElementById("aRow[" + rowId + "]");
	var sRowToRemove = document.getElementById("sRow[" + rowId + "]");
	
    if (rowCount > 1) {
        qRowToRemove.parentNode.removeChild(qRowToRemove);
        cRowToRemove.parentNode.removeChild(cRowToRemove);
        iRowToRemove.parentNode.removeChild(iRowToRemove);
        aRowToRemove.parentNode.removeChild(aRowToRemove);
        sRowToRemove.parentNode.removeChild(sRowToRemove);        

        fixRowNumbers();

        --rowCount;
    }
}

function removeAnswer(ansId)
{
	var dRow = document.getElementById(ansId).parentNode.removeChild(document.getElementById(ansId));
}

function fixRowNumbers ()
{
    var rowNumber   = 0;

    var rows = document.getElementById('question_answer_table').getElementsByTagName('tr');
    for (var i = 0; i < rows.length; i++) {
        if (rows[i].className == "question_row") {
            ++rowNumber;
			var tds = rows[i].getElementsByTagName('td');
            tds[0].innerHTML = "<span class=\"row_number\">" + rowNumber + ".</span>";
			tds[1].getElementsByTagName('input')[0].name = "question[" + rowNumber + "]";
		}
        if (rows[i].className == "answers_row") {
            var inps = rows[i].getElementsByTagName('input');
			for (var j = 0; j < inps.length; j++) {
				inps[j].name = "question_" + rowNumber + "_answer[" + (j + 1) + "]";
			}			
		}
		//alert("row: " + rowNumber + "     " + rows[i].innerHTML)
    }
}

function updateAvailableImages ()
{
    $.get(
        "/professor/quizzes/quiz-image/",
        {},
        function (data) {
            $("#available-images").html(data);
            $("#available-images .quiz-image").draggable({ helper: "clone" });

            updateDroppableAreas();
        }
    );
}

function updateDroppableAreas ()
{
    $("#answer-table :input,#problem :input").droppable({
        accept: ".quiz-image",
        drop: function (e, ui) {
            this.value += "::IMAGE(" + ui.draggable.get(0).alt + ")::";
            $(this).focus();
            $(this).change();
            $(this).blur();
        }
    });
}

function updateAllValues ()
{
	var tblInp = document.getElementById("question_answer_table").getElementsByTagName('input');
	
	for (var i = 0; i < tblInp.length; i++) {
	    if (tblInp[i].className == "quiz_input") {
			if (tblInp[i].value != '') {
			    tblInp[i].focus();
			    tblInp[i].blur();
			}
		}
	}
}

function showAnswers(rad,id,addbutton)
{
	var strDisplayTableRow = "block";
    if (navigator.appName.indexOf("Netscape") > -1)
    {
       strDisplayTableRow = "table-row";
    }

    if (rad.value == 1)
	{
	    document.getElementById("aRow[" + id + "]").style.display = "none";
	    document.getElementById("aRowTF[" + id + "]").style.display = strDisplayTableRow;
	    document.getElementById("iRow[" + id + "]").style.display = strDisplayTableRow;
		document.getElementById(addbutton).style.display = "none";
	}
	if (rad.value == 2)
	{
	    document.getElementById("aRow[" + id + "]").style.display = strDisplayTableRow;
	    document.getElementById("aRowTF[" + id + "]").style.display = "none";
	    document.getElementById("iRow[" + id + "]").style.display = strDisplayTableRow;
		document.getElementById(addbutton).style.display = "inline";
	}
}