function readCookie(){
	var c = document.cookie;
	var ary = c.split(";");
	var i = 0;
	while (i < ary.length){
		var ary1 = ary[i].split("=");
		eval(ary1[0] + " = '" + ary1[1] + "'");
		i++;
	}
}

readCookie();

/*
 *答题计时器
 */
function reduce() {
	var c = document.cookie;
	var ary = c.split(";");
	var i = 0;
	while (i < ary.length){
		var ary1 = ary[i].split("=");
		eval("var " + ary1[0] + " = '" + ary1[1] + "'");
		i++;
	}
	
	if (typeof dotime == "undefined") {
		alert("请从试卷首页开始答题 ");
		window.location.replace("first.html");
	} else {
		if (typeof reduce_time == "undefined")
			var reduce_time = 0;
		var hh = Math.floor(reduce_time / (60 * 60));
		var mm = Math.floor((reduce_time - hh * 60 * 60) / 60);
		var ss = reduce_time - hh * 60 * 60-mm * 60;
		document.all.reducetime.innerHTML = hh + ":" + mm + ":" + ss;
		reduce_time++;
		document.cookie = "reduce_time=" + reduce_time;
		if (reduce_time <= dotime * 60)
			var timestamp = setTimeout("reduce()", 1000);
		else {
			clearTimeout(timestamp);
			alert("答题时间到！本次测试结束！");
			window.location.replace("result.html");
		}
	}
}

/**
 *
 */

function showAnswer() {
	start = window.location.href.indexOf("doanswer_");
	end = window.location.href.indexOf(".html");
	index = window.location.href.substring(start + 9, end);
	index--;
	if (typeof answers != "undefined") {
		var ans = answers.split(":");
		if (typeof document.all.answer.type != "undefined") {
			//填空
			if (ans[index] != "Z")
				document.all.answer.value = ans[index];
		} else {
			//单选，多选
			for (i = 0; i < document.all.answer.length; i++) {
				if (document.all.answer[0].type == "radio") {
					if (document.all.answer[i].value == ans[index])
						document.all.answer[i].checked = true;
				}
				if (document.all.answer[0].type == "checkbox") {
					var c_answer = ans[index].split(",");
					for (j = 0; j < c_answer.length; j++) {
						if (document.all.answer[i].value == c_answer[j])
							document.all.answer[i].checked = true;
					}
				}
			}
		}
	}
}

function readAnswer(index){
  if (typeof answers != "undefined"){
    var ary2 = answers.split(":");
    if (ary2[index-1] != "Z")
      eval("document.all.your"+index+".innerHTML=ary2[index-1]");
  }
}

/*
 *记录答题信息
 */
function writeAnswer(idx){
	
	if (typeof document.all.answer.type != "undefined") {
		//填空
		var tmp = document.all.answer.value;
	} else {
		//单选，多选
		var tmp = "";
		for (i = 0; i < document.all.answer.length; i++) {
			if (document.all.answer[i].checked == true)
				tmp += answer[i].value + ",";
		}
		tmp = tmp.substring(0, tmp.length - 1);
	}

	var ary2 = answers.split(":");
	if (typeof tmp != "undefined")
		ary2[idx - 1] = tmp;

	var ans = "";
	for (j = 0; j < ary2.length; j++) {
		ans += ary2[j] + ":";
	}
	ans = ans.substring(0, ans.length - 1);


	if (typeof ans != "undefined")
		document.cookie = "answers=" + ans;
}

/**
 *上一题
 */

function previous() {
	start = window.location.href.indexOf("doanswer_");
	end = window.location.href.indexOf(".html");
	idx = window.location.href.substring(start + 9, end);

	writeAnswer(idx);
	
	idx--;
	window.location.replace("doanswer_" + idx + ".html");
}

/**
 *下一题
 */
function next() {
	start = window.location.href.indexOf("doanswer_");
	end = window.location.href.indexOf(".html");
	idx = window.location.href.substring(start + 9, end);

	writeAnswer(idx);
	
	idx++;
	window.location.replace("doanswer_" + idx + ".html");
}

/**
 *复查
 */
function recheck(){
	start = window.location.href.indexOf("doanswer_");
	end = window.location.href.indexOf(".html");
	idx = window.location.href.substring(start + 9, end);

	writeAnswer(idx);
	
	window.location.replace("recheck.html");
}

/**
 *交卷
 */
function submit(){
	
	if (!confirm("你确定要交卷么？"))
		return false;
	else {
		start = window.location.href.indexOf("doanswer_");
		end = window.location.href.indexOf(".html");
		idx = window.location.href.substring(start + 9, end);
		writeAnswer(idx);
		
		window.location.replace("result.html");
	}
}

function quit(){
	if (!confirm("你确定要放弃本次测试么？")) 
		return false;
	else{
		window.location.replace("result.html");
	}
}

function modify(i){
	window.location.replace("doanswer_"+i+".html");
}

function doit(){
	window.location.replace("doanswer_1.html");
}

function toFirst(){
	window.location.replace("first.html");
}

function toResult(){
	window.location.replace("result.html");
}

function readResult(answerary,pointary,answerpointary){
	var totalpoint = 0;
	if (answerpointary.length > 0) {
		if (typeof answers != "undefined" && answers.indexOf(":") >  - 1) {
			var ans = answers.split(":");
			for (index = 0; index < answerpointary.length; index++) {
				if (typeof answerpointary[index][ans[index]] != "undefined")
					totalpoint += answerpointary[index][ans[index]];
			}
		}
	} else {
		if (typeof answers != "undefined" && answers.indexOf(":") >  - 1) {
			var ans = answers.split(":");
			for (index = 0; index < answerary.length; index++) {
				if (ans[index] == answerary[index])
					totalpoint += pointary[index];
			}
		}
  }

  document.all.totalpoint.innerHTML = totalpoint;
  if (typeof document.all.score != "undefined"){
  	document.all.score.innerHTML = totalpoint*rate;
  }
  var hh = Math.floor(reduce_time/(60*60));
  var mm = Math.floor((reduce_time-hh*60*60)/60);
  var ss = reduce_time - hh*60*60 - mm*60;
  document.all.dotime.innerHTML= hh+":"+mm+":"+ss;
  if (typeof document.all.rr != "undefined") document.all.rr.innerHTML= reduce_time;

  var cookie = document.cookie;
  var ary2 = cookie.split(";");
  var i = 0;
  while (i < ary2.length){
    var ary3 = ary2[i].split("=");
    eval("document.cookie=\"" + ary3[0] + "=;expires=Fri, 02-Jan-1970 00:00:00 GMT\"");
  	i++;
  }
  //生成验证信息
  document.cookie = "isconfirm=ok;path=/;domain=webfox.it.sohu.com";
}

function save_score(pid){
	window.open("/servlet/SaveScore?pid=" + pid + 
		"&dotime="+document.all.rr.innerHTML+"&score=" + 
		document.all.score.innerHTML,"","width=500,height=320,toolbar=no,menubar=no,status=no,resizable=yes");
}