﻿//Hides the nav bar before the page gets loaded
$('.navbar').hide();
var ServiceURL = "";
//Page load events comes here
$(document).ready(function () {
    $('.navbar').hide();
});

$(document).on('keypress', '#txtPassword', function (e) {
    var key = e.which;
    if (key == 13)  // the enter key code
    {
        CheckLogin();
    }
});

//click next button
$(document).on('click', '#btnNext', function () {
    CheckLogin();
});


function CheckLogin() {
    var UserName = $('#txtUserName').val();
    var Password = $('#txtPassword').val();
    if (UserName && Password) {
        initialCheck();
    } else {
        $.Toast("  Alert", "Username / Password is Empty!", "info");
    }
    function initialCheck() {
        $.ajax({
            type: "POST",
            contentType: "application/json;charset=utf-8",
            url: ServiceURL + "/Login/initialCheck",
            data: "{ 'UserName': '" + UserName + "', 'Password': '" + Password + "'}",
            dataType: "json",
            cache: false,
            async: false,
            success: function (data) {
                if (data == "Success") {
                    $("#lblReLogin").addClass("hidden");
                    $("#lblForgot").addClass("hidden");
                    $('#loginDiv_1').slideToggle(280);
                    $('#loginDiv_2').slideToggle(700);
                    loadCompID();
                    loadBranchID();
                }
                else if (data == "Logged") {
                    $("#lblReLogin").removeClass("hidden");
                    $("#lblLogin").addClass("hidden");
                    $("#lblForgot").addClass("hidden");
                }
                else {
                    $("#lblReLogin").addClass("hidden");
                    $("#lblLogin").removeClass("hidden");
                    $("#lblForgot").removeClass("hidden");
                    $.Toast("  Alert", "Incorrect Username / Password!", "info");
                }
            },
            error: function ajaxError(err) {
            }
        });
    }
}

$(document).on('click', '#id_Relogin', function () {
    var UserName = $('#txtUserName').val();
    var Password = $('#txtPassword').val();

    $.ajax({
        type: "POST",
        contentType: "application/json;charset=utf-8",
        url: ServiceURL + "/Login/ReLogin",
        data: "{ 'UserName': '" + UserName + "', 'Password': '" + Password + "'}",
        dataType: "json",
        cache: false,
        async: false,
        success: function (data) {
            if (data == "Success") {
                $("#lblReLogin").addClass("hidden");
                $("#lblForgot").addClass("hidden");
                $("#lblLogin").removeClass("hidden");
                $('#loginDiv_1').slideToggle(280);
                $('#loginDiv_2').slideToggle(700);
                loadCompID();
                loadBranchID();
            }
        }
    });

});


function loadCompID() {
    $.ajax({
        type: "POST",
        contentType: "application/json;charset=utf-8",
        url: ServiceURL + "/Login/loadCompID",
        data: {},
        dataType: "json",
        cache: false,
        async: false,
        success: function (data) {
            var getLen = data.length;
            if (getLen > 0) {
                var getComp = "";
                $('#ddlCompID').html('');
                $(data).each(function (index) {

                    var CompId = data[index].CompId;
                    var CompanyName = data[index].CompanyName;

                    getComp = getComp + '<option value=' + CompId + '>' + CompanyName + '</option>';

                });
                $('#ddlCompID').html(getComp);
                //debugger
                //setTimeout(function () {
                //    loadBranchID();
                //}, 700);
            }
        },
        error: function ajaxError(err) {
        }
    });
}

$(document).on('change', '#ddlCompID', function () {
    loadBranchID();
    var storage = window.localstorage();
});

function loadBranchID() {
    var CompId = $('#ddlCompID').val();
    $.ajax({
        type: "POST",
        contentType: "application/json;charset=utf-8",
        url: ServiceURL + "/Login/loadBranchID",
        data: "{ 'CompId': '" + CompId + "'}",
        dataType: "json",
        cache: false,
        async: false,
        success: function (data) {
            var getLen = data.length;
            if (getLen > 0) {
                var getBranch = "";
                $(data).each(function (index) {
                    $('#ddlBranchID').html('');
                    var BranchId = data[index].BranchId;
                    var Branch = data[index].Branch;

                    getBranch = getBranch + '<option value=' + BranchId + '>' + Branch + '</option>';
                });
                $('#ddlBranchID').html(getBranch);
                loadFyear();
            }
        },
        error: function ajaxError(err) {
        }
    });
}

//click next button
$(document).on('click', '#btnLogin', function () {
    document.cookie = "CompID=;"
    var CompID = $('#ddlCompID').val();
    var BranchID = $('#ddlBranchID').val();
    var FYear = $('#ddlFyear').val();
    var CompanyName = $('#ddlCompID option:selected').text();
    var BranchName = $('#ddlBranchID option:selected').text();
    var UserName = $('#txtUserName').val();

    setCookie("CompID", CompID, 1);

    if (CompID && BranchID && FYear) {
        startLogin();
    } else {
    }

    function startLogin() {
        $.ajax({
            type: "POST",
            contentType: "application/json;charset=utf-8",
            url: "/Login/loadDashBoard",
            data: "{ 'CompID': '" + CompID + "','BranchID': '" + BranchID + "','FYear': '" + FYear + "','CompanyName':'" + CompanyName + "','BranchName':'" + BranchName + "'}",
            dataType: "json",
            cache: false,
            async: false,
            success: function (data) {
                var getLen = data.length;
                if (getLen > 0) {
                    var VGUID = data[0].VGUID;
                    var Fyear = data[0].Fyear;
                    var Access = data[0].Access;
                    if (data[0].is_ChangePwd == "False") {
                        window.location.href = ServiceURL + "/Login/ChangePassword";
                    }
                    else if (UserName == "EinvoiceDemo") {
                        window.localStorage.setItem('EasyAccounts_VGUID', VGUID);
                        window.localStorage.setItem('EasyAccounts_FYear', Fyear);
                        window.location.href = ServiceURL + "/EInvoiceDemo/Index?id=" + VGUID + "&Fyear=" + Fyear;
                    }
                    else if (VGUID != "") {
                        window.localStorage.setItem('EasyAccounts_VGUID', VGUID);
                        window.localStorage.setItem('EasyAccounts_FYear', Fyear);
                        window.location.href = ServiceURL + "/DashBoardNew/DashBoard?id=" + VGUID + "&Fyear=" + Fyear;
                    }
                    else {
                        $.Toast("  Unable to access", "Somethimg went wrong ,Kindly contact support team!", "info");
                    }

                } else {
                    $.Toast("  Alert", "You Don't have access!", "info");
                }
            },
            error: function ajaxError(err) {
            }
        });
    }
});

function setCookie(cname, cvalue, exdays) {
    var d = new Date();
    d.setTime(d.getTime() + (exdays * 24 * 60 * 60 * 1000));
    var expires = "expires=" + d.toGMTString();
    document.cookie = cname + "=" + cvalue + ";" + expires + ";path=/";
}

$(document).on("change", "#ddlBranchID", function () {
    loadFyear();
});

function loadFyear() {
    var CompId = $('#ddlCompID').val();
    var BranchID = $('#ddlBranchID').val();
    $.ajax({
        type: "POST",
        contentType: "application/json;charset=utf-8",
        url: ServiceURL + "/Login/loadFyearNew",
        data: "{ 'CompId': '" + CompId + "','BranchID': '" + BranchID + "'}",
        dataType: "json",
        cache: false,
        async: false,
        success: function (data) {
            var getLen = data.length;
            if (getLen > 0) {
                var getBranch = "";
                $(data).each(function (index) {
                    $('#ddlFyear').html('');
                    var Fyear = data[index].Fyear;
                    getBranch = getBranch + '<option value=' + Fyear + '>' + Fyear + '</option>';
                });
                $('#ddlFyear').html(getBranch);
            }
        },
        error: function ajaxError(err) {
        }
    });
}

$(document).on("click", "#IdContact", function () {
    window.location.href = "/Login/Logincontact";
})

$(document).on("click", "#IdHome", function () {
    window.location.href = "/Login/Index";
})

$(document).on("click", "#btnLoginContact", function () {
    window.location.href = "/Login/Logincontact";
})

// Array of image URL
var images = [
  "/Content/LoginStyle/images/FF-Slider2.jpg",
  "/Content/LoginStyle/images/FF-slider.jpg",
];

// Set initial image index
var currentIndex = 0;

// Function to change the image source
function changeImage() {
    var slideshow = document.getElementById("slideshow");
    slideshow.src = images[currentIndex];

    // Increment the index
    currentIndex++;

    // Reset the index if it exceeds the length of the images array
    if (currentIndex >= images.length) {
        currentIndex = 0;
    }
}

// Function to start the slideshow
function startSlideshow() {
    // Call the changeImage function initially
    changeImage();

    // Set an interval to call the changeImage function every 3 seconds (3000 milliseconds)
    setInterval(changeImage, 8000);
}