var validate =
{
	mode:2,
	all: false,
	msg:{	'req':"is required.\nCan not be empty.",
			'int':"should be integer.\nExample: 5 or 10 or -3.",
			'num':"should be numeric.\nExample: 3 or 5.20 or -5.",
			'pve':"should be a positive value.\nExample: 10 or 3.20 or 5.",
			'eml':"should be a valid email.\nExample: \"info@example.com\".",
			'img':"should be a valid image file type.\nExample: \"image.jpg\", \"image.gif\" or \"image.png\"."},
	ret:true,
	errmsg:'#errmsg',
	check: function()
	{
		validate.ret = true;
		if(validate.mode==0 || validate.mode==2 || validate.mode==4) $('span.errmsg').remove();
		
		$('.req').each(function(){
			
			$(this).removeClass('error');
			if($(this).val()=='')
			{
				validate.showmsg(this,validate.msg.req);
				validate.ret = false;
				if(!validate.all) return validate.ret;
			}
		})

		if(validate.ret) $('.int').each(function(){
			return validate.evalreg(this,(/^\-?\d+$/),validate.msg.int);
		})

		if(validate.ret) $('.pve').each(function(){
			return validate.evalreg(this,(/^\+?\d+([\.]\d+)?$/),validate.msg.pve);
		})

		if(validate.ret) $('.num').each(function(){
			return validate.evalreg(this,(/^\-?\d+([\.]\d+)?$/),validate.msg.num);
		})

		if(validate.ret) $('.eml').each(function(){
			return validate.evalreg(this,(/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,5})+$/),validate.msg.eml);
		})

		if(validate.ret) $('.img').each(function(){
			return validate.evalreg(this,(/^(.*)jpg|jpeg|gif|png$/i),validate.msg.img);
		})

		return validate.ret;	
	},
	evalreg: function(me,regexp,msg)
	{
		$(me).removeClass('error');
		var val = $(me).val();
		if(val != '' && !regexp.test(val))
		{
			validate.showmsg(me,msg);
			validate.ret = false;
			if(!validate.all) return validate.ret;
		}		
	},
	showmsg: function(me,msg)
	{
		$(me).addClass('error');
		if(validate.mode==0 || validate.mode==1) alert('"'+$("label[@for='"+me.id+"']").html()+"\" "+msg);
		if(validate.mode==0 || validate.mode==2) $(me).after('<span class="errmsg">'+msg+'</span>');
		if(validate.mode==3) $(validate.errmsg).html('"'+$("label[@for='"+me.id+"']:first").html()+"\" "+msg);
		if(validate.mode==4) $("label[@for='"+me.id+"']:first").after('<span class="errmsg">'+msg+'</span>');

		if(validate.ret) me.focus();	
	}
}