/**
 * JS
 * @author Darío Ruellan <druellan@ecimtech.com>
 * @copyright ecimTech 2008
 */

$(function()
{
	var $body = $("#body");
	
	// Zebra
	$("#testarea fieldset:odd").addClass("odd");
	
	// resaltamos los radio/checkboxes seleccionados
	$("form.test input[type=radio]", $body).click(function()
	{
		$("form.test input[type=radio]", $body).next("label").removeClass("checked");
		$("form.test input[type=radio]:checked", $body).next("label").addClass("checked");
	});
	
	// resaltamos textarea
	$("form.test textarea", $body).focus(function()
	{
		$(this).addClass("focus").animate({height: '96px'});
	});
	$("form.test textarea", $body).blur(function()
	{
		$(this).removeClass("focus").stop(true, true).animate({height: '48px'});
	});
	
	// Inicializamos
	var $slide = $("#form_slide");
	
	$(".page", $slide).hide();
	$(".page.p1", $slide).show();

	// Animación de slide

	$(".next", $slide).click(function()
	{
		var check = testCheck(this);
			
		if (check) 
		{
			$page = $(this).parents("div.page"); 
			$page.slideUp("normal", function()
			{
				$page.next("div.page")
				.slideDown("normal");
			});
		}
		return false;
	});
	
	$(".prev", $slide).click(function()
	{
		$page = $(this).parents("div.page"); 
		$page.slideUp("normal", function()
		{
			$page.prev("div.page")
			.slideDown("normal");
		})
		

		return false;
	});
	
	$(".submit", $slide).click(function()
	{
		return testCheck(this);
	});
});


function testCheck(obj)
{
	var check = true;
	var $activearea = $(obj).parents("div.page");
	$activearea.find("fieldset").each(function()
	{
		$(this).removeClass("error");
		if ( 
			!$(this).find("input[type=radio]:checked").length
			&& !$(this).find('option:selected[value!=""]').length
			&& !$(this).find("input[type=text]").val()
			&& !$(this).find("textarea").val()
			) 
		{
			check = false;
			$(this).addClass("error");
		}
	});

	if ( check )
	{
		$(".error_msg", $activearea).removeClass("error").hide();
		return true;
	}
	else
	{
		if ( $(".error_msg", $activearea).hasClass("error") )
		{
			$(".error_msg", $activearea).fadeOut("fast", function()
			{
				$(this).fadeIn("fast");
			});
		}
		else
		{
			$(".error_msg", $activearea).addClass("error").slideDown();
		}
		return false;
	}
}
