(function($) {

	globalVar="errorColor";
	globalVar="okColor";
	errorColor = "#990000";
	okColor = "#99CC00";
	validatenow = function(){
		$("form").submit(function(e) {
			var id = '#'+$(this).attr('id');
			var pid = $(this).attr('id');
			globalVar="valid";
			globalVar="rval";
			globalVar="radio";
			rval = 0;
			valid = 0;
			radio = "";
			$(id+" [validate]").each(function(){
				if($(this).get(0).tagName == 'INPUT' && $(this).attr('type') == 'checkbox' && $(this).attr('validate') == 'required'){
					
					var name = $(this).attr('name');
					var erMsg = $(this).attr('erMsg');
					if($(this).attr('checked') == false){
						$(this).siblings('label').css('color',errorColor);
						if($(this).siblings('label').parent('div').next('.vError').size() > 0){
							$(this).siblings('label').parent('div').next('.vError').fadeIn(200);
						} else {
							$(this).siblings('label').parent('div').after('<div class="vError">'+erMsg+'</div>');
						}
						$(this).bind('click', function() {
							if($(this).attr('checked') == true){
								$(this).siblings('label').css('color',okColor);
								$(this).siblings('label').parent('div').next('.vError').fadeOut(200);
							} else {
								$(this).siblings('label').css('color',errorColor);
								$(this).siblings('label').parent('div').next('.vError').fadeIn(200);
							}
						});
						valid++;
					}
					
				} else if(($(this).get(0).tagName == 'TEXTAREA' || $(this).get(0).tagName == 'INPUT') && $(this).attr('validate') == 'required'){
					
					var erMsg = $(this).attr('erMsg');
					var name = $(this).attr('name');
					if($(this).val() == ''){
						$(this).css('border-color',errorColor);
						if($(this).next('.vError').size() > 0){
							$(this).next('.vError').fadeIn(200);
						} else {
							$(this).after('<div class="vError">'+erMsg+'</div>');
						}
						$(this).bind('keyup', function() {
							if ($(this).val() != "") {
								$(this).css('border-color',okColor);
								$(this).next('.vError').fadeOut(200);
							} else {
								$(this).css('border-color',errorColor);
								$(this).next('.vError').fadeIn(200);
							}
						});
						valid++;
					}
					
				} else if($(this).get(0).tagName == 'INPUT' && $(this).attr('validate') == 'required nome'){
					
					var name = $(this).attr('name');
					var erMsg = $(this).attr('erMsg');
					var validemail = /^(.{2,})+ (.)/;
					if(!validemail.test($(this).val())){
						$(this).css('border-color',errorColor);
						if($(this).next('.vError').size() > 0){
							$(this).next('.vError').fadeIn(200);
						} else {
							$(this).after('<div class="vError">'+erMsg+'</div>');
						}
						$(this).bind('keyup', function() {
							if(validemail.test($(this).val())){
								$(this).css('border-color',okColor);
								$(this).next('.vError').fadeOut(200);
							} else {
								$(this).css('border-color',errorColor);
								$(this).next('.vError').fadeIn(200);
							}
						});
						valid++;
					}
					
				} else if($(this).get(0).tagName == 'INPUT' && $(this).attr('validate') == 'required email'){
					
					var name = $(this).attr('name');
					var erMsg = $(this).attr('erMsg');
					var validemail = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
					if(!validemail.test($(this).val())){
						$(this).css('border-color',errorColor);
						if($(this).next('.vError').size() > 0){
							$(this).next('.vError').fadeIn(200);
						} else {
							$(this).after('<div class="vError">'+erMsg+'</div>');
						}
						$(this).bind('keyup', function() {
							if(validemail.test($(this).val())){
								$(this).css('border-color',okColor);
								$(this).next('.vError').fadeOut(200);
							} else {
								$(this).css('border-color',errorColor);
								$(this).next('.vError').fadeIn(200);
							}
						});
						valid++;
					}
					
				} else if($(this).get(0).tagName == 'INPUT' && $(this).attr('validate') == 'required data'){
					
					var name = $(this).attr('name');
					var erMsg = $(this).attr('erMsg');
					var validdata = /^([0-9]{2})+\-([0-9]{2})+\-([0-9]{4})$/;
					var validdata2 = /^([0-9]{2})+\/([0-9]{2})+\/([0-9]{4})$/;
					if(!validdata.test($(this).val()) && !validdata2.test($(this).val())){
						$(this).css('border-color',errorColor);
						if($(this).next('.vError').size() > 0){
							$(this).next('.vError').fadeIn(200);
						} else {
							$(this).after('<div class="vError">'+erMsg+'</div>');
						}
						$(this).bind('keyup', function() {
							if(validdata.test($(this).val()) || validdata2.test($(this).val())){
								$(this).css('border-color',okColor);
								$(this).next('.vError').fadeOut(200);
							} else {
								$(this).css('border-color',errorColor);
								$(this).next('.vError').fadeIn(200);
							}
						});
						valid++;
					}
					
				} else if($(this).get(0).tagName == 'INPUT' && $(this).attr('validate') == 'required cep'){
					
					var name = $(this).attr('name');
					var erMsg = $(this).attr('erMsg');
					var validcep = /^([0-9]{5})+\-([0-9]{3})$/;
					if(!validcep.test($(this).val())){
						$(this).css('border-color',errorColor);
						if($(this).next('.vError').size() > 0){
							$(this).next('.vError').fadeIn(200);
						} else {
							$(this).after('<div class="vError">'+erMsg+'</div>');
						}
						$(this).bind('keyup', function() {
							if(validcep.test($(this).val())){
								$(this).css('border-color',okColor);
								$(this).next('.vError').fadeOut(200);
							} else {
								$(this).css('border-color',errorColor);
								$(this).next('.vError').fadeIn(200);
							}
						});
						valid++;
					}
					
				} else if($(this).get(0).tagName == 'INPUT' && ($(this).attr('validate') == 'required fone' || $(this).attr('validate') == 'optional fone')){
					if(($(this).attr('validate') == 'optional fone' && $(this).val() != "") || $(this).attr('validate') == 'required fone'){
						var name = $(this).attr('name');
						var erMsg = $(this).attr('erMsg');
						var validfone = /^([0-9\+\ ]{2,7})+ ([0-9]{4})+\-([0-9]{4,6})$/;
						if(!validfone.test($(this).val())){
							$(this).css('border-color',errorColor);
							if($(this).next('.vError').size() > 0){
								$(this).next('.vError').fadeIn(200);
							} else {
								$(this).after('<div class="vError">'+erMsg+'</div>');
							}
							$(this).bind('keyup', function() {
								if(validfone.test($(this).val())){
									$(this).css('border-color',okColor);
									$(this).next('.vError').fadeOut(200);
								} else if($(this).attr('validate') == 'optional fone' && $(this).val() == ""){
									$(this).css('border-color',okColor);
									$(this).next('.vError').fadeOut(200);
								} else {
									$(this).css('border-color',errorColor);
									$(this).next('.vError').fadeIn(200);
								}
							});
							valid++;
						}
					}
					
				} else if($(this).get(0).tagName == 'INPUT' && $(this).attr('validate') == 'required cpf'){
				
					var name = $(this).attr('name');
					var erMsg = $(this).attr('erMsg');
					var validcpf = /^([0-9]{3})+\.([0-9]{3})+\.([0-9]{3})+\-([0-9]{2})$/;
					if(!validcpf.test($(this).val())){
						$(this).css('border-color',errorColor);
						if($(this).next('.vError').size() > 0){
							$(this).next('.vError').fadeIn(200);
						} else {
							$(this).after('<div class="vError">'+erMsg+'</div>');
						}
						$(this).bind('keyup', function() {
							if(validcpf.test($(this).val())){
								$(this).css('border-color',okColor);
								$(this).next('.vError').fadeOut(200);
							} else {
								$(this).css('border-color',errorColor);
								$(this).next('.vError').fadeIn(200);
							}
						});
						valid++;
					}
					
				} else if($(this).get(0).tagName == 'INPUT' && $(this).attr('validate') == 'required R$'){				
				
					var name = $(this).attr('name');
					var erMsg = $(this).attr('erMsg');
					var validcR$ = /^\$?[1-9][0-9]{0,2}(.[0-9]{3})*(\,[0-9]{2}){1}$/;
					if(!validcR$.test($(this).val())){
						$(this).css('border-color',errorColor);
						if($(this).next('.vError').size() > 0){
							$(this).next('.vError').fadeIn(200);
						} else {
							$(this).after('<div class="vError">'+erMsg+'</div>');
						}
						$(this).bind('keyup', function() {
							if(validcR$.test($(this).val())){
								$(this).css('border-color',okColor);
								$(this).next('.vError').fadeOut(200);
							} else {
								$(this).css('border-color',errorColor);
								$(this).next('.vError').fadeIn(200);
							}
						});
						valid++;
					}
					
				} else if($(this).get(0).tagName == 'INPUT' && $(this).attr('equal') != '' && $(this).attr('validate') == 'required equal'){
					
					var name = $(this).attr('name');
					var erMsg = $(this).attr('erMsg');
					var eTo = "#"+$(this).attr('equal');
					if($(this).val() == '' || $(this).val() != $(eTo).val()){
						$(this).css('border-color',errorColor);
						if($(this).next('.vError').size() > 0){
							$(this).next('.vError').fadeIn(200);
						} else {
							$(this).after('<div class="vError">'+erMsg+'</div>');
						}
						$(this).bind('keyup', function() {
						
							if ($(this).val() != "" && $(this).val() == $(eTo).val()) {
								$(this).css('border-color',okColor);
								$(this).next('.vError').fadeOut(200);
							} else {
								$(this).css('border-color',errorColor);
								$(this).next('.vError').fadeIn(200);
							}
							
						});
						valid++;
					}
					
				} else if($(this).get(0).tagName == 'INPUT' && $(this).attr('min') != '' && ($(this).attr('validate') == 'required psw' || $(this).attr('validate') == 'required min')){
					
					var name = $(this).attr('name');
					var erMsg = $(this).attr('erMsg');
					if($(this).attr() == '' || $(this).val().length < $(this).attr('min')){
						$(this).css('border-color',errorColor);
						if($(this).next('.vError').size() > 0){
							$(this).next('.vError').fadeIn(200);
						} else {
							$(this).after('<div class="vError">'+erMsg+'</div>');
						}
						$(this).bind('keyup', function() {
						
							if ($(this).val().length >= $(this).attr('min')) {
								$(this).css('border-color',okColor);
								$(this).next('.vError').fadeOut(200);
							} else {
								$(this).css('border-color',errorColor);
								$(this).next('.vError').fadeIn(200);
							}
							
						});
						valid++;
					}
					
				} else if($(this).get(0).tagName == 'SELECT' && $(this).attr('validate') == 'required'){
					
					var name = $(this).attr('name');
					var erMsg = $(this).attr('erMsg');
					if($(this).val() == "" || $(this).val() == "notvalid" || $(this).val() <= 0){			
						$(this).css('border-color',errorColor);
						if($(this).next('.vError').size() > 0){
							$(this).next('.vError').fadeIn(200);
						} else {
							$(this).after('<div class="vError">'+erMsg+'</div>');
						}
						$(this).bind('change', function() {
						
							if ($(this).val() != "" && $(this).val() != "notvalid" && $(this).val() > 0) {
								$(this).css('border-color',okColor);
								$(this).next('.vError').fadeOut(200);
							} else {
								$(this).css('border-color',errorColor);
								$(this).next('.vError').fadeIn(200);
							}
							
						});
						valid++;
					}
					
				} else if($(this).get(0).tagName == 'INPUT' && $(this).attr('type') == 'radio' && $(this).attr('validate') == 'required radio' && $(this).attr('name') != radio){
					
					var name = $(this).attr('name');
					var erMsg = $(this).attr('erMsg');
					var name = $(this).attr('name');
					radio = name;
					if($('[type=radio][name='+name+']:checked').size() == 0){
						$('[type=radio][name='+name+']').parent('label').css('color',errorColor);
						if($(this).next('.vError').size() > 0){
							$('[type=radio][name='+name+']:last').parent('label').parent('div').next('.vError').fadeIn(200);
						} else {
							$('[type=radio][name='+name+']:last').parent('label').parent('div').after('<div class="vError">'+erMsg+'</div>');
						}
						$('[type=radio][name='+name+']').bind('click', function(){
							if($('[type=radio][name='+name+']:checked').size() > 0){
								$('[type=radio][name='+name+']').parent('label').css('color',okColor);
								$('[type=radio][name='+name+']:last').parent('label').parent('div').next('.vError').fadeOut(200);
							} else {
								$('[type=radio][name='+name+']').parent('label').css('color',errorColor);
								$('[type=radio][name='+name+']:last').parent('label').parent('div').next('.vError').fadeIn(200);
							}
						})
						valid++;
					}
					
				}
			})
			if(valid == 0){
				return true;
			} else {
				return false;
			}
		})
	};
	
})(jQuery);
