//****************************************
//小数点以下含む整数部・小数部桁数チェック
//引数：ChkStr	チェック対象半角数値文字列
//			IntBase	整数部基準桁数
//			DecBase	小数部基準桁数
//戻り値：桁数以内→TRUE	桁数OVER→FALSE
//どちらかのみをチェックしたい場合は対象外の方を-1指定する。
//****************************************
function DecimalChk(ChkStr,IntBase,DecBase){

	if (ChkStr.length == 0){
		//チェック対象半角数値文字列がない
		return true;
	}

	IntOnly = true;
	if (IntBase == -1){
		//整数部調査対象外
		IntOnly = false;
	}

	DecOnly = true;
	if (DecBase == -1){
		//小数部調査対象外
		DecOnly = false;
	}

	IntNum = 0;
	DecNum = 0;
	lx = ChkStr.indexOf(".",0);
	if (lx > 0){
		//小数有り
		IntNum = ChkStr.substr(0,lx).length;
		DecNum = ChkStr.substr(lx + 1,ChkStr.length - (lx + 1)).length;
	}else{
		//小数なし
		IntNum = ChkStr.length;
	}

	if (IntOnly == true){
		//整数部調査対象
		if (IntBase < IntNum){
			//基準より大きい
			return false;
		}
	}

	if (DecOnly == true){
		//少数部調査対象
		if (DecBase < DecNum){
			//基準より大きい
			return false;
		}
	}
	return true;
}

//****************************************
//数値チェック
//引数：ChkStr	チェック対象半角数値文字列
//戻り値：数値として認められる→TRUE	数値として認められない→FALSE
//****************************************
function NumChk(ChkStr){
	if (isNaN(ChkStr) == true){
		return false;
	}else{
		return true;
	}
}

//数値チェック
function numeric_check(checkstring,spaceng){

	if (spaceng == true && checkstring.length == 0){
		return false;
	}

	for(i = 0; i < checkstring.length; i++){
		ch = checkstring.substring(i, i + 1);
		if ("あ".length == 2){
			//以前のNetscape
			if (NNzenkaku(ch)){
				return false;
			}
		}
		if (ch < "0" || ch > "9"){
			return false;
		}
	}
	
	return true;
}

//メールアドレス文字チェック
function mailcheck(checkstring){

	atto = false;

	if (checkstring.length == 0){
		return "";
	}

	for(i = 0; i < checkstring.length; i++){
		ch = checkstring.substring(i, i + 1);
		if ("あ".length == 2){
			//以前のNetscape
			if (NNzenkaku(ch)){
				return "２バイト文字が含まれています";
			}
		}
		if (ch == ","){
			return "「,」が含まれています";
		}
		if (ch == " "){
			return "スペースが含まれています";
		}
		if (ch == "@"){
			atto = true;
		}
	}

	if (atto == false){
		return "「@」がありません";
	}

	return "";

}

//FAX・電話番号チェック
function faxcheck(checkstring,flg){

	chk_str = "0123456789-";
	fon = false;

	if(checkstring.length == 0){
		return "";
	}

	for(i = 0;i < checkstring.length;i++){
		if ("あ".length == 2){
			//以前のNetscape
			if (NNzenkaku(checkstring.substring(i,i+1))){
                                if (flg == 1){
                                      return "電話番号に入力できるのは半角英数字とハイフンだけです";
                                 }
                                else{
				      return "FAX番号に入力できるのは半角英数字とハイフンだけです";
                                 }
			}
		}
		if(chk_str.indexOf(checkstring.substring(i,i+1)) == -1){
                        if (flg == 1){
                                  return "電話番号に入力できるのは半角英数字とハイフンだけです";
                                 }
                        else {
			      return "FAX番号に入力できるのは半角英数字とハイフンだけです";
                        }
		}
		if(checkstring.substring(i,i+1) == "-"){
			fon = true;
		}
	}

//	if((checkstring.substring(0,1) == "0")&&(fon == false)){
//		return "外線FAX番号の場合は市外局番の後ろをハイフンで区切って下さい";
//	}

	return "";

}

//２バイト文字チェック
function chrbyte_check (checkstring){

	chk_str = "!\"#$%&\'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstuvwxyz{|}~｡｢｣､･ｦｧｨｩｪｫｬｭｮｯｰｱｲｳｴｵｶｷｸｹｺｻｼｽｾｿﾀﾁﾂﾃﾄﾅﾆﾇﾈﾉﾊﾋﾌﾍﾎﾏﾐﾑﾒﾓﾔﾕﾖﾗﾘﾙﾚﾛﾜﾝﾞﾟ ";
	for(i = 0;i < checkstring.length;i++){
		if ("あ".length == 2){
			//以前のNetscape
			if (NNzenkaku(checkstring.substring(i,i+1))){
				return false;
			}
		}
		if(chk_str.indexOf(checkstring.substring(i,i+1)) == -1){
			return false;
		}
	}
	return true;

 }

//半角英数チェック
function eisu_check (checkstring){

	chk_str = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
	for(i = 0;i < checkstring.length;i++){
		if ("あ".length == 2){
			//以前のNetscape
			if (NNzenkaku(checkstring.substring(i,i+1))){
				return false;
			}
		}
         if(chk_str.indexOf(checkstring.substring(i,i+1)) == -1){
			return false;
         }
	}
	return true;

 }

//半角カナチェック
function kana_check (checkstring){

	chk_str = "ｦｧｨｩｪｫｬｭｮｯｰｱｲｳｴｵｶｷｸｹｺｻｼｽｾｿﾀﾁﾂﾃﾄﾅﾆﾇﾈﾉﾊﾋﾌﾍﾎﾏﾐﾑﾒﾓﾔﾕﾖﾗﾘﾙﾚﾛﾜﾝﾞﾟ";
	for(i = 0;i < checkstring.length;i++){
		ch = checkstring.substring(i,i+1)
		if ("あ".length == 2){
			//以前のNetscape
			if (NNzenkaku(ch)){
				//２バイト文字なので２文字分切り出し
				ch = checkstring.substring(i,i+2);
				i++;
			}
		}
		if(chk_str.indexOf(ch) >= 0){
			return true;
		}
	}
	return false;

 }

//全角カナチェック
function Zenkana_check (checkstring){
	chk_str = "ヲァィゥェォャュョッーアイウエオカキクケコサシスセソタチツテトナニヌネノハヒフヘホマミムメモヤユヨラリルレロワンガギグゲゴザジズゼゾダヂヅデドバビブベボ";
	for(i = 0;i < checkstring.length;i++){
		ch = checkstring.substring(i,i+1)
		if ("あ".length == 2){
			//以前のNetscape
			if (NNzenkaku(ch)){
				//２バイト文字なので２文字分切り出し
				ch = checkstring.substring(i,i+2);
				i++;
			}
		}
		if(chk_str.indexOf(ch) >= 0){
			return true;
		}
	}
	return false;

 }

//文字数カウント
function chr_len(checkstring){

	len = 0;

	if ("あ".length == 2){
		//以前のNetscape
		for(i = 0;i < checkstring.length;i++){
			ch = checkstring.substring(i,i+1);
			len++;
			if (NNzenkaku(ch)){
				//２バイト文字
				i++;
			}
		}
		return len;
	}
	else{
		//それ以外はそのままlengthを返す
		return checkstring.length;
	}
}

//以前のNetscapeの全角チェック
function NNzenkaku(c) {

    var str = escape(c);
    
    if(str.charAt(0) != "%"){
        return false;
	}
    if(str.charAt(1) == "8"){
        return true;
	}
    else if(str.charAt(1) == "9"){
        return true;
	}
    else if(str.charAt(1) == "E"){
        return true;
	}
    else if(str.charAt(1) == "F"){
        return true;
	}
    else{
        return false;
	}

}

//郵便番号チェック
function zip_check (checkstring){

	chk_str = "0123456789-";
	for(i = 0;i < checkstring.length;i++){
		if ("あ".length == 2){
			//以前のNetscape
			if (NNzenkaku(checkstring.substring(i,i+1))){
				return false;
			}
		}
         if (chk_str.indexOf(checkstring.substring(i,i+1)) == -1){
			return false;
              }
         
	 }
         if (checkstring.substring(4,1) == "-"){
               return false;
         } 
	return true;

 }

//価格表示用フォーマット
function formatnumber(number) {

	str = "";
	len = number.toString().length;
	for ( i = 1; i <= len ; i++ ) {
		str = number.toString().substring(len - i,len - i + 1) + str;
		if ((parseInt(i / 3) == (i / 3))&&(i != len)) {
			str = "," + str;
		}
	}
	return str;

}

//価格表示用フォーマット変換（計算用）
function unformatnumber(number) {

	str = "";
	len = number.toString().length;
	for ( i = 1; i <= len ; i++ ) {
		if (number.toString().substring(len - i,len - i + 1) != ",") {
			str = number.toString().substring(len - i,len - i + 1) + str;
		}
	}
	return str;

}

function w2h(c){
	if(c == "１"){c = "1"}
	if(c == "２"){c = "2"}
	if(c == "３"){c = "3"}
	if(c == "４"){c = "4"}
	if(c == "５"){c = "5"}
	if(c == "６"){c = "6"}
	if(c == "７"){c = "7"}
	if(c == "８"){c = "8"}
	if(c == "９"){c = "9"}
	if(c == "０"){c = "0"}
	if(c == "／"){c = "/"}
	if(c == "・"){c = "/"}
	if(c == "年"){c = "/"}
	if(c == "月"){c = "/"}
	if(c == "日"){c = "/"}
	return c;
}

//日付のチェック
function check_date(element){
	if (element.value != ""){
		today = new Date();
		if (today.getYear() < 100){
			if (today.getYear() > 80){
				today = new Date(today.getYear()+1900,today.getMonth(),today.getDate());
				tY = parseInt(today.getYear()) + 1900;
			}
			else{
				today = new Date(today.getYear()+2000,today.getMonth(),today.getDate());
				tY = parseInt(today.getYear()) + 2000;
			}
		}
		else{
			if (today.getYear() < 1000){
				today = new Date(today.getYear()+1900,today.getMonth(),today.getDate());
				tY = parseInt(today.getYear()+1900);
			}
			else{
				today = new Date(today.getYear(),today.getMonth(),today.getDate());
				tY = parseInt(today.getYear());
			}
		}
		tM = parseInt(today.getMonth());
		tD = parseInt(today.getDate());
//	全角→半角変換
		str = "";
		for (i=0; i < element.value.length; i++){str = str + w2h(element.value.charAt(i))}
//	年がついているかの書式チェック
		dayArray = str.split("/");
		if ((dayArray.length != 3) && (dayArray.length != 2)){
			if (element.value != " "){
				alert("日付の記述方法が間違っています");
				element.focus();
			}
			element.value = "";
			return false;
		}
		else{
			if (dayArray.length == 3){
				iY = 2000
				if (numeric_string(dayArray[0]) == true){
					iY = eval(dayArray[0]);
				}
				iM = 1
				if (numeric_string(dayArray[1]) == true){
					iM = eval(dayArray[1]);
				}
				iD = 1
				if (numeric_string(dayArray[2]) == true){
					iD = eval(dayArray[2]);
				}
				if (iY < 100){
					if (iY > 80){
						cnt = 1900;
					}
					else{
						cnt = 2000;
					}
					iY = iY + cnt;
				}
			}
		else{
				iY = tY
				iM = 1
				if (numeric_string(dayArray[0]) == true){
					iM = eval(dayArray[0]);
				}
				iD = 1
				if (numeric_string(dayArray[1]) == true){
					iD = eval(dayArray[1]);
				}
				input = new Date(iY, iM-1, iD)
				if (input < today){
					iY = iY + 1;
				}
			}
			//input = new Date();
			today.setYear(iY);
			today.setMonth(iM-1);
			today.setDate(iD);
			//input = new Date(iY, iM-1, iD);
			if (today.getFullYear() < 100){
				if (today.getFullYear() > 80){
					element.value = (today.getFullYear() + 1900) + "/" + (today.getMonth() + 1) + "/" + today.getDate();
				}
				else{
					element.value = (today.getFullYear() + 2000) + "/" + (today.getMonth() + 1) + "/" + today.getDate();
				}
			}
			else{
				if (today.getFullYear() < 1000){
					element.value = (today.getFullYear() + 1900) + "/" + (today.getMonth() + 1) + "/" + today.getDate();
				}
				else{
					element.value = (today.getFullYear()) + "/" + (today.getMonth() + 1) + "/" + today.getDate();
				}
			}
		}
	}else{
		return false;
	}
	return true;

}

function numeric_string(checkstring){

	var i = 0;

	if (checkstring.length == 0){
		return false;
	}

	for(i = 0; i < checkstring.length; i++){
		ch = checkstring.substring(i, i + 1)
		if (ch < "0" || ch > "9"){
			return false;
		}
	}
	
	return true;
}

//日付の大小チェック
function compare_date(date1, date2){
	dayArray1 = date1.split("/");
	iY1 = eval(dayArray1[0]);
	iM1 = eval(dayArray1[1]);
	iD1 = eval(dayArray1[2]);

	dayArray2 = date2.split("/");
	iY2 = eval(dayArray2[0]);
	iM2 = eval(dayArray2[1]);
	iD2 = eval(dayArray2[2]);

	//大小比較
	if (iY1 < iY2) {
		return false;
	}
	else if (iM1 < iM2) {
		return false;
	}
	else if (iD1 < iD2) {
		return false;
	}

	return ture;
}

//フォルダ名チェック
function foldercheck(checkstring){

	atto = false;

	if (checkstring.length == 0){
		return "";
	}

	for(i = 0; i < checkstring.length; i++){
		ch = checkstring.substring(i, i + 1)
		if ("あ".length == 2){
			//以前のNetscape
			if (NNzenkaku(ch)){
				return "２バイト文字が含まれています";
			}
		}
		if (ch == "\\"){
			return "使用できない文字が含まれています";
		}
		if (ch == "/"){
			return "「/」が含まれています";
		}
		if (ch == ":"){
			return "「:」が含まれています";
		}
		if (ch == ","){
			return "「,」が含まれています";
		}
		if (ch == ";"){
			return "「;」が含まれています";
		}
		if (ch == "*"){
			return "「*」が含まれています";
		}
		if (ch == "?"){
			return "「?」が含まれています";
		}
		if (ch == "<"){
			return "「<」が含まれています";
		}
		if (ch == ">"){
			return "「>」が含まれています";
		}
		if (ch == "|"){
			return "「|」が含まれています";
		}
		if (ch == " "){
			return "スペースが含まれています";
		}
	}

	return "";

}
