var userName;
var quiestionXML;
var questionsNode;
var currentQuestion;
var answer = new Array();
startDia = function() {
	userName = document.getElementById("user_name").value;
	if (userName.length > 8) {
		alert("お名前は8文字までです。");
		return;
	} else if(userName.length == 0) {
		alert("お名前が未入力です。");
		return;
	}

	quiestionXML = loadQuestions();
	
	var kindNodes = quiestionXML.getElementsByTagName("kind");
	kind =  getKind();
	for (var index = 0 ; index < kindNodes.length ; index++) {
		if (kindNodes[index].getAttribute("kind") == kind) {
			questionsNode = kindNodes[index];
			break;
		}
	}

	currentQuestion = 0;
	document.getElementById("EntrySection").style.display = "none";
	document.getElementById("RankingSection").style.display = "none";
	document.getElementById("MessageSection").style.display = "none";
	document.getElementById("questionSection").style.display = "";
	drawQuestion();

}

drawQuestion = function() {
	
	var questionNode;
	
	for (var index = 0 ; index < questionsNode.getElementsByTagName("question").length ; index++) {
		if (questionsNode.getElementsByTagName("question")[index].getAttribute("id") == currentQuestion) {
			questionNode = questionsNode.getElementsByTagName("question")[index];
			break;
		}
	}
	
	document.getElementById("questionText").innerHTML = questionNode.childNodes[0].nodeValue + "<br />";
	
	var choicesNode = questionNode.getElementsByTagName("choices");
	for (var index = 0 ; index < choicesNode.length ; index++) {
		document.getElementById("choices" + index.toString()).innerHTML = choicesNode[index].textContent || choicesNode[index].text	;
	}
	
	if (currentQuestion == questionsNode.getElementsByTagName("question").length - 1) {
		var button = document.getElementById("nextButton");
		button.src = "img/commit.png";
		button.alt = "結果を表示";
	}
}

nextQuestion = function() {
	if (currentQuestion == questionsNode.getElementsByTagName("question").length - 1) {
		entry();
	} else {
		answer[currentQuestion] = getSelectedChoice();
		currentQuestion++;
		drawQuestion();
		document.getElementsByName("choices")[0].checked = true;;
	}
}

entry = function() {
	answer[currentQuestion] = getSelectedChoice();
	
	var XmlObj = GetXMLHttp();
	XmlObj.open("POST", "/User/add/", true);
	
	XmlObj.onreadystatechange = function () {
		if (XmlObj.readyState == 4 && XmlObj.responseText) {
			var limit = new Date();
			var prev = new Date();
			limit.setTime(limit.getTime() + (1000 * 60 * 60 * 24 * 30));
			prev.setYear(prev.getYear()-1);
			document.cookie = "abnormalUserID=" + XmlObj.responseText + "; expires=" + prev.toGMTString();
			document.cookie = "abnormalUserID=" + XmlObj.responseText + "; expires=" + limit.toGMTString();
			sendAnswer();
		}
	}
	var data = "";
	data += "name=" + encodeURIComponent(userName);
	data += "&blog_url=" + encodeURIComponent("");
	XmlObj.setRequestHeader("Content-Type" ,"application/x-www-form-urlencoded; charset=UTF-8");
	XmlObj.send(data);
}

entryRanking = function() {
	var blogURL = document.getElementById("blog_url").value;
	if (blogURL == "") {
		alert("BlogのURLを入力してください");
		return;
	}
	var XmlObj = GetXMLHttp();
	XmlObj.open("POST", "/User/update/", true);
	
	XmlObj.onreadystatechange = function () {
		if (XmlObj.readyState == 4 && XmlObj.responseText) {
			var scriptArea = document.getElementById("script_area");
			scriptArea.style.display = "";
			scriptArea.value = XmlObj.responseText;
		}
	}
	var data = "blog_url=" + encodeURIComponent(blogURL);
	XmlObj.setRequestHeader("Content-Type" ,"application/x-www-form-urlencoded; charset=UTF-8");
	XmlObj.send(data);
}


sendAnswer = function() {
	var XmlObj = GetXMLHttp();
	XmlObj.open("POST", "/Diagnosis/entry/", true);
	
	XmlObj.onreadystatechange = function () {
		if (XmlObj.readyState == 4 && XmlObj.responseText) {
			document.location.href = "http://dia.bloglue.jp/User/diaResult?user_id=" + encodeURIComponent(XmlObj.responseText);
		}
	}
	var data = "question_kind=" + encodeURIComponent(getKind());
	
	for (var index = 0 ; index < answer.length ; index++) {
		data += "&answer[" + index + "]=" + answer[index];
	}
	
	XmlObj.setRequestHeader("Content-Type" ,"application/x-www-form-urlencoded; charset=UTF-8");
	XmlObj.send(data);
}


GetXMLHttp = function() {
	if(window.ActiveXObject) {
		return new ActiveXObject("Msxml2.XMLHTTP");
	} else if(window.XMLHttpRequest) {
		return new XMLHttpRequest();
	}
}

getKind = function() {
	var kinds = document.getElementsByName("kinds");
	
	for (var index = 0 ; index < kinds.length ; index++) {
		if (kinds[index].checked) {
			return kinds[index].value;
		}
	}
}

getSelectedChoice = function() {
	var choices = document.getElementsByName("choices");
	for (var index = 0 ; index < choices.length ; index++) {
		if (choices[index].checked) {
			return choices[index].value;
		}
	}
}

loadQuestions = function() {
	var quiestionXML = getXMLClass();
	quiestionXML.load("http://dia.bloglue.jp/Questions/questionsXML");
	return quiestionXML;
}



getXMLClass = function() {
	var xml;
	
	if (document.all) {
		xml = new ActiveXObject("Msxml2.DOMDocument");
	} else {
		xml = document.implementation.createDocument("", "", null);
	}
    xml.async = false;
    return xml;
}

