PageSourceSearch

https://www.jagarexamenonline.se/bw_common/javascript/user_referrals.js

js jagarexamenonline.se collected 2026-10-02 22:05:56 UTC 16,049 bytes, 351 lines download raw bytes

1/*
2* File: user_referrals.js
3* Author: Mark Humphrey
4* Created: 20/10/2023
5*
6* Description:
7	* THIS FILE CONTAINS BOBWEB COMMON FUNCTIONALITY FOR USER REFERRALS
8*
9*/
10
11function getUserReferralRequestParams() {
12	return {
13		when: new Date().getTime(),
14		refSite: siteCode
15	};
16}
17
18function getUserReferralsData(createIfNotExists) {
19	showLoader();
20
21	const params = getUserReferralRequestParams();
22	params.action = 'getData';
23	params.createIfNotExists = createIfNotExists === true
24
25	const deferred = $.Deferred();
26
27	$.post('/bw_common/php/user_referrals/user_referral_utils.php', params, function(response) {
28		hideLoader();
29		const data = JSON.parse(response);
30		deferred.resolve(data);
31	});
32
33	return deferred.promise();
34}
35
36// checks whether user is due to be shown notice of a referral conversion
37async function getUserReferralsConversionInfo() {
38	const userReferralsData = await getUserReferralsData();
39	if (userReferralsData.userDataExists === true && userReferralsData.conversionNoticePending === 1) {
40		showUserReferralConversion(userReferralsData);
41	}
42}
43
44function showUserReferralConversion(userReferralsData) {
45	let title, message;
46	if (language === 'en') {
47		title = 'You have earned free membership time!';
48		message = '<div>Your unique referral code was used by a friend to join one of our online courses.<br/><br/></div>';
49		message += '<div>We have added <strong>10 additional days</strong> to your membership to say thanks for your recommendation. Refer more friends and we\'ll keep rewarding you every time!<br/><br/></div>';
50	} else {
51		title = 'Du har tjänat gratis medlemskapstid!';
52		message = '<div>En vän använde din unika kod för att gå med i en av våra onlineutbildningar.<br/><br/></div>';
53		message += '<div>Vi har lagt till <strong>10 extra dagar</strong> på ditt medlemskap som tack för din rekommendation. Värva fler vänner för att tjäna ännu mer gratis medlemskapstid!<br/><br/></div>';
54	}
55
56	$.confirm ({
57		'title'		: title,
58		'message'	: message,
59		'buttons'	: {
60			'Värva fler vänner'	: {
61				'text': language === 'en' ? 'Refer more friends' : 'Värva fler vänner',
62				'class'	: 'gray',
63				'action': function() {
64					//do nothing
65					$.confirm.remove();
66					getUserReferralInfo();
67				}
68			},
69			'Stäng'	: {
70				'text': language === 'en' ? 'Close' : 'Stäng',
71				'class'	: 'gray',
72				'action': function() {
73					//do nothing
74				}
75			}
76		}
77	});
78
79	// record that user has seen conversion notice
80	const params = getUserReferralRequestParams();
81	params.action = 'acknowledgeConversionNotification';
82	params.refCode = userReferralsData.code;
83
84	$.post('/bw_common/php/user_referrals/user_referral_utils.php', params, function(response) {
85		const data = JSON.parse(response);
86	});
87}
88
89// shows dialog informing user how to refer new users
90async function getUserReferralInfo() {
91	const userReferralsData = await getUserReferralsData(true);
92	if (userReferralsData.userDataExists !== true || userReferralsData.code == null) {
93		return;
94	}
95
96	const signupLink = '/signup.php?friendreferral=true&refcode=' + userReferralsData.code + '&refsite=' + siteCode;
97
98	const linkIKK_SV = 'https://www.ikorkort.nu' + signupLink;
99	const linkIKK_EN = 'https://www.ikorkort.nu/en' + signupLink;
100	const linkIKKBE = 'https://www.ikorkortbe.se' + signupLink;
101	const linkIKKMC = 'https://www.ikorkortmc.se' + signupLink;
102	const linkIKKMOPED = 'https://www.ikorkortmoped.se' + signupLink;
103	const linkJO = 'https://www.jagarexamenonline.se' + signupLink;
104
105	let title, message;
106	if (language === 'en') {
107		title = 'Refer friends and get free membership time';
108
109		message = [
110			'<div>For every friend you refer to ' + siteName + ' or to one of our other online courses, <strong>10 extra days</strong> are added to your membership. Your friend receives a <strong>20% discount on their membership</strong>. It\'s a great deal for everyone!<br/><br/></div>',
111			'<div>Your unique code</div>',
112			'<h3 style="margin-top: 0.25em;">',userReferralsData.code,'</h3>',
113			'<div>Send your code to friends – or share the discount links below. The links take them directly to the signup pages with the <strong>20% discount</strong> already applied to all prices.</div>',
114			'<ul class="link_list">',
115			/* IKK (SV) */
116			'<li data-site="IKK"><div class="link_item">',
117			'<a href="' + linkIKK_SV + '">iKörkort.nu: B-class (Car) - Svenska</a>',
118			'<button title="Copy link to clipboard" onclick="copyReferralLinkToClipboard(\'' + linkIKK_SV + '\');"><img src="/graphics/ic_link.svg" alt="Copy link to clipboard"></button>',
119			'</div></li>',
120			/* IKK (EN) */
121			'<li data-site="IKK_EN"><div class="link_item">',
122			'<a href="' + linkIKK_EN + '">iKörkort.nu: B-class (Car) - English</a>',
123			'<button title="Copy link to clipboard" onclick="copyReferralLinkToClipboard(\'' + linkIKK_EN + '\');"><img src="/graphics/ic_link.svg" alt="Copy link to clipboard"></button>',
124			'</div></li>',
125			/* IKKBE */
126			'<li data-site="IKKBE"><div class="link_item">',
127			'<a href="' + linkIKKBE + '">iKörkortBE.se: BE-class (Heavy trailer)</a>',
128			'<button title="Copy link to clipboard" onclick="copyReferralLinkToClipboard(\'' + linkIKKBE + '\');"><img src="/graphics/ic_link.svg" alt="Copy link to clipboard"></button>',
129			'</div></li>',
130			/* IKKMC */
131			'<li data-site="IKKMC"><div class="link_item">',
132			'<a href="' + linkIKKMC + '">iKörkortMC.se: A-class (Motorcycle)</a>',
133			'<button title="Copy link to clipboard" onclick="copyReferralLinkToClipboard(\'' + linkIKKMC + '\');"><img src="/graphics/ic_link.svg" alt="Copy link to clipboard"></button>',
134			'</div></li>',
135			/* IKKMOPED */
136			'<li data-site="IKKMOPED"><div class="link_item">',
137			'<a href="' + linkIKKMOPED + '">iKörkortMoped.se: AM-class (Moped)</a>',
138			'<button title="Copy link to clipboard" onclick="copyReferralLinkToClipboard(\'' + linkIKKMOPED + '\');"><img src="/graphics/ic_link.svg" alt="Copy link to clipboard"></button>',
139			'</div></li>',
140			/* JO */
141			'<li data-site="JO"><div class="link_item">',
142			'<a href="' + linkJO + '">Jägarexamen Online</a>',
143			'<button title="Copy link to clipboard" onclick="copyReferralLinkToClipboard(\'' + linkJO + '\');"><img src="/graphics/ic_link.svg" alt="Copy link to clipboard"></button>',
144			'</div></li>',
145			'</ul>',
146			'<div><strong>TIP!</strong> You can also enter your friend\'s email address below and send a ready-made email with all the links and a personal greeting.</div>',
147			'<div><form id="referral_info_form">',
148			'<label for="referral_info_email_input">Send to email address: <span class="required_indicator">*</label>',
149			'<input type="email" placeholder="Enter your friend\'s email address" id="referral_info_email_input" name="referral_info_email_input" required>',
150			'<label for="referral_info_name_input">Your name:</label>',
151			'<input type="text" placeholder="Enter your name (optional)" id="referral_info_name_input" name="referral_info_name_input">',
152			'<label for="referral_info_message_input">Message:</label>',
153			'<textarea id="referral_info_message_input" name="referral_info_message_input" rows="4" placeholder="Enter a greeting for your friend (optional)"></textarea>',
154			'<button id="submit_referral_info_form_button" class="standard_button">Send information</button>',
155			'</form></div>',
156		].join('');
157	}
158	else {
159		title = 'Värva en vän och få gratis medlemskapstid';
160
161		message = [
162			'<div>För varje vän du värvar till ' + siteName + ' eller till någon av våra andra onlineutbildningar får du <strong>10 dagars gratis medlemskap</strong>. Din vän får <strong>20% rabatt på sitt medlemskap</strong>. En riktigt bra deal för alla!<br/><br/></div>',
163			'<div>Din unika kod</div>',
164			'<h3 style="margin-top: 0.25em;">',userReferralsData.code,'</h3>',
165			'<div>Skicka din kod till vänner – eller dela rabattlänkarna nedan. Länkarna går direkt till registreringssidorna med <strong>20% rabatt på alla priser</strong>.</div>',
166			'<ul class="link_list">',
167			/* IKK (SV) */
168			'<li data-site="IKK"><div class="link_item">',
169			'<a href="' + linkIKK_SV + '">iKörkort.nu: B-behörighet (Bil) - Svenska</a>',
170			'<button title="Kopiera länk till urklipp" onclick="copyReferralLinkToClipboard(\'' + linkIKK_SV + '\');"><img src="/graphics/ic_link.svg" alt="Kopiera länk till urklipp"></button>',
171			'</div></li>',
172			/* IKK (EN) */
173			'<li data-site="IKK_EN"><div class="link_item">',
174			'<a href="' + linkIKK_EN + '">iKörkort.nu: B-behörighet (Bil) - English</a>',
175			'<button title="Kopiera länk till urklipp" onclick="copyReferralLinkToClipboard(\'' + linkIKK_EN + '\');"><img src="/graphics/ic_link.svg" alt="Kopiera länk till urklipp"></button>',
176			'</div></li>',
177			/* IKKBE */
178			'<li data-site="IKKBE"><div class="link_item">',
179			'<a href="' + linkIKKBE + '">iKörkortBE.se: BE-behörighet (Bil med tungt släp)</a>',
180			'<button title="Kopiera länk till urklipp" onclick="copyReferralLinkToClipboard(\'' + linkIKKBE + '\');"><img src="/graphics/ic_link.svg" alt="Kopiera länk till urklipp"></button>',
181			'</div></li>',
182			/* IKKMC */
183			'<li data-site="IKKMC"><div class="link_item">',
184			'<a href="' + linkIKKMC + '">iKörkortMC.se: A-behörighet (Motorcykel)</a>',
185			'<button title="Kopiera länk till urklipp" onclick="copyReferralLinkToClipboard(\'' + linkIKKMC + '\');"><img src="/graphics/ic_link.svg" alt="Kopiera länk till urklipp"></button>',
186			'</div></li>',
187			/* IKKMOPED */
188			'<li data-site="IKKMOPED"><div class="link_item">',
189			'<a href="' + linkIKKMOPED + '">iKörkortMoped.se: AM-behörighet (Moped)</a>',
190			'<button title="Kopiera länk till urklipp" onclick="copyReferralLinkToClipboard(\'' + linkIKKMOPED + '\');"><img src="/graphics/ic_link.svg" alt="Kopiera länk till urklipp"></button>',
191			'</div></li>',
192			/* JO */
193			'<li data-site="JO"><div class="link_item">',
194			'<a href="' + linkJO + '">Jägarexamen Online</a>',
195			'<button title="Kopiera länk till urklipp" onclick="copyReferralLinkToClipboard(\'' + linkJO + '\');"><img src="/graphics/ic_link.svg" alt="Kopiera länk till urklipp"></button>',
196			'</div></li>',
197			'</ul>',
198			'<div><strong>TIPS!</strong> Du kan också fylla i din väns e-postadress här nedanför och skicka ett färdigt mejl med alla länkar och en personlig hälsning.</div>',
199			'<div><form id="referral_info_form">',
200			'<label for="referral_info_email_input">Skicka till e-postadress: <span class="required_indicator">*</label>',
201			'<input type="email" placeholder="Fyll i din väns e-postadress" id="referral_info_email_input" name="referral_info_email_input" required>',
202			'<label for="referral_info_name_input">Ditt namn:</label>',
203			'<input type="text" placeholder="Fyll i ditt namn (valfritt)" id="referral_info_name_input" name="referral_info_name_input">',
204			'<label for="referral_info_message_input">Meddelande:</label>',
205			'<textarea id="referral_info_message_input" name="referral_info_message_input" rows="4" placeholder="Skriv en hälsning till din vän (valfritt)"></textarea>',
206			'<button id="submit_referral_info_form_button" class="standard_button">Skicka</button>',
207			'</form></div>',
208		].join('');
209	}
210
211	$.confirmUserReferrals({
212		'title'		: title,
213		'message'	: message,
214	});
215
216	setReferallInfoFormListener();
217
218	// move links to the current site to the top of the links list
219	if (siteCode !== 'IKK') {
220		const $currentSiteLink = $('#confirmBox').find('li[data-site="' + siteCode + '"]');
221		if ($currentSiteLink.length !== 0) {
222			$currentSiteLink.prependTo($currentSiteLink.parent());
223		}
224	}
225}
226
227function setReferallInfoFormListener() {
228	const $form = $('#referral_info_form');
229	if ($form.length === 0) {
230		return;
231	}
232
233	$form.on('submit', function(event) {
234		event.preventDefault();
235
236		const EMAIL_VALIDATE_EXPRESSION = /^[a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,6}$/;
237
238		const $emailInput = $('#referral_info_email_input');
239		if ($emailInput.length > 0 && $emailInput.val().length > 0 && EMAIL_VALIDATE_EXPRESSION.test($emailInput.val())) {
240			const email = $emailInput.val();
241
242			let name;
243			const $nameInput = $('#referral_info_name_input');
244			if ($nameInput.length > 0) {
245				// validate name is not empty whitespace
246				const NAME_VALIDATE = $nameInput.val().trim().length > 0;
247
248				if (NAME_VALIDATE) {
249					// Basic sanitization of the name
250					name = $nameInput.val().replace(/</g, "&lt;").replace(/>/g, "&gt;");
251				}
252			}
253
254			let message;
255			const $messageInput = $('#referral_info_message_input');
256			if ($messageInput.length > 0) {
257				// validate message is not empty whitespace
258				const MESSAGE_VALIDATE = $messageInput.val().trim().length > 0;
259
260				if (MESSAGE_VALIDATE) {
261					// Basic sanitization of the message
262					message = $messageInput.val().replace(/</g, "&lt;").replace(/>/g, "&gt;");
263				}
264			}
265
266			const params = getUserReferralRequestParams();
267			params.action = 'emailInfo';
268			params.infoEmail = email;
269			params.infoName = name && name.length > 0 ? name : null;
270			params.infoMessage = message && message.length > 0 ? message : null;
271			params.language = language;
272
273			const $formButton = $('#submit_referral_info_form_button')
274				.text(language === 'en' ? 'Sending email ...' : 'E-post skickas ...')
275				.addClass('disabled');
276
277			showLoader();
278			const $loader = $('#loader_container').addClass('front');
279
280			$.post('/bw_common/php/user_referrals/user_referral_utils.php', params, function(response) {
281				hideLoader();
282				$loader.removeClass('front');
283				const data = JSON.parse(response);
284
285				$emailInput.val('');
286				$messageInput.val('');
287				$formButton
288					.text(language === 'en' ? 'Email information' : 'Skicka information')
289					.removeClass('disabled');
290
291				if (data.success === true && data.error === 'none') {
292					let successMessage;
293					if (language === 'en') {
294						successMessage = 'Success! Your referral information has been shared with ' + email + '. You will receive 10 free days membership if they join one of our online courses.';
295					} else {
296						successMessage = 'Klart! Ett e-postmeddelande med länkar och instruktioner har skickats till ' + email + '. Om din vän blir medlem på någon av våra onlineutbildningar förlängs ditt medlemskap automatiskt med 10 dagar.';
297					}
298					alert(successMessage);
299				} else {
300					let errorMessage;
301					if (language === 'en') {
302						errorMessage = 'An error occurred. Your email was not sent. Please try again.';
303					} else {
304						errorMessage = 'Ett fel inträffade. Ditt email skickades inte. Vänligen försök igen.';
305					}
306					alert(errorMessage);
307				}
308			});
309		} else {
310			const errorMessage = language === 'en' ? 'Please enter a valid email address' : 'Vänligen ange en giltig e-postadress.';
311			alert(errorMessage);
312		}
313	});
314}
315
316function copyReferralLinkToClipboard(linkUrl) {
317	// Create a temporary textarea to copy the link URL
318	const tempTextarea = $('<textarea>');
319	$('body').append(tempTextarea);
320	tempTextarea.val(linkUrl).select();
321	document.execCommand('copy');
322	tempTextarea.remove();
323
324	// inform the user that the text has been copied
325	const message = language === 'en' ? 'The link has been copied to your clipboard. Share it with your friends!' : 'Länken har kopierats till urklipp. Dela den med dina vänner!';
326	alert(message);
327}
328
329// checks if referral parameters received on signup page are valid (called by signup.js)
330function checkUserReferralSignupParams(refCode, refSite) {
331
332	showLoader();
333
334	const params = {
335		when: new Date().getTime(),
336		action: 'checkSignupParams',
337		refSite: refSite,
338		refCode: refCode
339	};
340
341	const deferred = $.Deferred();
342
343	$.post('/bw_common/php/user_referrals/user_referral_utils.php', params, function(response) {
344		hideLoader();
345		const data = JSON.parse(response);
346		deferred.resolve(data);
347	});
348
349	return deferred.promise();
350}
351

Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.