﻿// JScript File
Function.prototype.bind = function() {
  var __method = this, args = $A(arguments), object = args.shift();
  return function() {
    return __method.apply(object, args.concat($A(arguments)));
  }
}

function ReadLocalFile(TestCode)
		{
			var FileReader = document.getElementById("ocx1");			
			return FileReader.GetDatFile(TestCode,1);
		}
	
function WriteLocalFile(Content,PagerCode)
		{
			var FileReader = document.getElementById("ocx1");			
			FileReader.SavePaper(Content,PagerCode);
		}
/*
function OnExamOver(TestStatus,ErrCode)
	{
		alert(TestStatus+':'+ErrCode);
		//var slector = document.all('slector');			
		//slector.options.add(new Option(TestStatus, ErrCode));
	}*/



		
		/*Report Service*/
		var Report={
		      mark:false,//for reportSee;

			OnGetReportReturn:function(result){
				//alert(Report.mark);
				if(result!=null && result!="")
				{
				    cancelLoading('');
				    document.getElementById("report").innerHTML=result;
				    document.getElementById("reg_all").style.display="block";
				    clearTimeout(tt);
				}
				else
				{

				    if(Report.mark)
				        {
				            cancelLoading('获取报告失败');
				            if(SOAPClient.xmlhttp)SOAPClient.xmlhttp.abort();
				            window.location.href='testException.aspx';			            
				            return;
				        }
				        else
				        {
				            setTimeout("Report.OnGetReport2();",5000);
				        }
				}
			},
			
			OnGetReport:function(){	
			window.location='ReportSee.aspx?code='+Exam.TestCode+"&userName="+Exam.UserName;				
			},
			/****在ReportSee中有效 *********/
			OnGetReport1:function()
			{
			    var params = [{"Name":"TestCode","Value":Exam.TestCode}];				
			    WebServiceClient.Invoke(Exam.WebService,"GetReport",params,Report.OnGetReportReturn);			   
			},
			OnGetReport2:function()
			{
			    var params = [{"Name":"TestCode","Value":Exam.TestCode},{"Name":"username","Value":userName}];				
			    WebServiceClient.Invoke(Exam.WebService,"GetReport2",params,Report.OnGetReportReturn);			   
			}
			/**********************/
		}
				
		
	
		/*Exam Service*/
		var Exam = {
			WebService:"",
			PaperCode:"",
			TestCode:"",
			UserName:"",
			ErrorCode:"",
			TestState:0,
			Timer:null,
			attachi:0,
			attachj:0,
			freeResult:"",
			ExamEvaluteRuslut:1,
			OnExamStartReturn:function(result){
				Exam.TestCode = result["TestCode"];
				Exam.PaperCode = result["PaperCode"];
				var start=result["CreateResult"];				
				try
				{
				    if(start == "0000")
				    {
				        clearTimeout(tt);
				        showAttention1(getDivContent_test());				        
				    }
 				    else if(start == "0004")
				    {
				        Exam.OnGetPapers();
				        document.getElementById("textInner").innerHTML="试卷下载中。。。";
				    }
				    else if(start=="0001")
				    {
				        alert('您的余额不足！');
				        window.location.href="../user/count.aspx";
				     }
				     
				     else if(start == "0003")
				     { 
				         if(window.confirm("您的余额不足！点击“确定”去互联星空支付，点击“取消”返回。"))
                  		{
                      			window.location.href="../VnetPay/Charge.aspx?flag=item";
                  		}
				       else
			            {
			                 clearTimeout(tt);
				             cancelLoading('');
			            }
					    return;
				     }
				     else
				     {
				        clearTimeout(tt);
				       
				        //alert("创建课程失败，请重试！");
				       
				        alert("请重新登陆!");
				        window.location.href="../other/login.aspx";
				        cancelLoading('');
				     }
				}
				catch(error)
				{
				
				}
				//alert("评测开始:"+result["TestCode"]+","+result["PaperCode"]);
			},	
			
			OnGetPapersReturn:function(result){
			   // alert("试卷下载完毕，开始考试");	
			    cancelLoading('试卷下载完毕，开始考试');
			    try{
			        clearTimeout(tt);
			        WriteLocalFile(result,Exam.PaperCode);
			    }
			    catch(err)
			    {
			        alert('智能测试软件保存试卷出错，请重新安装。');
			        return;
			    }
			document.getElementById("ocx2").detachEvent("OnExamOver", Exam.OnExamResult);
			document.getElementById("ocx2").detachEvent("OnExamOver", Exam.OnExamResult);
 			document.getElementById("ocx2").detachEvent("OnResultReady", Exam.OnEvaluate);
 			document.getElementById("ocx2").detachEvent("OnReasonUpload", Exam.OnReasonUpload);  
			document.getElementById("ocx2").attachEvent("OnExamOver",Exam.OnExamResult);
			document.getElementById("ocx2").attachEvent("OnUploadFile", Exam.OnUploadFile);
		    document.getElementById("ocx2").attachEvent("OnResultReady", Exam.OnEvaluate);
		    document.getElementById("ocx2").attachEvent("OnReasonUpload", Exam.OnReasonUpload);
		    try{document.getElementById("ocx1").SetParams("PARAMLIST", EipParams+","+FtpIp);}catch(e){}
		    if(Exam.UserName.indexOf('net$')!=-1)
		    {
                document.getElementById("ocx2").runeip(Exam.TestCode,"互联星空用户",Exam.PaperCode);
            }
            else
            {
                 document.getElementById("ocx2").runeip(Exam.TestCode,Exam.UserName,Exam.PaperCode);
            }
			},
			
			OnEvaluateResult:function(result){
				//alert("评测结束:"+result);
				//评测成功												
				if(result==true)
				Exam.ExamEvaluteRuslut = 2;
				else
				//评测失败
				Exam.ExamEvaluteRuslut = 3;
				//document.getElementById("ocx1").OnResultReady(FileName);
			},
			
			OnUploadFile:function(userName,fileType,fileContent)
			{
			    if(fileType == 1)
			    {
			        var params=[];
			        params[0]={"Name":"userName","Value":userName};
			        params[1]={"Name":"fileContent","Value":fileContent};
			        WebServiceClient.Invoke(Exam.WebService,"UploadFile",params,Exam.OnUploadFileReturn);
			    }
			},
			OnUploadFileReturn:function(result)
			{
			    
			},
			
			OnExamReturn:function (result)	{			
			switch(Exam.TestState)
			    {		    
			        case 0:
			        case 11:return;
			        
			        case 1:
			            if(result)
			            Report.OnGetReport();
			            else
			            alert("您的余额不足");
			            break;			        
			        case 14:
			        case 20:
 			            window.location.href="../test/testException.aspx";
 			            break;
 			        case 21:
 			        case 22:
 			        case 23:
			        case 24:return;
			        case 30:return;
			        case 4:return;			    
			        default:return;//其它状态的处理
			    }		
			  
			},
			//============================================================
			OnReasonUpload:function(detailInfo,category,testCode)
			{
			    var params=[];
			    params[0]={"Name":"testCode","Value":testCode};
			    params[1]={"Name":"category","Value":category.toString()};
			    params[2]={"Name":"detailInfo","Value":detailInfo};
			    WebServiceClient.Invoke(Exam.WebService,"UploadReason",params,Exam.upReturn);
			},
			upReturn:function(result)
			{
			//alert(result.toString());
			    //alert(result.toString());
			},
			OnExamStart:function(){
				if(Exam.Timer!=null)Exam.Timer=null;
				Exam.attachi=0;	
				Exam.attachj=0;		
				var params = [{"Name":"userName","Value":this.UserName}];
				WebServiceClient.Invoke(Exam.WebService,"Create",params,this.OnExamStartReturn);
			},			
			OnGetPapers:function(){		
							
				var params = [{"Name":"PaperCode","Value":this.PaperCode}];				
				WebServiceClient.Invoke(Exam.WebService,"GetPapers",params,this.OnGetPapersReturn);
			},		
			OnEvaluate:function (TestCode){	
				Exam.attachj++;
				if(Exam.attachj<5)
				{			
				    var Papers = ReadLocalFile(TestCode);//Get Papers			
				    var params = [];
				    params[0] = {"Name":"TestCode","Value":TestCode};
				    params[1] = {"Name":"papers","Value":Papers};
				    params[2]={"Name":"userName","Value":Exam.UserName};
				    
				    if(freeName)
				    {				       
				        WebServiceClient.Invoke(Exam.WebService,"FreeEvaluate",params,Exam.OnFreeEvaluteReturn);
				    }
				    else
				    {
				        WebServiceClient.Invoke(Exam.WebService,"Evaluate",params,Exam.OnEvaluateResult);
				    }
				}				
			},			
			OnFreeEvaluteReturn:function(result)
			{
			    Exam.freeResult=result;
			    if(result.indexOf('等')!=-1)
				Exam.ExamEvaluteRuslut = 2;
				else
				//评测失败
				Exam.ExamEvaluteRuslut = 3;		    
			},
			OnWaitEvaluate:function(ErrorCode)
			{		
			    if(Exam.ExamEvaluteRuslut != 1)
			    {		
				    clearInterval(Exam.Timer);
				    Exam.attachi=0;	        
			        cancelLoading('');				
			        Exam.OnExamResult(Exam.TestState,ErrorCode);
			    }			   
			},
			OnEvaluateTimeOut:function()
			{			  
			   cancelLoading('');
			   clearInterval(Exam.Timer);
			   //Exam.ExamEvaluteRuslut=4;
			   Exam.TestState=20;
			   Exam.OnExamReturn();
			},	
			OnExamResult:function (State,ErrorCode){
				Exam.attachi++;
				if(Exam.attachi<5)
				{		
				var params = [];
				Exam.TestState=State;
				Exam.ErrorCode=ErrorCode;
				params[0] = {"Name":"TestCode","Value":Exam.TestCode};
				params[1] = {"Name":"State","Value":State};
				params[2] = {"Name":"ErrorCode","Value":ErrorCode};
				params[3] = {"Name":"userName","Value":Exam.UserName};
				if(State == 1)
				{				    			    
			       if(Exam.ExamEvaluteRuslut == 1)
				  {
					    setTimeout("Exam.OnEvaluateTimeOut();",180000);	
				        if(document.getElementById("div_before") == null)
				        loading("语音数据评测中...");
    				    Exam.OnWaitEvaluate(Exam.ErrorCode);
					    Exam.Timer=window.setInterval("Exam.OnWaitEvaluate(Exam.ErrorCode)",5000);
				   }
				   else if(Exam.ExamEvaluteRuslut == 2)
				   {		
				     if(freeName)
				     {
				        window.location.href="../test/FreeReport.aspx?leval="+Exam.freeResult;
				        return;
				     }	
				      WebServiceClient.Invoke(Exam.WebService,"EndTask",params,Exam.OnExamReturn);				      
				   }
				   else if(Exam.ExamEvaluteRuslut == 3)
				   {		
				        if(freeName)
				         {
				            alert('评测失败！');
				            return;
				         }
				        State = 14;
				        Exam.TestState=14;
				        params[1] = {"Name":"State","Value":State};
				        WebServiceClient.Invoke(Exam.WebService,"EndTask",params,Exam.OnExamReturn);
				   }
				   else
				   {
				        if(freeName)
				         {
				            alert('评测失败！');
				            return;
				         }
				       Exam.OnExamReturn();
				      
				   }
				   return;
				}
				if(freeName)
			     {			        
			        return;
			     }
				WebServiceClient.Invoke(Exam.WebService,"EndTask",params,Exam.OnExamReturn);
				}
			}			
			
		}





//for Client

function StartExam()
 {
       cancelAttention();          
       tt= setTimeout("cancelLoading('');if(SOAPClient.xmlhttp)SOAPClient.xmlhttp.abort();alert('网络响应超时，请重新开始测试。');",30000);
       var localhref=window.location.href;      
       try{
       if(localhref.indexOf("www2") != -1)
       {
            FtpIp=FtpIp.split('#')[1];
       }
       else
       {
            FtpIp=FtpIp.split('#')[0];
       }
       }catch(e){}
       var start=localhref.indexOf("://");
       var head=localhref.substring(0,start+3);
       if(start != -1)
       {
            localhref=localhref.substring(start+3,localhref.length);                    
       }             
       localhref=localhref.substring(0,localhref.indexOf("/")); 
       Exam.WebService=eval("\""+head+localhref+"/webservice/TestTask.asmx"+"\"");
       Exam.UserName=usercode;
       loading("测试任务申请中...");
       setTimeout("Exam.OnExamStart();",800);
 }
 
 function EditionValidate(oldEdition,newEdition)
 {
    if(oldEdition.indexOf('.')!=-1 && newEdition.indexOf('.')!=-1)
    {
        if(oldEdition==newEdition)
        {
            return true;
        }
        var oldEditions=oldEdition.replace(/\./g,'');
        var newEditions=newEdition.replace(/\./g,'');
        if(oldEditions.length != newEditions.length)
        {
            return false;
        }
        if(parseInt(oldEditions)<parseInt(newEditions))
        {
            return true;
        }
        return false;        
    }
    return false;
 }
 
 function startup()
 {   
     if(usercode=="")
    {
        window.location.href="/other/login.aspx?url="+encodeURIComponent(window.location);
        return;
    }
     //判断是否为ie内核
     var version=navigator.appVersion.toLowerCase();         
        if(version.indexOf("msie")==-1)
        {
            alert('模拟测试现只支持IE浏览器！');
            return false;
        }        
     if(typeof ocx1.GetDatFile=='undefined' || typeof ocx2.Init == "undefined"  )
     {
        document.getElementById('controlBox').innerHTML='<object classid="clsid:863B212B-0786-4D31-ADF8-C43A20A03D2B" id="ocx1" width="0" height="0" style="display: none;"></object><object classid="clsid:B620DFE7-F198-4843-9115-589237AE4B87" id="ocx2" width="0"  height="0" style="display: none;">';
        ocx1=document.getElementById('ocx1');
        ocx2=document.getElementById('ocx2');
        try{ocx2.Init(2);}catch(e){} 
     }
    if(typeof ocx1.GetDatFile=='undefined' || typeof ocx2.Init == "undefined"  )
	{	
	    studyRequreClient();
	    window.event.returnValue=false; 
	    return;
	}
	if(typeof ocx1.GetEcpVersion=='undefined')
	{
	    studyRequreClient();
	    window.event.returnValue=false;
	    return;
	}
	var newEdition=ocx1.GetEcpVersion("");
	if(newEdition == "ecpisrun")
    {
        alert('您已经启动了客户端！');
        return;
    }
	if(!EditionValidate(oldEdition,newEdition))
	{	   
	     if(window.confirm("您的智能测试软件未安装或版本过低，您要重新下载安装吗？"))
	        window.location.href=downClient;
	    return;
	   
	}
// var taskcode='<% =TaskCode %>';  
    if(window.screen.width<1024 || window.screen.height<576)
    {
        alert('您的显示器分辨率小于1024*576，不能使用客户端，请将分辨率调整到1024*576或以上再试！');
        return;
    }
    else if(usercode.length<1)
    {
        alert('用户名未知，请重新登录后再试。');
        return;
    }
    else
    {   
       showAttention();
    }
 }
 var freeName=null;
