var hideProgress;
var last_error = '';

function hideProgressBox()
{
	Element.hide('progress_box');
}

function progressBox(text, stay_visible)
{
	Element.show('progress_box');

	$('progress_text').innerHTML = text;
	var new_width_half = (document.getElementById('progress_box').offsetWidth / 2);
	document.getElementById('progress_box').style.marginLeft = '-' + (new_width_half - 20) + 'px';
	document.getElementById('progress_box').style.marginRight = new_width_half + 'px';

	if ( !stay_visible )
	{
		hideProgress = setTimeout(hideProgressBox, 24000);
	}
}

function errorArrow(field_id)
{
	Element.hide(field_id + '_error_spacer');
	Element.show(field_id + '_error');
	Effect.Pulsate(field_id + '_error', { pulses: 3, duration: 0.7 });
	document.getElementById(field_id).focus();

	last_error = field_id + '_error';
}

function validEmail(email_address)
{
	var ampIndex = email_address.indexOf("@");
	var afterAmp = email_address.substring((ampIndex + 1), email_address.length);

	var dotIndex = afterAmp.indexOf("."); // dot before ampersand only
	dotIndex = dotIndex + ampIndex + 1; // determine dot position in entire string (not just after amp portion)
	afterAmp = email_address.substring((ampIndex + 1), dotIndex); // afterAmp will be portion of string from ampersand to dot
	var afterDot = email_address.substring((dotIndex + 1), email_address.length); // afterDot will be portion of string from dot to end of string
	var beforeAmp = email_address.substring(0,(ampIndex));
	//old regex did not allow subdomains and dots in names
	//var email_regex = /^[\w\d\!\#\$\%\&\'\*\+\-\/\=\?\^\_\`\{\|\}\~]+(\.[\w\d\!\#\$\%\&\'\*\+\-\/\=\?\^\_\`\{\|\}\~])*\@(((\w+[\w\d\-]*[\w\d]\.)+(\w+[\w\d\-]*[\w\d]))|((\d{1,3}\.){3}\d{1,3}))$/;
	var email_regex = /^\w(?:\w|-|\.(?!\.|@))*@\w(?:\w|-|\.(?!\.))*\.\w{2,3}/
	if ((email_address.indexOf("@") != "-1") && (email_address.length > 5) && (afterAmp.length > 0) && (beforeAmp.length > 1) && (afterDot.length > 1) && (email_regex.test(email_address)) ) // index of -1 means "not found"
	{
		return true;
	}
	else
	{
		return false;
	}
}

function unstealthMode()
{
	clearTimeout(hideProgress); // clear any existing timeouts

	/* set text to a loading box */
	$('progress_text').innerHTML = 'Loading...';
	Element.show('progress_box');

	/* add the ajax request to the end of defined url variable */
	var url = 'account_ajax.php?mode=admin';

	/* make the ajax request for the information */
	new Ajax.Request(url, {
		method: 'post',
		parameters: 'goback=1',
		onSuccess: function(xhrResponse) {
			parent.location = 'account.php';
		},
		onFailure: function(xhrResponse) {
			$('progress_text').innerHTML = 'Error: ' + xhrReponse.statusText;
		}
	});
}

function grayOut(vis, options)
{
	var options = options || {};
	var zindex = options.zindex || 50;
	var opacity = options.opacity || 70;
	var opaque = (opacity / 100);
	var bgcolor = options.bgcolor || '#000000';
	var dark = document.getElementById('darkenScreenObject');
	if ( !dark ) {
		var tbody = document.getElementsByTagName("body")[0];
		var tnode = document.createElement('div');
		tnode.style.position = 'absolute';
		tnode.style.top = '0px';
		tnode.style.left = '0px';
		tnode.style.overflow = 'hidden';
		tnode.style.display = 'none';
		tnode.id = 'darkenScreenObject';
		tbody.appendChild(tnode);
		dark = document.getElementById('darkenScreenObject');
	}

	if ( vis )
	{
		if( document.body && ( document.body.scrollWidth || document.body.scrollHeight ) )
		{
			var pageWidth = document.body.scrollWidth + 'px';
			var pageHeight = document.body.scrollHeight + 'px';
		}
		else if ( document.body.offsetWidth )
		{
			var pageWidth = document.body.offsetWidth + 'px';
			var pageHeight = document.body.offsetHeight + 'px';
		}
		else
		{
			var pageWidth = '100%';
			var pageHeight = '100%';
		}
		dark.style.opacity = opaque;
		dark.style.MozOpacity = opaque;
		dark.style.filter = 'alpha(opacity='+opacity+')';
		dark.style.zIndex = zindex;
		dark.style.backgroundColor = bgcolor;
		dark.style.width = pageWidth;
		dark.style.height = pageHeight;
		dark.style.display = 'block';
	}
	else
	{
		dark.style.display = 'none';
	}
}

function checkEnter(inField, e)
{
	var key_pressed;

	if ( e && e.which )
	{
		key_pressed = e.which;
	}
	else if ( window.event )
	{
		e = window.event;
		key_pressed = e.keyCode;
	}

	if ( key_pressed == 13 )
	{
		loginClient();
	}
}

function loginClient()
{
	clearTimeout(hideProgress); // clear any existing timeouts

	//grab submitted information
	var username = document.getElementById('username').value;
	var password = document.getElementById('password').value;

	progressBox('Loading...', 1);

	/* add the ajax request to the end of defined url variable */
	var url = 'account_ajax.php?mode=login';

	/* make the ajax request for the information */
	new Ajax.Request(url, {
		method: 'post',
		parameters: 'username=' + username + '&password=' + password,
		onSuccess: function(xhrResponse) {
			if ( xhrResponse.responseText == 0 )
			{
				progressBox('Invalid username or password.');
			}
			else if ( xhrResponse.responseText == 1 )
			{
				progressBox('You have been suspended from logging in because of 7 failed attempts.');
			}
			else
			{
				parent.window.location.href = ( xhrResponse.responseText == 2 ? 'blog.php' : xhrResponse.responseText );
			}
		},
		onFailure: function(xhrResponse) {
			$('progress_text').innerHTML = 'Error: ' + xhrReponse.statusText;
		}
	});
}

var checkSession;
var user_browser = '';
var user_page = '';

function trackSessions(browser, page)
{

}

function updateSession(browser, page, screenW, screenH)
{

}
