// バイト数取得関数
function getByte(text)
{
	count = 0;
	for (i=0; i<text.length; i++)
	{
		n = escape(text.charAt(i));
		if (n.length < 4) count++; else count+=2;
	}
	return count;
}


// SQL禁止文字チェック関数
function isSQL(inputvalue)
{
	if (inputvalue.search(/[\'\"\;]/) != -1) {
		return false;
	}else{
		return true;	
	}
}

// 日付フォーマットチェック関数
function checkDate(dateStr) {
	// YYYY/MM/DD
	datePat = /^(\d{4})(\/)(\d{2})\2(\d{2})$/;
	DateArray = dateStr.match(datePat);

	if (DateArray == null) {
		return false;
	}
	return true;
}

// 日付存在チェック関数
function checkValidityDate( mon,day,year )
{
	maxDayOfMonth = Array( 31,29,31,30,31,30,31,31,30,31,30,31 );
	if( mon < 1 || mon > 12 ){                      return false;   }
	if( day < 1 || day > maxDayOfMonth[mon-1] ){    return false;   }
	if( mon != 2 ){                                 return true;    }
	if( day < 29 ){                                 return true;    }
	if( ( year % 4 ) == 0 && ( year % 100 ) != 0 ){ return true;    }
	if( ( year % 400 ) == 0 ){                      return true;    }
	return false;
}

/////////////////////////////////////////////////////////////////////////
// 日付妥当性チェック関数
//
//	引数：
// inputvalue		 =	対象文字列
//
//	戻り値：日付妥当ならtrue、それ以外false
/////////////////////////////////////////////////////////////////////////
function isDate(inputvalue){
	// 日付フォーマットチェック
	if (!(checkDate(inputvalue))){
	  //return "「" + itemname + "」は例)2003/01/01の形式で入力してください。\n";
	  return false;
	}
	
	// 日付存在チェック	
	aryDate = inputvalue.split("/");
	if (!(checkValidityDate(aryDate[1],aryDate[2],aryDate[0]))){
	  //return "「" + itemname + "」で入力された日付は存在しません。\n";
		return false;
	}
	
	return true;
}

/////////////////////////////////////////////////////////////////////////
// 日付入力チェック関数
//
//	引数：
// inputvalue		 =	対象文字列
// itemname			 =	項目名
// boolnull			 =	(空欄許可:0,空欄不許可:1)
//
//	戻り値：エラーメッセージ
/////////////////////////////////////////////////////////////////////////
function InputCheckDate(inputvalue,itemname,boolnull){

	errflg = 0;
	
	if (boolnull == 1){
		if (!(isDate(inputvalue))){
			errflg = 1;
		}
	}else if (!(inputvalue == "")){
		if (!(isDate(inputvalue))){
			errflg = 1;
		}
	}
	
	if (errflg == 1){
		return "「" + itemname + "」で入力された日付は存在しません。\n　例）2003/01/01の形式で入力してください。\n";
	}

	return "";
}

/////////////////////////////////////////////////////////////////////////
// 期間入力チェック関数
//
//	引数：
// fromdate			 =	開始日
// todate			 =	終了日
// itemname			 =	項目名
//
//	戻り値：エラーメッセージ
/////////////////////////////////////////////////////////////////////////
function InputCheckFromTo(fromdate,todate,itemname){

	fromdate = fromdate.split('/');
	fromdate = fromdate.join('');
	
	todate = todate.split('/');
	todate = todate.join('');

	if (todate != "" && fromdate > todate){
		return "「" + itemname + "」は日付の順序、または範囲が不正です。\n";
	}
	
	return "";
}

/////////////////////////////////////////////////////////////////////////
// 	NULLチェック関数
//
//	引数：
// inputvalue		 =	対象文字列
// itemname			 =	項目名
//	戻り値：エラーメッセージ
/////////////////////////////////////////////////////////////////////////
function NullCheck(inputvalue,itemname){
	if (inputvalue == ""){
		return itemname + "は入力必須項目です。\n";
	}
	return "";
}


/////////////////////////////////////////////////////////////////////////
// 	重複チェック関数
//
//	引数：
// value1			 =	比較したい文字列1
// value2			 =	比較したい文字列2
// itemname1		 =	項目名1
// itemname2		 =	項目名2
//
//	戻り値：エラーメッセージ
/////////////////////////////////////////////////////////////////////////
function DuplicationCheck(value1,value2,itemname1,itemname2){
	if (value1 != value2){
		return itemname1 + "と" + itemname2 + "が一致していません。\n";
	}
	return "";
}

function NotDuplicationCheck(value1,value2,itemname1,itemname2){
	if (value1 == value2){
		return itemname1 + "と" + itemname2 + "に同じ値が入力されています。\n";
	}
	return "";
}

/////////////////////////////////////////////////////////////////////////
// 入力チェック関数
//
//	引数：
// inputvalue		 =	対象文字列
// itemname			 =	項目名
// min_byte			 =	最小入力可能バイト数（0ならば空欄許可）
// max_byte			 =	最大入力可能バイト数
// charattribute =	文字属性
//
//	戻り値：エラーメッセージ
/////////////////////////////////////////////////////////////////////////
function InputCheck(inputvalue,itemname,min_byte,max_byte,charattribute){

	switch (charattribute){
		case 0: //全角（半角カナはじく）
			hankaku = "ｱｲｳｴｵｶｷｸｹｺｻｼｽｾｿﾀﾁﾂﾃﾄﾅﾆﾇﾈﾉﾊﾋﾌﾍﾎﾏﾐﾑﾒﾓﾔﾕﾖﾗﾘﾙﾚﾛﾜｦﾝｧｨｩｪｫｬｭｮｯｰ､｡｢｣ﾞﾟ";
			for (i=0; i<inputvalue.length; i++)
			{
				if (hankaku.indexOf(inputvalue.charAt(i),0) >= 0) {
					return "「" + itemname + "」は半角カナ文字を使用できません。\n";
				}
			}
			break;
		case 1: //半角英数
			if (inputvalue.search(/[^a-zA-Z0-9]/) != -1) {
				return "「" + itemname + "」は半角英数で入力してください。\n";
			 }
			break;
		case 2: //半角数字
			if (inputvalue.search(/[^0-9]/) != -1) {
				return "「" + itemname + "」は半角数字で入力してください。\n";
			}
			break;
		case 3: //全角カナ
			if (inputvalue.search(/[^ァ-ヴー－0-9０-９]/) != -1) {
				return "「" + itemname + "」は全角カナで入力してください。\n";
			}
			break;
		case 4: //メールアドレス
			if ((inputvalue.search(/[0-9A-Za-z_\x2D.]+@[0-9A-Za-z_\x2D.]+\.[0-9A-Za-z_\x2D]+/) == -1) && inputvalue != "") {
				return "「" + itemname + "」は不正なメールアドレスが入力されています。\n";
			}
			break;
		case 5: //ASCII
			for (i=0;i<inputvalue.length;i++){
				code = inputvalue.charCodeAt(i);
				if (0 > code || code > 127) {
					return "「" + itemname + "」は半角英数(記号含)で入力してください。\n";
				}
			}
			break;
		case 6: //全角
			for (i=0;i<inputvalue.length;i++){
				code = inputvalue.charCodeAt(i);
				if (code < 256 || (code >= 0xff61 && code <= 0xff9f)) {
					return "「" + itemname + "」は全角で入力してください。\n";
				}
			}

	}

	//SQL文のエスケープ文字チェック
	//if (!(isSQL(inputvalue))) {
	//	return "「" + itemname + "」は不正文字「 \"\'\; 」が含まれています。\n";
	//}

	//バイト数チェック
	if (getByte(inputvalue) > max_byte){
		return "「" + itemname + "」が長すぎます。\n";
	}else if(getByte(inputvalue) < min_byte){
		if (min_byte == 1){
			return "「" + itemname + "」は入力必須項目です。\n";
		} else if (min_byte != 1){
			return "「" + itemname + "」が短すぎます。\n";
		}
	}	
	return ""; 

}

/////////////////////////////////////////////////////////////////////////
// 	ラジオボタン・チェックボックス選択チェック
//
//	引数：
// inputvalue		 =	対象となるラジオボタン・チェックボックス
// itemname			 =	項目名
// min_select			 =	最小選択可能項目数（0ならば空欄許可）
// max_select			 =	最大選択可能項目数
//	戻り値：エラーメッセージ
/////////////////////////////////////////////////////////////////////////
function BoxCheck(inputvalue,itemname,min_select,max_select){

	var box_select = 0;

	for(var i=0;i<inputvalue.length;i++){

		if (inputvalue[i].checked == true) {
			box_select++;
		}
	}

	//項目数チェック
	if (box_select > max_select){
		return "「" + itemname + "」を選択できるのは" + max_select + "個までです\n";
	}else if(box_select < min_select){
		if (min_select == 1){
			return "「" + itemname + "」が選択されていません。\n";
		} else if (min_select != 1){
			return "「" + itemname + "」は" + min_select + "個以上選択してください。\n";
		}
	}	
	return ""; 

}


