
$(function()
{
	$('.error').hide();
	$('#loginMessage').hide();
	$('input.text-input').css({backgroundColor:"#FFFFFF"});
	$('input.text-input').focus(function()
	{
		$(this).css({backgroundColor:"#FFDDAA"});
	});
	$('input.text-input').blur(function()
	{
		$(this).css({backgroundColor:"#FFFFFF"});
	});

	///////////////////////////////////////////////////
	// chat handle button clicked...
	$('.ch_button').click(function()
	{
		var chatHandle = $('input#chatHandle').val();
		if (chatHandle == "")
		{
			$('input#chatHandle').focus();
			return false;
		}
		var dataString = 'chatHandle=' + chatHandle;

		$.ajax(
		{
			type: 'POST',
			url: '/echosystem/bin/changechathandle.php',
			data: dataString,
			dataType: 'json',
			success: function(data)
			{
				if (data.success == "no") // could not create the chat handle... let the gummie know
					displayError('#chatHandleForm','#chatHandleMessage',data.err,'input#chatHandle');
				else
				{
					$('#mask').hide();
					$('.window').hide();
					window.location.reload();
				}
			} // end success:
		}); // end .ajax()
		return false;
	}); // end ch_button.click()

	////////////////////////////////////////////////////
	// Login button clicked...
	$('.button').click(function()
	{
		// validate and process form
		// first hide any error messages
		// $('.error').hide();

		var email = $("input#email").val();
		if (email == "")
		{
			// $("label#email_error").show();
			$("input#email").focus();
			return false;
		}
		var password = $("input#password").val();
		if (password == "")
		{
			// $("label#password_error").show();
			$("input#password").focus();
			return false;
		}

		var dataString = 'email=' + email + '&password=' + password;

		$.ajax(
		{
			type: "POST",
			url: "/echosystem/bin/processlogin.php",
			data: dataString,
			dataType: "json",
			success: function(data)
			{
				if (data.success == "no") // could not process the login...
				{
					displayError('#contact_form','#loginMessage',data.err,'input#email');
					/*
					$('#chatHandleForm').fadeOut('fast', function()
					{
						$('#chatHandleMessage').html(data.err).show().fadeIn('fast', function()
						{
							$('#chatHandleMessage').delay(2000).fadeOut('fast', function() {
								$('#chatHandleForm').fadeIn('fast', function() {
									$('input#chatHandle').select().focus();
								});
							});
						});
					});
					*/
				}
				else if (data.success == "yes")
				{
					$('#contact_form').fadeOut('fast', function()
					{
						$('#loginMessage').removeClass("message").addClass('loadingMsg');
						$('#loginMessage').html('<div align="center"><p>Initializing your session...</p><p><img src="/images/looploader.gif" /></p></div>' 
							// + '<iframe src="http://www.' + data.nextwebsite + '.com/atlogin.php?sid=' + data.session + '&s=1" width="340" height="1" scrolling="no" frameborder="0" id="loginFrame" name="loginFrame"></iframe>'
							).show();
					});
					setTimeout('checkLoginStatus()', 1000);
				}
			}
		});
	return false;
});
	return false;
});

var g_refreshTimes = 0;
function checkLoginStatus()
{
	/*
	var oIframe = document.getElementById("loginFrame"); // window.frames["loginFrame"];
	var oDoc = (oIframe.contentWindow || oIframe.contentDocument);
	if (oDoc.document) oDoc = oDoc.document;
	if (!oDoc.body)
	{
		if (++g_refreshTimes >= 4)
		{
			alert("Timeout while trying to log you in.  Please try again later.");
			$('#loginMessage').hide().fadeOut('fast', function()
				{ $('#contact_form').show().fadeIn('fast'); });
			return false;
		}
		setTimeout("checkLoginStatus()", 2000);
	}
	else
	{
		var success = oDoc.body.innerHTML;
		if (success != "SUCCESS")
		{
			$('#loginMessage').html('<p>There was an error while trying to log you in.  <a href="/contact.php">Contact Us</a> and let us know there was an issue.</p>');
		}
		else
		{
			$('#mask').hide();
			$('.window').hide();
			$('#loginMessage').hide();
			$('#contact_form').show();
			window.location.reload();
		}
	}
	*/

		// NOTE: comment this out and uncomment the above code to do multiple website logins. the code works, it just doesn't work for all browsers.
		$('#mask').hide();
		$('.window').hide();
		$('#loginMessage').hide();
		$('#contact_form').show();
		window.location.reload();
}

/*
runOnLoad(function(){
	$("input#email").select().focus();
});
*/
function displayError(fadeOutItem, fadeInItem, errorMsg, focusTarget)
{
	$(fadeOutItem).fadeOut('fast', function()
	{
		$(fadeInItem).html(errorMsg).show().fadeIn('fast', function()
		{
			$(fadeInItem).delay(2000).fadeOut('fast', function() {
				$(fadeOutItem).fadeIn('fast', function() {
					$(focusTarget).select().focus();
				});
			});
		});
	});
}

