/*
    Notifications
 */

const messaging = firebase.messaging();
messaging.requestPermission()
.then(function () {
    console.log("Have permission");
    if ('serviceWorker' in navigator) {
        navigator.serviceWorker.register('/firebase-messaging-sw.js')
        .then(function(registration) {
            console.log('Registration successful, scope is:', registration.scope);
        }).catch(function(err) {
            console.log('Service worker registration failed, error:', err);
        });
    }
});

messaging.onMessage((payload) => {
    appendMessage(payload);
});

function appendMessage(payload){
    const messagesElement = document.getElementById("messages");
    const dataHeaderElement = document.createElement("h4");
    const dataElement = document.createElement("pre");
    dataHeaderElement.textContent = payload.notification.title;
    dataElement.textContent = payload.notification.body;

    messagesElement.appendChild(dataHeaderElement);
    messagesElement.appendChild(dataElement);
}


/*
    Location
 */

var position;

function successCallback(pos){
    position = pos;
    console.log(pos);
}

function errorCallback(err){
    console.log("error");
}

function getLocation() {
    if(navigator.geolocation){
        navigator.geolocation.getCurrentPosition(successCallback,errorCallback,{timeout:10000, enableHighAccuracy:false});
    }
    else{
        console.log("Error. Geolocation not supported or not enabled");
    }
    return;
}

/*
    Setting password 
*/ 
$('#pass').click(function(){
    // activates the password requirments 
    showRequirements();
});

function showRequirements(){
    console.log("show requirements");
}

// Check if the password meets the password requirement 
$('#pass').on('keyup', function(){
    number();
});

// Checks if the password contains a number
function number(){
    var number = /[0-9]/;
    const password = document.getElementById("pass");
    if(password.value.match(number)){
        $('#pwNumber').addClass('alert-success');
        console.log("number")
    } 
    else{
        $('#pwNumber').removeClass('alert-success');
    }
}

// Check for password confirmation
$('#pass, #passconf').on('keyup', function(){
    if ((pass.value.length != 0) && (passconf.value.length != 0)){
        if ($('#pass').val() == $('#passconf').val()){
            $('#btnSignUp').prop('disabled', false);
            $('#pass').css('border-bottom','2px solid #d1b280');
            $('#passconf').css('border-bottom','2px solid #d1b280');
        }else{
            // disable sign up button
            $('#btnSignUp').prop('disabled', true);
            // underline the inputs in red
            $('#pass').css('border-bottom','3px solid #fa8072');
            $('#passconf').css('border-bottom','3px solid #fa8072');
        }
    }
})


/*
    Chatroom sidebar
*/ 

// Hides submenus. Profile and chat lists are in different submenus and appear when its sidebar option is clicked.
$('#body-row .collapse').collapse('hide'); 

// Collapse/Expand icon
$('#collapse-icon').addClass('fa-angle-double-left'); 

// Collapse on click
$('[data-toggle = sidebar-colapse]').click(function() {
    SidebarCollapse();
});

// Currently hides the sidebar on smaller and medium screens (TODO: adjust screen for different screen sizes)
function SidebarCollapse () {
    $('.menu-collapsed').toggleClass('d-none');
    $('.sidebar-submenu').toggleClass('d-none');
    $('.submenu-icon').toggleClass('d-none');
    $('#sidebar-container').toggleClass('sidebar-expanded sidebar-collapsed');
    
    // Treating d-flex/d-none on separators with title
    var SeparatorTitle = $('.sidebar-separator-title');
    if ( SeparatorTitle.hasClass('d-flex') ) {
        SeparatorTitle.removeClass('d-flex');
    } else {
        SeparatorTitle.addClass('d-flex');
    }
    
    // Collapse/Expand icon
    $('#collapse-icon').toggleClass('fa-angle-double-left fa-angle-double-right');
}
@@ -1772,66 +1772,52 @@
     
-number();%0A%7D);%0A%0A// Checks if the password contains a number
+containsNumber();%0A    containsSymbol();%0A%7D);%0A
 %0Afun
@@ -1822,17 +1822,25 @@
 unction 
-n
+containsN
 umber()%7B
@@ -2012,38 +2012,8 @@
 ');%0A
-        console.log(%22number%22)%0A
     
@@ -2083,24 +2083,313 @@
 );%0A    %7D%0A%7D%0A%0A
+function containsSymbol()%7B%0A    var symbol = /%5B$-/:-?%7B-~!%22%5E_%60%5C%5B%5C%5D%5D/;%0A    const password = document.getElementById(%22pass%22);%0A    if(password.value.match(symbol))%7B%0A        $('#pwSymbol').addClass('alert-success');%0A    %7D %0A    else%7B%0A        $('#pwSymbol').removeClass('alert-success');%0A    %7D%0A%7D%0A%0A
 // Check for

check if the password contains a number