function getKeyFromStr(keyStr){
   var xmlDoc=new ActiveXObject ("Microsoft.XMLDOM");
   xmlDoc.async = false
   xmlDoc.loadXML("<?xml version='1.0' encoding='utf-8' ?><keys>"+keyStr+"</keys>");
   var keyNode=xmlDoc.selectSingleNode("/keys/key");
   return new key(keyNode);

};
     
var __keys = null;
function loadPaperKey(isCache, sendData, action,asy) {
    if (isCache == null) isCache = true;
    var pathName = location.pathname;
    var lastDot = pathName.lastIndexOf(".");
    var a = true;if (asy != null) a = asy;
    var kzm = pathName.substr(pathName.lastIndexOf(".") + 1);
    var keyFile = location.protocol + "//" + location.host + pathName.substring(0, lastDot) + ".k." + kzm + location.search;
    //alert(keyFile)
    $.ajax({
        type: "get",
        url: keyFile,
        data: sendData,dataType:"xml",
        processData: true,global:false,
        cache: isCache,
        async: a,
        success: function(data, textStatus) {
           var keysNode = data.selectSingleNode("/keys");
           __keys = new keyList(keysNode); 
           if ($.isFunction(action)) {
               action();
           }
        },
        error: function(XMLHttpRequest, textStatus, errorThrown) {
            // 通常 textStatus 和 errorThrown 之中 + " Name:" +  JSON.parse(data).d.Name
        // 只有一个会包含信息

            //alert("请求试卷答案出错，答案可能不存在！"); 
        }
    });

}

function key(keyNode){
   var _keyNode=keyNode;

   this.getOutXML = function() { return _keyNode.xml };
   this.getStandardKey = function() {
       var ch = this.children();
       var chC = ch.count();
       if (chC > 0) {
           var rk = "";
           for (var i = 0; i < chC; i++) {
               var c = ch.item(i);
               rk += c.getStandardKey() + " ";
           }
           if (rk.length > 0) {return rk.substr(0,rk.length -1) } else { return rk; };
       } else
           return _keyNode.text;
   };
   this.getOtherScore=function(){
      var attr=_keyNode.getAttribute("other");
      if(attr==null)return 0;
      return parseFloat(attr);
  }
  this.getId = function() {
      var attr = _keyNode.getAttribute("id");
      if (attr == null) return "-1";
      return attr;
  }
   this.getScoreMode=function(){
      var attr=_keyNode.getAttribute("mode");
      if(attr==null)return "all";
     
      return attr;
   };
   this.getForKey=function(){
      var forKey=new Array();
   
      var attr=_keyNode.getAttribute("for");
      var c=_keyNode;
      while(attr==null){
         var p=c.parentNode;
         if(p.nodeName=="keys"){forKey[0]=0;return forKey;}
         attr=p.getAttribute("for");
         c=p;
      }
     if(attr==null){forKey[0]=0;
       return forKey;
     } else{ 
       var attrArr=new Array();
       attrArr=attr.split(",");
       for(var i=0;i<attrArr.length;i++){
         forKey[i]=parseInt(attrArr[i]);
       }
     
       return forKey;
     }
     
   
   };
  
   this.getScore=function(){
      var attr=_keyNode.getAttribute("score");
      var c=_keyNode;
      while(attr==null){
         var p=c.parentNode;
         if(p.nodeName=="keys"){return 0;}
        
         attr=p.getAttribute("score");
         c=p;
      }
      return parseFloat(attr);
   
   };
   this.getInpro=function(){
      var attr=_keyNode.getAttribute("inpro");
      if(attr ==null)attr ="1";
      return parseFloat(attr);
   
   };
   this.children=function(){
      return new keyList(_keyNode);
   
   
   };
   
}
function keyList(parentKey){
   var children=parentKey.selectNodes("key");
   var pKey=parentKey;
   this.count=function(){return children.length;}
   this.item=function(index){
      if(this.count()>0)
        return new key(children[index]);
      else
        return null;
   }
  this.getKeyById=function(keyId){
  if (this.count() > 0) {


      var k = pKey.selectSingleNode("key[@id='" + keyId + "']"); 
        if(k==null)return null;else
        return new key(k);
        
      }
      else
        return null;
  
  }
};

 // 计算子节点的分值
        function cal( fullScore, eKey,sKeyNode, eKeyIndex){
            var k = eKey[eKeyIndex];
            if (k == null) return 0;
            if (k == "") return 0;
            var otherScore = sKeyNode.getOtherScore(); 
            //switch (sKeyNode.judgeMode){
            //  case "equal":
              
                    if (sKeyNode.getStandardKey() == k)
                        return sKeyNode.getInpro() * fullScore;
                    else
                        return otherScore * fullScore;
               //     break;
              //  case "proportional":
             //       return 0;
            //}

        }
     
        
        // 计算分数, keyIndex As Int32

        function check(eKey, sKeyNode, passedIndex) {
            var haskey = false;
            for (var i = 0; i < eKey.length; i++) {
                if (eKey[i] != null) { if (eKey[i] != "") haskey = true };
            };
            if (!haskey) return 0;
            if(passedIndex==null)passedIndex =-1;
            var fullScore = sKeyNode.getScore();
            var otherScore = sKeyNode.getOtherScore();
            switch (sKeyNode.getScoreMode()){
                case "all":
                    var s = 0; 
                    var ks = sKeyNode.children();
                    var ksc = ks.count(); 
                    if (ksc == 0){
                        if (passedIndex > -1){
                            s = cal(fullScore, eKey, sKeyNode, passedIndex);
                          }
                        else{
                            var forList = sKeyNode.getForKey(); 
                            for(var keyIndex=0;keyIndex<forList.length;keyIndex++){
                                s += cal(fullScore, eKey, sKeyNode, forList[keyIndex]);
                            }
                        }

                      }
                    else{
                        for (var i = 0;i< ksc;i++){
                            s += check(eKey, ks.item(i));
                        }
                    }
                    
                    return s
                case "enum":
                    var s = 0;
                    var ks = sKeyNode.children();
                    var ksc = ks.count();
                    if (ksc == 0){
                        if (passedIndex > -1){
                            s = cal(fullScore, eKey, sKeyNode, passedIndex);}
                        else{
                            var forList = sKeyNode.getForKey();
                            for(var keyIndex=0;keyIndex<forList.length;keyIndex++){
                                s = cal(fullScore, eKey, sKeyNode, forList[keyIndex])
                                if (s !=0) break;
                            }
                            if (s == 0) s = otherScore * fullScore;
                        }
                     }
                    else{
                        var found =new  Array();
                        var forArr = sKeyNode.getForKey();
                        for (var ii=0;ii<forArr.length;ii++){
                            var f=forArr[ii];
                            var s1 = 0;
                            for (var i = 0;i< ksc;i++){
                                var noUse = true;
                                for (var jj=0;jj<found.length;jj++){
                                    var j=found[jj];
                                    if (j == i) noUse = false;
                                }
                                if (noUse){
                                    s1 = check(eKey, ks.item(i), f)
                                    if (s1 != 0){
                                        found[found.length]=i;
                                        break;
                                    }
                                }
                            }
                            if (s1 == 0) s1 = otherScore * fullScore;
                            s += s1;
                        }
                    }
                    return s;                  
                   
            }
        }

