	/*  Wizard */
	jQuery(function ($) {
		"use strict";
		$('form#wrapped').attr('action', 'registration.php');
		$("#wizard_container").wizard({
			stepsWrapper: "#wrapped",
			submit: ".submit",
			beforeSelect: function (event, state) {
				if ($('input#website').val().length != 0) {
					return false;
				}
				if ($('input#password1').val() != $('input#password2').val()) {
					return false;
				}
				if (!state.isMovingForward)
					return true;
				var inputs = $(this).wizard('state').step.find(':input');
				return !inputs.length || !!inputs.valid();
			}
		}).validate({
			errorPlacement: function (error, element) {
				if (element.is(':radio') || element.is(':checkbox')) {
					error.insertBefore(element.next());
				} else {
					error.insertAfter(element);
				}
			}
		});
		//  progress bar
		$("#progressbar").progressbar();
		$("#wizard_container").wizard({
			afterSelect: function (event, state) {
				$("#progressbar").progressbar("value", state.percentComplete);
				$("#location").text("(" + state.stepsComplete + "/" + state.stepsPossible + ")");
			}
		});
		// Validate select
		$('#wrapped').validate({
			ignore: [],
			rules: {
				select: {
					required: true
				}
			},
			errorPlacement: function (error, element) {
				if (element.is('select:hidden')) {
					error.insertAfter(element.next('.nice-select'));
				} else {
					error.insertAfter(element);
				}
			}
		});


		$('input[type="number"]').keyup(function(e)
		{
			if (/\D/g.test(this.value))
			{
				this.value = this.value.replace(/\D/g, '');
			}
		});


		function checkAvailability(email) {
			$("#loaderIcon").show();
			jQuery.ajax({
				url: "http://localhost/sinerjiteknoindo/lomba-karya-tulis/site/checkAvailability/type/1",
				data:'email='+$(email).val(),
				type: "POST",
				success:function(data){
					$("#user-availability-status").html(data);
					$("#loaderIcon").hide();
				},
				error:function (){}
			});
		}

		var i=1;
		$('#add').click(function(){
			i++;
			$('#dynamic_field').append('<div class="row" id="row'+i+'"><div class="col-md-12"><h3 class="main_question">Anggota Tim</h3></div><div class="col-md-5"> <div class="form-group"> <input type="number" id="member_code_'+i+'" name="member_code[]" placeholder="NIK E-KTP" class="form-control name_list" required="true" /> </div> <div class="form-group"> <input type="email" id="member_email_'+i+'" name="member_email[]" placeholder="Alamat Email Aktif" class="form-control name_list" required="true" /> </div> <div class="form-group"> <input type="file" accept="image/*" id="member_image_identity_'+i+'" name="member_image_identity[]" class="btn btn-sm name_list" required="true" >Upload Foto KTP</input> </div> </div> <div class="col-md-5"> <div class="form-group"> <input type="text" id="member_name_'+i+'" name="member_name[]" placeholder="Nama Lengkap" class="form-control name_list" required="true" /> </div> <div class="form-group"> <input type="number" id="member_phone_'+i+'" name="member_phone[]" placeholder="No. Whatsapp" class="form-control name_list" required="true" /> </div> <div class="form-group"> <input type="file" accept="image/*" id="member_image_foto_'+i+'" name="member_image_foto[]" class="btn btn-sm name_list" required="true" >Upload Pas Foto</input> </div> </div> <div class="col-md-2"> <button type="button" name="remove" id="'+i+'" class="btn btn-danger btn_remove">X</button> </div><div class="col-md-5"> <div class="form-group"> <input type="file" accept="image/*" id="member_image_sosmed_'+i+'" name="member_image_sosmed[]" class="btn btn-sm name_list" required="true" >Upload Bukti Follow Sosial Media</input> </div> </div>');
			$('#preview_field').append('<ul id="preview'+i+'"> <li><label>NIK E-KTP</label>: <span id="text_member_code_'+i+'"></span></li> <li><label>Nama Lengkap</label>: <span id="text_member_name_'+i+'"></span></li> <li><label>ALamat Email</label>: <span id="text_member_email_'+i+'"></span></li> <li><label>No. Whatsapp</label>: <span id="text_member_phone_'+i+'"></span></li> <li><label>Lampiran Identitas KTP</label>:</li> <li><img id="preview_member_image_identity_'+i+'" class="img-responsive" height="150px" src=""/></li> <li><label>Lampiran Pas Foto</label>:</li> <li><img id="preview_member_image_foto_'+i+'" class="img-responsive" height="150px" src=""/></li> <li><label>Lampiran Bukti Follow Sosial Media</label>:</li> <li><img id="preview_member_image_sosmed_'+i+'" class="img-responsive" height="150px" src=""/></li> </ul>');

			$("#member_code_"+i).change(function(){
				var value = $(this).val();
				$("#text_member_code_"+i).text(value);
				var mp = new Mprogress();
				mp.start();
				jQuery.ajax({
					url: "http://localhost/sinerjiteknoindo/lomba-karya-tulis/site/checkAvailability/type/3",
					data:'code='+$(this).val(),
					type: "POST",
					success:function(data){
						if(data==1){
							swal("Informasi", "NIK Sudah Terdaftar", "error");
							$("#text_member_code_"+i).val('');
						}
						mp.end();
					},
					error:function (){}
				});
				// swal("Informasi", value, "success");
			});

			$("#member_name_"+i).change(function(){
				var value = $(this).val();
				$("#text_member_name_"+i).text(value);
			});

			$("#member_email_"+i).change(function(){
				var value = $(this).val();
				$("#text_member_email_"+i).text(value);
				var mp = new Mprogress();
				mp.start();
				jQuery.ajax({
					url: "http://localhost/sinerjiteknoindo/lomba-karya-tulis/site/checkAvailability/type/1",
					data:'email='+$(this).val(),
					type: "POST",
					success:function(data){
						if(data==1){
							swal("Informasi", "Alamat Email Sudah Terdaftar", "error");
							$('#member_email_'+i).val('');
						}
						mp.end();
					},
					error:function (){}
				});
			});

			$("#member_phone_"+i).change(function(){
				var value = $(this).val();
				$("#text_member_phone_"+i).text(value);
			});

			$("#member_image_identity_"+i).change(function() {
				readURL(this,'preview_member_image_identity_'+i);
			});

			$("#member_image_foto_"+i).change(function() {
				readURL(this,'preview_member_image_foto_'+i);
			});

			$("#member_image_sosmed_"+i).change(function() {
				readURL(this,'preview_member_image_sosmed_'+i);
			});


			var rowCount = $('#dynamic_field .row').length;
			if(rowCount==3){
				$("#add").hide();
			}else{
				$("#add").show();
			}

		});
	$(document).on('click', '.btn_remove', function(){
		var button_id = $(this).attr("id");
		$('#row'+button_id+'').remove();
		$('#preview'+button_id+'').remove();

		var rowCount = $('#dynamic_field .row').length;
		if(rowCount==3){
			$("#add").hide();
		}else{
			$("#add").show();
		}

	});

	function readURL(input,preview) {
		if (input.files && input.files[0]) {
			var reader = new FileReader();
			reader.onload = function(e) {
				$('#'+preview).attr('src', e.target.result);
			}
			reader.readAsDataURL(input.files[0]);
		}
	}

	$("#member_code_1").change(function(){
		var value = $(this).val();
		$("#text_member_code_1").text(value);
		var mp = new Mprogress();
		mp.start();
		jQuery.ajax({
			url: "http://localhost/sinerjiteknoindo/lomba-karya-tulis/site/checkAvailability/type/3",
			data:'code='+$(this).val(),
			type: "POST",
			success:function(data){
				if(data==1){
					swal("Informasi", "NIK Sudah Terdaftar", "error");
					$('#member_code_1').val('');
				}
				mp.end();
			},
			error:function (){}
		});
	});

	$("#member_name_1").change(function(){
		var value = $(this).val();
		$("#text_member_name_1").text(value);
	});

	$("#member_email_1").change(function(){
		var value = $(this).val();
		$("#text_member_email_1").text(value);
		var mp = new Mprogress();
		mp.start();
		jQuery.ajax({
			url: "http://localhost/sinerjiteknoindo/lomba-karya-tulis/site/checkAvailability/type/1",
			data:'email='+$(this).val(),
			type: "POST",
			success:function(data){
				if(data==1){
					swal("Informasi", "Alamat Email Sudah Terdaftar", "error");
					$('#member_email_1').val('');
				}
				mp.end();
			},
			error:function (){}
		});
	});

	$("#member_phone_1").change(function(){
		var value = $(this).val();
		$("#text_member_phone_1").text(value);
	});

	$("#member_image_identity_1").change(function() {
		readURL(this,'preview_member_image_identity_1');
	});

	$("#member_image_foto_1").change(function() {
		readURL(this,'preview_member_image_foto_1');
	});

	$("#member_image_sosmed_1").change(function() {
		readURL(this,'preview_member_image_sosmed_1');
	});

	var checkbox = $("#finish_check"), napoli = $("#napoli");

	napoli.hide();

	checkbox.on("click", function () {
		if ($(this).is(":checked")) {
			napoli.show();
		} else {
			napoli.hide();
		}
	});

		// Submit loader mask
		$('form#users-form').on('submit', function () {
			var form = $("form#users-form");
			form.validate();
			if (form.valid()) {
				$("#loader_form").fadeIn();
			}
		});
	});

	// Summary
	function getVals(formControl, controlType) {
		switch (controlType) {

			case 'fullname':
				// Get the value for a input text
				var value = $(formControl).val();
				$("#fullname").text(value);
				break;

				case 'phone':
				// Get the value for a input text
				var value = $(formControl).val();
				$("#phone").text(value);
				break;

				case 'email':
				// Get the value for a input text
				var value = $(formControl).val();
				$("#email").text(value);
				var mp = new Mprogress();
				mp.start();
				jQuery.ajax({
					url: "http://localhost/sinerjiteknoindo/lomba-karya-tulis/site/checkAvailability/type/1",
					data:'email='+$(formControl).val(),
					type: "POST",
					success:function(data){
						if(data==1){
							swal("Informasi", "Alamat Email Sudah Terdaftar", "error");
							$(formControl).val('');
							$(formControl).addClass('error');
						}
						mp.end();
					},
					error:function (){}
				});
				break;

				case 'username':
				// Get the value for a input text
				var value = $(formControl).val();
				$("#username").text(value);

				if(jQuery.trim(value)!='' && !value.match(/^[A-Za-z0-9_]+$/)) {
					swal("Username Hanya Boleh diisi oleh Huruf & Angka dan Jangan Ada Spasi.");
					$(formControl).val('');
					$(formControl).addClass('error');
				}

				var mp = new Mprogress();
				mp.start();
				jQuery.ajax({
					url: "http://localhost/sinerjiteknoindo/lomba-karya-tulis/site/checkAvailability/type/2",
					data:'username='+$(formControl).val(),
					type: "POST",
					success:function(data){
						if(data==1){
							swal("Informasi", "Username Sudah Terdaftar", "error");
							$(formControl).val('');
							$(formControl).addClass('error');
						}
						mp.end();
					},
					error:function (){}
				});
				break;

				case 'password':
				// Get the value for a input text
				var value = $(formControl).val();
				$("#password").text(value);

				if(value.length<6) {
					swal("Password terlalu pendek (minimum 6 karakter).");
					$(formControl).val('');
					$(formControl).addClass('error');
				}
				break;
			}
		}


