//
//	Form validation
//
//	Yarick Glodov 08/2008
//

function Form(){

	this.expressions = new Array();
	this.controlExpr = new Array();

	this.checkControl = function(obj){
		this.clearError(obj);
		var result = true;
		var value = $(obj).val();
		if ($(obj).hasClass('required')){
			if ($(obj).get(0).tagName=='SELECT' && $(obj).val()==0 || value.trim()=='') result = false;
		} else {

		}
		if (result){
			result = this.checkExpr(obj);
		}
		if (!result){
			this.showError(obj);
		}
		return result;
	}

	this.checkExpr = function(obj){
		this.clearError(obj);
		var result = true;
		for (i=0; i<this.controlExpr.length; i++){
			if ($(obj).get(0).name==this.controlExpr[i][0]){
				if (this.expressions[this.controlExpr[i][1]]){
					var expr = this.expressions[this.controlExpr[i][1]];
					if (expr.test($(obj).val())){

					} else {
						result = false;
					}
				}
			}
		}
		return result;
	}

	this.check = function(form){
		var filled = true;
		$(form).find('input,select,textarea').each(function(){
			if (!Form.checkControl(this)) filled = false;
		});
		if (filled){
			$(form).find('input[type="submit"],input.submit').removeAttr('disabled');
		}

		return filled;
	}

	this.setCurrent = function(name,value){
		$('select[name="'+name+'"]').each(function(){
			for(i=this.options.length-1;i>=0;i--){
				if (this.options[i].value==value) this.selectedIndex=i;
			}
		});
	}

	this.setCheckbox = function(id,a){
		$('#'+id+' input[type="checkbox"]').each(function(){
			$(this).attr('checked','');
			for (i=0; i<a.length; i++){
				if ($(this).val()==a[i]) $(this).attr('checked','checked');
			}
		});
	}

	this.load = function(){
		$('form').submit(function(){
			return Form.check(this);
		});
		//$('input,form select.required,textarea').blur(function(){
		$('input,form select.required,textarea').keypress(function(){
			var o = $(this).parent();
			for (i=0;i<100;i++){
				o = o.parent();
				if (o.get(0).tagName=='FORM'){
					Form.check(o.get(0));
					return true;
					break;
				}
			}
			return Form.checkControl(this);
		});
	}

	this.addExpr = function(name,expr){
		this.expressions[name] = expr;
	}

	this.setExpr = function(control,expr){
		this.controlExpr[this.controlExpr.length] = new Array(control,expr);
	}

	this.clearError = function(obj){
		$(obj).removeClass('error');
	}

	this.showError = function(obj){
		$(obj).addClass('error');
	}

}

var Form = new Form();

Form.addExpr('email',/^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/);
Form.addExpr('phone',/^\+\d{1,3}\(\d{2,5}\)\d{5,9}$/);
Form.addExpr('date',/^\d{2}\.\d{2}\.\d{4}$/);
Form.addExpr('datetime',/^\d{2}\.\d{2}\.\d{4} \d{2}:\d{2}$/);
Form.addExpr('time',/^\d{2}\:\d{2}$/);


Form.select = function(p,val){
	if (!p) return false;
	$(p).each(function(){
		switch ($(this).get(0).tagName)
		{
			case 'INPUT':
				this.checked = false
			break;

			case 'SELECT':
				this.selectedIndex = 0;
			break;
		}
	});
	$(p).each(function(){
		switch ($(this).get(0).tagName)
		{
			case 'INPUT':
				if ($(this).attr('type')=='checkbox'){
					if (typeof val != 'number' && typeof val != 'string') {
						for (var i=0,len=val.length; i<len; i++){
							if ($(this).val()==val[i]) this.checked = true;
						}
					} else {
						if ($(this).val()==val) this.checked = true;
					}
				}
				if ($(this).attr('type')=='radio'){
					if ($(this).val()==val) this.checked = true;
				}
			break;

			case 'SELECT':
				var s = -1;
				for(i=0;i<this.options.length;i++) if (this.options[i].value==val) s = i;
				this.selectedIndex = s;
			break;
		}
	});
}
