$(document).ready(bloglogin);
var click = false;

function bloglogin()
{
    var login = $('div.logueate form.login input[name=user_blog]').val();
    var password = $('div.logueate form.login input[name=pass_blog]').val();

	$('div.logueate div.login a.salir').click(function (e) {
        e.preventDefault();
		var dir = window.location.href;
		dir = dir.replace('#','');
        var salir = $('<form id="blog_logout" method="post" action="'+dir+'"><input type="hidden" name="logout" value="1" /></form>');
        $('body').append(salir);
        $('form#blog_logout').submit();
		//window.location.href = dir+"?logout=1";
	});

	$('div.logueate form.comenta textarea').one("click", function () {
		if ($(this).attr('change') != '1')
		{
			$(this).text('');
            $(this).val('');
			$(this).attr('change','1');
		}
	});

	$('div.logueate form.comenta textarea').one("keyup", function () {
		if ($(this).attr('change') != '1')
		{
			$(this).attr('change','1');
		}
	});

	$('div.logueate form.comenta a.enviar').bind("click", function (e) {
		e.preventDefault();
		form = $(this).closest('form');
		comenta(form);
	});

	$('div.logueate form.login input').bind("click", function () {
		if ($(this).attr('change') != '1')
		{
			$(this).val('');
		}
	});

    $('div.logueate form.login input').blur(function () {
        if ($(this).attr('change') != '1')
        {
            if ($(this).attr('name') == 'user_blog')
                $(this).val(login);
            else
                $(this).val(password);
        }
    });

	$('div.logueate form.suscribe input').bind("click", function () {
		if ($(this).attr('change') != '1')
		{
            if ($.trim($(this).attr('default')) == '') $(this).attr('default',$(this).val());
			$(this).val('');
		}
	});

    $('div.logueate form.suscribe input').blur(function () {
        if ($(this).attr('change') != '1')
        {
            $(this).val($(this).attr('default'));
        }
    });

	$('div.logueate form.suscribe textarea').bind("click", function () {
		if ($(this).attr('change') != '1')
		{
            if ($.trim($(this).attr('default')) == '') $(this).attr('default',$(this).val());
			$(this).text('');
            $(this).val('');
		}
	});

    $('div.logueate form.suscribe textarea').blur(function () {
        if ($(this).attr('change') != '1')
        {
            $(this).val($(this).attr('default'));
            $(this).text($(this).attr('default'));
        }
    });

	$('div.logueate form.login input').one("keyup", function () {
		if ($(this).attr('change') != '1')
		{
			$(this).attr('change','1');
		}
	});

	$('div.logueate form.suscribe input').one("keyup", function () {
		if ($(this).attr('change') != '1')
		{
			$(this).attr('change','1');
		}
	});

	$('div.logueate form.suscribe textarea').one("keyup", function () {
		if ($(this).attr('change') != '1')
		{
			$(this).attr('change','1');
		}
	});

	$('div.logueate form.suscribe a.enviar').click(function (e) {
		e.preventDefault();
		var form = $(this).closest('form');
		doSuscripcion(form);
	});

	$('div.logueate form.login a.enviar').click(function (e) {
		e.preventDefault();
		var form = $(this).closest('form');
		doLogin(form);
	});

    $('div.logueate form.login a.contrasenia').click(function (e) {
        e.preventDefault();
        var form = $(this).closest('form');
        var user = form.find('input[name=user_blog]').val();
        var dir = window.location.href;
        var olvidado = $('<form id="blog_olvidado" method="post" action="'+dir+'"><input type="hidden" name="user_contrasenia_olvidada" value="'+user+'" /></form>');
        $('body').append(olvidado);
        $('form#blog_olvidado').submit();
    });

	var yaexiste = $('div.logueate form.suscribe input[yaexiste=true]').attr('value2');
	$('div.logueate form.suscribe input[yaexiste=true]').css('border','red solid 2px').val(yaexiste);
}

function doLogin(form)
{
    var activo = true;

    form.find('[req=y]').each(function () {
        var valor = $(this).val();
        if ($.trim(valor) == '' || $(this).attr('change') == '0')
        {
            $(this).css('border','red solid 2px');
            activo = false;
        }
        else
        {
            $(this).css('border','');
        }
    });
    if (activo)
    {
        form.submit();
    }
}

function doSuscripcion(form)
{
	var activo = true;

	form.find('[req=y]').each(function () {
		var valor = $(this).val();
		if ($.trim(valor) == '' || $(this).attr('change') == '0')
		{
			$(this).css('border','red solid 2px');
			activo = false;
		}
		else
		{
			$(this).css('border','');
		}
		if ($(this).is('.email'))
		{
			if (!mail($(this).val()))
			{
				$(this).css('border','red solid 2px');
				activo = false;
			}
			else
			{
				$(this).css('border','');
			}
		}
	});
	if ($.trim(form.find('input[name=contrasenia]').val()) != $.trim(form.find('input[name=repite-contrasenia]').val()))
	{
		form.find('input[name=contrasenia]').css('border','red solid 2px');
		form.find('input[name=repite-contrasenia]').css('border','red solid 2px');
	}
	if (activo)
	{
		if (form.find('input.file').attr('change') == '0')
			form.find('input.file').val('');
		form.submit();
	}
}

function comenta(form)
{
	var activo = true;

	form.find('[req=y]').each(function () {
		var valor = $(this).val();
		if ($.trim(valor) == '' || $(this).attr('change') == '0')
		{
			$(this).css('border','red solid 2px');
			activo = false;
		}
		else
		{
			$(this).css('border','');
		}
	});
	if (activo)
	{
		form.submit();
	}
}

function mail(txt)
{
	var s = txt;
	var filter=/^[A-Za-z]+[.]*[A-Za-z0-9_]*@[A-Za-z0-9_]+.[A-Za-z0-9_.]+[A-za-z]$/;
	if (s.length == 0 ) return false;

	if (filter.test(s))
		return true;
	else
		return false;
	return false;
}
