var OrcoQuestions = new Class({

	Implements: Options,
	
	options : {
		form : 'step1'
	},
	
	values : {},
	
	validates : {},
	
	errors : {
		phone : 'Lütfen geçerli bir telefon numarası yazın',
		email : 'Lütfen geçerli bir email adresi yazın'
	},
	
	regex : {
		phone : /^[-+]?\d*\.?\d+$/,
		email : /^[a-z0-9._%-]+@[a-z0-9.-]+\.[a-z]{2,4}$/i
	},
	
	elements: [],
	
	isValid: true,
	
	initialize : function(options) {
		this.setOptions(options);
		this.form = $(this.options.form)
		this.elements = this.form.getElements("*[type*=text],textarea");
		this.elements.each(function(el) {
			eval('this.values.' + el.id + '="' + el.value + '"');
			this.form.getElements("*[class*=validate]").each(function(el) {
				el.validation = [];
				var classes = el.getProperty("class").split(' ');
				classes.each(function(classX) {
					if(classX.match(/^validate(\[.+\])$/)) {
						var validators = eval(classX.match(/^validate(\[.+\])$/)[1]);
						eval('this.validates.' + el.id + '="' + validators[0] + '"');
					}
				}, this);
			}, this);
			el.addEvent('focus',this.focus.bind(el,this.values));
			el.addEvent('blur',this.blur.bind(this,[el,this.values]));
		},this);
		this.form.addEvent('submit',this.validate.bind(this,[this.values,this.elements]));
	},
	
	focus: function(values) {
		if(this.value==eval('values.' + this.id)) {
			this.value = "";
		}
	},
	
	blur: function(item,values) {
		if(item.value=="") {
			item.value = eval('values.' + item.id);
			this.removeError(item);
			this.showError(item);
		} else {
			var confirm = eval('this.validates.' + item.id);
			if(confirm) {
				var reg = new RegExp(eval('this.regex.' + confirm));
				if(!reg.test(item.value)) {
					this.removeError(item);
					this.showError(item,eval('this.errors.' + confirm));
				} else {
					this.removeError(item);
				}
			} else {
				this.removeError(item);
			}
		}
	},
	
	submit: function() {
		var form = this.form;
		new Fx.Tween(this.form,{
			property: 'opacity',
			duration: 400,
			onComplete: function() {
				$('content').addClass('waiting');
				var data = form.toQueryString();
				if($('step').value!='Son') {
					var step = parseInt($('step').value) + 1;
				} else {
					var step = 'Son';
				}
				new Request({
					url: "requestHandler.php",
					data: data,
					method: "post",
					onComplete: function(response) {
						$('content').removeClass('waiting');
						$('content').set('html',response);
						if(step!='Son') {
							var orco = new OrcoQuestions({
								form: 'step' + step
							});
						}
						var done = false;
						var stepSlide = new Fx.Slide('stepcounter',{
							mode: 'horizontal',
							onComplete: function() {
								if(!done) {
									if(step!='Son') {
										$('stepcounter').set('text',step + '. Adım');
									} else {
										$('stepcounter').set('text',step + ' Adım');
									}
									done = true;
									stepSlide.slideIn();
								} else {
									done = false;
								}
							}
						});
						stepSlide.slideOut();
					}
				}).send();
			}
		}).start(0);
		return false;
	},
	
	validate: function(values,elements) {
		this.isValid = true;
		for(var i in values) {
			if($(i).value==eval('values.' + i)) {
				this.isValid = false;
				this.showError($(i));
			}
		}
		if(this.isValid) {
			this.submit();
		}
		return false;
	},
	
	showError: function(item,msg) {
		if(!msg) {
			msg = 'Bu alanın doldurulması zorunludur';
		}
		var err = item.getNext();
		if(!err.hasClass("error_f")) {
			var el = new Element('div');
			el.set('text',msg);
			el.addClass('error_f');
			el.set('opacity','0');
			el.inject(item,'after');
			el.fade('in');
		}
	},
	
	removeError: function(item) {
		var err = item.getNext();
		if(err.hasClass("error_f")) {
			err.destroy();
		}	
	}
	
});

OrcoQuestions.implement(new Options());
