document.write( '<script type="text/javascript" src="/lib/js/fade-0.1-z.js"></script>' );

$(document).ready( function() {
	var imageBusy = new Image(); imageBusy.src = "/lib/img/busy2.gif"; // preload image
	document.getElementById('nojs').innerHTML = ""; // remove the no javascript message for people who have javascript
	setOpacity( 'divLogin', 0 ); // hide the JavaScript form, required to prevent flicker when first shown
} );
function login( f ) {
	if( f.username.value.length == 0 || f.password.value.length == 0 ) {
		alert( "Please enter your username and password." );
		return false;
	} else {
		document.getElementById('divBusy').innerHTML = '<img id="imgBusy" src="/lib/img/busy2.gif" width="16" height="16" alt="busy">';
		$.post( 
			'/login.php',
			{ 'username': f.username.value, 'password': f.password.value },
			function( data, textStatus ) {
				//alert( data + " " + textStatus );
				document.getElementById('divBusy').innerHTML = '';
				if( data == 'DISABLED' ) {
					alert( "Your account has been disabled." );
				} else if( data != 1 ) {
					alert( "The username or password you entered is incorrect." );
				} else if( data == 1 ) {
					window.location.href = '/app/';
				}
			},
			"text"
		);
	}
}

// when user presses the login tab, toggle the visibility of the login form and the highlighting of the login tab
function loginTab( divLogin ) {
	var tabStyle = document.getElementById('aLogin').style; // login tab
	var opacity = getOpacity( divLogin ); // login form
	if( opacity > 0 ) { // if visible, then hide it and reset login tab
		fadeOpacity( divLogin, 100, 0, 400); // fade form out of view
		// unhighlight login tab
		tabStyle.color = '#666'; tabStyle.backgroundColor = '#fff';
		setTimeout( "document.getElementById('divLogin').innerHTML = \"\";", 400); // remove the form after is has faded
	} else { // show login form and highlight login tab
		// generate form
		document.getElementById('divLogin').innerHTML = "<form name=\"formLogin\" action=\"javascript:void(0);\" method=\"post\"><table align=\"right\">"
			+ "<tr><td>username</td><td>password</td><td>&nbsp;</td></tr>"
			+ "<tr><td><input id=\"username\" class=\"inputText\" name=\"username\" type=\"text\" size=\"30\" maxlength=\"50\"/></td><td><input id=\"password\" class=\"inputText\" name=\"password\" type=\"password\" size=\"10\" maxlength=\"40\"/></td><td><input class=\"login\" type=\"button\" name=\"submitButton\" value=\"login\" onclick=\"login( document.formLogin );\"/></td></tr>"
			+ "<tr><td><div id=\"divBusy\"></div></td><td class=\"smlR\" colspan=\"2\"><a class=\"line\" href=\"javascript:cantremember( document.formLogin );\">can't remember?</a></td></tr>"
			+ "</table></form>";
		// if user presses return key in the username or password fields, then act as if user pressed login button
		$("input").keypress( function( e ) {
			if( e.which == 13 ) {
				login( document.formLogin );
			}
		} );
		// fade form into view
		fadeOpacity( divLogin, 0, 100, 400);
		document.formLogin.username.focus();
		// highlight login tab
		tabStyle.color = '#fff'; tabStyle.backgroundColor = '#f90';
	}
}

// called when the user presses the "can't remember" link
function cantremember( f ) {
	if( f.username.value.length == 0 ) {
		alert( "Please enter your email address in the username field then\nclick the \"can't remember\" link again to initiate your password reset." );
	} else {
		document.getElementById('divBusy').innerHTML = '<img id="imgBusy" src="/lib/img/busy2.gif" width="16" height="16" alt="busy">';
		$.post( 
			'/cantremember.php', 
			{ 'username': f.username.value },
			function( data, textStatus ) {
				document.getElementById('divBusy').innerHTML = '';
				switch( 1*data ) {
					case 0: alert( "We do not have "+f.username.value+" in our records." ); break;
					case 1:	alert( "A message has been emailed to "+f.username.value+" with further instructions." ); break;
					case 2: alert( "There was a problem emailing you a message.  Our support team has been notified.  Please try again later." ); break;
				}
			},
			"text"
		);
	}
}
