jQuery(document).on('submit','.fab-sugar-lead-form', function(){
    var $this = jQuery(this);
    jQuery(".fab-sugar-lead-form :input").attr("readonly", true);
    $this.find('.button-submit-form').attr('disabled',true);
    $this.find('.button-submit-form').html('<i class="fa fa-spin fa-spinner"></i>');
});

(function ($) {

	// check for "required" input support with modernizr
	if (Modernizr.input.required) {

		// do something else

	} else {

		// parse through each required input
		$('form').find('input[required]').each(function () {

			// add a class to each required field with "required" & the input type
			// using the normal "getAttribute" method because jQuery's attr always returns "text"
			$(this).attr('class', 'required ' + this.getAttribute('type')).removeAttr('required');

		});

		// call jQuery validate plugin on each form
		$('form').each(function () {
			$(this).validate();
		});

	} // if required supported

}(jQuery));

(function ($) {
    
    $('.ris2radio').attr('checked', false);
    $('.conditional_block1').attr('checked', false);
    $('.conditional_block1').attr('required', true);
    $(document).on('change','.conditional_block1', function(){
        if($('.conditional_block1:checked').val() == 'si'){
            $('.conditional_block2').show();
            $('.ris2radio').attr('required', true);
        }
        else {
            $('.conditional_block2').hide();
            $('.ris2radio').attr('checked', false);
            $('.ris2radio').attr('required', false);
        }
    }); 
    
}(jQuery));

var FabSugarLead = new function() {
    
    this.init = function() {
        this.plugin_url = FabSugarLeadVariables.pluginUrl;        
        this.crm_url = FabSugarLeadVariables.crmUrl;        
    };
    this.enableInputIfRadioSelected = function(radioclass,valuetocheck,elementtoenable,required) {
        
        jQuery(document).on('change ready', radioclass, function(){
            var $this = jQuery(this);
            if($this.val() == valuetocheck){
                jQuery(elementtoenable).attr('disabled', false);
                if(required)
                    jQuery(elementtoenable).attr('required', true);
            }
            else {
                jQuery(elementtoenable).attr('disabled', true);
                if(required)
                    jQuery(elementtoenable).attr('required', false);
                jQuery(elementtoenable).val('');
            }
        });        
    },
    this.getRegioni = function(){
        jQuery.post(this.crm_url+'/bc_location.php',{type : 'regione'},function(data){
            jQuery('.select_regione').html(data);
        });
    },
    this.getProvincia = function(id_regione){
        jQuery.post(this.crm_url+'/bc_location.php',{type : 'provincia', id : id_regione},function(data){
            if(data != ''){
                jQuery('.select_provincia').attr('disabled',true);
                jQuery('.select_provincia').html('<option>Attendere prego</option>');
                jQuery('.select_provincia').html(data);
                jQuery('.select_provincia').attr('disabled',false);
            }
            else {
                FabSugarLead.resetBlock(jQuery('.select_provincia'),'<option>Seleziona una regione</option>');
                FabSugarLead.resetBlock(jQuery('.select_comune'),'<option>Seleziona una provincia</option>');
            }
        });
    }
    this.getComune = function(id_provincia){
        jQuery.post(this.crm_url+'/bc_location.php',{type : 'comune', id : id_provincia},function(data){
            if(data != ''){
                jQuery('.select_comune').attr('disabled',true);
                jQuery('.select_comune').html('<option>Attendere prego</option>');
                jQuery('.select_comune').html(data);
                jQuery('.select_comune').attr('disabled',false);
            }
            else {
                FabSugarLead.resetBlock(jQuery('.select_comune'),'<option>Seleziona una provincia</option>');
            }
        });
    }
    this.resetBlock = function(block,msg){
        block.attr('disabled',true);
        block.html(msg);
    }
}

jQuery(document).ready(function(){
    FabSugarLead.init();    
    FabSugarLead.enableInputIfRadioSelected('.titolo_studio','Laurea','.titolo_studio_specializzazione',true);
    FabSugarLead.enableInputIfRadioSelected('.durata_titolo_studio','Altro','.durata_titolo_studio_altro',true)
    
    jQuery('.select_regione').trigger('change');
    jQuery('.select_provincia').trigger('change');
});
jQuery(document).on('change','.select_regione',function(){    
    var $this = jQuery(this);
    var id_regione = $this.find(':selected').data('id');
    FabSugarLead.resetBlock(jQuery('.select_provincia'),'<option>Seleziona una regione</option>');
    FabSugarLead.resetBlock(jQuery('.select_comune'),'<option>Seleziona una provincia</option>');
    FabSugarLead.getProvincia(id_regione);
});
jQuery(document).on('change','.select_provincia',function(){
    var $this = jQuery(this);
    var id_comune = $this.find(':selected').data('id');
    FabSugarLead.resetBlock(jQuery('.select_comune'),'<option>Seleziona una provincia</option>');
    FabSugarLead.getComune(id_comune);
});
