var HISSU_MAX 	= 5;	//必須項目の数

var ID_NAME 	= "name";
var ID_KANA		= "kana";
var ID_PREF 	= "pref";
var ID_TEL	 	= "tel";
var ID_MAIL 	= "mail";
var ID_MESSAGE 	= "message";

var EMSG_ZENKAKU	= "全角でご入力下さい。";
var EMSG_KANA		= "全角カタカナでご入力下さい。";
var EMSG_HANKAKU	= "半角数字とハイフンでご入力下さい。";
var EMSG_MAIL		= "正しいメールアドレスをご入力下さい。";
var EMSG_NAME_HISSU = "お名前をご入力下さい。";
var EMSG_PREF_HISSU	= "お住まい（都道府県）をご入力下さい。";
var EMSG_TEL_HISSU	= "電話番号をご入力下さい。";
var EMSG_MAIL_HISSU	= "メールアドレスをご入力下さい。";
var EMSG_MESSAGE_HISSU = "お問い合わせ内容をご入力下さい。";

var emsg_name;
var emsg_kana;
var emsg_pref;
var emsg_tel;
var emsg_mail;
var emsg_message;

var drag = false;

function body_onload(){
	if( document.URL.match( /(complete\.php|confirm\.php)/ ) )
	{
		return;
	}

	//エラーメッセージのTOP位置を設定
	setCssTop(ID_NAME);
	setCssTop(ID_KANA);
	setCssTop(ID_PREF);
	setCssTop(ID_TEL);
	setCssTop(ID_MAIL);
	setCssTop(ID_MESSAGE);
	
	//
	$("input").bind("focus", 	ele_onfocus);
	$("select").bind("focus", 	ele_onfocus);
	$("textarea").bind("focus", ele_onfocus);
	$("input").bind("blur", 	ele_onblur);
	$("select").bind("blur", 	ele_onblur);
	$("textarea").bind("blur", 	ele_onblur);
	
	$(".emsg").bind("click", 	emsg_hide);
	
	$('#fukurou').bind('mousedown', function(){
		drag = true;
	});
	$(window.document).bind('mouseup', function(){
		drag = false;
		try{
			window.getSelection().removeAllRanges(sel.getRangeAt(0));
			$('#fukurou').getSelection().removeAllRanges(sel.getRangeAt(0));
		}catch(e){}
	});	
	$(window.document).bind('mousemove', function(e){
		if (drag){
			$('#fukurou').css('left', e.clientX - 50);
			$('#fukurou').css('top', e.clientY - 30);
		}
		try{
			$('#fukurou').getSelection().removeAllRanges(sel.getRangeAt(0));
		}catch(e){}
		return false;
	});	
	
	$("#regist_form").keypress(function(ev) {
		if ((ev.which && ev.which === 13) || 
			(ev.keyCode && ev.keyCode === 13)) {
			return false;
		} else {
			return true;
		}
	});
	$("#regist_form").submit(function(ev) {
		return check_submit();
	});	
	
	set_color_all();
	var err_cnt = check_all();
	show_error_count(err_cnt);	

//	$("#name").focus();
}
function ele_onfocus(){

	//初期値データならば空白にする
	try{
		if ($(this).val() == eval("SAMPLE_" + this.id))
			$(this).val("");
	}catch(e){}
	
	//背景色・文字色を設定する
	$(this).css("background", "#f4e0e7");
	$(this).css("color", "#000");
}
function ele_onblur(){
	
	//無効データならば初期値に戻す
	try{
		if ($(this).val() == "")
			$(this).val(eval("SAMPLE_" + this.id));
	}catch(e){}

	//背景色を設定する
	$(this).css("background", "#fff");

	//項目チェックを行う
	var err_cnt = check_all();
	
	//エラー件数を表示する
	show_error_count(err_cnt);

	//全ての文字色を設定する
	set_color_all();
	
	//エラーメッセージを（非）表示する
	show_emsg(this.id);
}


//-----------------------------------------------
//全項目をチェックしてエラー件数を返す
//-----------------------------------------------
function check_all(){
	
	var val;
	
	//お名前
	emsg_name = "";
	val = document.getElementById(ID_NAME).value;
	val = trim(val);
	if (val == "" || val == SAMPLE_name){
		emsg_name = EMSG_NAME_HISSU;
	}else{
		if ( ! iszenkaku(val))
				emsg_name = EMSG_ZENKAKU;
	}
	//カナ
	emsg_kana = "";
	val = document.getElementById(ID_KANA).value;
	val = trim(val);
	if (val == "" || val == SAMPLE_kana){

	}else{
		if ( ! iskana(val))
				emsg_kana = EMSG_KANA;
	}
	//都道府県
	emsg_pref = "";
	val = document.getElementById(ID_PREF).value;
	val = trim(val);
	if (val == "" || val == SAMPLE_pref){
		emsg_pref = EMSG_PREF_HISSU;
	}
	//電話番号
	emsg_tel = "";
	val = document.getElementById(ID_TEL).value;
	val = trim(val);
	if (val == "" || val == SAMPLE_tel){
		emsg_tel = EMSG_TEL_HISSU;
	}else{
		if ( ! istel(val))
			emsg_tel = EMSG_HANKAKU;
	}
	//メールアドレス
	emsg_mail = "";
	val = document.getElementById(ID_MAIL).value;
	val = trim(val);
	if (val == "" || val == SAMPLE_mail)
		emsg_mail = EMSG_MAIL_HISSU;
	else{
		if ( ! ismail(val))
			emsg_mail = EMSG_MAIL;
	}
	//ご希望事項・ご相談など
	emsg_message = "";
	val = document.getElementById(ID_MESSAGE).value;
	val = trim(val);
	if (val == "" || val == SAMPLE_message){
		emsg_message = EMSG_MESSAGE_HISSU;
	}	

	//エラー件数を返す
	var cnt = 0;
	
	if (emsg_name != "")	cnt++;
	if (emsg_pref != "")	cnt++;
	if (emsg_tel != "")		cnt++;
	if (emsg_mail != "")	cnt++;
	if (emsg_message != "")	cnt++;
	
	return cnt;
}
//-----------------------------------------------
//エラー件数を表示する （フクロウ）
//-----------------------------------------------
function show_error_count(err_cnt){
	
	var html;
	if (err_cnt == 0){
		html = ""
			+ "必要な項目の入力は完了しました。"
			;
	}else{
		html = ""
			+ "必須入力項目は"
			+ "<br />"
			+ "<span class='b'>" + HISSU_MAX + "項目</span>"
			+ "中、残り"
			+ "<span class='b'>" + err_cnt + "項目</span>です。"
			;
	}
	$("#fukurou").html(html);
}

//-----------------------------------------------
//エラーメッセージを表示（または非表示）する
//-----------------------------------------------
function show_emsg(id){
	try{
		var jel = $("#emsg_" + id);

		if (eval("emsg_" + id) == ""){
			jel.css("visibility", "hidden");
		}else{
			jel.css("visibility", "visible");
			jel.find(".emsgtext").html(eval("emsg_" + id));
		}
	}catch(e){}
}
//-----------------------------------------------
//エラーメッセージを非表示にする
//-----------------------------------------------
function hide_emsg(id){
	
	var jel = $("#emsg_" + id);

	jel.css("visibility", "hidden");
}
//-----------------------------------------------
//エラーメッセージ（this）を非表示にする
//-----------------------------------------------
function emsg_hide(){
	this.style.visibility='hidden';
}


//-----------------------------------------------
//文字色を設定する　全項目
//-----------------------------------------------
function set_color_all(){
	
	set_color(ID_NAME);
	set_color(ID_KANA);
	set_color(ID_PREF);
	set_color(ID_TEL);
	set_color(ID_MAIL);
	set_color(ID_MESSAGE);
}
//-----------------------------------------------
//文字色を設定する
//-----------------------------------------------
function set_color(id){
	
	var jel;
	
	jel = $("#" + id);

	if (jel.val() == ""){

		jel.css("color", "#9D9D9D");
	}else{
		jel.css("color", "#000");
		try{
			if (jel.val() == eval("SAMPLE_" + id))
				jel.css("color", "#9D9D9D");
		}catch(e){}	
	}
}

//-----------------------------------------------
//サブミット前のチェックを行う
//-----------------------------------------------
function check_submit(){

	//項目チェックを行う
	var err_cnt = check_all();
	
	//エラー件数を表示する
	show_error_count(err_cnt);

	//全ての文字色を設定する
	set_color_all();
	
	//全てのエラーメッセージを表示する
	show_emsg(ID_NAME);
	show_emsg(ID_KANA);
	show_emsg(ID_PREF);
	show_emsg(ID_TEL);
	show_emsg(ID_MAIL);
	show_emsg(ID_MESSAGE);	
	
	//戻り値
	if (err_cnt == 0)
		return true;
	else{
		if (emsg_name != ""){
			$('#name').focus();
		}else if(emsg_kana != ""){
			$('#kana').focus();
		}else if(emsg_pref != ""){
			$('#pref').focus();
		}else if (emsg_tel != ""){
			$('#tel').focus();
		}else if (emsg_mail != ""){
			$('#mail').focus();
		}else if (emsg_message != ""){
			$('#message').focus();
		}
		if(jQuery.browser.msie)
			$('#jump01').get(0).click();
		else
			$('#jump01').click();
	
		return false;
	}
}



//-----------------------------------------------
//CSSのTOPを設定する
//-----------------------------------------------
function setCssTop(id){
	if( document.getElementById( id ) != undefined && document.getElementById( id ).type != "hidden" )
	{
		var bcr = $('#' + id).get(0).getBoundingClientRect();
		var top = 
			  bcr.top
			+ scrollYEx()
			- 40
			;
		/*
		document.title = 
			  'bcr.top:' + bcr.top
			+ ' window.scrollY:' + scrollYEx()
			;
		*/
		$('#emsg_' + id).css('top', top);
	}
}

//-----------------------------------------------
//汎用ルーチン
//-----------------------------------------------
function trim(str){
	if ( ! str)
		return "";
	return str.replace(/(^\s+)|(\s+$)/g, "");
}
function iszenkaku(str){
	return str.match(/^[^ -~｡-ﾟ]+$/) ? true:false;
}
function iskana(str){
	return str.match(/^[ァ-ン]+$/) ? true:false;
}
function ishankaku(str){
	return str.match(/^[a-zA-Z0-9-]+$/) ? true:false;
}
function istel(str){
	return str.match(/^[0-9-]+$/) ? true:false;
}
function ismail(str){
	return str.match(/^[A-Za-z0-9]+[\w-]+@[\w\.-]+\.\w{2,}$/) ? true:false;
}
function scrollTopEx(val){

	if (val == undefined)
		return get_scrollTopEx();
	else
		set_scrollTopEx(val);
}
function scrollYEx(){
	if(window.scrollY) return window.scrollY; // Moziila
	if(window.pageYOffset) return window.pageYOffset; // Opera, NN4
	if(document.documentElement && document.documentElement.scrollTop){ // 以下 IE
		return document.documentElement.scrollTop;
	}
	else if(document.body && document.body.scrollTop){
		return document.body.scrollTop;
	}
	return 0;
}
/*
function get_scrollTopEx(){
	var t1 = 0;
	var t2 = 0;
	var t3 = 0;

	try{
		t1 = document.documentElement.scrollTop;
	}catch(e){}
	try{
		t2 = document.body.scrollTop;
	}catch(e){}
	try{
		t3 = document.window.scrollTop;
	}catch(e){}

	if (t1 > t2){
		if (t3 > t1)
			return t3;
		else
			return t1;
	}else{
		if (t3 > t2)
			return t3;
		else
			return t2;
	}
}
function set_scrollTopEx(val){
	val = val.replace('px', '');
	alert(val);
	$(document.documentElement).animate({scrollTop:val}, 1000, 'swing');
	//document.documentElement.scrollTop = val;
	return;
	try{
		$(document.documentElement).animate({scrollTop:val}, 1000, 'swing');
	}catch(e){}
	try{
		$(document.body).animate({scrollTop:val}, 1000, 'swing');
	}catch(e){}
	try{
		$(document.window).animate({scrollTop:val}, 1000, 'swing');
	}catch(e){}
}
*/

