//クリック時に色を変える
function focusColor(i){
	i.style.borderColor='#10662f';
	i.style.backgroundColor='#FFFFFF';
}
function blurColor(i){
	i.style.borderColor='#CCCCCC';
	i.style.backgroundColor='#FAFAFA';
}

//以下Ajax
function createXMLHttpRequest(cbFunc) {
	var XMLhttpObject = null;
	try{
		XMLhttpObject = new XMLHttpRequest();
	}catch(e){
		try{
			XMLhttpObject = new ActiveXObject("Msxml2.XMLHTTP");
		}catch(e){
			try{
				XMLhttpObject = new ActiveXObject("Microsoft.XMLHTTP");
			}catch(e){
				return null;
			}
		}
	}
	if (XMLhttpObject) XMLhttpObject.onreadystatechange = cbFunc;
	return XMLhttpObject;
}

httpObj = null;
function dataSave(phpname,postdata) {
	sendData ="use="+encodeURI(postdata[0]);
	sendData+= "&name="+encodeURI(postdata[1]);
	sendData+= "&email="+encodeURI(postdata[2]);
	sendData+= "&subject="+encodeURI(postdata[3]);
	sendData+= "&content="+encodeURI(postdata[4]);
	
	httpObj = createXMLHttpRequest(displayData);
	if (httpObj) {
		httpObj.open("POST",phpname,true);
		httpObj.setRequestHeader('Content-Type','application/x-www-form-urlencoded;charset=UTF-8');
		httpObj.send(sendData);
	}
}

function displayData() {
	if ((httpObj.readyState == 4) && (httpObj.status == 200)) {
		//完了
		
		//入力部分を表示。
		$("entry").style.display = "block";
		//確認部分を消す。
		$("confirm").style.display = "none";
		
		if (httpObj.responseText>0) {
			$("success").style.display = "block";
			$("inqform").style.display = "none";
		}else{
			$("failure").style.display = "block";
			//$("error-code").innerHTML = "（Error Code：<span>" + httpObj.responseText + "</span>）<br />";
		}
		$("submitbutton").value = "送信";
	}else if (httpObj.readyState == 4) {
		$("failure").style.display = "block";
		$("error-code").innerHTML = "（Error Code：<span>" + httpObj.status + "</span>）<br />";
		//入力部分を表示。
		$("entry").style.display = "block";
		//確認部分を消す。
		$("confirm").style.display = "none";
		
		$("submitbutton").value = "送信";
	}else{
		//処理中
		$("submitbutton").value = "送信中...";
	}
}

function checkMailData(baai) {
	//初期化
	tf_maildata = true;
	//出力部分等を消す。
	$("success").style.display = "none";
	$("mail-error").style.display = "none";
	$("failure").style.display = "none";
	$("error-code").innerHTML = null;
	
	//取り込み
	js_use = "";//*
	js_name= document.inquiry.name.value;//*
	js_email= document.inquiry.email.value;//*
	js_subject = document.inquiry.subject.value;
	js_content= document.inquiry.content.value;//*
	
	//エラーを返す
	//name
	if (!js_name) {
		addText("name","add","氏名を入力してください。");
		tf_maildata = false;
	}else{
		addText("name","add","");
	}
	//use
	ns = 4;
	for (ii=0; ii<2; ii++) if (document.inquiry["use"][ii].checked) ns = ii;
	if (ns == 4) {
		addText("use","add","どちらか一つを選択してください。");
		tf_maildata = false;
	}else{
		js_use= document.inquiry["use"][ns].value;
		//js_use= fObj;
		addText("use","add","");
	}
	//email
	tf_email = js_email.match(/^[a-zA-Z0-9_\.\-]+?@[A-Za-z0-9_\.\-]+$/);
	if (!tf_email) {
		addText("email","add","メールアドレスが正しく無い可能性があります。");
		tf_maildata = false;
	}else{
		addText("email","add","");
	}
	//subject（セレクトボタンの時）
	if (!js_subject) {
		addText("subject","add","どれか一つを選択してください。");
		tf_maildata = false;
	}else{
		addText("subject","add","");
	}
	//content
	if (!js_content) {
		addText("content","add","入力してください。");
		tf_maildata = false;
	}else{
		addText("content","add","");
	}
	
	//確認
	if (tf_maildata) {
		if (baai == "soushin") {
			//送信画面
			//出力
			postdata = new Array(
				js_use,
				js_name,
				js_email,
				js_subject,
				js_content
			);
			dataSave("./mail.php",postdata);
		} else if (baai == "kakunin") {
			//確認画面
			//エラー表示を初期化。
			initializationText("add");
			//確認部分を表示。
			$("confirm").style.display = "block";
			//入力部分を消す。
			$("entry").style.display = "none";
			//確認画面を出力。
			addText("use","in",tagCuttere(js_use));
			addText("name","in",tagCuttere(js_name));
			addText("email","in",tagCuttere(js_email));
			addText("subject","in",tagCuttere(js_subject));
			addText("content","in",tagCuttere(js_content));
		} else {
			//編集画面
			//入力部分を表示。
			$("entry").style.display = "block";
			//出力部分を消す。
			$("confirm").style.display = "none";
			//確認画面を初期化。
			initializationText("in");
		}
	}else{
		//出力（エラーメッセージを表示。）
		//入力部分を表示。
		$("entry").style.display = "block";
		//出力部分を消す。
		$("confirm").style.display = "none";
		//エラーメッセージを表示。
		$("mail-error").style.display = "block";
	}
}

function addText(nn,aa,tx) {
	if (aa == "add") {
		nn = "e-" + nn;
		$(nn).innerHTML = "<em style='color:#DE0B0B;'>" + tx + "</em>";
	}else{
		nn = "c-" + nn;
		$(nn).innerHTML = tx;
	}
}

function initializationText(rx) {
	//初期化。
	addText("use",rx,"");
	addText("name",rx,"");
	addText("email",rx,"");
	addText("subject",rx,"");
	addText("content",rx,"");
}

function tagCuttere(rx) {
	rx = rx.replace(/</g,"&lt;");
	rx = rx.replace(/>/g,"&gt;");
	rx = rx.replace(/\r|\n/g,"<br />");
	return rx;
}



