| 
									
										
										
										
											2014-01-09 21:48:05 -10:00
										 |  |  | function codeMatches() { | 
					
						
							|  |  |  | 	var match = $('#code').val().match(/[0-9]{3}-?[0-9]{3}/g) | 
					
						
							|  |  |  | 	return match != null && match.length == 1 && match[0] == $('#code').val(); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | function numberMatches() { | 
					
						
							| 
									
										
										
										
											2014-01-11 21:32:13 -10:00
										 |  |  | 	var country_code = $('#countrycode').val().replace(/\D/g, ''); | 
					
						
							|  |  |  | 	return $('#number').val().replace(/\D/g, '').length > 5 && country_code.length > 0 && country_code.length < 4; | 
					
						
							| 
									
										
										
										
											2014-01-09 21:48:05 -10:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | $('#code').on('change', function() { | 
					
						
							|  |  |  | 	if (!codeMatches()) | 
					
						
							|  |  |  | 		$('#code').attr('style', 'background-color:#ff6666;'); | 
					
						
							|  |  |  | 	else | 
					
						
							|  |  |  | 		$('#code').attr('style', ''); | 
					
						
							|  |  |  | }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-01-10 21:58:58 -10:00
										 |  |  | $('#number').on('change', function() {//TODO
 | 
					
						
							| 
									
										
										
										
											2014-01-09 21:48:05 -10:00
										 |  |  | 	if (!numberMatches()) | 
					
						
							|  |  |  | 		$('#number').attr('style', 'background-color:#ff6666;'); | 
					
						
							|  |  |  | 	else | 
					
						
							|  |  |  | 		$('#number').attr('style', ''); | 
					
						
							| 
									
										
										
										
											2014-01-11 21:32:13 -10:00
										 |  |  | }); | 
					
						
							| 
									
										
										
										
											2014-01-09 21:48:05 -10:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-03-20 00:20:54 -07:00
										 |  |  | var single_device = false; | 
					
						
							| 
									
										
										
										
											2014-01-17 01:08:33 -05:00
										 |  |  | var signaling_key = getRandomBytes(32 + 20); | 
					
						
							| 
									
										
										
										
											2014-04-17 15:56:50 +02:00
										 |  |  | var password = btoa(getString(getRandomBytes(16))); | 
					
						
							| 
									
										
										
										
											2014-01-17 01:08:33 -05:00
										 |  |  | password = password.substring(0, password.length - 2); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | $('#init-go-single-client').click(function() { | 
					
						
							|  |  |  | 	if (numberMatches()) { | 
					
						
							|  |  |  | 		var number = "+" + $('#countrycode').val().replace(/\D/g, '') + $('#number').val().replace(/\D/g, ''); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		$('#init-go').html('Setup'); | 
					
						
							|  |  |  | 		$('#countrycode').prop('disabled', 'disabled'); | 
					
						
							|  |  |  | 		$('#number').prop('disabled', 'disabled'); | 
					
						
							|  |  |  | 		$('#init-go-single-client').prop('disabled', 'disabled'); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		single_device = true; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-03-11 01:21:28 -07:00
										 |  |  | 		API.requestVerificationCode(number, | 
					
						
							|  |  |  | 			function(response) { }, | 
					
						
							|  |  |  | 			function(code) { | 
					
						
							| 
									
										
										
										
											2014-01-17 01:08:33 -05:00
										 |  |  | 				alert("Failed to send key?" + code); //TODO
 | 
					
						
							|  |  |  | 			} | 
					
						
							| 
									
										
										
										
											2014-03-11 01:21:28 -07:00
										 |  |  | 		); | 
					
						
							| 
									
										
										
										
											2014-01-17 01:08:33 -05:00
										 |  |  | 	} | 
					
						
							|  |  |  | }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-01-09 21:48:05 -10:00
										 |  |  | $('#init-go').click(function() { | 
					
						
							|  |  |  | 	if (codeMatches() && numberMatches()) { | 
					
						
							| 
									
										
										
										
											2014-01-11 21:32:13 -10:00
										 |  |  | 		var number = "+" + $('#countrycode').val().replace(/\D/g, '') + $('#number').val().replace(/\D/g, ''); | 
					
						
							| 
									
										
										
										
											2014-01-09 21:48:05 -10:00
										 |  |  | 
 | 
					
						
							|  |  |  | 		$('#init-setup').hide(); | 
					
						
							|  |  |  | 		$('#verify1done').html(''); | 
					
						
							| 
									
										
										
										
											2014-01-17 01:08:33 -05:00
										 |  |  | 		$('#verify2').hide(); | 
					
						
							| 
									
										
										
										
											2014-01-09 21:48:05 -10:00
										 |  |  | 		$('#verify3done').html(''); | 
					
						
							| 
									
										
										
										
											2014-01-17 01:08:33 -05:00
										 |  |  | 		$('#verify4done').html(''); | 
					
						
							| 
									
										
										
										
											2014-01-09 21:48:05 -10:00
										 |  |  | 		$('#verify').show(); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-03-23 13:19:53 -04:00
										 |  |  | 		API.confirmCode($('#code').val(), number, password, signaling_key, single_device, | 
					
						
							| 
									
										
										
										
											2014-03-11 01:21:28 -07:00
										 |  |  | 			function(response) { | 
					
						
							| 
									
										
										
										
											2014-01-17 01:08:33 -05:00
										 |  |  | 				if (single_device) | 
					
						
							|  |  |  | 					response = 1; | 
					
						
							| 
									
										
										
										
											2014-01-10 21:58:58 -10:00
										 |  |  | 				var number_id = number + "." + response; | 
					
						
							| 
									
										
										
										
											2014-01-11 21:32:13 -10:00
										 |  |  | 				storage.putEncrypted("password", password); | 
					
						
							|  |  |  | 				storage.putEncrypted('signaling_key', signaling_key); | 
					
						
							| 
									
										
										
										
											2014-01-11 22:49:46 -10:00
										 |  |  | 				storage.putUnencrypted("number_id", number_id); | 
					
						
							|  |  |  | 				$('#verify1done').html('done'); | 
					
						
							| 
									
										
										
										
											2014-01-11 21:32:13 -10:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-01-17 01:08:33 -05:00
										 |  |  | 				var register_keys_func = function() { | 
					
						
							|  |  |  | 					$('#verify2done').html('done'); | 
					
						
							| 
									
										
										
										
											2014-01-22 06:23:41 +00:00
										 |  |  | 					crypto.generateKeys(function(keys) { | 
					
						
							|  |  |  | 						$('#verify3done').html('done'); | 
					
						
							| 
									
										
										
										
											2014-03-11 01:21:28 -07:00
										 |  |  | 						API.registerKeys(keys, | 
					
						
							|  |  |  | 							function(response) { | 
					
						
							| 
									
										
										
										
											2014-01-22 06:23:41 +00:00
										 |  |  | 								$('#complete-number').html(number); | 
					
						
							|  |  |  | 								$('#verify').hide(); | 
					
						
							|  |  |  | 								$('#setup-complete').show(); | 
					
						
							|  |  |  | 								registrationDone(); | 
					
						
							| 
									
										
										
										
											2014-03-11 01:21:28 -07:00
										 |  |  | 							}, function(code) { | 
					
						
							| 
									
										
										
										
											2014-01-22 06:23:41 +00:00
										 |  |  | 								alert(code); //TODO
 | 
					
						
							|  |  |  | 							} | 
					
						
							| 
									
										
										
										
											2014-03-11 01:21:28 -07:00
										 |  |  | 						); | 
					
						
							| 
									
										
										
										
											2014-01-17 01:08:33 -05:00
										 |  |  | 					}); | 
					
						
							|  |  |  | 				} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 				if (!single_device) { | 
					
						
							|  |  |  | 					getKeysForNumber(number, function(identityKey) { | 
					
						
							|  |  |  | 						subscribeToPush(function(message) { | 
					
						
							|  |  |  | 							//TODO receive shared identity key
 | 
					
						
							|  |  |  | 							register_keys_func(); | 
					
						
							| 
									
										
										
										
											2014-01-11 21:32:13 -10:00
										 |  |  | 						}); | 
					
						
							| 
									
										
										
										
											2014-01-17 01:08:33 -05:00
										 |  |  | 						requestIdentityPrivKeyFromMasterDevice(number); | 
					
						
							|  |  |  | 					}, function(error_msg) { | 
					
						
							|  |  |  | 						alert(error_msg); //TODO
 | 
					
						
							| 
									
										
										
										
											2014-01-11 22:49:46 -10:00
										 |  |  | 					}); | 
					
						
							| 
									
										
										
										
											2014-01-17 01:08:33 -05:00
										 |  |  | 				} else { | 
					
						
							|  |  |  | 					register_keys_func(); | 
					
						
							|  |  |  | 				} | 
					
						
							| 
									
										
										
										
											2014-03-11 01:21:28 -07:00
										 |  |  | 			}, function(code) { | 
					
						
							| 
									
										
										
										
											2014-01-09 21:48:05 -10:00
										 |  |  | 				var error; | 
					
						
							|  |  |  | 				switch(code) { | 
					
						
							|  |  |  | 				case 403: | 
					
						
							|  |  |  | 					error = "Invalid code, please try again."; | 
					
						
							|  |  |  | 					break; | 
					
						
							|  |  |  | 				case -1: | 
					
						
							|  |  |  | 					error = "Error connecting to server, please check your network connection."; | 
					
						
							|  |  |  | 					break; | 
					
						
							|  |  |  | 				default: | 
					
						
							|  |  |  | 					error = "Unknown error, please try again later."; | 
					
						
							|  |  |  | 					console.log("Got error code " + code); | 
					
						
							|  |  |  | 				} | 
					
						
							|  |  |  | 				alert(error); //TODO
 | 
					
						
							| 
									
										
										
										
											2014-01-11 22:49:46 -10:00
										 |  |  | 			} | 
					
						
							| 
									
										
										
										
											2014-03-11 01:21:28 -07:00
										 |  |  | 		); | 
					
						
							| 
									
										
										
										
											2014-01-09 21:48:05 -10:00
										 |  |  | 	} | 
					
						
							|  |  |  | }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-01-22 06:23:41 +00:00
										 |  |  | registerOnLoadFunction(function() { | 
					
						
							|  |  |  | 	if (!isRegistrationDone()) { | 
					
						
							|  |  |  | 		$('#init-setup').show(); | 
					
						
							|  |  |  | 	} else { | 
					
						
							|  |  |  | 		$('#complete-number').html(storage.getUnencrypted("number_id").split(".")[0]); | 
					
						
							|  |  |  | 		$('#setup-complete').show(); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | }); |