Dogrula = Class.create();

Dogrula.prototype = {
	initialize : function(className, error, test) {
		this.test = test ? test : function(){ return true };
		this.error = error ? error : 'Doğrulama Hatalı.';
		this.className = className;
	}
}

var Dogrulama = Class.create();

Dogrulama.prototype = {
	initialize : function(form, options){
		this.options = Object.extend({
			stopOnFirst : false,
			immediate : false,
			focusOnError : true
		}, options || {});
		this.form = $(form);
		Event.observe(this.form,'submit',this.onSubmit.bind(this),false);
		if(this.options.immediate) {
			Form.getElements(this.form).each(function(input) { 
				Event.observe(input, 'blur', function(ev) { Dogrulama.validate(Event.element(ev).id) });
			});
		}
	},
	onSubmit :  function(ev){
		if(!this.validate()) {
			Event.stop(ev);
			if(this.options.focusOnError) {
				$$('.hatali').first().focus();
			}
		}
	},
	validate : function() {
		if(this.options.stopOnFirst) {
			return Form.getElements(this.form).all(Dogrulama.validate);
		} else {
			return Form.getElements(this.form).collect(Dogrulama.validate).all();
		}
	},
	reset : function() {
		Form.getElements(this.form).each(Dogrulama.reset);
	}
}

Object.extend(Dogrulama, {
	validate : function(elm, index, options){ 
		var options = Object.extend({}, options || {}); 
		elm = $(elm);
		var cn = elm.classNames();
		return result = cn.all(Dogrulama.test.bind(elm));
	},
	test : function(adi) {
		var v = Dogrulama.get(adi);
		var prop = '__advice'+adi.camelize();
		if(Dogrulama.gizli(this) && !v.test($F(this))) {
			if(!this[prop]) {
				var advice = Dogrulama.oneriyap(adi, this.id);
				if(typeof advice == 'undefined') {
					advice = document.createElement('div');
					advice.appendChild(document.createTextNode(v.error));
					advice.className = 'tavsiye';
					advice.id = 'advice-' + adi + '-' + this.id;
					advice.style.display = 'none';
					this.parentNode.insertBefore(advice, this.nextSibling);
				}
				if(typeof Effect == 'undefined') {
					advice.style.display = 'block';
				} else {
					new Effect.Appear(advice.id, {duration : 1 });
				}
			}
			this[prop] = true;
			this.removeClassName('dogru');
			this.addClassName('hatali');
			return false;
		} else {
			var advice = Dogrulama.oneriyap(adi, this.id);
			if(typeof advice != 'undefined') advice.hide();
			this[prop] = '';
			this.removeClassName('hatali');
			this.addClassName('dogru');
			return true;
		}
	},
	gizli : function(elm) {
		while(elm.tagName != 'BODY') {
			if(!$(elm).visible()) return false;
			elm = elm.parentNode;
		}
		return true;
	},
	oneriyap : function(adi, id) {
		var advice = Try.these(
			function(){ return $('advice-' + adi + '-' + id) },
			function(){ return $('advice-' + id) }
		);
		return advice;
	},
	reset : function(elm) {
		var cn = elm.classNames();
		cn.each(function(value) {
			var prop = '__advice'+value.camelize();
			if(elm[prop]) {
				var advice = Dogrulama.oneriyap(value, elm.id);
				advice.hide();
				elm[prop] = '';
			}
			elm.removeClassName('hatali');
			elm.removeClassName('dogru');
		});
	},
	add : function(className, error, test, options) {
		var nv = {};
		nv[className] = new Dogrula(className, error, test, options);
		Object.extend(Dogrulama.methods, nv);
	},
	tipsec : function(validators) {
		var nv = {};
		$A(validators).each(function(value) {
				nv[value[0]] = new Dogrula(value[0], value[1], value[2], (value.length > 3 ? value[3] : {}));
			});
		Object.extend(Dogrulama.methods, nv);
	},
	get : function(adi) {
		return  Dogrulama.methods[adi] ? Dogrulama.methods[adi] : new Dogrula();
	},
	methods : {}
});

//var $V = Dogrulama.validate;
//var $VG = Dogrulama.get;
//var $VA = Dogrulama.add;

Dogrulama.add('bos', '', function(v) {
				return  ((v == null) || (v.length == 0) || /^\s+$/.test(v));
			});

Dogrulama.tipsec([
	['zorunlu', 'Bu alanı boş bırakamazsınız.', function(v) {
				return !Dogrulama.get('bos').test(v);
			}],
	['sayi', 'Lütfen bu alanda sadece rakam kullanınız.', function(v) {
				return Dogrulama.get('bos').test(v) || !isNaN(v);
			}],
	['rakam', 'Lütfen bu alanda sadece rakam kullanınız.', function(v) {
				return Dogrulama.get('bos').test(v) ||  !/[^\d]/.test(v);
			}],
	['karakter', 'Lütfen bu alanda sadece harf (a-z) kullanınız.', function (v) {
				return Dogrulama.get('bos').test(v) ||  /^[a-zA-Z]+$/.test(v)
			}],
	['alfanumerik', 'Lütfen bu alanda sadece rakam ve harf (a-z) kullanınız. Boşluk yada özel karakter kullanmayınız.', function(v) {
				return Dogrulama.get('bos').test(v) ||  !/\W/.test(v)
			}],
	['email', 'Lütfen geçerli bir email adresi yazınız.', function (v) {
				return Dogrulama.get('bos').test(v) || /\w{1,}[@][\w\-]{1,}([.]([\w\-]{3,})){1,3}$/.test(v)
			}],
	['tarih', 'Lütfen tarihi : gg/aa/yyyy şeklinde giriniz. Örneğin 17/05/2006 (17 Mayıs 2006 için).', function(v) {
				if(!Dogrulama.get('bos').test(v)) {
					var upper = 31;
					if(/^(\d{2})\/(\d{2})\/(\d{4})$/.test(v)) { // gg/aa/yyyy formatında tarih kontrolü
						if(RegExp.$2 == '02') upper = 29;
						if((RegExp.$1 <= upper) && (RegExp.$2 <= 12)) {
							return true;
						} else {
							return false;
						}
					} else {
						return false;
					}
				} else {
					return true;
				}
			}],
	['para', 'Lütfen YTL cinsinden yazınız. Örneğin 110,000.50 yada 0.25 .', function(v) {
	// 1[##][,###]+[.##]
	// 1###+[.##]
	// 0.##
	// .##
	return Dogrulama.get('bos').test(v) ||  /^\$?\-?([1-9]{1}[0-9]{0,2}(\,[0-9]{3})*(\.[0-9]{0,2})?|[1-9]{1}\d*(\.[0-9]{0,2})?|0(\.[0-9]{0,2})?|(\.[0-9]{1,2})?)$/.test(v)
			}]
]);
