/*
 * jquery.value.filter.js
 *
 * Copyright(C) 2008 codecheck.in/101000code/101000LAB/
 * http://trac.codecheck.in
 * http://code.101000lab.org
 *
 * Licensed under The MIT License
 */
	(function($) {

		 /**
		  * money
		  *
		  * 金額などを入力するinput要素のvalue値に、自動的に3桁区切りでカンマを付加します。
		  * elmで指定した要素がクリックされると自動でカンマが取り除かれます。
		  * elmはsubmit要素などを指定しておくとvalue値が数値型でPOSTされます。
		  */
		  /*
		  elm->フィルタリングするelement
		  bln->小数を含むときtrue
		  blnC->カンマ編集するときtrue 省略時true
		  dec_cnt->小数以下桁数 bln=trueのとき、省略時は2
		  */
			$.fn.money = function(elm,bln,blnC,dec_cnt) {
				var self = this;
				var strVal = self.val();
				var cnfVal;
				if(blnC == null) blnC = true;
				//小数以下桁数省略時 小数第2位まで
				if(dec_cnt == null) dec_cnt = 2;
				if (self.val() != undefined) {
					//self.val().replace(/[^0-9\.]/g,'').replace(/((?:^[-+])?\d{1,3})(?=(?:\d{3})+(?!\d))/g, "$1,")
					if(bln){
//						cnfVal = A_Comma2(R_Comma(strVal));
						//整数部分をカンマ編集し、小数以下桁数を整える
						cnfVal = chkNumFormat(strVal,dec_cnt);
//						self.val(A_Comma2(R_Comma(strVal)));
					} else{
						cnfVal = A_Comma(R_Comma(strVal));
//						self.val(A_Comma(R_Comma(strVal)));
					}
					//カンマ編集しないとき、カンマを取り除く
					if(!blnC) cnfVal = R_Comma(cnfVal);
					// cnfVal = R_Comma(cnfVal);
					self.val(cnfVal);
					//↓　新規サーバで挙動不審のためコメント化
					//$(this).select();
				};
				self.keyup(
					function(){
//					self.val().replace(/[^0-9]/g,'').replace(/((?:^[-+])?\d{1,3})(?=(?:\d{3})+(?!\d))/g, "$1,")
						strVal = $(this).val();
						//マイナスチェック
						var strTop = "";
						if(strVal.charAt(0) == "-") strTop = "-";
						//小数含むとき、半角数字と小数点以外の入力はキャンセル
						if(bln){
							self.val(strTop + strVal.replace(/[^0-9\.]/g,''));
						} else{
						//小数含まないとき、半角数字以外の入力はキャンセル
							self.val(strTop + strVal.replace(/[^0-9]/g,''));
						}
					}
				);
				self.blur(
					function(){
//					self.val().replace(/[^0-9]/g,'').replace(/((?:^[-+])?\d{1,3})(?=(?:\d{3})+(?!\d))/g, "$1,")
						strVal = $(this).val();
						//strVal = blurNum(strVal,bln,blnC);
						if(bln){
//						cnfVal = A_Comma2(R_Comma(strVal));
						//整数部分をカンマ編集し、小数以下桁数を整える
							cnfVal = chkNumFormat(strVal,dec_cnt);
//							self.val(A_Comma2(R_Comma(strVal)));
						} else{
							cnfVal = A_Comma(R_Comma(strVal));
//							self.val(A_Comma(R_Comma(strVal)));
						}
						if(!blnC) cnfVal = R_Comma(cnfVal);
						self.val(cnfVal);
					}
				);
				if(elm != undefined && blnC){
					$(elm).click(
						function(){
							if (self.val() != undefined) {
								self.val(
									self.val().replace(/[^0-9\.\-]/g,'')
								);
								self.select();
							};
						}
					);
					$(elm).focus(
						function(){
							if (self.val() != undefined) {
								self.val(
									self.val().replace(/[^0-9\.\-]/g,'')
								);
								self.select();
							};
						}
					);
				}
				 return self;
			 };

			//半角英数のみ
			$.fn.code = function(elm) {
				var self = this;
				var strVal = self.val();
				var cnfVal;
				if (self.val() != undefined) {
					//self.val().replace(/[^0-9\.]/g,'').replace(/((?:^[-+])?\d{1,3})(?=(?:\d{3})+(?!\d))/g, "$1,")
					self.val(cnfVal);
				};

				self.keyup(
					function(){
//					self.val().replace(/[^0-9]/g,'').replace(/((?:^[-+])?\d{1,3})(?=(?:\d{3})+(?!\d))/g, "$1,")
						strVal = $(this).val();
						self.val(strVal.replace(/[\W]/g,''));
					}
				);
				self.blur(
					function(){
//					self.val().replace(/[^0-9]/g,'').replace(/((?:^[-+])?\d{1,3})(?=(?:\d{3})+(?!\d))/g, "$1,")
						strVal = $(this).val();
						self.val(strVal.replace(/[\W]/g,''));
					}
				);

				 return self;
			 };

			//半角数のみ
			$.fn.numcode = function(elm) {
				var self = this;
				var strVal = self.val();
				var cnfVal;
				if (self.val() != undefined) {
					self.val(strVal.replace(/[^0-9]/g,''));
				};

				self.keyup(
					function(){
//					self.val().replace(/[^0-9]/g,'').replace(/((?:^[-+])?\d{1,3})(?=(?:\d{3})+(?!\d))/g, "$1,")
						strVal = $(this).val();
						self.val(strVal.replace(/[\D]/g,''));
					}
				);
				self.blur(
					function(){
//					self.val().replace(/[^0-9]/g,'').replace(/((?:^[-+])?\d{1,3})(?=(?:\d{3})+(?!\d))/g, "$1,")
						strVal = $(this).val();
						self.val(strVal.replace(/[\D]/g,''));
					}
				);

				 return self;
			 };

	 })(jQuery); // function($)
