function loginDialog() {
	var xScroll, yScroll;
	
	if (window.innerHeight && window.scrollMaxY) {	
		xScroll = window.innerWidth + window.scrollMaxX;
		yScroll = window.innerHeight + window.scrollMaxY;
	} else if (document.body.scrollHeight > document.body.offsetHeight){ // all but Explorer Mac
		xScroll = document.body.scrollWidth;
		yScroll = document.body.scrollHeight;
	} else { // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
		xScroll = document.body.offsetWidth;
		yScroll = document.body.offsetHeight;
	}
	
	var windowWidth, windowHeight;
	
	if (self.innerHeight) {	// all except Explorer
		if(document.documentElement.clientWidth){
			windowWidth = document.documentElement.clientWidth; 
		} else {
			windowWidth = self.innerWidth;
		}
		windowHeight = self.innerHeight;
	} else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
		windowWidth = document.documentElement.clientWidth;
		windowHeight = document.documentElement.clientHeight;
	} else if (document.body) { // other Explorers
		windowWidth = document.body.clientWidth;
		windowHeight = document.body.clientHeight;
	}	
	
	// for small pages with total height less then height of the viewport
	if(yScroll < windowHeight){
		pageHeight = windowHeight;
	} else { 
		pageHeight = yScroll;
	}
	
	// for small pages with total width less then width of the viewport
	if(xScroll < windowWidth){	
		pageWidth = xScroll;		
	} else {
		pageWidth = windowWidth;
	}
	
	var w = document.getElementById('login_dialog').offsetWidth;
	var h = document.getElementById('login_dialog').offsetHeight;

	leftpos = (windowWidth-w)/2;
	toppos = (windowHeight-h)/2;
	
	disableBrowser();
	
	document.getElementById('login_dialog').style.top = Math.round(toppos)+'px';
	document.getElementById('login_dialog').style.left = Math.round(leftpos)+'px';
	document.getElementById('login_recovery_content').style.position = 'absolute';
	document.getElementById('login_recovery_content').style.visibility = 'hidden';
	document.getElementById('login_dialog').style.visibility = 'visible';
	document.getElementById('login_content').style.visibility = 'visible';
	
	document.getElementById('login_username').focus();
}

function closeLoginDialog() {
	enableBrowser();
	
	document.getElementById('login_dialog').style.visibility = 'hidden';
	document.getElementById('login_content').style.visibility = 'hidden';
	
	document.getElementById('login_loader').style.position = 'absolute';
	document.getElementById('login_loader').style.visibility = 'hidden';
	
	document.getElementById('login_recovery_content').style.position = 'absolute';
	document.getElementById('login_recovery_content').style.visibility = 'hidden';
}

function checkLogin() {
	var error = false;
	for (field_id in login_required_fields['login']) {
		if (!login_required_fields['login'][field_id]['param'].test(document.getElementById(field_id).value)) {
			error = true;
			alert(login_required_fields['login'][field_id]['error']);
			document.getElementById(field_id).className = 'textfield_singleline_error';
			document.getElementById(field_id).onkeyup = function () {
				this.className = 'textfield_singleline';
				this.onkeyup = null;
			}
			
			document.getElementById(field_id).focus();
			break;
		}
	}
	
	if (!error) {
		document.getElementById('login_content').style.position = 'absolute';
		document.getElementById('login_content').style.visibility = 'hidden';
		
		document.getElementById('login_loader').style.position = 'static';
		document.getElementById('login_loader').style.visibility = 'visible';
		
		doLogin();
	}
}

var login_connection = 0;

function doLogin() {
	ran = Math.round(Math.random()*100000); 
	url = "/functions/login/check.php?Ran="+ran.toString();
	
	data = "un="+document.getElementById('login_username').value+"&pw="+document.getElementById('login_password').value+"&rm="+document.getElementById('login_remember').checked;

	if (window.XMLHttpRequest) {
		login_connection = new XMLHttpRequest();
	} else if (window.ActiveXObject) {
		login_connection = new ActiveXObject("Microsoft.XMLHTTP");
	}
	if (login_connection) {
		login_connection.onreadystatechange=loginEval
		login_connection.open("POST",url,true);
		login_connection.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
		login_connection.send(data);
	}
}

function loginEval() {
	if (login_connection.readyState==4) {
		if (login_connection.status==200){
			eval(login_connection.responseText);
		}
	}
}

function logout() {
	ran = Math.round(Math.random()*100000); 
	url = "/functions/login/out.php?Ran="+ran.toString();
	
	data = "";

	if (window.XMLHttpRequest) {
		login_connection = new XMLHttpRequest();
	} else if (window.ActiveXObject) {
		login_connection = new ActiveXObject("Microsoft.XMLHTTP");
	}
	if (login_connection) {
		login_connection.onreadystatechange=loginEval
		login_connection.open("POST",url,true);
		login_connection.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
		login_connection.send(data);
	}	
}

function showRecovery() {
	document.getElementById('login_content').style.position = 'absolute';
	document.getElementById('login_content').style.visibility = 'hidden';
	
	document.getElementById('login_recovery_content').style.position = 'static';
	document.getElementById('login_recovery_content').style.visibility = 'visible';
}

function backToLogin() {
	document.getElementById('login_content').style.position = 'static';
	document.getElementById('login_content').style.visibility = 'visible';
	
	document.getElementById('login_recovery_content').style.position = 'absolute';
	document.getElementById('login_recovery_content').style.visibility = 'hidden';
}

function checkRecovery() {
	var error = false;
	for (field_id in login_required_fields['recovery']) {
		if (!login_required_fields['recovery'][field_id]['param'].test(document.getElementById(field_id).value)) {
			error = true;
			alert(login_required_fields['recovery'][field_id]['error']);
			document.getElementById(field_id).className = 'textfield_singleline_error';
			document.getElementById(field_id).onkeyup = function () {
				this.className = 'textfield_singleline';
				this.onkeyup = null;
			}
			
			document.getElementById(field_id).focus();
			break;
		}
	}
	
	if (!error) {
		document.getElementById('login_recovery_content').style.position = 'absolute';
		document.getElementById('login_recovery_content').style.visibility = 'hidden';
		
		document.getElementById('login_loader').style.position = 'static';
		document.getElementById('login_loader').style.visibility = 'visible';
		
		doRecovery();
	}
}

function doRecovery() {
	ran = Math.round(Math.random()*100000); 
	url = "/functions/login/recovery.php?Ran="+ran.toString();
	
	data = "email="+document.getElementById('login_recovery_email').value;

	if (window.XMLHttpRequest) {
		login_connection = new XMLHttpRequest();
	} else if (window.ActiveXObject) {
		login_connection = new ActiveXObject("Microsoft.XMLHTTP");
	}
	if (login_connection) {
		login_connection.onreadystatechange=loginEval
		login_connection.open("POST",url,true);
		login_connection.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
		login_connection.send(data);
	}
}

function loginOnEnter(e){
	var characterCode;
	if (e && e.which) {
		e = e;
		characterCode = e.which;
	} else {
		e = event;
		characterCode = e.keyCode;
	}
	if (characterCode == 13) {
		checkLogin();
	}
}
