var jQueryDM = jQuery;
jQueryDM('window').ready(function(){
  
dap_member_profile_update_event_click();

jQueryDM('#dap_member_profile_frontend_shortcode input[name="dap_opted_out_checkbox"]').on('click',function(){
	dapMemberDoWarning(document.dap_update_profile_form);
});
dap_member_gdpr_operation();

	jQuery(document).on('change', '.maxNumFilesSelect' ,function(){
		var num = jQuery(this).val();
		jQuery('.uploadFilesInputOuter').find('.file-upload-wrapper').hide();
		
		for (i = 1; i <= num; i++) {
			jQuery('.uploadFilesInputOuter').find('.formBuilderFile'+i).show();	
		}
	});

	jQuery(document).on('change', '.file_upload' ,function(){
		var num =  jQuery(this).data("id");   
		var data =  jQuery(this).prop("files")[0];   
		var ext = data.name.split('.').pop().toLowerCase();
		var  size = data.size;
		
		var maxSize = parseInt(1000000) * parseInt(max_upload_size);
		
		var extensionErr = '';
		var sizeErr = '';
		
		if(typeof allowed_types_of_file != undefined && allowed_types_of_file != ''){
			var newFileTypes = allowed_types_of_file.split('|');	
			if(jQuery.inArray( ext,  newFileTypes) < 0){
				extensionErr = allowed_file_upload_types;		
			}
		}else{
			extensionErr = allowed_file_upload_types;	
		}
		
		if(extensionErr != ''){
			alert(extensionErr);
			jQuery(this).val('');
			jQuery('.uploadedFileName'+num).val('');
			jQuery('.uploadedFileName'+num).hide();
			return false;
		}
		
		if(size > maxSize){
			alert(file_upload_max_size);
			jQuery(this).val('');
			jQuery('.uploadedFileName'+num).val('');
			jQuery('.uploadedFileName'+num).hide();
			return false;
		}
		
		var btnName = jQuery(this).attr('name');
		jQuery('.uploadedFileName'+num).html('<span>Uploaded File: </span>'+data.name);
		jQuery('.uploadedFileName'+num).show();
		//uploadedFileName
		
		jQuery('.'+btnName).addClass('has-preview');
			

	});


});

function dap_member_gdpr_operation(){
	p_selector_gdpr = '#dap_member_profile_outer';
	allowgdpr = jQueryDM(p_selector_gdpr+' input[name="allowgdpr"]').val();
	if(allowgdpr == 'Y'){
		user_consent = jQueryDM(p_selector_gdpr+' input[name="u_gdpr_consent"]').val();
		if(user_consent == 'GDPR_CONSENT_YES'){
			jQuery(p_selector_gdpr).find('.gdpr_checkbox').attr('checked', true);
			 
		 }else{
			jQuery(p_selector_gdpr).find('.gdpr_checkbox').attr('checked', false);
			 
		 }

		jQuery(p_selector_gdpr+' .gdpr_checkbox').change(function() {
			current_date = jQueryDM(p_selector_gdpr+' input[name="current_date"]').val();
		  if (this.checked == true) {
			  jQuery('#gdpr_consent').val('GDPR_CONSENT_YES');
			  jQuery('#gdpr_compliance_date').val(current_date);
		  }else{
			 jQuery('#gdpr_consent').val('GDPR_CONSENT_NO');
			  jQuery('#gdpr_compliance_date').val(current_date);
		  }
		});
	}
}
function dap_member_profile_update_event_click(){
	
	jQueryDM('.dap_member_profile_update_btn').on('click',function(){
		
		
		// email validation 
		// password validation 
		var p_selector = "#dap_member_profile_frontend_shortcode";
		var allowgdpr = jQueryDM(p_selector).find('input[name="allowgdpr"]').val();
		var form = document.dap_update_profile_form;
		var okbuttontext = 'Yes';
		var cancelbuttontext = 'cancel';
		var current_date = jQueryDM(p_selector_gdpr+' input[name="current_date"]').val();
		var gdpr_check =  jQueryDM(p_selector).find('.gdpr_checkbox').prop('checked');
		var gdprconfirmation = jQueryDM(p_selector).find('input[name="gdpr_msg_section"]').val();
        var allowgdpr = jQueryDM(p_selector_gdpr+' input[name="allowgdpr"]').val();
		jQuery('<input />').attr('type', 'hidden').attr('name', "dap_profile_id").attr('value', dap_profile_id).appendTo(form);
		jQuery('<input />').attr('type', 'hidden').attr('name', "site_url").attr('value', site_url).appendTo(form);
		jQuery('<input />').attr('type', 'hidden').attr('name', "base_path").attr('value', base_path).appendTo(form);
			
		if (allowgdpr == 'N') {
			jQuery('#gdpr_consent').val('GDPR_CONSENT_NO');
			if(jQuery('.show_profile_gdpr_section').hasClass('d-none')){
				jQuery('#gdpr_consent').val('NOT_GDPR_USER');
			}
			
			jQuery('#gdpr_compliance_date').val(current_date);
			dap_member_profile_update(form); 
          }else{
			  
             if (gdpr_check != true) {
				 
					Swal.fire({
						title: '',
						text: gdprconfirmation,
						type: 'warning',
						showCancelButton: true,
						confirmButtonColor: '#3085d6',
						cancelButtonColor: '#d33',
						confirmButtonText: 'Yes'
					}).then((result) => {
						if (result.value) {
							setTimeout(function() {
									jQuery('#gdpr_consent').val('GDPR_CONSENT_NO');
									//jQuery('#gdpr_compliance_date').val(current_date);
									dap_member_profile_update(form);
								}, 'slow');
						}else{
							setTimeout(function() {
								jQuery('#gdpr_consent').val('GDPR_CONSENT_YES');
								jQuery('#gdpr_compliance_date').val(current_date);
								dap_member_profile_update(form);
							}, 'slow');
							
						}
					});
				 
				
				 
              
                  return false;
            }else{
            	setTimeout(function() {
					jQuery('#gdpr_consent').val('GDPR_CONSENT_YES');
					jQuery('#gdpr_compliance_date').val(current_date);
					dap_member_profile_update(form);
				}, 'slow');
            }
          }
          dap_member_profile_update(form);
			 
			 
	
		
		
	});
}


function dap_member_profile_update(form){
	
	jQuery('input[type="hidden"][name^="mce"]').remove();
	var u_opted_out;
	if(form.dap_opted_out_checkbox) {
		if(form.dap_opted_out_checkbox.checked) {
			u_opted_out = "N";
		} else {
			u_opted_out = "Y";
		}
		
		form.u_opted_out.value = u_opted_out;
	}
	
	
	if( form.u_first_name && (form.u_first_name.value == "") ){
		var msg = jQuery('input[name="dap_first_name_error"]').val();
		if(msg == ''){
			msg = "Please enter first name.";
		}
		dap_swal_fire_msg(msg);
		return false; 
	}
	
	if( form.u_email && !dapMemberProfileValidateEmail(form.u_email.value) ){
		var msg = jQuery('input[name="dap_invalid_email"]').val();
		if(msg == ''){
			msg = "Sorry, invalid email id: May not contain blank spaces or special characters";
		}
		
		dap_swal_fire_msg(msg);
		return false; 
		
	}
	
	if(jQuery('.show_profile_change_password_section').css('display') !="none"){		
		if(form.u_password && (form.u_password.value.length > 20)){
			var msg = jQuery('input[name="dap_password_leangth_error"]').val();
			if(msg == ''){
				msg = "Sorry, password cannot be more than 20 characters.";
			}
			dap_swal_fire_msg(msg);
			return false;	
		}
			
		if(form.u_password && (form.u_password.value != "") && !dapMemberProfileValidatePassword(form.u_password.value)) {
			var msg = jQuery('input[name="dap_password_special_chat_error"]').val();
			if(msg == ''){
				msg = "Sorry, only allowed characters are abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789@%*\/\"<=>_'|#!&+`$^?:,(){}[]~-.";
			}
			dap_swal_fire_msg(msg);
			return false;
			
		}
		
		if(form.u_password && (form.u_password.value != "") && (form.u_password.value != form.password_repeat.value) ) {
			var msg = jQuery('input[name="dap_password_repeat_error"]').val();
			if(msg == ''){
				msg = "'Password' doesn't match 'Repeat Password'. Please try again...";
			}
			dap_swal_fire_msg(msg);
			return false;
			
			
		} 
	}
	
	for(i=0; i < form.elements.length; i++) {
		//document.FormName.elements[i].getAttribute("required")
		if( (form.elements[i].getAttribute("required") == "Y") && (form.elements[i].value == "") ){
			msg = "Sorry, " + form.elements[i].getAttribute("realname") + " is a required field";
			dap_swal_fire_msg(msg);
			return false;
			
		}
		//var first_name = document.getElementById('u_first_name');
		//alert(first_name.getAttribute("required"));
	}
	
	jQueryDM('.dap_member_profile_update_btn').text('Please Wait')
    dap_member_show_loader();
	
	form.action = '/dap/userProfileSubmit.php';
	form.submit();
}

function dap_member_opted_click(){
	
	
}

function dapMemberProfileValidatePassword(password) {
 
	for(j=0; j<password.length; j++) {
	  if(parseInt(password[j])) continue;
	   var pattern = /^[a-zA-Z_0-9@\#"<=>_\$\^%()*!&+;`\-[]\\\',\.\/\{\}\|\:\? ]+$/;
	  if (pattern.test(password[j])) continue;
 
	} //end for

	return true;
}


function dapMemberProfileValidateEmail(email) {
    
     var emailReg = /^([\w-\.]+@([\w-]+\.)+[\w-]{2,4})?$/;
	 return emailReg.test( email );
  /*
	var email = trim(email); // value of field with whitespace trimmed off
	var emailFilter = /^[^@]+@[^@.]+\.[^@]*\w\w$/ ;
	var illegalChars= /[\(\)\<\>\,\;\:\\\"\[\]]/ ;

	if (email == "") {
		return false;
	} else if (!emailFilter.test(email)) { //test for valid email format
		return false;
	} else if (email.match(illegalChars)) { // test for illegal characters
		return false;
	}

	return true;*/
	
}



function dapMemberDoWarning(form) {
	if(form.dap_opted_out_checkbox.checked) return;
	
	var msg = jQuery('input[name="dap_opted_error"]').val();
	
	if(msg == ''){
		msg = 'WARNING: If you uncheck this box, you will NOT receive product and account related emails. Do you still wish to unsubscribe? (NOT RECOMMENDED)';
	}
	
		Swal.fire({
			title: '',
			text: msg,
			type: 'warning',
			showCancelButton: true,
			confirmButtonColor: '#3085d6',
			cancelButtonColor: '#d33',
			confirmButtonText: 'Yes'
			}).then((result) => {
		if (result.value) {
				return ;
			}else{
				form.dap_opted_out_checkbox.checked = true;
				return ;
			}	
			
		});
	
}


function dap_swal_fire_msg($msg){
	Swal.fire({
		title: $msg,
		animation: false,
		customClass: {
			popup: 'dap_member_swal2_class'
			}
		});
}
