
var xdxml;var objXMLDoc;
function createWord()
{
  var wApp 
   
    
try
     {wApp = new ActiveXObject("Word.Application")}
   catch(Err)
     {
      alert("无法生成Word文档，请查看帮助文件")
      window.open("/paperStyle/3.0/active.htm")
      
      return
     }
     
     alert("如果在你的试卷中有Html格式字符，在生成的Word文档中将不能自动转换成相应的格式，请手工检查修改！")
     xdxml=new ActiveXObject ("Microsoft.XMLDOM");
     xdxml.async = false;
     xdxml.load("testpaper.xml");
     objXMLDoc=new ActiveXObject ("Microsoft.XMLDOM");
     objXMLDoc.async = false;
     objXMLDoc.load("testpaper.k.xml");
     
    wApp.visible= true 
    wApp.Documents.Add()
    wApp.Selection.ParagraphFormat.LeftIndent = wApp.CentimetersToPoints(0)

    wApp.Selection.TypeParagraph()
    wApp.Selection.Font.Bold = true
    wApp.Selection.Font.Size=16
    var paperTopic=xdxml.selectSingleNode("/paper/title").text
    wApp.Selection.TypeText(paperTopic)
    wApp.Selection.ParagraphFormat.Alignment = 1 
    // 试卷标题居 中
    wApp.Selection.TypeParagraph()
    wApp.Selection.Font.Bold = false
    wApp.Selection.Font.Size=10
    wApp.Selection.TypeText("满分：totalScore分")
    wApp.Selection.ParagraphFormat.Alignment = 1
    
    wApp.Selection.TypeParagraph()
    wApp.Selection.Font.Bold = false
    wApp.Selection.Font.Size=12
    wApp.Selection.TypeText("姓名：________")
    wApp.Selection.ParagraphFormat.Alignment = 2
    
    wApp.Selection.TypeParagraph()
    var dtNode  = xdxml.selectNodes("/paper/main/theme")
   creatWordDocument(wApp,dtNode,1)
 
    

   window.top.blur()

}
function creatWordDocument(wApp,dtNode,LineIndent){
var rv=WordDocument(wApp,dtNode,LineIndent)
wApp.Selection.InsertBreak();
    wApp.Selection.TypeParagraph()
    wApp.Selection.Font.Bold = true
    wApp.Selection.Font.Size=16
    wApp.Selection.TypeText("参考答案")
    wApp.Selection.ParagraphFormat.Alignment = 1 
    
    var totalScore=rv[0];
    var kesArr=rv[1];
 writeKey("",wApp,kesArr)
htmlToWord(wApp,totalScore)
}
function writeKey(th,wApp,kesArr){

 for (var mm=0;mm<kesArr.length;mm++){
      wApp.Selection.TypeParagraph()
      wApp.Selection.TypeParagraph()
      wApp.Selection.ParagraphFormat.FirstLineIndent = 20;
      wApp.Selection.Font.Size=12
      wApp.Selection.Font.Bold= false 
      wApp.Selection.ParagraphFormat.Alignment =0
      var keystr=kesArr[mm];
      var dth;
      if (th=="")dth=(mm+1);else dth=th+"."+(mm+1)
      if(keystr[0]==null)
      wApp.Selection.TypeText(dth+"  "+keystr.substr(0,keystr.length-1))
      else
      writeKey(dth,wApp,kesArr[0])
  }
}
function WordDocument(wApp,dtNode,LineIndent){
  var totalScore=0;
  var kesArr=new Array;
  for(var i=0;i<dtNode.length;i++)
    {
      
      var dt=dtNode.item(i)
      var dttm=dt.getAttribute("explication");
      
      wApp.Selection.TypeParagraph()
      wApp.Selection.ParagraphFormat.FirstLineIndent = wApp.CentimetersToPoints(LineIndent-1)
      wApp.Selection.Font.Size=12
      wApp.Selection.Font.Bold= false 
      wApp.Selection.ParagraphFormat.Alignment =0
      var tx=dt.getAttribute("class");
      if(tx=="6"){
       wApp.Selection.TypeText((i+1)+"、"+dttm)
       var note=dt.selectSingleNode("text").text
          
           wApp.Selection.TypeParagraph()
           wApp.Selection.ParagraphFormat.FirstLineIndent = wApp.CentimetersToPoints(0.72/2*1.5*LineIndent)
           wApp.Selection.Font.Size=10
           wApp.Selection.Font.Bold= false
           wApp.Selection.ParagraphFormat.Alignment =0
           wApp.Selection.TypeText(note)
           
           var srv=WordDocument(wApp,dt.selectNodes("theme"),1.5)
           totalScore+=srv[0];
           kesArr[i]=srv[1];
           continue;}
         
      var xts=dt.selectNodes("test")
      var dtzf=0
      for(var k=0;k<xts.length;k++)
      {
        var xt=xts.item(k)
        var fs=xt.getAttribute("maxScore");
        if (tx=="2")
         {var fsArray=fs.split(",")
         
          for(var l=0;l<fsArray.length;l++)
          {
           dtzf+=parseFloat(fsArray[l])
          
          } 
         }
        else
        dtzf+=parseFloat(fs)
      }
      
      
      
      totalScore+=dtzf
      wApp.Selection.TypeText((i+1)+"、"+dttm+"(本题共计"+dtzf+"分)")
      
      
      
      
      kesArr[i]="";
      for(var j=0;j<xts.length;j++)
      { var xt=xts.item(j)
        
        switch(tx){
          case "0":
          {
           var stsm=xt.selectSingleNode("text").text
           wApp.Selection.TypeParagraph()
           wApp.Selection.ParagraphFormat.FirstLineIndent = wApp.CentimetersToPoints(0.72/2*1.5*LineIndent)
           wApp.Selection.Font.Size=10
           wApp.Selection.Font.Bold= false 
           wApp.Selection.ParagraphFormat.Alignment =0
           wApp.Selection.TypeText((j+1)+"、"+stsm+"   (  )")
           
           var xxa=xt.selectSingleNode("choice[@key=\"a\"]").text
           wApp.Selection.TypeParagraph()
           wApp.Selection.ParagraphFormat.FirstLineIndent = wApp.CentimetersToPoints(0.72/2*3*LineIndent)
           wApp.Selection.Font.Size=10
           wApp.Selection.Font.Bold= false 
           wApp.Selection.ParagraphFormat.Alignment =0
           wApp.Selection.TypeText("A、"+xxa)
           
           var xxb=xt.selectSingleNode("choice[@key=\"b\"]").text
           wApp.Selection.TypeParagraph()
           wApp.Selection.Font.Size=10
           wApp.Selection.Font.Bold= false 
           wApp.Selection.ParagraphFormat.Alignment =0
           wApp.Selection.TypeText("B、"+xxb)
           
           var xxcNode=xt.selectSingleNode("choice[@key=\"c\"]");
           if (xxcNode!=null){
           var xxc=xxcNode.text;
           if(xxc!=""){
           wApp.Selection.TypeParagraph()
           wApp.Selection.Font.Size=10
           wApp.Selection.Font.Bold= false 
           wApp.Selection.ParagraphFormat.Alignment =0
           wApp.Selection.TypeText("C、"+xxc)}
           }
           
           
           
           var xxdNode=xt.selectSingleNode("choice[@key=\"d\"]");
           if (xxdNode !=null){
           var xxd=xxdNode.text; 
           if(xxd!=""){
           wApp.Selection.TypeParagraph()
           wApp.Selection.Font.Size=10
           wApp.Selection.Font.Bold= false 
           wApp.Selection.ParagraphFormat.Alignment =0
           wApp.Selection.TypeText("D、"+xxd)}
           }
           
           
           var xxeNode=xt.selectSingleNode("choice[@key=\"e\"]");
           if(xxeNode !=null){
           var xxe=xxeNode.text;
           if(xxe!=""){
           wApp.Selection.TypeParagraph()
           wApp.Selection.Font.Size=10
           wApp.Selection.Font.Bold= false 
           wApp.Selection.ParagraphFormat.Alignment =0
           wApp.Selection.TypeText("E、"+xxe)}
           }
           
           
           var xxfNode=xt.selectSingleNode("choice[@key=\"f\"]");
           if(xxfNode !=null){
           var xxf=xxfNode.text;
           if(xxf!=""){
           wApp.Selection.TypeParagraph()
           wApp.Selection.Font.Size=10
           wApp.Selection.Font.Bold= false 
           wApp.Selection.ParagraphFormat.Alignment =0
           wApp.Selection.TypeText("F、"+xxf)}
           }
           
           var testid=xt.getAttribute("id");
           var stanKey=objXMLDoc.selectSingleNode("keys/key[@id='"+testid+"']").text;
           kesArr[i]+=(j+1)+"."+stanKey+",";
           break
          }
          case "1":{
           var stsm=xt.selectSingleNode("text").text
           wApp.Selection.TypeParagraph()
           wApp.Selection.ParagraphFormat.FirstLineIndent = wApp.CentimetersToPoints(0.72/2*1.5*LineIndent)
           wApp.Selection.Font.Size=10
           wApp.Selection.Font.Bold= false 
           wApp.Selection.ParagraphFormat.Alignment =0
           wApp.Selection.TypeText((j+1)+"、"+stsm+"   (  )")
           
           var xxa=xt.selectSingleNode("choice[@key=\"a\"]").text
           wApp.Selection.TypeParagraph()
           wApp.Selection.ParagraphFormat.FirstLineIndent = wApp.CentimetersToPoints(0.72/2*3*LineIndent)
           wApp.Selection.Font.Size=10
           wApp.Selection.Font.Bold= false 
           wApp.Selection.ParagraphFormat.Alignment =0
           wApp.Selection.TypeText("A、"+xxa)
           
           var xxb=xt.selectSingleNode("choice[@key=\"b\"]").text
           wApp.Selection.TypeParagraph()
           wApp.Selection.Font.Size=10
           wApp.Selection.Font.Bold= false 
           wApp.Selection.ParagraphFormat.Alignment =0
           wApp.Selection.TypeText("B、"+xxb)
           
           var xxcNode=xt.selectSingleNode("choice[@key=\"c\"]");
           if (xxcNode!=null){
           var xxc=xxcNode.text;
           if(xxc!=""){
           wApp.Selection.TypeParagraph()
           wApp.Selection.Font.Size=10
           wApp.Selection.Font.Bold= false 
           wApp.Selection.ParagraphFormat.Alignment =0
           wApp.Selection.TypeText("C、"+xxc)}
           }
           
           
           
           var xxdNode=xt.selectSingleNode("choice[@key=\"d\"]");
           if (xxdNode !=null){
           var xxd=xxdNode.text; 
           if(xxd!=""){
           wApp.Selection.TypeParagraph()
           wApp.Selection.Font.Size=10
           wApp.Selection.Font.Bold= false 
           wApp.Selection.ParagraphFormat.Alignment =0
           wApp.Selection.TypeText("D、"+xxd)}
           }
           
           
           var xxeNode=xt.selectSingleNode("choice[@key=\"e\"]");
           if(xxeNode !=null){
           var xxe=xxeNode.text;
           if(xxe!=""){
           wApp.Selection.TypeParagraph()
           wApp.Selection.Font.Size=10
           wApp.Selection.Font.Bold= false 
           wApp.Selection.ParagraphFormat.Alignment =0
           wApp.Selection.TypeText("E、"+xxe)}
           }
           
           
           var xxfNode=xt.selectSingleNode("choice[@key=\"f\"]");
           if(xxfNode !=null){
           var xxf=xxfNode.text;
           if(xxf!=""){
           wApp.Selection.TypeParagraph()
           wApp.Selection.Font.Size=10
           wApp.Selection.Font.Bold= false 
           wApp.Selection.ParagraphFormat.Alignment =0
           wApp.Selection.TypeText("F、"+xxf)}
           }
           
          
           var testid=xt.getAttribute("id");
           var stanKey=objXMLDoc.selectSingleNode("keys/key[@id='"+testid+"']").text;
           kesArr[i]+=(j+1)+"."+stanKey+",";
           
           break
          }
          case "2":{
           var stsm=xt.selectSingleNode("text").text
           wApp.Selection.TypeParagraph()
           wApp.Selection.ParagraphFormat.FirstLineIndent = wApp.CentimetersToPoints(0.72/2*1.5*LineIndent)
           wApp.Selection.Font.Size=10
           wApp.Selection.Font.Bold= false 
           wApp.Selection.ParagraphFormat.Alignment =0
           wApp.Selection.TypeText((j+1)+"、"+stsm)
          
           var testid=xt.getAttribute("id");
           var stanKeyNode=objXMLDoc.selectSingleNode("keys/key[@id='"+testid+"']");
           var childrenCount=stanKeyNode.childNodes.length;
           kesArr[i]+=(j+1)+".";
           for(var kkk=0;kkk<childrenCount;kkk++){
           
               var stanKey=stanKeyNode.childNodes[kkk].text;
              kesArr[i]+=stanKey+",";
           
           }
           //
            break
          }
          case "3":{
           var stsm=xt.selectSingleNode("text").text
           wApp.Selection.TypeParagraph()
           wApp.Selection.ParagraphFormat.FirstLineIndent = wApp.CentimetersToPoints(0.72/2*1.5*LineIndent)
           wApp.Selection.Font.Size=10
           wApp.Selection.Font.Bold= false 
           wApp.Selection.ParagraphFormat.Alignment =0
           wApp.Selection.TypeText((j+1)+"、"+stsm+"   （   ）")
           
           
           var testid=xt.getAttribute("id");
           var stanKey=objXMLDoc.selectSingleNode("keys/key[@id='"+testid+"']").text;
           
           kesArr[i]+=(j+1)+"."+stanKey+",";
            break
          }
          
          case "4":{
           var stsm=xt.selectSingleNode("text").text
           wApp.Selection.TypeParagraph()
           wApp.Selection.ParagraphFormat.FirstLineIndent = wApp.CentimetersToPoints(0.72/2*1.5*LineIndent)
           wApp.Selection.Font.Size=10
           wApp.Selection.Font.Bold= false 
           wApp.Selection.ParagraphFormat.Alignment =0
           wApp.Selection.TypeText((j+1)+"、"+stsm)
           
           wApp.Selection.TypeParagraph()
           wApp.Selection.ParagraphFormat.FirstLineIndent = wApp.CentimetersToPoints(0.72/2*1.5*LineIndent)
           wApp.Selection.Font.Size=10
           wApp.Selection.Font.Bold= false 
           wApp.Selection.ParagraphFormat.Alignment =0
           wApp.Selection.TypeText("答：")
           
           wApp.Selection.TypeParagraph()
           wApp.Selection.TypeParagraph()
           wApp.Selection.TypeParagraph()
           wApp.Selection.TypeParagraph()
           wApp.Selection.TypeParagraph()
           
           var testid=xt.getAttribute("id");
           var stanKey=objXMLDoc.selectSingleNode("keys/key[@id='"+testid+"']").text;
           kesArr[i]+=(j+1)+"."+stanKey+",";
           break
          }
          
          case "5":{
           var xxa=xt.selectSingleNode("choice[@key=\"a\"]").text
           var xxb=xt.selectSingleNode("choice[@key=\"b\"]").text;
           var xxc="";
           var xxcNode=xt.selectSingleNode("choice[@key=\"c\"]");
           if (xxcNode!=null){
           xxc=xxcNode.text;
           
           }
           
           
           var xxd="";
           var xxdNode=xt.selectSingleNode("choice[@key=\"d\"]");
           if (xxdNode !=null){
           xxd=xxdNode.text; 
           
           }
           
           wApp.Selection.TypeParagraph()
           wApp.Selection.ParagraphFormat.FirstLineIndent = wApp.CentimetersToPoints(0.72/2*3*LineIndent)
           wApp.Selection.Font.Size=10
           wApp.Selection.Font.Bold= false 
           wApp.Selection.ParagraphFormat.Alignment =0
           var p=(j+1)+"."+"  A."+xxa+"  B."+xxb
           if(xxc!=""){p+="  C."+xxc}
           if(xxd!=""){p+="  D."+xxd}
           p+="   (  )"
           wApp.Selection.TypeText(p)
           
           var testid=xt.getAttribute("id");
           var stanKey=objXMLDoc.selectSingleNode("keys/key[@id='"+testid+"']").text;
           kesArr[i]+=(j+1)+"."+stanKey+",";
           break
          }
         
          
        }
        //wApp.Selection.ParagraphFormat.FirstLineIndent = wApp.CentimetersToPoints(0)
      }
    
    }

//window.execScript('htmlToWord()',"VBscript");=wApp.wdPageBreak


  var rv=new Array;
  rv[0]=totalScore;
  rv[1]=kesArr;
  return rv;

}
function htmlToWord(wApp,totalScore){
    
    var  FindText,MatchCase,MatchWholeWord,MatchWildcards,MatchSoundsLike,MatchAllWordForms,Forward,Wrap,Format,ReplaceWith,Replace,MatchKashida,MatchDiacritics,MatchAlefHamxa,MatchControl
    //空格
    wApp.Selection.Find.ClearFormatting()    
    wApp.Selection.Find.Replacement.ClearFormatting()   
    FindText="&nbsp;"
    MatchCase=false
    MatchWholeWord=false
    MatchWildcards=false
    MatchSoundsLike=false
    MatchAllWordForms=false
    Forward=true
    Wrap=1
    Format=false
    ReplaceWith=""
    Replace=2
    MatchKashida=false
    MatchDiacritics=false
    MatchAlefHamxa=false
    MatchControl=false    
    wApp.Selection.Find.Execute( FindText,MatchCase,MatchWholeWord,MatchWildcards,MatchSoundsLike,MatchAllWordForms,Forward,Wrap,Format,ReplaceWith,Replace,MatchKashida,MatchDiacritics,MatchAlefHamxa,MatchControl)

    
    //满分：totalScore
    wApp.Selection.Find.ClearFormatting()    
    wApp.Selection.Find.Replacement.ClearFormatting()   
    FindText="满分：totalScore分"
    MatchCase=true
    MatchWholeWord=true
    MatchWildcards=false
    MatchSoundsLike=false
    MatchAllWordForms=false
    Forward=true
    Wrap=1
    Format=false
    ReplaceWith="满分："+totalScore+"分"
    Replace=1
    MatchKashida=false
    MatchDiacritics=false
    MatchAlefHamxa=false
    MatchControl=false    
    wApp.Selection.Find.Execute( FindText,MatchCase,MatchWholeWord,MatchWildcards,MatchSoundsLike,MatchAllWordForms,Forward,Wrap,Format,ReplaceWith,Replace,MatchKashida,MatchDiacritics,MatchAlefHamxa,MatchControl)


}


function printPaper()
{
  var wApp 
   
    
try
     {wApp = new ActiveXObject("Word.Application")}
   catch(Err)
     {
      alert("无法生成Word文档，请查看帮助文件")
      window.open("../../help/active.htm")
      
      return
     }
     
     alert("如果在你的试卷中有Html格式字符，在生成的Word文档中将不能自动转换成相应的格式，请手工检查修改！")
    wApp.visible= true 
    wApp.Documents.Add()
    wApp.Selection.ParagraphFormat.LeftIndent = wApp.CentimetersToPoints(0)

    wApp.Selection.TypeParagraph()
    wApp.Selection.Font.Bold = true
    wApp.Selection.Font.Size=16
    var paperTopic=xdxml.selectSingleNode("/paper/title").text
    wApp.Selection.TypeText(paperTopic)
    wApp.Selection.ParagraphFormat.Alignment = 1 
    // 试卷标题居 中
    wApp.Selection.TypeParagraph()
    wApp.Selection.Font.Bold = false
    wApp.Selection.Font.Size=10
    wApp.Selection.TypeText("满分：totalScore分")
    wApp.Selection.ParagraphFormat.Alignment = 1
    
    wApp.Selection.TypeParagraph()
    wApp.Selection.Font.Bold = false
    wApp.Selection.Font.Size=12
    wApp.Selection.TypeText("姓名：________")
    wApp.Selection.ParagraphFormat.Alignment = 2
    
    wApp.Selection.TypeParagraph()
    var dtNode  = xdxml.selectNodes("/paper/main/theme")
   creatWordDocument(wApp,dtNode,1)
 
    

   window.top.blur()

}




