// Initialize Firebase (ADD-admin-YOUR-noeld-OWN-degamo-DATA-com)
var firebaseConfig = {
    apiKey: "AIzaSyCMEVyp6H0Bv2uaiXWvtt93cqnUnTglbAo",
    authDomain: "erdana-bhert.firebaseapp.com",
    databaseURL: "https://erdana-bhert.firebaseio.com",
    projectId: "erdana-bhert",
    storageBucket: "erdana-bhert.appspot.com",
    messagingSenderId: "494181183339",
    appId: "1:494181183339:web:f3eba9e063add6401be62e",
    measurementId: "G-72B4SPT63Q"
};


// Initialize Firebase
firebase.initializeApp(firebaseConfig);

// Reference mindacommodities collection
var mindacommoditiesRef = firebase.database().ref('erdanabhert');

// Listen for form submit
document.getElementById('contactForm').addEventListener('submit', submitForm);

// Submit form
function submitForm(e) {
    e.preventDefault();

    // Get values 
    var region = getInputVal('region');
    var province = getInputVal('province');
    var citymun = getInputVal('citymun');
    var cough = getInputVal('cough');
    var sneezing = getInputVal('sneezing');
    var colds = getInputVal('colds');
    var loosebowmov = getInputVal('loosebowmov');
    var bodytemp = getInputVal('bodytemp');
    var bloodpress = getInputVal('bloodpress');
    var skincolor = getInputVal('skincolor');
    var swelling = getInputVal('swelling');
    var seizure = getInputVal('seizure');
    var complaints = getInputVal('complaints');
    var travelhistory = getInputVal('travelhistory');
    var location2 = getInputVal('location2');
    var datetravel = getInputVal('datetravel');

    // Save message 
    saveMessage(region, province, citymun, cough, sneezing, colds, loosebowmov, bodytemp, bloodpress, skincolor,
        swelling, seizure, complaints, travelhistory, location2, datetravel);

    // Show alert 
    document.querySelector('.alert').style.display = 'block';

    // Hide alert after 3 seconds 
    setTimeout(function() {
        document.querySelector('.alert').style.display = 'none';
    }, 3000);

    // Clear form 
    document.getElementById('contactForm').reset();


}

// Function to get get form values
function getInputVal(id) { return document.getElementById(id).value; }

// Save message to firebase
function saveMessage(region, province, citymun, cough, sneezing, colds, loosebowmov, bodytemp, bloodpress, skincolor,
    swelling, seizure, complaints, travelhistory, location2, datetravel) {
    var newMessageRef = mindacommoditiesRef.push();
    newMessageRef.set({
        region: region,
        province: province,
        citymun: citymun,
        cough: cough,
        sneezing: sneezing,
        colds: colds,
        loosebowmov: loosebowmov,
        bodytemp: bodytemp,
        bloodpress: bloodpress,
        skincolor: skincolor,
        swelling: swelling,
        seizure: seizure,
        complaints: complaints,
        travelhistory: travelhistory,
        location2: location2,
        datetravel: datetravel
    });
}

//DYNAMIC
function populate(region, province, citymun) {
    var region = document.getElementById(region);
    var province = document.getElementById(province);
    var citymun = document.getElementById(citymun);

    province.innerHTML = "";
    if (region.value == "REGION X") {
        var optionArray = ["|", "Bukidnon|Bukidnon"];
        var optionArray2 = ["|", "Damulog|Damulog", "Don Carlos|Don Carlos", "Kibawe|Kibawe", "Kitaotao|Kitaotao", "Maramag|Maramag"];
    } else if (region.value == "REGION XI") {
        var optionArray = ["|", "Compostela Valley|Compostela Valley"];
        var optionArray2 = ["|", "Laak|Laak", "Monkayo|Monkayo", "Pantukan|Pantukan", "Montevista|Montevista"];
    } else if (region.value == "REGION XII") {
        var optionArray = ["|", "North Cotabato|North Cotabato"];
        var optionArray2 = ["|", "Kidapawan|Kidapawan", "Banisilan|Banisilan", "Kabacan|Kabacan", "Matalam|Matalam"];
    }






    for (var option in optionArray) {
        var pair = optionArray[option].split("|");
        var newOption = document.createElement("option");
        newOption.value = pair[0];
        newOption.innerHTML = pair[1];
        province.options.add(newOption);
    }

    for (var option in optionArray2) {
        var pair = optionArray2[option].split("|");
        var newOption = document.createElement("option");
        newOption.value = pair[0];
        newOption.innerHTML = pair[1];
        citymun.options.add(newOption);
    }
}












//DYNAMIC
// function populate(region, province, citymun) {
//     var region = document.getElementById(region);
//     var province = document.getElementById(province);
//     var citymun = document.getElementById(citymun);

//     province.innerHTML = "";
//     if (region.value == "REGION X") {
//         var optionArray = ["|", "Bukidnon|Bukidnon", "Camiguin|Camiguin", "Lanao del Norte|Lanao del Norte", "Misamis Occidental|Misamis Occidental", "Misamis Oriental|Misamis Oriental"];
//         var optionArray2 = ["|", "Damulog|Damulog", "Don Carlos|Don Carlos", "Kibawe|Kibawe", "Kitaotao|Kitaotao", "Maramag|Maramag"];
//     } else if (region.value == "REGION XI") {
//         var optionArray = ["|", "Compostela Valley|Compostela Valley", "Davao del Norte|Davao del Norte", "Davao del Sur|Davao del Sur", "Davao Oriental|Davao Oriental"];
//         var optionArray2 = ["|", "Laak|Laak", "Monkayo|Monkayo", "Pantukan|Pantukan", "Montevista|Montevista"];
//     } else if (region.value == "REGION XII") {
//         var optionArray = ["|", "North Cotabato|North Cotabato", "Sarangani|Sarangani", "South Cotabato|South Cotabato", "Sultan Kudarat|Sultan Kudarat"];
//         var optionArray2 = ["|", "Kidapawan|Kidapawan", "Banisilan|Banisilan", "Kabacan|Kabacan", "Matalam|Matalam"];
//     }


//     for (var option in optionArray) {
//         var pair = optionArray[option].split("|");
//         var newOption = document.createElement("option");
//         newOption.value = pair[0];
//         newOption.innerHTML = pair[1];
//         province.options.add(newOption);
//     }


//     // citymun.innerHTML = "";
//     // if (province.value == "Bukidnon") {
//     //     var optionArray = ["|", "Damulog|Damulog", "Don Carlos|Don Carlos", "Kibawe|Kibawe", "Kitaotao|Kitaotao", "Maramag|Maramag"];
//     // } else if (province.value == "Compostela Valley") {
//     //     var optionArray = ["|", "Laak|Laak", "Monkayo|Monkayo", "Pantukan|Pantukan", "Montevista|Montevista"];
//     // } else if (province.value == "North Cotabato") {
//     //     var optionArray = ["|", "Kidapawan|Kidapawan", "Banisilan|Banisilan", "Kabacan|Kabacan", "Matalam|Matalam"];
//     // }

//     for (var option in optionArray2) {
//         var pair = optionArray2[option].split("|");
//         var newOption = document.createElement("option");
//         newOption.value = pair[0];
//         newOption.innerHTML = pair[1];
//         citymun.options.add(newOption);
//     }
// }