
function collapseAllQuestions() {
	changeDisplayByClassName('exam exam_collapse', 'block');
	changeDisplayByClassName('exam exam_expand', 'none');
	changeDisplayByClassName('exam exam_update', 'none');
	changeDisplayByClassName('exam exam_collapse', 'block');
	changeDisplayByClassName('exam exam_expand', 'none');
	changeDisplayByClassName('exam exam_update', 'none');
}

function expandAllQuestions() {
	changeDisplayByClassName('exam exam_collapse', 'none');
	changeDisplayByClassName('exam exam_expand', 'block');
	changeDisplayByClassName('exam exam_update', 'none');
}

function collapseQuestion(n) {
	changeDisplay('exam_' + n + '_collapse', 'block');
	changeDisplay('exam_' + n + '_expand', 'none');
	changeDisplay('exam_' + n + '_update', 'none');
}

function expandQuestion(n) {
	changeDisplay('exam_' + n + '_collapse', 'none');
	changeDisplay('exam_' + n + '_expand', 'block');
	changeDisplay('exam_' + n + '_update', 'none');
}

function deleteQuestion(qid) {
	if(confirm("削除してもよろしいですか？")) {
		var form = getById('delete_form');
		var deleteId = getById('delete_qid');
		deleteId.value = qid;
		form.submit();
	}
}






var addChoices = new Array();

function expandAdd() {
	var collapses = getElementsByClassName('exam exam_collapse');
	var expands = getElementsByClassName('exam exam_expand');
	var updates = getElementsByClassName('exam exam_update');
	for(var i = 0; i < updates.length; i++) {
		if(updates[i].style.display == 'block') {
			collapses[i].style.display = 'none';
			expands[i].style.display = 'block';
			updates[i].style.display = 'none';
		}
	}
	changeDisplay('add_expand', 'block');
	changeDisplay('add_collapse', 'none');
}

function addQuestion() {
	var error = '';
	if(getById('add_question').value == '') {
		error += "問題を記述してください。\n";
	}
	var regexp2 = new RegExp("^[0-9]+$");
	if(!getById('add_point').value.match(regexp2)) {
		error += "配点を半角数字で入力してください。\n";
	}
	/*
	if(getById('add_correct_choice').value == '') {
		error += "正解を選択してください。";
	}
	*/
	if(error) {
		alert(error);
	} else {
		var form = getById('add_form');
		form.submit();
	}
}

function refleshAddChoices() {
	var choices = getById('add_choices');
	var choicesList = getById('add_choices_list');
	choicesList.options.length = 0;
	var list = '';
	for(var i = 0; i < addChoices.length; i++ ) {
		list += createAddChoice(addChoices[i]);
		choicesList.options[i] = new Option(addChoices[i],addChoices[i],true,true);
	}
	choices.innerHTML = list;
//	alert(list);
//	alert(choicesList.innerHTML);
}

function addChoice() {
	var input = getById('input_add_choice');
	var regexp = new RegExp("^.+$");
	if(!input.value.match(regexp)) {
		 alert("追加する選択肢を入力してください。");
		 return;
	}
	for(var i = 0; i < addChoices.length; i++) {
		if(input.value == addChoices[i]) {
			alert('同じ内容の選択肢がすでに存在しています。');
			return;
		}
	}
	addChoices.push(input.value);
	refleshAddChoices();
	input.value = '';
	input.focus();
}

function createAddChoice(value) {
	var html = '';
	html += '<li id="add_choices_li_' + value + '">';
	html += '<input id="add_choice_' + value + '" type="radio" name="add_choice" value="' + value + '" onclick="changeAddRightChoice(\'' + value + '\');" /> ' + value + '';
	html += '<a href="javascript:deleteAddChoice(\'' + value + '\');"><img src="files/images/delete.gif" alt="削除" /></a>';
	html += '</li>';
	return html;
}

function deleteAddChoice(value) {
	var array = new Array();
	for(var i = 0; i < addChoices.length; i++ ) {
		if(addChoices[i] == value) {
			var target1 = getById('add_choice_right');
			var target2 = getById('add_correct_choice');
			var target3 = getById
			if(target2.value == value) {
				target1.innerHTML = '&nbsp;';
				target2.value = '';
				target3.innerHTML = '';
				colorBlinkStart(target3.id, "red", 100, 3);
				//borderBottomBlinkStart(target1.id, "green", 100, 3);
				//target1.className = 'fade';
				//readyToFade();
			}
		} else {
			array.push(addChoices[i]);
		}
	}
	addChoices = array;
//	alert(addChoices);
	refleshAddChoices();
}

function changeAddRightChoice(value) {
	var target1 = getById('add_choice_right');
	var target2 = getById('add_correct_choice');
	var target3 = getById('add_choice_right_disp');
	var choice = getById('add_choice_' + value).value;
	target1.value = choice;
	target2.value = choice;
	target3.innerHTML = choice;
	
	colorBlinkStart(target3.id, "red", 100, 3);
	//borderBottomBlinkStart(target1.id, "green", 100, 3);
	//target1.className = 'fade';
	//readyToFade();
}






function updateQuestion(n) {
	var collapses = getElementsByClassName('exam exam_collapse');
	var expands = getElementsByClassName('exam exam_expand');
	var updates = getElementsByClassName('exam exam_update');
	for(var i = 0; i < updates.length; i++) {
		if(updates[i].style.display == 'block') {
			collapses[i].style.display = 'none';
			expands[i].style.display = 'block';
			updates[i].style.display = 'none';
		}
	}
	changeDisplay('exam_' + n + '_collapse', 'none');
	changeDisplay('exam_' + n + '_expand', 'none');
	changeDisplay('exam_' + n + '_update', 'block');
	changeDisplay('add_expand', 'none');
	changeDisplay('add_collapse', 'block');
	refleshUpdateChoices(n);
//	location.hash = '#a_update_' + n;
}

function refleshUpdateChoices(n) {
	var choices = getById('update_choices_' + n);
	var list = '';
	for(var i = 0; i < updateChoices[n].length; i++ ) {
		list += createUpdateChoice(n, i+1, updateChoices[n][i]);
	}
	if(choices) {
		choices.innerHTML = list;
	}
	var correct = getById('update_correct_choice_num_' + n);
	if(correct) {
	var i = correct.value;
		if(i) {
			changeUpdateRightChoice(n, i);
		}
	}
//	alert(choices.innerHTML);
}

function setUpdateChoices(n) {
	var choicesList = getById('update_choices_list_' + n);
	choicesList.options.length = 0;
	for(var i = 0; i < updateChoices[n].length; i++ ) {
		choicesList.options[i] = new Option(desanitize(updateChoices[n][i].value),desanitize(updateChoices[n][i].value),true,true);
	}
//	alert(choicesList.innerHTML);
}

function addUpdateChoice(n) {
	var input = getById('input_update_choice_' + n);
	var regexp = new RegExp("^.+$");
	if(!input.value.match(regexp)) {
		 alert("追加する選択肢を入力してください。");
		 return;
	}
	for(var i = 0; i < updateChoices[n].length; i++) {
		if(input.value == updateChoices[n][i].value) {
			alert('同じ内容の選択肢がすでに存在しています。');
			return;
		}
	}
	updateChoices[n].push({cid:'',value:input.value});
	setUpdateChoices(n);
	var form = getById('update_form_choice_' + n);
	form.submit();
}

function updateSubmit(n) {
	var error = '';
	var targetQuestion = getById('update_question_' + n);
	if(targetQuestion && targetQuestion.value == '') {
		error += "問題を記述してください。\n";
	}
	var regexp2 = new RegExp("^[0-9]+$");
	var targetPoint = getById('update_point_' + n);
	if(targetPoint && !targetPoint.value.match(regexp2)) {
		error += "配点を半角数字で入力してください。\n";
	}
	if(error) {
		alert(error);
	} else {
		var form = getById('update_form_' + n);
		form.submit();
	}
}

function createUpdateChoice(n, i, choice) {
	var html = '';
	html += '<li id="update_choices_li_' + n + '_' + i + '">';
	html += '<input id="update_choice_' + n + '_' + i + '" type="radio" name="update_choice" value="' + choice.value + '" onclick="changeUpdateRightChoice(' + n + ',' + i + ');" /> ' + choice.value + '';
	html += '<a href="javascript:deleteUpdateChoice(' + n + ',' + i + ');"><img src="files/images/delete.gif" alt="削除" /></a>';
	html += '</li>';
	return html;
}

function deleteUpdateChoice(n, num) {
	var array = new Array();
	var target1 = getById('update_choice_right_' + n);
	var target2 = getById('update_correct_choice_' + n);
	var target3 = getById('update_correct_choice_num_' + n);
	var target4 = getById('update_choice_right_' + n + '_disp');
	
	for(var i = 0; i < updateChoices[n].length; i++ ) {
		if(i == num-1) {
			if(target3.value == num) {
				target1.value = '';
				target2.value = '';
				target3.value = '';
				target4.innerHTML = '';
				//target1.className = 'fade';
				//readyToFade();
			}
		} else {
			array.push(updateChoices[n][i]);
		}
	}
	updateChoices[n] = array;
	setUpdateChoices(n);
	var form = getById('update_form_choice_' + n);
	form.submit();
}

function changeUpdateRightChoice(n, i) {
	var target1 = getById('update_choice_right_' + n);
	var target2 = getById('update_correct_choice_' + n);
	var target3 = getById('update_correct_choice2_' + n);
	var target4 = getById('update_correct_choice_num_' + n);
	var target5 = getById('update_choice_right_' + n + '_disp');
	var choice = getById('update_choice_' + n + '_' + i)
	choice.checked = true;
	target1.value = choice.value;
	target2.value = choice.value;
	target3.value = choice.value;
	target4.value = i;
	target5.innerHTML = choice.value;

    //var a;
    //a += 'update_choice_right_' + n + ' : ' + target1.value + "\n";
    //a += 'update_correct_choice_' + n + ' : ' + target2.value + "\n";
    //a += 'update_correct_choice2_' + n + ' : ' + target3.value + "\n";
    //a += 'update_correct_choice_num_' + n + '_' + i + ' : ' + target4.value + "\n";
    //alert(a);
	
	colorBlinkStart(target5.id, "red", 100, 3);
	//target1.className = 'fade';
	//readyToFade();
}

function fadeById(id) {
	var target = getById(id);
	if(target) {
		target.className = 'fade';
		readyToFade();
	}
}

function fade(obj) {
	if(obj) {
		obj.className = 'fade';
		readyToFade();
	}
}





function checkExam() {
	var correct;
	var update;
	var msg = '';
	var regexp = new RegExp("^.+$");
	if(!document.getElementsByName('update_form')) {
		msg += "設問がありません。";
	}
	for(var i = 1; i < updateChoices.length; i++) {
		update = updateChoices[i];
		if(update.length < 1) {
			msg += i + "問目の選択肢がありません。\n";
		}
		correct = getById('update_correct_choice_' + i).value;
		if(!correct.match(regexp)) {
			msg += i + "問目の正解が選択されていません。\n";
		}
	}
	if(msg) {
		alert(msg);
		return false;
	} else {
		return true;
	}
}

function checkDistribute(url) {
	if(checkExam()) {
		location.href = url;
	}
}

function checkPrint(url) {
	if(checkExam()) {
		window.open(url, "_blank", "width=600,height=600,resizable=yes,scrollbars=yes,menubar=yes,directories=yes,personalbar=yes,status=yes,toolbar=yes");
	}
}

