function onError(input)
{
	$(input).css('border','1px red solid');
}

function isEmpty(val)
{
	if ( $.trim( val ) == '' ){ return true; }
	else { return false; }
}

function validForm(form){
	$('.req', form).css('border','');
	
	if ( $('.is-date:visible').size() != 0 ) $('.is-date:visible', form).css('border','');
	var error = 0;
	
	$('.req:visible', form).each( function(){ if ( isEmpty( $(this).val() ) ){error++; onError(this); } });
	
	$('.req-string:visible', form).each( function(){ if ( isEmpty($(this).val()) ) {error++; onError(this); }});
		
	//Vérification
	$('.is-mail:visible', form).each( function(){
		if ($.trim( $(this).val() ) != '' )
		{
			var mail = $(this).val();
			var place = mail.indexOf("@",1);
			var point = mail.indexOf(".",place+1);
			if ((place == -1)||(mail.length < 2)||( point < 1)){error++; onError(this);}
		}
	});

	$('.is-int:visible', form).each( function(){if ($.trim( $(this).val() ) != '' ){
			if ( parseInt($(this).val()) == 'NAN'){error++; onError(this);}
	}});
	
	$('.is-postCode:visible', form).each( function(){if ($.trim( $(this).val() ) != '' ){
			if ( parseInt($(this).val()) == 'NAN' || $(this).val().length != 5){error++; onError(this);}
	}});

	$('.is-date:visible', form).each( function(){
		var date = $(this).val();
		if ( date != '' )
		{
			var format = /^[0-9]{4}\-(0[1-9]|1[012])\-(0[1-9]|[12][0-9]|3[01])$/;			
			if(!format.test(date)){error++; onError(this);}
		}
	});
	
	$('.is-dateTime:visible', form).each( function(){
		var date = $(this).val();
		if ( date != '' )
		{
			var format = /^[0-9]{4}\-(0[1-9]|1[012])\-(0[1-9]|[12][0-9]|3[01]) [0-9]{2}\:[0-9]{2}$/;			
			if(!format.test(date)){error++; onError(this);}
		}
	});

	$('.is-phone:visible', form).each( function(){
		var num = $(this).val();
		if(num.length != 10) {error++; onError(this);}
		// var regex = new RegExp(/^0[1-9]{1}[0-9]{8}/gi);
		// if(! regex.test(num) ) {error++; onError(this);}
	})
	
	$('.is-time:visible', form).each( function(){
		var date = $(this).val();
		if ( date != '' )
		{
			var format = /^[0-9]{2}\:[0-9]{2}$/;			
			if(!format.test(date)){error++; onError(this);}
		}
	});
	
	$('.req-password-with-confirm:visible', form).each( function(){
		if ( $.trim( $(this).val() ) == '' || $(this).val() != $('.req-password-confirm:visible', form).val() )
		{
			error++; 
			onError(this);
			onError($('.req-password-confirm:visible', form));
		}
	})

	$('.is-not-zero:visible', form).each( function(){
		if( $(this).val() == 0 ){error++; onError(this);}
	});
	
	if(error == 0) {return true;}else{return false;}
}


function validInput(input){
	$(input).css('border','inherit');
	
	var value = $(input).val();
	
	if( $(input).hasClass('req') && isEmpty(value) ){
		onError(input);
	}

	if( $(input).hasClass('is-mail') && !isEmpty(value) ){
		var place = value.indexOf("@",1);
		var point = value.indexOf(".",place+1);
		if ((place == -1)||(value.length < 2)||( point < 1)){
			onError(input);
		}
	}

	if( $(input).hasClass('is-int') && !isEmpty(value) ){
		if ( parseInt(value) == 'NAN'){
			onError(input);
		}
	}

	
	if( $(input).hasClass('is-postCode') && !isEmpty(value) ){
		if ( parseInt(value) == 'NAN' || value.length != 5 ){
			onError(input);
		}
	}

	if( $(input).hasClass('is-date') && !isEmpty(value) ){
		var format = /^[0-9]{4}\-(0[1-9]|1[012])\-(0[1-9]|[12][0-9]|3[01])$/;			
		if(!format.test(value)){
			onError(input);
		}
	}
	
	if( $(input).hasClass('is-dateTime') && !isEmpty(value) ){
		var format = /^[0-9]{4}\-(0[1-9]|1[012])\-(0[1-9]|[12][0-9]|3[01]) [0-9]{2}\:[0-9]{2}$/;						
		if(!format.test(value)){
			onError(input);
		}
	}

	if( $(input).hasClass('is-phone') && !isEmpty(value) ){
		if(value.length != 10) {
			onError(input);
		}
	}

	if( $(input).hasClass('is-time') && !isEmpty(value) ){
		var format = /^[0-9]{2}\:[0-9]{2}$/;	
		if(!format.test(date)){
			onError(input);
		}
	}

	if( $(input).hasClass('is-time') && !isEmpty(value) ){
		if( value == 0 ){
			onError(input);
		}
	}
}

function function_exists (function_name) {
	if (typeof function_name === 'string') {
		function_name = this.window[function_name];
	}
	return typeof function_name === 'function';
}
