           insrtHTML += '<option id="'+language.name+'-option" value="'+language.name+'-txt">'+language.name+'</option>'
       $('#lang-select').html insrtHTML
       hideShow 'language-options','initial-text'
-    switchTextBox = ->
-      newLang = $('#lang-select',popup).val()
+
+
+    $('#Submit-button,#finalSubmit',popup).click ()=>
+      finXML = '<problem><text>'
+      isCodeSnippet= $('#isCodeSnippet',popup)[0].checked
+      for segment in segments
+        text= $("##{segment}-text",popup)[0].value
+        if(text.length>0)
+          finXML += '<h1><b>'+segment+'</b></h1> '+parseInput text
+      finXML += '</text> <select class="lang-options" style="margin-left:90%">'
+      checkedLang=[]
+      for language in languages
+        if($("##{language.name}-select",popup)[0].checked)
+          checkedLang.push(language)
+          finXML+='<option value="'+language.mode+'">'+language.name+'</option> '
+      finXML+='</select>'
+      if(isCodeSnippet)
+        finXML+='<div>'
+        for language in checkedLang 
+          finXML+='<div class="code-snippet code-stub" id="'+language.name+
+              '" data-mode="'+language.mode+'" style="background-color:#000"  ><![CDATA['+
+              $("##{language.name}-txt1",popup)[0].value+']]></div>'
+        finXML += '</div>'
+      finXML += '<span></span>
+      <coderesponse queuename="cpp-queue">
+        <textbox mode="'+(checkedLang[0]).mode+'" tabsize="4" />
+        <codeparam>
+          <initial_display><![CDATA[
+    ]]> </initial_display>
+          <grader_payload>
+    {"problem_name": "Lecture2Problem1"}
+        </grader_payload>
+        </codeparam>
+      </coderesponse>'
+      if(isCodeSnippet)
+        finXML += '<div id="last-code-stub">'
+        for language in checkedLang
+          finXML += '<div class="code-snippet code-stub" id="'+language.name+
+              '" data-mode="'+language.mode+'" style="background-color:#000"  ><![CDATA['+
+              $("##{language.name}-txt3",popup)[0].value+']]></div>'
+        finXML+='</div>'
+      finXML+='</problem>'
+      @xml_editor.setValue finXML
+      popup.hide()
+      $('statement-inputs',popup).show()
+      $('language-options',popup).hide()
+      $('initial-text',popup).hide()
+      
+
+    $('#backToLangSelect',popup).click ()->
+      hideShow 'initial-text','language-options'
+
+    $('#lang-select',popup).change ()->
+      newLang = $('#lang-select',popup)[0].value
       hideShow curLang,newLang
       curLang = newLang
-
-    $('#goToLangSelect',popup).click langSelect
-        
-    $('#isCodeSnippet',popup).click ()->
-      if($('#isCodeSnippet',popup)[0].checked)
-        hideShow 'Submit-button','Next-button'
-      else
-        hideShow 'Next-button','Submit-button'
-    
-    $('#backToStatInpt',popup).click hideShow 'language-options','statement-inputs'
-    
-    $('#Next-button',popup).click initTextSec
-
-    $('#Submit-button,#finalSubmit',popup).click parse
-
-    $('#backToLangSelect',popup).click hideShow 'initial-text','language-options'
-
-    $('#lang-select',popup).change switchTextBox
 
     parseInput = (s)->
       isMathBlock = false;

          'Output',
          'Example',
          'Constraints']
    
    render = ->
      probSegs = '<ul>'
      
      for segment in segments
        probSegs += '<li><label>' + segment + '</label> <textarea id="' + segment + '-text"></textarea></li>'
      probSegs += '</ul><button id="goToLangSelect" >Next</button>'
      $('#statement-inputs',popup).html probSegs
      checkboxes = '<ul>'
      

      while(s.length>1)
        if(s.charAt(0)=='$'&&s.charAt(1)=='$')
          if(isMathBlock)
            result=result+'$</math>\n'
            isMathBlock=false
          else
            result=result+'<math>$'
