jQuery(document).ready(function() {


		jQuery("#elex_multivendor_vendor_state").select2({
		placeholder: "Search State",
		minimumInputLength: 2,
		});
		var commission = elexMvManageVendorsParams.commission;
		jQuery("#elex_multivendor_vendor_store_commission").val(commission);
// Function to load states based on the selected country
		function loadStates(countryCode) {
			var stateSelect = jQuery("#elex_multivendor_vendor_state");
			
		
			// Clear the current options
			stateSelect.empty();

			// Load new options based on the selected country
	

			jQuery.ajax({
				url: elexMvManageVendorsParams.ajax_url,
				type: 'post',
				data: {
					action: 'elex_mv_load_states',
					country: countryCode,
					nonce: elexMvManageVendorsParams.load_states_nonce,
				},
				success: function(result) {
					if (result.data) {
						stateSelect.parent().show();

						stateSelect.next(".select2-container").show();
						stateSelect.append(result.data);
						stateSelect.trigger('change');
					}else{
					
						stateSelect.parent().hide();
						stateSelect.next(".select2-container").hide();
	
					}
				},
			});
		}

		// Event listener to load states when the country changes
		jQuery("#elex_multivendor_vendor_country").on('change', function() {
			var countryCode = jQuery(this).val();

			loadStates(countryCode);
		});

		// Load states on page load (if a country is already selected)
		var countryCode = jQuery("#elex_multivendor_vendor_country").val();
		if (countryCode) {
			loadStates(countryCode);
		}



	var input_phone_number = window.intlTelInput( document.querySelector("#elex_multivendor_vendor_phone_number"), {
		autoPlaceholder: "polite",
	} );

		jQuery("#elex_multivendor_vendor_dial_code").val( input_phone_number.getSelectedCountryData().dialCode );
		jQuery("#elex_multivendor_vendor_country_iso").val(input_phone_number.getSelectedCountryData().iso2);

		document.querySelector("#elex_multivendor_vendor_phone_number").addEventListener("countrychange", function() {
			jQuery("#elex_multivendor_vendor_dial_code").val(input_phone_number.getSelectedCountryData().dialCode );
			jQuery("#elex_multivendor_vendor_country_iso").val(input_phone_number.getSelectedCountryData().iso2);

		});
});
validation_for_forms_func('#elex_multivendor_vendor_user_name', /^[A-Za-z][ A-Za-z0-9_]{0,29}$/, '#elex_multivendor_vendor_user_name_error', 'Enter Valid Name')
validation_for_forms_func('#elex_multivendor_vendor_store_name', /^[A-Za-z][ A-Za-z0-9_]{0,29}$/, '#elex_multivendor_vendor_store_name_error', 'Enter Valid Store Name')
validation_for_forms_func('#elex_multivendor_vendor_email', /^[a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$/, '#elex_multivendor_vendor_email_error', 'Enter Valid Email Address')
validation_for_forms_func('#elex_multivendor_vendor_phone_number', /^[0-9]{0,20}$/, '#elex_multivendor_vendor_phone_number_error', 'Enter Valid Phone NUmber')
// validation_for_forms_func('#elex_multivendor_vendor_store_full_addr', /^[A-Za-z][ A-Za-z0-9_]{0,29}$/, '#elex_multivendor_vendor_store_full_addr_error', 'Enter Valid Address')
validation_for_forms_func('#elex_multivendor_vendor_city', /^[A-Za-z][ A-Za-z0-9_]{0,29}$/, '#elex_multivendor_vendor_city_error', 'Enter Valid City')
validation_for_forms_func('#elex_multivendor_vendor_postal_code', /^[0-9_]{0,29}$/, '#elex_multivendor_vendor_postal_code_error', 'Enter Valid Postal')
validation_for_forms_func('#elex_multivendor_vendor_state', /^[A-Za-z][ A-Za-z]{0,29}$/, '#elex_multivendor_vendor_state_error', 'Enter Valid State')
validation_for_forms_func('#elex_multivendor_vendor_country', /^[A-Za-z][ A-Za-z]{0,29}$/, '#elex_multivendor_vendor_country_error', 'Enter Valid Country')
validation_for_forms_func('#elex_multivendor_paypal_email', /^[a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$/, '#elex_multivendor_paypal_email_error', 'Enter Valid Email')





	

function validation_for_forms_func(id_for_input_element, regex_for_string, id_for_error_element, error_string){
	jQuery(id_for_input_element).keyup(function(){
	const regex = regex_for_string;
   
	if(! regex.test(this.value)){
		jQuery(id_for_error_element).html(`<div class="text-danger">${error_string}</div>`);
	}else{
		jQuery(id_for_error_element).html('');

	}
	});
}
