
var Validator = {
	check: function(field, reg, extra, cmp) {
		var response;
		var rule = this.rule;
		rule.field = field;
		rule.value = field.value;
		if( reg == 'bfCheck' ) rule.value += 'chkVal';//バッグ算出式用 前処理
		rule.extra = extra;
		rule.cmp   = cmp;

		if(!reg || !reg.match(/^!/))
			response = rule.input();

		if(reg && !response && rule.value != '') {
			reg = reg.replace(/^!/, '');

			var mode = reg.split(/\s+/);
			for(var i = 0, m; m = mode[i]; i++) {
				m = m.replace(/([\d\-]+)?$/, '');
				response = rule[m](RegExp.$1);
				if(response) break;
			}
		}

		if(response) {
			 this.baloon.open(field, response);
		}
	},

	submit: function(form, cnf) {
		this.allclose(form);
		var btns = new Array;

		for(var i = 0, f; f = form[i]; i++) {
			if(f.onblur)
				f.onblur();
			if(f.type == 'submit')
				btns.push(f);
		}

		for(var i = 0, f, z; f = form[i]; i++) {
			if(f._validbaloon && f._validbaloon.visible()) {
				while (z = btns.shift())
					this.baloon.open(z, this.rule.submit());
				return false;
			}
		}
		
	//	if(!cnf) {
	//		return confirm("ご入力いただいた内容で登録してもよろしいですか？");
	//	} else if(cnf=='DEL') {
	//		return confirm("ご選択いただいたものを削除してよろしいですか？");
	//	} else {
	//		return true;
	//	}
	},

	allclose: function(form) {
		for(var i = 0, f; f = form[i]; i++)
			if(f._validbaloon) f._validbaloon.close();
	}
};

Validator.baloon = {
	index: 0,

	open: function(field, msg) {
		if(!field._validbaloon) {
			var obj = new this.element(field);
			obj.create();
			field._validbaloon = obj;
			if(field.type == 'radio' || field.type == 'checkbox') {
				for(var i = 0, e; e = field.form[field.name][i]; i++)
					addEvent(e, 'focus', function() { obj.close(); });
			 }
		}

		field._validbaloon.show(msg);
	},

	element: function() {
		this.initialize.apply(this, arguments);
	}
};

Validator.baloon.element.prototype = {
	initialize: function(field) {
		this.parent = Validator.baloon;
		this.field = field;
	},

	create: function() {
		var field  = this.field;

		var box = document.createElement('div');
		box.className = 'baloon';
		var offset = Position.offset(field);

		var top  = offset.y - 25;
		var left = offset.x - 20 + field.offsetWidth;
		box.style.top  = top +'px';
		box.style.left = left+'px';

		var self = this;
		addEvent(box, 'click', function() { self.toTop(); });

		var bindClose = function() { self.close(); };
		var link = document.createElement('a');
		link.appendChild(document.createTextNode('X'));
		link.setAttribute('href', 'javascript:void(0);');
		addEvent(link, 'click', bindClose);
		addEvent(field, 'focus', bindClose);

		var msg = document.createElement('span');
		var div = document.createElement('div');
		div.appendChild(msg);
		div.appendChild(link);
		box.appendChild(div);
		document.body.appendChild(box);

		this.box = box;
		this.msg = msg;
	},

	show: function(msg) {
		var field = this.field;
		this.msg.innerHTML  = msg;

		this.box.style.display = '';
		this.toTop();

		if(field.type != 'radio' && field.type != 'checkbox') {
			var colors = new Array('#FF6666', '#FFAAAA', '#FF6666', '#FFAAAA');
			window.setTimeout(function() {
				if(colors.length > 0) {
					field.style.backgroundColor = colors.shift();
					window.setTimeout(arguments.callee, 70);
				}
			}, 10);
		}
	},

	close: function() {
		this.box.style.display = 'none';
		this.field.style.backgroundColor = '';
	},

	visible: function() {
		return (this.box.style.display == '');
	},

	toTop: function() {
		this.box.style.zIndex = ++ this.parent.index;
	}
};

Validator.rule = {
	msg: null,

	submit: function() {
		return this.msg.submit;
	},

	input: function() {
		if(this.field.type == 'radio' || this.field.type == 'checkbox') {
			if (!this.field.form[this.field.name].length) {
				if(this.field.form[this.field.name].checked) return;
				else return this.msg.noselect;
			}
			for(var i = 0, e; e = this.field.form[this.field.name][i]; i++)
				if(e.checked) return;
			return this.msg.noselect;
		} else if(this.value == '')
			return (this.field.type == 'select-one') ? this.msg.noselect : this.msg.noinput;
	},

	mail: function() {
		if(!this.value.match(/^[\x01-\x7F]+@((([-a-z0-9]+\.)*[a-z]+)|(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}))$/))
			return this.msg.mail;
	},
	
	url: function() {
		if(!this.value.match(/s?https?:\/\/[-_.!~*'()a-zA-Z0-9;\/?:\@&=+\$,%#]+/))
			return this.msg.url;
	},

	equal: function() {
		if(this.field.form[this.extra].value && this.value != this.field.form[this.extra].value)
			return this.msg.unequal;
	},

	alphabet: function() {
		if(!this.value.match(/^[a-zA-Z\-\d]+$/))
			return this.msg.alphabet;
	},
	
	time: function() {
		if(!this.value.match(/^\d{2}:\d{2}$/))
			return this.msg.time;
	},

	zip: function() {
		if(!this.value.match(/^\d{3}-\d{4}$/))
			return this.msg.time;
	},

	req_alphanum: function() {
		if(!this.value.match(/^[a-zA-Z\-\d]+$/) ||
			!(this.value.match(/[a-zA-Z]/) && this.value.match(/[0-9]/)) )
			return this.msg.req_alphanum;
	},
	


	kana: function() { //30A0 ～ 30FF
		for(var i = 0, len = this.value.length; i < len;i++) {
			if(this.value.charAt(i) == ' ' || this.value.charAt(i) == '\u3000') continue;
//			if(this.value.charAt(i) < '\u30A1' || this.value.charAt(i) > '\u30F6')
			if(this.value.charAt(i) < '\u30A0' || this.value.charAt(i) > '\u30FF')
				return this.msg.kana;
		}
	},

	hira: function() { //3040 ～ 309F
		for (var i = 0, len = this.value.length; i < len;i++) {
			if(this.value.charAt(i) == ' ' || this.value.charAt(i) == '\u3000' || this.value.charAt(i) == '\u30FC') continue;
			if(this.value.charAt(i) < '\u3040' || this.value.charAt(i) > '\u309F')
				return this.msg.hira;
		}
	},

//	kanji: function() { //4E00 ～ 9FFF
//		for (var i = 0, len = this.value.length; i < len;i++) {
//			if(this.value.charAt(i) == ' ' || this.value.charAt(i) == '\u3000') continue;
//			if(this.value.charAt(i) < '\u4E00' || this.value.charAt(i) > '\u9FFF')
//				return this.msg.kanji;
//		}
//	},

	nohankana: function() {
		if ( this.value.match(/[\uFF61-\uFF9F]/) )
			return this.msg.nohankana;
	},

	count: function(arg) {
		return this._range(arg, this.value.length, this.msg.count);
	},

	num: function(arg) {
		if(!this.value.match(/^\d+$/))
			return this.msg.num.nonumber;
		return this._range(arg, parseInt(this.value), this.msg.num);
	},


//追記　日時判定1
	date: function(arg) {
		var inp    = document.getElementById(this.extra);
		var dVal   = inp.value;
		
		if (dVal.match(/((?:20)?\d{2})\D*(1[0-2]|0?[1-9])\D*(1[0-9]|2[0-9]|3[01]|0?[1-9])/)) {
			try {
				var y = RegExp.$1;
				var m = RegExp.$2;
				var d = RegExp.$3;
				y = 1*y;
				if (y < 2000) y = 2000 + 1*y;
				m = 1*m;
				d = 1*d;
				var date = new Date(y, m-1, d);
				if (date.getFullYear() != y || date.getMonth() != m-1 || date.getDate() != d) {
					return this.msg.date.noanalysis;
				} else {
					if (m < 10) m = "0" + m;
					if (d < 10) d = "0" + d;
					inp.value = y + "-" + m + "-" + d;
				}
			} catch(e) {
				return this.msg.date.noanalysis;
			}
		} else {
			return this.msg.date.noanalysis;
		}
	},

//追記　日時判定2
	dateYM: function(arg) {
		var inp    = document.getElementById(this.extra);
		var dVal   = inp.value;

		if (dVal.match(/((?:20)?\d{2})\D*(1[0-2]|0?[1-9])\D*/)) {
			try {
				var y = RegExp.$1;
				var m = RegExp.$2;
				var d = 01;
				y = 1*y;
				if (y < 2000) y = 2000 + 1*y;
				m = 1*m;
				var date = new Date(y, m-1, d);
				if (date.getFullYear() != y || date.getMonth() != m-1) {
					return this.msg.date.noanalysis;
				} else {
					if (m < 10) m = "0" + m;
					inp.value = y + "-" + m;
				}
			} catch(e) {
				return this.msg.date.noanalysis;
			}
		} else {
			return this.msg.date.noanalysis;
		}
	},


//追記　日時判定3 開始日、終了日との比較
	dateCmp: function(arg) {
		var inp    = document.getElementById(this.extra);
		var cmpElm = document.getElementById(this.cmp);
		var dVal   = inp.value;
		var cVal   = cmpElm.value;
		
		if (dVal.match(/((?:20)?\d{2})\D*(1[0-2]|0?[1-9])\D*(1[0-9]|2[0-9]|3[01]|0?[1-9])/)) {
			try {
				var y = RegExp.$1;
				var m = RegExp.$2;
				var d = RegExp.$3;
				y = 1*y;
				if (y < 2000) y = 2000 + 1*y;
				m = 1*m;
				d = 1*d;
				var date = new Date(y, m-1, d);
				if (date.getFullYear() != y || date.getMonth() != m-1 || date.getDate() != d) {
					return this.msg.date.noanalysis;
				} else {
					if (m < 10) m = "0" + m;
					if (d < 10) d = "0" + d;
					inp.value = y + "-" + m + "-" + d;
					if (this.cmp == 'endDate' && cVal != '') {
						if (inp.value > cmpElm.value) return this.msg.date.oppositeS;
					} else if(this.cmp != 'endDate' && dVal != '') {
						if (inp.value < cmpElm.value) return this.msg.date.oppositeE;
					}
				}
			} catch(e) {
				return this.msg.date.noanalysis;
			}
		} else {
			return this.msg.date.noanalysis;
		}
	},


	checked: function(arg) {
		var value = 0;
		for(var i = 0, e; e = this.field.form[this.field.name][i]; i++)
			if(e.checked) value += 1;
		return this._range(arg, value, this.msg.check);
	},

	dup_id: function() {
		var id  = this.value;
		var uid = this.extra && this.field.form[this.extra] && this.field.form[this.extra].value ?
			this.field.form[this.extra].value : '';
		var self = this;
		var res;
		requestHttp("usrLoginID="+id+";usrID="+uid, "GET", "/mng/mngChkUsr.cgi",
			function(obj) {
				if (obj.responseText != "0") {
					res = self.msg.dupid;
				}
			},
			false
		);
		return res;
	},

	dup_URL: function() {
		if(!this.value.match(/^[a-zA-Z\-\_\d]+$/) ||
			!(this.value.match(/[a-zA-Z]/) || this.value.match(/[0-9]/)) )
			return this.msg.dupURLcheck;
	},


	dup_Dir: function() {
		var dir  = this.value;
		var cmpID = this.extra || '';
		var self = this;
		var res;
		requestHttp("subDir="+dir, "GET", "/shokokai/chkDir.cgi",
			function(obj) {
				if (obj.responseText != "0") {
					res = self.msg.dupURL;
				}
			},
			false
		);
		return res;
	},
	
	dup_ID: function() {
		var dir  = this.value;
		var cmpID = this.extra || '';
		var self = this;
		var res;
		requestHttp("subDir="+dir+";compID="+cmpID, "GET", "/shokokai/chkID.cgi",
			function(obj) {
				if (obj.responseText != "0") {
					res = self.msg.dupURL;
				}
			},
			false
		);
		return res;
	},

	dup_logid: function() {
		var lid  = this.value;
		var uid = this.extra && this.field.form[this.extra] && this.field.form[this.extra].value ?
			this.field.form[this.extra].value : '';
		var self = this;
		var res;
		requestHttp("usrLoginID="+lid+";usrID="+uid, "GET", "mngChkUsr.cgi",
			function(obj) {
				if (obj.responseText != "0") {
					res = self.msg.dupid;
				}
			},
			false
		);
		return res;
	},


//
	test_id: function() {
		var id  = this.value;
		var uid = this.extra && this.field.form[this.extra] && this.field.form[this.extra].value ?
			this.field.form[this.extra].value : '';
		var self = this;
		var res;
		requestHttp("DB.tProduct.prdCode="+id+";DB.tProduct.lot="+uid, "GET", "/mng/mngChkLot.cgi",
			function(obj) {
				if (obj.responseText != "0") {
					res = self.msg.testid;
				}
			},
			false
		);
		return res;
	},

	chk_lot: function() {
		var lot  = this.value;
		var pCode = this.extra && this.field.form[this.extra] && this.field.form[this.extra].value ?
			this.field.form[this.extra].value : '';
		var self = this;
		var res;
		requestHttp("DB.tProduct.prdCode="+pCode+";DB.tProduct.lot="+lot, "GET", "mngChkLot.cgi",
			function(obj) {
				if (obj.responseText != "0") {
					res = self.msg.testid;
				}
			},
			false
		);
		return res;
	},
//


	_range: function(range, value, msg) {
		if(!range) return;

		var result = '';
		var c = (" " + range).split(/\-/);
		var min = parseInt(c[0]) || 0;
		var max = parseInt(c[1]) || 0;

		if(value != min && /^\d+$/.test(range))
			result = msg.unequal;
		else if(min == 0 && value > max)
			result = msg.too_big;
		else if(max == 0 && value < min)
			result = msg.too_small;
		else if(min > 0 && max > 0 && (value < min || value > max))
			result = msg.outofrange;

		return result.replace(/%1/g, min).replace(/%2/g, max);
	},
	
	chkSlc: function() {
	var trg = document.getElementById(this.extra);
		if(!trg.value) {
				return this.msg.nohankana;
		}
	}

};

Validator.lang = {
	ja: {
		noselect:	'\u9078\u629E\u304C\u5FC5\u8981\u3067\u3059\u3002',
		noinput:	'\u5165\u529B\u304C\u5FC5\u8981\u3067\u3059\u3002',
		unequal:	'\u5165\u529B\u304C\u63C3\u3063\u3066\u3044\u307E\u305B\u3093\u3002',

		submit:		'\u5165\u529B\u30A8\u30E9\u30FC\u304C\u3042\u308A\u307E\u3059\u3002',
		mail:		'\u30E1\u30FC\u30EB\u30A2\u30C9\u30EC\u30B9\u306E\u5F62\u5F0F\u304C\u6B63\u3057\u304F\u3042\u308A\u307E\u305B\u3093\u3002',
		url:		'URL\u306E\u5F62\u5F0F\u304C\u6B63\u3057\u304F\u3042\u308A\u307E\u305B\u3093\u3002',

		alphabet:	'\u30A2\u30EB\u30D5\u30A1\u30D9\u30C3\u30C8\u3001\u6570\u5B57\u3001' +
						'- \u4EE5\u5916\u306F\u5165\u529B\u51FA\u6765\u307E\u305B\u3093\u3002',
		req_alphanum:'\u534A\u89D2\u82F1\u6570\u5B57\u54041\u6587\u5B57\u4EE5\u4E0A\u3092\u4F7F\u7528\u3057\u3066\u304F\u3060\u3055\u3044',
		kana:		'\u5168\u89D2\u30AB\u30BF\u30AB\u30CA\u3067\u5165\u529B\u3057\u3066\u4E0B\u3055\u3044\u3002',
		hira:		'\u5168\u89D2\u3072\u3089\u304C\u306A\u3067\u5165\u529B\u3057\u3066\u4E0B\u3055\u3044\u3002',
//		kanji:		'\u6F22\u5B57\u3067\u5165\u529B\u3057\u3066\u4E0B\u3055\u3044\u3002',
		nohankana:	'\u534A\u89D2\u30AB\u30BF\u30AB\u30CA\u306F\u5165\u529B\u3067\u304D\u307E\u305B\u3093\u3002',
		time:		'\u5165\u529B\u66F8\u5F0F\u304C\u9593\u9055\u3063\u3066\u3044\u307E\u3059\u3002',
		count: {
			unequal:	'%1'+'\u6587\u5B57\u3067\u5165\u529B\u3057\u3066\u4E0B\u3055\u3044\u3002',
			too_big:	'%2'+'\u6587\u5B57\u4EE5\u5185\u3067\u5165\u529B\u3057\u3066\u4E0B\u3055\u3044\u3002',
			too_small:	'%1'+'\u6587\u5B57\u4EE5\u4E0A\u5165\u529B\u3057\u3066\u4E0B\u3055\u3044\u3002',
			outofrange:	'%1'+'\u304B\u3089'+'%2'+'\u6587\u5B57\u306E\u9593\u3067\u5165\u529B\u3057\u3066\u4E0B\u3055\u3044\u3002'
		},

		num: {
			nonumber:	'\u6570\u5024\u3067\u5165\u529B\u3057\u3066\u4E0B\u3055\u3044\u3002',
			unequal:	'%1'+'\u3068\u5165\u529B\u3057\u3066\u4E0B\u3055\u3044\u3002',
			too_big:	'%2'+'\u4EE5\u4E0B\u306E\u5024\u3092\u5165\u529B\u3057\u3066\u4E0B\u3055\u3044\u3002',
			too_small:	'%1'+'\u4EE5\u4E0A\u306E\u5024\u3092\u5165\u529B\u3057\u3066\u4E0B\u3055\u3044\u3002',
			outofrange:	'%1'+'\u304B\u3089'+'%2'+'\u306E\u9593\u3067\u5165\u529B\u3057\u3066\u4E0B\u3055\u3044\u3002'
		},

		date: {
			noanalysis:	'\u65E5\u4ED8\u66F8\u5F0F\u304C\u89E3\u6790\u3067\u304D\u307E\u305B\u3093',
			oppositeS:	'\u7D42\u4E86\u65E5\u3088\u308A\u524D\u306E\u65E5\u3092\u3054\u5165\u529B\u304F\u3060\u3055\u3044\u3002',
			oppositeE:	'\u958B\u59CB\u65E5\u3088\u308A\u524D\u306E\u65E5\u3092\u3054\u5165\u529B\u304F\u3060\u3055\u3044\u3002'
		},
		
		dupURL:		'\u65E2\u306B\u4F7F\u7528\u4E2D\u3067\u3059\u3002\u4ED6\u306EURL\u306B\u3057\u3066\u4E0B\u3055\u3044\u3002',
		dupURLcheck:	'\u4F7F\u7528\u3067\u304D\u306A\u3044\u6587\u5B57\u7A2E\u304C\u5165\u529B\u3055\u308C\u3066\u307E\u3059\u3002',
		
		check: {
			unequal:	'\u30C1\u30A7\u30C3\u30AF\u306F'+'%1'+'\u500B\u3057\u3066\u4E0B\u3055\u3044\u3002',
			too_big:	'\u30C1\u30A7\u30C3\u30AF\u306F'+'%2'+'\u500B\u307E\u3067\u3067\u3059\u3002',
			too_small:	'\u30C1\u30A7\u30C3\u30AF\u306F'+'%1'+'\u500B\u4EE5\u4E0A\u3057\u3066\u4E0B\u3055\u3044\u3002',
			outofrange:	'\u30C1\u30A7\u30C3\u30AF\u306F'+'%1'+'\u500B\u304B\u3089'+'%2'+'\u500B\u307E\u3067\u3067\u3059\u3002'
		},
		
		bfCheck:		'\u7B97\u51FA\u5F0F\u3092\u3054\u5165\u529B\u304F\u3060\u3055\u3044\u3002',
		
		dupid:			'\u65E2\u306B\u4F7F\u7528\u4E2D\u3067\u3059\u3002\u4ED6\u306EID\u306B\u5909\u66F4\u3057\u3066\u304F\u3060\u3055\u3044\u3002',
		
		testid:			'\u65E2\u306B\u4F7F\u7528\u4E2D\u3067\u3059\u3002\u4ED6\u306EID\u306B\u5909\u66F4\u3057\u3066\u304F\u3060\u3055\u3044\u3002'
	}
};

Validator.rule.msg = Validator.lang.ja;


