
function checkJSLoaded()
{
    if (typeof(gmaps) != "undefined") 
    {
        clearInterval();
        $("#page-loader").hide();
        $("#fibrecontainer").show();   
    }   
}


$(document).ready(function()
{
    //adding map image
    $("body").append('<div id="axxessMap" style="position: fixed;bottom: 0;right: 145px;width: 80px;"><a href="#" data-toggle="modal" data-target="#telkomFixedMap"><img src="modules/servers/axxessReseller/assets/img/orange.png" alt="google-map-image" style="height:80px;"></a></div>');
    
    
        if(googleMapKey)
        {
            var ax = document.createElement("script");
            ax.id = "mainscript";
            ax.onload = setInterval(checkJSLoaded,5000);
            ax.type = "text/javascript";
            ax.async = true;
            ax.src = "https://rcp.axxess.co.za/public/js/telkomLteJs.php?key="+googleMapKey+"&width=100%&height=500px";
            var s = document.getElementsByTagName("script")[0];
            s.parentNode.insertBefore(ax, s);
        }

        $("#TelkomMapForm").submit(function(e)
        {
            e.preventDefault();

            $("#btn-check").html("<span>CHECKING...</span>");

            $(".btn-check-fibre").addClass("disabled").attr("disabled","disabled");

            $("#map-response").empty();

            var $form = $(this),
            form_url = $form.attr("action");

            $.ajax(
            {
              url: form_url,
              type: "POST",
              data: $form.serializeArray(),
              cache: false,

              timeout: 150000,
              dataType: "json",

            })
            .done(function(data)
            {
                $(".inner-div:visible .btn-check-fibre span").html("CHECK");
                $(".inner-div:visible .btn-check-fibre").removeClass("disabled").removeAttr("disabled");

                var msg = "";

                if(data.status=="success")
                {
                    msg += data.message;
                        
                    $("#map-response").html('<div class="msg success">'+msg+'</div>');
                }
                else
                {
                    $(".inner-div:visible .btn-check-fibre span").addClass("blink");
                    $("#map-response").html('<div class="msg error">'+data.message+'</div>');
                }
            })
            .fail(function(jqXHR, textStatus)
            {
                console.log("Error to get LTE availability");

                $(".inner-div:visible .btn-check-fibre span").addClass("blink").html("CHECK");
                $(".inner-div:visible .btn-check-fibre").removeClass("disabled").removeAttr("disabled");
                $("#map-response").html('<div class="alert alert-danger"><strong>Error! </strong>'+ error+'. Contact support team</div>');

                if (textStatus == "timeout") 
                {
                    $("#map-response").html('<div class="msg error">Results currently not available. Please try again later.</div>');					
                }
            });

        });
    
});
