var questionNUm = 0;
/* 数字转汉字数字 */
var chnNumChar = [ "零", "一", "二", "三", "四", "五", "六", "七", "八", "九" ];
var chnUnitSection = [ "", "万", "亿", "万亿", "亿亿" ];
var chnUnitChar = [ "", "十", "百", "千" ];
/* 快速创建答案列表 */
var score = 0;
var total_score = 0;
var answerLen = 0;
$(function() {
	function creat(str_ans, a_num, fraction) {
		// $(".section").remove();
		answer_index_b = $(".section").length + 1;
		/*
		 * $(".content").append('
')
		 */
		var str = str_ans.split('');
		console.log(str);
		for (i = 0; i < str.length; i++) {
			if (str[i] == " ") {
				str.splice(i, 1);
				i = i - 1;
			}
		}
		q_num = str.length;
		$(".content")
				.append(
						'第'
								+ answer_index_b
								+ '大题(单选)总分: '
								+ q_num
								+ ' X '
								+ fraction
								+ ' = '
								+ (q_num * fraction)
								+ ' 分
  '
										+SectionToChinese(i+1)+'、'
										+ tixing
										+ '(总分:'
										+ data.questions[i].questions.length
										+ " X "
										+ (parseFloat(data.questions[i].sug_score) / data.questions[i].questions.length)
										+ " = "
										+ data.questions[i].sug_score
										+ '分)'
										+ ''
										+ data.questions[i].content
										+ '
  ');
			} else {
				if (i == 0) {
					$(".content")
							.append(
									'');
				}
				$(".section0").find(".question_list").append(
						'');
				for (var k = 0; k < parseInt(data.questions[i].option_num); k++) {
					str_temp = data.questions[i].answer;
					$(".section0").find(".question" + (i + 1) + ' ul').append(
							''
									+ String.fromCharCode(0x41 + k) + '');
					for (var l = 0; l < str_temp.length; l++) {
						str_temp_arry = str_temp.split('');
						code = str_temp_arry[l].charCodeAt();
						on_index = code - 65;
						if (on_index == k) {
							$(".section0").find(
									".question" + (i + 1) + ' ul li').eq(
									on_index).attr("class",
									"btn btn-default on");
						}
					}
				}
			}
			if (data.questions[i].questions) {
				for (var j = 0; j < data.questions[i].questions.length; j++) {
					$(".section-" + i).find(".question_list").append(
							'');
					if ($(".section-" + i).attr("data-type") == "4") {
						for (var k = 0; k < parseInt(data.questions[i].questions[j].option_num); k++) {
							str_temp = data.questions[i].questions[j].answer;
							if (k % 2 == 0) {
								$(".section-" + i).find(
										".question" + (j + 1) + ' ul').append(
										'√');
							} else if (k % 2 == 1) {
								$(".section-" + i).find(
										".question" + (j + 1) + ' ul').append(
										'×');
							}
							if (str_temp == "√")
								$(".section-" + i).find(
										".question" + (j + 1) + ' ul li').eq(0)
										.attr("class", "btn btn-default on");
							else
								$(".section-" + i).find(
										".question" + (j + 1) + ' ul li').eq(1)
										.attr("class", "btn btn-default on");
						}
					}else if($(".section-" + i).attr("data-type") == "6"){
						str_temp = data.questions[i].questions[j].answer;
						$(".section-" + i).find(
								".question" + (j + 1) + ' ul').append(
								''+str_temp+'');
						if(!$(".section-" + i).hasClass("number"))
							$(".section-" + i).addClass("number");
					} else {
						for (var k = 0; k < parseInt(data.questions[i].questions[j].option_num); k++) {
							str_temp = data.questions[i].questions[j].answer;
							$(".section-" + i).find(
									".question" + (j + 1) + ' ul').append(
									''
											+ String.fromCharCode(0x41 + k)
											+ '');
							for (var l = 0; l < str_temp.length; l++) {
								str_temp_arry = str_temp.split('');
								code = str_temp_arry[l].charCodeAt();
								on_index = code - 65;
								if (on_index == k) {
									$(".section-" + i).find(
											".question" + (j + 1) + ' ul li')
											.eq(on_index).attr("class",
													"btn btn-default on");
								}
							}
						}
					}
				}
			}
		}
	}
	/* 快速建题中,每输入五个答案就加一个空格 */
	$("#answer").on("input propertychange", function(event) {
		var answerStr = "";
		answerStr = $("#answer").val();
		var result = "";
		if (answerStr.length > 5) {
			answerStr = answerStr.replace(new RegExp(" ", "gm"), "");
			for (var i = 0; i < answerStr.length; i++) {
				result += answerStr[i];
				if (i % 5 == 4 && i != answerStr.length - 1) {
					result += " ";
				}
			}
		} else {
			result = answerStr;
		}
		$("#answer").val(result);
		if (answerStr.length > 0) {
			$(".tips").text("共" + answerStr.length + "题");
		} else {
			$(".tips").text("");
		}
	})
	function SectionToChinese(section) {
		var strIns = '', chnStr = '';
		var unitPos = 0;
		var zero = true;
		while (section > 0) {
			var v = section % 10;
			if (v === 0) {
				if (!zero) {
					zero = true;
					chnStr = chnNumChar[v] + chnStr;
				}
			} else {
				zero = false;
				strIns = chnNumChar[v];
				strIns += chnUnitChar[unitPos];
				chnStr = strIns + chnStr;
			}
			unitPos++;
			section = Math.floor(section / 10);
		}
		return chnStr;
	}
	if (template != null && template.length > 0) {
		$("#addQuestion").attr("disabled", "disabled");
		$("#fast").attr("disabled", "disabled");
		for (var i = 0; i < template.length; i++) {
			if (template[i].type == 1) {
				var answer = "";
				for (var j = 0; j < template[i].questionNum; j++) {
					answer += "1";
				}
				creat(answer, template[i].optionNum, template[i].score);
			}
			if (template[i].type == 2) {
				addQuestion(5, template[i].optionNum, 1,
						template[i].questionNum, template[i].score);
			}
		}
		if (testData != null) {
			$(".header_box h1").html(testData.title);
			$("#time").html(testData.exam_time);
			if (testData.score != null && testData.score > 0
					&& testData.score != '') {
				$("#score_all").html(testData.score);
			}
		}
	} else if (testData != null && testData.questions != null
			&& testData.questions.length > 0) {
		fastSort(testData.questions, 0, testData.questions.length - 1);
		for (var i = 0; i < testData.questions.length; i++) {
			if (testData.questions[0].questions
					&& testData.questions[0].questions.length > 1) {
				fastSort(testData.questions[i].questions, 0,
						testData.questions[i].questions.length - 1);
			}
		}
		creatHtml(testData);
	} else {
		if (testData != null) {
			$(".header_box h1").html(testData.title);
			$("#time").html(testData.exam_time);
			if (testData.score != null && testData.score > 0
					&& testData.score != '') {
				$("#score_all").html(testData.score);
			}
		}
	}
	$(".content").on("focus", ".number .on", function() {
		if ($(this).text() == "请输入答案")
			$(this).text("");
	})
	$(".content").on("blur", ".number .on", function() {
		if ($(this).text() == "") {
			$(this).text("请输入答案");
		}
	})
})
/**
 * 数字校验
 * 
 * @param e
 */
function keydown2(e) {
	var reg = /^[0-9]*$/;
	console.log(reg.test(e.text()));
	console.log(e.text())
	if (!reg.test(e.text())) {
		e.text("");
	}
}
function NumberCheck(num, obj) {
	var str = num;
	// callback(str,obj)
	console.log("num:" + num)
	var len1 = str.substr(0, 1);
	var len2 = str.substr(1, 1);
	// 如果第一位是0,第二位不是点,就用数字把点替换掉
	if (str.length > 1 && len1 == 0 && len2 != ".") {
		str = str.substr(1, 1);
	}
	// 第一位不能是.
	if (len1 == ".") {
		str = "";
	}
	// 限制只能输入一个小数点
	if (str.indexOf(".") != -1) {
		var str_ = str.substr(str.indexOf(".") + 1);
		if (str_.indexOf(".") != -1) {
			str = str.substr(0, str.indexOf(".") + str_.indexOf(".") + 1);
		}
	}
	// 正则替换,保留数字和小数点
	str = str.replace(/[^\-?\d.]/g, '')
	obj.innerText = str
	callback(str, obj)
}
function callback(tp, obj) {
	var _div = obj// document.querySelector('.number .on');
	var range = document.createRange();
	range.selectNodeContents(_div);
	range.collapse(false);
	var sel = window.getSelection();
	sel.removeAllRanges();
	sel.addRange(range);
}