/*******************************************************************************
 * Copyright(c) Nagasaki Prefecture All Rights Reserved.
 *
 * システム名   ：  学校事務ネットワーク旅費システム
 * ファイル名	：　ry_comscript.js
 *
 *==============================================================================
 * 処理内容		：　共通Javascript関数
 *==============================================================================
 * version  変更日      変更者          変更内容
 *------------------------------------------------------------------------------
 * 1.00     2003.08.24  H.Hayashida     仮作成
 *******************************************************************************/

	/**
	 * 情報メッセージデータ保持オブジェクトを生成する
	 * 引数  	:StrMsg        メッセージ文字列
	 * 戻り値	:なし
	 */
	function ComInfoMsg(StrMsg) {
	    this.StrMsg = StrMsg;
	}

	/**
	 * 警告メッセージデータ保持オブジェクトを生成する
	 * 引数  	:StrMsg        メッセージ文字列
	 * 戻り値	:なし
	 */
	function ComWarnMsg(StrMsg) {
	    this.StrMsg = StrMsg;
	}

	/**
	 * 問合せメッセージデータ保持オブジェクトを生成する
	 * 引数  	:StrMsg        メッセージ文字列
	 * 戻り値	:なし
	 */
	function ComQueMsg(StrMsg) {
	    this.StrMsg = StrMsg;
	}

	/**
	 * エラーメッセージデータ保持オブジェクトを生成する
	 * 引数  	:StrMsg        メッセージ文字列
	 * 戻り値	:なし
	 */
	function ComErrMsg(StrMsg) {
	    this.StrMsg = StrMsg;
	}

	/**
	 * 日付が暦日として妥当かどうかを判断する
	 * 引数  	:obj        オブジェクト
	 * 戻り値	:true（正常）/false（異常）
	 */
	function com_ChkDate(obj) {

		var StrDate = obj.value;

		/* 日付文字列が数値６桁かどうか調べる */
		var StrMatch = StrDate.match(/^(\d{6})$/);
		if (StrMatch == null) {
			obj.focus();
			return false;
		}
		/* 年月日の切り分け */
		var year  = StrDate.substr(0,2);
		var mon   = StrDate.substr(2,2);
		var day   = StrDate.substr(4,2);

		/* 年を西暦に直す */
		year = parseInt(year) + 1988;
		/* 10000年加算 */
		var StrYear = "1" + year;
		/* 取得した年月日文字列によりDate型生成 */
		var ObjDate = new Date(year, mon-1, day);

		/* このDate型日付の年月日が元の年月日文字列と一致すれば妥当 */
		if (day != ObjDate.getDate()) {			/* 日が不一致の場合 */
			obj.focus();
			return false;
		}
		if (mon != ObjDate.getMonth() + 1) {	/* 月が不一致の場合 */
			obj.focus();
			return false;
		}
		if (year != ObjDate.getFullYear()) {	/* 年が不一致の場合 */
			obj.focus();
			return false;
		}

		/* 正常な日付の場合 */
		return true;
	}

	/**
	 * エラー出力範囲にエラーメッセージを出力する
	 * 引数  	:msg		エラーメッセージ
	 * 戻り値	:なし
	 */
	function com_OutError(msg) {

		pagetop.focus();
		error.innerHTML=msg;

	}

	/**
	 * エラー出力範囲のメッセージを消去する
	 * 引数  	:なし
	 * 戻り値	:なし
	 */
	function com_ClearError() {

		error.innerHTML="&nbsp;";

	}

	/**
	 * 指定したurlへジャンプする
	 * 引数  	:url		リンク先url
	 * 戻り値	:なし
	 */
	function com_LocationChange(url) {
	
		//location.href=url;
		document.forms[0].task.value="";
		document.forms[0].action=url;
		document.forms[0].submit();

	}

	/**
	 * Textオブジェクトに入力されているかチェックする。
	 * 引数  	:obj	オブジェクト
	 * 戻り値	:true（Textオブジェクトに入力されている場合）
	 * 			:false（Textオブジェクトに入力されていない場合）
	 */
	function cdm_ChkNull(obj) {

		var StrSrc = obj.value;

		if(StrSrc == null || StrSrc == ""){
			obj.focus();
			return false;
		}

		return true;

	}

	/**
	 * 文字列のバイト数を返す
	 * 引数  	:text	文字列
	 * 戻り値	:count	バイト数
	 */
	function cdm_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;
	}

	/**
	 * チェックされたラジオボタンの値を返す
	 * 引数  	:Obj	ラジオボタンのオブジェクト
	 * 戻り値	:チェックされたラジオボタンの値（チェックされていない場合はnullを返す）
	 */
	function cdm_getCheckValue(Obj) {
		var i;

	    if (Obj.length) {
	        for (i = 0; i < Obj.length; i++) {
	            if(Obj[i].checked){ return Obj[i].value; }
	        }
	    } else {
	        if(Obj.checked){ return Obj.value; }
	    }

		return null;
	}

	/**
	 * 先頭のスペースを削除して返す
	 * 引数  	:strData	削除前文字列
	 * 戻り値	:strData	削除後文字列
	 */
	function trim( strData ){
		var	strBuff = strData;

		while ( strBuff.indexOf( ' ' ) == 0 || strBuff.indexOf( '　' ) == 0 ){
			strBuff = strBuff.substr( 1, strBuff.length );
		}
		if ( strBuff.length > 0 ){
			while ( strBuff.lastIndexOf( ' ' ) == strBuff.length - 1 ||
				strBuff.lastIndexOf( '　' ) == strBuff.length - 1 )	{
				strBuff = strBuff.substr( 0, strBuff.length - 1 );
			}
		}

		return strBuff;
	}

	/**
	 * オブジェクトの値のバイト数をチェックする
	 * 引数  	:obj	オブジェクト
	 * 			:max	最大バイト数
	 * 戻り値	:true（正常）/false（異常）
	 */
	function cdm_byteCheck(obj, max) {
		if ( cdm_getByte(obj.value) > max ) {
			//obj.style.color = "red";
			obj.focus();
			return false;
		}
		//obj.style.color = "black";
		return true;
	}

	/**
	 * 文字列のバイト数を返す
	 * 引数  	:text	文字列
	 * 戻り値	:count	バイト数
	 */
	function cdm_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;
	}

	/**
	 * 数値（小数点つき）を3桁毎にカンマ区切りにする
	 * 引数  	:NumSrc	数値（小数点付き）
	 * 戻り値	:StrRet	カンマ挿入後数値
	 */
	function com_Mod3Comma(NumSrc) {

	    /* 文字列型へ変換 */
	    NumSrc = "" + NumSrc;

	    /* カンマ除去 */
	    var StrBuf = com_DelComma(NumSrc);  /* カンマ除去済み文字列 */

	    /* 整数部桁数(小数点の位置)取得 */
	    var NumFig = StrBuf.indexOf(".", 0);    /* 整数部桁数 */
	    if (NumFig < 0) {       				/* 小数点なしの場合 */

	        /* 整数部桁数は文字列長さ */
	        NumFig = StrBuf.length;
	    }

	    /* マイナス符号処理 */
	    var StrSign = "";       		/* 符号文字列 */
	    if (NumSrc.charAt(0) == "-") {  /* マイナスの場合 */

	        /* マイナス符号除去 */
	        StrBuf = StrBuf.slice(1, StrBuf.length);
	        NumFig--;

	        /* 符号文字列に"-"付加 */
	        StrSign = "-";
	    }

	    /* 整数部にカンマ挿入 */
	    var NumRest = NumFig - 1;       /* 整数部桁数残り */
	    var NumCnt = 1;                 /* 桁数カウンタ */
	    var StrRet = "";                /* 結果文字列 */
	    while (NumRest >= 0) {      	/* 整数部桁数残りがなくなるまで */

	        /* 3桁ごとにカンマ挿入 */
	        if (NumCnt > 3) {           /* 3桁経過 */

	            /* カンマ挿入 */
	            StrRet = StrBuf.charAt(NumRest) + "," + StrRet;

	            /* 桁数カウンタ初期化 */
	            NumCnt = 1;
	        } else {

	            /* 数字の追加 */
	            StrRet = StrBuf.charAt(NumRest) + StrRet;
	        }
	        NumRest--;
	        NumCnt++;
	    }

	    /* 符号文字列、カンマをつけた整数部と小数部を連結 */
	    StrRet = StrSign + StrRet + StrBuf.substring(NumFig, StrBuf.length);
	    return StrRet;
	}

	/**
	 * 文字列からカンマを取り除く
	 * 引数  	:StrSrc		文字列
	 * 戻り値	:StrRet		カンマ除去後文字列
	 */
	function com_DelComma(StrSrc) {

	    return StrSrc.replace(/,/g, "");

	}

	/**
	 * 文字列が半角数値のみかチェックする
	 * 引数  	:StrSrc		文字列
	 * 戻り値	:true（正常）/false（異常）
	 */
	function com_ChkNum(StrSrc) {

		if(StrSrc.match(/[^0-9]/g)){
			return false;
		}else{
			return true;
		}

	}

	/**
	 * 日付に指定期間（年 or 月 or 日単位）を加算する
	 * 引数    ：StrDate   （日付文字列（YYYY-MM-DD形式））
	 *         ：NumTerm   （期間）
	 *         ：StrYMD    （期間単位）
	 *                    　"Y", "y"：年
	 *                    　"M", "m"：月
	 *                    　"D", "d"：日
	 * 戻り値  ：StrRet
	 *         　結果文字列（YYYY/MM/DD形式）
	 */
	function com_InsTerm(StrDate, NumTerm, StrYMD) {
	    /* 年月日取得 */
	    var ObjDateAry = StrDate.split("/");
	    ObjDateAry[0] -= 0;
	    ObjDateAry[1] -= 0;
	    ObjDateAry[2] -= 0;
	    /* +10000年した日付型に変換 */
	    var ObjDate =
	        new Date(ObjDateAry[0] + 10000, ObjDateAry[1] - 1, ObjDateAry[2]);
	    /* 数値型に変換 */
	    NumTerm = NumTerm - 0;

	    /* 期間ごとの加算処理 */
	    switch (StrYMD) {
	    case "Y":   /* 年単位 */
	    case "y":   /* 年単位 */
	        var NumMonth = ObjDate.getMonth();      /* 処理前の月 */

	        /* 年単位の加算 */
	        ObjDate.setFullYear(ObjDate.getFullYear() + NumTerm);

	        /* 月末補正(うるう年->通常年処理) */
	        if (NumMonth != ObjDate.getMonth()) {   /* 月が変わっていた場合 */
	            /* 前月末へ変更 */
	            ObjDate.setDate(0);
	        }
	        break;

	    case "M":   /* 月単位 */
	    case "m":   /* 月単位 */

	        /* 処理後の月計算 */
	        var NumMonth = ObjDate.getMonth();      /* 処理前の月 */
	        var NumNewMonth                         /* 処理後の月 */
	             = (NumMonth + NumTerm) % 12;
	        if (NumNewMonth < 0) {      /* 負になった場合 */
	             NumNewMonth += 12;
	        }

	        /* 月単位の加算 */
	        ObjDate.setMonth(NumMonth + NumTerm);

	        /* 月末補正(大月->小月処理) */
	        if (NumNewMonth != ObjDate.getMonth()) {    /* 月が翌月になった場合 */
	            /* 前月末へ変更 */
	            ObjDate.setDate(0);
	        }
	        break;

	    case "D":   /* 日単位 */
	    case "d":   /* 日単位 */

	        /* 日単位の加算 */
	        ObjDate.setDate(ObjDate.getDate() + NumTerm);
	        break;
	    default:
	        return null;
	    }

	    /* -10000年した本来の年月日取得 */
	    ObjDateAry[0] = ObjDate.getFullYear() - 10000;
	    ObjDateAry[1] = ObjDate.getMonth() + 1;
	    ObjDateAry[2] = ObjDate.getDate();

		/* HYY.MM.DD形式へ変換 */
	    var StrRet = ObjDateAry[0] + "";
	    if (StrRet.length < 4) {    /* 4桁以下の時 */
	        var StrBuf = "0000" + StrRet;
	        StrRet = StrBuf.slice(StrBuf.length - 4, StrBuf.length);
	    }
		StrRet = "H" + parseInt(StrRet-1988);
	    var StrBuf = "00" + ObjDateAry[1];
	    StrRet += "." + StrBuf.slice(StrBuf.length - 2, StrBuf.length);
	    StrBuf = "00" + ObjDateAry[2];
	    StrRet += "." + StrBuf.slice(StrBuf.length - 2, StrBuf.length);

	    return StrRet;
	}

	/**
	 * テキストオブジェクトの値(アルファベット)を大文字に変換する
	 * 引数    ：objText   テキストオブジェクト
	 * 戻り値  ：なし
	 */
	function updateESC(objText) {
		if (document.all) {objText.value = objText.value.toUpperCase()};
	}

	/**
	 * テキストエリアの行数を返す
	 * 引数    ：obj   	テキストエリアのオブジェクト名
	 * 戻り値  ：ｎLine	テキストエリアの行数
	 */
	function count_start(obj) {

		var nLine; 
		var oRcts; 

	    oTextRange = obj.createTextRange(); 
	    oRcts = oTextRange.getClientRects(); 
	    nLine = oRcts.length; 
	    

		return nLine;

	}

	/**
	 * テキストエリアの行数を返す
	 * 引数    ：obj   	テキストエリアのオブジェクト名
	 * 戻り値  ：ｎLine	テキストエリアの行数
	 */
	function chkBreak(obj, line) {
		
		ary = obj.value.split("\r\n");
		
		var s = "";
		var i;

		if (ary.length > line) {
			for (i=0; i<line; i++) {
				if (i != line-1) {
					s += ary[i] + "\r\n";
				} else {
					s += ary[i];
				}
			}
			obj.value = s;
		}
	}

//	/**
//	 * テキストエリアの行数を返す
//	 * 引数    ：obj   	テキストエリアのオブジェクト名
//	 * 戻り値  ：ｎLine	テキストエリアの行数
//	 */
//	var chk_memo;
//	function chkBreak(obj, line) {
//
//		ary = obj.value.split("\r\n");
//
//		if (ary.length > line) {obj.value = chk_memo;}
//		else {chk_memo = obj.value;}
//	}



	function ckDate(strdate) { 
		tmp1 = strdate.split("/");
		if(tmp1.length!=3) return false;
		tmp2 = tmp1[2].split(" ");
		
		var nYear	= tmp1[0];
		var nMonth	= tmp1[1]-1;
		var nDay	= tmp2[0];
		
		if(nMonth < 0 || nMonth > 11 || nDay < 1 || nDay > 31) return false

		var dtChkDate = new Date(strdate); 
		if(dtChkDate.getFullYear() == nYear && dtChkDate.getMonth() == nMonth && dtChkDate.getDate() == nDay)
		{
			return true; 
		}
		return false; 
	} 
	function ckDate_range(strdate1,strdate2) { 
		if(!ckDate(strdate1))return false;
		if(!ckDate(strdate2))return false;

		var dtChkDate1 = new Date(strdate1); 
		var dtChkDate2 = new Date(strdate2); 
		
		if(dtChkDate1<=dtChkDate2) return true;
		return false;
	}
	function ckTime_range(strHour1,strMinute1, strHour2,strMinute2) { 

		var nNum1 = eval(strHour1) * 100 + eval(strMinute1);
		var nNum2 = eval(strHour2) * 100 + eval(strMinute2);
		if(nNum1<nNum2) return true;
		return false;
	}
	function ckDate_range_today(nLine, strdate1,strdate2,strtime1,strtime2, MesType) { 

		var ShowHousou = "表示期間";
		if(MesType==1){
			ShowHousou = "放送期間"
		}


		var nRet=0;
		if(!ckDate(strdate1)){
			if(nLine<0){
				error.innerHTML = "開始日が不正なデータです";
			}else{
				error.innerHTML = "「" + ShowHousou + nLine+"」　開始日が不正なデータです";
			}
			nRet = -1;
		}
		if(strdate2!=""){
			if(!ckDate(strdate2)){
				if(nLine<0){
					error.innerHTML = "終了日が不正なデータです"+strdate2;
				}else{
					error.innerHTML = "「" + ShowHousou + nLine+"」　終了日が不正なデータです";
				}
				nRet = -1;
			}
		}
		
		var today=new Date();
		
		if(strdate2!=""){
			var dtChkDate1 = new Date(strdate1);
			var dtChkDate2 = new Date(strdate2);

			var dtChkTime1 = new Date(strdate1 + " " + strtime1);
			if(dtChkDate1>dtChkDate2){
				if(nLine<0){
					error.innerHTML = ShowHousou + "が不正です";
				}else{
					error.innerHTML = "「" + ShowHousou + nLine+"」　" + ShowHousou + "が不正です";
				}
				nRet = -1;
			}
		
			if(today>=dtChkDate1 && today>=dtChkDate2){
				if(today>=dtChkTime1){
				if(nLine<0){
					error.innerHTML = "過去の日付を設定することは出来ません";
				}else{
					error.innerHTML = "「" + ShowHousou + nLine+"」　過去の日付を設定することは出来ません";
				}
				nRet = -1;
				}
			}
		}else{
			var dtChkDate1 = new Date(strdate1);
			var dtChkTime1 = new Date(strdate1 + " " + strtime1);
		
			if(today>=dtChkDate1){
				if(today>=dtChkTime1){
				if(nLine<0){
					error.innerHTML = "過去の日付を設定することは出来ません";
				}else{
					error.innerHTML = "「" + ShowHousou + nLine+"」　過去の日付を設定することは出来ません";
				}
				nRet = -1;
				}
			}
		}


		if(nRet<0){
			pagetop.focus();
			return false;
		}
		return true;
	}
	function ckDate_range_dup2(strdate1,strdate2, strdateA,strdateB, strtime1,strtime2, strtimeA,strtimeB) { 
		var nRet=0;
		var dtChkDate1 = new Date(strdate1);
		var dtChkDate2 = new Date(strdate2);
		var dtChkDateA = new Date(strdateA);
		var dtChkDateB = new Date(strdateB);
		if(dtChkDate1<=dtChkDateA && dtChkDate2>=dtChkDateA){
			if(strtime1<strtimeB && strtime2>strtimeA){
				return false;
			}
		}
		if(dtChkDate1<=dtChkDateB && dtChkDate2>=dtChkDateB){
			if(strtime1<strtimeB && strtime2>strtimeA){
				return false;
			}
		}
		if(dtChkDateA<=dtChkDate1 && dtChkDateB>=dtChkDate1){
			if(strtime1<strtimeB && strtime2>strtimeA){
				return false;
			}
		}
		if(dtChkDateB<=dtChkDate2 && dtChkDateB>=dtChkDate2){
			if(strtime1<strtimeB && strtime2>strtimeA){
				return false;
			}
		}
		return true;
	}
	function IsAnk(chkString)
	{
		return chkString.match(/^[0-9a-zA-Z]+$/);
	}
//ログインID用のチェックは「-」「_」を例外として許可する
	function IsAnk_Login(chkString)
	{
		return chkString.match(/^[0-9a-zA-Z_\-]+$/);
	}

