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 = '-' + (45 + new_width_half) + 'px';
	document.getElementById('progress_box').style.marginRight = new_width_half + 'px';

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

function errorArrow(field_id)
{
	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 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 updateProfile()
{
	/* set text to a loading box */
	$('progress_text').innerHTML = 'Saving...';
	Element.show('progress_box');

	var password = escape(document.getElementById('password').value);
	var password_again = escape(document.getElementById('password_again').value);

	if ( password && ( password == password_again ) )
	{
		/* add the ajax request to the end of defined url variable */
		var url = 'billing_ajax.php?user=1';

		/* make the ajax request for the information */
		new Ajax.Request(url, {
			method: 'post',
			parameters: 'password=' + password,
			onSuccess: function(xhrResponse) {
				/* set error text to a loading box */
				$('progress_text').innerHTML = 'Login information updated.';
				Element.show('progress_box');
				hideProgress = setTimeout(hideProgressBox, 24000);
			},
			onFailure: function(xhrResponse) {
				$('progress_text').innerHTML = 'Error: ' + xhrReponse.statusText;
			}
		});
	}
	else
	{
		if ( password == '' )
		{
			/* set error text to a loading box */
			$('progress_text').innerHTML = 'Password required.';
			Element.show('progress_box');
			hideProgress = setTimeout(hideProgressBox, 24000);
		}
		else
		{
			/* set error text to a loading box */
			$('progress_text').innerHTML = 'Passwords did not match.';
			Element.show('progress_box');
			hideProgress = setTimeout(hideProgressBox, 24000);
		}
	}
}

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

	//grab submitted information
	var first_name = document.getElementById('first_name').value;
	var last_name = document.getElementById('last_name').value;
	var state = document.getElementById('state').value;
	var address = document.getElementById('address').value;
	var city = document.getElementById('city').value;
	var zip = document.getElementById('zip').value;
	var invoice_type = document.getElementById('invoice_email').value;
	var auto = ( document.getElementById('auto_withdraw').checked == true ? 1 : 0 );

	var cc_type = document.getElementById('cc_type').value;
	var cc_exp_date = document.getElementById('cc_year').value + '-' + document.getElementById('cc_month').value + '-01';
	var cc_number = document.getElementById('cc_number').value;
	var cc_sec_code = document.getElementById('cc_sec_code').value;
	var cc_name = document.getElementById('cc_name').value;

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

	/* add the ajax request to the end of defined url variable */
	var url = 'billing_ajax.php';

	/* make the ajax request for the information */
	new Ajax.Request(url, {
		method: 'post',
		parameters: 'first_name=' + first_name + '&last_name=' + last_name + '&state=' + state + '&address=' + address + '&city=' + city + '&zip=' + zip + '&invoice_type=' + invoice_type + '&auto_withdraw=' + auto + '&cc_type=' + cc_type + '&cc_exp_date=' + cc_exp_date + '&cc_number=' + cc_number + '&cc_sec_code=' + cc_sec_code + '&cc_name=' + cc_name,
		onSuccess: function(xhrResponse) {
			$('progress_text').innerHTML = xhrResponse.responseText;
			if ( cc_number )
			{
				document.getElementById('cc_number').value = '************' + cc_number.substring(12, cc_number.length);
			}
		},
		onFailure: function(xhrResponse) {
			$('progress_text').innerHTML = 'Error: ' + xhrReponse.statusText;
		}
	});
}

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 ? 'account.php' : xhrResponse.responseText );
			}
		},
		onFailure: function(xhrResponse) {
			$('progress_text').innerHTML = 'Error: ' + xhrReponse.statusText;
		}
	});
}

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

function trackSessions(browser, page)
{
	var screenW = 640;
	var screenH = 480;
	
	if ( parseInt(navigator.appVersion) > 3 )
	{
		screenW = screen.width;
		screenH = screen.height;
	}
	else if ( navigator.appName == "Netscape" && parseInt(navigator.appVersion)==3 && navigator.javaEnabled() )
	{
		var jToolkit = java.awt.Toolkit.getDefaultToolkit();
		var jScreenSize = jToolkit.getScreenSize();
		screenW = jScreenSize.width;
		screenH = jScreenSize.height;
	}

	var url = 'track-sessions.php';

	/* make the ajax request for the information */
	new Ajax.Request(url, {
		method: 'post',
		parameters: 'browser=' + browser + '&page=' + page + '&screen_width=' + screenW + '&screen_height=' + screenH,
		onSuccess: function(xhrResponse) {
			checkSession = setInterval("updateSession('" + browser + "', '" + page + "', " + screenW + ", " + screenH + ")", 1000);
		},
		onFailure: function(xhrResponse) {

		}
	});
}

function updateSession(browser, page, screenW, screenH)
{
	var url = 'track-sessions.php';
	
	/* make the ajax request for the information */
	new Ajax.Request(url, {
		method: 'post',
		parameters: 'browser=' + browser + '&page=' + page + '&screen_width=' + screenW + '&screen_height=' + screenH,
		onSuccess: function(xhrResponse) {
			
		},
		onFailure: function(xhrResponse) {

		}
	});

}