(function( $ ) {
	
	jQuery(document).ready(function(){
		var $ = jQuery;
		var is_mobile = false;
		if( navigator.userAgent.match(/Android/i)
		 || navigator.userAgent.match(/webOS/i)
		 || navigator.userAgent.match(/iPhone/i)
		 || navigator.userAgent.match(/iPad/i)
		 || navigator.userAgent.match(/iPod/i)
		 || navigator.userAgent.match(/BlackBerry/i)
		 || navigator.userAgent.match(/Windows Phone/i)
		){
			is_mobile = true;	
		}
		if(!is_mobile && $('.planso-form-builder').length > 0){
			
			$('.planso-form-builder input[type="date"],.planso-form-builder input[type="datetime"],.planso-form-builder input[type="time"]').each(function(){
				 $(this).closest('.form-group .input-group').addClass('date');
			});
			if( $('.planso-form-builder input[type="date"]').length > 0){
				$('.planso-form-builder input[type="date"]').each(function(){
					$(this).addClass('planso_datepicker').get(0).type = 'text';
				});
				$('.planso-form-builder .planso_datepicker').each(function(){
					if($(this).attr('readonly') == 'readonly'){
						
					}else{
						if( $(this).closest('.form-group .input-group').length > 0){
							var me = $(this);//.closest('.form-group .input-group')
						} else {
							var me = $(this);
						}
						
						if(me.closest('form').data('datepicker') == 'bootstrap-datetimepicker'){
							me.datetimepicker({
			          locale: me.closest('form').data('locale'),
			          format:me.closest('form').data('moment_date_format'),
			          useCurrent:false,
			          toolbarPlacement:'top',
			          showTodayButton:true,
			          showClear:true,
			          showClose:true,
			          icons: {
		              time: 'fa fa-clock-o',
			            date: 'fa fa-calendar',
			            up: 'fa fa-arrow-up',
			            down: 'fa fa-arrow-down',
			            previous: 'fa fa-arrow-left',
			            next: 'fa fa-arrow-right',
			            today: 'fa fa-calendar',
			            clear: 'fa fa-trash',
			            close: 'fa fa-times'
		            }
				      });
				    } else if(me.closest('form').data('datepicker') == 'bootstrap-datepicker'){
				    	me.bootstrapDP({
								format:me.closest('form').data('eternicode_date_format')
							});
				    } else if(me.closest('form').data('datepicker') == 'bootstrap-datepicker-eternicode'){
				    	
				    	me.bootstrapDP({
								language: me.closest('form').data('locale'),
								format:me.closest('form').data('eternicode_date_format')
							});
							
				    } else if(me.closest('form').data('datepicker') == 'jquery-ui-datepicker'){
				    	me.datepicker({dateFormat:me.closest('form').data('jqueryui_date_format')});
				    }
				  } 
		    });
			}
			if( $('.planso-form-builder input[type="datetime"]').length > 0){
				$('.planso-form-builder input[type="datetime"]').attr('type','text').each(function(){
					if($(this).attr('readonly') == 'readonly'){
						
					}else{
						if( $(this).closest('.form-group .input-group').length > 0){
							var me = $(this);//.closest('.form-group .input-group')
						} else {
							var me = $(this);
						}
						if(me.closest('form').data('datepicker') == 'bootstrap-datetimepicker'){
							me.datetimepicker({
			          locale: me.closest('form').data('locale'),
			          useCurrent:false,
			          showTodayButton:true,
			          sideBySide: true,
			          showClear:true,
			          showClose:true,
			          toolbarPlacement:'top',
			          icons: {
		              time: 'fa fa-clock-o',
			            date: 'fa fa-calendar',
			            up: 'fa fa-arrow-up',
			            down: 'fa fa-arrow-down',
			            previous: 'fa fa-arrow-left',
			            next: 'fa fa-arrow-right',
			            today: 'fa fa-calendar',
			            clear: 'fa fa-trash',
			            close:'fa fa-times'
		            }
		          });
		        } else if(me.closest('form').data('datepicker') == 'bootstrap-datepicker'){
				    	/*
				    	me.datepicker({
								language: me.closest('form').data('locale')
							});
							*/
							me.datepicker();
				    } else if(me.closest('form').data('datepicker') == 'jquery-ui-datepicker'){
				    	me.datepicker();
				    }
				  }
	      });
			}
			if( $('.planso-form-builder input[type="time"]').length > 0){
				$('.planso-form-builder input[type="time"]').attr('type','text').each(function(){
					if($(this).attr('readonly') == 'readonly'){
						
					}else{
						if( $(this).closest('.form-group .input-group').length > 0){
							var me = $(this);//.closest('.form-group .input-group')
						} else {
							var me = $(this);
						}
						
						if(me.closest('form').data('datepicker') == 'bootstrap-datetimepicker'){
							me.datetimepicker({
			          locale: me.closest('form').data('locale'),
			          showTodayButton:true,
			          showClear:true,
			          toolbarPlacement:'top',
			          showClose:true,
			          format:'HH:mm',
			          useCurrent:false,
			          icons: {
		              time: 'fa fa-clock-o',
			            date: 'fa fa-calendar',
			            up: 'fa fa-arrow-up',
			            down: 'fa fa-arrow-down',
			            previous: 'fa fa-arrow-left',
			            next: 'fa fa-arrow-right',
			            today: 'fa fa-clock-o',
			            clear: 'fa fa-trash',
			            close: 'fa fa-times'
		            }
		          });
		        } else if(me.closest('form').data('datepicker') == 'bootstrap-datepicker'){
				    	
				    } else if(me.closest('form').data('datepicker') == 'jquery-ui-datepicker'){
				    	
				    }
				  }
	      });
			}
			
			
			
		} else {
			/* IS MOBILE */
			if( $('.planso-form-builder input[type="date"]').length > 0){
				$('.planso-form-builder input[type="date"]').each(function(){
					if( $(this).prop('readonly') ){
						$(this).get(0).type = 'text';
					}
				});
			}
			if( $('.planso-form-builder input[type="datetime"]').length > 0){
				$('.planso-form-builder input[type="datetime"]').each(function(){
					if( $(this).prop('readonly') ){
						$(this).get(0).type = 'text';
					}
				});
			}
			if( $('.planso-form-builder input[type="time"]').length > 0){
				$('.planso-form-builder input[type="time"]').each(function(){
					if( $(this).prop('readonly') ){
						$(this).get(0).type = 'text';
					}
				});
			}
		}
		
		
		$('.planso-form-builder').each(function(){
			
			if($(this).hasClass('pro')){
				//current_form.find('button[type="submit"]').attr("disabled", false);
			}else{
				var current_form = $(this);
				current_form.find(':input[data-required="required"]').removeAttr('required');
				current_form.submit(function( event ){
					var has_errors = false;
					current_form.find(':input[data-required="required"]').each(function(){
						var in_has_errors = false;
						if($(this).is('input[type="checkbox"]')){
							if($(this).closest('.form-group').find( 'input[type="checkbox"]:checked').length > 0){
								$(this).removeClass('has-error').closest('.form-group').removeClass('has-error');
							}else{
								$(this).addClass('has-error').closest('.form-group').addClass('has-error').animate({ 'opacity': 0.1 }, {duration: 100,complete: function() { $(this).delay(100).animate({ 'opacity': 1 }, {duration: 100});}});
								has_errors = true;in_has_errors = true;
							}
						}else if($(this).is('input[type="radio"]')){
							if($(this).closest('.form-group').find( 'input[type="radio"]:checked').length > 0){
								$(this).removeClass('has-error').closest('.form-group').removeClass('has-error');
							}else{
								$(this).addClass('has-error').closest('.form-group').addClass('has-error').animate({ 'opacity': 0.1 }, {duration: 100,complete: function() { $(this).delay(100).animate({ 'opacity': 1 }, {duration: 100});}});
								has_errors = true;
								in_has_errors = true;
							}	
						}else if($(this).is('input[type="email"]')){
							if($(this).val()== '' || $(this).val() == null || ($(this).val().indexOf('@')==-1 || $(this).val().indexOf('.')==-1)){
								$(this).addClass('has-error').closest('.form-group').addClass('has-error').animate({ 'opacity': 0.1 }, {duration: 100,complete: function() { $(this).delay(100).animate({ 'opacity': 1 }, {duration: 100});}});
								has_errors = true;
								in_has_errors = true;
							}else{
								$(this).removeClass('has-error').closest('.form-group').removeClass('has-error');
							}
						}else{
							if($(this).val()== '' || $(this).val() == null){
								$(this).addClass('has-error').closest('.form-group').addClass('has-error').animate({ 'opacity': 0.1 }, {duration: 100,complete: function() { $(this).delay(100).animate({ 'opacity': 1 }, {duration: 100});}});
								has_errors = true;
								in_has_errors = true;
							}else{
								$(this).removeClass('has-error').closest('.form-group').removeClass('has-error');
							}
						}
					});
					
					if(has_errors===true){
						event.preventDefault();
						current_form.find(':input.has-error').first().focus();
						return false;
					}
					current_form.find(':input[disabled="disabled"]').each(function(){
						$(this).removeAttr('disabled');
					});
					current_form.find('button[type="submit"]').attr("disabled", true);
					return true;
					
				});
			}
		});
		
		
	});
}( jQuery ));