// application.js

window.onload = function() {
	document.getElementById("inquiry").style.zIndex = "2";
	document.getElementById("confirm").style.zIndex = "1";
}

function swap() {
	var z = document.getElementById("inquiry").style.zIndex;
	if (z == 1) {
		document.getElementById("inquiry").style.zIndex = 2;
		document.getElementById("confirm").style.zIndex = 1;
	} else {
		document.getElementById("confirm").style.zIndex = 2;
		document.getElementById("inquiry").style.zIndex = 1;
	}
}

function wrap() {
	var val = document.getElementById("txtContent").value;
	while (true) {
		if (val.indexOf("\n") != -1) {
			val = val.replace("\n", "<br>");
		} else {
			break;
		}
	}
	return val;
}

function check() {
	var ret = true;
	var names = new Array("txtName", "txtEmail1", "txtEmail2", "txtPhone", "txtPref", "txtCity", "txtAddress", "txtContent");
	
	//必須チェック
	for (var i = 0; i < names.length; i++) {
		var tag = document.getElementById(names[i]);
		if (tag.value == "") {
			tag.style.backgroundColor = "red";
			ret = false;
		} else {
			tag.style.backgroundColor = "";
			var id = tag.id.replace("txt", "cfm");
			var val = tag.value;
			if (id == "cfmContent") {
				val = wrap();
			}
			document.getElementById(id).innerHTML = val;	
		}
	}
	
	//相関チェック
	var email1 = document.getElementById("txtEmail1");
	var email2 = document.getElementById("txtEmail2");
	if (email1.value != email2.value) {
		email1.style.backgroundColor = "red";
		email2.style.backgroundColor = "red";
		ret = false;
	}
	
	if (!ret) {
		alert("入力に誤りがあります。");
		return;
	}
	
	//値コピー
	names = new Array("txtPref", "txtBuild", "txtFile1", "txtFile2", "txtFile3");
	for (var i = 0;  i < names.length; i++) {
		var id = names[i].replace("txt", "cfm");
		document.getElementById(id).innerHTML = document.getElementById(names[i]).value;
	}

	swap();
}

