/***************************************************************************
 *
 *    Program Name   : chk_form.js
 *    Note           : 入力チェックを行う関数集
 *    Final Update   : 2002/06/04
 *    Version        : 1.0.01
 *
 *    Manual         [param]
 *                   : txt   チェック対象文字列
 *                   :       空の場合はチェックを行いません。
 *                   :
 *                   : mess  alert用追加メッセージ、空("")を指定した場合は
 *                   :       デフォルトメッセージが使用されます。
 *                   : 
 *                   : size  バイトチェックのみ使用
 *                   :       最大バイト数を設定します。
 *
 *                   [return]
 *                   : true or false
 *
 ***************************************************************************/


/* 日付チェック関数 */
function date_check(txt,mess){
	if(txt.length != 0){
		if(mess != '') mess = mess + "の";
		data = txt.match(/^(\d\d\d\d)\/(\d\d)\/(\d\d)$/);
		data2 = txt.match(/^(\d\d\d\d)-(\d\d)-(\d\d)$/);

		if (data == null && data2 == null) { alert(mess + "日付が不正です [XXXX/XX/XX]"); return false; }
		yy = eval(RegExp.$1);
		mm = eval(RegExp.$2);
		dd = eval(RegExp.$3);
		if (yy < 1) { alert("年が異常です"); return false; }
		if ((mm < 1) || (mm > 12)) { alert(mess + "月が不正です"); return false; }
		if ((dd < 1) || (dd > 31)) { alert(mess + "日が不正です"); return false; }

		if(mm == 2){
			if (((yy % 4 == 0) && (yy % 100 != 0)) || (yy % 400 == 0)) flag = true;
			alert(yy % 4);
			alert(yy % 100);
			alert(yy % 400);
			if(flag == true){
				alert("28");
				dd2 = 29;
			} else {
				alert("29");
				dd2 = 28;
			}
			alert("test");
		} else if (mm == 4 || mm == 6 || mm == 9 || mm == 11){
			dd2 = 30;
		} else {
			dd2 = 31;
		}

		if(dd > dd2){
			alert(mess + "日付が不正です");
			return false;
		}
	}
	return true;
}

/* 半角入力チェック関数 */
function hankaku_check(txt,mess){
	if(txt.length != 0){
		if(mess != '') mess = mess + "の";
		data = txt.match(/[0-9a-zA-Z\+\-\@\/\*\%\#\"\,\.\_\?\!\&\$\\ ]+/g);
		if (data != txt){
			alert(mess + '入力に全角が含まれています');
			return false;
		}
	}
	return true;
}

/* 半角英数字チェック関数 */
function hankaku2_check(txt,mess){
	if(txt.length != 0){
		if(mess != '') mess = mess + "の";
		data = txt.match(/[0-9a-zA-Z]/g);
		if (!data){
			alert(mess + '入力に英数字以外が含まれています');
			return false;
		}
	}
	return true;
}


/* 数字チェック関数 */
function number_check(txt,mess){
	if(txt.length != 0){
		if(mess != '') mess = mess + "の";
		data = txt.match(/[^0-9\.]/g);
		if(data) {
			alert(mess + "値が不正です");
			return false;
		}
	}
	return true;
}

/* 郵便番号 */
function zip_check(txt,mess){
	if(txt.length != 0){
		if(mess != '') mess = mess + "の";
		data = txt.match(/[^0-9-]/g);
		if(data) {
			alert(mess + "郵便番号が不正です");
			return false;
		}
	}
	return true;
}

/* 電話番号チェック */
function tel_check(txt,mess){
	if(txt.length != 0){
		if(mess != '') mess = mess + "の";
		data = txt.match(/[^0-9-]/g);
		if(data) {
			alert(mess + "電話番号が不正です");
			return false;
		}
	}
	return true;
}

/* メールアドレスチェック */
function mail_check(txt,mess){
	if(txt.length != 0){
		if(mess != '') mess = mess + "の";
		data = txt.match(/^\S+@\S+\.\S+$/);
		if(!data){
			alert(mess + "メールアドレスが正しくありません");
			return false;
		}
	}
	return true;
}

/* ＩＰアドレスチェック */
function ip_check(txt,mess){
	if(txt.length != 0){
		if(mess != '') mess = mess + "の";
		data = txt.match(/^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$/);
		if(data){
			alert(mess + "ＩＰアドレスが正しくありません");
			return false;
		}
	}
	return true;
}

/* ＵＲＬチェック */
function url_check(txt,mess){
	if(txt.length != 0){
		if(mess != '') mess = mess + "の";
		data = txt.match(/(http|ftp|https|ldap):\/\/.+/);
		if(data){
			alert(mess + "ＵＲＬが正しくありません");
			return false;
		}
	}
	return true;
}

/* バイトチェック */
function byte_check(txt,size,mess){
	if(txt.length != 0){
		count = 0;
		if(mess != ''){
			mess = mess + "の";
		}
		for (i=0; i<txt.length; i++){
			n = escape(txt.charAt(i));
			if (n.length < 4){
				count++;
			}else{
				count+=2;
			}
		}
		if(count > size){
			alert(mess + "文字が長すぎます");
			return false;
		}
	}
	return true;
}
