function startADraw(testId) { };


function sendToServer(submit) {//发送到服务器 0自动保存，1手动保存。2交卷
    if (!initPageComplete) return;
    
    var xmlhttp;
    
    var XMLDoc=new ActiveXObject("Microsoft.XMLDOM");
    XMLDoc.loadXML("<?xml version='1.0' encoding='utf-8' ?><KeyList></KeyList>")
    var root=XMLDoc.selectSingleNode("KeyList");
    var tigerLen = 0; var minLen = 0;
    if (submit == 0) { tigerLen = 3; leaveLen = 1; }
    var q = _testFlags.getQueue();
    var keyIdArr = [];
    if (q.size() >= tigerLen) {
        var count=q.size() - minLen;
        for (var y = 0; y < count; y++) {

            var keyId = q.getData();
            var sendData = _testFlags.getItem(keyId)
            if (sendData.sendState == 1) {continue; };
            var keyName = sendData.keyId;
            var keyArr = sendData.clientKey;
            keyIdArr[keyIdArr.length] = keyId;
            var keyNode = XMLDoc.createElement("StuKey");
            keyNode.setAttribute("KeyId", sendData.keyMode + keyName);
            if (keyArr.length == 0) { keyNode.text = ""; }
            else {
                if (keyArr.length == 1) {
                    keyNode.text = keyArr[0];
                }
                else {
                    for (var i = 0; i < keyArr.length; i++) {
                        var keyNode1 = XMLDoc.createElement("Key");
                        keyNode1.setAttribute("Addr", i + 1);
                        if (keyArr[i] == null) {
                            keyNode1.text = ""
                        } else {
                            keyNode1.text = keyArr[i];
                        }
                        keyNode.appendChild(keyNode1);
                    }
                }

            }

            root.appendChild(keyNode);
            _testFlags.savedState(sendData, 1);
        }
    }

   if(submit==2||(root.childNodes.length>0)){
       //发送答案
     
     
     xmlhttp= new ActiveXObject("Microsoft.XMLHTTP");
	 
	 
     if(submit==2)
	    {
	        xmlhttp.onreadystatechange =function() { submitingPaper(keyIdArr, xmlhttp) };
	    xmlhttp.open("POST","/exam/accessexam/submit.aspx"+location.search+"&op=sm",true);
	    
	    }
	 else
	    {//if(saveAll)getMsg();

	      var saveButn=document.getElementById("_save");
          if(saveButn)saveButn.disabled='disabled';
          var handButn=document.getElementById("_hand");
	      if(handButn)handButn.disabled='disabled';
	      xmlhttp.onreadystatechange = function() { savingKey(keyIdArr,xmlhttp) };
	      xmlhttp.open("POST","/exam/accessexam/submit.aspx"+location.search+"&op=sv",true);
	      
	      
	     }
	  xmlhttp.setRequestHeader("Content-Type","text/xml");
	  xmlhttp.send(XMLDoc);
	 
	  return true;
   
   }
   else
     return true;
   
}
function changeMarke(testId,state){
    var markDiv=document.getElementById("judgeDiv"+testId.substring(1));
    if(markDiv==null)return ;
    markDiv.style.display="block";
    switch (state ){
      case 0:
         markDiv.innerHTML="<img src='../../../images/check_error.gif'/>未保存";
         break;
      case 1:
         markDiv.innerHTML="<img src='../../../images/loading2.gif'/>saving...";
         break;
      case 2:
         markDiv.innerHTML="<img src='../../../images/check_right.gif'/>保存成功";
         break;
    }
    
}
function savingKey(keyIdArr, xmlhttp)
{

 //esave.innerText = "正在向服务器保存答卷，已经完成"+xmlhttp.readyState*25+"%。"

 if(xmlhttp.readyState==4)
 { var handButnd=document.getElementById("_hand");
   if(handButnd)handButnd.disabled=''//保存答卷完成时，启用用胶卷按钮
   var saveButn=document.getElementById("_save");
   if(saveButn)saveButn.disabled='';
 
  
  if(xmlhttp.status==200)
  { var saveXML=xmlhttp.responseXML;
    var keyNodes=saveXML.selectNodes("/KeyList/StuKey");
    for(var i=0;i<keyNodes.length;i++){
      var keyNode=keyNodes[i];
      var testId = keyNode.getAttribute("KeyId");
   
          //var keys=ele.getBKey();
          var sendKeyArr=new Array();
          
          for(var k=0;k<keyNode.childNodes.length;k++){
                      var akeyNode=keyNode.childNodes[k];
                      var index=0;
                      if(akeyNode.attributes!=null)index=parseInt(akeyNode.getAttribute("Addr"))-1;
                      var aKey=akeyNode.text;
                      sendKeyArr[index]=aKey.replace(/\n/gm,"\r\n");
                    }
          if(sendKeyArr.length==0)sendKeyArr[0]="";
         _testFlags.setServerKey(testId.substring(1),sendKeyArr);
    
    }
    
   //getKeysCount();
   //esave.innerText= "向服务器保存答卷成功完成"
  }
  else
  {
      //esave.innerText= "抱歉，向服务器保存答卷失败。原因：" + decodeURIComponent(xmlhttp.statusText)
for (var k = 0; k < keyIdArr.length; k++) {
          _testFlags.savedState(_testFlags.getItem(keyIdArr[k]), 3)
      };
      alert("抱歉，向服务器保存答卷失败。原因：" + decodeURIComponent(xmlhttp.statusText) + " Code:" + xmlhttp.status)
      
      
      
  } 
 
 }
}
function submitingPaper(keyIdArr, xmlhttp) {
    self.location = "#"; var sp = document.getElementById("autosubmit_span")
  sp.innerText="正在向服务器递交答卷，请勿乱乱动。已经完成"+xmlhttp.readyState*25+"%。"
  //esave.innerText = "正在向服务器递交答卷，已经完成"+xmlhttp.readyState*25+"%。"

 if(xmlhttp.readyState==4)
 { //保存答卷完成时
  if(xmlhttp.status==200)
  {
      window.onbeforeunload = null;
	 document.write(xmlhttp.responseText);
   //esave.innerText= "向服务器保存答卷成功完成"
  }
  else
  {var handButn=document.getElementById("_hand");
    if(handButn)handButn.disabled=''
	var saveButn=document.getElementById("_save");
     if(saveButn)saveButn.disabled=''//胶卷完毕后，启用保存按钮
   document.forms[0].removeChild(sp);//esave.innerText= "抱歉，向服务器保存答卷失败。原因：" + decodeURIComponent(xmlhttp.statusText)
   for (var k = 0; k < keyIdArr.length; k++) {
       _testFlags.savedState(_testFlags.getItem(keyIdArr[k]), 3)
   };
   alert("抱歉，向服务器递交答卷失败，请稍候再次提交。原因：" + decodeURIComponent(xmlhttp.statusText) +" Code:"+ xmlhttp.status);
  }
 }
};
function confirmSubmitKey(){

if (document.getElementById("confirm_span"))
confirm_span.style.display='block'
else
{
var spanElement=document.createElement("span");
    //"Z-INDEX: 999; LEFT: 0px; WIDTH: 100%; TOP: 0px; HEIGHT: 100%"<span style="BACKGROUND-COLOR: red"<span style= "LEFT: 0px; POSITION: absolute; TOP: 0px; BACKGROUND-COLOR: red" 
     spanElement.style.width ="100%";
     spanElement.style.height ="100%";
     spanElement.style.left="0"
     spanElement.style.top="0"
     spanElement.style.zIndex="9999"
     spanElement.style.position = "fixed"
     spanElement.style.background ="red"    
     spanElement.style.color ="#ffffff"
     spanElement.style.textAlign = "center";
     
     spanElement.style.fontSize ="20pt"
     spanElement.style.filter="alpha(opacity=90)";
     spanElement.id ="confirm_span";
     
     //front.style.textAlign ="center";
     if (navigator.appName.indexOf('Microsoft') == -1 || version < 7.0) {
         self.location = "#";
         spanElement.style.width = document.body.scrollWidth;
         spanElement.style.height = document.body.scrollHeight;
         spanElement.style.position = "absolute";

         spanElement.innerHTML = "<br/>确实要交卷吗？<p align='center'><INPUT type=\"button\" value=\"确定\" onclick =\"javascript:submitKey()\">&nbsp;&nbsp;<INPUT type=\"button\" value=\"取消\" onclick =\"javascript:document.forms[0].removeChild(document.getElementById('confirm_span'));\"></p>"
         

     } else {
         var front = document.createElement("span");
         front.style.width = "350px";
         front.style.height = "200px";
         front.style.left = "50%";
         front.style.top = "50%";
         front.style.zIndex = "99999";
         front.style.marginTop = "-100px";
         front.style.marginLeft = "-150px";
         front.style.position = "fixed";
         front.style.background = "#fff";
         front.style.padding = "20px";
         front.style.border = "4px outset #808080";
         front.style.color = "#ff0000";

         front.innerHTML = "确实要交卷吗？<p align='center'><INPUT type=\"button\" value=\"确定\" onclick =\"javascript:submitKey()\">&nbsp;&nbsp;<INPUT type=\"button\" value=\"取消\" onclick =\"javascript:document.forms[0].removeChild(document.getElementById('confirm_span'));\"></p>"
         spanElement.appendChild(front)
     };

     document.forms[0].appendChild(spanElement);
}

}
function submitKey() {
    self.location = "#";
   
   if (document.getElementById("autosubmit_span"))
    autosubmit_span.style.display='block'
  else
  {
     var spanElement=document.createElement("span");
    //"Z-INDEX: 999; LEFT: 0px; WIDTH: 100%; TOP: 0px; HEIGHT: 100%"<span style="BACKGROUND-COLOR: red"<span style= "LEFT: 0px; POSITION: absolute; TOP: 0px; BACKGROUND-COLOR: red" 
     spanElement.style.width ="100%"
     spanElement.style.height ="100%"
     spanElement.style.left="0"
     spanElement.style.top="0"
     spanElement.style.zIndex="99999"
     spanElement.style.position="absolute"
     spanElement.style.background ="red"
     spanElement.style.color ="#ffffff"
     spanElement.style.textAlign ="center"
     spanElement.style.fontSize ="20pt"
     spanElement.style.filter="alpha(opacity=90)";
     spanElement.id ="autosubmit_span"
     spanElement.innerText="正在向服务器递交答卷，请勿乱乱动。已经完成0%。"
     document.forms[0].appendChild(spanElement);
     }
     var saveButn=document.getElementById("_save");
     if(saveButn)saveButn.disabled='disabled'//递交答卷时，禁用保存按钮
     var handButn=document.getElementById("_hand");
    if(handButn)handButn.disabled='disabled';
    sendToServer(2);
	
}
var initPageComplete=false;
var myKeyXML;
function getMyKey() {
//设置显示
  if(myKeyXML !=null)return ;
  myKeyXML=new ActiveXObject ("Microsoft.XMLDOM");
 //得到用户id
   var serch=window.location.search
   var i1=serch.indexOf("ui=")
   var uid=""
   if (i1!=-1){
     uid=serch.substring(i1+3)
     if (uid!="")
      {
         i1=uid.indexOf("&")
         if (i1!=-1)
          {
             uid=uid.substring(0,i1)
          }
      
     }
  }
 //得到考试名称
   
   var i11=serch.indexOf("ei=")
   var ksid=""
   if (i11!=-1){
     ksid=serch.substring(i11+3)
     if (ksid!="")
      {
         i11=ksid.indexOf("&")
         if (i11!=-1)
          {
             ksid=ksid.substring(0,i11)
          }
      
     }
  }
 document.forms[0].reset();//表单还原
  //得到考生答案
 
   
   
   myKeyXML.async = false
   
   
     myKeyXML.load("/exam/accessexam/loadexamineekey.aspx?ei="+ksid+"&ui="+uid)

   


 };

 function getCookie(name) {

     var strArg = name + "=";
     var nArgLen = strArg.length;
     var nCookieLen = document.cookie.length;
     var nEnd;
     var i = 0;
     var j;
     while (i < nCookieLen) {
         j = i + nArgLen;
         if (document.cookie.substring(i, j) == strArg) {
             nEnd = document.cookie.indexOf(";", j);
             if (nEnd == -1) nEnd = document.cookie.length;
             var nr = document.cookie.substring(j, nEnd)
             return decodeURIComponent(nr);
             //return (URLDecode_vb(nr));
         }
         i = document.cookie.indexOf(" ", i) + 1;
         if (i == 0) break;
     }
     return null;
 };
 
 $().ready(
        function() {

            $("div[class='resizeHandler'],div[class='dragHandler']").remove();

            $("label[class='dragHandler']").removeClass('dragHandler');
            var userId = targetQueryString(top, "ui");
            var my = "../../data/usersphoto/" + userId + ".jpg";

            var photo=document.getElementById("myPhoto");photo.src = my;
            photo.onerror = function() {

                photo.src = "../../data/usersphoto/nophoto.gif";

            };


            document.getElementById("userIdDiv").innerText = userId;

            initInOnePage();

            var inOne = getCookie("allTestsDisplay") == "1";
            if (inOne) return;
            initOneTestMode(); displayNext();


        });
function initInOnePage(){
   
   
  GetExamineeKeyInit();
  down();
  initPageComplete = true;
  var sp = document.getElementById("cover_div");
  sp.parentNode.removeChild(sp);
  var p = getCookie("notLeavePage");
  if (p == "1") setNotLefe(); 
  //var inOne=getCookie("testsInOnePage")=="1";
  //if(inOne)return;
  //initOneTestMode(); displayNext();
  
}
var _allTests;
var _allThemeAreas;
var _testsIdHasPreArr;
function initOneTestMode(){
  $("#_save").val("下 一 题");
  $("#_save").attr({ title: "只能前进到下一个，不可后退到上一个。" });
  _allTests=$("li[id^='test_']");
  //var allTestAreas=$("div[id^='testsArea_']");
  //var allThemeTexts=$("div[id^='themeText_']");
  _allThemeAreas=$("li[id^='themeArea_']");
  //_allTests.attr({ style: "display:none;" });
  //_allThemeAreas.attr({ style: "display:none;" });
  _testsIdHasPreArr=_allTests.map(function(){
     return $(this).attr("id");
 }).get();

 testFlags.prototype.testFlagsEnabled=false;
  

}
function displayNext(){
   if(_allTests==null)return;
   _allTests.attr({ style: "display:none;" });
   _allThemeAreas.attr({ style: "display:none;" });
   for(var i=0;i<_testsIdHasPreArr.length;i++){
      var testIdHasPre=_testsIdHasPreArr[i];
      if(testIdHasPre ==null) continue;
      var testId=testIdHasPre.substring(5);
      //if(testFlagsGetItemState(testId)==0){
         //该题未做
          var test = $("#" + testIdHasPre);
         var theme=test.parents("li[id^='themeArea_']");
         theme.attr({ style: "display:block;" });
         //test.attr({ style: "display:block;" });
         test.attr({ style: "display:block;" });
         _testFlags.setFoucs(testId,null);
         _testsIdHasPreArr[i]=null;//已向考生显示此题，下次不再显示
         return;
      //}
   }
  submitKey();
};
//获取服务器时间

function obtainServerTime(){
  
  var countDownXML=new ActiveXObject ("Microsoft.XMLDOM");
  countDownXML.async = false

  countDownXML.load("/exam/accessexam/countDown.aspx" + location.search)

  var TimeList=countDownXML.selectSingleNode("/Time")
  var ServerTime=TimeList.selectSingleNode("ServerTime").text
  var EndTime=TimeList.selectSingleNode("EndTime").text
  var timecount=EndTime-ServerTime;
  timecount = Math.floor(timecount / 10000000);
  return timecount;
}
//调用倒计时程序
function down(){


//全屏显示
//var wsh = new ActiveXObject("WScript.Shell");
//wsh.sendKeys("{F11}")
//if (window != top) top.location.href = location.href;
//resize()

//try{}catch(e){ };
tim(obtainServerTime());



}
//倒计时

function tim(timecount) {
var diff;
timecount = timecount - 1;
diff = timecount;
var seconds=Math.floor(diff);
var days=Math.floor(seconds/86400);
var hrs = Math.floor(seconds / 3600);
var mins = Math.floor((seconds  - hrs * 3600) / 60);
var sec = Math.floor((seconds  - hrs * 3600 - mins * 60));
var timeDiv = document.getElementById("timer");
var timeStr=hrs + ':' + mins + ':' + sec;
timeDiv.innerText = timeStr;

if ((hrs==0)&&(mins==0)&&(sec<=1)){
  var handButn=document.getElementById("_hand");
    //if(handButn)handButn.focus();
 
 submitKey(); 
 return;
}
else
{
    setTimeout(function() { tim(timecount) }, 1000);
};
}

 document.body.oncopy=function(){
  if(event.srcElement.type==null)     
    return false ;     
      
  else
    if((event.srcElement.type.toLowerCase() == "text")||(event.srcElement.tagName.toLowerCase() == "textarea"))return !event.srcElement.readOnly;
    else return false;
 return true;

}
function document.onkeydown() {

 with (event){

    if (keyCode==116 || (ctrlKey && keyCode==82) || (ctrlKey && keyCode==78) || (altKey && keyCode==9)||(ctrlKey && keyCode==84)){
      // F5 eller Ctrl+R  ctrl+n  alt+tab ctrl+b
      event.keyCode = 0;
      event.cancelBubble = true;
    
      returnValue= false;
    }
  }

if (event.keyCode==122)  //屏蔽F11
    {event.cancelBubble = true;
        window.event.keyCode    = 0;
        window.event.returnValue= false;
    }

if (event.shiftKey && event.keyCode==121)          //屏蔽 shift+F10
{event.cancelBubble = true;
        window.event.keyCode    = 0;
        window.event.returnValue= false;
    }
    if (window.event.srcElement.tagName.toLowerCase() == "a" && window.event.shiftKey) //屏蔽 shift 加鼠标左键新开一网页
 {event.cancelBubble = true;
        window.event.keyCode    = 0;
        window.event.returnValue= false;
    }  
var disableBack   =true ;
if(event.srcElement.type!=null){
  if((event.srcElement.type.toLowerCase() == "text")||(event.srcElement.tagName.toLowerCase() == "textarea"))
    {if(event.srcElement.readOnly)disableBack =true; else disableBack =false ;}
  else 
   {disableBack =true;}

}

if((event.keyCode==8)&&disableBack)
  {
   //window.showModelessDialog("../../alert/alert1.htm","","dialogHeight:170px;dialogWidth:270px;scroll:no;Status:no;help:no")
   event.keyCode = 0;
   event.cancelBubble = true;
   event.returnValue= false;}


}  
  
  document.oncontextmenu = nocontextmenu; 
  function nocontextmenu() //禁用鼠标右键
  { 
if ((event.srcElement.tagName.toLowerCase()!='input' && event.srcElement.tagName.toLowerCase()!='textarea' && document.selection.type.toLowerCase()=='none') || (event.srcElement.tagName.toLowerCase()=='input' && event.srcElement.disabled) || (event.srcElement.tagName.toLowerCase()=='textarea' && event.srcElement.disabled))
  {
   event.cancelBubble = true 
   event.returnValue = false; 
  
   return false;} 
}

//关闭考试页时提示，并刷新服务器时间

function window.onbeforeunload() {
    //alert(event.clientX +","+event.clientY)//if (event.srcElement!=null)alert(event.srcElement.tagName)
    //if (event.clientX > document.body.clientWidth && event.clientY < 0||event.altKey)
    //{
    // event.returnValue="确实想关闭本页吗?\r\n随机考试状态下，该操作可能导致更换试卷以及答题丢失等情况的发生！\r\n建议您点击'取消'按钮。";
    // }
    //else
    //if ((event.clientY<0)||event.altKey)
    event.returnValue = "您现在正在考试，确实想退出本页吗?";
    obtainServerTime();return;

}  
  //得到一个考生的答案
function GetExamineeKeyInit() {
   
   getMyKey();
   if (myKeyXML.parseError.errorCode!=0)
    {
     alert("无法得到考生先前的答案文件!")
 
    }
   else
    {

        displayEmaineeKey(myKeyXML,false);
    }

}