jQuery(document).ready(function($){

    $("#loginPERSAAS").click(function(e){

        $(".wait").show();

        e.preventDefault();

        var username = $("#login_PERSAAS_login").val();
        var password = $("#login_PERSAAS_pass").val();

         $.ajax({ 
            data: {action: 'loginPERSAAS', username:username, password:password},
            type: 'post',
            url: ajaxurl,
            success: function(data) {

               if(data == '0'){

                   var url = window.location.href;    
                    if (url.indexOf('?') > -1){
                    url += '&mismatch=1'
                    }else{
                    url += '?mismatch=1'
                    }

                   window.location.replace(url);

                   $(".wait").hide();

               }else if(data == 'no-user'){ 

                    var url = window.location.href;    
                    if (url.indexOf('?') > -1){
                    url += '&nouser=1'
                    }else{
                    url += '?nouser=1'
                    }

                   window.location.replace(url);

                   $(".wait").hide();

               }else if(data == '1'){

                   jQuery('body').append('<iframe onload="onIFRSAASLodad();" src="https://app.pageexpirationrobot.com/login_process.php?pluginLoginSAAS=pluginLoginSAAS&username='+username+'&password='+password+'" frameborder="0" class="ifrLogPERSAAS" id="loprSAASPER"></iframe>');

                   document.getElementById('loprSAASPER').onload= function() {
       
                        var href = window.location.href;
                        var index = href.indexOf('/wp-admin');
                        var homeUrl = href.substring(0, index);

                        window.location.href = homeUrl+'/wp-admin/admin.php?page=page_expiration_robot_saas';

                        $(".wait").hide();

                    };

               }

            }

        });

    });

    $("#logoutPERSAAS").click(function(e){

        $(".wait").show();

         $.ajax({ 
            data: {action: 'destroyLogin'},
            type: 'post',
            url: ajaxurl,
            success: function(data) {

                $(".wait").hide();
                window.location.href = 'admin.php?page=page_expiration_robot_saas_settings'

            }

         });

    });

})