(function($){

    $('li a:contains("Dusupay")').text('Dusupay');

	//$('#countrypicker').selectpicker({ style: 'btn-white' });

new SlimSelect({
  select: '#countrypicker'
});

    var countryPicked;

    var countryPickedOptions = [];

    $(document).on('submit', '#form1', function(e) {

        e.preventDefault();

        countryPicked = $('#countrypicker').val();

        if (countryPicked == null) return false;



        countryPickedOptions = paymentOptions[countryPicked];

        if (!countryPickedOptions.options || countryPickedOptions.options.length == 0) {

            show_error("No payment methods loaded!");

            return false;

        }



        var htmlOptions = '';

        for (var i = 0; i < countryPickedOptions.options.length; i++) {

            htmlOptions += `<option value="${countryPickedOptions.options[i].id}">${countryPickedOptions.options[i].name}</option>`;

        }



        $('.countryDescription').text(countryPickedOptions.country_name);

        $('#form1').hide();

        $('#form2').fadeIn();

        $('.providerpicker-container').html('<select id="providerpicker"><option value="" selected disabled>Choose Option</option>' + htmlOptions + '</select>');

       // $('#providerpicker').selectpicker({ style: 'btn-white' });

	new SlimSelect({select: '#providerpicker'});


        // $('.loader-widget').toggleClass("pop-widget");

        return false;

    });



    var providerPicked;

    $(document).on('submit', '#form2', function(e) {

        e.preventDefault();



        providerPicked = $('#providerpicker').val();

        if (providerPicked == null) return false;



		for(var i=0; i< countryPickedOptions.options.length; i++){

			if(countryPickedOptions.options[i].id == providerPicked){

				providerPicked = countryPickedOptions.options[i];

				break;

			}

		}



		if(providerPicked.id=="vodafone_gh"){

			$('.voucher-input').show();

			$("#cust-voucher").prop('required',true);

		}else{

			$('.voucher-input').hide();

			$("#cust-voucher").prop('required',false);

		}



		$('#form2').hide();

		$('#form3').show();

		$('.providerDescription').text(providerPicked.name);

    });



	$(document).on('submit', '#form3', function(e) {

		e.preventDefault();

		var $form = $(this);

		var phone = $('#cust-phone').val();

		var name = $('#cust-name').val();

		var email = $('#cust-email').val();

		var ntwk = providerPicked.id;

		var voucher = $('#cust-voucher').val();



		if(ntwk=="vodafone_gh" && (!voucher || voucher.length < 1)){

			show_error('Voucher is required.');return false;

		}



		if((name).length < 3){show_error('Name is too short.');return false;}

		if((name).length > 30){show_error('Name is too long.');return false;}

		if(!validateEmail(email)){email='';}

		if((phone).length < 10 || (phone).length > 16){show_error('Phone number is invalid.');return false;}

		

		remove_error();

		// Disable the submit button to prevent repeated clicks

		$form.find('button').prop('disabled', true);

		

		$('.loader-widget').toggleClass("pop-widget");

		$.get(collectionUrl + '&phn=' + phone + '&ntk=' + ntwk + '&mth=' + providerPicked.type + '&nam=' + name + '&ema=' + email + '&vch='+voucher ,function(data){

			data = JSON.parse(data);

			if(data && data.status){

				$('.loader-widget').toggleClass("pop-widget");

				$('.form2').hide();

				$('.form3').fadeIn();

				if(data.redirect){

					window.top.location.href = data.redirect;

				}

			}else{

				show_error(data.msg);

				$form.find('button').prop('disabled', false);

				$form.find('button').text('Try Again');

				$('.loader-widget').toggleClass("pop-widget");

			}

		});

		

		return false;

	});


function remove_error() {

    $('.widget-error').text('').removeClass('alert').removeClass('alert-danger');

}



function show_error(msg, ) {

    $('.widget-error').text(msg).addClass('alert').addClass('alert-danger');

}


})(jQuery);



function validateEmail(email) {

	var re = /^(([^<>()\[\]\\.,;:\s@"]+(\.[^<>()\[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/;

	return re.test(email);

}
