function doValidation(){
	var check = 0;	
	$('form input[check], form textarea[check]').each(function() {
		if($(this).attr('check')!=''){
			var controle = $(this).attr('check').split(" ");
			for(i=0;i<controle.length;i++){
				if(controle[i]=='required'){ 
					var errors = checkRequired($(this).val());
					check += errors;
				}//if
				
				if(controle[i]=='email'){ 
					var errors = checkEmail($(this).val());
					check += errors;				
				}//if
				
				if(controle[i]=='integer'){ 
					var errors = checkInteger($(this).val());
					check += errors;						
				}//if
				
				if(controle[i]=='double'){ 
					var errors = checkDouble($(this).val());
					check += errors;						
				}//if
				
				if(controle[i]=='url'){ 
					var errors = checkUrl($(this).val());
					check += errors;						
				}//if
				
			}//for
				
			if(check>0){
				$(this).addClass('field_not_valid');
			} else {
				$(this).removeClass('field_not_valid');
			}
			
		}//if
	});
	if (check != 0) {
		$('#form_error_message').show();
	} else {
		$('#form_error_message').hide();
	}
	return check;
}//doValidation

function doValidationPerPage(page){
	var check = 0;
	$('#form_error_message').hide();
	$('span.field_error_message').hide();
	$(page+' input[check], '+page+' textarea[check]').each(function() {
		var innercheck = 0;
		if($(this).attr('check')!=''){
			var controle = $(this).attr('check').split(" ");
			for(i=0;i<controle.length;i++){
				if(controle[i]=='required'){ 
					var errors = checkRequired($(this).val());
					if (errors != 0) showError('required', $(this).attr('id'));
					check += errors;
					innercheck += errors;
				}//if
				
				if(controle[i]=='email'){ 
					var errors = checkEmail($(this).val());
					if (errors != 0) showError('email', $(this).attr('id'));
					check += errors;
					innercheck += errors;
				}//if
				
				if(controle[i]=='integer'){ 
					var errors = checkInteger($(this).val());
					if (errors != 0) showError('integer', $(this).attr('id'));
					check += errors;
					innercheck += errors;
				}//if
				
				if(controle[i]=='double'){ 
					var errors = checkDouble($(this).val());
					if (errors != 0) showError('double', $(this).attr('id'));
					check += errors;
					innercheck += errors;
				}//if
				
				if(controle[i]=='url'){ 
					var errors = checkUrl($(this).val());
					if (errors != 0) showError('url', $(this).attr('id'));
					check += errors;
					innercheck += errors;
				}//if
				
			}//for
				
			if(innercheck>0){
				$(this).addClass('field_not_valid');
			} else {
				$(this).removeClass('field_not_valid');
			}
			
		}//if
	});
	if (check != 0) {
		$('#form_error_message').show();
	}
	return check;
}//doValidationPerPage

function showError(check, id) {
	var id = id.split("_");
	id = id[(id.length-1)];
	$('span#'+id+'_'+check).css('display','block');
}

function checkRequired(value){
	if(jQuery.trim(value).length==0) return 1;
	else return 0;
}//checkRequired

function checkInteger(value){
	if(value!=''){
		var filter = /^[0-9]+$/;
		if(filter.test(value)==false) return 1;
		else return 0;
    }else{
    	return 0;
    }
}//checkInteger

function checkDouble(value){
	if(value!=''){
		var strValidChars = "0123456789.-";
		var strChar;
		for (i=0; i<value.length; i++){
			strChar = value.charAt(i);
			if (strValidChars.indexOf(strChar) == -1) return 1;
		}//for
	   return 0;
   }else{
		return 0;
   }//if
}//checkDouble

function checkEmail(value){
	if(value!=''){
		var filter  = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
		if(filter.test(value)==false) return 1;
		else return 0;
	}else{
		return 0;
	}
}//checkEmail

function checkUrl(value){ 
	if(value!=''){
		var filter = /(ftp|http|https):\/\/(\w+:{0,1}\w*@)?(\S+)(:[0-9]+)?(\/|\/([\w#!:.?+=&%@!\-\/]))?/;
		if(filter.test(value)==false) return 1; 
		else return 0; 
	}else{
		return 0;
	}    
}//checkUrl
