Subversion Repository Public Repository

Nextrek

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
var path = 'http://svm19842.vps.tagadab.it:8084/smartcharging/';
var affiliateImgs = [];
var i = 0;
var j = 0;
var u_id = '';
var token = '';

// Controllo sulla sintassi della email
function validateEmail(email) {
	var pattern = /^([\w-]+(?:\.[\w-]+)*)@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/i;
	return pattern.test(email);
}

// Controllo sulla sintassi della latitudine e longitudine
function validateLatLon(value) {
	if(value!=""){
		return true;
	}else{
		return false;
	}
	
}
// Controlli sui campi immessi nel form - funzione richiamata onsubmit dei form
function basicControls(user_type) {
	var control = 'ok';
	
	// Diversi controlli in base al form usato, utente o locale
	if (user_type == 'user') {
		// Estrazione dei campi digitati nel form
		var nome = jQuery.trim(jQuery('#name_user').val());
		var email = jQuery.trim(jQuery('#email_user').val());
		var password1 = jQuery.trim(jQuery('#passw1_user').val());
		var password2 = jQuery.trim(jQuery('#passw2_user').val());
		var firstname = jQuery.trim(jQuery('#name_user').val());
		var lastname = jQuery.trim(jQuery('#surname_user').val());
		//var cel = jQuery.trim(jQuery('#self_phone_user').val());
		var username = jQuery.trim(jQuery('#username_user').val());
		//var avatar = jQuery.trim(jQuery('#avatar_path_user').val());
		// Controllo su campi vuoti
		if (window.location.hash == "#affiliate"){
			if ((email=='') || (password1=='') || (password2=='') || (firstname=='') || (lastname=='') || (username=='') /*|| (avatar=='')*/) {
				// alert('Ci sono dei campi vuoti, completare quelli mancanti.');
				//jQuery('#all').prop('hidden', false);
				jQuery('#all').attr('hidden', false);
				control = 0;
			}
		}else{
			if ((email=='') || (password1=='') || (password2=='') || (username=='')) {
				// alert('Ci sono dei campi vuoti, completare quelli mancanti.');
				//jQuery('#all').prop('hidden', false);
				jQuery('#all').attr('hidden', false);
				control = 0;
			}
		}
		
		// Controllo su password
		if (password1.length < 8) {
			// alert('La lunghezza della password deve essere di almeno 8 caratteri alfanumerici.');
			jQuery('#passw_length').prop('hidden', false);
			control = 0;
		} else {
			if (password1 != password2) {
				// alert('Le password non combaciano, inserire la stessa password in entrambi i campi.');
				jQuery('#passw_equal').prop('hidden', false);
				control = 0;
			}
		}
		
		if (document.getElementById('condizioni').checked == false) {
			//jQuery('#terms').prop('hidden', false);
			jQuery('#terms').attr('hidden', false);
			control = 0;
		}	
		
	} else if (user_type == 'affiliate') {
		// Estrazione dei campi digitati nel form
		var nome = jQuery.trim(jQuery('#name-affiliate').val());
		var typology = jQuery.trim(jQuery('.typology').val());
		var lat = jQuery.trim(jQuery('#lat').val());
		var lon = jQuery.trim(jQuery('#lon').val());
		var cel = jQuery.trim(jQuery('#self_phone_affiliate').val());
		var email = jQuery.trim(jQuery('#email-affiliate').val());
		//var site = jQuery.trim(jQuery('#site-address').val());
		
		
		// Controllo su campi vuoti
		if ((email=='') || (lat=='') || (lon=='') || (cel=='') || (typology=='default')) {
			// alert('Ci sono dei campi vuoti, completare quelli mancanti.');
			jQuery('#all').attr('hidden', false);
			control = 0;
		}
		
		
		// Controllo che l'utente abbia scelto almeno una tipologia di locale
		if (typology == 'default') {
			// alert('Nessuna tipologia di locale selezionata, selezionarne almeno una.');
			jQuery('#typoloy-err').attr('hidden', false);
			control = 0;
		}
		
		// Controllo su latitudine e longitudine
		if (!validateLatLon(lat) && !validateLatLon(lon)) {
			// alert('La lunghezza della password deve essere di almeno 8 caratteri alfanumerici.');
			//jQuery('#passw_length').prop('hidden', false);
			jQuery('#lat-lon-err').attr('hidden', false);
			control = 0;
		} 
		// Controllo su longitudine
		/*if (!validateLatLon(lon)) {
			// alert('La lunghezza della password deve essere di almeno 8 caratteri alfanumerici.');
			//jQuery('#passw_length').prop('hidden', false);
			jQuery('#lon-err').attr('hidden', false);
			control = 0;
		}*/ 
		
		if (document.getElementById('condizioni2').checked == false) {
			//jQuery('#terms').prop('hidden', false);
			jQuery('#terms').attr('hidden', false);
			control = 0;
		}
	}
	
	// Uso html storage per il nome
	localStorage.setItem('nome', nome);
	
	// Controllo su numero di cellulare
	/*if (cel.length < 5 || !jQuery.isNumeric(cel)) {
		// alert('Il numero di telefono o cellulare non sembra valido.');
		jQuery('#alert_cel').attr('hidden', false);
		control = 0;
	}*/
		
	// Controllo su pattern email
	if (!validateEmail(email)){
		// alert('Inserire una mail valida!');
		jQuery('#email_invalida').attr('hidden', false);
		control = 0;
	} else {
		localStorage.setItem('email', email); // Uso html storage per la email
	}
	
	
	
	return Boolean(control);
}

// Invio del form in base all'utente che si vuole registrare, utente o locale
function send_form(user_type, form) {
	var user = user_type;
	var data ={};
	if (user == 'user') {
		var url_server = path + "registrazione_utente.php";
		data = {"nome": jQuery('#name_user').val(), "cognome": jQuery('#surname_user').val(), "cell": jQuery('#self_phone_user').val(), "email": jQuery('#email_user').val(), /*"avatar": jQuery('#avatar_path_user').val().substr(12, jQuery('#avatar_path_user').val().lenght),*/ "username": jQuery('#username_user').val(), "password": jQuery('#passw1_user').val()};
	} else if (user == 'affiliate') {
		var url_server = path + "add_locale.php";
		data = {"user_id": u_id, "nomelocale": jQuery('#name-affiliate').val(), "descrizione": jQuery('#description').val(), "tipologia": jQuery('.typology').val(), "lat": parseFloat(jQuery('#lat').val()), "lon": parseFloat(jQuery('#lon').val()), "indirizzo": jQuery('#address').val(), "telefono": jQuery('#self_phone_affiliate').val(), "email": jQuery('#email-affiliate').val(), "sito": jQuery('#site-address').val(), "n_punti_ricarica": jQuery('#charge-stations').val(), "imgs": affiliateImgs, "token": token};
		
		//localStorage.setItem('isProf', true); // Uso html storage per segnalare locale
	}
	// Controllo sui campi immessi
	if (basicControls(user_type) == true) {
		console.log(form);
		// Invio del form
		jQuery.ajax({
			url: url_server,
			type: 'POST',
			dataType: 'json',
			data: JSON.stringify(data),
			beforeSend: function(){
				$("form").hide();
				$(".loader").show();				
			},
			complete: function(){
				$(".loader").hide();
			},
			success: function(data) {
				// Controllo 
		        if (data) {
					switch(data.return){
						case -1: 
							//token error message
							if (user = 'user'){
								jQuery('.user-reg-header').show();
								jQuery('.user-reg-header h2').hide();
								$.removeCookie("user");
							} else {
								jQuery('.affiliate-reg-header').show();
								jQuery('#step1').hide();
								jQuery('#step2').hide();
								jQuery('.landing-ko').show();
								$.removeCookie("affiliate2");
								$.removeCookie("affiliate");
							}  
							
							
							break;
						case 1:
							$("form").show();
							if (user = 'user'){
								jQuery('#username_nondisp').attr('hidden', false);
							} else {
								jQuery('#errore_generico').attr('hidden', false);
							}
							break;
						case 2:
							$("form").show();
							jQuery('#email_nondisp').attr('hidden', false);
							break;
						case 3:
							/*$("form").show();
							jQuery('#errore_generico').attr('hidden', false);*/
							jQuery('.landing-ko').show();
							break;
						default:
							var id = '.' + user + '-reg-header h2';
							jQuery(id).hide();
							if (user == 'user'){								
								jQuery('#form-user').hide();
								//jQuery('.landing-confirm-user').show();
								//jQuery('#form-confirm-user').show();
								/*var formData = new FormData();
								formData.append('avatar_file', $('#avatar_path_user')[0].files[0]);
								formData.append('user_id', data.id);
								formData.append('avatar_name', data.avatar_name);
								formData.append('token', data.token);*/
								
								var hash = window.location.hash.slice(1,window.location.hash.length);
								if (hash == 'affiliate'){
									user = 'affiliate';
									u_id = data.id;
									token = data.token;
									jQuery('.landing-form-user').removeClass("visible");
									jQuery('.landing-form-user').hide();
									jQuery('#form-user').hide();
									jQuery('#step1').hide();
									jQuery('#step2').show();
									jQuery('.landing-form-affiliate').show();
									jQuery('.landing-form-affiliate .form-control').show();
									jQuery('#form-affiliate').show();
									getTypologies();
									$.removeCookie("affiliate");
									$.cookie("affiliate2", "#affiliate", {expires : expireDate()});
								} else {
									jQuery('.landing-ok').show();
								}
								//send_avatar(formData, user);
								
							} else {
								jQuery('.user-reg-header').hide();
								jQuery('.affiliate-reg-header').show();
								jQuery('#step1').hide();
								jQuery('#step2').hide();
								jQuery('#form-user').hide();
								jQuery('#form-affiliate').hide();
								jQuery('.landing-ok').show();
								$.removeCookie("affiliate2");
							}							
							$.removeCookie("user");
							break;
					}		
		        } 
		    },
		    error: function() {
		        jQuery('#modal_fail').modal('show');
		    }
		});
	} else {
		//alert('I controlli non sono andati a buon fine!');
	}
}

function send_avatar(formData, user){
	var url_server = path + "upload_avatar.php";
	jQuery.ajax({
		url: url_server,
		type: 'POST',
		data: formData,
		processData : false,
		enctype: 'multipart/form-data',
		contentType: false,
		beforeSend: function(){
			$("form").hide();
			$(".loader").show();				
		},
		complete: function(){
			$(".loader").hide();
		},
		success: function(data) {
	
			if (data) {
				switch(data.return){
					case 0: 
						if (user == 'user'){
							jQuery('.landing-ok').show();
						}else {
							jQuery('.landing-form-user').removeClass("visible");
							jQuery('.landing-form-user').hide();
							jQuery('#form-user').hide();
							jQuery('#step1').hide();
							jQuery('#step2').show();
							jQuery('.landing-form-affiliate').show();
							jQuery('.landing-form-affiliate .form-control').show();
							jQuery('#form-affiliate').show();
							$.removeCookie("affiliate");
							$.cookie("affiliate2", "#affiliate", {expires : expireDate()});
						}
						break;						
					default:				
						jQuery('.landing-ko').show();						
						break;
				}		
			} 
		},
		error: function() {
			jQuery('#modal_fail').modal('show');
		}
	});
}


function sendImgs(formData){
	var url_server = path + "upload_image.php";
	jQuery.ajax({
		url: url_server,
		type: 'POST',
		data: formData,
		processData : false,
		enctype: 'multipart/form-data',
		contentType: false,
		success: function(data) {
	
			if (data) {
				affiliateImgs[i]= data.image_file;
				i++;
			} 
		},
		error: function() {
			jQuery('#modal_fail').modal('show');
		}
	});
}

/*function deleteImgs(file){
	var url_server = path + "delete_immagine.php";
	data
	jQuery.ajax({
		url: url_server,
		type: 'POST',
		data: formData,
		processData : false,
		enctype: 'multipart/form-data',
		contentType: false,
	});
}*/


/*function deleteFile(){
		var id = '#'+ jQuery('.imgs-list').attr("id");
		var file = jQuery(id).text();
		deleteImgs(file);
		var index = affiliateImgs.indexOf(file);
		affiliateImgs.splice(index, 1);
		jQuery(id).detach();
		i = affiliateImgs.length;
}*/

function deleteUser(user_id, token){
	var url_server = path + "delete_user.php";
	var data = {"user_id": user_id, "token": token};
	jQuery.ajax({
		url: url_server,
		type: 'POST',
		dataType: 'json',
		data: JSON.stringify(data)
	});
}

function expireDate(){
	var date = new Date();
	var minutes = 30;
	date.setTime(date.getTime() + (minutes * 60 * 1000));
	
	return date;
}

function getTypologies(){
	var url_server = path + "get_tipologie_list.php";
	jQuery.ajax({
		url: url_server,
		type: 'POST',
		dataType: 'json',
		success: function(data){
			if(data){
				for (var i=0; i<data.tipologie.length; i++){
					$('select.typology').append('<option value="'+data.tipologie[i].id+'">'+data.tipologie[i].tipologia+'</option>');
				}
			}
		}
	});
}

//function setInfoAddress(AddressName, lat, lng){
	//$(".map-search-heading").after('<input id="address" type="hidden" class="form-control" value="'+AddressName+'" required=""><input id="lat" type="hidden" class="form-control" value="'+lat+'" required=""><input id="lon" type="hidden" class="form-control" value="'+lng+'" required="">');
//}


function setInfoAddress(AddressName, lat, lng){
	/*window.addressNemaVal = AddressName;
	window.latVal = lat;
	window.lngVal = lng;*/
	window.parent.addressDataGlobal = [];
	window.parent.addressDataGlobal.push(AddressName);
	window.parent.addressDataGlobal.push(lat);
	window.parent.addressDataGlobal.push(lng);
}

function getAddressData(){
	/*var addressData = [];
	addressData.push(window.addressNemaVal);
	addressData.push(window.latVal);
	addressData.push(window.lngVal);*/

	return window.addressDataGlobal;
}


jQuery(document).ready(function() {	
	
	// Definisco comportamento al submit del utente
	jQuery('#form-user').submit(function(event) {
		event.preventDefault();
		// Resetta tutti gli alert che sono apparsi nel form
		jQuery('.alert.alert-warning').attr('hidden', true);
		form = jQuery(this);
		if (token==''){
			send_form('user', form);
		} else{
			jQuery('.landing-form-user').removeClass("visible");
			jQuery('.landing-form-user').hide();
			jQuery('#form-user').hide();
			jQuery('#step1').hide();
			jQuery('#step2').show();
			jQuery('.landing-form-affiliate').show();
			jQuery('.landing-form-affiliate .form-control').show();
			jQuery('#form-affiliate').show();
			$.removeCookie("affiliate");
			$.cookie("affiliate2", "#affiliate", {expires : expireDate()});
		}		
		
	});


	// Definisco comportamento al submit del locale
	/*jQuery('#form-affiliate').submit(function(event) {
		event.preventDefault();
		// Resetta tutti gli alert che sono apparsi nel form
		jQuery('.alert.alert-warning').attr('hidden', true);
		
		form = jQuery(this);
		send_form('affiliate', form);
	});*/

	jQuery('#form-affiliate').submit(function(event) {
		event.preventDefault();
		// Resetta tutti gli alert che sono apparsi nel form
		jQuery('.alert.alert-warning').attr('hidden', true);
		
		var dataAddressInfo = getAddressData();
		
		form = jQuery(this);
		form.find("#address, #lon, #lat").remove();
		form.append('<input id="address" name="address" type="hidden" value="'+dataAddressInfo[0]+'" ><input id="lat" name="lat" type="hidden" value="'+dataAddressInfo[1]+'" ><input id="lon" name="lon" type="hidden" value="'+dataAddressInfo[2]+'">');
		send_form('affiliate', form);
		$(form).hide();
		
	});

	
	jQuery('#load-images').click(function(event) {
		event.preventDefault();
		var form = $('#form-affiliate');
		var formDataImgs = new FormData();
		formDataImgs.append('image_file', $('#affiliate-imgs')[0].files[0]);
		sendImgs(formDataImgs);
		var file = jQuery('#affiliate-imgs').val().substr(12, jQuery('#affiliate-imgs').val().lenght);		
		//jQuery('.img-container').append('<div id="'+j+'" class="col-md-6 imgs-list">'+file+'<span class="glyphicon glyphicon-remove" onclick="deleteFile();"></span></div>');
		jQuery('.img-container').append('<div id="'+j+'" class="col-md-6 imgs-list">'+file+'<span class="glyphicon glyphicon-ok"></span></div>');
		j++;
		
	});
	
	$('.affiliate-left').click(function(){
		$('.user-reg-header').show();
		$('.user-reg-header h2').show();
		$('.affiliate-reg-header').hide();
		$('.landing-form-affiliate').removeClass("visible");
		$('.landing-form-affiliate').hide();
		$('.landing-form-user').addClass("visible");
		$('.landing-form-user').show();
		$('#form-user').show();
		$('.landing-form-user #name_user').show();
		$('.landing-form-user #surname_user').show();
		$('.landing-form-user #name_user').attr("required", "");
		$('.landing-form-user #surname_user').attr("required", "");
		//$('.landing-form-user #avatar_path_user').attr("required", "");
		$('.reg-header').hide();
		$('.reg-select').hide();
		//deleteUser(u_id, token);
		var hash = window.location.hash;
		var cookieName = hash.slice(1,hash.length);
		$.removeCookie(cookieName+'2');
		$.cookie(cookieName,  "#"+cookieName, {expires : expireDate()});
		//window.location.href = window.location.origin + window.location.pathname + window.location.search;
		window.location.hash = window.location.hash.replace(window.location.hash, '#affiliate');
	});
	
	
	$('.user-left').click(function(){
		$('.user-reg-header').hide();
		$('.affiliate-reg-header').hide();
		$('.landing-form-affiliate').removeClass("visible");
		$('.landing-form-affiliate').hide();
		$('.landing-form-user').removeClass("visible");
		$('.landing-form-user').hide();		
		$('.reg-header').show();
		$('.reg-select').show();
		if (token != '' && u_id != ''){
			deleteUser(u_id, token);
			token = '';
			u_id = '';
		}
		var hash = window.location.hash;
		var cookieName = hash.slice(1,hash.length);
		$.removeCookie(cookieName);
		//window.location.href = window.location.origin + window.location.pathname + window.location.search;
		window.location.hash = window.location.hash.replace(window.location.hash, '');
	});
	
	
});

Commits for Nextrek/Web/Smartcharging_site/js/formcheck.js

Diff revisions: vs.
Revision Author Commited Message
804 Diff Diff JMBauan picture JMBauan Mon 19 Oct, 2015 21:51:20 +0000

registration – get data from google maps

803 JMBauan picture JMBauan Mon 19 Oct, 2015 14:14:39 +0000