var registerInfo = null;

$(document).ready(function () {
    if (window.top.$("#hfIsCloud").val() === "1")
        $("#txtMobile").prop("disabled", true);
    $("#txtMobile").val($("#hfMobileNoL").val());
    $("#txtEmail").val($("#hfEmailAddressL").val());
});
$('.next-btn').click(function () {
    var itemBox = $('.items-box.active').attr('id');
    var currentTab = itemBox.replace('item', '');
    if ((parseInt(currentTab) + 1) > 5) {
        //ignre
    }
    else {

        if ((parseInt(currentTab) + 1) == 2) {
            $('.prev-btn').css('display', 'block');
            var nextTab = 'item' + (parseInt(currentTab) + 1);
            $('.items-box').removeClass('active');
            $('.div-item > label').removeClass('active');
            $('#' + nextTab).addClass('active');
            $('#head' + (parseInt(currentTab) + 1) + ' > label').addClass('active');
            $('#head' + (parseInt(currentTab)) + ' > label').addClass('passed');
        }
        else if ((parseInt(currentTab) + 1) == 3) {
            var exceprionText = checkValidation(itemBox);
            if (exceprionText == '') {
                registerInfo = {
                    validCode: "",
                    name: $('#txtName').val(),
                    mobileNo: $('#txtMobile').val(),
                    phone: $('#txtPhone').val(),
                    email: $('#txtEmail').val(),
                    address: $('#txtAddress').val()
                };
                ajax({
                    loading: true,
                    url: 'GeneralApi/ValidUserInfo',
                    data: registerInfo,
                    success: function (result) {
                        var tomorrow = new Date();
                        tomorrow.setMinutes(tomorrow.getMinutes() + 1);
                        setCounter(tomorrow);
                        $('.prev-btn').css('display', 'block');
                        var nextTab = 'item' + (parseInt(currentTab) + 1);
                        $('.items-box').removeClass('active');
                        $('.div-item > label').removeClass('active');
                        $('#' + nextTab).addClass('active');
                        $('#head' + (parseInt(currentTab) + 1) + ' > label').addClass('active');
                        $('#head' + (parseInt(currentTab)) + ' > label').addClass('passed');
                    }
                });
            }
            else {
                showMsgModal(0, exceprionText);
            }
        }
        else if ((parseInt(currentTab) + 1) == 4) {
            if ($('#txtActivationCode').val().trim() != '') {
                var json = registerInfo;
                json.validCode = $('#txtActivationCode').val().trim();
                ajax({
                    loading: true,
                    url: 'GeneralApi/SaveUserInfo',
                    data: json,
                    success: function (result) {
                        $('.prev-btn').css('display', 'block');
                        var nextTab = 'item' + (parseInt(currentTab) + 1);
                        $('.items-box').removeClass('active');
                        $('.div-item > label').removeClass('active');
                        $('#' + nextTab).addClass('active');
                        $('#head' + (parseInt(currentTab) + 1) + ' > label').addClass('active');
                        $('#head' + (parseInt(currentTab)) + ' > label').addClass('passed');

                        $('.next-btn').val(window.top.loginPortal);
                        $('.next-btn').hide();
                        $('.portal-inactive-lbl').val(window.top.end);
                        $('.prev-btn').css('display', 'none');
                    }
                });
            }
            else {
                showMsgModal(0, window.top.activeCode);
            }
        }
        else if ((parseInt(currentTab) + 1) == 5) {
            openPortal();
        }
    }
});
$('.prev-btn').click(function () {
    clearCounter();
    var currentTab = $('.items-box.active').attr('id');
    currentTab = currentTab.replace('item', '');
    if ((parseInt(currentTab) - 1) <= 0) {

    }
    else {
        var nextTab = 'item' + (parseInt(currentTab) - 1);
        $('.items-box').removeClass('active');
        $('.div-item > label').removeClass('active');
        $('#' + nextTab).addClass('active');
        $('#head' + (parseInt(currentTab) - 1) + ' > label').addClass('active');
        $('#head' + (parseInt(currentTab)) + ' > label').removeClass('passed');
        if ((parseInt(currentTab) + 1) == 1)
            $('.prev-btn').css('display', 'none');
    }
});
$(window).load(function () {
    openPortal();
});
function openPortal() {
    if (window.top.$('#hfWebUser').val() !== '') {
        var link = document.createElement("a");
        link.setAttribute("href", window.top.$('#hfWebUser').val() );
        link.setAttribute("target", "_blank");
        link.click();
        if (link.parentNode)
            link.parentNode.removeChild(link);
        closeTopModal();
    }
}
function resendCode() {
    $('#counter').css('display', 'block');
    $('#resend').css('display', 'none');
    var tomorrow = new Date();
    tomorrow.setMinutes(tomorrow.getMinutes() + 1);
    setCounter(tomorrow);
}
var waitInterval;
function setCounter(tomorrow) {
    waitInterval = setInterval(function () {
        // Get todays date and time
        var today = new Date().getTime();

        // Find the distance between now an the count down date
        var distance = tomorrow.getTime() - today;

        // Time calculations for days, hours, minutes and seconds
        var hours = Math.floor((distance % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60));
        var minutes = Math.floor((distance % (1000 * 60 * 60)) / (1000 * 60));
        var seconds = Math.floor((distance % (1000 * 60)) / 1000);
        if ((parseInt(minutes / 10) <= 0) && ((minutes - parseInt(minutes / 10) * 10) <= 0) && (parseInt(seconds / 10) <= 0) && ((seconds - parseInt(seconds / 10) * 10) <= 0)) {
            clearInterval(waitInterval);
            $('#counter').css('display', 'none');
            $('#resend').css('display', 'block');
            $('#timer').text('10:00');
            return;
        }
        var time = '' + parseInt(minutes / 10) + '' + (minutes - parseInt(minutes / 10) * 10) + ':' + parseInt(seconds / 10) + '' + (seconds - parseInt(seconds / 10) * 10);

        $('#timer').text(time);

    }, 1000);
}
function clearCounter() {
    if (waitInterval) {
        clearTimeout(waitInterval);
        $('#timer').text('01:00');
    }
}