landing.js 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366
  1. // Shorthand for $( document ).ready()
  2. $(function() {
  3. /*---------------------------------------------------
  4. CHECKING THE URL FOR PARAMETERS (USER REDIRECTION)
  5. ------------------------------------------------------*/
  6. /*Get the dynamic variables stored in the url as parameters and store them as JavaScript variables ready for use in the code:*/
  7. // example.com?param1=name&param2=&id=6
  8. // $.urlParam('param1'); // name
  9. // $.urlParam('id'); // 6
  10. // $.urlParam('param2'); // null
  11. $.urlParam = function(name) {
  12. var results = new RegExp('[\?&]' + name + '=([^&#]*)').exec(window.location
  13. .href);
  14. // user is not redirected from the email activation link
  15. if (results === null) {
  16. return null;
  17. }
  18. // user is redirected from the email activation link
  19. else {
  20. return results[1] || 0;
  21. }
  22. };
  23. /*-------- USER EMAIL ACTIVATED OR NOT ---------*/
  24. /* When activation.php returns the varification variable: (redirecting the user to the page)*/
  25. //store the parameter in a variable
  26. var emailVerification = $.urlParam('verification');
  27. /*verification sucessful, parameter is true*/
  28. if (emailVerification !== null && emailVerification === 'true') {
  29. //create an html for the activation fail message
  30. var verificationMessage_true =
  31. "<div class='success-activation'><p>Great! Your email is activated. You can now log in!</p><p><a href='#log-in-pill'>Log in</a> </p></div>";
  32. $('header').append(verificationMessage_true);
  33. /*clicking on the login will send the user to the log in area
  34. the user does not nesseccarily want to log in at this point (optional)
  35. They want to continue reading the website or try online anyway*/
  36. //have the log in pill field activated
  37. $('.nav-tabs a[href="#login-tab"]').tab('show'); // Select tab by name
  38. /*verification failed, parameter is false*/
  39. } else if (emailVerification !== null && emailVerification === 'false') {
  40. //create an html for the activation fail message
  41. var verificationMessage_false =
  42. "<div class='failed-activation'><p>Sorry, we couldn't activate your email :(</p><p>Please contact us at <a href='mailto:hello@ethicscanvas.org'>hello@ethicscanvas.org</a> and we will fix it for you.</p></div>";
  43. $('header').append(verificationMessage_false);
  44. }
  45. /*This is for testing*/
  46. if (emailVerification === null) {
  47. }
  48. /* -------USER PASSWORD RESET OR NOT ----------*/
  49. /* When reset.php returns the password reset variable: (redirecting the user to the page)*/
  50. //changed=true
  51. var passwordReset = $.urlParam('changed');
  52. if (passwordReset !== null && passwordReset === 'true') {
  53. //create an html for the "password changed" message
  54. var passwordResetMessage_true =
  55. "<div class='success-activation'><p>Great! Your password has been changed. You can now log in!</p><p><a href='#log-in-pill'>Log in</a> </p></div>";
  56. $('header').append(passwordResetMessage_true);
  57. /*clicking on the login will send the user to the log in area
  58. the user does not nesseccarily want to log in at this point (optional)
  59. They want to continue reading the website or try online anyway*/
  60. //have the log in pill field activated
  61. $('.nav-tabs a[href="#login-tab"]').tab('show'); // Select tab by name
  62. /*verification failed, parameter is false*/
  63. } else if (passwordReset !== null && passwordReset === 'false') {
  64. //create an html for the activation fail message
  65. var passwordResetMessage_false =
  66. "<div class='failed-activation'><p>Sorry, We couldn't change your password :(</p><p>Please contact us at <a href='mailto:hello@ethicscanvas.org'>hello@ethicscanvas.org</a> and we will fix it for you.</p></div>";
  67. $('header').append(passwordResetMessage_false);
  68. }
  69. /*This is for testing*/
  70. if (passwordReset === null) {
  71. }
  72. //end of password reset change verification
  73. /*-----------------------------------------------
  74. SLICK CAROUSEL SETTING
  75. -----------------------------------------------*/
  76. $('.slickCarousel').slick({
  77. autoplay: true,
  78. autoplaySpeed: 4500,
  79. dots: true,
  80. infinite: true,
  81. fade: true,
  82. cssEase: 'linear'
  83. });
  84. /*-----------------------------------------------
  85. EMAIL VALIDATION
  86. -----------------------------------------------*/
  87. function ValidateEmail(email) {
  88. var expr =
  89. /^([\w-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([\w-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$/;
  90. return expr.test(email);
  91. }
  92. /* -------------------------------------------
  93. SIGN UP FORM SUBMISSION & SERVER FEEDBACK
  94. ---------------------------------------------*/
  95. //A. Submitting sign up
  96. $('.sign-up-form').on('submit', function(event) {
  97. //prevent the form from getting submit if the passwords don't match or the email is invalid
  98. var pass1 = $('#password-signup').val();
  99. var pass2 = $('#password-signup-conf').val();
  100. if (!ValidateEmail($("#email-signup").val()) || pass1 !== pass2) {
  101. event.preventDefault();
  102. } else {
  103. var url = "php/sign-up.php";
  104. var sign_up_data = $(this).serialize();
  105. /* ----------------------------
  106. Post the serialized form data
  107. to the sign-up.php file
  108. -----------------------------*/
  109. $.post(url, {
  110. sign_up_data: sign_up_data
  111. }, function(data, status) {
  112. // HANDLE sign up SERVER RESPONSE
  113. if (data == 400) {
  114. $('.sign-up-in').find('.form-signup-feedback ').addClass(
  115. 'success-feedback').html(
  116. '<p>Something went wrong! Please contact us at hello@ethicscanvas.org.</p>'
  117. );
  118. /*user is already registered*/
  119. } else if (data == 401) {
  120. $('.sign-up-in').find('.form-login-feedback ').addClass(
  121. 'info-feedback').html(
  122. '<p>You are already registered. Please log in!</p>');
  123. //have the log in pill field activated
  124. $('.nav-tabs a[href="#login-tab"]').tab('show'); // Select tab by name
  125. /*put the already entered email in the email input
  126. of the login form*/
  127. //the email that the existing user tried to sign up with
  128. var user_email = $('.sign-up-form').find('#email-signup')
  129. .val();
  130. //put the email in the login form
  131. $('.log-in-form').find('#email-login').val(user_email);
  132. } else if (data == 201) {
  133. $('.sign-up-in').find('.form-signup-feedback ').addClass(
  134. 'success-feedback').html(
  135. '<p>Thank you! Please check your email <span>' + $(
  136. "#email-signup").val() +
  137. '</span> and activate your account.</p>');
  138. }
  139. }).fail(function(jqXHR) {
  140. console.log("Fail Error -> " + jqXHR.status + ' ' + jqXHR
  141. .statustext);
  142. });
  143. /* Because the type of the button is submit */
  144. return false;
  145. } //end of else
  146. }); // end of handling the click on THE Sign Up button
  147. /* -------------------------------------------
  148. LOGIN FORM SUBMISSION & SERVER FEEDBACK
  149. ---------------------------------------------*/
  150. $('.log-in-form').on('submit', function(event) {
  151. var url = "php/log-in.php";
  152. var sign_in_data = $(this).serialize();
  153. /* ---------------------------
  154. Post the serialized form data
  155. to the log-in.php file
  156. -----------------------------*/
  157. $.post(url, {
  158. sign_in_data: sign_in_data
  159. }, function(data, status) {
  160. //Handling LOGIN form feedback from the server
  161. if (data == 400) {
  162. $('.sign-up-in').find('.form-login-feedback').addClass(
  163. 'warning-feedback').html(
  164. '<p>Something went wrong! Please contact us at hello@ethicscanvas.org.</p>'
  165. );
  166. /*user is already registered*/
  167. } else if (data == 401) {
  168. $('.sign-up-in').find('.form-login-feedback').addClass(
  169. 'warning-feedback').html(
  170. '<p>The username or password is incorrect. Please try again!</p>'
  171. );
  172. } else if (data == 200) {
  173. $('.sign-up-in').find('.form-login-feedback ').addClass(
  174. 'success-feedback').html(
  175. '<p>Great! Going to the canvas now...</p>');
  176. /*Send the user to their canvas dashbord*/
  177. window.location.href = 'canvas/php/dashboard.php';
  178. } else if (data == 402) {
  179. $('.sign-up-in').find('.form-login-feedback').addClass(
  180. 'warning-feedback').html(
  181. '<p>You already have an account. Please activate it in the email we sent you.</p><button class="resend-activation">Send me the activation email again</button>'
  182. );
  183. }
  184. }).fail(function(jqXHR) {
  185. console.log("Error " + jqXHR.status + ' ' + jqXHR.statustext);
  186. });
  187. /* Because the type of the button is submit */
  188. return false;
  189. }); // end of handling the click on THE Sign Up button
  190. /*------------#Resend The Activation Email --------*/
  191. $('.sign-up-in').find('.form-login-feedback').on("click",
  192. '.resend-activation',
  193. function() {
  194. var url = 'php/resend-activation.php';
  195. var email_resend = $('.log-in-form').find('#email-login').serialize();
  196. $.post(url, {
  197. email_resend: email_resend
  198. }, function(data, status) {
  199. console.log(
  200. 'User clicked on resend activation email: \n' +
  201. 'RESPONSE from resend-activation.php => \n' +
  202. 'data:' + data + '\n status: ' + status);
  203. }); //end of post
  204. }); //end of on click for resending activation
  205. /*------------#Forgot password --------*/
  206. $('.log-in-form').find('.forgot-password').on("click", 'a',
  207. function(e) {
  208. e.preventDefault();
  209. //have the reset password pill field activated
  210. $('.nav-tabs a[href="#reset-password-tab"]').tab('show'); // Select tab by name
  211. //get the value of the email input field in the login form
  212. var email_to_reset = $('.log-in-form').find('#email-login').val();
  213. /*if the user already entered an email address here, and it's a valid email*/
  214. /* put that email address in the input field of the password reset*/
  215. if (email_to_reset !== null && ValidateEmail($("#email-login").val())) {
  216. $('.reset-password-form').find('#email-reset-password').val(
  217. email_to_reset);
  218. }
  219. }); //end of click on forgot the password link
  220. /*--------------- #Reset password ----------------*/
  221. $('.reset-password-form').on('submit', function() {
  222. var url = 'php/reset-password.php';
  223. var reset_password = $('.reset-password-form').find(
  224. '#email-reset-password').serialize();
  225. $.post(url, {
  226. reset_password: reset_password
  227. }, function(data, status) {
  228. if (data == 400) {
  229. $('.sign-up-in').find('.form-reset-password-feedback').addClass('warning-feedback').html('<p>Something is not right. :/ Please contact us at hello@thicscanvas.org</p>');
  230. } else if (data == 401) {
  231. $('.sign-up-in').find('.form-reset-password-feedback').addClass('warning-feedback').html('<p>Please enter a correct email address.</p>');
  232. } else if (data == 200) {
  233. $('.sign-up-in').find('.form-reset-password-feedback').addClass('success-feedback').html('<p>Thank you! :) We sent you an email to reset your password</p>');
  234. }
  235. }); //end of post
  236. return false;
  237. });
  238. /* -------------------------------------------
  239. Inline FRONT END FORM VALIDATION
  240. ---------------------------------------------*/
  241. /* ------- Validating the sign up form ------------ */
  242. $(".sign-up-form").on("click", '.sign-up', function() {
  243. var user_name = $('#firstname-signup').val();
  244. if (user_name === '') {
  245. $('#name-register-message').addClass('message-field').text(
  246. "Please enter your name.");
  247. } else {
  248. $('#name-register-message').removeClass('message-field').text(
  249. "");
  250. }
  251. if (!ValidateEmail($("#email-signup").val())) {
  252. $('#email-register-message').addClass('message-field').text(
  253. 'Please enter a valid email address.');
  254. } else {
  255. $('#email-register-message').removeClass('message-field')
  256. .text(
  257. "");
  258. }
  259. var pass1 = $('#password-signup').val();
  260. var pass2 = $('#password-signup-conf').val();
  261. if (pass1 === '') {
  262. $('#pass-register-message').addClass('message-field').text(
  263. "Please enter a password.");
  264. }
  265. if (pass2 === '') {
  266. $('#pass-conf-register-message').addClass('message-field')
  267. .text(
  268. "Please confirm your password.");
  269. }
  270. if (pass1 !== '' && pass2 !== '' && pass1 !== pass2) {
  271. $('#pass-register-message, #pass-conf-register-message').addClass(
  272. 'message-field').text(
  273. "Passwords don't match.");
  274. }
  275. if (pass1 !== '' && pass2 !== '' && pass1 === pass2) {
  276. $('#pass-register-message, #pass-conf-register-message').removeClass(
  277. 'message-field').text(
  278. "");
  279. }
  280. });
  281. /* -------- Validating the log in form ---------- */
  282. $(".log-in-form").on("click", '.log-in', function() {
  283. if (!ValidateEmail($("#email-login").val())) {
  284. $('#email-login-message').addClass('message-field').text(
  285. 'Please enter a valid email address.');
  286. }
  287. var pass1b = $('#password-login').val();
  288. if (pass1b === '') {
  289. $('#pass-login-message').addClass('message-field').text(
  290. "Please enter your password.");
  291. }
  292. });
  293. /* -------- Validating the password reset form ---------- */
  294. $('.reset-password-form').on('click', '#reset-password-btn', function() {
  295. // if the email input field is empty
  296. if ($('.reset-password-form').find(
  297. '#email-reset-password').val() === '') {
  298. $('.reset-password-form').find('#reset-password-message').addClass('message-field').text('Please enter your email.');
  299. }
  300. }); // end of click on reset
  301. }); // end of jQuery file