prelauncher.js
3.44 KB
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
var Prelauncher = function(company_id){
var rootUrl = "https://api.prelauncher.info/companies/" + company_id + "/clients/";
this.buildFirstPage = function(){
jQuery('html').attr('style', function(i,s) { return (s || '') + 'margin-top: 0 !important;' });
addHiddenTags();
onSubmitFormHandler();
}
function onSubmitFormHandler(){
jQuery("form").on("submit", function(e){
e.preventDefault();
jQuery.ajax({
type: "POST",
url: rootUrl,
dataType: "json",
'beforeSend' : function(xhr){
xhr.setRequestHeader("Accept", "application/json")
},
data: jQuery(this).serialize(),
success: function(data) {
createClientCallback(data);
},
error: function(data) {
errorMesage(data["responseText"]);
}
});
});
}
function GetURLParameter(sParam) {
var sPageURL = window.location.search.substring(1);
var sURLVariables = sPageURL.split('&');
for (var i = 0; i < sURLVariables.length; i++) {
var sParameterName = sURLVariables[i].split('=');
if (sParameterName[0] == sParam) {
return sParameterName[1];
}
}
}
function addHiddenTags(){
var referralId;
if (referralCode = GetURLParameter("ref")){
addHiddenTag("referral_code", referralCode);
}
if (document.referrer){
addHiddenTag("traffic_source", document.referrer);
}
}
function addHiddenTag(name, value) {
jQuery('<input>').attr('type','hidden').attr('name', "client[" + name + "]").attr('value', value).appendTo('form');
}
function errorMesage(message){
if (jQuery(".alert-danger").length > 0){
jQuery(".alert-danger").text(message)
} else {
jQuery("body").prepend("<div class='alert alert-danger'>" + message + "</div>" );
}
}
function createClientCallback(data){
var url;
if (url = data["clients"]["client_url"]){
window.location.replace(url);
}
}
function apiCall(url, callback){
jQuery.ajax({
url: url,
cache: false,
contentType: "application/json; charset=utf-8",
dataType: "json",
'beforeSend' : function(xhr){
xhr.setRequestHeader("Accept", "application/json")
},
success: function(data) {
callback(data);
}, error: function(data) {
console.log(data);
}
});
}
function clientURL(referralCode){
return rootUrl + referralCode;
}
function shareURL(referralCode, service){
return clientURL(referralCode) + "/shares?share%5Bsocial_network%5D=" + service
}
this.buildSecondPage = function(referralCode){
jQuery('html').attr('style', function(i,s) { return (s || '') + 'margin-top: 0 !important;' });
apiCall(clientURL(referralCode), function(data){
var client = data["clients"];
if (data["prize_id"]){
jQuery(".prize[data-prize_id=" + client["prize_id"] + "]").addClass("active-prize")
}
jQuery("strong > a").text(client["number_of_referrals"] + " friends ").attr("href", clientURL(referralCode) + "/referrals");
jQuery("[name=referral_link]").val(client["referral_url"]);
modifySocialLinks(referralCode);
jQuery(".ico-mail").attr("href", jQuery(".ico-mail").attr("href") + " " + client["referral_url"]);
jQuery(".progressbar").css("width", client['progress'] * 100 + "%");
});
}
function modifySocialLinks(referralCode){
jQuery(".foo_social").not(".ico-mail").each(function() {
var service = jQuery(this).data("service");
jQuery(this).attr("href", shareURL(referralCode, service));
});
}
}