/*
 * フォーム内容を送信し、検証結果を表示します。
 * 内容に問題がない場合、そのままお問い合わせを受領します。
 * Ver. 20071022
 */



/*
 * form全体の基本設定など
 * window.onload で呼び出されています。
 */
$(function(){
	$("#inquiryform input").focus(function(){
		 $(this).addClass("focus");
	});

	$("#inquiryform input").blur(function(){
		$(this).removeClass("focus");
	});

	$("#inquiryform textarea").focus(function(){
		$(this).addClass("focus");
	});
	
	$("#inquiryform textarea").blur(function(){
		$(this).removeClass("focus");
	});

	if(document.getElementById("inquirybody").value.match(/詳細をご記入ください。/)){
		$("#inquirybody").one("focus",(function(){
			this.value = "";
		}));
	}

	$("#validate").one("click",(function(){
		this.blur();
		validateItems();
		
	}));
});


/*
 * formの内容を検証します。
 */
function validateItems(){
	$("#validate").attr({"src":"http://static.bitcash.jp/corp/contact/loading.gif"});

	var postItem = {};
	postItem["company"] 	= document.getElementById("company").value;
	postItem["division"]	= document.getElementById("division").value;
	postItem["name"]		= document.getElementById("name").value;
	postItem["email"]		= document.getElementById("email").value;
	postItem["phone"]		= document.getElementById("phone").value;
	postItem["zip"] 		= document.getElementById("zip").value;
	postItem["address"]    = document.getElementById("address").value;
	postItem["service"] 	= document.getElementById("service").value;
	postItem["serviceurl"]	= document.getElementById("serviceurl").value;
	postItem["servicedesc"] = document.getElementById("servicedesc").value;
	postItem["inquiry"] 	= document.getElementById("inquiry").options[document.getElementById("inquiry").selectedIndex].value;

	if(document.getElementById("inquirybody").value.match(/詳細をご記入ください。/)){
		postItem["inquirybody"] = "";
	}else{
		postItem["inquirybody"] = document.getElementById("inquirybody").value;
	}

	$.post("/contact/validate/",postItem,validateCallback);
}

/*
 * postデータのcallback処理
 * ステータスは，"success" / "error"
 */
function validateCallback(json, status){
	var result = eval("("+ json +")");
	var response = result["response"];
	if(status == "success"){
		if(result["state"] == 0){
			for(var key in response){	//全部成功した場合。値を修正値に直して表示。
				$("#" + key + "Error").html(" ");
				$("#" + key).parent().html(response[key]["filtered"]);
			}
			$("#inquiryform td").css("padding","10px");
			$("#response").html("<strong>下記の内容でお問い合わせを受け付けました。<br />後ほど弊社営業担当よりご連絡させていただきます。</strong>");
			$("#notice").html("※お問い合わせの内容により、ご返答にお時間を頂く場合がありますのでご了承下さい。<br />※1週間以上弊社からの返信がない場合、弊社からのメールが届いていない事が考えられます。恐れ入りますがご登録頂いたメールアドレスにお間違いがないかご確認の上、再度、本メールフォームよりお問い合わせ頂きますようお願い致します。");
			$("#contactTitle").html('<img src="http://static.bitcash.jp/corp/contact/ttl_complete.gif" width="540" height="50" alt="送信完了" />');
			$("#validate").css("display","none");
			window.location.href = "#top";

			$("#contents").append("<iframe src=\"http://o.advg.jp/oif?aid=1533&pid=2\" width=\"1\" height=\"1\" style=\"display:none\"></iframe>");

		}else{							// 問題がある場合。必須項目にはerrorメッセージを表示する。
			for(var key in response){
				// エラー表示
				if(response[key]["state"] == 1){
					$("#" + key).addClass("error");
					$("#" + key + "Error").html(response[key]["message"]);
				}else{
					$("#" + key).removeClass("error");
					$("#" + key + "Error").html(" ");
				}
				if(key != "inquiry"){	// inquiry は selectなので除外。それ以外を修正値に。
					document.getElementById(key).value = response[key]["filtered"];
				}
			}
			if(response["inquirybody"]["filtered"] == ""){
				document.getElementById("inquirybody").value = "詳細をご記入ください。";
				$("#inquirybody").one("focus",(function(){
					this.value = "";
				}));
			}

			$("#validate").one("click",(function(){
				this.blur();
				validateItems();
			}));
			$("#top").ScrollTo("300");

			$("#validate").attr({"src":"http://static.bitcash.jp/corp/contact/btn_transmission.gif"});

		}
	}else{
		$("#top").ScrollTo("fast");
		$("#validate").attr({"src":"http://static.bitcash.jp/corp/contact/btn_transmission.gif"});
	}
}


