
var err_msg = new Array();

function checkValidate( formobj )
{
	var valid = true;
	var msg_input = 'は必須項目となっております。';
	var msg_regex = 'の形式に誤りがあります。';

	//エラー初期化
	initMessage();


	//-- 必須チェック ----
	if( !checkInput( formobj.surname ) || !checkInput( formobj.name ) ) {
		setMessage( 'f_name', '名前' + msg_input );
		valid = false;
	}

	if( !checkInput( formobj.kana_surname ) || !checkInput( formobj.kana_name ) ) {
		setMessage( 'f_kana', 'フリガナ' + msg_input );
		valid = false;
	}

	if( !checkInput( formobj.email01 ) ) {
		setMessage( 'f_email01', 'メールアドレス' + msg_input );
		valid = false;
	} else if( !checkFormat( 'email', formobj.email01.value ) ) {
		setMessage( 'f_email01', 'メールアドレス' + msg_regex );
		valid = false;
	}

	if( !checkInput( formobj.email02 ) ) {
		setMessage( 'f_email02', 'メールアドレス (確認)' + msg_input );
		valid = false;
	}


	//-- 形式チェック ----
	if( checkInput( formobj.tel01 ) || checkInput( formobj.tel02 ) || checkInput( formobj.tel03 ) )
	{
		if( !checkFormat( 'tel', formobj.tel01.value ) || !checkFormat( 'tel', formobj.tel02.value ) || !checkFormat( 'tel', formobj.tel03.value ) )
		{
			setMessage( 'f_tel', '電話番号' + msg_regex );
			valid = false;
		}
	}
	if( checkInput( formobj.fax01 ) || checkInput( formobj.fax02 ) || checkInput( formobj.fax03 ) )
	{
		if( !checkFormat( 'tel', formobj.fax01.value ) || !checkFormat( 'tel', formobj.fax02.value ) || !checkFormat( 'tel', formobj.fax03.value ) )
		{
			setMessage( 'f_fax', 'FAX番号' + msg_regex );
			valid = false;
		}
	}

	//同一値入力チェック
	if( formobj.email01.value != formobj.email02.value )
	{
		setMessage( 'f_email02', '確認のためメールアドレスと同じものを入力してください。' );
		valid = false;
	}


	//エラーメッセージの表示
	dispMessage();

	return valid;
}

function initMessage()
{
	var ids = new Array( 'f_type', 'f_name', 'f_kana', 'f_tel', 'f_fax', 'f_email01', 'f_email02');

	for(i=0; i<ids.length; i++)
	{
		document.getElementById(ids[i]).innerHTML = '';
		document.getElementById(ids[i]).style._display = 'block';
		err_msg[ids[i]] = '';
	}
}


function setMessage( id, msg )
{
	if( !err_msg[id] )
		err_msg[id] = msg;
	else
		err_msg[id] += "<br />" + msg;
}


function dispMessage()
{
	var ids = new Array( 'f_type', 'f_name', 'f_kana', 'f_tel', 'f_fax', 'f_email01', 'f_email02');
	var str;

	for(i=0; i<ids.length; i++)
	{
		str = err_msg[ids[i]];

		if( str )
			document.getElementById(ids[i]).innerHTML = str;
		else
			document.getElementById(ids[i]).style._display = 'none';
	}
}

function checkInput( obj )
{
	if( obj.value == '' )
		return false;

	return true;
}

function checkFormat( type, str )
{
	if( type == 'tel' )
	{
		if( !str.match(/^[0-9]{2,4}$/) )
			return false;
	}
	else if( type == 'email' )
	{
		if( !str.match(/^(.+)@(.+)/) )
			return false;
	}

	return true;
}