$(function() {

	if($('html').height() > $('body').height()) {
		$('body').height($('html').height());
	}
	$(window).resize(function(){
		if($('html').height() > $('body').height()) {
			$('body').height($('html').height());
		}
	});

	
	$("#loginform").submit(checkLoginForm);
    
    hideEmails();

});

function checkLoginForm() {
	var execute_submit = true;
	
	// Validate the fields
	if( ! checkValidLoginField('#password')) {
		execute_submit = false;
	}
	if( ! checkValidLoginField('#username')) {
		execute_submit = false;
	}
	
	// If valid, do a pre-get
	if(execute_submit) {
		$.get(	
				"alta_checklogin.php", 
				{ u: $('#username').val(), pw: $('#password').val() },
				function(data) {
					// If valid, return true
					if(data == 1) {
						$("#loginform").unbind('submit').submit();
					} else {
						alert(INVALID_LOGIN_MESSAGE);
					}

				}
			);
	}
	return false;
}

function checkValidLoginField(field) {
	if($(field).val() == '') {
		$(field)
			.css({backgroundColor : '#ffeff2'})
			.keyup(
				function() {
					fixLoginBgColor($(this));
				}
			);
		return false;
	} else {
		$(field).css({backgroundColor : '#fff'});			
		return true;
	}
}

function fixLoginBgColor(input) {
	if($(input).val() != '') {
		$(input)
			.css({ backgroundColor : '#fff' })
			.unbind('keypress');	
	}
}

function hideEmails() {
    var spt = $('a.mailme');
    var at = / (at|arroba) /;
    var dot = / (dot|punto) /g;
    var addr = $(spt).text().replace(at,"@").replace(dot,".");
    $(spt).attr('href', 'mailto:'+addr)
        .text(addr)
        .hover(function(){window.status="Send a letter!";}, function(){window.status="";});
}