/* $Id: assessmentItemGapMatch.js 55846 2009-11-06 09:55:39Z johnarne $ */

function createFullXML(){
    getGapTexts();
    var theXML = '<assessmentItem xmlns="http://www.imsglobal.org/xsd/imsqti_v2p1" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.imsglobal.org/xsd/imsqti_v2p1 imsqti_v2p1.xsd" identifier="replaceWithIdent" title="'+legalXMLAttribute(document.getElementById("questionTitle").value)+'" adaptive="false" timeDependent="false" xmlns:inspera="http://www.inspera.no">\n';
	theXML += getGapMatchResponseDeclarationXML();
	theXML += getHintsResponseDeclarationXML(document.getElementById('hintsTable'));
	theXML += getOutcomeDeclarationsXML();
    theXML += getScoreTemplateDeclarationsXML();
    theXML += '<itemBody>\n';
    theXML += getIllustrationXML();
    theXML += '<gapMatchInteraction responseIdentifier="RESPONSE" shuffle="false"'+getInteractionClass()+'>\n';
    //theXML += '<inspera:insperaAttributes freePlacing="'+document.getElementById("free_placing_gapimages").checked+'"/>\n';
    theXML += getPromptXML();
    backgroundImageObject._class = "background";
    theXML += toXMLTransferObject(backgroundImageObject)+'\n';
    theXML += getGapTextsXML();
//    theXML += document.getElementById('gapMatchInteractionBlockqoute').innerHTML+'\n';

    var l_object = flexApp.getBlockquote();
    //alert('ww e '+flexApp.getBlockquote().style);
    var blockQuoteText = l_object.text_value;
    if (blockQuoteText == '<blockquote><p/></blockquote>') blockQuoteText = '<blockquote></blockquote>';
    //alert('l_object.text_value '+l_object.text_value);

    // <p/> is written as <p> in textarea (gapMatchInteractionBlockqoute), which causes trouble
    //blockQuoteText = replaceAllStr(blockQuoteText, "<p/>", "<p> </p>");

    //alert("blockQuoteText"+blockQuoteText);
    if(blockQuoteText == "<blockquote/>"){
        blockQuoteText = "</blockquote>";    
    }else{
        blockQuoteText = blockQuoteText.substring("<blockquote>".length, blockQuoteText.length);
    }
    blockQuoteText = '<blockquote style="'+l_object.style+'">'+blockQuoteText;
    //alert('blockQuoteText='+blockQuoteText);
    theXML += blockQuoteText
    theXML += '</gapMatchInteraction>\n';
    theXML += getHintsXML(document.getElementById('hintsTable'));
    theXML += '</itemBody>\n';
    theXML += getMatchResponseProcessingXML();
    theXML += getStandardFeedbackXML();
    theXML += getFeedbackTag('feedback_partially_correct');
    theXML += getSolutionFeedback();
    theXML += "</assessmentItem>\n";

    //alert(theXML);
	return theXML;
}

/**
*   Fill gapObjects_js and correctResponses (if not provided for swf)
*/
function getGapTexts(){
    var l_gapObjects = flexApp.getGapObjects();
    correctResponses = [];
    gapObjects_js = [];
    for(var i=0; i < l_gapObjects.length; i++){
        var tObject = new TransferObject('gapText');
        var l_object = l_gapObjects[i];
        for(var name in l_object){
            tObject[name] = l_object[name];
        }
        gapObjects_js[tObject.identifier] = tObject;
        //correctResponses[correctResponses.length] = tObject.identifier+' '+tObject.identifier.substring(0, tObject.identifier.lastIndexOf("_text"));
    }
    correctResponses = flexApp.getResponseMappings();
    //alert("got responsemappings "+correctResponses);
    //onChangeResponseMappings(null);
}



function getGapTextsXML(){
    var theXML = '';
    for(var i in gapObjects_js){
        if(gapObjects_js[i] instanceof TransferObject){
            theXML += toXMLTransferObject(gapObjects_js[i]);
        }
    }
    
    return theXML;
}

function onChangeGapObjects(a_event){
	document.getElementById("output").innerHTML = "";
	var l_gapObjects = flexApp.getGapObjects();
	for(var i=0; i < l_gapObjects.length; i++){
		var l_object = l_gapObjects[i];
		document.getElementById("output").innerHTML += '\n' + "Changed " + l_gapObjects[i].identifier + ":";
		for(var name in l_object){
			document.getElementById("output").innerHTML += '\n' + "gapObjects[" + l_gapObjects[i].identifier + "]." + name + " = " + l_object[name];
		}
	}
}

function onChangeBlockquote(a_event){
	var l_object = flexApp.getBlockquote();
	document.getElementById("output").innerHTML = "Changed blockquote:";
	for(var name in l_object){
		document.getElementById("output").innerHTML += '\n' + "blockquote." + name + " = " + l_object[name];
	}
}


function onChangeResponseMappings(a_event){
	var l_responseMappings = flexApp.getResponseMappings();
//    alert('ere noe '+l_responseMappings);
    for(var i=0; i < l_responseMappings.length; i++){
		var l_object = l_responseMappings[i];
		document.getElementById("output").innerHTML += '\n' + "Mapping #" + i + ": " + l_responseMappings[i];
	}
}



function replaceEmptyPTags(original){
    original = replaceAllStr(original, "GAP", "gap");
    original = replaceAllStr(original, "<p> </p>", "<p/>");
    original = replaceAllStr(original, "<p> </p>", "<P/>");
    original = replaceAllStr(original, "P>", "p>");
    original = replaceAllStr(original, "BLOCKQUOTE", "blockquote");
    return original;
}

