aboutsummaryrefslogtreecommitdiff
path: root/website/resources/js/order-license.js
blob: 21550f347ece7b7bfb3220176cae634b75190d1b (plain)
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
"use strict";

(function() {
	if (!String.prototype.trim) {
		String.prototype.trim = function () {
			return this.replace(/^[\s\uFEFF\xA0]+|[\s\uFEFF\xA0]+$/g, '');
		};
	}
	
	var imgDataUrl = null;
	
	function updateLocationInfo() {
		var locationType = $("input:radio[name='locationType']:checked").val();
		$("#locationType_usa").toggle(locationType === "usa");
		$("#locationType_eu").toggle(locationType === "eu");
		$("#locationType_other").toggle(locationType === "other");
		$("#paymentMethod_iban").toggle(locationType === "eu");
		if ($("#paymentMethod_iban input").is(":checked") && locationType !== "eu") $("#paymentMethod_stripe input").prop("checked", true);
		if ($("#paymentMethod_intl input").is(":checked") && locationType === "eu") $("#paymentMethod_iban input").prop("checked", true);
		$("#paymentMethod_intl").toggle(locationType !== "eu");
	}
	
	function updatePriceIndication() {
		$("#onlyYearlyWarning").hide();
		var x = getPriceIndication();
		if (x === "") {
			$("#costIndicator").text("").hide();
		} else {
			$("#costIndicator").text(x).show();
		}
	}
	
	function getPriceIndication() {
		try {
			var seats = parseInt($("#seats").val());
			if (isNaN(seats) || seats < 1) return "";
			var licenseType = $("input:radio[name='licenseType']:checked").val();
			var costPer;
			if (licenseType === "enterprise") costPer = 5;
			else if (licenseType === "professional") costPer = 2;
			else return "";
			var periodRaw = $("input:radio[name='paymentType']:checked").val();
			if (periodRaw === "monthly") {
				if (costPer*seats < 50) {
					$("#onlyYearlyWarning").show();
					return "Your license will cost €" + (costPer*seats*12) + ",- per year.";
				}
				return "Your license will cost €" + (costPer*seats) + ",- per month.";
			}
			return "Your license will cost €" + (costPer*seats*12) + ",- per year.";
		} catch (e) {
			return "";
		}
	}
	
	function showError(elemName) {
		var elem = $("#" + elemName + "Err");
		elem.fadeIn();
		$("#" + elemName).on("change keyup", function() {
			elem.hide();
		});
	}
	
	function applyLicense() {
		var submitButton = $("#submit");
		var spinner = $("<img>").attr("src", "/img/spinner.gif").attr("alt", "submitting").css("float", "left").css("margin-right", "20px");
		
		submitButton.on("click", function(evt) {
			evt.preventDefault();
			
			var onSuccess = function() {
				$("#orderHelp").hide();
				var okMsg = $("<div>").addClass("formSubmitOk").html("Thank you for ordering a Project Lombok license! We'll send your bill via email. If you have any further questions please contact us at <a href=\"mailto:orders@projectlombok.org\"><code>orders@projectlombok.org</code></a>.");
				spinner.replaceWith(okMsg);
			};
			
			var onFailure = function() {
				$("#orderHelp").hide();
				var errMsg = $("<div>").addClass("formSubmitFail").html("Our order form appears to be broken. Could you do us a favour and contact us at <a href=\"mailto:orders@projectlombok.org\"><code>orders@projectlombok.org</code></a>? Thank you!");
				spinner.replaceWith(errMsg);
			};
			
			var data = {};
			data.name = $("#companyName").val();
			data.email = $("#email").val();
			data.licenseType = $("input:radio[name='licenseType']:checked").val();
			data.seats = parseInt($("#seats").val());
			data.paymentType = $("input:radio[name='paymentType']:checked").val();
			data.mentionMe = $("#mentionMe").prop("checked");
			data.companyUrl = $("#companyUrl").val();
			data.locationType = $("input:radio[name='locationType']:checked").val();
			data.euVat = $("#euVat").val();
			data.paymentMethod = $("input:radio[name='paymentMethod']:checked").val();
			
			var formFail = false;
			
			if (!data.paymentMethod) {
				showError("paymentMethod");
				formFail = true;
			}
			
			if (!data.euVat && data.locationType === 'eu') {
				$("#euVat").focus();
				showError("euVat");
				formFail = true;
			}
			
			if (!data.locationType) {
				showError("locationType");
				formFail = true;
			}
			
			if (!data.seats || isNaN(data.seats) || data.seats < 1) {
				$("#seats").focus();
				showError("seats");
				formFail = true;
			}
			
			if (!data.paymentType) {
				showError("paymentType");
				formFail = true;
			}
			
			if (data.email.indexOf('@') === -1) {
				$("#email").focus();
				showError("email");
				formFail = true;
			}
			
			if (data.name.trim().length < 1) {
				$("#companyName").focus();
				showError("companyName");
				formFail = true;
			}
			
			if (formFail) return;
			
			var rnd = generateRandom();
			var err = processImageUpload();
			if (err) {
				alert(err);
				$("#logo")[0].value = null;
				return;
			}
			if (imgDataUrl) data.logo = imgDataUrl;
			
			submitButton.replaceWith(spinner);
			
			$.ajax({
				url: "/license-submit/" + rnd,
				method: "PUT",
				contentType: "application/json",
				data: JSON.stringify(data),
				processData: false,
				success: onSuccess,
				error: onFailure
			});
		});
		
		$("#companyLogo").on("click", function(evt) {
			evt.preventDefault();
			$("#logo").click();
		});
		
		$("#deleteCompanyLogo").on("click", function(evt) {
			evt.preventDefault();
			$("#logo")[0].value = null;
			$("#logoCnt").empty().hide();
			$("#companyLogo").show();
			$("#deleteCompanyLogo").hide();
		});
		
		$("#logo").on("change", function() {
			var err = showImage();
			if (err) {
				alert(err);
				$("#logo")[0].value = null;
			} else {
				$("#companyLogo").hide();
				$("#deleteCompanyLogo").show();
			}
		});
		
		$("#seats,.paymentType,.licenseType").on("change keyup", function() {
			updatePriceIndication();
		});
		$(".locationType").on("change", function() {
			updateLocationInfo();
		});
	}
	
	function generateRandom() {
		var buf = new Uint8Array(40);
		window.crypto.getRandomValues(buf);
		return btoa(String.fromCharCode.apply(null, buf)).replace(/\+/g, '_').replace(/\//gi, '-');
	}
	
	function showImage() {
		$("#logoCnt").empty().hide();
		try {
			return processImageUpload(function(dataUrl) {
				var img = $("<img />");
				img.css({
					"max-width": "500px",
					"max-height": "300px"
				});
				$("#logoCnt").append(img).show();
				img.attr("src", dataUrl);
			});
		} catch (e) {
			if (console && console.log) console.log(e);
		}
	}
	
	function processImageUpload(fnc) {
		var f = $("#logo")[0].files[0];
		if (!f) return;
		if (f.size > 10000000) return "Logo too large; please give us a logo below 10MiB in size.";
		var imageType = /^image\//;
		if (!imageType.test(f.type)) return "Please upload an image, for example in PNG format.";
		var reader = new FileReader();
		reader.onload = function(e) {
			imgDataUrl = e.target.result;
			if (fnc) fnc(e.target.result);
		};
		reader.readAsDataURL(f);
		return null;
	}
	
	$(applyLicense);
})();