<reponame>Alpha-github/NUS_G5_Internship<filename>static/form.js<gh_stars>0const firebaseConfig = {
    apiKey: "caf86f4uutaoxfysmf7anj01xl6sv3ps",
    authDomain: "freshlite-pro.firebaseapp.com",
    databaseURL: "https://freshlite-pro-default-rtdb.firebaseio.com",
    projectId: "freshlite-pro",
    storageBucket: "freshlite-pro.appspot.com",
    messagingSenderId: "802369717905",
    appId: "1:802369717905:web:a5ea97173ccc990c416130"
  };
    
    // initialize firebase
    firebase.initializeApp(firebaseConfig);
    
    // reference your database
    var contactFormDB = firebase.database().ref("contactForm");
    
    document.getElementById("contactForm").addEventListener("submit", submitForm);
    
    function submitForm(e) {
      e.preventDefault();
    
      var name = getElementVal("name");
      var emailid = getElementVal("emailid");
      var msgContent = getElementVal("msgContent");
    
      saveMessages(name, emailid, msgContent);
    
      //   enable alert
      document.querySelector(".alert").style.display = "block";
    
      //   remove the alert
      setTimeout(() => {
        document.querySelector(".alert").style.display = "none";
      }, 3000);
    
      //   reset the form
      document.getElementById("contactForm").reset();
    }
    
    const saveMessages = (name, emailid, msgContent) => {
      var newContactForm = contactFormDB.push();
    
      newContactForm.set({
        name: name,
        emailid: emailid,
        msgContent: msgContent,
      });
    };
    
    const getElementVal = (id) => {
      return document.getElementById(id).value;
    };