$(document).ready(function() {
    $('form[name="volunteer_form"]').validate({
        errorElement: "li",
        errorLabelContainer:'.error_container',
        validClass: 'valid',
        focusInvalid: true,
        rules: {
            name: {
                required: true
            },
            phone_no: {
                required: true
            },
            email: {
                //required: true,
                email: true
            },
            blk: {
               required: true 
            },
            unit_no: {
                required: true
            }
        },
        messages: {
            name: {
                required: 'Please enter Volunteer Name!'
            },
            phone_no: {
                required: 'Please enter Contact No!'
            },
            email: {
                //required: 'Please enter Email!',
                email: 'Please enter Valid Email!'
            },
            blk: {
               required: 'Please enter BLK!' 
            },
            unit_no: {
                required: 'Please enter Unit No!'
            }
        }
      });
      
      $('[type="file"]').on('change', function(){
            var prevImg = $(this).attr('data-value') || "";
            if(prevImg != '') {
                $(this).after('<input type="hidden" name="prev-image" value="'+ prevImg +'">');
            }
      });
      $('.img-preview .rm-btn').on('click', function(){
          var $this = $(this);
          if( window.confirm("Are you sure and want to remove..?") ){
              // Ok
              var $parent = $this.parent().closest('.form-group') //img-preview
              ,   $input = $parent.find('[type="file"]')
              ,   value  = $input.attr('data-value');  
              
              $this.after('<input type="hidden" name="rm-image" value="'+ value +'">');
              
              $this.remove();
              $input.val('');
              $parent.find('img').remove();
          };
          
          return false;
          
      })
});

function goBack() {
    window.history.back();
}