/* Site specific Javascript code */

(function($) { $(document).ready(function() {
   // Pages with sign in interface
   if ($("div.sign-in-interface").length) {
      setUpSignInInterfaces();
   }
});})(jQuery);


// Add new function declarations specific to one Tait mini site here
function setUpSignInInterfaces() {

   // If user presses the Enter key, either click the Get Access button or the Sign In button depending on which input element currently has the focus
   $('input.email-address').keypress(function(e){ if (e.which == 13) { $("input.get-access-button").click(); } });
   $('input.username, input.password').keypress(function(e){ if (e.which == 13) { $("input.sign-in-button").click(); } });

   // When 'Access this xxx' link is clicked, drop down the Sign in interface and show the Close button
   $("a.open-sign-in-interface").click(function() {
      var signInWrapper = $(this).parents("div.sign-in-interface-wrapper");
      signInWrapper.find("a.close-sign-in-interface").show(); signInWrapper.find("div.sign-in-interface").slideDown();
   });

   // When the Close button is clicked, hide the Sign in interface and the Close button
   $("a.close-sign-in-interface").click(function() {
      $(this).hide();
      var signInWrapper = $(this).parents("div.sign-in-interface-wrapper");
      signInWrapper.find("div.sign-in-interface").slideUp();
   });

   // When the Get Access button is clicked, send the email address to Tait via a Custom Form
   $("input.get-access-button").click(function() {

      var formName = "SQ_FORM_43413_PAGE";
      var formPage = "1";
      var formSubmitName = "form_email_43413_submit";
      var formSubmitVal = "Submit";
      var ajaxQuery = "&" + formName + "=" + formPage + "&" + formSubmitName + "=" + formSubmitVal;
      var emailAddress = $(this).parents("form").find("input.email-address").attr("value");
      var serializedData = "q43413:q1=" + emailAddress;
      var signInWrapper = $(this).parents("div.sign-in-interface-wrapper");

      // Client side check for valid email address
      if (isValidEmail(emailAddress)) {
         $.post("/guest-user-custom-form", serializedData + ajaxQuery, function(data) {
            // Server side check to see if custom form returned any errors (invalid email address)
            if (data.indexOf('Question "EmailAddress" is a required field; it must be filled in') > -1 ) {
               signInWrapper.find("div.new-visitors-error").text("Invalid email address");
            }
            else {
               signInWrapper.find("div.new-visitors-error").text("");
               // Now sign the user in using the Guest user account
               signInWrapper.find("input.username").attr("value", "pubUser");
               signInWrapper.find("input.password").attr("value", "publicsafety");
               signInWrapper.find("input.sign-in-button").click();
            }
         });
      }
      else {
         signInWrapper.find("div.new-visitors-error").text("Invalid email address");
      }

   });

   // When the Sign in button is clicked, populate the hidden login form with the username and password and then click the Login button
   $("input.sign-in-button").click(function() {
      var signInWrapper = $(this).parents("div.sign-in-interface-wrapper");
      var username = signInWrapper.find("input.username").attr("value");
      var password = signInWrapper.find("input.password").attr("value");
      $("#SQ_LOGIN_USERNAME").attr("value", username);
      $("#SQ_LOGIN_PASSWORD").attr("value", password);
      $("#log_in_out_button").click();
   });

   // If there are any errors logging in, show these on the form
   var loginErrors = $("#login-form-errors").text();
   if (loginErrors.length) {
      $("div.registered-users-error").text(loginErrors);
   }

   // Ensure height of new-visitors and registered-users divs are the same. The height should be set to the maximum of the two
   var newVisitors = $("div.new-visitors:first");
   var registeredUsers = $("div.regisitered-users:first");
   var newVisitorsHeight = newVisitors.css("height");
   var registeredUsersHeight = registeredUsers.css("height");
   if (newVisitorsHeight > registeredUsersHeight) {
      $(registeredUsers).css("height", newVisitorsHeight);
   }
   else {
      $(newVisitors).css("height", registeredUsersHeight);
   }

}  // end of setUpSignInInterfaces()

function isValidEmail(emailAddress) {
   var pattern = new RegExp(/^(("[\w-\s]+")|([\w-]+(?:\.[\w-]+)*)|("[\w-\s]+")([\w-]+(?:\.[\w-]+)*))(@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$)|(@\[?((25[0-5]\.|2[0-4][0-9]\.|1[0-9]{2}\.|[0-9]{1,2}\.))((25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\.){2}(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\]?$)/i); return pattern.test(emailAddress);
}  // end isValidEmail
