var cursorInt = 0;
var loginInProgress = 0;

function PerformLogin() {

 if (loginInProgress == 0) {
 
 loginInProgress = 1;

 var lg_user = document.getElementById('login_user').value;
 var lg_pass = document.getElementById('login_pass').value;
 
 var ajax = false;

 // Create the object:

 // Choose the objecttype, depending on what is supported:
 if (window.XMLHttpRequest) {

    // IE 7, Mozilla, Safari, Firefox, Opera, most browsers:
    ajax = new XMLHttpRequest();

 } else if (window.ActiveXObject) { // Old IE-browsers

    // Make the type Msxml2.XMLHTTP, if possible:
    try {
        ajax = new ActiveXObject("Msxml2.XMLHTTP");
    } catch (e1) { // Else use the other type:
        try {
            ajax = new ActiveXObject("Microsoft.XMLHTTP");
        } catch (e2) { }
    }

 }

 // Retrieving the data from the page

 if (ajax) {

   var url = "/login.php?user=" + encodeURIComponent(lg_user) + "&password=" + encodeURIComponent(lg_pass);

   ajax.open('GET', url);

   // Sends request
   ajax.send(null);
   var t = 0;
   // Function that handles response
   ajax.onreadystatechange=function(){
    // If everything is OK:
     if ( (ajax.readyState == 4) && (ajax.status == 200) ) {
          // Returns the value to the document
		  var ret_vars = ajax.responseText.split(',');
          if (ret_vars[0] == 'Succes') {
			SimulateLogInSucces();
		  } else {
			SimulateLogInFail();
		  }
	 }
    }


 } else { // AJAX is not useable
     alert('It is not possible to connect, please update your browser.');
 }
 
 
 }

 
}

function SimulateLogInSucces() {
 if (navigator.appName == 'Microsoft Internet Explorer') {
 ClearText();
 cursorInt = setInterval('ToggleLg001()', 300);
 setTimeout(function(){ AddTextIE('Initializing...'); }, 1000);
 setTimeout(function(){ AddTextIE('<br />Connecting...'); }, 2000);
 setTimeout(function(){ AddTextIE('<br />Verifying user...'); }, 3000);
 setTimeout(function(){ AddTextIE('<br />Verifying password...'); }, 4000);
 setTimeout(function(){ AddTextIE('<br />Verifying activation...'); }, 5000);
 setTimeout(function(){ AddTextIE('<br />Acces granted...'); }, 6000);
 setTimeout(function(){ AddTextIE('<br />Exiting...'); }, 7000);
 setTimeout(function(){ clearInterval(cursorInt); document.getElementById('lg_001').style.display = 'none'; loginInProgress = 0; }, 7500);
 setTimeout(function(){ location.reload(true); }, 8000);
 } else {
 ClearText();
 cursorInt = setInterval('ToggleLg001()', 300);
 setTimeout(function(){ AddText('Initializing...'); }, 1000);
 setTimeout(function(){ AddText('\nConnecting...'); }, 2000);
 setTimeout(function(){ AddText('\nVerifying user...'); }, 3000);
 setTimeout(function(){ AddText('\nVerifying password...'); }, 4000);
 setTimeout(function(){ AddText('\nVerifying activation...'); }, 5000);
 setTimeout(function(){ AddText('\nAcces granted...'); }, 6000);
 setTimeout(function(){ AddText('\nExiting...'); }, 7000);
 setTimeout(function(){ clearInterval(cursorInt); document.getElementById('lg_001').style.display = 'none'; loginInProgress = 0; }, 7500);
 setTimeout(function(){ location.reload(true); }, 8000);
 }
}
function SimulateLogInFail() {
 if (navigator.appName == 'Microsoft Internet Explorer') {
 ClearText();
 cursorInt = setInterval('ToggleLg001()', 300);
 setTimeout(function(){ AddTextIE('Initializing...'); }, 1000);
 setTimeout(function(){ AddTextIE('<br />Connecting...'); }, 2000);
 setTimeout(function(){ AddTextIE('<br />Verifying user...'); }, 3000);
 setTimeout(function(){ AddTextIE('<br />Verifying password...'); }, 4000);
 setTimeout(function(){ AddTextIE('<br />Verifying activation...'); }, 5000);
 setTimeout(function(){ AddTextIE('<br />Acces denied...'); }, 6000);
 setTimeout(function(){ AddTextIE('<br />Exiting...'); }, 7000);
 setTimeout(function(){ clearInterval(cursorInt); document.getElementById('lg_001').style.display = 'none'; loginInProgress = 0; }, 7500);
 } else {
 ClearText();
 cursorInt = setInterval('ToggleLg001()', 300);
 setTimeout(function(){ AddText('Initializing...'); }, 1000);
 setTimeout(function(){ AddText('\nConnecting...'); }, 2000);
 setTimeout(function(){ AddText('\nVerifying user...'); }, 3000);
 setTimeout(function(){ AddText('\nVerifying password...'); }, 4000);
 setTimeout(function(){ AddText('\nVerifying activation...'); }, 5000);
 setTimeout(function(){ AddText('\nAcces denied...'); }, 6000);
 setTimeout(function(){ AddText('\nExiting...'); }, 7000);
 setTimeout(function(){ clearInterval(cursorInt); document.getElementById('lg_001').style.display = 'none'; loginInProgress = 0; }, 7500);
 }
}

var g_i = 0;

function AddText(text) {
	var charAr = new Array();
	var length = text.length;
	var i = 0;
	for (i = 0; i < length; i++) {
		charAr[i] = text.substr(i, 1);
	}
	var to = 800 / length;
	var x = 0;
	while (x < length) {
		setTimeout(function(){ 
		document.getElementById('prog_text').innerHTML += charAr[g_i];
		g_i++;
		}, parseInt(to * (x + 1)));
		x++;
	}
	g_i = 0;
}
function ClearText() {
	document.getElementById('prog_text').innerHTML = "";
}
function ToggleLg001() {
	if (document.getElementById('lg_001').style.display != 'none') {
		document.getElementById('lg_001').style.display = 'none';
	} else {
		document.getElementById('lg_001').style.display = 'inline';
	}
}

function AddTextIE(text) {
	document.getElementById('prog_text').innerHTML += text;
}

function enableEnterKey(e)
{
     var key;     
     if(window.event)
          key = window.event.keyCode; //IE
     else
          key = e.which; //firefox     

     if (key == 13) {
		PerformLogin();
	 }
}

function showPopup() {
document.getElementById('FullScreen').style.display='inline';
document.getElementById('Popup').style.display='inline';
}

function hidePopup() {
clearInterval(cursorInt);
document.getElementById('FullScreen').style.display='none';
document.getElementById('Popup').style.display='none';
}

function PerformLogout() {

 var ajax2 = false;

 // Create the object:

 // Choose the objecttype, depending on what is supported:
 if (window.XMLHttpRequest) {

    // IE 7, Mozilla, Safari, Firefox, Opera, most browsers:
    ajax2 = new XMLHttpRequest();

 } else if (window.ActiveXObject) { // Old IE-browsers

    // Make the type Msxml2.XMLHTTP, if possible:
    try {
        ajax2 = new ActiveXObject("Msxml2.XMLHTTP");
    } catch (e1) { // Else use the other type:
        try {
            ajax2 = new ActiveXObject("Microsoft.XMLHTTP");
        } catch (e2) { }
    }

 }


 // Retrieving the data from the page

 if (ajax2) {

   var url2 = "/logout.php";

   ajax2.open('GET', url2);

   // Sends request
   ajax2.send(null);
   
   // Function that handles response
   ajax2.onreadystatechange=function(){
    // If everything is OK:
     if ( (ajax2.readyState == 4) && (ajax2.status == 200) ) {
		  location.reload(true);
	 }
    }


 } else { // AJAX is not useable
     alert('It is not possible to connect, please update your browser.');
 }

 
}
