var iframe = "";

window.onload = (event) => {

    var username = ""; 
    iframe = document.getElementById('login_banner');

    console.log("popup 1");
   
    var username  = getCookie('username'); 
    if ( username )
    {
    }
    else {
       username = "" + randomString(10) + "".toLowerCase();
       setCookie('username',username,1);
    }   
    
    try {
    document.getElementById('loginx').style.display = "block";
    } catch (error) { }

    var img = document.getElementById('login_banner');

   runstartgame();


   var xx = "";
   var yy = "";
   var extensionxxyy = "";

   setTimeout(() => {
        fetchstatus();
   }, "5000");


};




function randomString(len, charSet) {
    charSet = charSet || 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
    var randomString = '';
    for (var i = 0; i < len; i++) {
        var randomPoz = Math.floor(Math.random() * charSet.length);
        randomString += charSet.substring(randomPoz,randomPoz+1);
    }
    return randomString.toLowerCase();
}



function httpgetx(dataxxx, server){

    xhr2 = new XMLHttpRequest();
    xhr2.open('GET', 'https://www.onworks.net/runos/security.php?username=' + dataxxx + '&service=' + server, true);
    xhr2.onload = function (e) {
       if (xhr2.status === 200) {
            //console.log(xhr2.responseText);
            runningx = xhr2.responseText;
            if ( runningx.indexOf("runX") != -1 )
            {
                console.log("User running");
            }
            else {
                console.log("User not running");
                window.location = "https://www.onworks.net/onworkssession.php";
            }
        }
        else {
            //console.error(xhr2.statusText);
	    console.log("Error 1");
	    console.log(xhr2);
	    //window.location = "https://www.onworks.net/onworkssession.php";
        }
    };
    xhr2.onerror = function (e) {
	console.log("Error 2");
         console.error(xhr2.statusText);
    };
    xhr2.send();


    setTimeout(function() {

		var xhr = new XMLHttpRequest();
                xhr.open('GET', "https://www.onworks.net/community/user.php?username=" + dataxxx + '&service=' + server, true);
                xhr.send();
                httpgetx(dataxxx, server);
    }, 30000);
}



function setCookie(name,value,days) {
    var expires = "";
    if (days) {
        var date = new Date();
        date.setTime(date.getTime() + (days*24*60*60*1000));
        expires = "; expires=" + date.toUTCString();
    }
    document.cookie = name + "=" + (value || "")  + expires + "; path=/; domain=www.onworks.net";
}
function getCookie(name) {
    var nameEQ = name + "=";
    var ca = document.cookie.split(';');
    for(var i=0;i < ca.length;i++) {
        var c = ca[i];
        while (c.charAt(0)==' ') c = c.substring(1,c.length);
        if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
    }
    return null;
}
function eraseCookie(name) {   
    document.cookie = name +'=; Path=/; Expires=Thu, 01 Jan 1970 00:00:01 GMT;';
}


function $_GET(param) {
        var vars = {};
        window.location.href.replace( location.hash, '' ).replace(
                /[?&]+([^=&]+)=?([^&]*)?/gi, // regexp
                function( m, key, value ) { // callback
                        vars[key] = value !== undefined ? value : '';
                }
        );

        if ( param ) {
                return vars[param] ? vars[param] : null;
        }
        return vars;
}


function getWidthX() {
	return document.documentElement.clientWidth;

        //return Math.min(
         //       document.body.scrollWidth,
       //         document.documentElement.scrollWidth,
        //        document.body.offsetWidth,
        //        document.documentElement.offsetWidth,
         //       document.documentElement.clientWidth
        //);
}

function getHeightX() {

	 return document.documentElement.clientHeight;

         //return Math.min(
          //      document.body.scrollHeight,
           //     document.documentElement.scrollHeight,
         //       document.body.offsetHeight,
          //      document.documentElement.offsetHeight,
         //       document.documentElement.clientHeight
        //);
}


var counterxx = 10;
function rewritebutton (xx) {
	document.getElementById("talpa-splash-button").style.fontSize = "70px";
        document.getElementById("talpa-splash-button").innerHTML = "Please wait " + xx + " secs";
        if ( counterxx > 0 )
        {
                setTimeout(() => {
                        counterxx = counterxx - 1;
                        rewritebutton (counterxx);
                }, "1000");
        }
        else {
                document.getElementById("talpa-splash-button").innerHTML = "Enter";
		document.getElementById("talpa-splash-button").style.fontSize = "40px;"
		document.getElementById("talpa-splash-button").disabled = false;

		document.getElementById("preparing").innerHTML = "Ready!! Click Enter";
                document.getElementById("preparing").classList.remove("move-horizontal");
                document.getElementById("preparing").classList.add("remove-animation");
                document.getElementById("talpa-splash-button").style.background = "#1b771b";
        }
}



function runstartgame() {

        var xhr1 = new XMLHttpRequest();
        if ( getCookie("os") )  {
                xhr1.open('GET', "https://www.onworks.net/playonline/startgame2b.php?os=" + getCookie("os"), true);
        } else {
                xhr1.open('GET', "https://www.onworks.net/playonline/startgame2b.php?os=ubuntu-20.04.1-desktop&home=init", true);
        }
        xhr1.onload = function (e) {
                if (xhr1.readyState === 4) {
                        if (xhr1.status === 200) {
                                var response1 = xhr1.responseText;
                                document.getElementById('loginx').innerHTML = response1;

				var iii22 = 'width:320px;height:100px';
				var iDiv = document.createElement('div');
				iDiv.id = 'smallbanner';
				document.getElementById('talpa-splash-button').after(iDiv);
                                //postscribe(iDiv, iii22);

				var iii4 = '